在尝试实现DCGAN时,在使用我的训练函数时收到了这个错误消息:
ValueError: Cannot feed value of shape (40, 24, 24, 4) for Tensor u'real_images:0', which has shape '(40, 24, 24, 3)'
这个错误发生在使用以下这行代码时:
_,summary_str = self.sess.run([dis_optim, self.dis_sum],feed_dict = {self.inputs: batch_images, self.z: batch_z})
关于为什么张量形状不同,有什么想法吗?下面我附上了我认为错误所在的代码:
real_images占位符(self.colour_dim是3):
image_dimension = [self.input_H,self.input_H, self.colour_dim]self.inputs = tf.placeholder(tf.float32, shape=[self.batch_size] + image_dimension, name='real_images')
错误发生的代码段:
for idx in xrange(0, batch_idxs): batch_files = data[idx * config.batch_size:(idx +1) * config.batch_size] batch = [getImage(batch_file,resize_h=self.output_H,resize_w=self.output_W) for batch_file in batch_files] batch_images = np.array(batch).astype(np.float32) print(batch_images) batch_z = np.random.uniform(-1, 1, [config.batch_size, self.z_dimension]).astype(np.float32) # 判别器 _,summary_str = self.sess.run([dis_optim, self.dis_sum],feed_dict = {self.inputs: batch_images, self.z: batch_z}) self.writer.add_summary(summary_str, counter)
使用的getImage和imread函数:
def imread(path): return scipy.misc.imread(path).astype(np.float)def getImage(im_path, resize_h, resize_w): image = imread(im_path) return transform(image, resize_h, resize_w)
谢谢大家!
回答:
看起来你的图像加载函数返回的是RGBA图像,而网络期望的是RGB图像。在feed_dict中将batch_images
替换为batch_images[:,:,:,:3]
应该是一个简单的临时解决方案,你也可以查看你的加载函数是否支持直接返回RGB图像。