我使用 Keras 和 Tensorflow 作为后端,这是我的代码:
#image loading and preprocessing
import os
from PIL import Image as Image
import numpy as np
#files is a list of images
files = [os.path.join('Save', file_i)
for file_i in os.listdir('Save')
if '.jpg' in file_i]
imgs = []
for image in files:
img = Image.open(image)
img = img.resize((227,227),Image.BILINEAR)
img = img.convert('L')
img = np.asarray(img)
array = img.astype('float32')
array /= 255
imgs.append(array)
imgs = np.asarray(imgs)
The_data = imgs.reshape(imgs.shape[0], 227, 227,1)
The_data = The_data.reshape(10, 25, 227, 227, 1)
from keras.models import Sequential
from keras.layers.convolutional import Convolution2D,Deconvolution2D
from keras.layers.convolutional_recurrent import ConvLSTM2D
from keras.layers.normalization import BatchNormalization
from keras.layers.wrappers import TimeDistributed
import numpy as np
import pylab as plt
model = Sequential()
#2 Convolution layer
model.add(TimeDistributed(Convolution2D(128, 11, 11 , border_mode='same', subsample = (4,4)), input_shape=(None,227, 227, 1)))
model.add(TimeDistributed(Convolution2D(64, 5, 5, border_mode='same', subsample = (2,2))))
model.add(TimeDistributed(ConvLSTM2D(nb_filter=64, nb_row=3, nb_col=3,
border_mode='same', return_sequences=True)))
model.add(BatchNormalization())
model.add(TimeDistributed(ConvLSTM2D(nb_filter=32, nb_row=3, nb_col=3,
border_mode='same', return_sequences=True)))
model.add(BatchNormalization())
model.add(TimeDistributed(ConvLSTM2D(nb_filter=64, nb_row=3, nb_col=3,
border_mode='same', return_sequences=True)))
model.add(BatchNormalization())
model.add(TimeDistributed(Deconvolution2D(128, 5, 5,border_mode='same', output_shape=(None,57, 57, 128), subsample = (2,2))))
model.add(TimeDistributed(Deconvolution2D(1, 11, 11,border_mode='same', output_shape=(None,227, 227, 1), subsample = (4,4))))
model.compile(optimizer='adadelta', loss='binary_crossentropy')
model.fit(The_data,The_data, batch_size=5,nb_epoch=1)
model.summary()
我正在尝试读取一些图像并对它们进行一些预处理,然后应用 (A) 2 个卷积层、(B) 三个 ConvLSTM 层和 (C) 2 个反卷积层。
我正在尝试实现这项研究中使用的算法 paper 但我看到每个 layers(conv,deconv,convlstm) 都需要不同的东西,我已经搜索并知道 convlstm 需要 5-dim 输入(帧数)但是如何更改它的输入形状,因为它不是模型中的第一层。
我在这里有三个主要问题:
1- Convultion2d 抛出该错误
Error when checking model target: expected convolution2d_2 to have shape (None, 26, 26, 64)
but got array with shape(250, 227, 227, 1)`
2- 我有评论 ConvLSTM2D 因为它抛出那个错误
ValueError: Input 0 is incompatible with layer convlstm2d_1: expected ndim=5, found ndim=4
我还评论了反卷积,因为我不知道 output_shape 应该是什么。 我知道最后我应该重建输入图像。
3- 在 model.fit 中,我没有标记数据,因为我正在进行无监督学习,我应该保持这种状态还是怎样?
请您参考如下方法:
问题在于:
- 错误的
input_shape- 数据应被裁剪为视频5-d格式。它是通过reshape和裁剪完成的。 - 将
TimeDistributed添加到conv和deconv层。 - 将
deconv输出形状更改为适当的值。 - 将
border_mode更改为same。
所有其他详细信息可以在问题下的评论中找到。
