MCPcopy Create free account
hub / github.com/DingXiaoH/RepLKNet-pytorch / evaluate

Function evaluate

engine.py:139–180  ·  view source on GitHub ↗
(data_loader, model, device, use_amp=False)

Source from the content-addressed store, hash-verified

137
138@torch.no_grad()
139def evaluate(data_loader, model, device, use_amp=False):
140 criterion = torch.nn.CrossEntropyLoss()
141
142 metric_logger = utils.MetricLogger(delimiter=" ")
143 header = 'Test:'
144
145 # switch to evaluation mode
146 model.eval()
147 i = 0
148 for batch in metric_logger.log_every(data_loader, 10, header):
149 i += 1
150 images = batch[0]
151 target = batch[-1]
152
153 images = images.to(device, non_blocking=True)
154 target = target.to(device, non_blocking=True)
155
156 # compute output
157 if use_amp:
158 with torch.cuda.amp.autocast():
159 output = model(images)
160 if type(output) is dict:
161 output = output['main']
162 loss = criterion(output, target)
163 else:
164 output = model(images)
165 if type(output) is dict:
166 output = output['main']
167 loss = criterion(output, target)
168
169 acc1, acc5 = accuracy(output, target, topk=(1, 5))
170
171 batch_size = images.shape[0]
172 metric_logger.update(loss=loss.item())
173 metric_logger.meters['acc1'].update(acc1.item(), n=batch_size)
174 metric_logger.meters['acc5'].update(acc5.item(), n=batch_size)
175 # gather the stats from all processes
176 metric_logger.synchronize_between_processes()
177 print('* Acc@1 {top1.global_avg:.3f} Acc@5 {top5.global_avg:.3f} loss {losses.global_avg:.3f}'
178 .format(top1=metric_logger.acc1, top5=metric_logger.acc5, losses=metric_logger.loss))
179
180 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