【问题标题】: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)Tensor("conv2d_1/kernel:0", shape=(9, 9, 1, 64), dtype=float32_ref) 必须与 Tensor("input_1:0", shape=(?, 352, 288, 1)
【发布时间】:2019-11-24 13:17:36
【问题描述】:

我正在尝试应用 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

Traceback(最近一次调用最后一次):

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

【问题讨论】:

  • 错误不是关于形状而是关于不同的图表,你为什么要重置图表?这就引入了这个问题。
  • @MatiasValdenegro 我删除了图重置,模型可以编译了,谢谢!

标签: python keras keras-layer


【解决方案1】:

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

这是因为您使用的是tf.reset_default_graph(),我认为根本不需要它,请尝试不使用它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-10-23
    • 2018-04-25
    • 2019-04-22
    • 1970-01-01
    • 2020-06-09
    • 2019-09-17
    • 2020-08-06
    相关资源
    最近更新 更多