Tensor(“ conv2d_1 / kernel:0”,shape =(9,9,1,64),dtype = float32_ref)必须与Tensor(“ input_1:0”,shape =(?, 352,288, 1)

时间:2019-07-15 20:04:40

标签: python keras keras-layer

我正在尝试应用keras功能模型。我不太明白为什么输入大小与卷积层不兼容。

我试图使用顺序模型将插件作为一个功能层,但是问题仍然存在。

# if input shape = (TARGET_HEIGHT, TARGET_WIDTH, 1),
#     You must feed a value for placeholder tensor 'input_1' with dtype float and shape [?,352,288,1]
# if input shape = (AMOUNT, TARGET_HEIGHT, TARGET_WIDTH),
#     number of input channels does not match corresponding dimension of filter, 288 != 1
# if input shape = (TARGET_HEIGHT, TARGET_WIDTH, AMOUNT)
#     number of input channels does not match corresponding dimension of filter, 300 != 1
# if input shape = (AMOUNT, TARGET_HEIGHT, TARGET_WIDTH, 1),
#     input tensor must have rank 4
# if shape = (AMOUNT, TARGET_HEIGHT, TARGET_WIDTH), data_format='channels_last'
#     Input() got an unexpected keyword argument 'data_format'

如果我使用功能模型来实现,编译器会说SRCNN1转换层的张量必须与输入张量来自同一张图。

def base_model_SRCNN(FILENAME, HEIGHT, WIDTH):

    par = load_parameter(FILENAME)  #load transfer learning parameters

    model = Sequential()

    model.add(Conv2D(64, (9,9), padding = 'same', activation ='relu', use_bias = True,
    input_shape=(HEIGHT,WIDTH, 1), trainable = False))
    model.add(Conv2D(32, (1, 1), padding='same', activation='relu', use_bias = True, trainable = True))
    model.add(Conv2D(1, (5, 5), padding='same', activation = 'relu', use_bias = True, trainable = True))

    for i in range(3):
        model.layers[i].set_weights(par[i])

    print(model.summary())

    model.compile(loss = 'mean_squared_error', optimizer = adam(lr=0.0005, decay=1e-6), metrics=[ssim_for2d, psnr_for2d])

    return model


def combined(FILENAME, AMOUNT, DEPTH, TARGET_HEIGHT, TARGET_WIDTH):
    ip = Input(shape = (TARGET_HEIGHT, TARGET_WIDTH, 1))

    tf.reset_default_graph()
    #SRCNN_network = base_model_SRCNN(FILENAME, TARGET_HEIGHT, TARGET_WIDTH) (ip)  

    SRCNN1 = Conv2D(64, (9, 9), padding = 'same', activation ='relu', use_bias = True, 
     data_format='channels_last', trainable = False) (ip)  #input_shape=(TARGET_HEIGHT, TARGET_WIDTH, 1),

    SRCNN2 = Conv2D(32, (1, 1), padding='same', activation='relu', use_bias = True, trainable = True) (SRCNN1)

    SRCNN3 = Conv2D(1, (5, 5), padding='same', activation = 'relu', use_bias = True, trainable = True) (SRCNN2)

model = combined(FILENAME, AMOUNT, DEPTH, TARGET_HEIGHT, TARGET_WIDTH)

#AMOUNT, TARGET_HEIGHT, TARGET_WIDTH = 300, 352, 288

回溯(最近通话最近一次):

File "main.py", line 71, in <module>
    model = combined(FILENAME, AMOUNT, DEPTH, TARGET_HEIGHT, TARGET_WIDTH)
  File "/home/user1/REUS/image-reconstruction/code/functional/model_build_up.py", line 51, in combined
    data_format='channels_last', trainable = False) (ip)  #input_shape=(TARGET_HEIGHT, TARGET_WIDTH, 1),
  File "/home/user1/.conda/envs/tf-gpu/lib/python3.7/site-packages/keras/engine/base_layer.py", line 457, in __call__
    output = self.call(inputs, **kwargs)
  File "/home/user1/.conda/envs/tf-gpu/lib/python3.7/site-packages/keras/layers/convolutional.py", line 171, in call
    dilation_rate=self.dilation_rate)
  File "/home/user1/.conda/envs/tf-gpu/lib/python3.7/site-packages/keras/backend/tensorflow_backend.py", line 3650, in conv2d
    data_format=tf_data_format)
  File "/home/user1/.conda/envs/tf-gpu/lib/python3.7/site-packages/tensorflow/python/ops/nn_ops.py", line 838, in convolution
    with ops.name_scope(name, "convolution", [input, filter]) as name:
  File "/home/user1/.conda/envs/tf-gpu/lib/python3.7/site-packages/tensorflow/python/framework/ops.py", line 6083, in __enter__
    g = _get_graph_from_inputs(self._values)
  File "/home/user1/.conda/envs/tf-gpu/lib/python3.7/site-packages/tensorflow/python/framework/ops.py", line 5713, in _get_graph_from_inputs
    _assert_same_graph(original_graph_element, graph_element)
  File "/home/user1/.conda/envs/tf-gpu/lib/python3.7/site-packages/tensorflow/python/framework/ops.py", line 5649, in _assert_same_graph
    original_item))
ValueError: Tensor("conv2d_1/kernel:0", shape=(9, 9, 1, 64), dtype=float32_ref) must be from the same graph as Tensor("input_1:0", shape=(?, 352, 288, 1), dtype=float32).

1 个答案:

答案 0 :(得分:0)

该错误告诉您有多个tensorflow计算图,并且您正在尝试合并来自不同图的操作,但这是行不通的。

发生这种情况是因为您正在使用tf.reset_default_graph(),我认为根本不需要它,请尝试使用它。

相关问题