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

最全的Linux教程,Linux从入门到精通

======================

  1. linux从入门到精通(第2版)

  2. Linux系统移植

  3. Linux驱动开发入门与实战

  4. LINUX 系统移植 第2版

  5. Linux开源网络全栈详解 从DPDK到OpenFlow

华为18级工程师呕心沥血撰写3000页Linux学习笔记教程

第一份《Linux从入门到精通》466页

====================

内容简介

====

本书是获得了很多读者好评的Linux经典畅销书**《Linux从入门到精通》的第2版**。本书第1版出版后曾经多次印刷,并被51CTO读书频道评为“最受读者喜爱的原创IT技术图书奖”。本书第﹖版以最新的Ubuntu 12.04为版本,循序渐进地向读者介绍了Linux 的基础应用、系统管理、网络应用、娱乐和办公、程序开发、服务器配置、系统安全等。本书附带1张光盘,内容为本书配套多媒体教学视频。另外,本书还为读者提供了大量的Linux学习资料和Ubuntu安装镜像文件,供读者免费下载。

华为18级工程师呕心沥血撰写3000页Linux学习笔记教程

本书适合广大Linux初中级用户、开源软件爱好者和大专院校的学生阅读,同时也非常适合准备从事Linux平台开发的各类人员。

需要《Linux入门到精通》、《linux系统移植》、《Linux驱动开发入门实战》、《Linux开源网络全栈》电子书籍及教程的工程师朋友们劳烦您转发+评论

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

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

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

第97行的loss_c[pos] = 0前面加上一句loss_c = loss_c.view(num, -1)

ssd.py

把所有的num_classes的数量(第32、198行)都改为类别数+1

train.py

parser batch_sizelearning-rate根据自己电脑情况修改(batchsize=16);
basenet 预训练模型,start_iter迭代起始点,save_folder模型保存地址
搜索这里面的data[0],全部替换为item()
第84、85行注释掉;

# if args.dataset\_root == COCO\_ROOT: 
# parser.error('Must specify dataset if specifying dataset\_root')

第198行iteration % 5000 == 0,意味着每5000次保存一次模型,可改为200。后两行可改保存的模型名。

可以在第195行创建txt记录loss值:

with open('loss.txt', 'a') as f:
    f.write(str(loss.item()) + '\n')

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 (c) 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
修改rootclasses

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


### 最后的话

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

### 资料预览

给大家整理的视频资料:

![](https://img-blog.csdnimg.cn/img_convert/b28151d072227b704b6fe8ed24dfa88a.png)

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

  

![](https://img-blog.csdnimg.cn/img_convert/66684766caa580fe40ed9f60e000002e.png)



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

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

**[需要这份系统化的资料的朋友,可以点击这里获取!](https://bbs.csdn.net/topics/618635766)**


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

nels = img.shape


### 最后的话

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

### 资料预览

给大家整理的视频资料:

[外链图片转存中...(img-HG7OwUQW-1715881764719)]

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

  

[外链图片转存中...(img-XgydUGbk-1715881764720)]



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

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

**[需要这份系统化的资料的朋友,可以点击这里获取!](https://bbs.csdn.net/topics/618635766)**


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

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值