我在运行tensorflow代码时遇到了这个错误,为什么?

时间:2018-01-15 13:27:29

标签: python image tensorflow neural-network

我在网上搜索了很长时间。但我什么都没发现。请帮助我或尝试给我一些关于如何实现这一点的想法。 总而言之:

import tensorflow as tf
import numpy as np


def model(input_tensor):
with tf.device("/gpu:0"):
    weights = []
    biases = []
    tensor = None

    conv_00_w = tf.get_variable("conv_00_w", [3, 3, 1, 64],
                                initializer=tf.random_normal_initializer(stddev=np.sqrt(2.0 / 9)))
    conv_00_b = tf.get_variable("conv_00_b", [64], initializer=tf.constant_initializer(0))
    weights.append(conv_00_w)
    biases.append(conv_00_b)
    low_tensor = tf.nn.relu(
        tf.nn.bias_add(tf.nn.conv2d(input_tensor, conv_00_w, strides=[1, 1, 1, 1], padding='SAME'), conv_00_b))

    for i in range(18):
        conv_w = tf.get_variable("conv_d_w" % (i + 1), [3, 3, 64, 64],
                                 initializer=tf.random_normal_initializer(stddev=np.sqrt(2.0 / 9 / 64)))
        conv_b = tf.get_variable("conv_d_b" % (i + 1), [64], initializer=tf.constant_initializer(0))
        weights.append(conv_w)
        biases.append(conv_b)
        tensor = tf.nn.relu(
            tf.nn.bias_add(tf.nn.conv2d(low_tensor, conv_w, strides=[1, 1, 1, 1], padding='SAME'), conv_b))

    tensor = tf.concat([tensor, low_tensor],3)
    for j in range(2):
        deconv_w = tf.get_variable("deconv_d_w" % (j + 1), [2, 2, 128, 128],initializer=tf.random_normal_initializer(stddev=np.sqrt(2.0 / 4 / 128)))
        deconv_b = tf.get_variable("deconv_d_b" % (j + 1), [128], initializer=tf.constant_initializer(0))
        weights.append(deconv_w)
        biases.append(deconv_b)
        deconv_tensor = tf.nn.relu(
            tf.nn.bias_add(tf.nn.conv2d_transpose(tensor, deconv_w, [64,48*(j+1),48*(j+1),128], [1, 2, 2, 1], 'SAME'), deconv_b))

    conv_w = tf.get_variable("conv_20_w", [3, 3, 128, 1],
                             initializer=tf.random_normal_initializer(stddev=np.sqrt(2.0 / 9 / 128)))
    conv_b = tf.get_variable("conv_20_b", [1], initializer=tf.constant_initializer(0))
    weights.append(conv_w)
    biases.append(conv_b)
    tensor = tf.nn.bias_add(tf.nn.conv2d(deconv_tensor, conv_w, strides=[1, 1, 1, 1], padding='SAME'), conv_b)
    weights = weights + biases


    return tensor, weights
if __name__ == '__main__':
INPUT_SIZE = (24, 24)
OUTPUT_SIZE = (96, 96)
BATCH_SIZE = 64
train_input = tf.placeholder(tf.float32, shape=(BATCH_SIZE, INPUT_SIZE[0], INPUT_SIZE[1], 1))
train_gt = tf.placeholder(tf.float32, shape=(BATCH_SIZE, OUTPUT_SIZE[0], OUTPUT_SIZE[1], 1))

shared_model = tf.make_template('shared_model', model)

train_output, weights = shared_model(train_input)
loss = tf.reduce_sum(tf.nn.l2_loss(tf.subtract(train_output,train_gt)))

learning_rate =  0.001 
params = tf.trainable_variables()

grads = tf.gradients(loss, weights)
print("finished")

最后,我在运行上述内容时遇到了这个错误:

Traceback (most recent call last):
File "C:\Users\111\Anaconda3\lib\site-packages\tensorflow\python\framework\tensor_shape.py", line 560, in merge_with
new_dims.append(dim.merge_with(other[i]))
File "C:\Users\111\Anaconda3\lib\site-packages\tensorflow\python\framework\tensor_shape.py", line 135, in merge_with
self.assert_is_compatible_with(other)
File "C:\Users\111\Anaconda3\lib\site-packages\tensorflow\python\framework\tensor_shape.py", line 108, in assert_is_compatible_with
% (self, other))
ValueError: Dimensions 48 and 24 are not compatible

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "E:/Experiments/self-experiments/experiment1/MODEL.py", line 80, in <module>
grads = tf.gradients(loss, weights)
File "C:\Users\111\Anaconda3\lib\site-packages\tensorflow\python\ops\gradients_impl.py", line 580, in gradients
in_grad.set_shape(t_in.get_shape())
File "C:\Users\111\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py", line 413, in set_shape
self._shape = self._shape.merge_with(shape)
File "C:\Users\111\Anaconda3\lib\site-packages\tensorflow\python\framework\tensor_shape.py", line 564, in merge_with
(self, other))
ValueError: Shapes (64, 48, 48, 128) and (64, 24, 24, 128) are not compatible

我想使用两个deconv层进行上采样,正是使用了tf.nn.conv2d_transpose的问题。

0 个答案:

没有答案