simpledet
simpledet copied to clipboard
How to draw detected bbox on test images?
Hi, I try to detection one image,I debug the following code, but meet the Error,my command is
python simpledet-test1.py --config config/resnet_v1b/tridentnet_r50v1bc4_c5_1x.py --inputs data/nanchang_20180209_JPEGImages_58215.jpg
mxnet.base.MXNetError: [08:59:39] src/operator/nn/./../tensor/../elemwise_op_common.h:135: Check failed: assign(&dattr, vec.at(i)): Incompatible attr in node at 0-th output: expected [1,64,1,1], got [64]
How can i deal with it?
code: import os, argparse import importlib import json import time import cv2 import numpy as np import mxnet as mx from core.detection_module import DetModule from utils.load_model import load_checkpoint
coco = ( 'person', 'bicycle', 'car', 'tricycle')
class TDNDetector: def init(self, configFn, ctx, outFolder, threshold): os.environ["MXNET_CUDNN_AUTOTUNE_DEFAULT"] = "0" config = importlib.import_module(configFn.replace('.py', '').replace('/', '.')) ,,,,,, self._pModel,, self.__pTest,,,, = config.get_config(is_train=False) if callable(self.__pTest.nms.type): self.__nms = self.__pTest.nms.type(self.__pTest.nms.thr) else: from operator_py.nms import py_nms_wrapper self.__nms = py_nms_wrapper(self.__pTest.nms.thr) arg_params, aux_params = load_checkpoint(self.__pTest.model.prefix, self.__pTest.model.epoch) sym = self.__pModel.test_symbol self.__mod = DetModule(sym, data_names=['data','im_info','im_id','rec_id'], context=ctx) self.__mod.bind(data_shapes=[('data', (1, 3, 600, 899)), ('im_info', (1, 3)), ('im_id', (1,)), ('rec_id', (1,))], for_training=False) self.__mod.set_params(arg_params, aux_params, allow_extra=False) self.__saveSymbol(sym, outFolder, self.__pTest.model.prefix.split('/')[-1]) self.__threshold = threshold
def __call__(self, imgFilename): # detect onto image
img, im_data, scale = self.__readImg(imgFilename)
if img is None: return None, None
h, w = im_data.shape[-2:]
im_info, im_id, rec_id = [(h, w, scale)], [1], [1]
data = mx.io.DataBatch(data=[mx.nd.array(im_data),
mx.nd.array(im_info),
mx.nd.array(im_id),
mx.nd.array(rec_id)])
self.__mod.forward(data, is_train=False)
# extract results
outputs = self.__mod.get_outputs(merge_multi_context=False)
rid, id, info, cls, box = [x[0].asnumpy() for x in outputs]
rid, id, info, cls, box = rid.squeeze(), id.squeeze(), info.squeeze(), cls.squeeze(), box.squeeze()
cls = cls[:, 1:] # remove background
box = box / scale
output_record = dict(rec_id=rid, im_id=id, im_info=info, bbox_xyxy=box, cls_score=cls)
output_record = self.__pTest.process_output([output_record], None)[0]
final_result = self.__do_nms(output_record)
# obtain representable output
detections = []
for cid ,bbox in final_result.items():
idx = np.where(bbox[:,-1] > self.__threshold)[0]
for i in idx:
final_box = bbox[i][:4]
score = bbox[i][-1]
detections.append({'cls':cid, 'box':final_box, 'score':score})
return detections, img
def __do_nms(self, all_output):
box = all_output['bbox_xyxy']
score = all_output['cls_score']
final_dets = {}
for cid in range(score.shape[1]):
score_cls = score[:, cid]
valid_inds = np.where(score_cls > self.__threshold)[0]
box_cls = box[valid_inds]
score_cls = score_cls[valid_inds]
if valid_inds.shape[0]==0:
continue
det = np.concatenate((box_cls, score_cls.reshape(-1, 1)), axis=1).astype(np.float32)
det = self.__nms(det)
cls = coco[cid]
final_dets[cls] = det
return final_dets
def __readImg(self, imgFilename):
image_ori = cv2.imread(imgFilename, cv2.IMREAD_COLOR)
if image_ori is None: return None, None, None
#BGR2RGB
image = image_ori[:, :, ::-1]
resizeParam = (800, 2000)
shorts, longs = min(image.shape[:2]), max(image.shape[:2])
scale = min(resizeParam[0] / shorts, resizeParam[1] / longs)
image = cv2.resize(image, None, None, scale, scale, interpolation=cv2.INTER_LINEAR)
#HWC2CHW
image = image.transpose((2, 0,1))
image = np.expand_dims(image, axis=0)
return image_ori, image, scale
def __saveSymbol(self, sym, outFolder, fnPrefix):
if not os.path.exists(outFolder): os.makedirs(outFolder)
resFilename = os.path.join(outFolder, fnPrefix + "_symbol_test.json")
sym.save(resFilename)
def parse_args(): parser = argparse.ArgumentParser(description='Test Detection') parser.add_argument('--config', type=str, required=True, help='config file path') parser.add_argument('--ctx', type=int, default=0, help='GPU index. Set negative value to use CPU') parser.add_argument('--inputs', type=str, nargs='+', required=True, help='File(-s) to test') parser.add_argument('--output', type=str, default='results', help='Where to store results') parser.add_argument('--threshold', type=float, default=0.5, help='Detector threshold') return parser.parse_args()
if name == "main":
args = parse_args()
ctx = mx.gpu(args.ctx) if args.ctx>=0 else args.cpu()
imgFilenames = args.inputs
detector = TDNDetector(args.config, ctx, args.output, args.threshold)
for i, imgFilename in enumerate(imgFilenames):
dets, img = detector(imgFilename)
@xchani
Got exactly the same exception:
mxnet.base.MXNetError: [08:59:39] src/operator/nn/./../tensor/../elemwise_op_common.h:135: Check failed: assign(&dattr, vec.at(i)): Incompatible attr in node at 0-th output: expected [1,64,1,1], got [64]
While just running rpn_test.py on trained (MSCOCO) tridentnet_r101v2c4_c5_2x checkpoint. detection_train and detection_test work fine.
Bug confirmed. It seems we forget to add graph optimization for rpn_test.
https://github.com/TuSimple/simpledet/blob/78467b7233d33d09b692ee4ca5fb3cbe46b85ee5/mask_test.py#L95-L98
@dl19940602 Hi, how to test with multi-sacle on one image?
他的代码不能用,骗人的