PaddleX代码GitHub链接:https://github.com/PaddlePaddle/PaddleX/tree/develop
PaddleX文档链接:https://paddlex.readthedocs.io/zh_CN/latest/index.html
PaddleX官网链接:https://www.paddlepaddle.org.cn/paddle/paddlex
MobileNetV3是在MobileNetV2的基础上提出的网络,其计算量小,参数少,相比其他轻量级网络,取得很好的效果。同时以ResNetXt101_32x16d_wsl为teacher模型,运用SSLD(简单的半监督标签知识蒸馏)方式蒸馏出MobileNetV3_large模型,作为预训练模型;相对比原有的MobileNetV3预训练模型,在参数量不变的情况下,MobileNetV3_ssld预训练模型在ImageNet数据集上的精度提升3%,有助于用户进一步提升在自定义数据集上模型训练的效果。本示例只在一个小数据集上展示了如何通过PaddleX进行训练。
! pip install paddlex -i https://mirror.baidu.com/pypi/simple
以下是安装步骤及结果。
Looking in indexes: https://mirror.baidu.com/pypi/simple
Collecting paddlex
Downloading https://mirror.baidu.com/pypi/packages/a2/84/d68acc5b8b4593d8850798eb791a0909a6cbb02ddc3b357cf292d8847147/paddlex-0.1.5-py3-none-any.whl (190kB)
|████████████████████████████████| 194kB 11.9MB/s eta 0:00:01
Collecting paddlehub>=1.6.2 (from paddlex)
Downloading https://mirror.baidu.com/pypi/packages/6e/07/d4839d63853c01d2f9d040ff079e63e007c9e4084e74f447baf46b426811/paddlehub-1.6.2-py3-none-any.whl (207kB)
|████████████████████████████████| 215kB 47.9MB/s eta 0:00:01
Requirement already satisfied: tqdm in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from paddlex) (4.36.1)
Requirement already satisfied: visualdl==1.3.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from paddlex) (1.3.0)
Requirement already satisfied: pyyaml in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from paddlex) (5.1.2)
Collecting pycocotools (from paddlex)
Downloading https://mirror.baidu.com/pypi/packages/96/84/9a07b1095fd8555ba3f3d519517c8743c2554a245f9476e5e39869f948d2/pycocotools-2.0.0.tar.gz (1.5MB)
|████████████████████████████████| 1.5MB 14.5MB/s eta 0:00:01
Collecting colorama (from paddlex)
Downloading https://mirror.baidu.com/pypi/packages/c9/dc/45cdef1b4d119eb96316b3117e6d5708a08029992b2fee2c143c7a0a5cc5/colorama-0.4.3-py2.py3-none-any.whl
Collecting paddleslim==1.0.1 (from paddlex)
Downloading https://mirror.baidu.com/pypi/packages/69/3c/880afac020e3393da5a55b4e0b504d2b644a7ebe91092d953185f09660d1/paddleslim-1.0.1-py2.py3-none-any.whl (103kB)
|████████████████████████████████| 112kB 12.5MB/s eta 0:00:01
Requirement already satisfied: Pillow in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from paddlehub>=1.6.2->paddlex) (6.2.0)
Requirement already satisfied: sentencepiece in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from paddlehub>=1.6.2->paddlex) (0.1.85)
Requirement already satisfied: six>=1.10.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from paddlehub>=1.6.2->paddlex) (1.12.0)
Requirement already satisfied: pandas; python_version >= "3" in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from paddlehub>=1.6.2->paddlex) (0.23.4)
Requirement already satisfied: pre-commit in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from paddlehub>=1.6.2->paddlex) (1.21.0)
Requirement already satisfied: gunicorn>=19.10.0; sys_platform != "win32" in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from paddlehub>=1.6.2->paddlex) (20.0.4)
Requirement already satisfied: opencv-python in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from paddlehub>=1.6.2->paddlex) (4.1.1.26)
Requirement already satisfied: protobuf>=3.6.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from paddlehub>=1.6.2->paddlex) (3.10.0)
Requirement already satisfied: tensorboard>=1.15 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from paddlehub>=1.6.2->paddlex) (2.1.0)
Requirement already satisfied: nltk in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from paddlehub>=1.6.2->paddlex) (3.4.5)
Requirement already satisfied: cma==2.7.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from paddlehub>=1.6.2->paddlex) (2.7.0)
Requirement already satisfied: flake8 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from paddlehub>=1.6.2->paddlex) (3.7.9)
Requirement already satisfied: numpy; python_version >= "3" in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from paddlehub>=1.6.2->paddlex) (1.16.4)
Requirement already satisfied: flask>=1.1.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from paddlehub>=1.6.2->paddlex) (1.1.1)
Requirement already satisfied: requests in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from paddlehub>=1.6.2->paddlex) (2.22.0)
Requirement already satisfied: tb-paddle in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from paddlehub>=1.6.2->paddlex) (0.3.6)
Requirement already satisfied: colorlog in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from paddlehub>=1.6.2->paddlex) (4.1.0)
Requirement already satisfied: yapf==0.26.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from paddlehub>=1.6.2->paddlex) (0.26.0)
Requirement already satisfied: chardet==3.0.4 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from paddlehub>=1.6.2->paddlex) (3.0.4)
Requirement already satisfied: scipy in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from visualdl==1.3.0->paddlex) (1.3.0)
Requirement already satisfied: pytz>=2011k in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from pandas; python_version >= "3"->paddlehub>=1.6.2->paddlex) (2019.3)
Requirement already satisfied: python-dateutil>=2.5.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from pandas; python_version >= "3"->paddlehub>=1.6.2->paddlex) (2.8.0)
Requirement already satisfied: cfgv>=2.0.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from pre-commit->paddlehub>=1.6.2->paddlex) (2.0.1)
Requirement already satisfied: nodeenv>=0.11.1 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from pre-commit->paddlehub>=1.6.2->paddlex) (1.3.4)
Requirement already satisfied: importlib-metadata; python_version < "3.8" in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from pre-commit->paddlehub>=1.6.2->paddlex) (0.23)
Requirement already satisfied: aspy.yaml in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from pre-commit->paddlehub>=1.6.2->paddlex) (1.3.0)
Requirement already satisfied: virtualenv>=15.2 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from pre-commit->paddlehub>=1.6.2->paddlex) (16.7.9)
Requirement already satisfied: identify>=1.0.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from pre-commit->paddlehub>=1.6.2->paddlex) (1.4.10)
Requirement already satisfied: toml in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from pre-commit->paddlehub>=1.6.2->paddlex) (0.10.0)
Requirement already satisfied: setuptools>=3.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from gunicorn>=19.10.0; sys_platform != "win32"->paddlehub>=1.6.2->paddlex) (41.4.0)
Requirement already satisfied: wheel>=0.26; python_version >= "3" in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from tensorboard>=1.15->paddlehub>=1.6.2->paddlex) (0.33.6)
Requirement already satisfied: google-auth<2,>=1.6.3 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from tensorboard>=1.15->paddlehub>=1.6.2->paddlex) (1.10.0)
Requirement already satisfied: absl-py>=0.4 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from tensorboard>=1.15->paddlehub>=1.6.2->paddlex) (0.8.1)
Requirement already satisfied: markdown>=2.6.8 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from tensorboard>=1.15->paddlehub>=1.6.2->paddlex) (3.1.1)
Requirement already satisfied: google-auth-oauthlib<0.5,>=0.4.1 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from tensorboard>=1.15->paddlehub>=1.6.2->paddlex) (0.4.1)
Requirement already satisfied: werkzeug>=0.11.15 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from tensorboard>=1.15->paddlehub>=1.6.2->paddlex) (0.16.0)
Requirement already satisfied: grpcio>=1.24.3 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from tensorboard>=1.15->paddlehub>=1.6.2->paddlex) (1.26.0)
Requirement already satisfied: pycodestyle<2.6.0,>=2.5.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from flake8->paddlehub>=1.6.2->paddlex) (2.5.0)
Requirement already satisfied: mccabe<0.7.0,>=0.6.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from flake8->paddlehub>=1.6.2->paddlex) (0.6.1)
Requirement already satisfied: entrypoints<0.4.0,>=0.3.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from flake8->paddlehub>=1.6.2->paddlex) (0.3)
Requirement already satisfied: pyflakes<2.2.0,>=2.1.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from flake8->paddlehub>=1.6.2->paddlex) (2.1.1)
Requirement already satisfied: Jinja2>=2.10.1 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from flask>=1.1.0->paddlehub>=1.6.2->paddlex) (2.10.3)
Requirement already satisfied: itsdangerous>=0.24 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from flask>=1.1.0->paddlehub>=1.6.2->paddlex) (1.1.0)
Requirement already satisfied: click>=5.1 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from flask>=1.1.0->paddlehub>=1.6.2->paddlex) (7.0)
Requirement already satisfied: idna<2.9,>=2.5 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from requests->paddlehub>=1.6.2->paddlex) (2.8)
Requirement already satisfied: certifi>=2017.4.17 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from requests->paddlehub>=1.6.2->paddlex) (2019.9.11)
Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from requests->paddlehub>=1.6.2->paddlex) (1.25.6)
Requirement already satisfied: moviepy in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from tb-paddle->paddlehub>=1.6.2->paddlex) (1.0.1)
Requirement already satisfied: zipp>=0.5 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from importlib-metadata; python_version < "3.8"->pre-commit->paddlehub>=1.6.2->paddlex) (0.6.0)
Requirement already satisfied: pyasn1-modules>=0.2.1 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from google-auth<2,>=1.6.3->tensorboard>=1.15->paddlehub>=1.6.2->paddlex) (0.2.7)
Requirement already satisfied: cachetools<5.0,>=2.0.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from google-auth<2,>=1.6.3->tensorboard>=1.15->paddlehub>=1.6.2->paddlex) (4.0.0)
Requirement already satisfied: rsa<4.1,>=3.1.4 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from google-auth<2,>=1.6.3->tensorboard>=1.15->paddlehub>=1.6.2->paddlex) (4.0)
Requirement already satisfied: requests-oauthlib>=0.7.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from google-auth-oauthlib<0.5,>=0.4.1->tensorboard>=1.15->paddlehub>=1.6.2->paddlex) (1.3.0)
Requirement already satisfied: MarkupSafe>=0.23 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from Jinja2>=2.10.1->flask>=1.1.0->paddlehub>=1.6.2->paddlex) (1.1.1)
Requirement already satisfied: decorator<5.0,>=4.0.2 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from moviepy->tb-paddle->paddlehub>=1.6.2->paddlex) (4.4.0)
Requirement already satisfied: imageio<3.0,>=2.5; python_version >= "3.4" in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from moviepy->tb-paddle->paddlehub>=1.6.2->paddlex) (2.6.1)
Requirement already satisfied: imageio-ffmpeg>=0.2.0; python_version >= "3.4" in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from moviepy->tb-paddle->paddlehub>=1.6.2->paddlex) (0.3.0)
Requirement already satisfied: proglog<=1.0.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from moviepy->tb-paddle->paddlehub>=1.6.2->paddlex) (0.1.9)
Requirement already satisfied: more-itertools in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from zipp>=0.5->importlib-metadata; python_version < "3.8"->pre-commit->paddlehub>=1.6.2->paddlex) (7.2.0)
Requirement already satisfied: pyasn1<0.5.0,>=0.4.6 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from pyasn1-modules>=0.2.1->google-auth<2,>=1.6.3->tensorboard>=1.15->paddlehub>=1.6.2->paddlex) (0.4.8)
Requirement already satisfied: oauthlib>=3.0.0 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from requests-oauthlib>=0.7.0->google-auth-oauthlib<0.5,>=0.4.1->tensorboard>=1.15->paddlehub>=1.6.2->paddlex) (3.1.0)
Building wheels for collected packages: pycocotools
Building wheel for pycocotools (setup.py) ... done
Created wheel for pycocotools: filename=pycocotools-2.0.0-cp37-cp37m-linux_x86_64.whl size=286744 sha256=8b7a096d43a42eef60b0cc3f9d3a57069e3b5466c58c814e9dbc9be73426a284
Stored in directory: /home/aistudio/.cache/pip/wheels/87/53/98/53ebc0e2e042812e7626cb4e1e9e5418a7e77c187d1719620f
Successfully built pycocotools
Installing collected packages: paddlehub, pycocotools, colorama, paddleslim, paddlex
Found existing installation: paddlehub 1.5.0
Uninstalling paddlehub-1.5.0:
Successfully uninstalled paddlehub-1.5.0
Successfully installed colorama-0.4.3 paddlehub-1.6.2 paddleslim-1.0.1 paddlex-0.1.5 pycocotools-2.0.0
下载并解压数据集
#下载数据集
! wget https://bj.bcebos.com/paddlex/datasets/makeup.tar.gz
#解压数据集makeup
! tar xzf makeup.tar.gz
#jupyter中使用paddle需要设置matplotlib
import matplotlib
matplotlib.use('Agg')
#设置使用0号GPU(如无GPU,执行此代码后仍会使用GPU训练模型)
import os
os.environ['CUDA_VISIBLE_DEVICES'] = '0'
import paddle as pdx
定义训练和验证过程中,图像的处理流程,其中训练过程包括了部分数据增强操作(验证时不需要),在本示例中,训练过程中使用了RandomCrop和RandomHorizontalFlip两种数据增强方式。
from paddlex.cls import transforms
train_transform = transforms.Compose([
transforms.RandomCrop(crop_size=224),
transforms.RandomHorizontalFlip(),
transforms.Normalize()
])
eval_transforms = transforms.Compose([
transforms.ResizeByShort(short_size=256),
transforms.CenterCrop(crop_size=224),
transforms.Normalize()
])
使用PaddleX内置的数据集读取器读取训练和验证数据集,并应用上面配置的图像处理流程。在图像分类中使用ImageNet格式的数据集,因此这里使用pdx.datasets.ImageNet来加载数据集。
train_dataset = pdx.datasets.ImageNet(
data_dir='makeup',
file_list='makeup/train_list.txt',
label_list='makeup/labels.txt',
transforms=train_transforms,
shuffle=True)
eval_dataset = pdx.datasets.ImageNet(
data_dir=makeup',
file_list='makeup/val_list.txt',
label_list='makeup/labels.txt',
transforms=eval_transforms)
在定义好数据集后,即可选择分类模型(这里使用了MobileNetV3_large_ssld模型),开始训练。MobileNetV3_large是面向移动端应用场景的模型,而MobileNetV3_large_ssld是百度通过SSLD蒸馏策略所得的模型,具有更好的精度表现。在如下代码中,模型训练过程每间隔save_interval_epochs轮会保存一次模型在save_dir目录下,同时在保存过程中也会在验证数据集上计算相关指标。
本数据集在P40 GPU上训练MobileNetV3_large_ssld,模型的训练过程中预估为10分钟,如无GPU,则预估为30分钟。
num_classes = len(train_dataset.labels)
model = pdx.cls.MobileNetV3_large_ssld(num_classes=num_classes)
model.train(num_epochs=10,
train_dataset=train_dataset,
train_batch_size=32,
eval_dataset=eval_dataset,
lr_decay_epochs=[4,6,8],
save_interval_epochs=1,
learning_rate=0.025,
save_dir='outpit/mobilenetv3_large_ssla')
result = modle.predict('makeup/mascara/27.jpg',topk=1)
print("Predict Resule:",result)