我正在尝试训练两个流的CNN体系结构,但是不幸的是,我得到了一个意外的结果,该结果几乎与两个流之一相同(我已经分别训练了每个流)。我的想法是合并来自两个不同图像文件夹(两个来源)的两个流CNN,并将它们组合以获得更准确的结果。 这是我的代码,请告诉我我做错了什么:
## Arch
def fusionNet(nb_classes, inputs=(3, 224, 224)):
input_img = Input(shape=inputs, name='RGB_input')
conv1 = Conv2D(96, (7, 7), activation="relu", name="conv1", strides=(2, 2), padding="same",
## ....
## ....
## ....
firstCNN_output = BatchNormalization(name='firstCNN_output_BN')(last_dropout)
##
print(firstCNN_output._keras_shape) # I got here (None, 512, 13, 13)
input_img2 = Input(shape=inputs, name='depth_input')
conv12 = Conv2D(96, (7, 7), activation="relu", name="conv12", strides=(2, 2), padding="same", kernel_initializer="glorot_uniform")(input_img2)
#....
#....
#...
SecondCNN_output = BatchNormalization(name='SecondCNN_output_BN2')(last_dropout2)
print(SecondCNN_output._keras_shape) # I got here (None, 512, 13, 13)
#
merge2CNN = merge([firstCNN_output, SecondCNN_output], mode='concat', concat_axis=1)
### got here (None, 1024, 13, 13)
conv10 = Conv2D(
nb_classes, (1, 1),init='glorot_uniform',
border_mode='valid', name='conv10')(merge2CNN)
avgpool10 = AveragePooling2D((13, 13), name='avgpool10')(conv10)
flatten = Flatten(name='flatten')(avgpool10)
softmax = Activation("softmax", name='softmax')(flatten)
return Model(inputs=[input_img,input_img2], output=softmax)
## Training
sn = fusionNet(nb_classes=24)
sgd = SGD(lr=0.001, decay=0.0002, momentum=0.9, nesterov=True)
sn.compile( optimizer=sgd, loss='categorical_crossentropy', metrics=['accuracy')
train_datagen = ImageDataGenerator(
rescale=1./255,
zoom_range=0.01,
rotation_range=2.,
featurewise_center=True,
featurewise_std_normalization=True,)
test_datagen = ImageDataGenerator(rescale=1./255)
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)
genX2 = generator.flow_from_directory(dir2,
target_size = (img_height, img_width),
class_mode = 'categorical',
batch_size = batch_size)
while True:
X1i = genX1.next()
X2i = genX2.next()
yield [X1i[0], X2i[0]], X2i[1] #Yield both images and their mutual label
train_generator=generate_generator_multiple(generator=train_datagen,
dir1=train_data_dir,
dir2=train_data_dir_colored,
batch_size=32,
img_height=size,
img_width=size)
validation_generator=generate_generator_multiple(test_datagen,
dir1=validation_data_dir,
dir2=validation_data_dir_colored,
batch_size=batch_size,
img_height=size,
img_width=size)
tensorboardcb = keras.callbacks.TensorBoard(log_dir='/workspace/sqn_224/fusion', histogram_freq=0, batch_size=32, write_graph=True)
sn.fit_generator(
train_generator,
steps_per_epoch=nb_train_samples//32,
nb_epoch=nb_epoch,
validation_data=validation_generator,
validation_steps = nb_validation_samples//32,
callbacks=[tensorboardcb])