MCPcopy Create free account
hub / github.com/GeWu-Lab/AnyTouch2 / evaluate

Function evaluate

train/probe_objbench_engine.py:93–144  ·  view source on GitHub ↗
(data_loader, model, device, args)

Source from the content-addressed store, hash-verified

91
92@torch.no_grad()
93def evaluate(data_loader, model, device, args):
94 if args.dataset in ['material', 'cloth']:
95 criterion = torch.nn.CrossEntropyLoss()
96 else:
97 criterion = torch.nn.BCEWithLogitsLoss()
98 sigmoid = torch.nn.Sigmoid()
99
100 metric_logger = misc.MetricLogger(delimiter=" ")
101 header = 'Test:'
102
103 # switch to evaluation mode
104 model.eval()
105
106 for batch in metric_logger.log_every(data_loader, 40, header):
107 images = batch[0]
108 sensors = batch[1]
109 target = batch[-1]
110 images = images.to(device, non_blocking=True)
111 sensors = sensors.to(device, non_blocking=True).int()
112 target = target.to(device, non_blocking=True)
113
114 if args.use_universal:
115 sensors = torch.ones_like(sensors) * -1
116 sensors = sensors.int()
117 # compute output
118 with torch.amp.autocast('cuda'):
119 output = model(images, sensor_type = sensors)
120 if args.dataset in ['rough', 'hard']:
121 output = output.squeeze(1)
122 target = target.float()
123 loss = criterion(output, target)
124
125 if args.dataset in ['material', 'cloth']:
126 acc1, acc5 = accuracy(output, target, topk=(1,5))
127 else:
128 output = sigmoid(output)
129 predictions = (output > 0.5).float()
130 correct_predictions = (predictions == target).sum().item()
131 acc1 = correct_predictions / target.size(0) * 100.0
132
133 batch_size = images.shape[0]
134 metric_logger.update(loss=loss.item())
135 if args.dataset in ['material']:
136 metric_logger.meters['acc1'].update(acc1.item(), n=batch_size)
137 else:
138 metric_logger.meters['acc1'].update(acc1, n=batch_size)
139
140 metric_logger.synchronize_between_processes()
141 print('* Acc@1 {top1.global_avg:.3f} loss {losses.global_avg:.3f}'
142 .format(top1=metric_logger.acc1, losses=metric_logger.loss))
143
144 return {k: meter.global_avg for k, meter in metric_logger.meters.items()}

Callers 1

mainFunction · 0.90

Calls 4

log_everyMethod · 0.95
updateMethod · 0.95
printFunction · 0.85

Tested by

no test coverage detected