how to separate coding model from decoding model in autoencoder?

The below source code works fine.

# The encoding process
input_img = Input(shape=(img_cols, img_cols, 1))
x = Conv2D(16, (3, 3), activation='relu', padding='same')(input_img)
x = MaxPooling2D(pool_size = (2, 2), padding='same')(x)
x = Conv2D( 8, kernel_size = (3, 3), activation='relu', padding='same')(x)
x = MaxPooling2D(pool_size = (2, 2), padding='same')(x)
x = Conv2D( 8, (3, 3), activation='relu', padding='same')(x)
encoded = MaxPooling2D(pool_size = (2, 2), padding='same')(x)

x1 = Conv2D(8, (3, 3), activation='relu', padding='same')(encoded)   ### 
x = UpSampling2D((2, 2))(x1)
x = Conv2D(8, (3, 3), activation='relu', padding='same')(x)
x = UpSampling2D((2, 2))(x)
x = Conv2D(16, (3, 3), activation='relu')(x)
x = UpSampling2D((2, 2))(x)
decoded = Conv2D(1, (3, 3), activation='sigmoid', padding='same')(x)

autoencoder = Model(input_img, decoded)
autoencoder.compile(optimizer='adadelta', loss='binary_crossentropy')
autoencoder.fit(x_train, x_train, epochs=10, batch_size=128, shuffle=True) #

However, i want to separate code model from decoding model like the following:

encoder=Model(inputs=input_img, outputs=encoded)
decoder=Model(inputs=x1,outputs=decoded )
autoencoder_outputs = decoder(encoder(input_img))
autoencoder= Model(input_img, autoencoder_outputs, name='AE')
autoencoder.summary()

It doesn’t work for me. I am new in keras and python

i get the following error:

Graph disconnected: cannot obtain value for tensor Tensor(“input_13:0”, shape=(None, 28, 28, 1), dtype=float32) at layer “input_13”. The following previous layers were accessed without issue: []

Answer

A model must have a keras.layers.Input for inputs.

decoder=Model(inputs=x1,outputs=decoded )

Here, x1 is not an Input. It is connected to the encoder graph, hence this error.