【问题标题】:How to seperately save Keras encoder and decoder如何分别保存 Keras 编码器和解码器
【发布时间】:2021-08-22 23:11:33
【问题描述】:

我已经使用 Tensorflow Keras 训练了一个自动编码器解码器

x_train = np.reshape(x_train, (len(x_train), n_row,n_col, 1))
x_test = np.reshape(x_test, (len(x_test),  n_row,n_col, 1))


input_img = Input(shape=(n_row,n_col, 1))

x = Convolution2D(16, (10, 10), activation='relu', padding='same')(input_img)
x = MaxPooling2D((5, 5), padding='same')(x)
x = Convolution2D(8, (2, 2), activation='relu', padding='same')(x)
x = MaxPooling2D((3, 3), padding='same')(x)
x = Convolution2D(8, (3, 3), activation='relu', padding='same')(x)
encoded = MaxPooling2D((2, 2), padding='same')(x)

x = Convolution2D(8, (2, 2), activation='relu', padding='same')(encoded)
x = UpSampling2D((3, 3))(x)
x = Convolution2D(8, (3, 3), activation='relu', padding='same')(x)
x = UpSampling2D((5, 5))(x)
x = Convolution2D(16, (10, 10), activation='relu', padding='same')(x)
x = UpSampling2D((2, 2))(x)
x = Cropping2D(cropping=((5, 0), (1, 0)), data_format=None)(x)

decoded = Convolution2D(1, (10, 10), activation='sigmoid', padding='same')(x)

autoencoder = Model(inputs=input_img, outputs=decoded)

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

我可以通过使用保存整个自动编码器模型

autoencoder.save('...')

如何分别保存和访问编码器和解码器?

【问题讨论】:

    标签: tensorflow keras tf.keras


    【解决方案1】:

    由于您已经保存了autoencoder,您可以同样从保存的自动编码器中提取encoderdecoder

    autoencoder= K.models.load_model('your_saved_autoencoder')
    
    encoder = Model(autoencoder.input, autoencoder.layers[-2].output)
    
    decoder_input = Input(shape=(encoding_dim,))
    decoder = Model(decoder_input, autoencoder.layers[-1](decoder_input))
    

    【讨论】:

    • 感谢您的回答。但是,由于我有一个深度自动编码器,所以 autoencoder.layers[-1] 在这里不起作用
    • 我已经为您的具体问题环顾了一下,我认为独立保存编码器和解码器的方法是将它们创建为单独的模型,然后在保存自动编码器时保存它们.您可以参考this answer 以获得更清晰的信息:
    猜你喜欢
    • 2019-01-05
    • 2019-07-22
    • 2019-09-13
    • 2020-09-26
    • 2019-05-20
    • 2019-07-07
    • 1970-01-01
    • 2019-07-12
    • 2022-01-04
    相关资源
    最近更新 更多