【问题标题】:how to separate coding model from decoding model in autoencoder?如何在自动编码器中将编码模型与解码模型分开?
【发布时间】:2020-04-13 14:35:02
【问题描述】:

下面的源代码可以正常工作。

# 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) #

但是,我想将代码模型与解码模型分开,如下所示:

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()

这对我不起作用。我是 keras 和 python 的新手

我收到以下错误:

图形断开连接:无法在“input_13”层获取张量 Tensor("input_13:0", shape=(None, 28, 28, 1), dtype=float32) 的值。访问以下先前层没有问题:[]

【问题讨论】:

    标签: python tensorflow keras conv-neural-network autoencoder


    【解决方案1】:

    模型必须有一个 keras.layers.Input 用于输入。

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

    这里,x1 不是输入。它连接到编码器图,因此出现此错误。

    【讨论】:

      【解决方案2】:

      这是您更新后的可重现代码:

      您输入的形状都放错了。在瓶颈中,您传递了一个形状为 (8,8,8) 的张量。你可以从 summary() 中得到一个想法。

      from tensorflow.keras.layers import *
      from tensorflow.keras.models import *
      import numpy as np
      # The encoding process
      
      img_cols = 64
      
      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', padding = 'same')(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.summary()
      x_train = np.zeros((10,64,64,1))
      y_train =  np.zeros((10,64,64,1))
      autoencoder.fit(x_train, x_train, epochs=10, batch_size=128, shuffle=True) 
      
      # second approach
      
      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)
      
      reduced_dim = 8
      filters = 8
      input_decoder = Input(shape = (reduced_dim, reduced_dim, 8) )
      x1 = Conv2D(8, (3, 3), activation='relu', padding='same')(input_decoder)   ### 
      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', padding = 'same')(x)
      x = UpSampling2D((2, 2))(x)
      decoded = Conv2D(1, (3, 3), activation='sigmoid', padding='same')(x)
      
      encoder=Model(inputs=input_img, outputs=encoded)
      decoder=Model(inputs=input_decoder, outputs=decoded )
      autoencoder_outputs = decoder(encoder(input_img))
      autoencoder= Model(input_img, autoencoder_outputs, name='AE')
      autoencoder.summary()
      
      Model: "model_9"
      _________________________________________________________________
      Layer (type)                 Output Shape              Param #   
      =================================================================
      input_10 (InputLayer)        [(None, 64, 64, 1)]       0         
      _________________________________________________________________
      conv2d_49 (Conv2D)           (None, 64, 64, 16)        160       
      _________________________________________________________________
      max_pooling2d_21 (MaxPooling (None, 32, 32, 16)        0         
      _________________________________________________________________
      conv2d_50 (Conv2D)           (None, 32, 32, 8)         1160      
      _________________________________________________________________
      max_pooling2d_22 (MaxPooling (None, 16, 16, 8)         0         
      _________________________________________________________________
      conv2d_51 (Conv2D)           (None, 16, 16, 8)         584       
      _________________________________________________________________
      max_pooling2d_23 (MaxPooling (None, 8, 8, 8)           0         
      _________________________________________________________________
      conv2d_52 (Conv2D)           (None, 8, 8, 8)           584       
      _________________________________________________________________
      up_sampling2d_21 (UpSampling (None, 16, 16, 8)         0         
      _________________________________________________________________
      conv2d_53 (Conv2D)           (None, 16, 16, 8)         584       
      _________________________________________________________________
      up_sampling2d_22 (UpSampling (None, 32, 32, 8)         0         
      _________________________________________________________________
      conv2d_54 (Conv2D)           (None, 32, 32, 16)        1168      
      _________________________________________________________________
      up_sampling2d_23 (UpSampling (None, 64, 64, 16)        0         
      _________________________________________________________________
      conv2d_55 (Conv2D)           (None, 64, 64, 1)         145       
      =================================================================
      Total params: 4,385
      Trainable params: 4,385
      Non-trainable params: 0
      _________________________________________________________________
      Epoch 1/10
      1/1 [==============================] - 0s 1ms/step - loss: 0.6931
      Epoch 2/10
      1/1 [==============================] - 0s 1ms/step - loss: 0.6931
      Epoch 3/10
      1/1 [==============================] - 0s 1ms/step - loss: 0.6931
      Epoch 4/10
      1/1 [==============================] - 0s 1ms/step - loss: 0.6931
      Epoch 5/10
      1/1 [==============================] - 0s 1ms/step - loss: 0.6931
      Epoch 6/10
      1/1 [==============================] - 0s 1ms/step - loss: 0.6931
      Epoch 7/10
      1/1 [==============================] - 0s 1ms/step - loss: 0.6931
      Epoch 8/10
      1/1 [==============================] - 0s 1ms/step - loss: 0.6931
      Epoch 9/10
      1/1 [==============================] - 0s 2ms/step - loss: 0.6931
      Epoch 10/10
      1/1 [==============================] - 0s 2ms/step - loss: 0.6931
      Model: "AE"
      _________________________________________________________________
      Layer (type)                 Output Shape              Param #   
      =================================================================
      input_11 (InputLayer)        [(None, 64, 64, 1)]       0         
      _________________________________________________________________
      model_10 (Model)             (None, 8, 8, 8)           1904      
      _________________________________________________________________
      model_11 (Model)             (None, 64, 64, 1)         2481      
      =================================================================
      Total params: 4,385
      Trainable params: 4,385
      Non-trainable params: 0
      

      【讨论】:

      • 非常感谢您的帮助,@furcifer 当我运行 fit 命令时出现以下错误:检查目标时出错:预期 model_37 具有形状(32、32、1)但得到数组形状 (28, 28, 1) 我的图像数据库形状为 28x28,我该如何调整代码?提前致谢
      • 不要选择 28,将输入调整为形状 32,原因是您正在应用最大池化和上采样。所以,28 不是 2 的幂,模型会有不同的输入输出形状。
      • 是的,我注意到,我将其更改为 64,它可以完美运行。但它在与 28x28 分离之前确实有效,有什么线索吗? @furcifer
      • 因为您的模型无法再现形状,您可以查看摘要。在模型中使用最大池化和上采样时,始终使用输入大小作为 2 的幂(考虑池化内核的大小为 2)。
      • 好的,非常感谢,我将其调整为 32,效果很好。感谢您的帮助。
      猜你喜欢
      • 2019-09-13
      • 2021-03-25
      • 2019-05-19
      • 2019-05-20
      • 2019-06-14
      • 2019-09-11
      • 1970-01-01
      • 2019-08-14
      • 2022-01-04
      相关资源
      最近更新 更多