Search code examples
pythontensorflowkerasautoencoder

Graph disconnected: cannot obtain value for tensor Tensor("conv2d_1_input:0", shape=(?, 128, 128, 1), dtype=float32)


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

the error is:

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']

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

but I got an error "'Tensor' object has no attribute 'output'"


Solution

  • 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