Я написал следующий код и пытаюсь предсказать изображение из вариационной модели автоэнкодера:
Кодировщик:
input_img = Input(shape=(28, 28, 3))
x = Conv2D(32, 3,
padding='same',
activation='relu')(input_img)
x = Conv2D(64, 3,
padding='same',
activation='relu',
strides=(2, 2))(x)
x = Conv2D(64, 3,
padding='same',
activation='relu')(x)
x = Conv2D(64, 3,
padding='same',
activation='relu')(x)
x = Flatten()(x)
x = Dense(16, activation='relu')(x)
# Two outputs, latent mean and (log)variance
z_mu = Dense(latent_dim)(x)
z_log_sigma = Dense(latent_dim)(x)
encoder = Model(inputs = input_img, outputs = x)
Декодер:
# decoder takes the latent distribution sample as input
decoder_input = Input(K.int_shape(z)[1:])
# Expand to 784 total pixels
x = Dense(np.prod(shape_before_flattening[1:]),
activation='relu')(decoder_input)
# reshape
x = Reshape(shape_before_flattening[1:])(x)
# use Conv2DTranspose to reverse the conv layers
x = Conv2DTranspose(32, 3,
padding='same',
activation='relu',
strides=(2, 2))(x)
x = Conv2D(3, 3,
padding='same',
activation='sigmoid')(x)
# decoder model statement
decoder = Model(decoder_input, x)
# apply the decoder to the sample from the latent distribution
z_decoded = decoder(z)
Кодировщик выглядит следующим образом:
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_13 (InputLayer) (None, 28, 28, 3) 0
_________________________________________________________________
conv2d_1 (Conv2D) (None, 28, 28, 32) 896
_________________________________________________________________
conv2d_2 (Conv2D) (None, 14, 14, 64) 18496
_________________________________________________________________
conv2d_3 (Conv2D) (None, 14, 14, 64) 36928
_________________________________________________________________
conv2d_4 (Conv2D) (None, 14, 14, 64) 36928
_________________________________________________________________
flatten_1 (Flatten) (None, 12544) 0
_________________________________________________________________
dense_10 (Dense) (None, 16) 200720
=================================================================
Total params: 293,968
Trainable params: 293,968
Non-trainable params: 0
, а декодер как таковой:
Layer (type) Output Shape Param #
=================================================================
input_15 (InputLayer) (None, 2) 0
_________________________________________________________________
dense_14 (Dense) (None, 12544) 37632
_________________________________________________________________
reshape_3 (Reshape) (None, 14, 14, 64) 0
_________________________________________________________________
conv2d_transpose_2 (Conv2DTr (None, 28, 28, 32) 18464
_________________________________________________________________
conv2d_6 (Conv2D) (None, 28, 28, 3) 867
=================================================================
Total params: 56,963
Trainable params: 56,963
Non-trainable params: 0
_________________________________________________________________
Работает очень хорошо.Вот полная модель:
__________________________________________________________________________________________________
Layer (type) Output Shape Param # Connected to
==================================================================================================
input_13 (InputLayer) (None, 28, 28, 3) 0
__________________________________________________________________________________________________
conv2d_1 (Conv2D) (None, 28, 28, 32) 896 input_13[0][0]
__________________________________________________________________________________________________
conv2d_2 (Conv2D) (None, 14, 14, 64) 18496 conv2d_1[0][0]
__________________________________________________________________________________________________
conv2d_3 (Conv2D) (None, 14, 14, 64) 36928 conv2d_2[0][0]
__________________________________________________________________________________________________
conv2d_4 (Conv2D) (None, 14, 14, 64) 36928 conv2d_3[0][0]
__________________________________________________________________________________________________
flatten_1 (Flatten) (None, 12544) 0 conv2d_4[0][0]
__________________________________________________________________________________________________
dense_10 (Dense) (None, 16) 200720 flatten_1[0][0]
__________________________________________________________________________________________________
dense_11 (Dense) (None, 2) 34 dense_10[0][0]
__________________________________________________________________________________________________
dense_12 (Dense) (None, 2) 34 dense_10[0][0]
__________________________________________________________________________________________________
lambda_5 (Lambda) (None, 2) 0 dense_11[0][0]
dense_12[0][0]
__________________________________________________________________________________________________
model_16 (Model) (None, 28, 28, 3) 56963 lambda_5[0][0]
__________________________________________________________________________________________________
custom_variational_layer_3 (Cus [(None, 28, 28, 3), 0 input_13[0][0]
model_16[1][0]
==================================================================================================
Total params: 350,999
Trainable params: 350,999
Non-trainable params: 0
__________________________________________________________________________________________________
Проблема заключается в том, когда я пытаюсь создать изображение на основе существующего изображения.Это показывает изображение из обучающего набора:
rnd_file = np.random.choice(files)
file_id = os.path.basename(rnd_file)
img = imread(rnd_file)
plt.imshow(img)
plt.show()
Затем я добавляю изображение в кодировщик, чтобы получить скрытое представление изображения:
z = encoder.predict(img)
Когда у меня естьскрытое представление, я декодирую его на основе данного представления в изображение:
decoder.predict(z)
Это дает следующую ошибку:
ValueError: Ошибка при проверке ввода: ожидается, что input_15 будет иметь форму (2,), но получил массив с формой (16,)
z выглядит следующим образом:
[0. 0. 0. 0. 0. 0.03668813
0.10211123 0.08731555 0. 0.01327576 0. 0.
0. 0. 0.03561973 0.02009114]
Выход кодера равен (None, 16), так же, как мой z,И это работает как модель.Как я могу это исправить?Заранее спасибо