0

I trained an autoencoder in Keras and saved the model as two separate models: The encoder and the decoder.

I successfully load these, and then recreate the whole autoencoder with the following:

ae_v = decoder(encoder(ae_in))

autoencoder = Model(inputs = ae_in, outputs = ae_v)

Now, when I use:

autoencoder.predict(sample)

The model works properly.

However, I was under the impression that this would be equivalent:

a = encoder.predict(sample)

b = decoder.predict(a)

However, when doing this, the model produces entirely different results. That is, the end result (b) is different from autoencoder.predict(sample), for the exact same sample. But from what I can tell, shouldn't these be equivalent?

All that's happening is that in the latter code, I'm deliberately taking the output from the encoder and passing it to the decoder, rather than doing one continuous forward pass. I want to be able to do this, so I can make small modifications to the output produced by the encoder before passing it in to the decoder.

Am I missing something obvious here?

Edit: Showing network construction

x_2 = Input(shape = (HEIGHT, WIDTH, CHANNELS))
x_3 = Input(shape=(CHOKE_DIM_2, CHOKE_DIM_1))

en_1 = Convolution2D(32, (4, 4,), subsample = (2, 2), activation = 'relu', init = INIT, padding = "same")(x_2)
en_bn_1 = BatchNormalization(momentum = momentum)(en_1)
en_drop_1 = Dropout(dropout)(en_bn_1)

en_2 = Convolution2D(64, (2, 2,), subsample = (2, 2), activation = 'relu', init = INIT, padding = "same")(en_drop_1)
en_bn_2 = BatchNormalization(momentum = momentum)(en_2)
en_drop_2 = Dropout(dropout)(en_bn_2)

en_3 = Convolution2D(64, (2, 2,), activation = 'relu', init = INIT, padding = "same")(en_drop_2)
en_bn_3 = BatchNormalization(momentum = momentum)(en_3)
en_drop_3 = Dropout(dropout)(en_bn_3)
en_flat = Flatten()(en_drop_3)


chokepoint, d_mid, d_out = [], [], []
for i in range(CHOKE_DIM_2):
    a = Dense(CHOKE_DIM_1, init = INIT)(en_flat)
    chokepoint.append(a)
en_out_1 = Concatenate(axis = 1)(chokepoint)
en_out_2 = BatchNormalization(momentum = momentum)(en_out_1)
en_out = Reshape((CHOKE_DIM_2, CHOKE_DIM_1))(en_out_2)    

for i in range(CHOKE_DIM_2):
    x_tmp = Lambda(lambda x : x[:, i, 0:32])(x_3)
    x_tmp.trainable = False
    de_1 = Dense(CHOKE_DIM_1*2, activation = 'relu', init = INIT)(x_tmp)
    de_bn_1 = BatchNormalization(momentum = momentum)(de_1)
    de_drop_1 = Dropout(dropout)(de_bn_1)

    de_2 = Dense(CHOKE_DIM_1*4, activation = 'relu', init = INIT)(de_drop_1)
    de_bn_2 = BatchNormalization(momentum = momentum)(de_2)
    de_drop_2 = Dropout(dropout)(de_bn_2)

    de_3 = Dense(CHOKE_DIM_1*8, activation = 'relu', init = INIT)(de_drop_2)
    de_bn_3 = BatchNormalization(momentum = momentum)(de_3)
    de_drop_3 = Dropout(dropout)(de_bn_2)

    de_4 = Dense(30*40*3, activation = 'relu', init = INIT)(de_drop_3)
    r = Reshape((30, 40, 3))(de_4)
    de_out = UpSampling2D()(r)

    d_mid.extend([x_tmp, de_1, de_bn_1, de_drop_1, de_2, de_bn_2, de_drop_2, de_3, de_bn_3, de_drop_3, de_4, r])
    d_out.append(de_out)
de_true_out = Add()(d_out)

encoder = Model(inputs = x_2, outputs = en_out)
decoder = Model(inputs = x_3, outputs = de_true_out)
encoder.load_weights(LOADPATH + "encoder_weights.h5")
decoder.load_weights(LOADPATH + "decoder_weights.h5")

ae_v = decoder(encoder(x_2))
autoencoder = Model(inputs = x_2, outputs = ae_v)

0 Answers0