我试图通过向图像显示输出来测试我的CNN。
源代码来自https://xueyangfu.github.io/projects/tip2017.html
我运行原始代码很有用。
然后我改变了CNN的一些结构。我添加了一些图层并更改了一些参数。
培训是正确的,我使用一些图像来测试模型。
我尝试打印输出ndarray,它包含正确的值。
但是,cv2或plt显示的图像在每个像素中都是相同的深色。 输出ndarray和输入ndarray的形状和类型都是(高度,宽度,3)相同,但输出ndarray的值不能写入图像。
我读取了输出ndarray保存的纯色图像。它的形状变成(高度,宽度,4)。它在每个数组中添加1.的值,前三个的值与我的输出ndarray不同。
我在这个奇怪的问题上浪费了大约一个星期。请给我一些关于如何解决或问题的建议。
感谢您的帮助。
以下是测试代码:
import os
import training_0116 as DerainNet
import tensorflow as tf
import matplotlib.pyplot as plt
import matplotlib.image as img
import numpy as np
import cv2
##################### Select GPU device ####################################
os.environ['CUDA_VISIBLE_DEVICES'] = "0"
file = "h.jpg"
ori = img.imread(file)
ori = ori/255.0
detail = ori
details = np.zeros([ori.shape[0], ori.shape[1], ori.shape[2]])
for j in range(3):
tmp = detail[:,:,j]
details[:,:,j] = np.pad(tmp, 0, 'symmetric')
details = np.expand_dims(details[:,:,:], axis = 0)
image = tf.placeholder(tf.float32, shape=(1, details.shape[1], details.shape[2], details.shape[3]))
out = DerainNet.inference(image)
saver = tf.train.Saver()
config = tf.ConfigProto()
config.gpu_options.per_process_gpu_memory_fraction = 0.5
config.gpu_options.allow_growth = True
with tf.Session(config=config) as sess:
if tf.train.get_checkpoint_state('./model/'):
ckpt = tf.train.latest_checkpoint('./model/')
saver.restore(sess, ckpt)
print ("load new model")
else:
saver.restore(sess, "./model/test-model/model") # this model uses 128 feature maps and for debug only
print ("load pre-trained model")
detail_out = sess.run(out, feed_dict={image:details})
derained = detail_out[0,:, :, :]
derained[np.where(derained < 0. )] = 0.
derained[np.where(derained > 1. )] = 1.
img.imsave("h_11.jpg",derained)
plt.subplot(1,2,1)
plt.imshow(ori)
plt.title('input')
plt.subplot(1,2,2)
plt.imshow(derained)
plt.title('output')
plt.show()
以下是培训代码的一部分:
def inference(images):
# conv1
with tf.variable_scope('conv_1'):
kernel = tf.Variable(tf.random_normal([16, 16, FLAGS.num_channels, FLAGS.num_feature], dtype=tf.float32, stddev=1e-3), trainable=True, name='weights1')
biases = tf.Variable(tf.constant(0.0, shape=[FLAGS.num_feature], dtype=tf.float32), trainable=True, name='biases1')
conv = tf.nn.conv2d(images, kernel, [1, 1, 1, 1], padding='SAME')
bias = tf.nn.bias_add(conv, biases)
conv1 = tf.nn.tanh(bias)
pool1 = tf.nn.max_pool(conv1, ksize=[1,3,3,1], strides=[1,1,1,1],padding='SAME')
# conv2
with tf.variable_scope('conv_2'):
kernel = tf.Variable(tf.random_normal([1, 1, FLAGS.num_feature, FLAGS.num_feature], dtype=tf.float32, stddev=1e-3), trainable=True, name='weights2')
biases = tf.Variable(tf.constant(0.0, shape=[FLAGS.num_feature], dtype=tf.float32), trainable=True, name='biases2')
conv = tf.nn.conv2d(pool1, kernel, [1, 1, 1, 1], padding='SAME')
bias = tf.nn.bias_add(conv, biases)
conv2 = tf.nn.tanh(bias)
pool2 = tf.nn.max_pool(conv2, ksize=[1,3,3,1], strides=[1,1,1,1],padding='SAME')
with tf.variable_scope('conv_3'):
kernel = tf.Variable(tf.random_normal([4, 4, FLAGS.num_feature, FLAGS.num_feature], dtype=tf.float32, stddev=1e-3), trainable=True, name='weights2')
biases = tf.Variable(tf.constant(0.0, shape=[FLAGS.num_feature], dtype=tf.float32), trainable=True, name='biases2')
conv = tf.nn.conv2d(pool2, kernel, [1, 1, 1, 1], padding='SAME')
bias = tf.nn.bias_add(conv, biases)
conv3 = tf.nn.tanh(bias)
pool3 = tf.nn.max_pool(conv3, ksize=[1,3,3,1], strides=[1,1,1,1],padding='SAME')
with tf.variable_scope('conv_4'):
kernel = tf.Variable(tf.random_normal([6, 6, FLAGS.num_feature, FLAGS.num_feature], dtype=tf.float32, stddev=1e-3), trainable=True, name='weights2')
biases = tf.Variable(tf.constant(0.0, shape=[FLAGS.num_feature], dtype=tf.float32), trainable=True, name='biases2')
conv = tf.nn.conv2d(pool3, kernel, [1, 1, 1, 1], padding='SAME')
bias = tf.nn.bias_add(conv, biases)
conv4 = tf.nn.tanh(bias)
pool4 = tf.nn.max_pool(conv4, ksize=[1,3,3,1], strides=[1,1,1,1],padding='SAME')
with tf.variable_scope('conv_6'):
kernel = tf.Variable(tf.random_normal([8, 8, FLAGS.num_channels,FLAGS.num_feature], dtype=tf.float32, stddev=1e-3), trainable=True, name='weights3')
biases = tf.Variable(tf.constant(0.0, shape=[FLAGS.num_channels], dtype=tf.float32), trainable=True, name='biases3')
conv = tf.nn.conv2d_transpose(conv4,kernel,[tf.shape(images)[0], tf.shape(images)[1], tf.shape(images)[2], FLAGS.num_channels],[1, 1, 1, 1],padding='SAME')
conv5 = tf.nn.bias_add(conv, biases)
out = tf.nn.max_pool(conv5, ksize=[1,3,3,1], strides=[1,1,1,1],padding='SAME')
return out
答案 0 :(得分:0)
这一行:
ori = ori/255.0
在读取图像时将图像标准化。正常像素的值为0到255.对于训练,这些通常标准化为介于0和1之间。但是如果要查看图像,您将拥有再次放大像素值。它是黑色的,因为接近零的像素值非常暗。
我想你想要一个相应的
ori = ori*255
就在你的表演之前。