我尝试使用keras创建自动编码器。 (参考:https://blog.keras.io/building-autoencoders-in-keras.html)
以下测试代码在我的mac上运行正常:
import numpy as np
from keras.layers import Input, Dense, Convolution2D, MaxPooling2D, UpSampling2D, Cropping2D, ZeroPadding2D
from keras.models import Model
input_img = Input(shape=(1, 30, 32))
testx = np.random.rand(10,1,30,32)
x = ZeroPadding2D((1, 0))(input_img)
x = Convolution2D(16, 3, 3, activation='relu', border_mode='same')(x)
x = MaxPooling2D((2, 2), border_mode='same')(x)
x = Convolution2D(8, 3, 3, activation='relu', border_mode='same')(x)
x = MaxPooling2D((2, 2), border_mode='same')(x)
x = Convolution2D(8, 3, 3, activation='relu', border_mode='same')(x)
encoded = MaxPooling2D((2, 2), border_mode='same')(x)
x = Convolution2D(8, 3, 3, activation='relu', border_mode='same')(encoded)
x = UpSampling2D((2, 2))(x)
x = Convolution2D(8, 3, 3, activation='relu', border_mode='same')(x)
x = UpSampling2D((2, 2))(x)
x = Convolution2D(16, 3, 3, activation='relu', border_mode='same')(x)
x = UpSampling2D((2, 2))(x)
x = Cropping2D(cropping=((1,1),(0,0)))(x)
decoded = Convolution2D(1, 3, 3, activation='sigmoid', border_mode='same')(x)
autoencoder = Model(input_img, decoded)
autoencoder.compile(optimizer='adadelta', loss='binary_crossentropy')
autoencoder.fit(testx, testx, nb_epoch = 5, batch_size =1)
但是,当我在AWS EC2 g2.2xlarge上测试这段代码时(我的配置信息在本文末尾),我遇到了以下错误:
Using Theano backend.
Using gpu device 0: GRID K520 (CNMeM is enabled with initial size: 95.0% of memory, cuDNN 5005)
____________________________________________________________________________________________________
Layer (type) Output Shape Param # Connected to
====================================================================================================
input_1 (InputLayer) (None, 1, 30, 32) 0
____________________________________________________________________________________________________
zeropadding2d_1 (ZeroPadding2D) (None, 1, 32, 32) 0 input_1[0][0]
____________________________________________________________________________________________________
convolution2d_1 (Convolution2D) (None, 16, 32, 32) 160 zeropadding2d_1[0][0]
____________________________________________________________________________________________________
maxpooling2d_1 (MaxPooling2D) (None, 16, 16, 16) 0 convolution2d_1[0][0]
____________________________________________________________________________________________________
convolution2d_2 (Convolution2D) (None, 8, 16, 16) 1160 maxpooling2d_1[0][0]
____________________________________________________________________________________________________
maxpooling2d_2 (MaxPooling2D) (None, 8, 8, 8) 0 convolution2d_2[0][0]
____________________________________________________________________________________________________
convolution2d_3 (Convolution2D) (None, 8, 8, 8) 584 maxpooling2d_2[0][0]
____________________________________________________________________________________________________
maxpooling2d_3 (MaxPooling2D) (None, 8, 4, 4) 0 convolution2d_3[0][0]
____________________________________________________________________________________________________
convolution2d_4 (Convolution2D) (None, 8, 4, 4) 584 maxpooling2d_3[0][0]
____________________________________________________________________________________________________
upsampling2d_1 (UpSampling2D) (None, 8, 8, 8) 0 convolution2d_4[0][0]
____________________________________________________________________________________________________
convolution2d_5 (Convolution2D) (None, 8, 8, 8) 584 upsampling2d_1[0][0]
____________________________________________________________________________________________________
upsampling2d_2 (UpSampling2D) (None, 8, 16, 16) 0 convolution2d_5[0][0]
____________________________________________________________________________________________________
convolution2d_6 (Convolution2D) (None, 16, 16, 16) 1168 upsampling2d_2[0][0]
____________________________________________________________________________________________________
upsampling2d_3 (UpSampling2D) (None, 16, 32, 32) 0 convolution2d_6[0][0]
____________________________________________________________________________________________________
cropping2d_1 (Cropping2D) (None, 16, 30, 32) 0 upsampling2d_3[0][0]
____________________________________________________________________________________________________
convolution2d_7 (Convolution2D) (None, 1, 30, 32) 145 cropping2d_1[0][0]
====================================================================================================
Total params: 4,385
Trainable params: 4,385
Non-trainable params: 0
____________________________________________________________________________________________________
Epoch 1/5
Traceback (most recent call last):
File "testtest.py", line 32, in <module>
autoencoder.fit(testx, testx, nb_epoch = 5, batch_size =1)
File "/usr/local/lib/python2.7/dist-packages/keras/engine/training.py", line 1144, in fit
initial_epoch=initial_epoch)
File "/usr/local/lib/python2.7/dist-packages/keras/engine/training.py", line 844, in _fit_loop
outs = f(ins_batch)
File "/usr/local/lib/python2.7/dist-packages/keras/backend/theano_backend.py", line 953, in __call__
return self.function(*inputs)
File "/usr/local/lib/python2.7/dist-packages/theano/compile/function_module.py", line 886, in __call__
storage_map=getattr(self.fn, 'storage_map', None))
File "/usr/local/lib/python2.7/dist-packages/theano/gof/link.py", line 325, in raise_with_op
reraise(exc_type, exc_value, exc_trace)
File "/usr/local/lib/python2.7/dist-packages/theano/compile/function_module.py", line 873, in __call__
self.fn() if output_subset is None else\
ValueError: Input dimension mis-match. (input[0].shape[3] = 32, input[1].shape[3] = 0)
Apply node that caused the error: Elemwise{mul,no_inplace}(convolution2d_7_target, Elemwise{log,no_inplace}.0)
Toposort index: 703
Inputs types: [TensorType(float32, 4D), TensorType(float32, 4D)]
Inputs shapes: [(1, 1, 30, 32), (1, 1, 30, 0)]
Inputs strides: [(3840, 3840, 128, 4), (120, 120, 4, 4)]
Inputs values: ['not shown', array([], shape=(1, 1, 30, 0), dtype=float32)]
Outputs clients: [[Elemwise{add,no_inplace}(Elemwise{mul,no_inplace}.0, Elemwise{mul,no_inplace}.0)]]
Backtrace when the node is created(use Theano flag traceback.limit=N to make it longer):
File "testtest.py", line 30, in <module>
autoencoder.compile(optimizer='adadelta', loss='binary_crossentropy')
File "/usr/local/lib/python2.7/dist-packages/keras/engine/training.py", line 619, in compile
sample_weight, mask)
File "/usr/local/lib/python2.7/dist-packages/keras/engine/training.py", line 307, in weighted
score_array = fn(y_true, y_pred)
File "/usr/local/lib/python2.7/dist-packages/keras/objectives.py", line 49, in binary_crossentropy
return K.mean(K.binary_crossentropy(y_pred, y_true), axis=-1)
File "/usr/local/lib/python2.7/dist-packages/keras/backend/theano_backend.py", line 1235, in binary_crossentropy
return T.nnet.binary_crossentropy(output, target)
如果删除了Cropping2D功能,我可以在GPU上运行代码:
import numpy as np
from keras.layers import Input, Dense, Convolution2D, MaxPooling2D, UpSampling2D, Cropping2D, ZeroPadding2D
from keras.models import Model
input_img = Input(shape=(1, 30, 32))
testx = np.random.rand(10,1,30,32)
testy = np.random.rand(10,1,32,32)
x = ZeroPadding2D((1, 0))(input_img)
x = Convolution2D(16, 3, 3, activation='relu', border_mode='same')(x)
x = MaxPooling2D((2, 2), border_mode='same')(x)
x = Convolution2D(8, 3, 3, activation='relu', border_mode='same')(x)
x = MaxPooling2D((2, 2), border_mode='same')(x)
x = Convolution2D(8, 3, 3, activation='relu', border_mode='same')(x)
encoded = MaxPooling2D((2, 2), border_mode='same')(x)
# at this point the representation is (8, 4, 4) i.e. 128-dimensional
x = Convolution2D(8, 3, 3, activation='relu', border_mode='same')(encoded)
x = UpSampling2D((2, 2))(x)
x = Convolution2D(8, 3, 3, activation='relu', border_mode='same')(x)
x = UpSampling2D((2, 2))(x)
x = Convolution2D(16, 3, 3, activation='relu', border_mode='same')(x)
x = UpSampling2D((2, 2))(x)
# x = Cropping2D(cropping=((1,1),(0,0)))(x)
decoded = Convolution2D(1, 3, 3, activation='sigmoid', border_mode='same')(x)
autoencoder = Model(input_img, decoded)
autoencoder.compile(optimizer='adadelta', loss='binary_crossentropy')
autoencoder.summary()
autoencoder.fit(testx, testy, nb_epoch = 5, batch_size =1)
在类似的情况下,Cropping3D也会引发类似的错误......: - (
我的问题是:
如何解决此问题并在GPU上运行Cropping2D / Cropping3D?
PS。我的AWS EC2 g2.2xlarge配置如下:
谢谢!
答案 0 :(得分:0)
Keras似乎有一个错误。 它一直在照顾。结案!