I want to use learned weights with a tensor flow












1















I trained my neural network with a tensorflow and I learned 1,000,000 times.



Three files have been created in "C / folder". (meta, index and data files).



I would like to load only the my weight and bias.



Please look following code.



c_dim = 1
scale = 3
im = Image.open('test.bmp')
#shape of im is(256, 256, 3)
image_size_width, image_size_height = im.width, im.height
img = im.convert('YCbCr')
# I need only Y channel
# shape of img is (256, 256, 3)

arr_img = np.asarray(img)
arr_img = arr_img[:, :, 0]
#shape of arr_img is (256, 256)
arrimg = np.expand_dims(arr_img, 0)
arrimg = np.expand_dims(arrimg, 3)
# Tensorflow needs... [?, 256, 256, 1] so, i expand dimention of 'arrimg'

images = tf.placeholder(tf.float32, [None, image_size_width, image_size_height, c_dim], name='images')
# I define plachholder
w1 = tf.Variable(tf.random_normal([9, 9, 1, 64], stddev=1e-3), name='w1')
w2 = tf.Variable(tf.random_normal([1, 1, 64, 32], stddev=1e-3), name='w2')
w3 = tf.Variable(tf.random_normal([5, 5, 32, 1], stddev=1e-3), name='w3')
# I define weight
b1 = tf.Variable(tf.zeros([64]), name='b1')
b2 = tf.Variable(tf.zeros([32]), name='b2')
b3 = tf.Variable(tf.zeros([1]), name='b3')
# I define bias

conv1 = tf.nn.relu(tf.nn.conv2d(images, w1, strides=[1,1,1,1], padding='VALID') + b1)
conv2 = tf.nn.relu(tf.nn.conv2d(conv1, w2, strides=[1,1,1,1], padding='VALID') + b2)
result = tf.nn.conv2d(conv2, w3, strides=[1,1,1,1], padding='VALID') + b3
# After restoring the saved my weights, I want to put it into the calculation graph I want.

sess = tf.Session()
saver = tf.train.Saver()
saver.restore(sess, 'C:/folder/my.model-1000000')
saver.restore(sess, tf.train.latest_checkpoint('C:/folder/'))
#I restore my weight and bias

sess.run(tf.global_variables_initializer())
aa = sess.run(result, {images : arrimg})

#aa = aa[0,:,:,0]
print(type(aa))
# this is numpy array
print(np.shape(aa))
# (1, 244, 244, 1)
# I can not change this(shape of (1, 244, 244, 1)) to image!

aa = np.reshape(aa, (244, 244))
# so i change shape

resultimage = Image.fromarray(aa, 'L')
resultimage.save('C:/SRCNN/result.bmp')


However, there is only meaningless black and white image.



The tensorflow must have rank 4 for graph computation.



So I changed the dimensions of the original RGB image (256, 256, 3) at will.



Is it because I made a mistake in image processing?



Or did i make a mistake in how to restore weights and bias?










share|improve this question



























    1















    I trained my neural network with a tensorflow and I learned 1,000,000 times.



    Three files have been created in "C / folder". (meta, index and data files).



    I would like to load only the my weight and bias.



    Please look following code.



    c_dim = 1
    scale = 3
    im = Image.open('test.bmp')
    #shape of im is(256, 256, 3)
    image_size_width, image_size_height = im.width, im.height
    img = im.convert('YCbCr')
    # I need only Y channel
    # shape of img is (256, 256, 3)

    arr_img = np.asarray(img)
    arr_img = arr_img[:, :, 0]
    #shape of arr_img is (256, 256)
    arrimg = np.expand_dims(arr_img, 0)
    arrimg = np.expand_dims(arrimg, 3)
    # Tensorflow needs... [?, 256, 256, 1] so, i expand dimention of 'arrimg'

    images = tf.placeholder(tf.float32, [None, image_size_width, image_size_height, c_dim], name='images')
    # I define plachholder
    w1 = tf.Variable(tf.random_normal([9, 9, 1, 64], stddev=1e-3), name='w1')
    w2 = tf.Variable(tf.random_normal([1, 1, 64, 32], stddev=1e-3), name='w2')
    w3 = tf.Variable(tf.random_normal([5, 5, 32, 1], stddev=1e-3), name='w3')
    # I define weight
    b1 = tf.Variable(tf.zeros([64]), name='b1')
    b2 = tf.Variable(tf.zeros([32]), name='b2')
    b3 = tf.Variable(tf.zeros([1]), name='b3')
    # I define bias

    conv1 = tf.nn.relu(tf.nn.conv2d(images, w1, strides=[1,1,1,1], padding='VALID') + b1)
    conv2 = tf.nn.relu(tf.nn.conv2d(conv1, w2, strides=[1,1,1,1], padding='VALID') + b2)
    result = tf.nn.conv2d(conv2, w3, strides=[1,1,1,1], padding='VALID') + b3
    # After restoring the saved my weights, I want to put it into the calculation graph I want.

    sess = tf.Session()
    saver = tf.train.Saver()
    saver.restore(sess, 'C:/folder/my.model-1000000')
    saver.restore(sess, tf.train.latest_checkpoint('C:/folder/'))
    #I restore my weight and bias

    sess.run(tf.global_variables_initializer())
    aa = sess.run(result, {images : arrimg})

    #aa = aa[0,:,:,0]
    print(type(aa))
    # this is numpy array
    print(np.shape(aa))
    # (1, 244, 244, 1)
    # I can not change this(shape of (1, 244, 244, 1)) to image!

    aa = np.reshape(aa, (244, 244))
    # so i change shape

    resultimage = Image.fromarray(aa, 'L')
    resultimage.save('C:/SRCNN/result.bmp')


    However, there is only meaningless black and white image.



    The tensorflow must have rank 4 for graph computation.



    So I changed the dimensions of the original RGB image (256, 256, 3) at will.



    Is it because I made a mistake in image processing?



    Or did i make a mistake in how to restore weights and bias?










    share|improve this question

























      1












      1








      1








      I trained my neural network with a tensorflow and I learned 1,000,000 times.



      Three files have been created in "C / folder". (meta, index and data files).



      I would like to load only the my weight and bias.



      Please look following code.



      c_dim = 1
      scale = 3
      im = Image.open('test.bmp')
      #shape of im is(256, 256, 3)
      image_size_width, image_size_height = im.width, im.height
      img = im.convert('YCbCr')
      # I need only Y channel
      # shape of img is (256, 256, 3)

      arr_img = np.asarray(img)
      arr_img = arr_img[:, :, 0]
      #shape of arr_img is (256, 256)
      arrimg = np.expand_dims(arr_img, 0)
      arrimg = np.expand_dims(arrimg, 3)
      # Tensorflow needs... [?, 256, 256, 1] so, i expand dimention of 'arrimg'

      images = tf.placeholder(tf.float32, [None, image_size_width, image_size_height, c_dim], name='images')
      # I define plachholder
      w1 = tf.Variable(tf.random_normal([9, 9, 1, 64], stddev=1e-3), name='w1')
      w2 = tf.Variable(tf.random_normal([1, 1, 64, 32], stddev=1e-3), name='w2')
      w3 = tf.Variable(tf.random_normal([5, 5, 32, 1], stddev=1e-3), name='w3')
      # I define weight
      b1 = tf.Variable(tf.zeros([64]), name='b1')
      b2 = tf.Variable(tf.zeros([32]), name='b2')
      b3 = tf.Variable(tf.zeros([1]), name='b3')
      # I define bias

      conv1 = tf.nn.relu(tf.nn.conv2d(images, w1, strides=[1,1,1,1], padding='VALID') + b1)
      conv2 = tf.nn.relu(tf.nn.conv2d(conv1, w2, strides=[1,1,1,1], padding='VALID') + b2)
      result = tf.nn.conv2d(conv2, w3, strides=[1,1,1,1], padding='VALID') + b3
      # After restoring the saved my weights, I want to put it into the calculation graph I want.

      sess = tf.Session()
      saver = tf.train.Saver()
      saver.restore(sess, 'C:/folder/my.model-1000000')
      saver.restore(sess, tf.train.latest_checkpoint('C:/folder/'))
      #I restore my weight and bias

      sess.run(tf.global_variables_initializer())
      aa = sess.run(result, {images : arrimg})

      #aa = aa[0,:,:,0]
      print(type(aa))
      # this is numpy array
      print(np.shape(aa))
      # (1, 244, 244, 1)
      # I can not change this(shape of (1, 244, 244, 1)) to image!

      aa = np.reshape(aa, (244, 244))
      # so i change shape

      resultimage = Image.fromarray(aa, 'L')
      resultimage.save('C:/SRCNN/result.bmp')


      However, there is only meaningless black and white image.



      The tensorflow must have rank 4 for graph computation.



      So I changed the dimensions of the original RGB image (256, 256, 3) at will.



      Is it because I made a mistake in image processing?



      Or did i make a mistake in how to restore weights and bias?










      share|improve this question














      I trained my neural network with a tensorflow and I learned 1,000,000 times.



      Three files have been created in "C / folder". (meta, index and data files).



      I would like to load only the my weight and bias.



      Please look following code.



      c_dim = 1
      scale = 3
      im = Image.open('test.bmp')
      #shape of im is(256, 256, 3)
      image_size_width, image_size_height = im.width, im.height
      img = im.convert('YCbCr')
      # I need only Y channel
      # shape of img is (256, 256, 3)

      arr_img = np.asarray(img)
      arr_img = arr_img[:, :, 0]
      #shape of arr_img is (256, 256)
      arrimg = np.expand_dims(arr_img, 0)
      arrimg = np.expand_dims(arrimg, 3)
      # Tensorflow needs... [?, 256, 256, 1] so, i expand dimention of 'arrimg'

      images = tf.placeholder(tf.float32, [None, image_size_width, image_size_height, c_dim], name='images')
      # I define plachholder
      w1 = tf.Variable(tf.random_normal([9, 9, 1, 64], stddev=1e-3), name='w1')
      w2 = tf.Variable(tf.random_normal([1, 1, 64, 32], stddev=1e-3), name='w2')
      w3 = tf.Variable(tf.random_normal([5, 5, 32, 1], stddev=1e-3), name='w3')
      # I define weight
      b1 = tf.Variable(tf.zeros([64]), name='b1')
      b2 = tf.Variable(tf.zeros([32]), name='b2')
      b3 = tf.Variable(tf.zeros([1]), name='b3')
      # I define bias

      conv1 = tf.nn.relu(tf.nn.conv2d(images, w1, strides=[1,1,1,1], padding='VALID') + b1)
      conv2 = tf.nn.relu(tf.nn.conv2d(conv1, w2, strides=[1,1,1,1], padding='VALID') + b2)
      result = tf.nn.conv2d(conv2, w3, strides=[1,1,1,1], padding='VALID') + b3
      # After restoring the saved my weights, I want to put it into the calculation graph I want.

      sess = tf.Session()
      saver = tf.train.Saver()
      saver.restore(sess, 'C:/folder/my.model-1000000')
      saver.restore(sess, tf.train.latest_checkpoint('C:/folder/'))
      #I restore my weight and bias

      sess.run(tf.global_variables_initializer())
      aa = sess.run(result, {images : arrimg})

      #aa = aa[0,:,:,0]
      print(type(aa))
      # this is numpy array
      print(np.shape(aa))
      # (1, 244, 244, 1)
      # I can not change this(shape of (1, 244, 244, 1)) to image!

      aa = np.reshape(aa, (244, 244))
      # so i change shape

      resultimage = Image.fromarray(aa, 'L')
      resultimage.save('C:/SRCNN/result.bmp')


      However, there is only meaningless black and white image.



      The tensorflow must have rank 4 for graph computation.



      So I changed the dimensions of the original RGB image (256, 256, 3) at will.



      Is it because I made a mistake in image processing?



      Or did i make a mistake in how to restore weights and bias?







      python image tensorflow






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 14 '18 at 18:00









      ddjfjfj djfiejdnddjfjfj djfiejdn

      444




      444
























          0






          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53306252%2fi-want-to-use-learned-weights-with-a-tensor-flow%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53306252%2fi-want-to-use-learned-weights-with-a-tensor-flow%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Florida Star v. B. J. F.

          Danny Elfman

          Lugert, Oklahoma