def evaluate(data_loader): meter = Meter('eval', 0) model.eval() total_loss = 0 with torch.no_grad(): for idx, (img, segm) in enumerate(data_loader): img = img.cuda() segm = segm.cuda() outputs = model(img) loss = criterion(outputs, segm) outputs = outputs.detach().cpu() segm = segm.detach().cpu() meter.update(segm, outputs) total_loss += loss.item() dices, iou = meter.get_metrics() dice, dice_neg, dice_pos = dices torch.cuda.empty_cache() return total_loss/len(data_loader), iou, dice, dice_neg, dice_pos
alpha = 0.5 if (TARGET == "kitti") and (orig_image.shape[:2] != pic.shape[:2]): orig_image = cv2.resize( orig_image, (DATASET["orig_size"][1], DATASET["orig_size"][0]), cv2.INTER_LANCZOS4) pic = cv2.addWeighted(orig_image, (1 - alpha), pic, alpha, 0) pred_name = "_".join( re.split("\.|_", image_id[0].split("/")[-1])[:-1]) + "_predicted_mask.png" cv2.imwrite(os.path.join(images_path, pred_name), pic) torch.cuda.empty_cache() if not EVAL["test_mode"]: dices, iou = meter.get_metrics("val") print("***** Prediction done in {} sec.; IoU: {}, Dice: {} ***** \n(total elapsed time: {} sec.) ".\ format(int(time.time()-start), iou, dices[0]["dice_all"], int(time.time()-global_start))) if TARGET == "cityscapes" and len(dices[0]) > 1: labels_df = image_dataset.label_encoder.cityscapes_labels_df if DATASET["train_on_cats"]: cat, name = "catId", "category" else: cat, name = "trainId", "name" print("***** Class metrics: *****") for k, v in dices[0].items(): if k != "dice_all": print(labels_df[labels_df[cat] == int(k)][name].iloc[0], " : ", v) else: print("***** Prediction on test set done in {} sec. ***** \n(total elapsed time: {} sec.) ".\