简体   繁体   中英

I want to use learned weights with a tensor flow

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM