-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathNeptune_fastai_more_options.py
98 lines (84 loc) · 2.82 KB
/
Neptune_fastai_more_options.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import neptune
import torch
from fastai.callback.all import SaveModelCallback
from fastai.vision.all import (
ImageDataLoaders,
URLs,
accuracy,
resnet18,
untar_data,
vision_learner,
)
from neptune.integrations.fastai import NeptuneCallback
from neptune.types import File
run = neptune.init_run(
project="common/fastai-integration",
api_token=neptune.ANONYMOUS_API_TOKEN,
tags="more options",
)
path = untar_data(URLs.MNIST_TINY)
dls = ImageDataLoaders.from_csv(path, num_workers=0)
# Single & Multi phase logging
# 1. (Neptune) Log a single training phase
learn = vision_learner(dls, resnet18, metrics=accuracy)
learn.fit_one_cycle(1, cbs=[NeptuneCallback(run=run, base_namespace="experiment_1")])
learn.fit_one_cycle(2)
# 2. (Neptune) Log all training phases of the learner
learn = vision_learner(dls, resnet18, cbs=[NeptuneCallback(run=run, base_namespace="experiment_2")])
learn.fit_one_cycle(1)
# Log model weights
# Add SaveModelCallback
""" You can log your model weight files
during single training or all training phases
add SavemodelCallback() to the callbacks' list
of your learner or fit method."""
# 1.(Neptune) Log Every N epochs
n = 2
learn = vision_learner(
dls,
resnet18,
metrics=accuracy,
cbs=[
SaveModelCallback(every_epoch=n),
NeptuneCallback(run=run, base_namespace="experiment_3", upload_saved_models="all"),
],
)
learn.fit_one_cycle(5)
# 2. (Neptune) Best Model
learn = vision_learner(
dls,
resnet18,
metrics=accuracy,
cbs=[SaveModelCallback(), NeptuneCallback(run=run, base_namespace="experiment_4")],
)
learn.fit_one_cycle(5)
# 3. (Neptune) Pickling and logging the learner
""" Remove the NeptuneCallback class before pickling the learner object
to avoid errors due to pickle's inability to pickle local objects
(i.e., nested functions or methods)"""
pickled_learner = "learner.pkl"
base_namespace = "experiment_5"
neptune_cbk = NeptuneCallback(run=run, base_namespace=base_namespace)
learn = vision_learner(
dls,
resnet18,
metrics=accuracy,
cbs=[neptune_cbk],
)
learn.fit_one_cycle(1) # training
learn.remove_cb(neptune_cbk) # remove NeptuneCallback
learn.export(f"./{pickled_learner}") # export learner
run[f"{base_namespace}/pickled_learner"].upload(pickled_learner) # (Neptune) upload pickled learner
learn.add_cb(neptune_cbk) # add NeptuneCallback back again
learn.fit_one_cycle(1) # continue training
# (Neptune) Log images
batch = dls.one_batch()
for i, (x, y) in enumerate(dls.decode_batch(batch)):
# Neptune supports torch tensors
# fastai uses their own tensor type name TensorImage
# so you have to convert it back to torch.Tensor
run["images/one_batch"].append(
File.as_image(x.as_subclass(torch.Tensor).permute(2, 1, 0)),
name=f"{i}",
description=f"Label: {y}",
)