from fastai.vision.all import *
from fastai.text.all import *
from fastai.tabular.all import *
from fastai.collab import *
from accelerate import notebook_launcher
from fastai.distributed import *
Notebook Launcher examples
Important
Before running, ensure that Accelerate has been configured through either accelerate config
in the command line or by running write_basic_config
# from accelerate.utils import write_basic_config
# write_basic_config()
Image Classification
= untar_data(URLs.PETS)/'images'
path
def train():
= ImageDataLoaders.from_name_func(
dls =0.2,
path, get_image_files(path), valid_pct=lambda x: x[0].isupper(), item_tfms=Resize(224))
label_func= vision_learner(dls, resnet34, metrics=error_rate).to_fp16()
learn with learn.distrib_ctx(in_notebook=True, sync_bn=False):
1)
learn.fine_tune(
=2) notebook_launcher(train, num_processes
Launching training on 2 GPUs.
Training Learner...
epoch | train_loss | valid_loss | error_rate | time |
---|---|---|---|---|
0 | 0.258557 | 0.024234 | 0.008119 | 00:13 |
epoch | train_loss | valid_loss | error_rate | time |
---|---|---|---|---|
0 | 0.039532 | 0.019273 | 0.005413 | 00:15 |
Image Segmentation
= untar_data(URLs.CAMVID_TINY)
path
def train():
= SegmentationDataLoaders.from_label_func(
dls =8, fnames = get_image_files(path/"images"),
path, bs= lambda o: path/'labels'/f'{o.stem}_P{o.suffix}',
label_func = np.loadtxt(path/'codes.txt', dtype=str)
codes
)= unet_learner(dls, resnet34)
learn with learn.distrib_ctx(in_notebook=True, sync_bn=False):
8)
learn.fine_tune(
=2) notebook_launcher(train, num_processes
Launching training on 2 GPUs.
/opt/conda/lib/python3.7/site-packages/torch/_tensor.py:1142: UserWarning: __floordiv__ is deprecated, and its behavior will change in a future version of pytorch. It currently rounds toward 0 (like the 'trunc' function NOT 'floor'). This results in incorrect rounding for negative values. To keep the current behavior, use torch.div(a, b, rounding_mode='trunc'), or for actual floor division, use torch.div(a, b, rounding_mode='floor').
ret = func(*args, **kwargs)
/opt/conda/lib/python3.7/site-packages/torch/_tensor.py:1142: UserWarning: __floordiv__ is deprecated, and its behavior will change in a future version of pytorch. It currently rounds toward 0 (like the 'trunc' function NOT 'floor'). This results in incorrect rounding for negative values. To keep the current behavior, use torch.div(a, b, rounding_mode='trunc'), or for actual floor division, use torch.div(a, b, rounding_mode='floor').
ret = func(*args, **kwargs)
Training Learner...
epoch | train_loss | valid_loss | time |
---|---|---|---|
0 | 4.339367 | 2.756470 | 00:03 |
epoch | train_loss | valid_loss | time |
---|---|---|---|
0 | 2.405208 | 2.095044 | 00:02 |
1 | 2.113619 | 1.692979 | 00:02 |
2 | 1.931254 | 1.333691 | 00:02 |
3 | 1.753757 | 1.187579 | 00:02 |
4 | 1.612463 | 1.097649 | 00:02 |
5 | 1.493950 | 0.992424 | 00:02 |
6 | 1.393139 | 0.949843 | 00:02 |
7 | 1.312021 | 0.942510 | 00:02 |
Text Classification
= untar_data(URLs.IMDB_SAMPLE)
path = pd.read_csv(path/'texts.csv')
df
def train():
= DataBlock(blocks=(TextBlock.from_df('text', seq_len=72), CategoryBlock),
imdb_clas =ColReader('text'), get_y=ColReader('label'), splitter=ColSplitter())
get_x= imdb_clas.dataloaders(df, bs=64)
dls = rank0_first(lambda: text_classifier_learner(dls, AWD_LSTM, drop_mult=0.5, metrics=accuracy))
learn with learn.distrib_ctx(in_notebook=True):
4, 1e-2)
learn.fine_tune(
=2) notebook_launcher(train, num_processes
Launching training on 2 GPUs.
Training Learner...
epoch | train_loss | valid_loss | accuracy | time |
---|---|---|---|---|
0 | 0.683830 | 0.640674 | 0.710000 | 00:06 |
epoch | train_loss | valid_loss | accuracy | time |
---|---|---|---|---|
0 | 0.505055 | 0.618315 | 0.650000 | 00:10 |
1 | 0.433232 | 0.522627 | 0.785000 | 00:11 |
2 | 0.391711 | 0.460229 | 0.810000 | 00:11 |
3 | 0.347983 | 0.450882 | 0.805000 | 00:11 |
Tabular
= untar_data(URLs.ADULT_SAMPLE)
path = pd.read_csv(path/'adult.csv')
df
def train():
= TabularDataLoaders.from_csv(path/'adult.csv', path=path, y_names="salary",
dls = ['workclass', 'education', 'marital-status', 'occupation',
cat_names 'relationship', 'race'],
= ['age', 'fnlwgt', 'education-num'],
cont_names = [Categorify, FillMissing, Normalize])
procs
= tabular_learner(dls, metrics=accuracy)
learn with learn.distrib_ctx(in_notebook=True):
3)
learn.fit_one_cycle(
=2) notebook_launcher(train, num_processes
Launching training on 2 GPUs.
Training Learner...
epoch | train_loss | valid_loss | accuracy | time |
---|---|---|---|---|
0 | 0.436493 | 0.383866 | 0.832463 | 00:03 |
1 | 0.359663 | 0.352825 | 0.837224 | 00:02 |
2 | 0.349231 | 0.350312 | 0.839988 | 00:02 |
Collab Filtering
= untar_data(URLs.ML_SAMPLE)
path = pd.read_csv(path/'ratings.csv')
df
def train():
= CollabDataLoaders.from_df(df)
dls = collab_learner(dls, y_range=(0.5,5.5))
learn with learn.distrib_ctx(in_notebook=True):
6)
learn.fine_tune(
=2) notebook_launcher(train, num_processes
Launching training on 2 GPUs.
Training Learner...
epoch | train_loss | valid_loss | time |
---|---|---|---|
0 | 1.553747 | 1.430443 | 00:00 |
epoch | train_loss | valid_loss | time |
---|---|---|---|
0 | 1.484851 | 1.394805 | 00:00 |
1 | 1.424410 | 1.255329 | 00:00 |
2 | 1.277911 | 1.028214 | 00:00 |
3 | 1.099660 | 0.882485 | 00:00 |
4 | 0.969005 | 0.835191 | 00:00 |
5 | 0.894699 | 0.828167 | 00:00 |
Keypoints
= untar_data(URLs.BIWI_HEAD_POSE)
path def img2pose(x): return Path(f'{str(x)[:-7]}pose.txt')
def get_ctr(f):
= np.genfromtxt(img2pose(f), skip_header=3)
ctr = ctr[0] * cal[0][0]/ctr[2] + cal[0][2]
c1 = ctr[1] * cal[1][1]/ctr[2] + cal[1][2]
c2 return tensor([c1,c2])
= get_image_files(path)
img_files = np.genfromtxt(path/'01'/'rgb.cal', skip_footer=6)
cal
def train():
= DataBlock(
biwi =(ImageBlock, PointBlock),
blocks=get_image_files,
get_items=get_ctr,
get_y=FuncSplitter(lambda o: o.parent.name=='13'),
splitter=[*aug_transforms(size=(240,320)),
batch_tfms*imagenet_stats)])
Normalize.from_stats(= biwi.dataloaders(path)
dls = vision_learner(dls, resnet18, y_range=(-1,1))
learn with learn.distrib_ctx(in_notebook=True, sync_bn=False):
1)
learn.fine_tune(
=2) notebook_launcher(train, num_processes
Launching training on 2 GPUs.
Training Learner...
epoch | train_loss | valid_loss | time |
---|---|---|---|
0 | 0.247702 | 0.066427 | 00:47 |
epoch | train_loss | valid_loss | time |
---|---|---|---|
0 | 0.052143 | 0.007451 | 00:55 |