Suck the cat with code! This paper is participating in[Cat Essay Campaign].
Paddlex’s super simple cat and dog classification
Who does not love the cute little cat, I am no exception, but I want to say what kind, that is difficult for me, I do not know what kind, purely like, like to see, never raised, looking forward to raising a cat……
Project Introduction:
12 cat categories we have done before, first I want to quickly get the classification model, just want the results…
PaddleHub can easily obtain pre-training models under PaddlePaddle ecology, complete model management and one-click prediction. In conjunction with the Fine-Tune API, transfer learning can be quickly accomplished based on large-scale pre-training models, making the pre-training models better suited to user specific scenarios. Why not use PaddleHub when you see this??
- www.paddlepaddle.org.cn/hublist
- Github.com/paddlepaddl…
- Gitee.com/paddlepaddl…
The original work uses ResNet101 as the skeleton of the cat image classification model of deep neural network, cat image classification and target recognition, image classification recognition accuracy can reach 94%.
I want to see if I can take it a step further…
0. Set the environment
Change the environment to paddlePaddle2.0rC1,PaddleHub 2.0.0B2
Import some image processing packages
%cd /home/aistudio
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import os, shutil, cv2, random
%matplotlib inline
Copy the code
/home/aistudio
Copy the code
1. Data processing
! mkdir dataset ! unzip data/data10954/cat_12_train.zip-d dataset/ ! unzip data/data10954/cat_12_test.zip-d dataset/ ! cp data/data10954/train_list.txt dataset/Copy the code
%cd dataset/
Copy the code
/home/aistudio/dataset
Copy the code
Picture data is sorted by directory
import pandas as pd
import os
import shutil
def mkdir() :
for i in range(12):
os.mkdir(str(i))
if __name__ == "__main__":
data = pd.read_csv("train_list.txt", sep="", header=None)
mkdir()
for i, r in data.iterrows():
print(os.path.split(r[0]))
old_file = r[0]
new_file = os.path.join(str(r[1]), os.path.split(r[0[-])1])
shutil.move(old_file, new_file)
print("File resort finished!")
Copy the code
Import the required package
import os
import random
import json
import cv2
import numpy as np
from PIL import Image
import paddle
import matplotlib.pyplot as plt
Convert 4 channels to 3 channels
def proc_img(src) :
for root, dirs, files in os.walk(src):
if '__MACOSX' in root:continue
for file in files:
src=os.path.join(root,file)
img=Image.open(src)
Convert 4 channels to 3 channels
ifimg.mode ! ='RGB':
img = img.convert('RGB')
img.save(src)
if __name__=='__main__':
proc_img("dataset")
Copy the code
! pip install paddlexCopy the code
%cd ~
Copy the code
/home/aistudio
Copy the code
! rm dataset/train_list.txt ! rmdir dataset/cat_12_train/ ! mv dataset/cat_12_test/ ~Copy the code
! paddlex --split_dataset --format ImageNet --dataset_dir dataset/ --val_value 0.2 --test_value 0.1
Copy the code
! pip install imgaugCopy the code
# Environment variable configuration, used to control whether to use the GPU
# documentation: https://paddlex.readthedocs.io/zh_CN/develop/appendix/parameters.html#gpu
import os
os.environ['CUDA_VISIBLE_DEVICES'] = '0'
from paddlex.cls import transforms
import paddlex as pdx
train_transforms = transforms.Compose([
transforms.ResizeByShort(short_size=224),
transforms.RandomCrop(crop_size=64), transforms.RandomHorizontalFlip(),
transforms.Normalize()
])
eval_transforms = transforms.Compose([
transforms.ResizeByShort(short_size=224),
transforms.CenterCrop(crop_size=64), transforms.Normalize()
])
Define the data set for training and validation
# API description: https://paddlex.readthedocs.io/zh_CN/develop/apis/datasets.html#paddlex-datasets-imagenet
train_dataset = pdx.datasets.ImageNet(
data_dir='/home/aistudio/dataset/',
file_list='/home/aistudio/train_list.txt',
label_list='/home/aistudio/labels.txt',
transforms=train_transforms,
shuffle=True)
eval_dataset = pdx.datasets.ImageNet(
data_dir='/home/aistudio/dataset/',
file_list='/home/aistudio/val_list.txt',
label_list='/home/aistudio/labels.txt',
transforms=eval_transforms)
Initialize the model and train it
# can use VisualDL view the training target, the reference https://paddlex.readthedocs.io/zh_CN/develop/train/visualdl.html
# model = pdx.cls.MobileNetV3_small_ssld(num_classes=len(train_dataset.labels))
model = pdx.cls.ResNet101_vd_ssld(num_classes=len(train_dataset.labels))
# API description: https://paddlex.readthedocs.io/zh_CN/develop/apis/models/classification.html#train
# of each parameter is introduced and adjust details: https://paddlex.readthedocs.io/zh_CN/develop/appendix/parameters.html
model.train(
num_epochs=200,
train_dataset=train_dataset,
train_batch_size=256,
eval_dataset=eval_dataset,
lr_decay_epochs=[4.6.8],
learning_rate=0.05,
save_dir='output/ResNet101_vd_ssld')
Copy the code