训练可变自动编码器时出现不兼容的形状错误

时间:2019-02-07 18:28:30

标签: python deep-learning

我正在尝试训练可变编码器。但是我得到

  

InvalidArgumentError:不兼容的形状:[32,784][32,2352]

     

[[{{node custom_variational_layer_21/logistic_loss/mul}}]]

我使用opencv读取图像并将其附加到列表中,然后将其转换为numpy数组。 复制的代码来自:http://www.stokastik.in/understanding-variational-autoencoders/
我正在使用卷积变分自动编码器。

images = []

files = glob.glob('../dataset/maggi/*.*')
i=0
for file in files:
    try:
        img = cv2.imread(file)
        img = cv2.resize(img, (28,28))
        images.append(img)
    except:
        print('error')

x_train = np.asarray(images)
x_train = x_train.astype('float32') / 255.

print('Input size : ',x_train.shape)

conv_variational_autoencoder(x_train)

输出:

Input size :  (1446, 28, 28, 3)
Epoch 1/50

----------------------------------------------------------------
InvalidArgumentError                      Traceback (most recent call last)
<ipython-input-166-2e8711de7bdc> in <module>()
72 print('Input size : ',x_train.shape)
73 
---> 74 conv_variational_autoencoder(x_train)

<ipython-input-166-2e8711de7bdc> in conv_variational_autoencoder(X_train)
 50     adam = Adam(lr=0.0005)
 51     autoencoder.compile(optimizer=adam, loss=None)
---> 52     autoencoder.fit(X_train, shuffle=True, epochs=50, batch_size=32)
 53 
 54 

/usr/local/lib/python3.6/dist-packages/keras/engine/training.py in    fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split,   validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, **kwargs)
1037                                         initial_epoch=initial_epoch,
1038                                         steps_per_epoch=steps_per_epoch,
-> 1039                                         validation_steps=validation_steps)
1040 
1041     def evaluate(self, x=None, y=None,

/usr/local/lib/python3.6/dist-packages/keras/engine/training_arrays.py in fit_loop(model, f, ins, out_labels, batch_size, epochs, verbose, callbacks, val_f, val_ins, shuffle, callback_metrics, initial_epoch, steps_per_epoch, validation_steps)
197                     ins_batch[i] = ins_batch[i].toarray()
198 
--> 199                 outs = f(ins_batch)
200                 outs = to_list(outs)
201                 for l, o in zip(out_labels, outs):

/usr/local/lib/python3.6/dist-packages/keras/backend/tensorflow_backend.py in __call__(self, inputs)
2713                 return self._legacy_call(inputs)
2714 
-> 2715             return self._call(inputs)
2716         else:
2717             if py_any(is_tensor(x) for x in inputs):

/usr/local/lib/python3.6/dist-packages/keras/backend/tensorflow_backend.py in _call(self, inputs)
2673             fetched = self._callable_fn(*array_vals,     run_metadata=self.run_metadata)
2674         else:
-> 2675             fetched = self._callable_fn(*array_vals)
2676         return fetched[:len(self.outputs)]
2677 

/usr/local/lib/python3.6/dist-packages/tensorflow/python/client/session.py in __call__(self, *args, **kwargs)
1437           ret = tf_session.TF_SessionRunCallable(
1438               self._session._session, self._handle, args, status,
-> 1439               run_metadata_ptr)
1440         if run_metadata:
1441           proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)

/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/errors_impl.py in __exit__(self, type_arg, value_arg, traceback_arg)
526             None, None,
527             compat.as_text(c_api.TF_Message(self.status.status)),
--> 528             c_api.TF_GetCode(self.status.status))
529     # Delete the underlying status object from memory otherwise it stays alive
530     # as there is a reference to status from this from the traceback due to

InvalidArgumentError: Incompatible shapes: [32,784] vs. [32,2352]
 [[{{node custom_variational_layer_21/logistic_loss/mul}}]]

1 个答案:

答案 0 :(得分:0)

感谢您的文章链接!这是一个非常有趣而且很好的文章。

现在解决这个问题: 通常,请始终使用model.summaray()函数检查模型的输入和输出。在您的情况下,您的模型如下所示:

autoencoder summary

现在密切注意。输入图像的形状像您自己定义的28x28x3。但是输出为28x28x1,因为您使用的文章在mnist上训练了模型,该模型是灰度的,因此只有1个颜色通道,而您只有3个通道。

这会导致损失函数产生错误,因为它试图比较灰度图像看起来像彩色图像的程度,这当然是行不通的。

要解决此问题,您所需要做的就是转到conv_variational_autoencoder(x_train)函数的解码器部分,并将最后一个Conv2DTranspose的输出大小更改为28x28x3而不是28x28x1

#Decoder
decoder_input = Input(shape=(196,))
p = Reshape((14, 14, 1))(decoder_input)
x = Conv2DTranspose(32, (3, 3), activation='relu', padding='same')(p)
x = UpSampling2D((2, 2))(x)
# dec_out = Conv2DTranspose(1, (3, 3), activation='sigmoid', padding='same')(x)
# Change the above line to:
dec_out = Conv2DTranspose(3, (3, 3), activation='sigmoid', padding='same')(x)
decoder = Model(decoder_input, dec_out)

它应该立即训练。祝你好运!

相关问题