def validate_epoch(dataloader, model, criterion, epoch, classLabels, validClasses, void=-1, maskColors=None, flip=False, deg=None): batch_time = AverageMeter('Time', ':6.3f') data_time = AverageMeter('Data', ':6.3f') loss_running = AverageMeter('Loss', ':.4e') acc_running = AverageMeter('Accuracy', ':.4e') iou = iouCalc(classLabels, validClasses, voidClass=void) progress = ProgressMeter( len(dataloader), [batch_time, data_time, loss_running, acc_running], prefix="Test, epoch: [{}]".format(epoch)) # input resolution res = test_size[0] * test_size[1] all_predictions = torch.zeros( (200, 20, test_size[0], test_size[1])).float().cuda() all_labels = torch.zeros((200, test_size[0], test_size[1])).long().cuda() # Set model in evaluation mode model.eval() # TODO ADD PLATO SCHEDULAR INSPECT LOSSES all_filepaths = [] with torch.no_grad(): end = time.time() for epoch_step, (inputs, labels, filepath) in enumerate(dataloader): filepath = filepath[0].split('/')[-1] data_time.update(time.time() - end) inputs = inputs.float().cuda() labels = labels.long().cuda() if flip: idx = [i for i in range(inputs.shape[3] - 1, -1, -1)] idx = torch.LongTensor(idx) inputs = inputs[:, :, :, idx] # forward outputs = model(inputs) if flip: idx = [i for i in range(1024 - 1, -1, -1)] idx = torch.LongTensor(idx) outputs = outputs[:, :, :, idx] all_predictions[epoch_step, :, :, :] = F.softmax(outputs, 1) all_labels[epoch_step, :, :] = labels preds = torch.argmax(outputs, 1) loss = criterion(outputs, labels) # Statistics bs = inputs.size(0) # current batch size loss = loss.item() loss_running.update(loss, bs) corrects = torch.sum(preds == labels.data) nvoid = int((labels == void).sum()) acc = corrects.double() / ( bs * res - nvoid) # correct/(batch_size*resolution-voids) acc_running.update(acc, bs) # Calculate IoU scores of current batch iou.evaluateBatch(preds, labels) # measure elapsed time batch_time.update(time.time() - end) end = time.time() # print progress info progress.display(epoch_step) all_filepaths.append(filepath) miou = iou.outputScores() print('Accuracy : {:5.3f}'.format(acc_running.avg)) print('---------------------') return acc_running.avg, loss_running.avg, miou, all_predictions, all_labels, all_filepaths
def validate_epoch(dataloader, model, criterion, epoch, classLabels, validClasses, void=-1, maskColors=None): batch_time = AverageMeter('Time', ':6.3f') data_time = AverageMeter('Data', ':6.3f') loss_running = AverageMeter('Loss', ':.4e') acc_running = AverageMeter('Accuracy', ':.4e') iou = iouCalc(classLabels, validClasses, voidClass=void) progress = ProgressMeter( len(dataloader), [batch_time, data_time, loss_running, acc_running], prefix="Test, epoch: [{}]".format(epoch)) # input resolution res = args.test_size[0] * args.test_size[1] # Set model in evaluation mode model.eval() with torch.no_grad(): end = time.time() for epoch_step, (inputs, labels, filepath) in enumerate(dataloader): data_time.update(time.time() - end) inputs = inputs.float().cuda() labels = labels.long().cuda() # forward outputs = model(inputs) preds = torch.argmax(outputs, 1) loss = criterion(outputs, labels) # Statistics bs = inputs.size(0) # current batch size loss = loss.item() loss_running.update(loss, bs) corrects = torch.sum(preds == labels.data) nvoid = int((labels == void).sum()) acc = corrects.double() / ( bs * res - nvoid) # correct/(batch_size*resolution-voids) acc_running.update(acc, bs) # Calculate IoU scores of current batch iou.evaluateBatch(preds, labels) # Save visualizations of first batch if epoch_step == 0 and maskColors is not None: for i in range(inputs.size(0)): filename = os.path.splitext(os.path.basename( filepath[i]))[0] # Only save inputs and labels once if epoch == 0: img = visim(inputs[i, :, :, :]) label = vislbl(labels[i, :, :], maskColors) if len(img.shape) == 3: cv2.imwrite( 'baseline_run/images/{}.png'.format(filename), img[:, :, ::-1]) else: cv2.imwrite( 'baseline_run/images/{}.png'.format(filename), img) cv2.imwrite( 'baseline_run/images/{}_gt.png'.format(filename), label[:, :, ::-1]) # Save predictions pred = vislbl(preds[i, :, :], maskColors) cv2.imwrite( 'baseline_run/images/{}_epoch_{}.png'.format( filename, epoch), pred[:, :, ::-1]) # measure elapsed time batch_time.update(time.time() - end) end = time.time() # print progress info progress.display(epoch_step) miou = iou.outputScores() print('Accuracy : {:5.3f}'.format(acc_running.avg)) print('---------------------') return acc_running.avg, loss_running.avg, miou
criterion, 1, MiniCity.classLabels, MiniCity.validClasses, void=MiniCity.voidClass, maskColors=MiniCity.mask_colors, flip=False, deg=None) image_predictions += all_predictions.cpu() image_labels = all_labels.cpu() preds = torch.argmax(image_predictions, 1) iou = iouCalc(MiniCity.classLabels, MiniCity.validClasses, voidClass=MiniCity.voidClass) iou.evaluateBatch(preds, image_labels) miou = iou.outputScores() for i in range(preds.shape[0]): selected_filepath = all_filepaths[i] selected_preds = preds[i, :, :] pred_id = MiniCity.trainid2id[selected_preds] pred_id = Image.fromarray(pred_id) pred_id = pred_id.resize((2048, 1024), resample=Image.NEAREST) pred_id.save('results/' + selected_filepath)