从自动编码器获取瓶颈层的输出

Get the output of just bottleneck layer from autoencoder

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

# 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`.

我也试过一些其他的方法,但还是不行。谁能告诉我如何在训练模型后恢复压缩数组。

您需要为 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) 应该 return 每个图像的向量。

正在获取中间层的输出(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)