from fastai.vision import * # Quick access to computer vision functionality
Images can be in labeled folders, or a single folder with a CSV.
path = untar_data(URLs.MNIST_SAMPLE)
path
PosixPath('/home/ubuntu/.fastai/data/mnist_sample')
Create a DataBunch
, optionally with transforms:
data = ImageDataBunch.from_folder(path, ds_tfms=(rand_pad(2, 28), []), bs=64)
data.normalize(imagenet_stats)
img,label = data.train_ds[0]
img
Create and fit a Learner
:
learn = cnn_learner(data, models.resnet18, metrics=accuracy)
learn.fit_one_cycle(1, 0.01)
epoch | train_loss | valid_loss | accuracy | time |
---|---|---|---|---|
0 | 0.046350 | 0.008395 | 0.997547 | 00:06 |
accuracy(*learn.get_preds())
tensor(0.9975)
Same as above, using CSV instead of folder name for labels
data = ImageDataBunch.from_csv(path, ds_tfms=(rand_pad(2, 28), []), bs=64)
data.normalize(imagenet_stats)
img,label = data.train_ds[0]
img
learn = cnn_learner(data, models.resnet18, metrics=accuracy)
learn.fit_one_cycle(1, 0.01)
epoch | train_loss | valid_loss | accuracy | time |
---|---|---|---|---|
0 | 0.055503 | 0.007956 | 0.996881 | 00:05 |