Beispiel #1
0
def get_databunch():
  df = get_decorated_df()

  data = (ImageDataBunch.from_df(df, size=224)
          .random_split_by_pct()
          .transform())

  return data
Beispiel #2
0
 def export_to_fastAI(self, imagedatabunch_args={}):
     # https://docs.fast.ai/vision.data.html#ImageDataBunch.from_df
     # from fastai.vision import ImageDataBunch
     # data = ImageDataBunch.from_df(path, df, ds_tfms=tfms, size=24)
     # return data
     try:
         from fastai.vision import ImageDataBunch, ImageList
         from fastai.vision.transform import get_transforms
     except ModuleNotFoundError as e:
         ERROR['missing_module']('fastai')
     df = self.filesnames_to_ml_df(withAbsPath=False)
     fastai_idb  = ImageDataBunch.from_df(path='', df=df, **imagedatabunch_args)
     return fastai_idb
Beispiel #3
0
def save_preds(input_csv, output_csv):
    df = pd.read_csv(input_csv)
    try:
        df = df[['Study']]
    except:
        try:
            df = df[['Path']]
        except:
            raise ValueError('csv has no attribute for path/study.')

    for lbl in ALL_LBLS:
        df[lbl] = np.zeros(len(df))

    test = ImageDataBunch.from_df(
        path=folder_path,
        df=df,
        folder=chexpert_folder,
        seed=0,
        label_col=ALL_LBLS,
        suffix='',
        valid_pct=1,
        ds_tfms=data_tfms,
        bs=BS,
        size=IMG_SZ)  #.normalize([IMG_MEAN, IMG_STD])

    IDs, outputs = test.valid_ds.x.items, []

    learn = cnn_learner(test,
                        models.densenet121,
                        model_dir=model_path,
                        pretrained=False)
    learn.load(model_names[0])
    output, y, _ = learn.get_preds(ds_type=DatasetType.Valid, with_loss=True)
    outputs.append(output)

    learn.load(model_names[1])
    output, y, _ = learn.get_preds(ds_type=DatasetType.Valid, with_loss=True)
    outputs.append(output)

    learn = cnn_learner(test,
                        models.resnet152,
                        model_dir=model_path,
                        pretrained=False)
    learn.load(model_names[2])
    output, y, _ = learn.get_preds(ds_type=DatasetType.Valid, with_loss=True)
    outputs.append(output)

    learn.load(model_names[3])
    output, y, _ = learn.get_preds(ds_type=DatasetType.Valid, with_loss=True)
    outputs.append(output)

    model = resnext101_64x4d(pretrained=None)
    model.last_linear = nn.Sequential(nn.Linear(32768, 2048), nn.ReLU(True),
                                      nn.Dropout(), nn.Linear(2048, 14))
    learn = Learner(test, model, model_dir=model_path)
    learn.load(model_names[4])
    output, y, _ = learn.get_preds(ds_type=DatasetType.Valid, with_loss=True)
    outputs.append(output)

    learn = cnn_learner(test,
                        models.vgg19_bn,
                        model_dir=model_path,
                        pretrained=False)
    learn.load(model_names[5])
    output, y, _ = learn.get_preds(ds_type=DatasetType.Valid, with_loss=True)
    outputs.append(output)

    learn.load(model_names[6])
    output, y, _ = learn.get_preds(ds_type=DatasetType.Valid, with_loss=True)
    outputs.append(output)

    learn = cnn_learner(test,
                        models.densenet121,
                        model_dir=model_path,
                        pretrained=False)
    learn.load(model_names[7])
    output, y, _ = learn.get_preds(ds_type=DatasetType.Valid, with_loss=True)
    outputs.append(output)

    output = ensemble_method(outputs, mode='avg')
    if torch.cuda.is_available():
        output = output.cpu()
    output = output.numpy()

    df = pd.DataFrame({
        'Path': IDs,
        EVAL_LBLS[0]: output[:, 1],
        EVAL_LBLS[1]: output[:, 2],
        EVAL_LBLS[2]: output[:, 3],
        EVAL_LBLS[3]: output[:, 4],
        EVAL_LBLS[4]: output[:, 5]
    })

    df.to_csv(output_csv, index=False)
    print('submission saved.')