我正在使用带有Tensorflow的Keras作为后端,这是我的代码:
#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抛出该错误
检查模型目标时出错:预期的卷积2d_2具有形状(无,26,26,64)但是具有形状的数组(250,227,227,1)
2-我有评论ConvLSTM2D,因为它抛出了这个错误
ValueError:输入0与图层convlstm2d_1不兼容:预期ndim = 5,发现ndim = 4
我也评论Deconvolution,因为我不知道output_shape应该是什么 . 我知道最后我应该重建输入图像 .
3-在 model.fit
中,我没有标记数据,因为我正在进行无监督学习,我应该这样做还是什么?
1 回答
问题在于:
错误
input_shape
- 数据应裁剪为视频5-d
格式 . 它完成了reshaping
和裁剪 .将
TimeDistributed
添加到conv
和deconv
图层 .将
deconv
输出形状更改为适当的值 .将
border_mode
更改为same
.所有其他细节可以在问题的评论中找到 .