faster RCNN 训练新数据集(kitti)

基于pytorch版的faster rcnn源码:https://github.com/jwyang/faster-rcnn.pytorch

一、训练(trainval)

1. 制作voc格式的kitti数据集,并链接到data/

数据集软链接
格式

ln -s $VOCdevkit VOCdevkit2007

我们的:(这三个都要链过去,只链一个1819不够的)

cd data/
ln -s /home/zhaoxl/jiaqun/lujialin/data/VOCdevkit2018 VOCdevkit2018
ln -s /home/zhaoxl/jiaqun/lujialin/data/VOCdevkit2019 VOCdevkit2019
ln -s /home/zhaoxl/jiaqun/lujialin/data/VOCdevkit1819 VOCdevkit1819

注:
直接将kitti的label(.txt)转换成voc格式的(.xml)

这是最简单方便的办法,因为faster rcnn源码中,制作imdb数据的过程中集成了pascalvoc.py 和coco.py ,也就是已经写好了对着两种数据格式的解析。如果不转换格式,就需要自己写解析文件(将.txt转为数据训练时需要用的imdb)

2. train_val.py中仿照pascal_voc数据集改动一些内容

-(1)42行左右,–dataset中把预设值改成自制数据集的名字’citykitti’

parser.add_argument('--dataset', dest='dataset',
                      help='training dataset',
                      default='citykitti', type=str)

(2)159行左右, if args.dataset == “pascal_voc”:后面添加一个elif,内容如下,用于将自制数据集根据‘数据集/年份/trainval’,制作imdb丢进gpu训练,其中:

  • voc_2018是自制kitti数据集
  • voc_2019是自制cityscape数据集
  • 两者相加表示两个一起训练
  • 在训练(trainval.py)的时候只会用到args.imdb_name,在测试(test.py)的时候会用到args.imdbval_name

elif args.dataset == "citykitti":
      args.imdb_name = "voc_2018_train+voc_2019_train"
      args.imdbval_name = "voc_2018_val+voc_2019_val"
      args.set_cfgs = ['ANCHOR_SCALES', '[8, 16, 32]', 'ANCHOR_RATIOS', '[0.5,1,2]', 'MAX_NUM_GT_BOXES', '50']

3. lib/datasets/factory.py 解析voc数据集时,加上新加入的两个年份

第20行加上’2018’,‘2019’,改完如下:

# Set up voc_<year>_<split>
for year in ['2007', '2012','2018','2019']: # 自制数据集名称是VOC2018和VOC2019
  for split in ['train', 'val', 'trainval', 'test']:
    name = 'voc_{}_{}'.format(year, split)
    __sets[name] = (lambda split=split, year=year: pascal_voc(split, year))

4. lib/datasets/pascalvoc.py的 class,改成我们的

第47行

self._classes = ('__background__',  # always index 0
                         'pedestrian','car')

出现问题及解决:

问题1. target_ratio类型转换

File "/home/zhaoxl/jiaqun/lujialin/faster-rcnn-kitti-light/lib/roi_data_layer/roibatchLoader.py", line 54, in __init__
    self.ratio_list_batch[left_idx:(right_idx+1)] = target_ratio
TypeError: can't assign a numpy.int64 to a torch.FloatTensor
  • 解决方法

在lib/roi_data_layer/roibatchLoader.py
第53行,加上:

target_ratio = int(target_ratio)

问题2. 数据整理

File "/home/zhaoxl/jiaqun/lujialin/faster-rcnn.cittykitti/lib/datasets/imdb.py", line 123, in append_flipped_images
    assert (boxes[:, 2] >= boxes[:, 0]).all()
AssertionError
  • 解决方法

修改lib/datasets/imdb.py,append_flipped_images()函数。
在 boxes[:, 2] = widths[i] - oldx1 - 1 的下面加入代码:

for b in range(len(boxes)):
  if boxes[b][2]< boxes[b][0]:
    boxes[b][0] = 0

问题3:loss=nan

第二个iter时,

fg/bg = 256/0

各种loss = nan

  • 解决方法

查了很多教程,大家遇到的问题和解决方法基本就这几种,下面进行总结:

方法1.删掉data/cache

这个最终解决了我的问题!!

这里删除catch的意义是!把之前生成的imdb给删掉,也就是说,如果用后面的方法改了一些代码,也必须要重新删除一次cache!才能奏效!
方法2. 确定gt坐标不会为负值(方法1)

修改lib/datasets/imdb.py,append_flipped_images()函数
数据整理,在一行代码为 boxes[:, 2] = widths[i] - oldx1 - 1下加入代码:

for b in range(len(boxes)):
  if boxes[b][2]< boxes[b][0]:
    boxes[b][0] = 0
改完代码记得删cache再运行
方法3. 确定gt坐标不会为负值(方法2)

如果你自己制作了voc pascal或者coco数据集格式,那么你需要注意,看看是否有类似下面的报错

RuntimeWarning: invalid value encountered in log targets_dw = np.log(gt_widths / ex_widths)

这种报错说明数据集的数据有一些问题,多出现在没有控制好边界的情况,首先,打开lib/database/pascal_voc.py文件,找到208行,将208行至211行每一行后面的-1删除,如下所示:

x1 = float(bbox.find(‘xmin’).text) 
y1 = float(bbox.find(‘ymin’).text) 
x2 = float(bbox.find(‘xmax’).text) 
y2 = float(bbox.find(‘ymax’).text)

原因是因为我们制作的xml文件中有些框的坐标是从左上角开始的,也就是(0,0)如果再减一就会出现log(-1)的情况

改完代码记得删cache再运行
方法4. 取消数据翻转(先做2,3,不行再做4。最好不要取消)

打开./lib/model/config.py文件,找到flipp选项,将其置为False

__C.TRAIN.USE_FLIPPED = False
改完代码记得删cache再运行
方法5. 调小学习率(最后进行,最好不要进行)

其他一些有帮助的链接

解决fasterrcnn制作新数据集

https://blog.csdn.net/xzzppp/article/details/52036794
https://blog.csdn.net/ksws0292756/article/details/80702704
https://blog.csdn.net/qq_14839543/article/details/72900863
https://blog.csdn.net/flztiii/article/details/73881954

解决loss=nan的

https://www.cnblogs.com/hypnus-ly/p/9895885.html

检查输入数据和target中是否有 nan 值

    print('-------------new iter----------------')
    for step in range(iters_per_epoch):

      data = next(data_iter)

      im_data.data.resize_(data[0].size()).copy_(data[0])
      im_info.data.resize_(data[1].size()).copy_(data[1])
      gt_boxes.data.resize_(data[2].size()).copy_(data[2])
      num_boxes.data.resize_(data[3].size()).copy_(data[3])
      print(im_data.size(),gt_boxes.size())
      
      tempa = im_data.view(-1).cpu().numpy()
      tempb = gt_boxes.view(-1).cpu().numpy()
      tempa = pd.Series(tempa)
      tempb = pd.Series(tempb)
      anull = tempa.isnull().values.any()
      bnull = tempb.isnull().values.any()
      print('im_data: ',anull)
      print('gt_boxes: ',bnull)

      if anull or bnull:
          sys.exit()

loss=nan 程序停止

if isnan(loss_temp):
          sys.exit()

打印每一层的weight和gred

model改成fasterRCNN,把这段代码放在 iter的循环里。

for params in model.named_parameters():
    [name, param] = params

    if param.grad is not None:
        print(name, end='\t')
        print('weight:{}'.format(param.data.mean()), end='\t')
        print('grad:{}'.format(param.grad.data.mean()))

二、测试(test)

先改 test.py

类似trainval.py的修改模式, 这段复制过去,并着重把args.imdbval_name,改成测试或验证集的名称

elif args.dataset == "citykitti":
      args.imdb_name = "voc_2018_train+voc_2019_train"
      args.imdbval_name = "voc_2018_val+voc_2019_val"
      args.set_cfgs = ['ANCHOR_SCALES', '[8, 16, 32]', 'ANCHOR_RATIOS', '[0.5,1,2]', 'MAX_NUM_GT_BOXES', '50']

重点改 lib/datasets/voc_eval.py

  • 主要参考这篇,但他有一点点说的不全: https://blog.csdn.net/forteenscoops/article/details/79738870
  • 主旨就是把 voc_eval 里的关于’difficult’的都删了,因为我们自己做数据集的时候没有标注这个东西。但在删的时候,有一个地方不删,而是改一下

npos = npos + sum(~difficult)

改为

npos = npos + len®

因为改动的地方比较多,这里贴出全部的代码~
# --------------------------------------------------------
# Fast/er R-CNN
# Licensed under The MIT License [see LICENSE for details]
# Written by Bharath Hariharan
# --------------------------------------------------------
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import xml.etree.ElementTree as ET
import os
import pickle
import numpy as np

def parse_rec(filename):
  """ Parse a PASCAL VOC xml file """
  tree = ET.parse(filename)
  objects = []
  for obj in tree.findall('object'):
    obj_struct = {}
    obj_struct['name'] = obj.find('name').text
    # obj_struct['pose'] = obj.find('pose').text
    # obj_struct['truncated'] = int(obj.find('truncated').text)
    # obj_struct['difficult'] = int(obj.find('difficult').text)
    bbox = obj.find('bndbox')
    obj_struct['bbox'] = [int(bbox.find('xmin').text),
                          int(bbox.find('ymin').text),
                          int(bbox.find('xmax').text),
                          int(bbox.find('ymax').text)]
    objects.append(obj_struct)

  return objects


def voc_ap(rec, prec, use_07_metric=False):
  """ ap = voc_ap(rec, prec, [use_07_metric])
  Compute VOC AP given precision and recall.
  If use_07_metric is true, uses the
  VOC 07 11 point method (default:False).
  """
  if use_07_metric:
    # 11 point metric
    ap = 0.
    for t in np.arange(0., 1.1, 0.1):
      if np.sum(rec >= t) == 0:
        p = 0
      else:
        p = np.max(prec[rec >= t])
      ap = ap + p / 11.
  else:
    # correct AP calculation
    # first append sentinel values at the end
    mrec = np.concatenate(([0.], rec, [1.]))
    mpre = np.concatenate(([0.], prec, [0.]))

    # compute the precision envelope
    for i in range(mpre.size - 1, 0, -1):
      mpre[i - 1] = np.maximum(mpre[i - 1], mpre[i])

    # to calculate area under PR curve, look for points
    # where X axis (recall) changes value
    i = np.where(mrec[1:] != mrec[:-1])[0]

    # and sum (\Delta recall) * prec
    ap = np.sum((mrec[i + 1] - mrec[i]) * mpre[i + 1])
  return ap


def voc_eval(detpath,
             annopath,
             imagesetfile,
             classname,
             cachedir,
             ovthresh=0.5,
             use_07_metric=False):
  """rec, prec, ap = voc_eval(detpath,
                              annopath,
                              imagesetfile,
                              classname,
                              [ovthresh],
                              [use_07_metric])

  Top level function that does the PASCAL VOC evaluation.

  detpath: Path to detections
      detpath.format(classname) should produce the detection results file.
  annopath: Path to annotations
      annopath.format(imagename) should be the xml annotations file.
  imagesetfile: Text file containing the list of images, one image per line.
  classname: Category name (duh)
  cachedir: Directory for caching the annotations
  [ovthresh]: Overlap threshold (default = 0.5)
  [use_07_metric]: Whether to use VOC07's 11 point AP computation
      (default False)
  """
  # assumes detections are in detpath.format(classname)
  # assumes annotations are in annopath.format(imagename)
  # assumes imagesetfile is a text file with each line an image name
  # cachedir caches the annotations in a pickle file

  # first load gt
  if not os.path.isdir(cachedir):
    os.mkdir(cachedir)
  cachefile = os.path.join(cachedir, '%s_annots.pkl' % imagesetfile)
  # read list of images
  with open(imagesetfile, 'r') as f:
    lines = f.readlines()
  imagenames = [x.strip() for x in lines]

  if not os.path.isfile(cachefile):
    # load annotations
    recs = {}
    for i, imagename in enumerate(imagenames):
      recs[imagename] = parse_rec(annopath.format(imagename))
      if i % 100 == 0:
        print('Reading annotation for {:d}/{:d}'.format(
          i + 1, len(imagenames)))
    # save
    print('Saving cached annotations to {:s}'.format(cachefile))
    with open(cachefile, 'wb') as f:
      pickle.dump(recs, f)
  else:
    # load
    with open(cachefile, 'rb') as f:
      try:
        recs = pickle.load(f)
      except:
        recs = pickle.load(f, encoding='bytes')

  # extract gt objects for this class
  class_recs = {}
  npos = 0 
  for imagename in imagenames:
    R = [obj for obj in recs[imagename] if obj['name'] == classname]
    bbox = np.array([x['bbox'] for x in R])
    # difficult = np.array([x['difficult'] for x in R]).astype(np.bool)
    det = [False] * len(R)
    # npos = npos + sum(~difficult)
    npos = npos + len(R)
    class_recs[imagename] = {'bbox': bbox,
                             #'difficult': difficult,
                             'det': det}

  # read dets
  detfile = detpath.format(classname)
  with open(detfile, 'r') as f:
    lines = f.readlines()

  splitlines = [x.strip().split(' ') for x in lines]
  image_ids = [x[0] for x in splitlines]
  confidence = np.array([float(x[1]) for x in splitlines])
  BB = np.array([[float(z) for z in x[2:]] for x in splitlines])

  nd = len(image_ids)
  tp = np.zeros(nd)
  fp = np.zeros(nd)

  if BB.shape[0] > 0:
    # sort by confidence
    sorted_ind = np.argsort(-confidence)
    sorted_scores = np.sort(-confidence)
    BB = BB[sorted_ind, :]
    image_ids = [image_ids[x] for x in sorted_ind]

    # go down dets and mark TPs and FPs
    for d in range(nd):
      R = class_recs[image_ids[d]]
      bb = BB[d, :].astype(float)
      ovmax = -np.inf
      BBGT = R['bbox'].astype(float)

      if BBGT.size > 0:
        # compute overlaps
        # intersection
        ixmin = np.maximum(BBGT[:, 0], bb[0])
        iymin = np.maximum(BBGT[:, 1], bb[1])
        ixmax = np.minimum(BBGT[:, 2], bb[2])
        iymax = np.minimum(BBGT[:, 3], bb[3])
        iw = np.maximum(ixmax - ixmin + 1., 0.)
        ih = np.maximum(iymax - iymin + 1., 0.)
        inters = iw * ih

        # union
        uni = ((bb[2] - bb[0] + 1.) * (bb[3] - bb[1] + 1.) +
               (BBGT[:, 2] - BBGT[:, 0] + 1.) *
               (BBGT[:, 3] - BBGT[:, 1] + 1.) - inters)

        overlaps = inters / uni
        ovmax = np.max(overlaps)
        jmax = np.argmax(overlaps)
        
#      if ovmax > ovthresh:
#        if not R['difficult'][jmax]:
#          if not R['det'][jmax]:
#            tp[d] = 1.
#            R['det'][jmax] = 1
#          else:
#            fp[d] = 1.
#      else:
#        fp[d] = 1.
        
      if ovmax > ovthresh:
        if not R['det'][jmax]:
          tp[d] = 1.
          R['det'][jmax] = 1
        else:
          fp[d] = 1.
      else:
        fp[d] = 1.

  # compute precision recall
  fp = np.cumsum(fp)
  tp = np.cumsum(tp)
  rec = tp / float(npos)
  # avoid divide by zero in case the first detection matches a difficult
  # ground truth
  prec = tp / np.maximum(tp + fp, np.finfo(np.float64).eps)
  ap = voc_ap(rec, prec, use_07_metric)

  return rec, prec, ap

test可视化结果

# 首先
arg.vis=True

# 然后test.py文件最后:
if vis:
          cv2.imwrite('result.png', im2show)
          # pdb.set_trace()
          cv2.imshow('test', im2show)
          cv2.waitKey(0)
  • 4
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
### 回答1: Faster R-CNN是一种目标检测算法,可以用于训练VOC数据集训练过程需要先准备好VOC数据集,包括图片和标注文件。然后,需要使用Faster R-CNN的代码库进行训练,可以选择使用已经训练好的模型进行fine-tune,也可以从头开始训练训练过程需要设置好一些参数,如学习率、迭代次数等。最后,训练好的模型可以用于目标检测任务。 ### 回答2: Faster R-CNN是一种目标检测算法,其核心是使用深度学习技术对图像中的物体进行识别和检测。在训练过程中,VOC数据集是一种常用的数据集,它包含了多种物体的图像数据和标注信息,可用于训练目标检测模型。 首先,需要对VOC数据集进行预处理。具体来说,需要将数据集划分为训练集、验证集和测试集,并将图像数据和对应的标注信息进行处理,转化为模型可以处理的格式。这个过程需要使用相关的工具和软件,如Pascal VOC tools等。 接下来,需要选择适合的深度学习框架和算法,如TensorFlow等,并进行相关的配置。然后,可以使用上述工具和软件进行训练。在训练过程中,首先需要确定模型的结构和超参数,如网络层数、学习率等。然后,需要处理训练数据,并将其输入到模型中进行训练。 在训练过程中,需要不断调整超参数和模型结构,优化模型性能。同时,还需要进行模型的验证和测试,确认模型的准确性和可靠性。 总体而言,Faster R-CNN训练VOC数据集是一个复杂的过程,需要细致地设计和调整模型,并针对特定的任务进行不断迭代和优化。只有在充分的准备和细致的实验设计下,才能获得稳定的高性能检测模型。 ### 回答3: Faster R-CNN是一种基于深度学习的目标检测算法,可以对图像中的不同物体进行准确的识别和定位,并给出其在图像中的位置和类别。在Faster R-CNN中,利用了RPN网络对图像进行区域提议,然后通过分类和回归网络对提议区域进行检测,从而实现目标检测。 在Faster R-CNN训练中,VOC数据集是经典的物体识别和检测数据集之一,包含了20个不同类别的物体,每个类别的训练数据和测试数据均有多个样本。训练Faster R-CNN时,需要将VOC数据集转换成特定的格式,通常采用Pascal VOC或者COCO格式,然后通过类似于fine-tuning的方式对模型进行训练。 具体地说,Faster R-CNN训练流程可以分为以下几个步骤: 1. 数据准备和预处理:将VOC数据集转换成Pascal VOC或者COCO格式,并进行数据增强和预处理,如随机裁剪、缩放、旋转等操作,从而增加样本的多样性和模型的鲁棒性。 2. 网络初始化和参数设置:初始化Faster R-CNN网络,并设置相关的超参数和优化器,如学习率、迭代次数、损失函数等。 3. 区域提议训练:利用RPN网络对图像进行区域提议,然后通过IoU计算和NMS筛选,对提议区域进行优化,从而得到最终的候选区域。 4. 物体分类和回归训练:针对候选区域,利用分类和回归网络进行检测和修正,从而获得检测结果和物体位置信息。 5. 模型评估和调优:通过测试数据集对模型进行评估和调优,如调整超参数、选择不同的优化器等,从而获得更加准确和高效的检测模型。 以上就是Faster R-CNN训练VOC数据集的基本流程和步骤。需要注意的是,训练过程需要耗费大量的计算资源和时间,对硬件环境和数据集的选择和优化十分重要。此外,也需要不断地尝试和调整算法参数和模型架构,从而获得更加优秀和高效的目标检测结果。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值