python猫狗识别

该文描述了一个流程,首先通过Python脚本将YOLOv5的数据集转换为COCO格式,然后利用PaddlePaddle的PaddleYOLO进行训练,生成模型。接着,对Paddle模型进行剪裁,最后使用OpenVINO工具进行推理,实现猫狗识别的模型优化与部署。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先需要安装openvino、paddlepaddle、labelimg等模块,python版本需要小于3.8(可以通过虚拟环境创建),
classes.txt:

dog
cat

在这里插入图片描述

在这个位置运行gen.py
在这里插入图片描述
gen.py:

import os
def train_val(labels_path, data_path, ratio=0.3):
    nomask_num = 0
    mask_num = 0
    image_dir = "\\".join(data_path.split("\\")[-3:]) + "\\images"
    txt_files = os.listdir(labels_path)
    f_train = open("train.txt", "w")
    f_val = open("val.txt", "w")
    m = 0
    n = 0
    
    for txt in txt_files:
        f_txt = open(os.path.join(labels_path, txt), 'r')
        if f_txt.read()[0] == "0":
            nomask_num += 1
        else:
            mask_num += 1
        f_txt.close()

    for txt in txt_files:
        f_txt = open(os.path.join(labels_path, txt), 'r')
        if f_txt.read()[0] == "0":
            n += 1
            if n >= int(nomask_num * ratio):
                f_train.writelines(image_dir+"\\" + txt.split(".")[0] + ".jpg" + "\n")
            else:
                f_val.writelines(image_dir+"\\" + txt.split(".")[0] + ".jpg" + "\n")
        else:
            m += 1
            if m >= int(mask_num * ratio):
                f_train.writelines(image_dir+"\\" + txt.split(".")[0] + ".jpg" + "\n")
            else:
                f_val.writelines(image_dir+"\\" + txt.split(".")[0] + ".jpg" + "\n")
        f_txt.close()
    f_train.close()
    f_val.close()
if __name__ == "__main__":
    data_path = os.path.join(os.getcwd(), "m_animal")  
    labels_path = os.path.join(data_path, "labels")
    train_val(labels_path=labels_path, data_path=data_path, ratio=0.3)
   


运行后产生train.txt、val.txt,在yolo2coco目录用控制台运行

python yolov5_2_coco.py --dir_path dataset/YOLOV5

yolov5_2_coco.py:


import argparse
from pathlib import Path
import json
import shutil
import cv2


def read_txt(txt_path):
    with open(str(txt_path), 'r', encoding='utf-8') as f:
        data = f.readlines()
    data = list(map(lambda x: x.rstrip('\n'), data))
    return data


def mkdir(dir_path):
    Path(dir_path).mkdir(parents=True, exist_ok=True)


class YOLOV5ToCOCO(object):
    def __init__(self, dir_path):
        self.src_data = Path(dir_path)
        self.src = self.src_data.parent
        self.train_txt_path = self.src_data / 'train.txt'
        self.val_txt_path = self.src_data / 'val.txt'

        # 构建COCO格式目录
        self.dst = Path(self.src) / f"{
     
     Path(self.src_data).name}_COCO_format"
        self.coco_train = "train2017"
        self.coco_val = "val2017"
        self.coco_annotation = "annotations"
        self.coco_train_json = self.dst / self.coco_annotation \
                                   / f'instances_{
     
     self.coco_train}.json'
        self.coco_val_json = self.dst / self.coco_annotation \
                                   / f'instances_{
     
     self.coco_val}.json'

        mkdir(self.dst)
        mkdir(self.dst / self.coco_train)
        mkdir(self.dst / self.coco_val)
        mkdir(self.dst / self.coco_annotation)

        # 构建json内容结构
        self.type = 'instances'
        self.categories = []
        self.annotation_id = 1

        # 读取类别数
        self._get_category()

        self
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值