我想将学习到的权重与张量流一起使用

时间:2018-11-14 18:00:37

标签: python image tensorflow

我用张量流训练了我的神经网络,并且学习了1,000,000次。

在“ C /文件夹”中创建了三个文件。 (元,索引和数据文件)。

我只想负担我的体重和偏见。

请查看以下代码。

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')

但是,只有黑白图像没有意义。

张量流必须具有等级4才能进行图形计算。

所以我随意更改了原始RGB图像的尺寸(256、256、3)。

是因为我在图像处理中出错了吗?

还是我在如何减轻体重和偏见方面犯了错误?

0 个答案:

没有答案