【问题标题】:Get the output of just bottleneck layer from autoencoder从自动编码器获取瓶颈层的输出
【发布时间】:2021-01-07 05:10:21
【问题描述】:

我是自动编码器的新手。我已经构建了一个简单的卷积自动编码器,如下所示:

# ENCODER
input_img = Input(shape=(64, 64, 1))

encode1 = Conv2D(32, (3, 3), activation=tf.nn.leaky_relu, padding='same')(input_img) 
encode2 = MaxPooling2D((2, 2), padding='same')(encode1)
l = Flatten()(encode2)
l = Dense(100, activation='linear')(l)

# DECODER
d = Dense(1024, activation='linear')(l) 
d = Reshape((32,32,1))(d)
decode3 = Conv2D(64, (3, 3), activation=tf.nn.leaky_relu, padding='same')(d) 
decode4 = UpSampling2D((2, 2))(decode3)

model = models.Model(input_img, decode4)

model.compile(optimizer='adam', loss='mse')

# Train it by providing training images
model.fit(x, y, epochs=20, batch_size=16)

现在在训练这个模型之后,我想从瓶颈层(即密集层)获得输出。这意味着如果我将形状数组 (1000, 64, 64) 扔给模型,我想要压缩的形状数组 (1000, 100)。

我尝试了如下所示的一种方法,但它给了我一些错误。

model = Model(inputs=[x], outputs=[l])

错误:

ValueError: Input tensors to a Functional must come from `tf.keras.Input`.

我也尝试了其他一些方法,但这也不起作用。有人可以告诉我如何在训练模型后恢复压缩数组。

【问题讨论】:

    标签: python machine-learning deep-learning autoencoder encoder


    【解决方案1】:

    您需要为encoder 创建单独的模型。训练整个系统encoder-decoder 后,您只能使用encoder 进行预测。代码示例:

    # ENCODER
    input_img = layers.Input(shape=(64, 64, 1))
    encode1 = layers.Conv2D(32, (3, 3), activation=tf.nn.leaky_relu, padding='same')(input_img) 
    encode2 = layers.MaxPooling2D((2, 2), padding='same')(encode1)
    l = layers.Flatten()(encode2)
    encoder_output = layers.Dense(100, activation='linear')(l)
    
    # DECODER
    d = layers.Dense(1024, activation='linear')(encoder_output) 
    d = layers.Reshape((32,32,1))(d)
    decode3 = layers.Conv2D(64, (3, 3), activation=tf.nn.leaky_relu, padding='same')(d) 
    decode4 = layers.UpSampling2D((2, 2))(decode3)
    
    model_encoder = Model(input_img, encoder_output)
    model = Model(input_img, decode4)
    
    model.fit(X, y, epochs=20, batch_size=16)
    

    model_encoder.predict(X) 应该为每个图像返回一个向量。

    【讨论】:

      【解决方案2】:

      获取中间层(bottleneck_layer)的输出。

      # ENCODER
      input_img = Input(shape=(64, 64, 1))
      
      encode1 = Conv2D(32, (3, 3), activation=tf.nn.leaky_relu, padding='same')(input_img) 
      encode2 = MaxPooling2D((2, 2), padding='same')(encode1)
      l = Flatten()(encode2)
      bottleneck = Dense(100, activation='linear', name='bottleneck_layer')(l)
      
      # DECODER
      d = Dense(1024, activation='linear')(bottleneck) 
      d = Reshape((32,32,1))(d)
      decode3 = Conv2D(64, (3, 3), activation=tf.nn.leaky_relu, padding='same')(d) 
      decode4 = UpSampling2D((2, 2))(decode3)
      
      # full model
      model_full = models.Model(input_img, decode4)
      model_full.compile(optimizer='adam', loss='mse')
      model_full.fit(x, y, epochs=20, batch_size=16)
      
      # bottleneck model
      bottleneck_output = model_full.get_layer('bottleneck_layer').output
      model_bottleneck = models.Model(inputs = model_full.input, outputs = bottleneck_output)
      
      bottleneck_predictions = model_bottleneck.predict(X_test)
      

      【讨论】:

        猜你喜欢
        • 2020-01-12
        • 2018-10-15
        • 1970-01-01
        • 1970-01-01
        • 2021-04-23
        • 1970-01-01
        • 1970-01-01
        • 2011-01-26
        • 1970-01-01
        相关资源
        最近更新 更多