```python
# !cd data/data73997 &&\
# !unzip -qo data/Butterfly20.zip
# !unzip -qo data/Butterfly20_test.zip
```
/bin/sh: 1: !unzip: not found
unzip: cannot find or open data/Butterfly20_test.zip, data/Butterfly20_test.zip.zip or data/Butterfly20_test.zip.ZIP.
# 基于PaddlePaddle2.0的蝴蝶图像识别分类——利用预训练残差网络ResNet50模型
## 1. 蝴蝶识别分类任务概述
人工智能技术的应用领域日趋广泛,新的智能应用层出不穷。本项目将利用人工智能技术来对蝴蝶图像进行分类,需要能对蝴蝶的类别、属性进行细粒度的识别分类。相关研究工作者能够根据采集到的蝴蝶图片,快速识别图中蝴蝶的种类。期望能够有助于提升蝴蝶识别工作的效率和精度。
## 2. 创建项目和挂载数据
数据集都来源于网络公开数据(和鲸社区)。图片中所涉及的蝴蝶总共有9个属,20个物种,文件genus.txt中描述了9个属名,species.txt描述了20个物种名。
在创建项目时,可以为该项目挂载Butterfly20蝴蝶数据集,即便项目重启,该挂载的数据集也不会被自动清除。具体方法如下:首先采用notebook方式构建项目,项目创建框中的最下方有个数据集选项,选择“+添加数据集”。然后,弹出搜索框,在关键词栏目输入“bufferfly20”,便能够查询到该数据集。最后,选中该数据集,可以自动在项目中挂载该数据集了。
需要注意的是,每次重新打开该项目,data文件夹下除了挂载的数据集,其他文件都将被删除。
被挂载的数据集会自动出现在data目录之下,通常是压缩包的形式。在data/data63004目录,其中有两个压缩文件,分别是Butterfly20.zip和Butterfly20_test.zip。也可以利用下载功能把数据集下载到本地进行训练。
## 3. 初探蝴蝶数据集
我们看看蝴蝶图像数据长什么样子?
首先,解压缩数据。类以下几个步骤:
第一步,把当前路径转换到data目录,可以使用命令!cd data。在AI studio nootbook中可以使用Linux命令,需要在命令的最前面加上英文的感叹号(!)。用&&可以连接两个命令。用\号可以换行写代码。需要注意的是,每次重新打开该项目,data文件夹下除了挂载的数据集,其他文件都会被清空。因此,如果把数据保存在data目录中,每次重新启动项目时,都需要解压缩一下。如果想省事持久化保存,可以把数据保存在work目录下。
实际上,!加某命令的模式,等价于python中的get_ipython().system('某命令')模式。
第二步,利用unzip命令,把压缩包解压到当前路径。unzip的-q参数代表执行时不显示任何信息。unzip的-o参数代表不必先询问用户,unzip执行后覆盖原有的文件。两个参数合起来,可以写为-qo。
```python
!cd data/data73997 &&\
unzip -qo Butterfly20_test.zip &&\
unzip -qo Butterfly20.zip &&\
rm -r __MACOSX
```
### 3.1 导入相关库文件
```python
import paddle
import matplotlib.pyplot as plt
import PIL.Image as Image
import numpy as np
import matplotlib.pyplot as plt
import cv2
import os
import glob
import random
import time
import pandas as pd
print(f'Welcome to paddle {paddle.__version__} zoo,\n there are many butterflies here today,\n please enjoy the good time with us!' )
```
Welcome to paddle 2.0.2 zoo,
there are many butterflies here today,
please enjoy the good time with us!
```python
# but_files = np.array(glob("/data/images/*/*/*"))
# print number of images in each dataset
# print('There are %d total dog images.' % len(but_files))
data_path='/home/aistudio/data/data73997/Butterfly20/*/*.jpg'
test_path='/home/aistudio/data/data73997/Butterfly20_test/*.jpg'
but_files =glob.glob(data_path)
test_files =glob.glob(test_path)
print(f'训练集样品数量为:{len(but_files)}个\n 测试集样品数量为:{len(test_files)}个')
```
训练集样品数量为:1866个
测试集样品数量为:200个
### 欣赏一下各类的蝴蝶
每次运行均会随机产生不同的蝴蝶图片
```python
index=random.choice(but_files)
index20 =random.sample(but_files,20)
plt.figure(figsize=(12,12),dpi=100)
for i in range(20):
img = Image.open(index20[i])
name=index20[i].split('/')[-2]
plt.subplot(4, 5, i + 1)
plt.imshow(img, 'gray')
plt.title(name, fontsize=8)
plt.xticks([]), plt.yticks([])
plt.tight_layout()
```
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/matplotlib/cbook/__init__.py:2349: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated, and in 3.8 it will stop working
if isinstance(obj, collections.Iterator):
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/matplotlib/cbook/__init__.py:2366: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated, and in 3.8 it will stop working
return list(data) if isinstance(data, collections.MappingView) else data
![png](output_10_1.png)
#### 随机查看一个类别
Butterfly20文件夹中有很多子文件夹,每个子文件夹下又有很多图片,每个子文件夹的名字都是蝴蝶属种的名字。由此,可以推测每个文件夹下是样本,而样本的标签就是子文件夹的名字。
我们绘制data/Butterfly20/001.Atrophaneura_horishanus文件夹下的图片006.jpg。根据百度百科,Atrophaneura horishanus是凤蝶科、曙凤蝶属的一个物种。
我们再绘制data/Butterfly20/002.Atrophaneura_varuna文件夹下的图片006.jpg。根据百度百科,Atrophaneura varuna对应的中文名称是“瓦曙凤蝶”,它是凤蝶科、曙凤蝶属的另一个物种。
虽然乍一看蝴蝶都是相似的,但不同属种的蝴蝶在形状、颜色等细节方面还是存在很大的差别。
```python
import matplotlib.pyplot as plt
import PIL.Image as Image
path='/home/aistudio/data/data73997/Butterfly20/001.Atrophaneura_horishanus/006.jpg'
img = Image.open(path)
plt.imshow(img) #根据数组绘制图像
plt.show() #显示图像
```
![png](output_12_0.png)
```python
path='/home/aistudio/data/data73997/Butterfly20/002.Atrophaneura_varuna/006.jpg'
img = Image.open(path)
plt.imshow(img) #根据数组绘制图像
plt.show() #显示图像
```
![png](output_13_0.png)
更具挑战的是,即便属于同一属种,不同的蝴蝶图片在角度、明暗、背景、姿态、颜色等方面均存在不小差别。甚至有的图片里面有多只蝴蝶。以下两张蝴蝶图片均出自同一个属种Atrophaneura horishanus。
```python
path1='/home/aistudio/data/data73997/Butterfly20/001.Atrophaneura_horishanus/006.jpg'
path2='/home/aistudio/data/data73997/Butterfly20/001.Atrophaneura_horishanus/100.jpg'
img1 = Image.open(path1)
plt.imshow(img1) #根据数组绘制图像
plt.show()
img2 = Image.open(path2)
plt.imshow(img2) #根据数组绘制图像
plt.show() #显示图像
```
![png](output_15_0.png)
![png](output_15_1.png)
### 仿射填充+中心裁切增广技术,可以避免resize造成的变形
```python
def enlarge(img):
h,w,_=img.shape
ty=(600-h)//2
tx=(600-w)//2
# 定义平移矩阵,需要是numpy的float32类型
# x轴平移200,y轴平移500
M = np.float32([[1, 0, tx], [0, 1, ty]])
# 用仿射变换实现平移
dst = cv2.warpAffine(img, M, (600, 600))
dst = dst[100:501,100:501,:]
return dst
index=random.choice(but_files)
#index=but_files[1]
print(index)
name=index.split('/')[-2]
img = Ima
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
python基于paddle框架+ResNet残差网络的蝴蝶种类识别和分类项目源码(高分项目).zip已获导师指导并通过的97分的高分大作业设计项目,可作为课程设计和期末大作业,下载即用无需修改,项目完整确保可以运行。 python基于paddle框架+ResNet残差网络的蝴蝶种类识别和分类项目源码(高分项目).zip已获导师指导并通过的97分的高分大作业设计项目,可作为课程设计和期末大作业,下载即用无需修改,项目完整确保可以运行。 python基于paddle框架+ResNet残差网络的蝴蝶种类识别和分类项目源码(高分项目).zip已获导师指导并通过的97分的高分大作业设计项目,可作为课程设计和期末大作业,下载即用无需修改,项目完整确保可以运行。 python基于paddle框架+ResNet残差网络的蝴蝶种类识别和分类项目源码(高分项目).zip已获导师指导并通过的97分的高分大作业设计项目,可作为课程设计和期末大作业,下载即用无需修改,项目完整确保可以运行。 python基于paddle框架+ResNet残差网络的蝴蝶种类识别和分类项目源码(高分项目).zip已获导师指
资源推荐
资源详情
资源评论
收起资源包目录
基于paddlepaddle框架 + ResNet 残差网络的蝴蝶种类识别和分类.zip (14个子文件)
Butterfly-species-identification-and-classification-based-on-RESNET-residu-main
readme.md 58KB
dataset
readme.txt 203B
README.md 287B
code
output_15_0.png 68KB
output_28_0.png 8KB
output_13_0.png 62KB
output_12_0.png 68KB
output_26_1.png 39KB
output_15_1.png 73KB
butterfly_identification.py 28KB
output_17_2.png 205KB
output_10_1.png 1.38MB
output_49_0.png 1.35MB
output_25_1.png 40KB
共 14 条
- 1
资源评论
不安分的小女孩
- 粉丝: 9800
- 资源: 2324
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功