def valid_isic(valid_loader, model, criterion, optimizer, args, epoch, minloss): val_losses = AverageMeter() val_isic_dice = AverageMeter() val_isic_jaccard = AverageMeter() model.eval() for step, (t, k) in tqdm(enumerate(valid_loader), total=len(valid_loader)): image = t.float().cuda() target = k.float().cuda() output = model(image) # model output output_dis = torch.max(output, 1)[1].unsqueeze(dim=1) output_soft = get_soft_label(output_dis, args.num_classes) target_soft = get_soft_label(target, args.num_classes) # get soft label val_loss = criterion(output, target_soft, args.num_classes) # the dice losses val_losses.update(val_loss.data, image.size(0)) isic = val_dice_isic(output_soft, target_soft, args.num_classes) # the dice score val_isic_dice.update(isic.data, image.size(0)) # SKY jaccard = jaccard_isic(output_soft, target_soft, args.num_classes) # the jaccard score val_isic_jaccard.update(jaccard.data, image.size(0)) if step % (math.ceil( float(len(valid_loader.dataset)) / args.batch_size)) == 0: print('Valid Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {losses.avg:.6f}'. format(epoch, step * len(image), len(valid_loader.dataset), 100. * step / len(valid_loader), losses=val_losses)) print('The ISIC Mean Average Dice score: {isic.avg: .4f}; ' 'The Average Loss score: {loss.avg: .4f}'.format(isic=val_isic_dice, loss=val_losses)) # SKY print('The ISIC Mean Average Jaccard score: {isic.avg: .4f}; ' 'The Average Loss score: {loss.avg: .4f}'.format( isic=val_isic_jaccard, loss=val_losses)) if val_losses.avg < min(minloss): minloss.append(val_losses.avg) print(minloss) modelname = args.ckpt + '/' + 'min_loss' + '_' + args.data + '_checkpoint.pth.tar' print('the best model will be saved at {}'.format(modelname)) state = { 'epoch': epoch, 'state_dict': model.state_dict(), 'opt_dict': optimizer.state_dict() } torch.save(state, modelname) return val_losses.avg, val_isic_dice.avg
def test_isic(test_loader, model): isic_dice = [] isic_iou = [] isic_assd = [] infer_time = [] model.eval() for step, (img, lab) in enumerate(test_loader): image = img.float().cuda() target = lab.float().cuda() # output, atten2_map, atten3_map = model(image) # model output begin_time = time() output = model(image) end_time = time() pred_time = end_time - begin_time infer_time.append(pred_time) output_dis = torch.max(output, 1)[1].unsqueeze(dim=1) output_soft = get_soft_label(output_dis, args.num_classes) target_soft = get_soft_label(target, args.num_classes) # get soft label # input_arr = np.squeeze(image.cpu().numpy()).astype(np.float32) label_arr = target_soft.cpu().numpy().astype(np.uint8) # label_shw = np.squeeze(target.cpu().numpy()).astype(np.uint8) output_arr = output_soft.cpu().byte().numpy().astype(np.uint8) isic_b_dice = val_dice_isic(output_soft, target_soft, args.num_classes) # the dice accuracy isic_b_iou = Intersection_over_Union_isic(output_soft, target_soft, args.num_classes) # the iou accuracy isic_b_asd = assd(output_arr[:, :, :, 1], label_arr[:, :, :, 1]) dice_np = isic_b_dice.data.cpu().numpy() iou_np = isic_b_iou.data.cpu().numpy() isic_dice.append(dice_np) isic_iou.append(iou_np) isic_assd.append(isic_b_asd) # df = pd.DataFrame(data=dice_np) # df.to_csv(args.ckpt + '/refine_result.csv') isic_dice_mean = np.average(isic_dice) isic_dice_std = np.std(isic_dice) isic_iou_mean = np.average(isic_iou) isic_iou_std = np.std(isic_iou) isic_assd_mean = np.average(isic_assd) isic_assd_std = np.std(isic_assd) all_time = np.sum(infer_time) print('The ISIC mean Accuracy: {isic_dice_mean: .4f}; The ISIC Accuracy std: {isic_dice_std: .4f}'.format( isic_dice_mean=isic_dice_mean, isic_dice_std=isic_dice_std)) print('The ISIC mean IoU: {isic_iou_mean: .4f}; The ISIC IoU std: {isic_iou_std: .4f}'.format( isic_iou_mean=isic_iou_mean, isic_iou_std=isic_iou_std)) print('The ISIC mean assd: {isic_asd_mean: .4f}; The ISIC assd std: {isic_asd_std: .4f}'.format( isic_asd_mean=isic_assd_mean, isic_asd_std=isic_assd_std)) print('The inference time: {time: .4f}'.format(time=all_time))
def train(train_loader, model, criterion, optimizer, args, epoch): losses = AverageMeter() model.train() for step, (x, y) in tqdm(enumerate(train_loader), total=len(train_loader)): image = x.float().cuda() target = y.float().cuda() output = model(image) # model output target_soft = get_soft_label(target, args.num_classes) # get soft label loss = criterion(output, target_soft, args.num_classes) # the dice losses losses.update(loss.data, image.size(0)) # compute gradient and do SGD step optimizer.zero_grad() loss.backward() optimizer.step() if step % (math.ceil( float(len(train_loader.dataset)) / args.batch_size)) == 0: print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {losses.avg:.6f}'. format(epoch, step * len(image), len(train_loader.dataset), 100. * step / len(train_loader), losses=losses)) print('The average loss:{losses.avg:.4f}'.format(losses=losses)) return losses.avg
def test_isic(test_loader, model, args): isic_dice = [] isic_iou = [] isic_assd = [] # SKY # isic_j1 = [] # isic_j2 = [] isic_j3 = [] # isic_j4 = [] # isic_jf = [] modelname = args.ckpt + '/' + 'min_loss' + '_' + args.data + '_checkpoint.pth.tar' if os.path.isfile(modelname): print("=> Loading checkpoint '{}'".format(modelname)) checkpoint = torch.load(modelname) # start_epoch = checkpoint['epoch'] model.load_state_dict(checkpoint['state_dict']) # optimizer.load_state_dict(checkpoint['opt_dict']) print("=> Loaded saved the best model at (epoch {})".format( checkpoint['epoch'])) else: print("=> No checkpoint found at '{}'".format(modelname)) model.eval() for step, (img, lab) in tqdm(enumerate(test_loader), total=len(test_loader)): image = img.float().cuda() target = lab.float().cuda() output = model(image) # model output output_dis = torch.max(output, 1)[1].unsqueeze(dim=1) output_soft = get_soft_label(output_dis, args.num_classes) target_soft = get_soft_label(target, args.num_classes) # get soft label label_arr = np.squeeze(target_soft.cpu().numpy()).astype(np.uint8) output_arr = np.squeeze(output_soft.cpu().byte().numpy()).astype( np.uint8) isic_b_dice = val_dice_isic(target_soft, output_soft, args.num_classes) # the dice accuracy isic_b_iou = Intersection_over_Union_isic( output_soft, target_soft, args.num_classes) # the iou accuracy isic_b_asd = assd(output_arr[:, :, :, 1], label_arr[:, :, :, 1]) # the assd dice_np = isic_b_dice.data.cpu().numpy() iou_np = isic_b_iou.data.cpu().numpy() isic_dice.append(dice_np) isic_iou.append(iou_np) isic_assd.append(isic_b_asd) # SKY # isic_b_jaccard1_mean, isic_b_jaccard1_thresh = compute_jaccard(target_soft, output_soft) # isic_b_jaccard2iou = iou_numpy(labels = target_soft, outputs = output_soft) isic_b_jaccard3 = jaccard_coef( target_soft, output_soft ) # jaccard_isic(output_soft, target_soft, args.num_classes) # isic_b_jaccard4 = jaccard_coef_loss(target_soft, output_soft) # isic_b_jaccardf = jaccard_isic(output_soft, target_soft, args.num_classes) # isic_jaccard_1_np = isic_b_jaccard1_mean.data.cpu().numpy() # isic_jaccard_2_np = isic_b_jaccard2iou.data.cpu().numpy() isic_jaccard_3_np = isic_b_jaccard3.data.cpu().numpy() # isic_jaccard_4_np = isic_b_jaccard4.data.cpu().numpy() # jaccard_np = isic_b_jaccardf.data.cpu().numpy() # if isic_jaccard_2_np >= 0.65: # isic_j2.append(isic_jaccard_2_np) # else: # isic_j2.append(0) # isic_j2.append(isic_jaccard_2_np) # isic_j1.append(isic_jaccard_1_np) # isic_j2.append(isic_jaccard_2_np) isic_j3.append(isic_jaccard_3_np) # isic_j4.append(isic_jaccard_4_np) # isic_jf.append(jaccard_np) isic_dice_mean = np.average(isic_dice) isic_dice_std = np.std(isic_dice) isic_iou_mean = np.average(isic_iou) isic_iou_std = np.std(isic_iou) isic_assd_mean = np.average(isic_assd) isic_assd_std = np.std(isic_assd) # SKY # isic_jaccard_mean1 = np.average(isic_j1) # isic_jaccard_mean2 = np.average(isic_j2) isic_jaccard_mean3 = np.average(isic_j3) isic_jaccard_std = np.std(isic_j3) # isic_jaccard_mean4 = np.average(isic_j4) # isic_jaccard_f_mean = np.average(isic_jf) print('The ISIC mean Accuracy: {isic_dice_mean: .4f};'.format( isic_dice_mean=isic_dice_mean)) print( 'The ISIC mean IoU: {isic_iou_mean: .4f}; The ISIC IoU std: {isic_iou_std: .4f}' .format(isic_iou_mean=isic_iou_mean, isic_iou_std=isic_iou_std)) print( 'The ISIC mean assd: {isic_asd_mean: .4f}; The ISIC assd std: {isic_asd_std: .4f}' .format(isic_asd_mean=isic_assd_mean, isic_asd_std=isic_assd_std)) # SKY # print('The ISIC mean thresh Jaccard1: {isic_jaccard: .4f};'.format( # isic_jaccard=isic_jaccard_mean1)) # print('The ISIC mean thresh Jaccard2: {isic_jaccard: .4f};'.format( # isic_jaccard=isic_jaccard_mean2)) print( 'The ISIC mean thresh Jaccard: {isic_jaccard: .4f}; The ISIC Jaccard std: {isic_jaccard_std: .4f};' .format(isic_jaccard=isic_jaccard_mean3, isic_jaccard_std=isic_jaccard_std))
def test_isic(test_loader, model, args): isic_dice = [] isic_iou = [] isic_assd = [] modelname = args.ckpt + '/' + 'min_loss' + '_' + args.data + '_checkpoint.pth.tar' if os.path.isfile(modelname): print("=> Loading checkpoint '{}'".format(modelname)) checkpoint = torch.load(modelname) # start_epoch = checkpoint['epoch'] model.load_state_dict(checkpoint['state_dict']) # optimizer.load_state_dict(checkpoint['opt_dict']) print("=> Loaded saved the best model at (epoch {})".format( checkpoint['epoch'])) else: print("=> No checkpoint found at '{}'".format(modelname)) model.eval() for step, (img, lab) in tqdm(enumerate(test_loader), total=len(test_loader)): image = img.float().cuda() target = lab.float().cuda() output = model(image) # model output output_dis = torch.max(output, 1)[1].unsqueeze(dim=1) output_soft = get_soft_label(output_dis, args.num_classes) target_soft = get_soft_label(target, args.num_classes) # get soft label label_arr = np.squeeze(target_soft.cpu().numpy()).astype(np.uint8) output_arr = np.squeeze(output_soft.cpu().byte().numpy()).astype( np.uint8) isic_b_dice = val_dice_isic(output_soft, target_soft, args.num_classes) # the dice accuracy isic_b_iou = Intersection_over_Union_isic( output_soft, target_soft, args.num_classes) # the iou accuracy isic_b_asd = assd(output_arr[:, :, :, 1], label_arr[:, :, :, 1]) # the assd dice_np = isic_b_dice.data.cpu().numpy() iou_np = isic_b_iou.data.cpu().numpy() isic_dice.append(dice_np) isic_iou.append(iou_np) isic_assd.append(isic_b_asd) isic_dice_mean = np.average(isic_dice) isic_dice_std = np.std(isic_dice) isic_iou_mean = np.average(isic_iou) isic_iou_std = np.std(isic_iou) isic_assd_mean = np.average(isic_assd) isic_assd_std = np.std(isic_assd) print( 'The ISIC mean Accuracy: {isic_dice_mean: .4f}; The Placenta Accuracy std: {isic_dice_std: .4f}' .format(isic_dice_mean=isic_dice_mean, isic_dice_std=isic_dice_std)) print( 'The ISIC mean IoU: {isic_iou_mean: .4f}; The ISIC IoU std: {isic_iou_std: .4f}' .format(isic_iou_mean=isic_iou_mean, isic_iou_std=isic_iou_std)) print( 'The ISIC mean assd: {isic_asd_mean: .4f}; The ISIC assd std: {isic_asd_std: .4f}' .format(isic_asd_mean=isic_assd_mean, isic_asd_std=isic_assd_std))
def test_fetus(test_loader, model, args): placenta_dice = [] brain_dice = [] placenta_iou = [] brain_iou = [] placenta_assd = [] brain_assd = [] modelname = args.ckpt + '/' + 'min_loss' + '_' + args.data + '_checkpoint.pth.tar' if os.path.isfile(modelname): print("=> Loading checkpoint '{}'".format(modelname)) checkpoint = torch.load(modelname) # start_epoch = checkpoint['epoch'] model.load_state_dict(checkpoint['state_dict']) # optimizer.load_state_dict(checkpoint['opt_dict']) print("=> Loaded saved the best model at (epoch {})".format( checkpoint['epoch'])) else: print("=> No checkpoint found at '{}'".format(modelname)) model.eval() for step, (img, lab) in tqdm(enumerate(test_loader), total=len(test_loader)): image = img.float().cuda() target = lab.float().cuda() output = model(image) # model output output_dis = torch.max(output, 1)[1].unsqueeze(dim=1) output_soft = get_soft_label(output_dis, args.num_classes) target_soft = get_soft_label(target, args.num_classes) # get soft label # input_arr = np.squeeze(image.cpu().numpy()).astype(np.float32) label_arr = np.squeeze(target_soft.cpu().numpy()).astype(np.uint8) output_arr = np.squeeze(output_soft.cpu().byte().numpy()).astype( np.uint8) placenta_b_dice, brain_b_dice = val_dice_fetus( output_soft, target_soft, args.num_classes) # the dice accuracy placenta_b_iou, brain_b_iou = Intersection_over_Union_fetus( output_soft, target_soft, args.num_classes) # the iou accuracy placenta_b_asd = assd(output_arr[:, :, :, 1], label_arr[:, :, :, 1]) brain_b_asd = assd(output_arr[:, :, :, 2], label_arr[:, :, :, 2]) pla_dice_np = placenta_b_dice.data.cpu().numpy() bra_iou_np = brain_b_iou.data.cpu().numpy() bra_dice_np = brain_b_dice.data.cpu().numpy() pla_iou_np = placenta_b_iou.data.cpu().numpy() placenta_dice.append(pla_dice_np) brain_dice.append(bra_dice_np) placenta_iou.append(pla_iou_np) brain_iou.append(bra_iou_np) placenta_assd.append(placenta_b_asd) brain_assd.append(brain_b_asd) placenta_dice_mean = np.average(placenta_dice) placenta_dice_std = np.std(placenta_dice) brain_dice_mean = np.average(brain_dice) brain_dice_std = np.std(brain_dice) placenta_iou_mean = np.average(placenta_iou) placenta_iou_std = np.std(placenta_iou) brain_iou_mean = np.average(brain_iou) brain_iou_std = np.std(brain_iou) placenta_assd_mean = np.average(placenta_assd) placenta_assd_std = np.std(placenta_assd) brain_assd_mean = np.average(brain_assd) brain_assd_std = np.std(brain_assd) print( 'The Placenta mean Accuracy: {placenta_dice_mean: .4f}; The Placenta Accuracy std: {placenta_dice_std: .4f}; ' 'The Brain mean Accuracy: {brain_dice_mean: .4f}; The Brain Accuracy std: {brain_dice_std: .4f}' .format(placenta_dice_mean=placenta_dice_mean, placenta_dice_std=placenta_dice_std, brain_dice_mean=brain_dice_mean, brain_dice_std=brain_dice_std)) print( 'The Placenta mean IoU: {placenta_iou_mean: .4f}; The Placenta IoU std: {placenta_iou_std: .4f}; ' 'The Brain mean IoU: {brain_iou_mean: .4f}; The Brain IoU std: {brain_iou_std: .4f}' .format(placenta_iou_mean=placenta_iou_mean, placenta_iou_std=placenta_iou_std, brain_iou_mean=brain_iou_mean, brain_iou_std=brain_iou_std)) print( 'The Placenta mean assd: {placenta_asd_mean: .4f}; The Placenta assd std: {placenta_asd_std: .4f}; ' 'The Brain mean assd: {brain_asd_mean: .4f}; The Brain assd std: {brain_asd_std: .4f}' .format(placenta_asd_mean=placenta_assd_mean, placenta_asd_std=placenta_assd_std, brain_asd_mean=brain_assd_mean, brain_asd_std=brain_assd_std))