def test_smoothL1_random_derivative(backend_default, fargs): s1, s2, m = fargs shape = (s1, s2) magnitude = m outputs = (np.random.random(shape) - 0.5) * magnitude targets = np.random.random(shape) x = outputs - targets expected_result = np.zeros(shape) I1, J1 = np.where(abs(x) < 1) I2, J2 = np.where(abs(x) >= 1) expected_result[I1, J1] = x[I1, J1] expected_result[I2, J2] = np.sign(x[I2, J2]) compare_tensors(SmoothL1Loss(), outputs, targets, expected_result, deriv=True, tol=1e-5)
def test_smoothL1_ones(backend_default, fargs): s1, s2, m = fargs shape = (s1, s2) outputs = np.ones(shape) * m targets = np.ones(shape) * m x = outputs - targets expected_result = np.zeros(shape) I1, J1 = np.where(abs(x) < 1) I2, J2 = np.where(abs(x) >= 1) expected_result[I1, J1] = 0.5 * x[I1, J1]**2 expected_result[I2, J2] = abs(x[I2, J2]) - 0.5 expected_result = np.sum(expected_result, axis=0, keepdims=True) compare_tensors(SmoothL1Loss(), outputs, targets, expected_result, deriv=False, tol=1e-5)
def test_smoothL1_zeros(backend_default, fargs): s1, s2, m, sigma = fargs sigma2 = sigma**2 shape = (s1, s2) outputs = np.zeros(shape) targets = np.zeros(shape) x = outputs - targets expected_result = np.zeros(shape) I1, J1 = np.where(abs(x) < 1.0 / sigma2) I2, J2 = np.where(abs(x) >= 1.0 / sigma2) expected_result[I1, J1] = 0.5 * x[I1, J1]**2 * sigma2 expected_result[I2, J2] = abs(x[I2, J2]) - 0.5 / sigma2 expected_result = np.sum(expected_result, axis=0, keepdims=True) compare_tensors(SmoothL1Loss(sigma=sigma), outputs, targets, expected_result, deriv=False, tol=1e-5)
model = create_frcn_model(frcn_fine_tune) # setup optimizer opt_w = GradientDescentMomentum( 0.001 * learning_rate_scale, 0.9, wdecay=0.0005) opt_b = GradientDescentMomentum(0.002 * learning_rate_scale, 0.9) optimizer = MultiOptimizer({'default': opt_w, 'Bias': opt_b}) # if training a new model, seed the image model conv layers with pre-trained weights # otherwise, just load the model file if args.model_file is None: load_vgg_weights(model, args.data_dir) cost = Multicost(costs=[GeneralizedCostMask(costfunc=CrossEntropyMulti()), GeneralizedCostMask(costfunc=SmoothL1Loss())], weights=[1, 1]) callbacks = Callbacks(model, eval_set=test_set, **args.callback_args) model.fit(train_set, optimizer=optimizer, num_epochs=num_epochs, cost=cost, callbacks=callbacks) # Fast R-CNN model requires scale the bbox regression branch linear layer weights # before saving the model model = scale_bbreg_weights( model, train_set.bbtarget_means, train_set.bbtarget_stds) save_obj(model.serialize(keep_states=True), args.save_path) neon_logger.display('running eval...')
rois_per_img=rpn_rois_per_img, inference=False) config['subset_fraction'] = float(args.subset_pct / 100.0) train_set = faster_rcnn.build_dataloader(config, frcn_rois_per_img) # build the Faster-RCNN model model = faster_rcnn.build_model(train_set, frcn_rois_per_img, inference=False) # set up cost different branches, respectively weights = 1.0 / (rpn_rois_per_img) roi_w = 1.0 / (frcn_rois_per_img) frcn_tree_cost = Multicost(costs=[ GeneralizedCostMask(costfunc=CrossEntropyMulti(), weights=roi_w), GeneralizedCostMask(costfunc=SmoothL1Loss(), weights=roi_w) ], weights=[1, 1]) cost = Multicost(costs=[ GeneralizedCostMask(costfunc=CrossEntropyMulti(), weights=weights), GeneralizedCostMask(costfunc=SmoothL1Loss(sigma=3.0), weights=weights), frcn_tree_cost, ], weights=[1, 1, 1]) # setup optimizer schedule_w = StepSchedule(step_config=[10], change=[0.001 / 10]) schedule_b = StepSchedule(step_config=[10], change=[0.002 / 10]) opt_w = GradientDescentMomentum(0.001, 0.9, wdecay=0.0005, schedule=schedule_w)