Exemplo n.º 1
0
 def _instantiate_empty_dataloader(self):
     """
         As it is almost impossible to instantiate an empty dataloader, we use a CIFAR as a dummy.
     """
     path = untar_data(URLs.CIFAR_100)
     files = get_image_files(path / "train")
     return ImageDataLoaders.from_lists(path, files, [0] * len(files))
Exemplo n.º 2
0
def main(epochs):
    Task.init(project_name="examples", task_name="fastai v2")

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

    dls = ImageDataLoaders.from_name_func(path,
                                          files,
                                          label_func,
                                          item_tfms=Resize(224),
                                          num_workers=0)
    dls.show_batch()
    learn = cnn_learner(dls, resnet34, metrics=error_rate)
    learn.fine_tune(epochs)
    learn.show_results()
Exemplo n.º 3
0
def create_dataloaders() -> DataLoaders:
    """
    Create the dataloaders for the cats vs. dogs dataset.
    """
    path = untar_data(URLs.PETS) / "images"
    dls = ImageDataLoaders.from_name_func(
        path,
        get_image_files(path),
        valid_pct=0.2,
        batch_size=8,
        seed=42,
        label_func=is_cat,
        item_tfms=Resize(224),
    )
    return dls
def objective(trial):
    # Data Augmentation
    apply_tfms = trial.suggest_categorical("apply_tfms", [True, False])
    if apply_tfms:
        # MNIST is a hand-written digit dataset. Thus horizontal and vertical flipping are
        # disabled. However, the two flipping will be important when the dataset is CIFAR or
        # ImageNet.
        tfms = aug_transforms(
            do_flip=False,
            flip_vert=False,
            max_rotate=trial.suggest_int("max_rotate", 0, 45),
            max_zoom=trial.suggest_float("max_zoom", 1, 2),
            p_affine=trial.suggest_float("p_affine", 0.1, 1.0, step=0.1),
        )
    data = ImageDataLoaders.from_folder(
        path, bs=BATCHSIZE, batch_tfms=tfms if apply_tfms else None
    )

    n_layers = trial.suggest_int("n_layers", 2, 5)

    n_channels = [3]
    for i in range(n_layers):
        out_channels = trial.suggest_int("n_channels_{}".format(i), 3, 32)
        n_channels.append(out_channels)
    n_channels.append(2)

    model = SimpleCNN(n_channels)

    learn = Learner(
        data,
        model,
        metrics=[accuracy],
        # You could as FastAIPruningCallback in the fit function
        cbs=FastAIPruningCallback(trial),
    )

    # See https://forums.fast.ai/t/how-to-diable-progress-bar-completely/65249/3
    # to disable progress bar and logging info
    with learn.no_bar():
        with learn.no_logging():
            learn.fit(EPOCHS)

    return learn.validate()[-1]
Exemplo n.º 5
0
 def fetch(cls, df, imagesize: int, batchsize: int):
     """
     Function to create a dataloader with the required transformations
     """
     device = cls._detect_device(cls.SEED)
     # We apply the following augmentations:
     # - Flip the images vertically
     # - Rotate the images randomly, with max rotation angle as 10 degres
     # - Zoom in and out of the image, with max zoom as 1.1x
     # - Change lighting of the image by 20%
     # - Add warping to the image
     # - Resize the image to given size. Typically, this is 224x224 and 320x320
     # - Perform affine transformations on the image with probability 0.7
     # - Perform lighing transformation on the image with probability 0.9
     trfm = aug_transforms(
         do_flip=True,
         flip_vert=True,
         max_rotate=10.0,
         max_zoom=1.1,
         max_lighting=0.2,
         max_warp=0.2,
         size=imagesize,
         p_affine=0.7,
         p_lighting=0.9,
     )
     # Change valid_pct to use a validation set and measure progress
     image_loader = ImageDataLoaders.from_df(
         df[["name", "label"]],
         valid_pct=0.0,
         seed=cls.SEED,
         device=device,
         item_tfms=Resize(460),
         bs=batchsize,
         batch_tfms=trfm,
     )
     return image_loader
Exemplo n.º 6
0
    fold = n
    print('=' * 10, f'FOLD {n}', '=' * 10)
    X_tr = [imgs_idxs[i] for i in tr]
    X_val = [imgs_idxs[i] for i in te]
    print('train:', len(X_tr), '| test:', len(X_val))
    print('groups train:', set([x[:9] for x in X_tr]), '\ngroups test:',
          set([x[:9] for x in X_val]))

    model = HuBMAPModel()

    train_ds = HuBMAPDataset(X_tr, tfms)
    valid_ds = HuBMAPDataset(X_val)

    data = ImageDataLoaders.from_dsets(train_ds,
                                       valid_ds,
                                       bs=cfg.batch_size,
                                       num_workers=4 * 4,
                                       pin_memory=True)

    if torch.cuda.is_available():
        data.cuda(), model.cuda()

    cbs = [SaveModelCallback(monitor='dice_th', comp=np.greater)]
    learn = Learner(data,
                    model,
                    metrics=cfg.metrics,
                    wd=cfg.weight_decay,
                    loss_func=cfg.loss_func,
                    opt_func=ranger,
                    cbs=cbs,
                    model_dir='models' + '_' + name + cfg.cv_method +
Exemplo n.º 7
0
from fastai.vision.all import untar_data, URLs, ImageDataLoaders, get_image_files, Resize, error_rate, resnet34, \
    cnn_learner

from labml import lab, experiment
from labml.utils.fastai import LabMLFastAICallback

path = untar_data(
    URLs.PETS,
    dest=lab.get_data_path(),
    fname=lab.get_data_path() / URLs.path(URLs.PETS).name) / 'images'


def is_cat(x):
    return x[0].isupper()


dls = ImageDataLoaders.from_name_func(path,
                                      get_image_files(path),
                                      valid_pct=0.2,
                                      seed=42,
                                      label_func=is_cat,
                                      item_tfms=Resize(224))
# Train the model ⚡
learn = cnn_learner(dls,
                    resnet34,
                    metrics=error_rate,
                    cbs=LabMLFastAICallback())

with experiment.record(name='pets', exp_conf=learn.labml_configs()):
    learn.fine_tune(5)
        convs = [ConvLayer(c, c * 2, stride=2) for c in ch_in]
        convs += [AdaptiveAvgPool(), Flatten(), nn.Linear(48, emb_size)]
        self.convs = nn.Sequential(*convs)
        self.classifier = classifier

    def get_embs(self, x):
        return self.convs(x)

    def forward(self, x):
        x = self.get_embs(x)
        x = self.classifier(x)
        return x


dls = ImageDataLoaders.from_folder(untar_data(URLs.MNIST),
                                   train="training",
                                   valid="testing",
                                   num_workers=0)
learn = Learner(dls,
                SimpleConv(ArcFaceClassifier(3, 10)),
                metrics=accuracy,
                loss_func=arcface_loss)

# %%
learn.fit_one_cycle(5, 5e-3)

# %%


def get_embs(model, dl):
    embs = []
    ys = []