图表已断开连接:无法获取张量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)

查看:112
本文介绍了图表已断开连接:无法获取张量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

但是出现错误"张量"对象没有属性输出" "

推荐答案

如果需要获取 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屋!

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