本项目针对30种常见鱼类,通过爬取多平台图片形成含1917张图片的数据集,用PaddleHub实现分类并部署到微信小程序。先预处理数据,选ResNet50模型训练,经调参优化,用Momentum优化器、batch_size=8时效果佳。再封装模型为PaddleHub Module,借PaddleHub Serving部署,实现小程序端鱼类识别,后续计划扩充数据集与功能。
☞☞☞AI 智能聊天, 问答助手, AI 智能搜索, 免费无限量使用 DeepSeek R1 模型☜☜☜

针对以上30种常见鱼类进行分类,并部署到微信小程序
包含30类常见鱼类,合计1917张图片,由于有部分鱼类爬取到的图片数量较少,因此每种鱼类的数据量并不是一致的。
友情提示:数据集仅供学习和个人使用
30种鱼类中英文对照字典:
{'Cuttlefish': '墨鱼', 'Turbot': '多宝鱼', 'Hairtail': '带鱼', 'Grouper': '石斑鱼', 'Saury': '秋刀鱼', 'Octopus': '章鱼', 'Red_fish': '红鱼', 'Tilapia_mossambica': '罗非鱼', 'Variegated_carp': '胖头鱼', 'Grass_Carp': '草鱼', 'Silverfish': '银鱼', 'Herring': '青鱼', 'Horsehead_fish': '马头鱼', 'Squid': '鱿鱼', 'Catfish': '鲇鱼', 'Perch': '鲈鱼', 'Abalone': '鲍鱼', 'Salmon': '鲑鱼', 'Silver_carp': '鲢鱼', 'Carp': '鲤鱼', 'Crucian_carp': '鲫鱼', 'Silvery_pomfret': '鲳鱼', 'Bream': '鲷鱼', 'Plaice': '鲽鱼', 'Parabramis_pekinensis': '鳊鱼', 'Eel': '鳗鱼', 'Yellow_croaker': '黄鱼', 'Ricefield_eel': '黄鳝', 'Snakehead': '黑鱼', 'Bibcock_fish': '龙头鱼'}
# 解压缩数据!unzip -oq -d images data/data103322/images.zip
# 数据加载和预处理import osimport paddleimport numpy as npimport paddlehub.vision.transforms as T# 定义数据集class FishDataset(paddle.io.Dataset):
def __init__(self, dataset_dir, transforms, mode='train'):
# 数据集存放路径
self.dataset_dir = dataset_dir # 数据增强
self.transforms = transforms # 分类数
self.label_lst = []
self.num_classes= self.get_label()
self.mode = mode # 根据mode读取对应的数据集
if self.mode == 'train':
self.file = 'train_list.txt'
elif self.mode == 'test':
self.file = 'test_list.txt'
else:
self.file = 'validate_list.txt'
self.file = os.path.join(self.dataset_dir, self.file) with open(self.file, 'r') as f:
self.data = f.read().split('\n')[:-1]
def get_label(self):
# 获取分类数
with open(os.path.join(dataset_dir, 'label_list.txt'), 'r') as f:
labels = f.readlines() for idx, label in enumerate(labels):
dic = {}
dic['label_name'] = label.split('\n')[0]
dic['label_id'] = idx
self.label_lst.append(dic) return len(self.label_lst) def __getitem__(self, idx):
img_path, label = self.data[idx].split(' ')
img_path = os.path.join(self.dataset_dir, img_path)
im = self.transforms(img_path) return im, int(label)
def __len__(self):
return len(self.data)# 定义数据增强train_Transforms = T.Compose([
T.Resize((256, 256)),
T.CenterCrop(224),
T.RandomHorizontalFlip(),
T.Normalize()
], to_rgb=True)
eval_Transforms = T.Compose([
T.Resize((256, 256)),
T.CenterCrop(224),
T.Normalize()
], to_rgb=True)# 读取数据集dataset_dir = 'images/images'fish_train = FishDataset(dataset_dir, train_Transforms)
fish_validate = FishDataset(dataset_dir, eval_Transforms, mode='validate')print('训练集的图片数量: {}'.format(len(fish_train)))print('验证集的图片数量: {}'.format(len(fish_validate)))print('分类数: {}'.format(len(fish_train.label_lst)))# label_id转labelid2label = {}for i in fish_train.label_lst:
id2label[i['label_id']] = i['label_name']print(id2label)# 鱼类英文名转中文en2zh = {'Cuttlefish': '墨鱼', 'Turbot': '多宝鱼', 'Hairtail': '带鱼', 'Grouper': '石斑鱼', 'Saury': '秋刀鱼', 'Octopus': '章鱼', 'Red_fish': '红鱼', 'Tilapia_mossambica': '罗非鱼', 'Variegated_carp': '胖头鱼', 'Grass_Carp': '草鱼', 'Silverfish': '银鱼', 'Herring': '青鱼', 'Horsehead_fish': '马头鱼', 'Squid': '鱿鱼', 'Catfish': '鲇鱼', 'Perch': '鲈鱼', 'Abalone': '鲍鱼', 'Salmon': '鲑鱼', 'Silver_carp': '鲢鱼', 'Carp': '鲤鱼', 'Crucian_carp': '鲫鱼', 'Silvery_pomfret': '鲳鱼', 'Bream': '鲷鱼', 'Plaice': '鲽鱼', 'Parabramis_pekinensis': '鳊鱼', 'Eel': '鳗鱼', 'Yellow_croaker': '黄鱼', 'Ricefield_eel': '黄鳝', 'Snakehead': '黑鱼', 'Bibcock_fish': '龙头鱼'}print(en2zh)from PIL import Imageimport matplotlib.pyplot as plt
path = 'images/images/train'plt.figure(figsize=(30, 8))for idx, name in enumerate(en2zh.keys()): for fpath, dirname, fname in os.walk(os.path.join(path, name)):
plt.subplot(3, 10, idx+1)
img = Image.open(os.path.join(fpath, fname[0]))
plt.title(name)
plt.imshow(img)import paddlefrom paddle.vision.models import resnet50# 设置pretrained参数为True,可以加载resnet50在imagenet数据集上的预训练模型model = paddle.Model(resnet50(pretrained=True, num_classes=len(fish_train.label_lst)))
from paddle.optimizer import Momentumfrom paddle.regularizer import L2Decayfrom paddle.nn import CrossEntropyLossfrom paddle.metric import Accuracy# 配置优化器optimizer = Momentum(learning_rate=0.001,
momentum=0.9,
weight_decay=L2Decay(1e-4),
parameters=model.parameters())# 进行训练前准备model.prepare(optimizer, CrossEntropyLoss(), Accuracy(topk=(1, 5)))# 启动训练model.fit(fish_train,
fish_validate,
epochs=50,
batch_size=8,
save_dir="./output")if not os.path.exists('final'):
os.mkdir('final')# 将final.pdparams复制到final文件夹!cp output/final.pdparams final# 模型评估,根据prepare接口配置的loss和metric进行返回result = model.evaluate(fish_validate)print(result)
# 批量预测from paddle.static import InputSpec# 加载final模型参数inputs = InputSpec([None, 1*1*3*224*224], 'float32', 'x')
labels = InputSpec([None, 30], 'int32', 'x')
model = paddle.Model(resnet50(num_classes=len(fish_train.label_lst)), inputs, labels)# 加载模型参数model.load('final/final.pdparams')# 定义优化器optimizer = Momentum(learning_rate=0.001,
momentum=0.9,
weight_decay=L2Decay(1e-4),
parameters=model.parameters())# 进行预测前准备model.prepare(optimizer, CrossEntropyLoss(), Accuracy(topk=(1, 5)))# 加载测试集数据fish_test = FishDataset(dataset_dir, eval_Transforms, mode='test')# 进行预测操作result = model.predict(fish_test)# 定义画图方法def show_img(idx, predict):
with open(os.path.join(dataset_dir, 'test_list.txt')) as f:
data = f.readlines()
plt.figure() print('predict: {}'.format(predict))
img = Image.open(os.path.join(dataset_dir, data[idx].split()[0]))
plt.imshow(img)
plt.show()# 抽样展示indexs = [2, 15, 38, 100]for idx in indexs:
show_img(idx, en2zh[id2label[np.argmax(result[0][idx])]])# 单张图片预测# 读取单张图片image = paddle.to_tensor(fish_test[80][0]).reshape([1, 1, 3, 224, 224]) image_id = 80# 单张图片预测result = model.predict(image)# 可视化结果show_img(image_id, en2zh[id2label[np.argmax(result)]])
内容根据PaddleHub文档教程>如何创建自己的Module改编。文档基于情感分类(NLP)模型,本文基于(CV),开发者可根据需要相互参考。
在 /home/aistudio/work 目录下创建 fish_predict 文件夹,并在该目录下分别创建 module.py __init__.py ,其中 module.py 作为 Module 的入口,用来实现逻辑预测功能。
! tree work/
在 module.py 中编写好代码后,就可以通过 hub install xxx 的方式来安装模型了!
!pip install --upgrade paddlehub
# 安装模型!hub install work/fish_predict/
# 预测import paddlehub as hub
my_fish_predict = hub.Module(name="fish_predict")
my_fish_predict.fish_predict('images/images/test/Hairtail/137.jpg')部署方法:
在终端运行命令 hub serving start -m fish_predict 。如果它出现下面的提示说明部署成功
通过POST请求实现预测
# 通过POST请求实现预测import requestsimport jsonimport cv2import base64def cv2_to_base64(image):
data = cv2.imencode('.png', image)[1] return base64.b64encode(data.tobytes()).decode('utf-8')# 发送HTTP请求data = {'img_b64': cv2_to_base64(cv2.imread("images/images/test/Hairtail/137.jpg"))}
headers = {"Content-type": "application/json", "Connection": "close"}
url = "http://0.0.0.0:8866/predict/fish_predict"r = requests.post(url=url, headers=headers, data=json.dumps(data))# 打印预测结果print(r)print(r.json()['results'].encode('utf-8').decode('unicode_escape'))
我在AI Studio上获得白银等级,点亮3个徽章,来互关呀~ https://aistudio.baidu.com/aistudio/personalcenter/thirdview/158581
<br/>
以上就是『AI达人创造营』基于PaddleHub实现常见鱼类分类及微信小程序部署的详细内容,更多请关注php中文网其它相关文章!
微信是一款手机通信软件,支持通过手机网络发送语音短信、视频、图片和文字。微信可以单聊及群聊,还能根据地理位置找到附近的人,带给大家全新的移动沟通体验,有需要的小伙伴快来保存下载体验吧!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号