创意项目_用PaddleHub抠图后再分类
最近参加百度PaddlePaddle(https://paddlepaddle.org.cn/)的线上师资培训,学习好了很多好东西,和我之前所用的pytorch,tensorflow相比,模型框架类似,关键在于PaddlePaddle基本国内的用户出发,提供了非常容易理解的文档和案例模型,对初习者来说,利用PaddlePaddle进行尝试学习是能够坚持和实现自己想法的是好场所。详情请参见 https://paddlepaddle.org.cn/
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import matplotlib.image as image
from matplotlib import animation
import cv2
import paddlehub as hub
from PIL import Image, ImageSequence, ImageDraw,ImageFont
from IPython.display import display, HTML
import numpy as np
import imageio
import os
import zipfile
train_parameters = {
“src_path”:"/home/aistudio/data/data48958/sisters.zip", #原始数据集路径
“target_path”:"/home/aistudio/data/",
}
#Step1、基础工作
def unzip_data(src_path,target_path):
‘’’
解压原始数据集,将src_path路径下的zip包解压至data目录下
‘’’
if(not os.path.isdir(target_path + “sisters”)):
z = zipfile.ZipFile(src_path, ‘r’)
z.extractall(path=target_path)
z.close()
src_path = train_parameters[“src_path”]
target_path = train_parameters[“target_path”]
unzip_data(src_path,target_path)
#step2、抠图
test_path = ‘data/sisters/’
output_path = ‘data/sisters_blend_out/’
class_dirs=[‘wanqian’,‘wanglikun’,‘ningjing’]
img_list_wanqian=[]
img_list_wanglikun=[]
img_list_ningjing=[]
img_class_dirs=[]
for class_dir in class_dirs:
img_path=test_path+class_dir
#print(img_path)
img_class_dirs.append(img_path)
imgs=os.listdir(img_path)
for img in imgs:
#print(img_path+’/’+img)
#img_list.append(img_path+’/’+img)
if img!=".DS_Store":
if class_dir==‘wanqian’:
img_list_wanqian.append(img_path+’/’+img)
if class_dir==‘wanglikun’:
img_list_wanglikun.append(img_path+’/’+img)
if class_dir==‘ningjing’:
img_list_ningjing.append(img_path+’/’+img)
#获取文件件名
print(img_list_ningjing[1])
print(img_list_ningjing[1].split("/")[-1])
img=img_list_wanglikun[0]
#展示图片
img = image.imread(img)
plt.figure(figsize=(10,10))
plt.imshow(img)
plt.axis(‘off’)
#plt.show()
img_class_dirs[0]
module_seg = hub.Module(name=“deeplabv3p_xception65_humanseg”)
#查重数据情况
print(img_class_dirs[0])
print(img_list_wanqian[0])
#分别对三个类别中的图像进行抠图 img_list_wanqian[] 、img_list_wanglikun[]、img_list_ningjing[]
for imgs in img_list_wanqian:
test_img= [cv2.imread(imgs)]
results = module_seg.segmentation(images=test_img,output_dir=‘data/sisters_out/wanqian’,visualization=True)
for imgs in img_list_wanglikun:
test_img= [cv2.imread(imgs)]
results = module_seg.segmentation(images=test_img,output_dir=‘data/sisters_out/wanglikun’,visualization=True)
for imgs in img_list_ningjing:
test_img= [cv2.imread(imgs)]
results = module_seg.segmentation(images=test_img,output_dir=‘data/sisters_out/ningjing’,visualization=True)
#改名
rename_path=‘data/sisters_out/wanqian’#表示需要命名处理的文件夹
items = os.listdir(rename_path)
for name,new_name in zip(items,img_list_wanqian):
#print(name,new_name.split("/")[-1])
src = os.path.join(os.path.abspath(rename_path), name)
dst = os.path.join(os.path.abspath(rename_path),new_name.split("/")[-1])
#print(src,dst)
os.rename(src,dst)
rename_path=‘data/sisters_out/wanglikun’#表示需要命名处理的文件夹
items = os.listdir(rename_path)
for name,new_name in zip(items,img_list_wanglikun):
#print(name,new_name.split("/")[-1])
src = os.path.join(os.path.abspath(rename_path), name)
dst = os.path.join(os.path.abspath(rename_path),new_name.split("/")[-1])
#print(src,dst)
os.rename(src,dst)
rename_path=‘data/sisters_out/ningjing’#表示需要命名处理的文件夹
items = os.listdir(rename_path)
for name,new_name in zip(items,img_list_ningjing):
#print(name,new_name.split("/")[-1])
src = os.path.join(os.path.abspath(rename_path), name)
dst = os.path.join(os.path.abspath(rename_path),new_name.split("/")[-1])
#print(src,dst)
os.rename(src,dst)
#此处sisters和sisters_out用相同的label.txt train.txt eval.txt,所以直接拷贝
get_ipython().system(‘cp data/sisters/*.txt data/sisters_out/’)
test_img_path = “data/sisters_out/ningjing/4.jpg”
img = mpimg.imread(test_img_path)
plt.figure(figsize=(10,10))
plt.imshow(img)
plt.axis(‘off’)
plt.show()
#没有抠图前的finetune情况
train_parameters = {
“src_path”:"/home/aistudio/data/data48958/sisters.zip", #原始数据集路径
“target_path”:"/home/aistudio/data/", #要解压的路径
“train_list_path”: “/home/aistudio/data/sisters/train.txt”, #train.txt路径
“eval_list_path”: “/home/aistudio/data/sisters/eval.txt”, #eval.txt路径
“num_epochs”: 10, #训练轮数
“train_batch_size”: 8, #训练时每个批次的大小
“checkpoint_path”: “cv_finetune_turtorial_demo”,
“eval_interval”: 10
}
#加载预训练模型
module_class = hub.Module(name=“resnet_v2_50_imagenet”)
#没有抠图前的finetune过程:
#1、准备数据
from paddlehub.dataset.base_cv_dataset import BaseCVDataset
class DemoDataset(BaseCVDataset):
def init(self):
# 数据集存放位置
self.dataset_dir = "data/sisters" #抠图前
#self.dataset_dir = "data/sisters_out" #抠图后
super(DemoDataset, self).__init__(
base_path=self.dataset_dir,
train_list_file="train.txt",
validate_list_file="eval.txt",
test_list_file="eval.txt",
label_list_file="label.txt",
)
dataset = DemoDataset()
#2、生成数据读取器
data_reader = hub.reader.ImageClassificationReader(
image_width=module_class.get_expected_image_width(),
image_height=module_class.get_expected_image_height(),
images_mean=module_class.get_pretrained_images_mean(),
images_std=module_class.get_pretrained_images_std(),
dataset=dataset
)
#3、配置策略
config = hub.RunConfig(
use_cuda=True, #是否使用GPU
num_epoch=15, #Fine-tune的轮数
checkpoint_dir=“cv_finetune_turtorial_sisters”,#抠图前,训练的参数和数据保存到cv_finetune_turtorial_demo目录
#checkpoint_dir=“cv_finetune_turtorial_sisters_out”,#抠图后
batch_size=4, #选择一个合适的batch_size是很重要的一步
eval_interval=1,#模型评估的间隔,默认每100个Step评估一次验证集
#strategy=hub.finetune.strategy.DefaultFinetuneStrategy() # Fine_tune优化策略
strategy=hub.finetune.strategy.AdamWeightDecayStrategy()
)
#4、组建Finetune Task
input_dict, output_dict, program = module_class.context(trainable=True)
img = input_dict[“image”]
feature_map = output_dict[“feature_map”]
feed_list = [img.name]
task = hub.ImageClassifierTask(
data_reader=data_reader,
feed_list=feed_list,
feature=feature_map,
num_classes=dataset.num_labels,
#num_classes=5,
config=config)
#5、开始Finetune 我们选择finetune_and_eval接口来进行模型训练,这个接口在finetune的过程中,会周期性的进行模型效果的评估,
#以便我们了解整个训练过程的性能变化。
run_states = task.finetune_and_eval()
#6、预测
get_ipython().run_line_magic(‘matplotlib’, ‘inline’)
def show_image(image_path):
‘’’
展示图片
‘’’
img = image.imread(image_path)
plt.figure(figsize=(10,10))
plt.imshow(img)
plt.axis(‘off’)
plt.show()
index = 0
data = [“data/sisters/infer/infer_ningjing.jpg”]
label_map = dataset.label_dict()
run_states = task.predict(data=data)
results = [run_state.run_results for run_state in run_states]
for batch_result in results:
batch_result = np.argmax(batch_result, axis=2)[0]
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))
show_image(data[0])
#抠图后的分类过程:
#1、准备数据
from paddlehub.dataset.base_cv_dataset import BaseCVDataset
class DemoDataset(BaseCVDataset):
def init(self):
# 数据集存放位置
#self.dataset_dir = "data/sisters" #抠图前
self.dataset_dir = "data/sisters_out" #抠图后
super(DemoDataset, self).__init__(
base_path=self.dataset_dir,
train_list_file="train.txt",
validate_list_file="eval.txt",
test_list_file="eval.txt",
label_list_file="label.txt",
)
dataset = DemoDataset()
#2、生成数据读取器
data_reader = hub.reader.ImageClassificationReader(
image_width=module_class.get_expected_image_width(),
image_height=module_class.get_expected_image_height(),
images_mean=module_class.get_pretrained_images_mean(),
images_std=module_class.get_pretrained_images_std(),
dataset=dataset
)
#3、配置策略
config = hub.RunConfig(
use_cuda=True, #是否使用GPU
num_epoch=15, #Fine-tune的轮数
#checkpoint_dir=“cv_finetune_turtorial_sisters2”,#抠图前,训练的参数和数据保存到cv_finetune_turtorial_demo目录
checkpoint_dir=“cv_finetune_turtorial_sisters_out”,#抠图后,此处注意:修改数据戓其他参数造成不能重样finetune时,可以删除或修改该目录
batch_size=4, #选择一个合适的batch_size是很重要的一步
eval_interval=1,#模型评估的间隔,默认每100个Step评估一次验证集
#strategy=hub.finetune.strategy.DefaultFinetuneStrategy() # Fine_tune优化策略
strategy=hub.finetune.strategy.AdamWeightDecayStrategy()
)
#4、组建Finetune Task
#input_dict, output_dict, program = module_class.context(trainable=True)
input_dict, output_dict, program = module_class.context(trainable=False)
img = input_dict[“image”]
feature_map = output_dict[“feature_map”]
feed_list = [img.name]
task = hub.ImageClassifierTask(
data_reader=data_reader,
feed_list=feed_list,
feature=feature_map,
num_classes=dataset.num_labels,
#num_classes=5,
config=config)
#5、开始Finetune 我们选择finetune_and_eval接口来进行模型训练,这个接口在finetune的过程中,会周期性的进行模型效果的评估,
#以便我们了解整个训练过程的性能变化。
run_states = task.finetune_and_eval()
#6、预测 此处代码和前面代码一样,为了看结果重新弄一遍,平时可以用一个代码。
get_ipython().run_line_magic(‘matplotlib’, ‘inline’)
def show_image(image_path):
‘’’
展示图片
‘’’
img = image.imread(image_path)
plt.figure(figsize=(10,10))
plt.imshow(img)
plt.axis(‘off’)
plt.show()
index = 0
data = [“data/sisters/infer/infer_ningjing.jpg”]
label_map = dataset.label_dict()
run_states = task.predict(data=data)
results = [run_state.run_results for run_state in run_states]
for batch_result in results:
batch_result = np.argmax(batch_result, axis=2)[0]
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))
show_image(data[0])