def search_threshold(mask, pred): best_score = 0 best_th = 0 count = 0 results = [] ths = np.linspace(0, 1, 101) for th in ths: scores = [] for y_val_, y_pred_ in zip(mask, pred): score = dice(y_val_, y_pred_ > th) if np.isnan(score): scores.append(1) else: scores.append(score) if np.mean(scores) > best_score: best_score = np.mean(scores) best_th = th count = 0 results.append(np.mean(scores)) if np.mean(scores) < best_score: count += 1 #if count == 50: # break #results = results + [0 for _ in range(len(ths) - len(results))] return best_th, best_score, ths, results
t /= 100 for ms in [5000, 10000, 15000, 20000, 25000, 27000]: masks = [] for i in range(class_id, len(probabilities), 4): probability = probabilities[i] predict, num_predict = post_process(sigmoid(probability), t, ms, size=size) masks.append(predict) d = [] for i, j in zip(masks, valid_masks[class_id::4]): if (i.sum() == 0) & (j.sum() == 0): d.append(1) else: d.append(dice(i, j)) attempts.append((t, ms, np.mean(d))) attempts_df = pd.DataFrame(attempts, columns=['threshold', 'size', 'dice']) attempts_df = attempts_df.sort_values('dice', ascending=False) print(attempts_df.head()) best_threshold = attempts_df['threshold'].values[0] best_size = attempts_df['size'].values[0] class_params[class_id] = (best_threshold, best_size) del probabilities with open(output_name + "_params.pkl", 'wb') as handle:
def main(seed): with timer('load data'): df = pd.read_csv(FOLD_PATH) with timer('preprocessing'): val_df = df[df.fold_id == FOLD_ID] val_augmentation = None val_dataset = SeverDataset(val_df, IMG_DIR, IMG_SIZE, N_CLASSES, id_colname=ID_COLUMNS, transforms=val_augmentation) val_loader = DataLoader(val_dataset, batch_size=BATCH_SIZE, shuffle=False, num_workers=8) del val_df, df, val_dataset gc.collect() with timer('create model'): model = smp.Unet('resnet34', encoder_weights="imagenet", classes=N_CLASSES, encoder_se_module=True, decoder_semodule=True, h_columns=False, skip=True, act="swish", freeze_bn=True, classification=CLASSIFICATION, attention_type="cbam") model.load_state_dict(torch.load(base_model)) model.to(device) criterion = torch.nn.BCEWithLogitsLoss() with timer('predict'): valid_loss, y_pred, y_true, cls = predict( model, val_loader, criterion, device, classification=CLASSIFICATION) LOGGER.info('Mean valid loss: {}'.format(round(valid_loss, 5))) scores = [] for i, (th, remove_mask_pixel) in enumerate(zip(ths, remove_pixels)): sum_val_preds = np.sum( y_pred[:, i, :, :].reshape(len(y_pred), -1) > th, axis=1) cls_ = cls[:, i] best = 0 for th_cls in np.linspace(0, 1, 101): val_preds_ = copy.deepcopy(y_pred[:, i, :, :]) val_preds_[sum_val_preds < remove_mask_pixel] = 0 val_preds_[cls_ <= th_cls] = 0 scores = [] for y_val_, y_pred_ in zip(y_true[:, i, :, :], val_preds_): score = dice(y_val_, y_pred_ > 0.5) if np.isnan(score): scores.append(1) else: scores.append(score) if np.mean(scores) >= best: best = np.mean(scores) best_th = th_cls else: break LOGGER.info('dice={} on {}'.format(best, best_th)) scores.append(best) LOGGER.info('holdout dice={}'.format(np.mean(scores)))
def main(seed): with timer('load data'): df = pd.read_csv(FOLD_PATH) if N_CLASSES == 3: df.drop("EncodedPixels_2", axis=1, inplace=True) df = df.rename(columns={"EncodedPixels_3": "EncodedPixels_2"}) df = df.rename(columns={"EncodedPixels_4": "EncodedPixels_3"}) with timer('preprocessing'): val_df = df[df.fold_id == FOLD_ID] val_augmentation = None val_dataset = SeverDataset(val_df, IMG_DIR, IMG_SIZE, N_CLASSES, id_colname=ID_COLUMNS, transforms=val_augmentation) val_loader = DataLoader(val_dataset, batch_size=BATCH_SIZE, shuffle=False, num_workers=8) del val_df, df, val_dataset gc.collect() with timer('create model'): model = smp_old.Unet('resnet34', encoder_weights="imagenet", classes=N_CLASSES, encoder_se_module=True, decoder_semodule=True, h_columns=False, skip=True, act="swish", freeze_bn=True, classification=CLASSIFICATION) model.load_state_dict(torch.load(base_model)) model.to(device) model.eval() criterion = torch.nn.BCEWithLogitsLoss() with timer('predict'): valid_loss, y_pred, y_true, cls = predict(model, val_loader, criterion, device, classification=CLASSIFICATION) LOGGER.info('Mean valid loss: {}'.format(round(valid_loss, 5))) scores = [] all_scores = [] min_sizes = [300, 0, 600, 1600] for i in range(N_CLASSES): if i == 1: continue best = 0 count = 0 min_size = min_sizes[i] for th in [0.7+i*0.01 for i in range(30)]: val_preds_ = copy.deepcopy(y_pred[:, i, :, :]) scores_ = [] all_scores_ = [] for y_val_, y_pred_ in zip(y_true[:, i, :, :], val_preds_): y_pred_ = post_process(y_pred_ > 0.5, y_pred_, min_size, th) score = dice(y_val_, y_pred_) if np.isnan(score): scores_.append(1) else: scores_.append(score) LOGGER.info('dice={} on {}'.format(np.mean(scores_), th)) if np.mean(scores_) >= best: best = np.mean(scores_) count = 0 else: count += 1 if count == 3: break scores.append(best) all_scores.append(all_scores_) LOGGER.info('holdout dice={}'.format(np.mean(scores)))
def main(seed): with timer('load data'): df = pd.read_csv(FOLD_PATH) y1 = (df.EncodedPixels_1 != "-1").astype("float32").values.reshape(-1, 1) y2 = (df.EncodedPixels_2 != "-1").astype("float32").values.reshape(-1, 1) y3 = (df.EncodedPixels_3 != "-1").astype("float32").values.reshape(-1, 1) y4 = (df.EncodedPixels_4 != "-1").astype("float32").values.reshape(-1, 1) y = np.concatenate([y1, y2, y3, y4], axis=1) with timer('preprocessing'): val_df = df[df.fold_id == FOLD_ID] y_val = y[df.fold_id == FOLD_ID] val_dataset = SeverCLSDataset(val_df, IMG_DIR, IMG_SIZE, N_CLASSES, y_val, id_colname=ID_COLUMNS, transforms=None) val_loader = DataLoader(val_dataset, batch_size=BATCH_SIZE, shuffle=False, num_workers=8, pin_memory=True) models = [] model = cls_models.SEResNext(num_classes=N_CLASSES) model.load_state_dict(torch.load(base_model_cls)) model.to(device) model.eval() models.append(model) """model = cls_models.ResNet(num_classes=N_CLASSES, pretrained=None) model.load_state_dict(torch.load(base_model_res)) model.to(device) model.eval() models.append(model)""" with timer('predict cls'): criterion = torch.nn.BCEWithLogitsLoss() valid_loss, y_val, y_true = predict_cls(models, val_loader, criterion, device) #y_val = np.load("../exp_cls/y_pred_ema_ckpt8.npy") LOGGER.info("val loss={}".format(valid_loss)) with timer('preprocessing seg'): val_augmentation = None val_dataset = SeverDataset(val_df, IMG_DIR, IMG_SIZE, N_CLASSES, id_colname=ID_COLUMNS, transforms=val_augmentation, class_y=y_val) val_loader = DataLoader(val_dataset, batch_size=BATCH_SIZE, shuffle=False, num_workers=8) del val_df, df, val_dataset gc.collect() with timer('create model'): model = smp.Unet('resnet34', encoder_weights="imagenet", classes=N_CLASSES, encoder_se_module=True, decoder_semodule=True, h_columns=False, skip=True, act="swish", freeze_bn=True, classification=CLASSIFICATION, attention_type="cbam", center=True) model.load_state_dict(torch.load(base_model)) model.to(device) criterion = torch.nn.BCEWithLogitsLoss() with timer('predict'): valid_loss, y_pred, y_true, cls = predict(model, val_loader, criterion, device, classification=CLASSIFICATION) LOGGER.info('Mean valid loss: {}'.format(round(valid_loss, 5))) scores = [] for i, (th, remove_mask_pixel) in enumerate(zip(ths, remove_pixels)): if i <= 1: continue sum_val_preds = np.sum(y_pred[:, i, :, :].reshape(len(y_pred), -1) > th, axis=1) cls_ = cls[:, i] best = 0 for th_cls in np.linspace(0, 1, 101): val_preds_ = copy.deepcopy(y_pred[:, i, :, :]) val_preds_[sum_val_preds < remove_mask_pixel] = 0 val_preds_[cls_ <= th_cls] = 0 scores = [] for y_val_, y_pred_ in zip(y_true[:, i, :, :], val_preds_): score = dice(y_val_, y_pred_ > 0.5) if np.isnan(score): scores.append(1) else: scores.append(score) if np.mean(scores) >= best: best = np.mean(scores) best_th = th_cls #else: # break LOGGER.info('dice={} on {}'.format(best, best_th)) scores.append(best) LOGGER.info('holdout dice={}'.format(np.mean(scores)))
vol += 0.1 * np.random.rand(*vol.shape) # plt.imshow(vol[25,:,:], cmap='gray') seg1 = np.zeros((50, 128, 128)) seg1[20:30, 40:88, 40:88] = 1 seg2 = np.zeros((50, 128, 128)) seg2[20:29, 45:95, 45:95] = 1 seg2[29, 40:88, 40:88] = 1 # fig, axs = plt.subplots(1,3) # axs[0].imshow(np.squeeze(vol[25,:,:]), cmap='gray') # axs[1].imshow(np.squeeze(seg1[25,:,:]), cmap='gray') # axs[2].imshow(np.squeeze(seg2[25,:,:]), cmap='gray') # 1. Processing #%% # from jximgtools.metric import dicePerSlice, dice # from jximgtools.visualization.volSlicer import VolSlicer from metric import dicePerSlice, dice from visualization.volSlicer import VolSlicer # slicesInfo = [{'info':'Slice # {}'.format(sliceIdx+1) for sliceIdx in range(50)}] dicee = dice(seg1, seg2) dices = dicePerSlice(seg1, seg2) volInfo = {'Name': 'Test Volume', 'DicePerSlice': dices} slicesInfo = [{'Dice': dices[sliceIdx]} for sliceIdx in range(50)] # VolSlicer(vol, volInfo = volInfo, segs = [seg1, seg2], slicesInfo = slicesInfo) VolSlicer(vol, volInfo=volInfo, segs=[seg1, seg2]) # VolSlicer(vol)
def main(seed): with timer('load data'): df = pd.read_csv(FOLD_PATH) with timer('preprocessing'): val_df = df[df.fold_id == FOLD_ID] val_augmentation = None val_dataset = SeverDataset(val_df, IMG_DIR, IMG_SIZE, N_CLASSES, id_colname=ID_COLUMNS, transforms=val_augmentation) val_loader = DataLoader(val_dataset, batch_size=BATCH_SIZE, shuffle=False, num_workers=8) del val_df, df, val_dataset gc.collect() with timer('create model'): models = [] for p in base_model_res: model = smp.Unet('resnet34', encoder_weights="imagenet", classes=N_CLASSES, encoder_se_module=True, decoder_semodule=True, h_columns=False, skip=True, act="swish", freeze_bn=True, classification=CLASSIFICATION, attention_type="cbam", center=True) model.load_state_dict(torch.load(p)) model.to(device) model.eval() models.append(model) model = smp_old.Unet('resnet34', encoder_weights="imagenet", classes=N_CLASSES, encoder_se_module=True, decoder_semodule=True, h_columns=False, skip=True, act="swish", freeze_bn=True, classification=CLASSIFICATION) model.load_state_dict(torch.load(base_model_res_old)) model.to(device) model.eval() models.append(model) criterion = torch.nn.BCEWithLogitsLoss() with timer('predict'): valid_loss, y_pred, y_true, cls = predict( models, val_loader, criterion, device, classification=CLASSIFICATION) LOGGER.info('Mean valid loss: {}'.format(round(valid_loss, 5))) scores = [] all_scores = [] for i, th in enumerate(ths): sum_val_preds = np.sum( y_pred[:, i, :, :].reshape(len(y_pred), -1) > th, axis=1) best = 0 for n_th, remove_mask_pixel in enumerate( [200, 400, 600, 800, 1000, 1200, 1400, 1600, 1800]): val_preds_ = copy.deepcopy(y_pred[:, i, :, :]) val_preds_[sum_val_preds < remove_mask_pixel] = 0 scores_ = [] all_scores_ = [] for y_val_, y_pred_ in zip(y_true[:, i, :, :], val_preds_): score = dice(y_val_, y_pred_ > 0.5) if np.isnan(score): scores_.append(1) else: scores_.append(score) LOGGER.info('dice={} on {}'.format(np.mean(scores_), remove_mask_pixel)) if np.mean(scores_) >= best: best = np.mean(scores_) all_scores_.append(np.mean(scores_)) scores.append(np.mean(scores_)) all_scores.append(all_scores_) LOGGER.info('holdout dice={}'.format(np.mean(scores))) np.save("all_scores_fold{}.npy".format(FOLD_ID), np.array(all_scores))