yolov3代码学习(1)小功能实现代码

本文档介绍了在学习YoloV3代码过程中遇到的关键点,包括使用OpenCV读取图片并调整格式,从.cfg文件加载网络配置,文件路径操作,异常处理,map函数的应用,图片批次处理及resize方法。详细阐述了darknet.py和util.py中的实用功能,如将图片按比例resize并填充,以及如何在视频中显示检测结果。
摘要由CSDN通过智能技术生成

从下面的博客开始学习的,总结一些自己学到的东西。
https://blog.csdn.net/qq_34199326/article/details/84206079
看源码一个大的感慨就是维度问题,维度,维度,维度,会经常看到unsqueeze,repeat

1. opencv读取图片–>model能处理的格式,在darknet.py的第1个函数。

 from torch.autograd import Variable
   def get_test_input():
       img = cv2.imread("dog-cycle-car.png")
       img = cv2.resize(img, (416,416))          # Resize to the input dimension, img[416,416,3]
       print(img.shape)
       img_ =  img[:,:,::-1].transpose((2,0,1))  # [:,:,::-1]第三个维度从后向前取所有元素,BGR -> RGB | img[3,416,416]
       img_ = torch.from_numpy(img_).float()     # Convert to float
       img_ = Variable(img_)                     # Convert to Variable
       return img_

2. 从.cfg读取yolov3网络,在darknet.py的第2个函数。

把.cfg文件按行读取,保存在list中,并将注释,空行,左右两边去掉。

file = open(cfgfile, 'r')
lines = file.read().split('\n')  # store the lines in a list
lines = [x for x in lines if len(x) > 0]  # get rid of the empty lines = save noozero lines
lines = [x for x in lines if x[0] != '#']  # get rid of comments
lines = [x.rstrip().lstrip() for x in lines]  # 去掉左右两边的空格

总体代码如下,写的很简短,有效,看了半天才看明白。

def parse_cfg(cfgfile):
    """
    Takes a configuration file

    Returns a list of blocks. Each blocks describes a block in the neural
    network to be built. Block is represented as a dictionary in the list

    """
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值