问题描述
我正在尝试使用 tuorial 为图像分类模型训练 keras ResNet50
模型。安装 inbult 数据生成器后,我想使用 albumentations 进行扩充。
from albumentations import Compose
transforms = Compose([HorizontalFlip()])
我读了几篇文章,但我不知道如何实现相册。
我应该修改哪一行代码来实现相册。
删除不必要的行后,我正在复制下面的代码。
NUM_CLASSES = 2
CHANNELS = 3
IMAGE_RESIZE = 224
RESNET50_POOLING_AVERAGE = 'avg'
DENSE_LAYER_ACTIVATION = 'softmax'
OBJECTIVE_FUNCTION = 'categorical_crossentropy'
LOSS_METRICS = ['accuracy']
NUM_EPOCHS = 300
EARLY_STOP_PATIENCE = 20
STEPS_PER_EPOCH_TRAINING = 20
STEPS_PER_EPOCH_VALIDATION = 20
BATCH_SIZE_TRAINING = 10
BATCH_SIZE_VALIDATION = 10
# %% ---------------------------------------------------------------------
TrainingData_directory = 'C:/datafolder/Train'
ValidationData_directory = 'C:/datafolder/Validation'
ModelCheckpointPath = 'C:/datafolder/ResNet50_Weights.hdf5'
# %% ---------------------------------------------------------------------
from albumentations import Compose
import tensorflow as tf
from tensorflow.keras.applications import ResNet50
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
# %% ---------------------------------------------------------------------
model = Sequential()
model.add(ResNet50(include_top = False,pooling = RESNET50_POOLING_AVERAGE,weights = 'imagenet'))
model.add(Dense(NUM_CLASSES,activation = DENSE_LAYER_ACTIVATION))
model.layers[0].trainable = False
from tensorflow.keras import optimizers
sgd = optimizers.SGD(lr = 0.001,decay = 1e-6,momentum = 0.9,nesterov = True)
model.compile(optimizer = sgd,loss = OBJECTIVE_FUNCTION,metrics = LOSS_METRICS)
from keras.applications.resnet50 import preprocess_input
from keras.preprocessing.image import ImageDataGenerator
image_size = IMAGE_RESIZE
data_generator = ImageDataGenerator(preprocessing_function = preprocess_input)
train_generator = data_generator.flow_from_directory(TrainingData_directory,target_size = (image_size,image_size),batch_size = BATCH_SIZE_TRAINING,class_mode = 'categorical')
validation_generator = data_generator.flow_from_directory(ValidationData_directory,batch_size = BATCH_SIZE_VALIDATION,class_mode = 'categorical')
from tensorflow.python.keras.callbacks import EarlyStopping,ModelCheckpoint
cb_early_stopper = EarlyStopping(monitor = 'val_loss',patience = EARLY_STOP_PATIENCE)
cb_checkpointer = ModelCheckpoint(filepath = ModelCheckpointPath,monitor = 'val_loss',save_best_only = True,mode = 'auto')
fit_history = model.fit_generator(
train_generator,steps_per_epoch=STEPS_PER_EPOCH_TRAINING,epochs = NUM_EPOCHS,validation_data=validation_generator,validation_steps=STEPS_PER_EPOCH_VALIDATION,callbacks=[cb_checkpointer,cb_early_stopper]
)
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)