Skip to content

Instantly share code, notes, and snippets.

@jewelcai jewelcai/dataset.py
Created Jul 11, 2019

Embed
What would you like to do?
faster rcnn training code
from __future__ import absolute_import
from __future__ import division
import os
import numpy as np
import mxnet as mx
from gluoncv.data.mscoco.utils import try_import_pycocotools
from gluoncv.data.base import VisionDataset
from gluoncv.utils.bbox import bbox_clip_xyxy, bbox_xywh_to_xyxy
__all__ = ['TableDetectionDataset']
class TableDetectionDataset(VisionDataset):
"""MS COCO detection dataset.
Parameters
----------
root : str, default '~/.mxnet/datasets/voc'
Path to folder storing the dataset.
splits : list of str, default ['instances_val2017']
Json annotations name.
Candidates can be: instances_val2017, instances_train2017.
transform : callable, default None
A function that takes data and label and transforms them. Refer to
:doc:`./transforms` for examples.
A transform function for object detection should take label into consideration,
because any geometric modification will require label to be modified.
min_object_area : float
Minimum accepted ground-truth area, if an object's area is smaller than this value,
it will be ignored.
skip_empty : bool, default is True
Whether skip images with no valid object. This should be `True` in training, otherwise
it will cause undefined behavior.
use_crowd : bool, default is True
Whether use boxes labeled as crowd instance.
"""
CLASSES = ['table']
def __init__(self, root=os.path.join('~', '.mxnet', 'datasets', 'coco'),
splits=('instances_val2017',), transform=None, min_object_area=0,
skip_empty=True, use_crowd=True):
super(TableDetectionDataset, self).__init__(root)
self._root = os.path.expanduser(root)
self._transform = transform
self._min_object_area = min_object_area
self._skip_empty = skip_empty
self._use_crowd = use_crowd
if isinstance(splits, mx.base.string_types):
splits = [splits]
self._splits = splits
# to avoid trouble, we always use contiguous IDs except dealing with cocoapi
self.index_map = dict(zip(type(self).CLASSES, range(self.num_class)))
self.json_id_to_contiguous = None
self.contiguous_id_to_json = None
self._coco = []
self._items, self._labels = self._load_jsons()
# print(len(self._items))
def __str__(self):
detail = ','.join([str(s) for s in self._splits])
return self.__class__.__name__ + '(' + detail + ')'
@property
def coco(self):
"""Return pycocotools object for evaluation purposes."""
if not self._coco:
raise ValueError("No coco objects found, dataset not initialized.")
if len(self._coco) > 1:
raise NotImplementedError(
"Currently we don't support evaluating {} JSON files. \
Please use single JSON dataset and evaluate one by one".format(len(self._coco)))
return self._coco[0]
@property
def classes(self):
"""Category names."""
return type(self).CLASSES
@property
def annotation_dir(self):
"""
The subdir for annotations. Default is 'annotations'(coco default)
For example, a coco format json file will be searched as
'root/annotation_dir/xxx.json'
You can override if custom dataset don't follow the same pattern
"""
return 'annotations'
def _parse_image_path(self, entry):
"""How to parse image dir and path from entry.
Parameters
----------
entry : dict
COCO entry, e.g. including width, height, image path, etc..
Returns
-------
abs_path : str
Absolute path for corresponding image.
"""
# dirname, filename = entry['coco_url'].split('/')[-2:]
filename = entry['file_name']
abs_path = os.path.join(self._root, filename)
return abs_path
def __len__(self):
return len(self._items)
def __getitem__(self, idx):
img_path = self._items[idx]
label = self._labels[idx]
img = mx.image.imread(img_path, 1)
if self._transform is not None:
return self._transform(img, label)
return img, np.array(label)
def _load_jsons(self):
"""Load all image paths and labels from JSON annotation files into buffer."""
items = []
labels = []
# lazy import pycocotools
try_import_pycocotools()
from pycocotools.coco import COCO
for split in self._splits:
anno = os.path.join(self._root, self.annotation_dir, split) + '.json'
# print(anno)
_coco = COCO(anno)
self._coco.append(_coco)
classes = [c['name'] for c in _coco.loadCats(_coco.getCatIds())]
# print(classes)
if not classes == self.classes:
raise ValueError("Incompatible category names with COCO: ")
assert classes == self.classes
json_id_to_contiguous = {
v: k for k, v in enumerate(_coco.getCatIds())}
if self.json_id_to_contiguous is None:
self.json_id_to_contiguous = json_id_to_contiguous
self.contiguous_id_to_json = {
v: k for k, v in self.json_id_to_contiguous.items()}
else:
assert self.json_id_to_contiguous == json_id_to_contiguous
# iterate through the annotations
image_ids = sorted(_coco.getImgIds())
# print(len(image_ids))
for entry in _coco.loadImgs(image_ids):
abs_path = self._parse_image_path(entry)
if not os.path.exists(abs_path):
raise IOError('Image: {} not exists.'.format(abs_path))
label = self._check_load_bbox(_coco, entry)
if not label:
# print('not',str(i))
continue
items.append(abs_path)
labels.append(label)
# if label[0][-1] is not 0:
# print(label)
# print(len(labels))
return items, labels
def _check_load_bbox(self, coco, entry):
"""Check and load ground-truth labels"""
entry_id = entry['id']
# fix pycocotools _isArrayLike which don't work for str in python3
entry_id = [entry_id] if not isinstance(entry_id, (list, tuple)) else entry_id
ann_ids = coco.getAnnIds(imgIds=entry_id, iscrowd=None)
objs = coco.loadAnns(ann_ids)
# check valid bboxes
valid_objs = []
width = entry['width']
height = entry['height']
for obj in objs:
if obj['area'] < self._min_object_area:
continue
if obj.get('ignore', 0) == 1:
continue
if not self._use_crowd and obj.get('iscrowd', 0):
continue
# convert from (x, y, w, h) to (xmin, ymin, xmax, ymax) and clip bound
xmin, ymin, xmax, ymax = bbox_clip_xyxy(bbox_xywh_to_xyxy(obj['bbox']), width, height)
# require non-zero box area
if obj['area'] > 0 and xmax > xmin and ymax > ymin:
contiguous_cid = self.json_id_to_contiguous[obj['category_id']]
valid_objs.append([xmin, ymin, xmax, ymax, contiguous_cid])
if not valid_objs:
if not self._skip_empty:
# dummy invalid labels if no valid objects are found
valid_objs.append([-1, -1, -1, -1, -1])
# if len(valid_objs) > 2:
# print(len(valid_objs) )
return valid_objs
Traceback (most recent call last):
File "/Users/zack/PycharmProjects/keras_tutorial/mxnet_models/train_faster_rcnn.py", line 492, in <module>
FasterRCNNDefaultValTransform, args.batch_size, args.num_workers, args.use_fpn)
File "/Users/zack/PycharmProjects/keras_tutorial/mxnet_models/train_faster_rcnn.py", line 221, in get_dataloader
train_transform(net.short, net.max_size, net, ashape=net.ashape, multi_stage=multi_stage)), batch_size,
File "/Users/zack/PycharmProjects/keras_tutorial/venvde/lib/python3.5/site-packages/gluoncv/data/transforms/presets/rcnn.py", line 171, in __init__
mx.nd.zeros((1, 3, ashape, ashape))).reshape((1, 1, ashape, ashape, -1))
File "/Users/zack/PycharmProjects/keras_tutorial/venvde/lib/python3.5/site-packages/mxnet/gluon/block.py", line 540, in __call__
out = self.forward(*args)
File "/Users/zack/PycharmProjects/keras_tutorial/venvde/lib/python3.5/site-packages/mxnet/gluon/block.py", line 917, in forward
return self.hybrid_forward(ndarray, x, *args, **params)
File "/Users/zack/PycharmProjects/keras_tutorial/venvde/lib/python3.5/site-packages/mxnet/gluon/nn/basic_layers.py", line 117, in hybrid_forward
x = block(x)
File "/Users/zack/PycharmProjects/keras_tutorial/venvde/lib/python3.5/site-packages/mxnet/gluon/block.py", line 540, in __call__
out = self.forward(*args)
File "/Users/zack/PycharmProjects/keras_tutorial/venvde/lib/python3.5/site-packages/mxnet/gluon/block.py", line 917, in forward
return self.hybrid_forward(ndarray, x, *args, **params)
File "/Users/zack/PycharmProjects/keras_tutorial/venvde/lib/python3.5/site-packages/gluoncv/model_zoo/rpn/anchor.py", line 91, in hybrid_forward
a = F.slice_like(anchors, x * 0, axes=(2, 3))
File "<string>", line 88, in slice_like
File "/Users/zack/PycharmProjects/keras_tutorial/venvde/lib/python3.5/site-packages/mxnet/_ctypes/ndarray.py", line 92, in _imperative_invoke
ctypes.byref(out_stypes)))
File "/Users/zack/PycharmProjects/keras_tutorial/venvde/lib/python3.5/site-packages/mxnet/base.py", line 252, in check_call
raise MXNetError(py_str(_LIB.MXGetLastError()))
mxnet.base.MXNetError: [22:27:09] src/operator/tensor/./matrix_op-inl.h:1286: Check failed: from_shape.ndim() > axis (3 vs. 3) Slice axis: 3 exceeds second input: 3
Stack trace returned 8 entries:
[bt] (0) 0 libmxnet.so 0x0000000119f3dc90 std::__1::__tree<std::__1::__value_type<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, mxnet::NDArrayFunctionReg*>, std::__1::__map_value_compare<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::__value_type<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, mxnet::NDArrayFunctionReg*>, std::__1::less<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >, true>, std::__1::allocator<std::__1::__value_type<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, mxnet::NDArrayFunctionReg*> > >::destroy(std::__1::__tree_node<std::__1::__value_type<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, mxnet::NDArrayFunctionReg*>, void*>*) + 2736
[bt] (1) 1 libmxnet.so 0x0000000119f3da3f std::__1::__tree<std::__1::__value_type<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, mxnet::NDArrayFunctionReg*>, std::__1::__map_value_compare<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::__value_type<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, mxnet::NDArrayFunctionReg*>, std::__1::less<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >, true>, std::__1::allocator<std::__1::__value_type<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, mxnet::NDArrayFunctionReg*> > >::destroy(std::__1::__tree_node<std::__1::__value_type<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, mxnet::NDArrayFunctionReg*>, void*>*) + 2143
[bt] (2) 2 libmxnet.so 0x000000011b3649a4 void mxnet::op::SliceEx<mshadow::cpu>(nnvm::NodeAttrs const&, mxnet::OpContext const&, std::__1::vector<mxnet::NDArray, std::__1::allocator<mxnet::NDArray> > const&, std::__1::vector<mxnet::OpReqType, std::__1::allocator<mxnet::OpReqType> > const&, std::__1::vector<mxnet::NDArray, std::__1::allocator<mxnet::NDArray> > const&) + 179220
[bt] (3) 3 libmxnet.so 0x000000011b5d85a9 mxnet::imperative::SetShapeType(mxnet::Context const&, nnvm::NodeAttrs const&, std::__1::vector<mxnet::NDArray*, std::__1::allocator<mxnet::NDArray*> > const&, std::__1::vector<mxnet::NDArray*, std::__1::allocator<mxnet::NDArray*> > const&, mxnet::DispatchMode*) + 1577
[bt] (4) 4 libmxnet.so 0x000000011b5d6f06 mxnet::Imperative::Invoke(mxnet::Context const&, nnvm::NodeAttrs const&, std::__1::vector<mxnet::NDArray*, std::__1::allocator<mxnet::NDArray*> > const&, std::__1::vector<mxnet::NDArray*, std::__1::allocator<mxnet::NDArray*> > const&) + 742
[bt] (5) 5 libmxnet.so 0x000000011b522d9e SetNDInputsOutputs(nnvm::Op const*, std::__1::vector<mxnet::NDArray*, std::__1::allocator<mxnet::NDArray*> >*, std::__1::vector<mxnet::NDArray*, std::__1::allocator<mxnet::NDArray*> >*, int, void* const*, int*, int, int, void***) + 1774
[bt] (6) 6 libmxnet.so 0x000000011b523ac0 MXImperativeInvokeEx + 176
[bt] (7) 7 _ctypes.cpython-35m-darwin.so 0x0000000118cba577 ffi_call_unix64 + 79
'''
@Time:2019/7/103:42 PM
@Author:Zack
'''
"""Train Faster-RCNN end to end."""
import argparse
import os
# disable autotune
os.environ['MXNET_CUDNN_AUTOTUNE_DEFAULT'] = '0'
import logging
import time
import numpy as np
import mxnet as mx
from mxnet import gluon
from mxnet import autograd
import gluoncv as gcv
from gluoncv import data as gdata
from gluoncv import utils as gutils
from gluoncv.model_zoo import get_model
from gluoncv.data import batchify
from gluoncv.data.transforms.presets.rcnn import FasterRCNNDefaultTrainTransform, \
FasterRCNNDefaultValTransform
from gluoncv.utils.metrics.coco_detection import COCODetectionMetric
from mxnet_models.table_detection_dataset import TableDetectionDataset
def parse_args():
parser = argparse.ArgumentParser(description='Train Faster-RCNN networks e2e.')
parser.add_argument('--network', type=str, default='resnet50_v1b',
help="Base network name which serves as feature extraction base.")
parser.add_argument('--dataset', type=str, default='coco',
help='Training dataset. Now support voc and coco.')
parser.add_argument('--num-workers', '-j', dest='num_workers', type=int,
default=5, help='Number of data workers, you can use larger '
'number to accelerate data loading, '
'if your CPU and GPUs are powerful.')
parser.add_argument('--gpus', type=str, default='0',
help='Training with GPUs, you can specify 1,3 for example.')
parser.add_argument('--epochs', type=str, default='',
help='Training epochs.')
parser.add_argument('--resume', type=str, default='',
help='Resume from previously saved parameters if not None. '
'For example, you can resume from ./faster_rcnn_xxx_0123.params')
parser.add_argument('--start-epoch', type=int, default=0,
help='Starting epoch for resuming, default is 0 for new training.'
'You can specify it to 100 for example to start from 100 epoch.')
parser.add_argument('--lr', type=str, default='',
help='Learning rate, default is 0.001 for voc single gpu training.')
parser.add_argument('--lr-decay', type=float, default=0.1,
help='decay rate of learning rate. default is 0.1.')
parser.add_argument('--lr-decay-epoch', type=str, default='',
help='epochs at which learning rate decays. default is 14,20 for voc.')
parser.add_argument('--lr-warmup', type=str, default='',
help='warmup iterations to adjust learning rate, default is 0 for voc.')
parser.add_argument('--momentum', type=float, default=0.9,
help='SGD momentum, default is 0.9')
parser.add_argument('--wd', type=str, default='',
help='Weight decay, default is 5e-4 for voc')
parser.add_argument('--log-interval', type=int, default=100,
help='Logging mini-batch interval. Default is 100.')
parser.add_argument('--save-prefix', type=str, default='',
help='Saving parameter prefix')
parser.add_argument('--save-interval', type=int, default=1,
help='Saving parameters epoch interval, best model will always be saved.')
parser.add_argument('--val-interval', type=int, default=1,
help='Epoch interval for validation, increase the number will reduce the '
'training time if validation is slow.')
parser.add_argument('--seed', type=int, default=233,
help='Random seed to be fixed.')
parser.add_argument('--verbose', dest='verbose', action='store_true',
help='Print helpful debugging info once set.')
parser.add_argument('--mixup', action='store_true', help='Use mixup training.')
parser.add_argument('--no-mixup-epochs', type=int, default=20,
help='Disable mixup training if enabled in the last N epochs.')
# Norm layer options
parser.add_argument('--norm-layer', type=str, default=None,
help='Type of normalization layer to use. '
'If set to None, backbone normalization layer will be fixed,'
' and no normalization layer will be used. '
'Currently supports \'bn\', and None, default is None')
# FPN options
parser.add_argument('--use-fpn', action='store_true',
help='Whether to use feature pyramid network.')
# Performance options
parser.add_argument('--disable-hybridization', action='store_true',
help='Whether to disable hybridize the model. '
'Memory usage and speed will decrese.')
parser.add_argument('--static-alloc', action='store_true',
help='Whether to use static memory allocation. Memory usage will increase.')
args = parser.parse_args()
if args.dataset == 'voc':
args.epochs = int(args.epochs) if args.epochs else 20
args.lr_decay_epoch = args.lr_decay_epoch if args.lr_decay_epoch else '14,20'
args.lr = float(args.lr) if args.lr else 0.001
args.lr_warmup = args.lr_warmup if args.lr_warmup else -1
args.wd = float(args.wd) if args.wd else 5e-4
elif args.dataset == 'coco':
args.epochs = int(args.epochs) if args.epochs else 26
args.lr_decay_epoch = args.lr_decay_epoch if args.lr_decay_epoch else '17,23'
args.lr = float(args.lr) if args.lr else 0.00125
args.lr_warmup = args.lr_warmup if args.lr_warmup else 8000
args.wd = float(args.wd) if args.wd else 1e-4
num_gpus = len(args.gpus.split(','))
if num_gpus == 1:
args.lr_warmup = -1
else:
args.lr *= num_gpus
args.lr_warmup /= num_gpus
return args
class RPNAccMetric(mx.metric.EvalMetric):
def __init__(self):
super(RPNAccMetric, self).__init__('RPNAcc')
def update(self, labels, preds):
# label: [rpn_label, rpn_weight]
# preds: [rpn_cls_logits]
rpn_label, rpn_weight = labels
rpn_cls_logits = preds[0]
# calculate num_inst (average on those fg anchors)
num_inst = mx.nd.sum(rpn_weight)
# cls_logits (b, c, h, w) red_label (b, 1, h, w)
# pred_label = mx.nd.argmax(rpn_cls_logits, axis=1, keepdims=True)
pred_label = mx.nd.sigmoid(rpn_cls_logits) >= 0.5
# label (b, 1, h, w)
num_acc = mx.nd.sum((pred_label == rpn_label) * rpn_weight)
self.sum_metric += num_acc.asscalar()
self.num_inst += num_inst.asscalar()
class RPNL1LossMetric(mx.metric.EvalMetric):
def __init__(self):
super(RPNL1LossMetric, self).__init__('RPNL1Loss')
def update(self, labels, preds):
# label = [rpn_bbox_target, rpn_bbox_weight]
# pred = [rpn_bbox_reg]
rpn_bbox_target, rpn_bbox_weight = labels
rpn_bbox_reg = preds[0]
# calculate num_inst (average on those fg anchors)
num_inst = mx.nd.sum(rpn_bbox_weight) / 4
# calculate smooth_l1
loss = mx.nd.sum(
rpn_bbox_weight * mx.nd.smooth_l1(rpn_bbox_reg - rpn_bbox_target, scalar=3))
self.sum_metric += loss.asscalar()
self.num_inst += num_inst.asscalar()
class RCNNAccMetric(mx.metric.EvalMetric):
def __init__(self):
super(RCNNAccMetric, self).__init__('RCNNAcc')
def update(self, labels, preds):
# label = [rcnn_label]
# pred = [rcnn_cls]
rcnn_label = labels[0]
rcnn_cls = preds[0]
# calculate num_acc
pred_label = mx.nd.argmax(rcnn_cls, axis=-1)
num_acc = mx.nd.sum(pred_label == rcnn_label)
self.sum_metric += num_acc.asscalar()
self.num_inst += rcnn_label.size
class RCNNL1LossMetric(mx.metric.EvalMetric):
def __init__(self):
super(RCNNL1LossMetric, self).__init__('RCNNL1Loss')
def update(self, labels, preds):
# label = [rcnn_bbox_target, rcnn_bbox_weight]
# pred = [rcnn_reg]
rcnn_bbox_target, rcnn_bbox_weight = labels
rcnn_bbox_reg = preds[0]
# calculate num_inst
num_inst = mx.nd.sum(rcnn_bbox_weight) / 4
# calculate smooth_l1
loss = mx.nd.sum(
rcnn_bbox_weight * mx.nd.smooth_l1(rcnn_bbox_reg - rcnn_bbox_target, scalar=1))
self.sum_metric += loss.asscalar()
self.num_inst += num_inst.asscalar()
def get_dataset(dataset, args):
train_dataset = TableDetectionDataset(root='./images/train', splits='train', use_crowd=False)
val_dataset = TableDetectionDataset(root='./images/test', splits='test', skip_empty=False)
val_metric = COCODetectionMetric(val_dataset, args.save_prefix + '_eval', cleanup=True)
if args.mixup:
from gluoncv.data.mixup import detection
train_dataset = detection.MixupDetection(train_dataset)
return train_dataset, val_dataset, val_metric
def get_dataloader(net, train_dataset, val_dataset, train_transform, val_transform, batch_size,
num_workers, multi_stage):
"""Get dataloader."""
train_bfn = batchify.Tuple(*[batchify.Append() for _ in range(5)])
train_loader = mx.gluon.data.DataLoader(
train_dataset.transform(
train_transform(net.short, net.max_size, net, ashape=net.ashape, multi_stage=multi_stage)), batch_size,
True, batchify_fn=train_bfn, last_batch='rollover', num_workers=num_workers)
val_bfn = batchify.Tuple(*[batchify.Append() for _ in range(3)])
short = net.short[-1] if isinstance(net.short, (tuple, list)) else net.short
val_loader = mx.gluon.data.DataLoader(
val_dataset.transform(val_transform(short, net.max_size)),
batch_size, False, batchify_fn=val_bfn, last_batch='keep', num_workers=num_workers)
return train_loader, val_loader
def save_params(net, logger, best_map, current_map, epoch, save_interval, prefix):
current_map = float(current_map)
if current_map > best_map[0]:
logger.info('[Epoch {}] mAP {} higher than current best {} saving to {}'.format(
epoch, current_map, best_map, '{:s}_best.params'.format(prefix)))
best_map[0] = current_map
net.save_parameters('{:s}_best.params'.format(prefix))
with open(prefix + '_best_map.log', 'a') as f:
f.write('{:04d}:\t{:.4f}\n'.format(epoch, current_map))
if save_interval and (epoch + 1) % save_interval == 0:
logger.info('[Epoch {}] Saving parameters to {}'.format(
epoch, '{:s}_{:04d}_{:.4f}.params'.format(prefix, epoch, current_map)))
net.save_parameters('{:s}_{:04d}_{:.4f}.params'.format(prefix, epoch, current_map))
def split_and_load(batch, ctx_list):
"""Split data to 1 batch each device."""
num_ctx = len(ctx_list)
new_batch = []
for i, data in enumerate(batch):
new_data = [x.as_in_context(ctx) for x, ctx in zip(data, ctx_list)]
new_batch.append(new_data)
return new_batch
def validate(net, val_data, ctx, eval_metric, args):
"""Test on validation dataset."""
clipper = gcv.nn.bbox.BBoxClipToImage()
eval_metric.reset()
if not args.disable_hybridization:
net.hybridize(static_alloc=args.static_alloc)
for batch in val_data:
batch = split_and_load(batch, ctx_list=ctx)
det_bboxes = []
det_ids = []
det_scores = []
gt_bboxes = []
gt_ids = []
gt_difficults = []
for x, y, im_scale in zip(*batch):
# get prediction results
ids, scores, bboxes = net(x)
det_ids.append(ids)
det_scores.append(scores)
# clip to image size
det_bboxes.append(clipper(bboxes, x))
# rescale to original resolution
im_scale = im_scale.reshape((-1)).asscalar()
det_bboxes[-1] *= im_scale
# split ground truths
gt_ids.append(y.slice_axis(axis=-1, begin=4, end=5))
gt_bboxes.append(y.slice_axis(axis=-1, begin=0, end=4))
gt_bboxes[-1] *= im_scale
gt_difficults.append(y.slice_axis(axis=-1, begin=5, end=6) if y.shape[-1] > 5 else None)
# update metric
for det_bbox, det_id, det_score, gt_bbox, gt_id, gt_diff in zip(det_bboxes, det_ids,
det_scores, gt_bboxes,
gt_ids, gt_difficults):
eval_metric.update(det_bbox, det_id, det_score, gt_bbox, gt_id, gt_diff)
return eval_metric.get()
def get_lr_at_iter(alpha):
return 1. / 3. * (1 - alpha) + alpha
def train(net, train_data, val_data, eval_metric, ctx, args):
"""Training pipeline"""
net.collect_params().setattr('grad_req', 'null')
net.collect_train_params().setattr('grad_req', 'write')
trainer = gluon.Trainer(
net.collect_train_params(), # fix batchnorm, fix first stage, etc...
'sgd',
{'learning_rate': args.lr,
'wd': args.wd,
'momentum': args.momentum})
# lr decay policy
lr_decay = float(args.lr_decay)
lr_steps = sorted([float(ls) for ls in args.lr_decay_epoch.split(',') if ls.strip()])
lr_warmup = float(args.lr_warmup) # avoid int division
# TODO(zhreshold) losses?
rpn_cls_loss = mx.gluon.loss.SigmoidBinaryCrossEntropyLoss(from_sigmoid=False)
rpn_box_loss = mx.gluon.loss.HuberLoss(rho=1 / 9.) # == smoothl1
rcnn_cls_loss = mx.gluon.loss.SoftmaxCrossEntropyLoss()
rcnn_box_loss = mx.gluon.loss.HuberLoss() # == smoothl1
metrics = [mx.metric.Loss('RPN_Conf'),
mx.metric.Loss('RPN_SmoothL1'),
mx.metric.Loss('RCNN_CrossEntropy'),
mx.metric.Loss('RCNN_SmoothL1'), ]
rpn_acc_metric = RPNAccMetric()
rpn_bbox_metric = RPNL1LossMetric()
rcnn_acc_metric = RCNNAccMetric()
rcnn_bbox_metric = RCNNL1LossMetric()
metrics2 = [rpn_acc_metric, rpn_bbox_metric, rcnn_acc_metric, rcnn_bbox_metric]
# set up logger
logging.basicConfig()
logger = logging.getLogger()
logger.setLevel(logging.INFO)
log_file_path = args.save_prefix + '_train.log'
log_dir = os.path.dirname(log_file_path)
if log_dir and not os.path.exists(log_dir):
os.makedirs(log_dir)
fh = logging.FileHandler(log_file_path)
logger.addHandler(fh)
logger.info(args)
if args.verbose:
logger.info('Trainable parameters:')
logger.info(net.collect_train_params().keys())
logger.info('Start training from [Epoch {}]'.format(args.start_epoch))
best_map = [0]
for epoch in range(args.start_epoch, args.epochs):
mix_ratio = 1.0
if args.mixup:
# TODO(zhreshold) only support evenly mixup now, target generator needs to be modified otherwise
train_data._dataset._data.set_mixup(np.random.uniform, 0.5, 0.5)
mix_ratio = 0.5
if epoch >= args.epochs - args.no_mixup_epochs:
train_data._dataset._data.set_mixup(None)
mix_ratio = 1.0
while lr_steps and epoch >= lr_steps[0]:
new_lr = trainer.learning_rate * lr_decay
lr_steps.pop(0)
trainer.set_learning_rate(new_lr)
logger.info("[Epoch {}] Set learning rate to {}".format(epoch, new_lr))
for metric in metrics:
metric.reset()
tic = time.time()
btic = time.time()
if not args.disable_hybridization:
net.hybridize(static_alloc=args.static_alloc)
base_lr = trainer.learning_rate
for i, batch in enumerate(train_data):
if epoch == 0 and i <= lr_warmup:
# adjust based on real percentage
new_lr = base_lr * get_lr_at_iter(i / lr_warmup)
if new_lr != trainer.learning_rate:
if i % args.log_interval == 0:
logger.info(
'[Epoch 0 Iteration {}] Set learning rate to {}'.format(i, new_lr))
trainer.set_learning_rate(new_lr)
batch = split_and_load(batch, ctx_list=ctx)
batch_size = len(batch[0])
losses = []
metric_losses = [[] for _ in metrics]
add_losses = [[] for _ in metrics2]
with autograd.record():
for data, label, rpn_cls_targets, rpn_box_targets, rpn_box_masks in zip(*batch):
gt_label = label[:, :, 4:5]
gt_box = label[:, :, :4]
cls_pred, box_pred, roi, samples, matches, rpn_score, rpn_box, anchors = net(
data, gt_box)
# losses of rpn
rpn_score = rpn_score.squeeze(axis=-1)
num_rpn_pos = (rpn_cls_targets >= 0).sum()
rpn_loss1 = rpn_cls_loss(rpn_score, rpn_cls_targets,
rpn_cls_targets >= 0) * rpn_cls_targets.size / num_rpn_pos
rpn_loss2 = rpn_box_loss(rpn_box, rpn_box_targets,
rpn_box_masks) * rpn_box.size / num_rpn_pos
# rpn overall loss, use sum rather than average
rpn_loss = rpn_loss1 + rpn_loss2
# generate targets for rcnn
cls_targets, box_targets, box_masks = net.target_generator(roi, samples,
matches, gt_label,
gt_box)
# losses of rcnn
num_rcnn_pos = (cls_targets >= 0).sum()
rcnn_loss1 = rcnn_cls_loss(cls_pred, cls_targets,
cls_targets >= 0) * cls_targets.size / \
cls_targets.shape[0] / num_rcnn_pos
rcnn_loss2 = rcnn_box_loss(box_pred, box_targets, box_masks) * box_pred.size / \
box_pred.shape[0] / num_rcnn_pos
rcnn_loss = rcnn_loss1 + rcnn_loss2
# overall losses
losses.append(rpn_loss.sum() * mix_ratio + rcnn_loss.sum() * mix_ratio)
metric_losses[0].append(rpn_loss1.sum() * mix_ratio)
metric_losses[1].append(rpn_loss2.sum() * mix_ratio)
metric_losses[2].append(rcnn_loss1.sum() * mix_ratio)
metric_losses[3].append(rcnn_loss2.sum() * mix_ratio)
add_losses[0].append([[rpn_cls_targets, rpn_cls_targets >= 0], [rpn_score]])
add_losses[1].append([[rpn_box_targets, rpn_box_masks], [rpn_box]])
add_losses[2].append([[cls_targets], [cls_pred]])
add_losses[3].append([[box_targets, box_masks], [box_pred]])
autograd.backward(losses)
for metric, record in zip(metrics, metric_losses):
metric.update(0, record)
for metric, records in zip(metrics2, add_losses):
for pred in records:
metric.update(pred[0], pred[1])
trainer.step(batch_size)
# update metrics
if args.log_interval and not (i + 1) % args.log_interval:
# msg = ','.join(['{}={:.3f}'.format(*metric.get()) for metric in metrics])
msg = ','.join(['{}={:.3f}'.format(*metric.get()) for metric in metrics + metrics2])
logger.info('[Epoch {}][Batch {}], Speed: {:.3f} samples/sec, {}'.format(
epoch, i, args.log_interval * batch_size / (time.time() - btic), msg))
btic = time.time()
msg = ','.join(['{}={:.3f}'.format(*metric.get()) for metric in metrics])
logger.info('[Epoch {}] Training cost: {:.3f}, {}'.format(
epoch, (time.time() - tic), msg))
if not (epoch + 1) % args.val_interval:
# consider reduce the frequency of validation to save time
map_name, mean_ap = validate(net, val_data, ctx, eval_metric, args)
val_msg = '\n'.join(['{}={}'.format(k, v) for k, v in zip(map_name, mean_ap)])
logger.info('[Epoch {}] Validation: \n{}'.format(epoch, val_msg))
current_map = float(mean_ap[-1])
else:
current_map = 0.
save_params(net, logger, best_map, current_map, epoch, args.save_interval, args.save_prefix)
if __name__ == '__main__':
import sys
sys.setrecursionlimit(1100)
args = parse_args()
# fix seed for mxnet, numpy and python builtin random generator.
gutils.random.seed(args.seed)
# training contexts
ctx = [mx.cpu()]
# ctx = [mx.gpu(int(i)) for i in args.gpus.split(',') if i.strip()]
# ctx = ctx if ctx else [mx.cpu()]
args.batch_size = len(ctx) # 1 batch per device
# network
kwargs = {}
module_list = []
if args.use_fpn:
module_list.append('fpn')
if args.norm_layer is not None:
module_list.append(args.norm_layer)
if args.norm_layer == 'bn':
kwargs['num_devices'] = len(args.gpus.split(','))
# net_name = '_'.join(('faster_rcnn', *module_list, args.network, args.dataset))
net_name = 'faster_rcnn_fpn_resnet101_v1d_coco'
args.save_prefix += net_name
net = get_model(net_name, pretrained_base=True, **kwargs)
if args.resume.strip():
net.load_parameters(args.resume.strip())
else:
for param in net.collect_params().values():
if param._data is not None:
continue
param.initialize()
net.reset_class(['table'])
print(ctx)
net.collect_params().reset_ctx(ctx)
# training data
train_dataset, val_dataset, eval_metric = get_dataset(args.dataset, args)
train_data, val_data = get_dataloader(
net, train_dataset, val_dataset, FasterRCNNDefaultTrainTransform,
FasterRCNNDefaultValTransform, args.batch_size, args.num_workers, args.use_fpn)
# training
train(net, train_data, val_data, eval_metric, ctx, args)
@jewelcai

This comment has been minimized.

Copy link
Owner Author

commented Jul 11, 2019

Traceback (most recent call last):
File "/Users/XXXX/PycharmProjects/MX_T/mxnet_models/train_faster_rcnn.py", line 492, in
FasterRCNNDefaultValTransform, args.batch_size, args.num_workers, args.use_fpn)
File "/Users/XXXX/PycharmProjects/MX_T/mxnet_models/train_faster_rcnn.py", line 221, in get_dataloader
train_transform(net.short, net.max_size, net, ashape=net.ashape, multi_stage=multi_stage)), batch_size,
File "/Users/XXXX/PycharmProjects/MX_T/venvde/lib/python3.5/site-packages/gluoncv/data/transforms/presets/rcnn.py", line 171, in init
mx.nd.zeros((1, 3, ashape, ashape))).reshape((1, 1, ashape, ashape, -1))
File "/Users/XXXX/PycharmProjects/MX_T/venvde/lib/python3.5/site-packages/mxnet/gluon/block.py", line 540, in call
out = self.forward(*args)
File "/Users/XXXX/PycharmProjects/MX_T/venvde/lib/python3.5/site-packages/mxnet/gluon/block.py", line 917, in forward
return self.hybrid_forward(ndarray, x, *args, **params)
File "/Users/XXXX/PycharmProjects/MX_T/venvde/lib/python3.5/site-packages/mxnet/gluon/nn/basic_layers.py", line 117, in hybrid_forward
x = block(x)
File "/Users/XXXX/PycharmProjects/MX_T/venvde/lib/python3.5/site-packages/mxnet/gluon/block.py", line 540, in call
out = self.forward(*args)
File "/Users/XXXX/PycharmProjects/MX_T/venvde/lib/python3.5/site-packages/mxnet/gluon/block.py", line 917, in forward
return self.hybrid_forward(ndarray, x, *args, **params)
File "/Users/XXXX/PycharmProjects/MX_T/venvde/lib/python3.5/site-packages/gluoncv/model_zoo/rpn/anchor.py", line 91, in hybrid_forward
a = F.slice_like(anchors, x * 0, axes=(2, 3))
File "", line 88, in slice_like
File "/Users/XXXX/PycharmProjects/MX_T/venvde/lib/python3.5/site-packages/mxnet/_ctypes/ndarray.py", line 92, in _imperative_invoke
ctypes.byref(out_stypes)))
File "/Users/XXXX/PycharmProjects/MX_T/venvde/lib/python3.5/site-packages/mxnet/base.py", line 252, in check_call
raise MXNetError(py_str(_LIB.MXGetLastError()))
mxnet.base.MXNetError: [22:27:09] src/operator/tensor/./matrix_op-inl.h:1286: Check failed: from_shape.ndim() > axis (3 vs. 3) Slice axis: 3 exceeds second input: 3

Stack trace returned 8 entries:
[bt] (0) 0 libmxnet.so 0x0000000119f3dc90 std::__1::__tree<std::__1::__value_type<std::__1::basic_string<char, std::__1::char_traits, std::__1::allocator >, mxnet::NDArrayFunctionReg*>, std::__1::__map_value_compare<std::__1::basic_string<char, std::__1::char_traits, std::__1::allocator >, std::__1::__value_type<std::__1::basic_string<char, std::__1::char_traits, std::__1::allocator >, mxnet::NDArrayFunctionReg*>, std::__1::less<std::__1::basic_string<char, std::__1::char_traits, std::__1::allocator > >, true>, std::__1::allocator<std::__1::__value_type<std::__1::basic_string<char, std::__1::char_traits, std::__1::allocator >, mxnet::NDArrayFunctionReg*> > >::destroy(std::__1::__tree_node<std::__1::__value_type<std::__1::basic_string<char, std::__1::char_traits, std::__1::allocator >, mxnet::NDArrayFunctionReg*>, void*>) + 2736
[bt] (1) 1 libmxnet.so 0x0000000119f3da3f std::__1::__tree<std::__1::__value_type<std::__1::basic_string<char, std::__1::char_traits, std::__1::allocator >, mxnet::NDArrayFunctionReg
>, std::__1::__map_value_compare<std::__1::basic_string<char, std::__1::char_traits, std::__1::allocator >, std::__1::__value_type<std::__1::basic_string<char, std::__1::char_traits, std::__1::allocator >, mxnet::NDArrayFunctionReg*>, std::__1::less<std::__1::basic_string<char, std::__1::char_traits, std::__1::allocator > >, true>, std::__1::allocator<std::__1::__value_type<std::__1::basic_string<char, std::__1::char_traits, std::__1::allocator >, mxnet::NDArrayFunctionReg*> > >::destroy(std::__1::__tree_node<std::__1::__value_type<std::__1::basic_string<char, std::__1::char_traits, std::__1::allocator >, mxnet::NDArrayFunctionReg*>, void*>) + 2143
[bt] (2) 2 libmxnet.so 0x000000011b3649a4 void mxnet::op::SliceExmshadow::cpu(nnvm::NodeAttrs const&, mxnet::OpContext const&, std::__1::vector<mxnet::NDArray, std::__1::allocatormxnet::NDArray > const&, std::__1::vector<mxnet::OpReqType, std::__1::allocatormxnet::OpReqType > const&, std::__1::vector<mxnet::NDArray, std::__1::allocatormxnet::NDArray > const&) + 179220
[bt] (3) 3 libmxnet.so 0x000000011b5d85a9 mxnet::imperative::SetShapeType(mxnet::Context const&, nnvm::NodeAttrs const&, std::__1::vector<mxnet::NDArray
, std::__1::allocatormxnet::NDArray* > const&, std::__1::vector<mxnet::NDArray*, std::__1::allocatormxnet::NDArray* > const&, mxnet::DispatchMode*) + 1577
[bt] (4) 4 libmxnet.so 0x000000011b5d6f06 mxnet::Imperative::Invoke(mxnet::Context const&, nnvm::NodeAttrs const&, std::__1::vector<mxnet::NDArray*, std::__1::allocatormxnet::NDArray* > const&, std::__1::vector<mxnet::NDArray*, std::__1::allocatormxnet::NDArray* > const&) + 742
[bt] (5) 5 libmxnet.so 0x000000011b522d9e SetNDInputsOutputs(nnvm::Op const*, std::__1::vector<mxnet::NDArray*, std::__1::allocatormxnet::NDArray* >, std::__1::vector<mxnet::NDArray, std::__1::allocatormxnet::NDArray* >, int, void const*, int*, int, int, void***) + 1774
[bt] (6) 6 libmxnet.so 0x000000011b523ac0 MXImperativeInvokeEx + 176
[bt] (7) 7 _ctypes.cpython-35m-darwin.so 0x0000000118cba577 ffi_call_unix64 + 79

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.