def validate(net, device, val_data, batches_per_epoch): """ Run validation. :param net: Network :param device: Torch device :param val_data: Validation Dataset :param batches_per_epoch: Number of batches to run :return: Successes, Failures and Losses """ net.eval() results = { 'correct': 0, 'failed': 0, 'loss': 0, 'losses': { } } ld = len(val_data) with torch.no_grad(): batch_idx = 0 while batch_idx < batches_per_epoch: for x, y, didx, rot, zoom_factor in val_data: batch_idx += 1 if batches_per_epoch is not None and batch_idx >= batches_per_epoch: break xc = x.to(device) yc = [yy.to(device) for yy in y] lossd = net.compute_loss(xc, yc) loss = lossd['loss'] results['loss'] += loss.item()/ld for ln, l in lossd['losses'].items(): if ln not in results['losses']: results['losses'][ln] = 0 results['losses'][ln] += l.item()/ld q_out, ang_out, w_out = post_process_output(lossd['pred']['pos'], lossd['pred']['cos'], lossd['pred']['sin'], lossd['pred']['width']) s = evaluation.calculate_iou_match(q_out, ang_out, val_data.dataset.get_gtbb(didx, rot, zoom_factor), no_grasps=1, grasp_width=w_out, ) if s: results['correct'] += 1 else: results['failed'] += 1 return results
results = {'correct': 0, 'failed': 0} if args.jacquard_output: jo_fn = args.network + '_jacquard_output.txt' with open(jo_fn, 'w') as f: pass with torch.no_grad(): for idx, (x, y, didx, rot, zoom) in enumerate(test_data): logging.info('Processing {}/{}'.format(idx + 1, len(test_data))) xc = x.to(device) yc = [yi.to(device) for yi in y] lossd = net.compute_loss(xc, yc) q_img, ang_img, width_img = post_process_output( lossd['pred']['pos'], lossd['pred']['cos'], lossd['pred']['sin'], lossd['pred']['width']) if args.iou_eval: s = evaluation.calculate_iou_match( q_img, ang_img, test_data.dataset.get_gtbb(didx, rot, zoom), no_grasps=args.n_grasps, grasp_width=width_img, ) if s: results['correct'] += 1 else: results['failed'] += 1
if args.jacquard_output: jo_fn = args.network + '_jacquard_output.txt' with open(jo_fn, 'w') as f: pass with torch.no_grad(): if args.dataset == "my" or args.dataset == "my2" or args.dataset == "my_crop": for idx, (x, didx, rot, zoom) in enumerate(test_data): logging.info('Processing {}/{}'.format(idx + 1, len(test_data))) xc = x.to(device) #data print('shape of xc:', xc.shape) # Approach 2: alternative to "lossd = net.compute_loss(xc, yc)" _pos_output, _cos_output, _sin_output, _width_output = net.forward( xc) # only x(data) is used tq_img, tang_img, twidth_img = post_process_output( _pos_output, _cos_output, _sin_output, _width_output) if args.vis: evaluation.plot_output( test_data.dataset.get_rgb(didx, rot, zoom, normalise=False), test_data.dataset.get_depth(didx, rot, zoom), tq_img, tang_img, no_grasps=args.n_grasps, grasp_width_img=twidth_img) else: for idx, (x, y, didx, rot, zoom) in enumerate(test_data): # data(x), label(y)=(pos, cos, sin, width), idx, rot, zoom_factor logging.info('Processing {}/{}'.format(idx + 1,