2024年大数据最新MobileNet实战:tensorflow2(7)

文章讲述了如何使用Python和Keras实现图片数据生成器,用于训练InceptionV3和MobileNet模型,涉及数据预处理、模型训练、学习率调整策略和模型评估。作者还讨论了在训练过程中使用的ModelCheckpoint和ReduceLROnPlateau回调函数。
摘要由CSDN通过智能技术生成
  • 分别从file_pathList和labels,得到图片的路径和对应的label

  • 读取图片

  • 如果是训练就训练的transform,如果不是就执行验证的transform。

  • resize图片

  • 将image转数组

  • 将图像和label分别放到input_samples和input_labels

  • 将list转numpy数组。

  • 返回一次迭代

def generator(file_pathList,labels,batch_size,train_action=False):

L = len(file_pathList)

while True:

input_labels = []

input_samples = []

for row in range(0, batch_size):

temp = np.random.randint(0, L)

X = file_pathList[temp]

Y = labels[temp]

image = cv2.imdecode(np.fromfile(X, dtype=np.uint8), -1)

if image.shape[2] > 3:

image = image[:, :, :3]

if train_action:

image=train_transform(image=image)[‘image’]

else:

image = val_transform(image=image)[‘image’]

image = cv2.resize(image, (norm_size, norm_size), interpolation=cv2.INTER_LANCZOS4)

image = img_to_array(image)

input_samples.append(image)

input_labels.append(Y)

batch_x = np.asarray(input_samples)

batch_y = np.asarray(input_labels)

yield (batch_x, batch_y)

第五步 保留最好的模型和动态设置学习率


ModelCheckpoint:用来保存成绩最好的模型。

语法如下:

keras.callbacks.ModelCheckpoint(filepath, monitor=‘val_loss’, verbose=0, save_best_only=False, save_weights_only=False, mode=‘auto’, period=1)

该回调函数将在每个epoch后保存模型到filepath

filepath可以是格式化的字符串,里面的占位符将会被epoch值和传入on_epoch_end的logs关键字所填入

例如,filepath若为weights.{epoch:02d-{val_loss:.2f}}.hdf5,则会生成对应epoch和验证集loss的多个文件。

参数

  • filename:字符串,保存模型的路径
  • monitor:需要监视的值
  • verbose:信息展示模式,0或1
  • save_best_only:当设置为True时,将只保存在验证集上性能最好的模型
  • mode:‘auto’,‘min’,‘max’之一&#
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值