123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- import os
- os.environ['CUDA_VISIBLE_DEVICES'] = '0'
- from paddlex import transforms as T
- import paddlex as pdx
- # 定义训练和验证时的transforms
- # API说明https://paddlex.readthedocs.io/zh_CN/develop/apis/transforms/cls_transforms.html
- # train_transforms = T.Compose(
- # [T.RandomCrop(crop_size=224), T.RandomHorizontalFlip(), T.Normalize()])
- train_transforms = T.Compose(
- [T.Resize(target_size=224), T.Normalize()]
- )
- eval_transforms = T.Compose([
- T.Resize(target_size=224), T.Normalize()
- ])
- # 定义训练和验证所用的数据集
- # API说明:https://paddlex.readthedocs.io/zh_CN/develop/apis/datasets.html#paddlex-datasets-imagenet
- train_dataset = pdx.datasets.ImageNet(
- data_dir='/home/xiongweixp/data/handwriting/train',
- file_list='/home/xiongweixp/data/handwriting/train/train_list.txt',
- label_list='/home/xiongweixp/data/handwriting/train/labels.txt',
- transforms=train_transforms,
- shuffle=True)
- eval_dataset = pdx.datasets.ImageNet(
- data_dir='/home/xiongweixp/data/handwriting/train',
- file_list='/home/xiongweixp/data/handwriting/train/val_list.txt',
- label_list='/home/xiongweixp/data/handwriting/train/labels.txt',
- transforms=eval_transforms)
- # 初始化模型,并进行训练
- # 可使用VisualDL查看训练指标,参考https://paddlex.readthedocs.io/zh_CN/develop/train/visualdl.html
- # model = pdx.cls.MobileNetV2(num_classes=len(train_dataset.labels))
- model = pdx.cls.MobileNetV3_small(num_classes=len(train_dataset.labels))
- # API说明:https://paddlex.readthedocs.io/zh_CN/develop/apis/models/classification.html#train
- # 各参数介绍与调整说明:https://paddlex.readthedocs.io/zh_CN/develop/appendix/parameters.html
- model.train(
- num_epochs=10,
- train_dataset=train_dataset,
- train_batch_size=64,
- eval_dataset=eval_dataset,
- lr_decay_epochs=[4, 6, 8],
- learning_rate=0.025,
- save_dir='output/mobilenetv3_small',
- use_vdl=True
- #resume_checkpoint='output/resnet/epoch_9'
- )
|