import fastai
from fastai import * # Quick access to most common functionality
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('/data1/jhoward/git/fastai/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 = ConvLearner(data, models.resnet18, metrics=accuracy)
learn.fit_one_cycle(1, 0.01)
VBox(children=(HBox(children=(IntProgress(value=0, max=1), HTML(value='0.00% [0/1 00:00<00:00]'))), HTML(value…
Total time: 00:04 epoch train loss valid loss accuracy 0 0.053222 0.013478 0.996565 (00:04)
accuracy(*learn.get_preds())
HBox(children=(IntProgress(value=0, max=16), HTML(value='')))
HBox(children=(IntProgress(value=0, max=16), HTML(value='0.00% [0/16 00:00<00:00]')))
tensor(0.9966)
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 = ConvLearner(data, models.resnet18, metrics=accuracy)
learn.fit_one_cycle(1, 0.01)
VBox(children=(HBox(children=(IntProgress(value=0, max=1), HTML(value='0.00% [0/1 00:00<00:00]'))), HTML(value…
Total time: 00:06 epoch train loss valid loss accuracy 0 0.047875 0.014735 0.995466 (00:06)