我是深度学习的初学者,希望您能帮助我解决问题。
我想创建一个CNN模型,该模型接受两个图像输入,并产生一个输出,该输出是两个图像的类。该模型从数据集类型1获取一幅图像,从数据集类型2获取一幅图像。 我有两个数据集:type1和type2,每个数据集包含相同数量的类,但是数据集type1中每个类的图像数量大于数据集type2中每个类的图像数量。以下是数据集的结构。
模型应从Type1数据集中获取一幅图像,从Type2数据集中获取一幅图像,然后将这些图像分类为一类(ClassA或ClassB或------)。
Type1 dataset
|Train
|ClassA
|image1
|image2
|image3
|image4
-----
|ClassB
|image1
|image2
|image3
|image4
-----
|ClassC
|image1
|image2
|image3
|image4
-----
|ClassD
|image1
|image2
|image3
|image4
-----
----------------
|Validate
-----------
|Test
--------------
Type2 dataset
|Train
|ClassA
|image1
|image2
-----
|ClassB
|image1
|image2
-----
|ClassC
|image1
|image2
-----
|ClassD
|image1
|image2
-----
----------------
|Validate
-----------
|Test
--------------
因此,我想创建一个模型来输入两个图像(来自类型1和2),只要它们来自同一类即可。另外,我希望将来自type1的每个图像与来自同一类的type2的每个图像配对。我该怎么办??
代码:
in1 = Input(...)
x = Conv2D(...)(in1)
--------
--------
out1 = Dense(...)(x)
in2 = Input(...)
x = Conv2D(...)(in2)
--------
--------
out2 = Dense(...)(x)
concatenated_layer = concatenate([out1, out2]) # merge the outputs of the two models
output_layer = Dense(no_classes, activation='softmax', name='prediction')(concatenated_layer)
modal= Model(inputs=[in1, in2], outputs=[output_layer])
input_imgen = ImageDataGenerator(rescale = 1./255,
shear_range = 0.2,
zoom_range = 0.2,
rotation_range=5.,
horizontal_flip = True)
test_imgen = ImageDataGenerator()
def generate_generator_multiple(generator,dir1, dir2, batch_size, img_height,img_width):
genX1 = generator.flow_from_directory(dir1,
target_size = (img_height,img_width),
class_mode = 'categorical',
batch_size = batch_size,
shuffle=False,
seed=7)
genX2 = generator.flow_from_directory(dir2,
target_size = (img_height,img_width),
class_mode = 'categorical',
batch_size = batch_size,
shuffle=False,
seed=7)
while True:
X1i = genX1.next()
X2i = genX2.next()
yield [X1i[0], X2i[0]], X2i[1] #Yield both images and their mutual label
inputgenerator=generate_generator_multiple(generator=input_imgen,
dir1=train_iris_data,
dir2=train_face_data,
batch_size=32,
img_height=224,
img_width=224)
testgenerator=generate_generator_multiple(generator=test_imgen,
dir1=valid_iris_data,
dir2=valid_face_data,
batch_size=1,
img_height=224,
img_width=224)