# train and test path
train_path ='train'
test_path ='test'# get the names of train set and test set
train_filenames = os.listdir(train_path)# list[str] 25000
test_filenames = os.listdir(test_path)# get the train categories
categories =[]for train_file in train_filenames:
categories.append(train_file.split('.')[0])# get the dataframe format of train set and test set
train_df = pd.DataFrame({'image': train_filenames,'category': categories
})
test_df = pd.DataFrame({'image': test_filenames
})
batch_size =32
inception_path ='gap_InceptionV3.h5'
resnet_path ='gap_ResNet50.h5'
xception_path ='gap_Xception.h5'
# get the train features and test features of pre models
resnet_train_features = np.array([])
resnet_test_features = np.array([])
resnet_train_categories = np.array([])ifnot os.path.lexists(resnet_path):
resnet_train_features, resnet_test_features, resnet_train_categories = model_output_features(
tf.keras.applications.ResNet50,(train_df, test_df),(224,224),
batch_size=batch_size,
lambda_func=tf.keras.applications.resnet50.preprocess_input)else:with h5py.File(resnet_path,'r')as h:
resnet_train_features = np.array(h['train'])
resnet_test_features = np.array(h['test'])
resnet_train_categories = np.array(h['label'])
inception_train_features = np.array([])
inception_test_features = np.array([])
inception_train_categories = np.array([])ifnot os.path.lexists(inception_path):
inception_train_features, inception_test_features, inception_train_categories = model_output_features(
tf.keras.applications.InceptionV3,(train_df, test_df),(299,299),
batch_size=batch_size,
lambda_func=tf.keras.applications.inception_v3.preprocess_input)else:with h5py.File(inception_path,'r')as h:
inception_train_features = np.array(h['train'])
inception_test_features = np.array(h['test'])
inception_train_categories = np.array(h['label'])
xception_train_features = np.array([])
xception_test_features = np.array([])
xception_train_categories = np.array([])ifnot os.path.lexists(xception_path):
xception_train_features, xception_test_features, xception_train_categories = model_output_features(
tf.keras.applications.Xception,(train_df, test_df),(299,299),
batch_size=batch_size,
lambda_func=tf.keras.applications.xception.preprocess_input)else:with h5py.File(xception_path,'r')as h:
xception_train_features = np.array(h['train'])
xception_test_features = np.array(h['test'])
xception_train_categories = np.array(h['label'])# combine the train_features and test features# np.array
train_features = np.concatenate((resnet_train_features,
inception_train_features,
xception_train_features,),
axis=1)# concatenate must the same size# ndarray
test_features = np.concatenate((resnet_test_features,
inception_test_features,
xception_test_features,),
axis=1)
train_categories = resnet_train_categories
# shuffle the train_features and test_features
train_features, train_categories = sklearn.utils.shuffle(train_features, train_categories, random_state=0)
自定义模型
自定义模型,在dropout之后,直接接给sigmoid,训练速度很快。
# DIY the model using the features of pre-trained models
input_tensor = tf.keras.Input(train_features.shape[1:])# (25000, 6144)
x = tf.keras.layers.Dropout(0.5)(input_tensor)
x = tf.keras.layers.Dense(1, activation='sigmoid')(x)
model = tf.keras.models.Model(input_tensor, x)# model.summary()# compile/config the model
model.compile(optimizer='adadelta',
loss='binary_crossentropy',
metrics=['accuracy'])# train the DIY-model# for i in train_categories:# print(i)
model.fit(train_features, train_categories, batch_size=128, epochs=100, validation_split=0.1)