SSD-Pytorch训练自己的VOC数据集&遇到的问题及解决办法_no such file or directory ‘c users admin data(2)

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以点击这里获取!

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!


165行的`images, targets = next(batch_iterator)`改成:



try:
images, targets = next(batch_iterator)
except StopIteration:
batch_iterator = iter(data_loader)
images, targets = next(batch_iterator)


### 预训练文件vgg16\_reducedfc.pth


开始训练时需要一个预训练文件 `vgg16_reducedfc.pth`



> 
> [链接]( )
> 
> 
> 


下载之后放在SSD项目下新建的weights文件夹下,然后就可以进行训练了。  
 注:训练中途遇到 loss=nan 的现象,将`train.py`中,`parser.add_argument('--lr', '--learning-rate', default=1e-3, type=float,`中的 `default=1e-3`改为`default=1e-4`。\*~~直到loss降低到1左右时即可~~ \*


### eval.py


`trained_model`评估的模型路径,`save_folder` 评估保存路径


### demo.py


新建`test_image`,在文件夹中放置几张待测图片(四处修改 20220106更新)



import os
import sys
import torch
from torch.autograd import Variable
import numpy as np
import cv2
from ssd import build_ssd
from data import VOC_CLASSES as labels
from matplotlib import pyplot as plt

------ 初始化 libiomp5md.dll 报错修改 ------

os.environ[“KMP_DUPLICATE_LIB_OK”] = “TRUE”

-----------------------------------------

module_path = os.path.abspath(os.path.join(‘…’))
if module_path not in sys.path:
sys.path.append(module_path)

if torch.cuda.is_available():
torch.set_default_tensor_type(‘torch.cuda.FloatTensor’)

net = build_ssd(‘test’, 300, 5) # 第一处修改:类别+1

将预训练的权重加载到数据集上

net.load_weights(‘weights/ssd300_VOC_1995.pth’) # 第二处修改:使用自己训练好的文件

加载多张图像

imgs = ‘test_image/’# 第三处修改:改成你自己的文件夹
img_list = os.listdir(imgs)
for img in img_list:
# 对输入图像进行预处理
current_img = imgs + img
image = cv2.imread(current_img)
rgb_image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

x = cv2.resize(image, (300, 300)).astype(np.float32)
x -= (104.0, 117.0, 123.0)
x = x.astype(np.float32)
x = x[:, :, ::-1].copy()
x = torch.from_numpy(x).permute(2, 0, 1)

# 把图片设为变量
xx = Variable(x.unsqueeze(0))
if torch.cuda.is_available():
    xx = xx.cuda()
y = net(xx)

# 解析 查看结果

top_k = 10

plt.figure(figsize=(6, 6))
colors = plt.cm.hsv(np.linspace(0, 1, 21)).tolist()
currentAxis = plt.gca()

detections = y.data
scale = torch.Tensor(rgb_image.shape[1::-1]).repeat(2)
for i in range(detections.size(1)):
    j = 0
    while detections[0, i, j, 0] >= 0.6:     # 第四处修改:置信度修改
        score = detections[0, i, j, 0]
        label_name = labels[i-1]
        display_txt = '%s: %.2f'%(label_name, score)
        print(display_txt)
        pt = (detections[0,i,j,1:]\*scale).cpu().numpy()
        coords = (pt[0], pt[1]), pt[2]-pt[0]+1, pt[3]-pt[1]+1
        color = colors[i]
        currentAxis.add_patch(plt.Rectangle(\*coords, fill=False, edgecolor=color, linewidth=2))
        currentAxis.text(pt[0], pt[1], display_txt, bbox={'facecolor':color, 'alpha':0.5})
        j += 1
plt.imshow(rgb_image)
plt.show()

### demo/live.py


摄像头识别 (没试)  
 第10行用…/找到上一级目录



parser.add_argument(‘–weights’, default=‘…/weights/xxxxxx.pth’,


第78行 类别+1


## 遇到的问题


报错顺序不记得了,下面是遇到的大部分错误


### train.py


#### TypeError: unsupported operand type(s) for /=: ‘Tensor’ and ‘builtin\_function\_or\_method’…


*loss\_l /= N这句错误*


因为一些教程里还改了`layers/modules/multibox_loss.py`程序:  
 第115行`N = num_pos.data.sum()`改为



N = num_pos.data.sum().double
loss_l = loss_l.double()
loss_c = loss_c.double()


会出现这个问题.


#### 找不到数据集里的文件夹/文件


VOC数据集名字错了 注意名称 和 大小写


#### FileNotFoundError: [Errno 2] No such file or directory: ‘C:\Users\Administrator\data/coco/coco\_labels.txt’


`train.py` 第二行如果有 `from data.coco import COCO_ROOT, COCODetection`注释掉


#### RuntimeError: Legacy autograd function with non-static forward method is deprecated. Please use new-style autograd function with static forward method.


版本问题。 [参考]( )  
 改 `detection.py`为(更新 注释部分已翻译)



“”"
Copyright © 2017 Max deGroot, Ellis Brown
Released under the MIT license
https://github.com/amdegroot/ssd.pytorch
Updated by: Takuya Mouri
“”"
import torch
from torch.autograd import Function
from …box_utils import decode, nms
from data import voc as cfg

class Detect(Function):
“”“At test time, Detect is the final layer of SSD. Decode location preds,
apply non-maximum suppression to location predictions based on conf
scores and threshold to a top_k number of output predictions for both
confidence score and locations.
“””
# PyTorch1.5.0 support new-style autograd function
#def __init__(self, num_classes, bkg_label, top_k, conf_thresh, nms_thresh):
# self.num_classes = num_classes
# self.background_label = bkg_label
# self.top_k = top_k
# # Parameters used in nms.
# self.nms_thresh = nms_thresh
# if nms_thresh <= 0:
# raise ValueError(‘nms_threshold must be non negative.’)
# self.conf_thresh = conf_thresh
# self.variance = cfg[‘variance’]

#def forward(self, loc\_data, conf\_data, prior\_data):
@staticmethod
def forward(self, num_classes, bkg_label, top_k, conf_thresh, nms_thresh, loc_data, conf_data, prior_data):
    self.num_classes = num_classes
    self.background_label = bkg_label
    self.top_k = top_k
    # Parameters used in nms.
    self.nms_thresh = nms_thresh
    if nms_thresh <= 0:
        raise ValueError('nms\_threshold must be non negative.')
    self.conf_thresh = conf_thresh
    self.variance = cfg['variance']
# PyTorch1.5.0 support new-style autograd function
    """

Args:
loc_data: (tensor) Loc preds from loc layers
Shape: [batch,num_priors*4]
conf_data: (tensor) Shape: Conf preds from conf layers
Shape: [batch*num_priors,num_classes]
prior_data: (tensor) Prior boxes and variances from priorbox layers
Shape: [1,num_priors,4]
“”"
num = loc_data.size(0) # batch size
num_priors = prior_data.size(0)
# [バッチサイズN,クラス数5,トップ200件,確信度+位置]のゼロリストを作成
# 创建一个 [batch size = N,classes = 5,预测框最大数量 top_k = 200,置信度 + 位置] 的零列表
output = torch.zeros(num, self.num_classes, self.top_k, 5)
# 確信度を[バッチサイズN,クラス数,ボックス数]の順番に変更
# 按照 [batch size N, number of classes, number of box] 的顺序改变置信度
conf_preds = conf_data.view(num, num_priors,
self.num_classes).transpose(2, 1)

    # Decode predictions into bboxes.
    for i in range(num):
        decoded_boxes = decode(loc_data[i], prior_data, self.variance)
        # For each class, perform nms
        conf_scores = conf_preds[i].clone()

        for cl in range(1, self.num_classes):
            # 確信度の閾値を使ってボックスを削除
            # 使用置信阈值删除框
            c_mask = conf_scores[cl].gt(self.conf_thresh)
            scores = conf_scores[cl][c_mask]
            # handbook
            #if scores.dim() == 0:
            if scores.size(0) == 0:
            # handbook
                continue
            l_mask = c_mask.unsqueeze(1).expand_as(decoded_boxes)
            # ボックスのデコード処理
            # box 解码过程
            boxes = decoded_boxes[l_mask].view(-1, 4)
            # idx of highest scoring and non-overlapping boxes per class
            # boxesからNMSで重複するボックスを削除
            # 使用 NMS 从 boxes 中删除重复的 box
            ids, count = nms(boxes, scores, self.nms_thresh, self.top_k)
            output[i, cl, :count] = \
                torch.cat((scores[ids[:count]].unsqueeze(1),
                           boxes[ids[:count]]), 1)
    flt = output.contiguous().view(num, -1, 5)
    _, idx = flt[:, :, 0].sort(1, descending=True)
    _, rank = idx.sort(1)
    flt[(rank < self.top_k).unsqueeze(-1).expand_as(flt)].fill_(0)
    return output

`ssd.py`中99行左右



output = self.detect(


改为



output = self.detect.apply(self.num_classes, 0, 200, 0.01, 0.45,


#### AttributeError: ‘NoneType’ object has no attribute ‘shape’


change `coco.py`:  
 from: `img=cv2.imread(osp.join(self.root,path))`  
 to:`img=cv2.imread(path)`


#### IndexError: Too many indices for array:Array is 1-dimensional,but 2 were indexed (20220105更新)


annotation也就是xml文件里面有些包含空目标(我的没有也报错了)  
 参考 [网址]( )  
 出错的xml和jpg修改 或 删掉 (流程结束后需要重新生成VOC的四个txt文件)  
 新建 `check.py`  
 修改`root` 、 `classes`



import argparse
import sys
import cv2
import os

import os.path as osp
import numpy as np

if sys.version_info[0] == 2:
import xml.etree.cElementTree as ET
else:
import xml.etree.ElementTree as ET

parser = argparse.ArgumentParser(
description=‘Single Shot MultiBox Detector Training With Pytorch’)
train_set = parser.add_mutually_exclusive_group()

parser.add_argument(‘–root’, default=“xxxxxxxxxxxxxxxxxxxxxxxxxxx”, help=‘Dataset root directory path’)

args = parser.parse_args()

CLASSES = ( # always index 0
‘fire’, ‘xxxxxxxxxxxxxxxxxxxxx’)

annopath = osp.join(‘%s’, ‘Annotations’, ‘%s.{}’.format(“xml”))
imgpath = osp.join(‘%s’, ‘JPEGImages’, ‘%s.{}’.format(“jpg”))

def vocChecker(image_id, width, height, keep_difficult = False):
target = ET.parse(annopath % image_id).getroot()
res = []

for obj in target.iter('object'):

    difficult = int(obj.find('difficult').text) == 1

    if not keep_difficult and difficult:
        continue

    name = obj.find('name').text.lower().strip()
    bbox = obj.find('bndbox')

    pts    = ['xmin', 'ymin', 'xmax', 'ymax']
    bndbox = []

    for i, pt in enumerate(pts):

        cur_pt = int(bbox.find(pt).text) - 1
        # scale height or width
        cur_pt = float(cur_pt) / width if i % 2 == 0 else float(cur_pt) / height

        bndbox.append(cur_pt)

    print(name)
    label_idx =  dict(zip(CLASSES, range(len(CLASSES))))[name]
    bndbox.append(label_idx)
    res += [bndbox]  # [xmin, ymin, xmax, ymax, label\_ind]
    # img\_id = target.find('filename').text[:-4]
print(res)
try :
    print(np.array(res)[:,4])
    print(np.array(res)[:,:4])
except IndexError:
    print("\nINDEX ERROR HERE !\n")
    exit(0)
return res  # [[xmin, ymin, xmax, ymax, label\_ind], ... ]

if name == ‘__main__’ :

i = 0

for name in sorted(os.listdir(osp.join(args.root,'Annotations'))):
# as we have only one annotations file per image
    i += 1

    img    = cv2.imread(imgpath  % (args.root,name.split('.')[0]))
    height, width, channels = img.shape
    print("path : {}".format(annopath % (args.root,name.split('.')[0])))
    res = vocChecker((args.root, name.split('.')[0]), height, width)
print("Total of annotations : {}".format(i))

### eval.py


#### 右键运行变成test模式


打开pycharm进入了test模式,具体表现为用“Run ‘py.test xxx.py’”  
 左上角File-settings-python integrated tools里面修改,选择unittest修改后记得apply


#### 开始运行后到某一个图片突然出错


改VOC2007的main里边的 test.txt 删掉错误的那一行


#### eval运行到最后 FileNotFoundError: [Errno 2] No such file or directory: ‘test.txt’


这只是一个符号问题;os.path.join 不接受在原始实现中加入带有括号“{😒}.txt”的路径。它会忽略所有路径`~/VOC2007/ImageSets/Main/test.txt` 并简单地假设路径是:`currentpath/test.txt`


修复指定 `imgsetpath` 的行,如下所示:



imgsetpath = os.path.join(args.voc_root, ‘VOC2007’, ‘ImageSets’, ‘Main’, ‘%s.txt’)

最后的话

最近很多小伙伴找我要Linux学习资料,于是我翻箱倒柜,整理了一些优质资源,涵盖视频、电子书、PPT等共享给大家!

资料预览

给大家整理的视频资料:

给大家整理的电子书资料:

如果本文对你有帮助,欢迎点赞、收藏、转发给朋友,让我有持续创作的动力!

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以点击这里获取!

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

nux学习资料,于是我翻箱倒柜,整理了一些优质资源,涵盖视频、电子书、PPT等共享给大家!

资料预览

给大家整理的视频资料:

[外链图片转存中…(img-JDwGwxyo-1715881800038)]

给大家整理的电子书资料:

[外链图片转存中…(img-Bf8slFgS-1715881800039)]

如果本文对你有帮助,欢迎点赞、收藏、转发给朋友,让我有持续创作的动力!

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以点击这里获取!

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值