Exemplo n.º 1
0
    def __init__(self,
                 exp_dir='expert_segmentations',
                 config=None,
                 path=None,
                 cmap='viridis',
                 verbose=1):
        self.exp_dir = exp_dir
        self.config = config or Config()
        self.path = Path(path) if path is not None else Path('.')
        self.mask_fn = lambda exp, msk: self.path / self.exp_dir / exp / msk
        self.cmap = cmap
        self.gt = {}

        f_list = get_image_files(self.path / self.exp_dir)
        assert len(
            f_list
        ) > 0, f'Found {len(f_list)} masks in "{self.path/self.exp_dir}". Please check your masks and expert folders.'
        ass_str = f'Found unexpected folder structure in {self.path/self.exp_dir}. Please check your provided masks and folders.'
        assert len(f_list[0].relative_to(self.path /
                                         self.exp_dir).parents) == 2, ass_str

        self.masks = {}
        self.experts = []
        for m in sorted(f_list):
            exp = m.parent.name
            if m.name in self.masks:
                self.masks[m.name].append(exp)
            else:
                self.masks[m.name] = [exp]
            self.experts.append(exp)
        self.experts = sorted(set(self.experts))
        if verbose > 0:
            print(
                f'Found {len(self.masks)} unique segmentation mask(s) from {len(self.experts)} expert(s)'
            )
Exemplo n.º 2
0
      def __init__(self, root=MyPath.db_root_dir('bird'), split='train', transform=None):
        super(Birds, self).__init__()



        self.transform = transform

        self.resize = tf.Resize(256)


        path = untar_data(URLs.CUB_200_2011)


        self.files = get_image_files(path/"images")
        self.label = dict(sorted(enumerate(set(self.files.map(self.label_func))), key=itemgetter(1)))
        self.labels = dict([(value, key) for key, value in self.label.items()])
        self.df = pd.read_csv(path/'train_test_split.txt',delimiter=' ')


        if self.split == 'train':
          self.file_index = [i['1'] for i in self.df.to_dict('records') if i['0']==1]
          self.Files= [i for i in self.files if self.splitter(i) in self.file_index]


        else:
          self.file_index = [i['1'] for i in self.df.to_dict('records') if i['0']==0]
          self.Files = [i for i in self.files if self.splitter(i) in self.file_index]
Exemplo n.º 3
0
    def __init__(self,
                 image_dir='images',
                 mask_dir=None,
                 config=None,
                 path=None,
                 ensemble_dir=None,
                 label_fn=None,
                 metrics=None,
                 loss_fn=None,
                 cbs=None,
                 ds_kwargs={},
                 stats=None,
                 files=None):

        self.config = config or Config()
        self.stats = stats
        self.path = Path(path) if path is not None else Path('.')
        self.metrics = metrics or [Iou()]  #Dice_f1()
        self.loss_fn = loss_fn or WeightedSoftmaxCrossEntropy(axis=1)
        self.cbs = cbs or [
            SaveModelCallback(monitor='iou'), ElasticDeformCallback
        ]  #ShowGraphCallback
        self.ensemble_dir = ensemble_dir or self.path / 'ensemble'

        self.files = L(files) or get_image_files(self.path / image_dir,
                                                 recurse=False)
        assert len(
            self.files
        ) > 0, f'Found {len(self.files)} images in "{image_dir}". Please check your images and image folder'
        if any([mask_dir, label_fn]):
            if label_fn: self.label_fn = label_fn
            else:
                self.label_fn = get_label_fn(self.files[0],
                                             self.path / mask_dir)
            #Check if corresponding masks exist
            mask_check = [self.label_fn(x).is_file() for x in self.files]
            chk_str = f'Found {len(self.files)} images in "{image_dir}" and {sum(mask_check)} masks in "{mask_dir}".'
            assert len(self.files) == sum(mask_check) and len(
                self.files
            ) > 0, f'Please check your images and masks (and folders). {chk_str}'
            print(chk_str)

        else:
            self.label_fn = label_fn

        self.n_splits = min(len(self.files), self.max_splits)
        for key, value in get_default_shapes(self.arch).items():
            ds_kwargs.setdefault(key, value)
        self.ds_kwargs = ds_kwargs
        self.item_tfms = [Brightness(max_lighting=self.light)]
        self.models = {}
        self.recorder = {}
        self._set_splits()
        self.ds = RandomTileDataset(self.files,
                                    label_fn=self.label_fn,
                                    create_weights=False,
                                    **self.mw_kwargs,
                                    **self.ds_kwargs)
        self.in_channels = self.ds.get_data(max_n=1)[0].shape[-1]
        self.df_val, self.df_ens, self.df_model, self.ood = None, None, None, None
def bayes_build_inference_dfdlpreds(self:Learner, path, dataset, item_count=100,n_sample=10):
    items = get_image_files(path).shuffle()[:item_count]
    dl = self.dls.test_dl(items.map(lambda o: PILImage.create(o)), num_workers=0)
    res = self.bayes_get_preds(dl=dl,n_sample=n_sample)
    ents = res[2]
    preds = res[0]
    unc = uncertainty_best_probability(preds)
    bald = BALD(preds)
    df = pd.DataFrame(pd.Series(items,name='image_files'))
    df['entropy'] = pd.Series(ents,name='entropy')
    df['best_prob_uncertainty'] = pd.Series(unc,name='best_prob_uncertainty')
    df['bald'] = pd.Series(bald,name='bald')
    df['dataset'] = dataset
    return (df,dl, preds)
Exemplo n.º 5
0
def get_tuple_files(path):
    files = get_image_files(path)
    return [[f, *draw_other(f)] for f in files]
Exemplo n.º 6
0
# ## Preparing the data

# To make our data ready for training a model, we need to create a `DataLoaders` object in fastai. It is just a wrapper around a training `DataLoader` and a validation `DataLoader`, so if you already have your own PyTorch dataloaders, you can create such an object directly.
#
# Here we don't have anything ready yet. Usually, when using PyTorch, the first step is to create a `Dataset` that is then wrapped inside a `DataLoader`. We will do this first, then see how to change this `Dataset` into a `Transform` that will let us take advantage of fastai's functionality for showing a batch or using data augmentation on the GPU. Lastly we will see how we can customize the data block API and create our own new `TransformBlock`.

# ### Purely in PyTorch

# To begin with, we will only use PyTorch and PIL to create a `Dataset` and see how to get this inside fastai. The only helper functions from fastai we will use are `untar_data` (to download and untar the dataset) and `get_image_files` (that looks for all images in a folder recursively). Here, we will use the [Oxford-IIIT Pet Dataset](https://www.robots.ox.ac.uk/~vgg/data/pets/).


# `untar_data` returns a `pathlib.Path` object with the location of the decompressed dataset, and in this case, all the images are in an images subfolder:

path = untar_data(URLs.PETS)
files = get_image_files(path / "images")
files[0]

# We can open the first image with PIL and have a look at it:


img = PIL.Image.open(files[0])
img

# Let's wrap all the standard preprocessing (resize, conversion to tensor, dividing by 255 and reordering of the channels) in one helper function:



def open_image(fname, size=224):
    img = PIL.Image.open(fname).convert('RGB')
    img = img.resize((size, size))
Exemplo n.º 7
0
    def __init__(self,
                 image_dir='images',
                 mask_dir=None,
                 config=None,
                 path=None,
                 ensemble_path=None,
                 preproc_dir=None,
                 label_fn=None,
                 metrics=None,
                 cbs=None,
                 ds_kwargs={},
                 dl_kwargs={},
                 model_kwargs={},
                 stats=None,
                 files=None):

        self.config = config or Config()
        self.stats = stats
        self.dl_kwargs = dl_kwargs
        self.model_kwargs = model_kwargs
        self.add_ds_kwargs = ds_kwargs
        self.path = Path(path) if path is not None else Path('.')
        default_metrics = [Dice()] if self.n_classes == 2 else [DiceMulti()]
        self.metrics = metrics or default_metrics
        self.loss_fn = self.get_loss()
        self.cbs = cbs or [
            SaveModelCallback(
                monitor='dice' if self.n_classes == 2 else 'dice_multi')
        ]  #ShowGraphCallback
        self.ensemble_dir = ensemble_path or self.path / self.ens_dir
        if ensemble_path is not None:
            ensemble_path.mkdir(exist_ok=True, parents=True)
            self.load_ensemble(path=ensemble_path)
        else:
            self.models = {}

        self.files = L(files) or get_image_files(self.path / image_dir,
                                                 recurse=False)
        assert len(
            self.files
        ) > 0, f'Found {len(self.files)} images in "{image_dir}". Please check your images and image folder'
        if any([mask_dir, label_fn]):
            if label_fn: self.label_fn = label_fn
            else:
                self.label_fn = get_label_fn(self.files[0],
                                             self.path / mask_dir)
            #Check if corresponding masks exist
            mask_check = [self.label_fn(x).exists() for x in self.files]
            chk_str = f'Found {len(self.files)} images in "{image_dir}" and {sum(mask_check)} masks in "{mask_dir}".'
            assert len(self.files) == sum(mask_check) and len(
                self.files
            ) > 0, f'Please check your images and masks (and folders). {chk_str}'
            print(chk_str)

        else:
            self.label_fn = label_fn
        self.n_splits = min(len(self.files), self.max_splits)
        self._set_splits()
        self.ds = RandomTileDataset(
            self.files,
            label_fn=self.label_fn,
            preproc_dir=preproc_dir,
            instance_labels=self.instance_labels,
            n_classes=self.n_classes,
            stats=self.stats,
            normalize=True,
            sample_mult=self.sample_mult if self.sample_mult > 0 else None,
            verbose=0,
            **self.add_ds_kwargs)

        self.stats = self.ds.stats
        self.in_channels = self.ds.get_data(max_n=1)[0].shape[-1]
        self.df_val, self.df_ens, self.df_model, self.ood = None, None, None, None
        self.recorder = {}