Beispiel #1
0
def get_preds(learn: Learner,
              dl: DataLoader,
              with_loss: bool = False,
              n_batch: Optional[int] = None,
              pbar: Optional[PBar] = None) -> List[Tensor]:
    """Return predictions and targets on `dl` dataset.
    This function is the same as fastai's Learner.get_preds except this allows an external DataLoader.
    For more details about Learner.get_preds, see:
    https://github.com/fastai/fastai/blob/master/fastai/basic_train.py

    Args:
        learn: Learner object that will be used for prediction
        dl: DataLoader the model will use to load samples
        with_loss: If True, it will also return the loss on each prediction
        n_batch: Number of batches to predict. If not specified, it will run the predictions for n batches
            where n = sample size // BATCH_SIZE
        pbar: ProgressBar object
    """
    lf = learn.loss_func if with_loss else None
    return fastai.basic_train.get_preds(
        learn.model,
        dl,
        cb_handler=CallbackHandler(learn.callbacks),
        activ=_loss_func2activ(learn.loss_func),
        loss_func=lf,
        n_batch=n_batch,
        pbar=pbar)
Beispiel #2
0
def get_preds(learn: Learner,
              dl: DataLoader,
              with_loss: bool = False,
              n_batch: Optional[int] = None,
              pbar: Optional[PBar] = None) -> List[Tensor]:
    """Return predictions and targets on `dl` dataset.
    This function is the same as fastai's Learner.get_preds except this allows an external DataLoader.
    For more details about Learner.get_preds, see:
    https://github.com/fastai/fastai/blob/master/fastai/basic_train.py

    Args:
        learn: Learner object that will be used for prediction
        dl: DataLoader the model will use to load samples
        with_loss: If True, it will also return the loss on each prediction
        n_batch: Number of batches to predict. If not specified, it will run the predictions for n batches
            where n = sample size // BATCH_SIZE
        pbar: ProgressBar object
    """

    # Note: In Fastai, for DatasetType.Train, only the output of complete minibatches is computed. Ie if one has 101 images,
    # and uses a minibatch size of 16, then len(feats) is 96 and not 101. For DatasetType.Valid this is not the case,
    # and len(feats) is as expected 101. A way around this is to use DatasetType.Fix instead when referring to the training set.
    # See e.g. issue: https://forums.fast.ai/t/get-preds-returning-less-results-than-length-of-original-dataset/34148
    if dl == DatasetType.Train:
        dl = DatasetType.Fix

    lf = learn.loss_func if with_loss else None
    return fastai.basic_train.get_preds(
        learn.model,
        dl,
        cb_handler=CallbackHandler(learn.callbacks),
        activ=_loss_func2activ(learn.loss_func),
        loss_func=lf,
        n_batch=n_batch,
        pbar=pbar)
def _tta_only(learn:Learner, ds_type:DatasetType=DatasetType.Valid) -> Iterator[List[Tensor]]:
    "Computes the outputs for several augmented inputs for TTA"
    dl = learn.dl(ds_type)
    ds = dl.dataset
    old = ds.tfms
    augm_tfm = [o for o in learn.data.train_ds.tfms]
    try:
        pbar = master_bar(range(8))
        for i in pbar:
            ds.tfms = augm_tfm
            yield get_preds(learn.model, dl, pbar=pbar, activ=_loss_func2activ(learn.loss_func))[0]
    finally: ds.tfms = old
Beispiel #4
0
def custom_tta(learn:Learner, ds_type:DatasetType=DatasetType.Valid):
    dl = learn.dl(ds_type)
    ds = dl.dataset

    old_open_image = fastai.vision.data.open_image
    try:
        maxNumberOfCrops = 20
        for i in range(maxNumberOfCrops):
            #print("starting")
            setupNewCrop(i)
            yield get_preds(learn.model, dl, activ=_loss_func2activ(learn.loss_func))[0]
    finally:
            fastai.vision.data.open_image = old_open_image
Beispiel #5
0
def _seg_tta_only_v1(
        learn: Learner,
        ds_type: DatasetType = DatasetType.Valid) -> Iterator[List[Tensor]]:
    "Computes the outputs for non-flip and flip_lr augmented inputs"
    dl = learn.dl(ds_type)
    ds = dl.dataset
    old = ds.tfms
    try:
        pbar = master_bar(range(2))
        for i in pbar:
            tfm = []  # to remove random crop resize aug
            if i: tfm.append(flip_lr(p=1.))
            ds.tfms = tfm
            yield get_preds(learn.model,
                            dl,
                            pbar=pbar,
                            activ=_loss_func2activ(learn.loss_func))[0]
    finally:
        ds.tfms = old