图断开:无法获得张量 Tensor("conv2d_1_input:0", shape=(?, 128, 128, 1), dtype=float32) 的值 [英] Graph disconnected: cannot obtain value for tensor Tensor("conv2d_1_input:0", shape=(?, 128, 128, 1), dtype=float32)
问题描述
我正在尝试实现一个自动编码器,它获得 3 个不同的输入并融合这三个图像.我想获得编码器中一个层的输出,并将它与解码器中的一个层连接起来,但是当我运行它时,我得到了图形断开连接的错误.这是我的代码:
I'm trying to implement an autoencoder which gets 3 different inputs and fuse this three image. I want to get the output of a layer in the encoder and concatenate it with a layer in the decoder but when I run it I get graph disconnected error. here is my code:
def create_model(input_shape):
input_1 = keras.layers.Input(input_shape)
input_2 = keras.layers.Input(input_shape)
input_3 = keras.layers.Input(input_shape)
network = keras.models.Sequential([
keras.layers.Conv2D(32, (7, 7), activation=tf.nn.relu, padding='SAME',input_shape=input_shape),
keras.layers.Conv2D(32, (7, 7), activation=tf.nn.relu, padding='SAME', name = 'a'),
keras.layers.AvgPool2D((2, 2)),
keras.layers.BatchNormalization(),
keras.layers.Dropout(0.3)])
encoded_1 = network(input_1)
encoded_2 = network(input_2)
encoded_3 = network(input_3)
a = network.get_layer('a').output
x = keras.layers.Concatenate()([encoded_1,encoded_2,encoded_3])
x = keras.layers.Conv2D(32, (3, 3), activation=tf.nn.relu, padding='SAME')(x)
x = keras.layers.UpSampling2D((2,2))(x)
x = keras.layers.BatchNormalization()(x)
x = keras.layers.Dropout(0.3)(x)
x = keras.layers.Concatenate()([x,a])
x = keras.layers.Conv2D(32, (3, 3), activation=tf.nn.relu, padding='SAME')(x)
x = keras.layers.UpSampling2D((2,2))(x)
x = keras.layers.BatchNormalization()(x)
x = keras.layers.Dropout(0.3)(x)
decoded = keras.layers.Conv2D(3, (3, 3), activation=tf.nn.relu, padding='SAME')(x)
final_net= keras.models.Model(inputs=[input_1,input_2,input_3],outputs=decoded)
return final_net
错误是:
图断开:无法在层conv2d_1_input"处获取张量 Tensor("conv2d_1_input:0", shape=(?, 128, 128, 1), dtype=float32) 的值.访问以下先前层没有问题:['input_6', 'input_5', 'input_4', 'sequential_1', 'sequential_1', 'sequential_1', 'concatenate', 'conv2d_2']
Graph disconnected: cannot obtain value for tensor Tensor("conv2d_1_input:0", shape=(?, 128, 128, 1), dtype=float32) at layer "conv2d_1_input". The following previous layers were accessed without issue: ['input_6', 'input_5', 'input_4', 'sequential_1', 'sequential_1', 'sequential_1', 'concatenate', 'conv2d_2']
这是因为连接了 [x,a].我试图从三个图像中获取图层的输出,例如:
and it is because of concatenating [x,a]. I've tried to get the output of layer from three images like:
encoder_1.get_layer('a').output
encoder_2.get_layer('a').output
encoder_3.get_layer('a').output
但我收到错误消息'Tensor' 对象没有属性 'output'"
推荐答案
如果需要获取a1
、a2
和a3,需要创建子网
输出.并且可以如下连接x
和a
.
You need to create a subnetwork if you need to get a1
, a2
and a3
outputs. And can connext x
and a
as follows.
def create_model(input_shape):
input_1 = keras.layers.Input(input_shape)
input_2 = keras.layers.Input(input_shape)
input_3 = keras.layers.Input(input_shape)
network = keras.models.Sequential([
keras.layers.Conv2D(32, (7, 7), activation=tf.nn.relu, padding='SAME',input_shape=input_shape),
keras.layers.Conv2D(32, (7, 7), activation=tf.nn.relu, padding='SAME', name = 'a'),
keras.layers.AvgPool2D((2, 2)),
keras.layers.BatchNormalization(),
keras.layers.Dropout(0.3)])
encoded_1 = network(input_1)
encoded_2 = network(input_2)
encoded_3 = network(input_3)
subnet = keras.models.Sequential()
for l in network.layers:
subnet.add(l)
if l.name == 'a': break
a1 = subnet(input_1)
a2 = subnet(input_2)
a3 = subnet(input_3)
x = keras.layers.Concatenate()([encoded_1,encoded_2,encoded_3])
a = keras.layers.Concatenate()([a1,a2,a3])
x = keras.layers.Conv2D(32, (3, 3), activation=tf.nn.relu, padding='SAME')(x)
x = keras.layers.UpSampling2D((2,2))(x)
x = keras.layers.BatchNormalization()(x)
x = keras.layers.Dropout(0.3)(x)
x = keras.layers.Concatenate()([x,a])
x = keras.layers.Conv2D(32, (3, 3), activation=tf.nn.relu, padding='SAME')(x)
x = keras.layers.UpSampling2D((2,2))(x)
x = keras.layers.BatchNormalization()(x)
x = keras.layers.Dropout(0.3)(x)
decoded = keras.layers.Conv2D(3, (3, 3), activation=tf.nn.relu, padding='SAME')(x)
final_net= keras.models.Model(inputs=[input_1,input_2,input_3],outputs=decoded)
return final_net
这篇关于图断开:无法获得张量 Tensor("conv2d_1_input:0", shape=(?, 128, 128, 1), dtype=float32) 的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!