nb_classes = 1 # number of classes
based_model_last_block_layer_number = 126 # value is based on based model
selected.
img_width, img_height = 299, 299 # change based on the shape/structure of
your images
batch_size = 3 # try 4, 8, 16, 32, 64, 128, 256 dependent on CPU/GPU memory
capacity (powers of 2 values).
nb_epoch = 100 # number of iteration the algorithm gets trained.
learn_rate = 1e-4 # sgd learning rate
momentum = .9 # sgd momentum to avoid local minimum
transformation_ratio = .05 # how aggressive will be the data
augmentation/transformation
train_datagen = ImageDataGenerator(rescale=1. / 255,
rotation_range=transformation_ratio,
shear_range=transformation_ratio,
zoom_range=transformation_ratio,
cval=transformation_ratio,
horizontal_flip=True,
vertical_flip=True)
validation_datagen = ImageDataGenerator(rescale=1. / 255)
os.makedirs(os.path.join(os.path.abspath(train_data_dir), '../preview'), exist_ok=True)
train_generator = train_datagen.flow_from_directory(train_data_dir,
target_size=(img_width, img_height),
batch_size=batch_size,
class_mode='binary')
validation_generator = validation_datagen.flow_from_directory(validation_data_dir,
target_size=(img_width, img_height),
batch_size=batch_size,
class_mode='binary')
# Optimizer
sgd = SGD(lr=learn_rate, decay=1e-6, momentum=momentum, nesterov=True)
class_weightage = class_weight.compute_class_weight('balanced', np.unique(train_generator.classes),
train_generator.classes)
class_weightage = {0: class_weightage[0], 1: class_weightage[1]}
print(class_weightage)
model.compile(optimizer=sgd,
loss='binary_crossentropy',
metrics=['binary_accuracy'])
final_weights_path = os.path.join(os.path.abspath(model_path), 'model_weights.h5')
callbacks_list = [
ModelCheckpoint(final_weights_path, monitor='val_binary_accuracy', verbose=1, save_best_only=True),
EarlyStopping(monitor='val_binary_accuracy', patience=5, verbose=0),
TensorBoard(log_dir='./Graph', histogram_freq=0, write_graph=True, write_images=True)
]
# fine-tune the model
history = model.fit_generator(train_generator,
epochs=nb_epoch,
validation_data=validation_generator,
class_weight=class_weightage,
callbacks=callbacks_list)
这是我用来训练XCeption模型的代码,使用imagenet权重进行了初始化,下面是我用来预测类的代码
def local_load_image(image_file):
img = image.load_img(image_file, target_size=(299, 299))
image_array = image.img_to_array(img)
image_array = preprocess_input(np.expand_dims(image_array, axis=0))
return image_array
prediction = model.predict(
local_load_image(path_to_image)
)
无论我做什么,输出预测始终为1。我正在尝试对简单的二进制数据集(狗与猫)进行分类,但没有成功。我将所有图层都设为可训练,因为BN图层会混淆训练和预测。我还尝试使用其他架构,例如VGG16和ResNet。我尝试用class_weights来解决过度拟合问题,并将学习率降低到1e-4。
我不知道为什么我的模型不能训练任何东西,而只能预测所有东西。
在11个时代之后,我可以实现
12269/12269 [==============================] - 5983s 488ms/step - loss: 0.4465 - binary_accuracy: 0.8106 val_loss: 0.4395 - val_binary_accuracy: 0.8107
但是所有的预测仍然惨败。任何帮助将不胜感激!