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]
Esempio n. 2
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
    writer.add_figure('Sample results', fig, step)


# ## Test


# ## TensorBoardCallback

# +
path = untar_data(URLs.PETS)

db = DataBlock(blocks=(ImageBlock, CategoryBlock),
               get_items=get_image_files,
               item_tfms=Resize(128),
               splitter=RandomSubsetSplitter(train_sz=0.1, valid_sz=0.01),
               batch_tfms=aug_transforms(size=64),
               get_y=using_attr(RegexLabeller(r'(.+)_\d+.*$'), 'name'))

dls = db.dataloaders(path / 'images')
# -

learn = cnn_learner(dls, resnet18, metrics=accuracy)

learn.unfreeze()
learn.fit_one_cycle(3, cbs=TensorBoardCallback(Path.home() / 'tmp' / 'runs' / 'tb', trace_model=True))

# ## Projector

# ### Projector in TensorBoardCallback

path = untar_data(URLs.PETS)
def get_y_from_df(fname):
    """
    根据名字从df中找到label
    :param fname: 某张图像的绝对路径
    :return: csv中对称性
    """
    fname = path.basename(fname)
    return df[df.img_name==fname]['label'].values[0]

data_block_512 = DataBlock(blocks = (ImageBlock, CategoryBlock, RegressionBlock),
                       get_items  = get_image_files,
                       get_y      = [get_y_from_name, get_y_from_df],
                       n_inp      = 1,
                       item_tfms  = Resize(512),
                       batch_tfms = aug_transforms(batch=True)
)

data_block_224 = DataBlock(blocks = (ImageBlock, CategoryBlock, RegressionBlock),
                       get_items  = get_image_files,
                       get_y      = [get_y_from_name, get_y_from_df],
                       n_inp      = 1,
                       item_tfms  = Resize(224),
                       batch_tfms = aug_transforms(batch=True)
)

data_block_312 = DataBlock(blocks = (ImageBlock, CategoryBlock, RegressionBlock),
                       get_items  = get_image_files,
                       get_y      = [get_y_from_name, get_y_from_df],
                       n_inp      = 1,
                       item_tfms  = [Resize(312)],