一、序言
使用百度飞浆提供的paddle框架实现蝴蝶分类,环境:paddle 2.0.2,opencv 4.5.4.58,pycharm编译器。
目录结构:
- Butterfly20里有20个文件夹,分别代表20种蝴蝶种类,每个文件夹内有多个同种类的蝴蝶照片
- Butterfly20_test里有200张蝴蝶照片用于测试训练好的网络
- visualdl_log里存放训练好的网络,使用log文件格式
- species.txt里存放20种类别的名称和序号
- train_set和validation_set在运行时随机分配
二、准备数据
随机查看一个蝴蝶图片及其类别
data_path= '.\Butterfly20\*\*.jpg'
but_files =glob.glob(data_path) #获取Butterfly20中的所有图片地址
print('图片数据为',len(but_files))
#随机显示一个样品的图片
index=random.choice(but_files) # 随机获取一个图片
print(index) # 查看地址
name=index.split('\\')[-2] # 获取标签,得到的是训练集中随机蝴蝶的类别
img = Image.open(index) # 打开图片
img = cv2.imread(index) # 图片处理
print(img.shape) # 输出图片形状(441,600,3)
img = img[:,:,::-1] # 三通道,-1表示从右往左切片,opencv输入为BGR,故从右往左切片为RGB三通道
print(f'该样本标签为:{
name}')
cv2.imshow("ran_img",img)
cv2.waitKey(0)
测试输出为:
写一个Reader类,其中定义三个函数,分别为初始化、处理图像、计算长度,使用Reader类加载训练集与数据集
### 查看数据类型
data_list = [] #用个列表保存每个样本的读取路径、标签
# 由于属种名称本身是字符串,而输入模型的是数字。需要构造一个字典,把某个数字代表该属种名称。键是属种名称,值是整数。
label_list=[]
with open("E:/Pycharm/workspace/OpenCV/butterfly/species.txt") as f:
for line in f:
a,b = line.strip("\n").split(" ") #a为1-20的序号,b为每个种类的name
label_list.append([b, int(a)-1]) #将20种txt种的类别加入label_list数组种
label_dic = dict(label_list) #dict创建一个字典,字典中有20种蝴蝶类型
butterfly_path = './Butterfly20/'
#若项目目录内已经有train_set与validation_set两个数据集,则删除,之后重新创建这两个数据集
if(os.path.exists('E:/Pycharm/workspace/OpenCV/butterfly/train_set.txt')): # 判断有误文件
os.remove('E:/Pycharm/workspace/OpenCV/butterfly/train_set.txt') # 删除文件
if(os.path.exists('E:/Pycharm/workspace/OpenCV/butterfly/validation_set.txt')):
os.remove('E:/Pycharm/workspace/OpenCV/butterfly/validation_set.txt')
for i in os.listdir(butterfly_path): #得到Butterfly20里的所有文件夹
if i not in '.DS_Store': #DB_Store里是20种蝴蝶类型的名字
for j in os.listdir(os.path.join(butterf