Python Keras为ConvLSTM导入同一对象的两个图像
我试着用Keras的循环模型ConvLSTM2D来建立我的神经网络。我的目标是从不同角度拍摄同一物体的两张图像,并根据这两张图像的特征,尝试确定它是什么类型的物体 图像生成并存储在单独的文件夹中,但路径相似,因此:Python Keras为ConvLSTM导入同一对象的两个图像,python,keras,Python,Keras,我试着用Keras的循环模型ConvLSTM2D来建立我的神经网络。我的目标是从不同角度拍摄同一物体的两张图像,并根据这两张图像的特征,尝试确定它是什么类型的物体 图像生成并存储在单独的文件夹中,但路径相似,因此: -dir1 -> class1 -> image001.jpg -> class2 -> image101.jpg -dir2 -> class1 -> image001.jpg -> class2 -> im
-dir1 -> class1 -> image001.jpg
-> class2 -> image101.jpg
-dir2 -> class1 -> image001.jpg
-> class2 -> image101.jpg
来自dir1和dir2的image001.jpg属于同一对象,但角度不同
我的问题是:
ConvLSTM2D以额外的时间维度拍摄图像。所以我的图像以前是128128,1,现在我将使用np.stack将其转换为2128128,1。但是,我不知道如何:
生成时匹配图像
仅堆叠图像,而不堆叠标签
我的尝试
我已尝试使用所描述的解决方案解决此问题。据我所知,保持种子相同将确保两个图像是同一个对象。然而,当模型试图使用图像时,问题就出现了。Keras有以下输出消息:
ValueError: Error when checking model input: the list of Numpy arrays that you are
passing to your model is not the size the model expected. Expected to see 1 array(s)
, but instead got the following list of 2 arrays:
[array([[[[0.07843138],
[0.02745098],
[0.07450981],
...,
[0.02745098],
[0.03137255],
[0.0509804 ]],
[[0.05490196],
[0.10980393],...
这让我觉得我需要使用numpy.stack来堆叠这两个图像,这是我接下来尝试的。但是,这导致了以下错误:
ValueError: Error when checking input: expected conv_lst_m2d_1_input to have shape
(2, 128, 128, 1) but got array with shape (100, 128, 128, 1)
我可以理解这个错误,因为我已经说明了我的输入有无、无图像、宽度、高度、通道的尺寸。因为它是一个双峰图像,所以2是图像的数量。模型得到的实际输入是100128128,1,其中第一个元素是批量大小。所以我认为我没有把这两幅图像正确地叠加到一个时间轴上
这让我对如何解决这个问题感到困惑。我已经很好地定义了我的模型,因此输入的形状是samples=None,2128128,1,但是我不知道如何将我的两个图像转换成相同的格式
我的代码
模型
model = Sequential()
no_of_img = 2
#Adding additional convolution + maxpool layers 15/1/19
model.add(ConvLSTM2D(32, (5,5), batch_input_shape=(batch_size,no_of_img,img_width,img_height,1),return_sequences=True))
model.add(Activation('relu'))
model.add(Dropout(0.4))
model.add(TimeDistributed(MaxPooling2D((2,2))))
model.add(ConvLSTM2D(64, (3,3),return_sequences=True))
model.add(Activation('relu'))
model.add(TimeDistributed(MaxPooling2D(pool_size=(2,2))))
model.add(Dropout(0.2))
model.add(ConvLSTM2D(128, (3,3),return_sequences=True))
model.add(Activation('relu'))
model.add(TimeDistributed(MaxPooling2D(pool_size=(2,2))))
model.add(Dropout(0.2))
model.add(ConvLSTM2D(256, (3,3),return_sequences=True))
model.add(Activation('relu'))
model.add(TimeDistributed(MaxPooling2D(pool_size=(2,2))))
model.add(Dropout(0.2))
model.add(Flatten())
#Possible dense layer with our 128x128 number of pixels is too much, too high. We should add a few convolutional and maxpool layers beforehand.
model.add(Dense(128, #dimensionality of output space
#input_shape=(128,128,1), #Commented out as only the first layer needs input shape.
))
model.add(Activation('relu'))
#model.add(Dropout(0.2))
#model.add(Dense(num_classes, activation='softmax'))
model.add(Dense(num_classes,activation='softmax'))
model.compile(loss='categorical_crossentropy',optimizer='RMSProp',metrics=['accuracy'])
数据准备的当前尝试
我明白了。结果表明,使用np.stack后的输出以时间、批次大小、宽度、高度、通道的形式出现。我使用np.transpose切换时间和批量大小的通道,并将图像精细地输入到模型中
train_datagen = ImageDataGenerator(
rescale=1./255,
#Normalized inputs from 0-255 to 0-1
horizontal_flip=True,
vertical_flip=True)
test_datagen = ImageDataGenerator(rescale=1./255)
def generate_generator_multiple(generator,dir1, dir2, batch_size,
img_width,img_height,subset):
genX1 = generator.flow_from_directory(dir1,
color_mode='grayscale',
target_size=
(img_width,img_height),
batch_size=batch_size,
class_mode='categorical',
shuffle=False,
subset=subset,
seed=1)
#Same seed for consistency.
genX2 = generator.flow_from_directory(dir2,
color_mode='grayscale',
target_size=
(img_width,img_height),
batch_size=batch_size,
class_mode='categorical',
shuffle=False,
subset=subset,
seed=1)
while True:
X1i = genX1.next()
X2i = genX2.next()
yield numpy.stack((X1i[0],X2i[0])),X1i[1] #Yields both images and their mutual label
train_generator = generate_generator_multiple(generator=train_datagen,
dir1=train_data_dirA,
dir2=train_data_dirB,
batch_size=batch_size,
img_width=img_width,
img_height=img_height,
subset='training')
validation_generator
=generate_generator_multiple(generator=test_datagen,
dir1=train_data_dirA,
dir2=train_data_dirB,
batch_size=batch_size,
img_width=img_width,
img_height=img_height,
subset='validation')