def eval_batch(model, image, target, weather, timeofday, scene): weather = weather.cuda() timeofday = timeofday.cuda() outputs, weather_o, timeofday_o = model(image) # Gathers tensors from different GPUs on a specified device # outputs = gather(outputs, 0, dim=0) pred = outputs[0] target = target.cuda() b, _, h, w = weather_o.size() weather_t = torch.ones((b, h, w)).long().cuda() for bi in range(b): weather_t[bi] *= weather[bi] timeofday_t = torch.ones((b, h, w)).long().cuda() for bi in range(b): timeofday_t[bi] *= timeofday[bi] correct, labeled = utils.batch_pix_accuracy(pred.data, target) inter, union = utils.batch_intersection_union( pred.data, target, self.nclass) correct_weather, labeled_weather = utils.batch_pix_accuracy( weather_o.data, weather_t) correct_timeofday, labeled_timeofday = utils.batch_pix_accuracy( timeofday_o.data, timeofday_t) return correct, labeled, inter, union, correct_weather, labeled_weather, correct_timeofday, labeled_timeofday
def eval_batch(image, dst, evaluator, eval_mode): if eval_mode: # evaluation mode on validation set targets = dst outputs = evaluator.parallel_forward(image) batch_inter, batch_union, batch_correct, batch_label = 0, 0, 0, 0 for output, target in zip(outputs, targets): correct, labeled = utils.batch_pix_accuracy( output.data.cpu(), target) inter, union = utils.batch_intersection_union( output.data.cpu(), target, testset.num_class) batch_correct += correct batch_label += labeled batch_inter += inter batch_union += union return batch_correct, batch_label, batch_inter, batch_union else: # Visualize and dump the results im_paths = dst outputs = evaluator.parallel_forward(image) predicts = [ torch.max(output, 1)[1].cpu().numpy() + testset.pred_offset for output in outputs ] for predict, impath in zip(predicts, im_paths): mask = utils.get_mask_pallete(predict, args.dataset) outname = os.path.splitext(impath)[0] + '.png' mask.save(os.path.join(outdir, outname)) # dummy outputs for compatible with eval mode return 0, 0, 0, 0
def eval_batch(model, image, target): outputs = model(image) outputs = gather(outputs, 0, dim=0) pred = outputs[0] target = target.cuda() correct, labeled = utils.batch_pix_accuracy(pred.data, target) inter, union = utils.batch_intersection_union(pred.data, target, self.nclass) return correct, labeled, inter, union
def evaluate(self, x, target=None): pred = self.forward(x) if isinstance(pred, (tuple, list)): pred = pred[0] if target is None: return pred correct, labeled = batch_pix_accuracy(pred.data, target.data) inter, union = batch_intersection_union(pred.data, target.data, self.nclass) return correct, labeled, inter, union
def eval_batch(model, image, target): #image = image.cuda(non_blocking=True) #target = target.cuda(non_blocking=True) image = image.to(self.device) target = target.to(self.device) _, outputs = model(image, target) #outputs = model(image) #correct, labeled = utils.batch_pix_accuracy(pred.data, target) #inter, union = utils.batch_intersection_union(pred.data, target, self.nclass) correct, labeled = utils.batch_pix_accuracy(outputs, target) inter, union = utils.batch_intersection_union( outputs, target, self.nclass) return correct, labeled, inter, union
def eval_batch(image, dst, evaluator, eval_mode, hist, names): if eval_mode: # evaluation mode on validation set targets = dst outputs = evaluator.parallel_forward(image) batch_inter, batch_union, batch_correct, batch_label = 0, 0, 0, 0 for output, target, name in zip(outputs, targets, names): correct, labeled = utils.batch_pix_accuracy( output.data.cpu(), target) inter, union = utils.batch_intersection_union( output.data.cpu(), target, testset.num_class) batch_correct += correct batch_label += labeled batch_inter += inter batch_union += union a = target.numpy().flatten() b = output.data.cpu() _, b = torch.max(b, 1) b = b.numpy().flatten() n = testset.num_class k = (a >= 0) & (a < n) hist += np.bincount(n * a[k].astype(int) + b[k], minlength=n**2).reshape(n, n) output = output.data.cpu().numpy()[0] output = output.transpose(1, 2, 0) output = np.asarray(np.argmax(output, axis=2), dtype=np.uint8) output_col = colorize_mask(output) output = Image.fromarray(output) name = name.split('/')[-1] output.save('%s/%s' % (args.save, name)) output_col.save('%s/%s_color.png' % (args.save, name.split('.')[0])) return batch_correct, batch_label, batch_inter, batch_union, hist else: # test mode, dump the results im_paths = dst outputs = evaluator.parallel_forward(image) predicts = [ torch.max(output, 1)[1].cpu().numpy() + testset.pred_offset for output in outputs ] for predict, impath in zip(predicts, im_paths): mask = utils.get_mask_pallete(predict, args.dataset) outname = os.path.splitext(impath)[0] + '.png' mask.save(os.path.join(outdir, outname)) # dummy outputs for compatible with eval mode return 0, 0, 0, 0
def eval_batch(model, image, target, weather, timeofday, scene): outputs, weather_o, timeofday_o = model(image) # Gathers tensors from different GPUs on a specified device # outputs = gather(outputs, 0, dim=0) pred = outputs[0] b, _, h, w = weather_o.size() weather_t = torch.ones((b, h, w)).long() for bi in range(b): weather_t[bi] *= weather[bi] timeofday_t = torch.ones((b, h, w)).long() for bi in range(b): timeofday_t[bi] *= timeofday[bi] self.confusion_matrix_weather.update([ m.astype(np.int64) for m in weather_t.numpy() ], weather_o.cpu().numpy().argmax(1)) self.confusion_matrix_timeofday.update([ m.astype(np.int64) for m in timeofday_t.numpy() ], timeofday_o.cpu().numpy().argmax(1)) correct, labeled = utils.batch_pix_accuracy(pred.data, target) inter, union = utils.batch_intersection_union(pred.data, target, self.nclass) correct_weather, labeled_weather = utils.batch_pix_accuracy(weather_o.data, weather_t) correct_timeofday, labeled_timeofday = utils.batch_pix_accuracy(timeofday_o.data, timeofday_t) return correct, labeled, inter, union, correct_weather, labeled_weather, correct_timeofday, labeled_timeofday