def main(): '''Main. ''' # Parse arguments args = get_args() # Create YOLOv2 detection network x = nn.Variable((1, 3, args.width, args.width)) y = yolov2.yolov2(x, args.anchors, args.classes, test=True) params = nn.get_parameters(grad_only=False) # Parse network parameters dn_weights = parser.load_weights_raw(args.input) cursor = 0 for i in range(1, 19): # 1 to 18 cursor = parser.load_convolutional_and_get_next_cursor( dn_weights, cursor, params, 'c{}'.format(i)) cursor = parser.load_convolutional_and_get_next_cursor( dn_weights, cursor, params, 'c18_19') cursor = parser.load_convolutional_and_get_next_cursor( dn_weights, cursor, params, 'c18_20') cursor = parser.load_convolutional_and_get_next_cursor( dn_weights, cursor, params, 'c13_14') cursor = parser.load_convolutional_and_get_next_cursor( dn_weights, cursor, params, 'c21') cursor = parser.load_convolutional_and_get_next_cursor(dn_weights, cursor, params, 'detection', no_bn=True) assert cursor == dn_weights.size # Save to a h5 file nn.save_parameters(args.output)
def main(): args = get_args() # Load parameter _ = nn.load_parameters(args.weights) # Build a YOLO v2 network x = nn.Variable((1, 3, args.height, args.width)) y = yolov2.yolov2(x / 255.0, args.num_anchors, args.classes, test=True) y = yolov2.yolov2_activate(y, args.num_anchors, args.anchors) y = F.nms_detection2d(y, args.thresh, args.nms, args.nms_per_class) # Save NNP file (used in C++ inference later.). runtime_contents = { 'networks': [{ 'name': 'runtime', 'batch_size': 1, 'outputs': { 'y': y }, 'names': { 'x': x } }], 'executors': [{ 'name': 'runtime', 'network': 'runtime', 'data': ['x'], 'output': ['y'] }] } import nnabla.utils.save nnabla.utils.save.save(args.nnp, runtime_contents, variable_batch_size=False)
def create_losses(batchsize, imheight, imwidth, test=True): import gc gc.collect() nnabla_ext.cuda.clear_memory_cache() anchors = args.num_anchors classes = args.num_classes yolo_x = nnabla.Variable((batchsize, 3, imheight, imwidth)) yolo_features = yolov2.yolov2(yolo_x, anchors, classes, test=test) return yolo_x, yolo_features
def create_network(batchsize, imheight, imwidth, args, seen): import gc gc.collect() nnabla_ext.cuda.clear_memory_cache() anchors = args.num_anchors classes = args.num_classes yolo_x = nn.Variable((batchsize, 3, imheight, imwidth)) target = nn.Variable((batchsize, 50 * 5)) yolo_features = yolov2.yolov2(yolo_x, anchors, classes, test=False) nB = yolo_features.shape[0] nA = args.num_anchors nC = args.num_classes nH = yolo_features.shape[2] nW = yolo_features.shape[3] # Bouding box regression loss # pred.shape = [nB, nA, 4, nH, nW] output = F.reshape(yolo_features, (nB, nA, (5 + nC), nH, nW)) xy = F.sigmoid(output[:, :, :2, ...]) wh = output[:, :, 2:4, ...] bbox_pred = F.concatenate(xy, wh, axis=2) conf_pred = F.sigmoid(output[:, :, 4:5, ...]) cls_pred = output[:, :, 5:, ...] region_loss_targets = RegionLossTargets(nC, args.anchors, seen, args.coord_scale, args.noobject_scale, args.object_scale, args.class_scale, args.thresh) tcoord, mcoord, tconf, mconf, tcls, mcls = region_loss_targets( bbox_pred, target) for v in tcoord, mcoord, tconf, mconf, tcls, mcls: v.need_grad = False # Bounding box regression bbox_loss = F.sum(F.squared_error(bbox_pred, tcoord) * mcoord) # Conf (IoU) regression loss conf_loss = F.sum(F.squared_error(conf_pred, tconf) * mconf) # Class probability regression loss cls_loss = F.sum(F.softmax_cross_entropy(cls_pred, tcls, axis=2) * mcls) # Note: # loss is devided by 2.0 due to the fact that the original darknet # code doesn't multiply the derivative of square functions by 2.0 # in region_layer.c. loss = (bbox_loss + conf_loss) / 2.0 + cls_loss return yolo_x, target, loss, region_loss_targets
def create_network(batchsize, imheight, imwidth, args): import gc gc.collect() nnabla_ext.cuda.clear_memory_cache() anchors = args.num_anchors classes = args.num_classes yolo_x = nn.Variable((batchsize, 3, imheight, imwidth)) yolo_features = yolov2.yolov2(yolo_x, anchors, classes, test=False) nB = yolo_features.shape[0] nA = args.num_anchors nC = args.num_classes nH = yolo_features.shape[2] nW = yolo_features.shape[3] output = yolo_features.get_unlinked_variable(need_grad=True) # TODO: Workaround until v1.0.2. # Explicitly enable grad since need_grad option above didn't work. output.need_grad = True output = F.reshape(output, (nB, nA, (5 + nC), nH, nW)) output_splitted = F.split(output, 2) x, y, w, h, conf = [v.reshape((nB, nA, nH, nW)) for v in output_splitted[0:5]] x, y, conf = map(F.sigmoid, [x, y, conf]) cls = F.stack(*output_splitted[5:], axis=2) cls = cls.reshape((nB*nA, nC, nH*nW)) cls = F.transpose(cls, [0, 2, 1]).reshape((nB*nA*nH*nW, nC)) tx, ty, tw, th, tconf, coord_mask, conf_mask_sq = [ nn.Variable(v.shape) for v in [x, y, w, h, conf, x, conf]] cls_ones, cls_mask = [nn.Variable(cls.shape) for _ in range(2)] tcls, cls_mask_bb = [nn.Variable((cls.shape[0], 1)) for _ in range(2)] coord_mask_sq = F.pow_scalar(coord_mask, 2) loss_x = args.coord_scale * F.sum(F.squared_error(x, tx) * coord_mask_sq) loss_y = args.coord_scale * F.sum(F.squared_error(y, ty) * coord_mask_sq) loss_w = args.coord_scale * F.sum(F.squared_error(w, tw) * coord_mask_sq) loss_h = args.coord_scale * F.sum(F.squared_error(h, th) * coord_mask_sq) loss_conf = F.sum(F.squared_error(conf, tconf) * conf_mask_sq) loss_cls = args.class_scale * \ F.sum(cls_mask_bb * F.softmax_cross_entropy(cls + cls_ones - cls_mask, tcls)) loss_nnabla = loss_x + loss_y + loss_w + loss_h + loss_conf + loss_cls return yolo_x, yolo_features, (x, y, w, h, conf, cls), (tx, ty, tw, th, tconf, coord_mask, conf_mask_sq, cls_ones, cls_mask, tcls, cls_mask_bb), loss_nnabla
def main(): args = get_args() names = np.genfromtxt(args.class_names, dtype=str, delimiter='?') rng = np.random.RandomState(1223) colors = rng.randint(0, 256, (args.classes, 3)).astype(np.uint8) colors = [tuple(c.tolist()) for c in colors] # Set context from nnabla.ext_utils import get_extension_context ctx = get_extension_context(args.context, device_id=args.device_id, type_config=args.type_config) nn.set_default_context(ctx) # Load parameter _ = nn.load_parameters(args.weights) # Build a YOLO v2 network feature_dict = {} x = nn.Variable((1, 3, args.width, args.width)) y = yolov2.yolov2(x, args.anchors, args.classes, test=True, feature_dict=feature_dict) y = yolov2.yolov2_activate(y, args.anchors, args.biases) y = F.nms_detection2d(y, args.thresh, args.nms, args.nms_per_class) # Read image img_orig = imread(args.input) im_h, im_w, _ = img_orig.shape # letterbox w = args.width h = args.width if (w * 1.0 / im_w) < (h * 1. / im_h): new_w = w new_h = int((im_h * w) / im_w) else: new_h = h new_w = int((im_w * h) / im_h) patch = imresize(img_orig, (new_h, new_w)) / 255. img = np.ones((h, w, 3), np.float32) * 0.5 # resize x0 = int((w - new_w) / 2) y0 = int((h - new_h) / 2) img[y0:y0 + new_h, x0:x0 + new_w] = patch # Execute YOLO v2 print("forward") in_img = img.transpose(2, 0, 1).reshape(1, 3, args.width, args.width) x.d = in_img y.forward(clear_buffer=True) print("done") bboxes = y.d[0] img_draw = draw_bounding_boxes(img_orig, bboxes, im_w, im_h, names, colors, new_w * 1.0 / w, new_h * 1.0 / h, args.thresh) imsave(args.output, img_draw) # Timing s = time.time() n_time = 10 for i in range(n_time): x.d = in_img y.forward(clear_buffer=True) # Invoking device-to-host copy if CUDA # so that time contains data transfer. _ = y.d print("Processing time: {:.1f} [ms/image]".format( (time.time() - s) / n_time * 1000))