图断开:无法获得张量 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)

查看:51
本文介绍了图断开:无法获得张量 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'"

推荐答案

如果需要获取a1a2a3,需要创建子网 输出.并且可以如下连接xa.

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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆