paddlehub口罩检测模型实例:
#!unzip /home/aistudio/data/data25505/detection.zip
Archive: /home/aistudio/data/data25505/detection.zip
replace detection/test_mask_detection.jpg? [y]es, [n]o, [A]ll, [N]one, [r]ename: ^C
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
test_img_path = ["/home/aistudio/detection/test_mask_detection.jpg"]
img = mpimg.imread(test_img_path[0])
plt.figure(figsize=(10,10))
plt.imshow(img)
plt.axis('off')
plt.show()
**!cat /home/aistudio/detection/test.txt
/home/aistudio/detection/test_mask_detection.jpg
with open('detection/test.txt', 'r') as f:
test_img_path=[]
for line in f:
test_img_path.append(line.strip())
print(test_img_path)
['/home/aistudio/detection/test_mask_detection.jpg']
PaddleHub口罩检测提供了两种预训练模型,pyramidbox_lite_mobile_mask和pyramidbox_lite_server_mask。不同点在于,pyramidbox_lite_mobile_mask是针对于移动端优化过的模型,适合部署于移动端或者边缘检测等算力受限的设备上。
import paddlehub as hub
module =hub.Module(name="pyramidbox_lite_mobile_mask")
# module = hub.Module(name="pyramidbox_lite_server_mask")
[2020-03-20 19:17:18,815] [ INFO] - Installing pyramidbox_lite_mobile_mask module
Downloading pyramidbox_lite_mobile_mask
[==================================================] 100.00%
Uncompress /home/aistudio/.paddlehub/tmp/tmpejfqd2s1/pyramidbox_lite_mobile_mask
[==================================================] 100.00%
[2020-03-20 19:17:20,947] [ INFO] - Successfully installed pyramidbox_lite_mobile_mask-1.2.0
[2020-03-20 19:17:21,054] [ INFO] - Installing pyramidbox_lite_mobile module
Downloading pyramidbox_lite_mobile
[==================================================] 100.00%
Uncompress /home/aistudio/.paddlehub/tmp/tmpw76_shxb/pyramidbox_lite_mobile
[==================================================] 100.00%
[2020-03-20 19:17:21,431] [ INFO] - Successfully installed pyramidbox_lite_mobile-1.1.1
input_dict = {"image": test_img_path}
results = module.face_detection(data=input_dict)
for result in results:
print(result)
img = mpimg.imread("detection_result/test_mask_detection.jpg")
plt.figure(figsize=(10,10))
plt.imshow(img)
plt.axis('off')
plt.show()
[2020-03-20 19:17:25,402] [ INFO] - 133 pretrained paramaters loaded by PaddleHub
[2020-03-20 19:17:25,469] [ INFO] - 149 pretrained paramaters loaded by PaddleHub
image with bbox drawed saved as /home/aistudio/detection_result/test_mask_detection.jpg
{'data': {'label': 'MASK', 'left': 459.0687048435211, 'right': 657.8806104660034, 'top': 191.39037430286407, 'bottom': 444.0621703863144, 'confidence': 0.99913305}, 'id': 1, 'path': '/home/aistudio/detection/test_mask_detection.jpg'}
{'data': {'label': 'NO MASK', 'left': 1166.9745614528656, 'right': 1323.218990802765, 'top': 294.5009618997574, 'bottom': 500.64271688461304, 'confidence' 0.99955636}, 'id': 1, 'path': '/home/aistudio/detection/test_mask_detection.jpg'}
{'data': {'label': 'MASK', 'left': 942.4126571416855, 'right': 1127.8870369195938, 'top': 340.60501903295517, 'bottom': 585.9683722257614, 'confidence': 0.94331}, 'id': 1, 'path': '/home/aistudio/detection/test_mask_detection.jpg'}
{'data': {'label': 'NO MASK', 'left': 1237.7968076467514, 'right': 1443.2840930223465, 'top': 8
得到结果
{'data': {'label': 'MASK', 'left': 457.5789153575897, 'right': 654.8277450799942, 'top': 182.25817680358887, 'bottom': 440.94200134277344, 'confidence': 0.8900112}, 'id': 1}
{'data': {'label': 'MASK', 'left': 945.8848892450333, 'right': 1125.7660418748856, 'top': 340.7917723059654, 'bottom': 578.5958737134933, 'confidence': 0.99627507}, 'id': 1}
{'data': {'label': 'NO MASK', 'left': 1166.5987054109573, 'right': 1323.4498780965805, 'top': 292.07742512226105, 'bottom': 500.40101408958435, 'confidence': 0.9576567}, 'id': 1}
如何使用PaddleHub进行图像分类:
py中安装paddle hub
step1 加载预训练模型(导包):
import paddlehub as hub
module = hub.Module(name="resnet_v2_50_imagenet")
input_dict, output_dict, program = module.context(
trainable=True)
[2020-04-04 18:01:32,986] [ INFO] - Installing resnet_v2_50_imagenet module
Downloading resnet_v2_50_imagenet
[==================================================] 100.00%
Uncompress /home/aistudio/.paddlehub/tmp/tmppobhgwyu/resnet_v2_50_imagenet
[==================================================] 100.00%
[2020-04-04 18:01:44,252] [ INFO] - Successfully installed resnet_v2_50_imagenet-1.0.1
[2020-04-04 18:01:44,580] [ INFO] - 267 pretrained paramaters loaded by PaddleHub
Step2、数据准备
直接用PaddleHub提供的数据集
dataset = hub.dataset.DogCat()
data_reader = hub.reader.ImageClassificationReader(
image_width=module.get_expected_image_width(),
image_height=module.get_expected_image_height(),
images_mean=module.get_pretrained_images_mean(),
images_std=module.get_pretrained_images_std(),
dataset=dataset)
Downloading dog-cat.tar.gz
[==================================================] 100.00%
Uncompress /home/aistudio/.paddlehub/dataset/dog-cat.tar.gz
[==================================================] 100.00%
[2020-04-04 18:02:34,783] [ INFO] - Dataset label map = {'cat': 0, 'dog': 1}
Step3、配置策略:
use_cuda:使用cpu还是gpu
epoch:要求Finetune的任务只遍历1次训练集;
batch_size:每次训练的时候,给模型输入的每批数据大小为32
log_interval:每隔10 step打印一次训练日志;
eval_interval:每隔50 step在验证集上进行一次性能评估;
checkpoint_dir:将训练的参数和数据保存到cv_finetune_turtorial_demo目录中;
strategy:使用DefaultFinetuneStrategy策略进行finetune;
config = hub.RunConfig(
use_cuda=False, #是否使用GPU训练,默认为False;
num_epoch=1, #Fine-tune的轮数;
checkpoint_dir="cv_finetune_turtorial_demo",#模型checkpoint保存路径, 若用户没有指定,程序会自动生成;
batch_size=32, #训练的批大小,如果使用GPU,请根据实际情况调整batch_size;
eval_interval=50, #模型评估的间隔,默认每100个step评估一次验证集;
strategy=hub.finetune.strategy.DefaultFinetuneStrategy()) #Fine-tune优化策略;
Step4、迁移组网:
feature_map = output_dict["feature_map"]
feed_list = [input_dict["image"].name]
#ImageClassifierTask:通用的分类任务Task,该Task基于输入的特征,添加一个或多个全连接层来创建一个分类任务用于Fine-tune
task = hub.ImageClassifierTask(
data_reader=data_reader, #提供数据的reader
feed_list=feed_list, #待feed变量的名字列表
feature=feature_map, #输入的特征矩阵
num_classes=dataset.num_labels, #分类任务的类别数量
config=config) #运行配置
2020-04-04 18:02:35,138-WARNING: paddle.fluid.layers.py_reader() may be deprecated in the near future. Please use paddle.fluid.io.DataLoader.from_generator() instead.
[2020-04-04 18:02:35,290] [ INFO] - Strategy with scheduler: {'warmup': 0.0, 'linear_decay': {'start_point': 1.0, 'end_learning_rate': 0.0}, 'noam_decay': False, 'discriminative': {'blocks': 0, 'factor': 2.6}, 'gradual_unfreeze': 0, 'slanted_triangle': {'cut_fraction': 0.0, 'ratio': 32}}, regularization: {'L2': 0.001, 'L2SP': 0.0, 'weight_decay': 0.0} and clip: {'GlobalNorm': 0.0, 'Norm': 0.0}
[2020-04-04 18:02:39,780] [ INFO] - Try loading checkpoint from cv_finetune_turtorial_demo/ckpt.meta
[2020-04-04 18:02:40,889] [ INFO] - PaddleHub model checkpoint loaded. current_epoch=2, global_step=564, best_score=0.99105
[2020-04-04 18:02:40,890] [ INFO] - PaddleHub finetune start
[2020-04-04 18:02:40,891] [ INFO] - PaddleHub finetune finished.
Step5、预测
!wget --no-check-certificate https://paddlehub.bj.bcebos.com/resources/test_img_cat.jpg
!wget --no-check-certificate https://paddlehub.bj.bcebos.com/resources/test_img_dog.jpg
--2020-04-04 18:02:41-- https://paddlehub.bj.bcebos.com/resources/test_img_cat.jpg
Resolving paddlehub.bj.bcebos.com (paddlehub.bj.bcebos.com)... 182.61.200.195, 182.61.200.229
Connecting to paddlehub.bj.bcebos.com (paddlehub.bj.bcebos.com)|182.61.200.195|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 36914 (36K) [image/jpeg]
Saving to: ‘test_img_cat.jpg.1’
test_img_cat.jpg.1 100%[===================>] 36.05K --.-KB/s in 0.01s
2020-04-04 18:02:41 (3.56 MB/s) - ‘test_img_cat.jpg.1’ saved [36914/36914]
--2020-04-04 18:02:41-- https://paddlehub.bj.bcebos.com/resources/test_img_dog.jpg
Resolving paddlehub.bj.bcebos.com (paddlehub.bj.bcebos.com)... 182.61.200.195, 182.61.200.229
Connecting to paddlehub.bj.bcebos.com (paddlehub.bj.bcebos.com)|182.61.200.195|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 113952 (111K) [image/jpeg]
Saving to: ‘test_img_dog.jpg.1’
test_img_dog.jpg.1 100%[===================>] 111.28K --.-KB/s in 0.04s
2020-04-04 18:02:41 (2.87 MB/s) - ‘test_img_dog.jpg.1’ saved [113952/113952]
import numpy as np
data = ["test_img_dog.jpg"]
label_map = dataset.label_dict()
index = 0
run_states = task.predict(data=data)
results = [run_state.run_results for run_state in run_states]
for batch_result in results:
print(batch_result)
batch_result = np.argmax(batch_result, axis=2)[0]
print(batch_result)
for result in batch_result:
index += 1
result = label_map[result]
print("input %i is %s, and the predict result is %s" %
(index, data[index - 1], result))
[2020-04-04 18:02:41,713] [ INFO] - Load the best model from cv_finetune_turtorial_demo/best_model
2020-04-04 18:02:42,041-WARNING: paddle.fluid.layers.py_reader() may be deprecated in the near future. Please use paddle.fluid.io.DataLoader.from_generator() instead.
[2020-04-04 18:02:43,097] [ INFO] - PaddleHub predict start
[2020-04-04 18:02:43,528] [ INFO] - PaddleHub predict finished.
更多推荐
第十天:paddlehub体验
发布评论