基于PaddleClas的热轧钢带表面缺陷分类
!unzip -q data/data47421/PaddleClas.zip
!unzip -q data/data47260/NEU-CLS.zip
!mv NEU-CLS PaddleClas/dataset
import codecs
import os
import random
import shutil
from PIL import Image
train_ratio = 4.0 / 5
all_file_dir = 'PaddleClas/dataset/NEU-CLS'
class_list = [c for c in os.listdir(all_file_dir) if os.path.isdir(os.path.join(all_file_dir, c)) and not c.endswith('Set') and not c.startswith('.')]
class_list.sort()
print(class_list)
train_image_dir = os.path.join(all_file_dir, "trainImageSet")
if not os.path.exists(train_image_dir):
os.makedirs(train_image_dir)
eval_image_dir = os.path.join(all_file_dir, "evalImageSet")
if not os.path.exists(eval_image_dir):
os.makedirs(eval_image_dir)
train_file = codecs.open(os.path.join(all_file_dir, "train.txt"), 'w')
eval_file = codecs.open(os.path.join(all_file_dir, "eval.txt"), 'w')
with codecs.open(os.path.join(all_file_dir, "label_list.txt"), "w") as label_list:
label_id = 0
for class_dir in class_list:
label_list.write("{0}\t{1}\n".format(label_id, class_dir))
image_path_pre = os.path.join(all_file_dir, class_dir)
for file in os.listdir(image_path_pre):
try:
img = Image.open(os.path.join(image_path_pre, file))
if random.uniform(0, 1) <= train_ratio:
shutil.copyfile(os.path.join(image_path_pre, file), os.path.join(train_image_dir, file))
train_file.write("{0} {1}\n".format(os.path.join("trainImageSet", file), label_id))
else:
shutil.copyfile(os.path.join(image_path_pre, file), os.path.join(eval_image_dir, file))
eval_file.write("{0} {1}\n".format(os.path.join("evalImageSet", file), label_id))
except Exception as e:
pass
# 存在一些文件打不开,此处需要稍作清洗
label_id += 1
train_file.close()
eval_file.close()
! head PaddleClas/dataset/NEU-CLS/train.txt
%cd PaddleClas
import os
os.environ['PYTHONPATH']="/home/aistudio/PaddleClas"
!python ../download_model.py ResNet50_vd_pretrained
!mv ../ResNet50_vd_pretrained ./
!python -m paddle.distributed.launch --selected_gpus="0" tools/train.py -c ../neu.yaml
!python -m paddle.distributed.launch --selected_gpus="0" tools/eval.py \
-c ../eval.yaml \
-o pretrained_model=output/ResNet50_vd/best_model/ppcls
!python tools/export_model.py \
--model='ResNet50_vd' \
--pretrained_model=output/ResNet50_vd/best_model/ppcls \
--output_path=./inference
!python tools/infer/predict.py \
-m inference/model \
-p inference/params \
-i "dataset/NEU-CLS/liewen_1.bmp" \
--use_gpu=1
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-sKwlBGYP-1634298183330)(E:\图像处理数据集\rygd6qx8738\fruits\liewen_1.bmp)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-H6dwTcs8-1634298183332)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\image-20211015151223245.png)]
0——Cr
1——Pa
2——PS
3——RS
4——Sc
5——In
轧制氧化皮(RS),斑块(Pa),开裂(Cr),点蚀表面( PS),内含物(In)和划痕(Sc)