Deep Learning for Coder: Chapter 5

Show/Hide the code
1
2
import fastbook
fastbook.setup_book()
Show/Hide the code
1
2
from fastai.vision.all import *
path = untar_data(URLs.PETS)
Show/Hide the code
1
path.ls()
(#2) [Path('/root/.fastai/data/oxford-iiit-pet/annotations'),Path('/root/.fastai/data/oxford-iiit-pet/images')]
Show/Hide the code
1
2
3
4
5
6
7
8
9
pets = DataBlock(
    blocks=(ImageBlock, CategoryBlock),
    get_items=get_image_files,
    splitter=RandomSplitter(seed=42),
    get_y=using_attr(RegexLabeller(r"(.+)_\d+.jpg$"), "name"),
    item_tfms=Resize(460),
    batch_tfms=aug_transforms(size=224, min_scale=0.75),
)
dls = pets.dataloaders(path/"images")
Show/Hide the code
1
dls.show_batch(nrows=3, ncols=3)

Show/Hide the code
1
2
learn = cnn_learner(dls, resnet34, metrics=error_rate)
learn.fine_tune(2)
Downloading: "https://download.pytorch.org/models/resnet34-b627a593.pth" to /root/.cache/torch/hub/checkpoints/resnet34-b627a593.pth
epochtrain_lossvalid_losserror_ratetime
01.4978840.3026660.08728001:02
epochtrain_lossvalid_losserror_ratetime
00.4946300.3093100.09945901:04
10.3106870.2540760.08457401:06
Show/Hide the code
1
x, y = dls.one_batch()
Show/Hide the code
1
2
preds, _ = learn.get_preds(dl = [(x,y)])
torch.argmax(preds[0])
tensor(10)
Show/Hide the code
1
2
acts = torch.randn((6,2))*2
acts
tensor([[ 0.2126, -0.6733],
        [ 0.5868,  1.8977],
        [-1.5544,  0.4964],
        [-0.5226, -0.6044],
        [ 0.4885, -0.1626],
        [-3.0538, -2.0372]])
Show/Hide the code
1
acts.sigmoid().sum(1).unsqueeze(1)
tensor([[0.8907],
        [1.5123],
        [0.7961],
        [0.7256],
        [1.0792],
        [0.1604]])
Show/Hide the code
1
(acts[:,0]-acts[:,1]).sigmoid().unsqueeze(1)
tensor([[0.7080],
        [0.2123],
        [0.1140],
        [0.5204],
        [0.6573],
        [0.2657]])
Show/Hide the code
1
2
def softmax(x): return torch.exp(x) / torch.exp(x).sum(dim=1, keepdim=True)
sm_acts = softmax(acts)
Show/Hide the code
1
sm_acts
tensor([[0.7080, 0.2920],
        [0.2123, 0.7877],
        [0.1140, 0.8860],
        [0.5204, 0.4796],
        [0.6573, 0.3427],
        [0.2657, 0.7343]])
Show/Hide the code
1
2
targ = tensor([0,1,0,0,1,1])
sm_acts[range(6),targ].unsqueeze(1)
tensor([[0.7080],
        [0.7877],
        [0.1140],
        [0.5204],
        [0.3427],
        [0.7343]])
Show/Hide the code
1
-sm_acts[range(6),targ].log().mean()
tensor(0.7981)
Show/Hide the code
1
F.nll_loss(acts.log_softmax(dim=1), targ)
tensor(0.7981)
Show/Hide the code
1
2
loss_func = nn.CrossEntropyLoss(reduction='none')
loss_func(acts, targ)
tensor([0.3452, 0.2387, 2.1718, 0.6531, 1.0708, 0.3088])
Show/Hide the code
1
2
interp  = ClassificationInterpretation.from_learner(learn)
interp.plot_confusion_matrix(figsize=(12,12), dpi=60)

Show/Hide the code
1
interp.most_confused(min_val=5)
[('american_pit_bull_terrier', 'staffordshire_bull_terrier', np.int64(12)),
 ('Birman', 'Ragdoll', np.int64(7)),
 ('Egyptian_Mau', 'Bengal', np.int64(5)),
 ('american_pit_bull_terrier', 'american_bulldog', np.int64(5))]
Show/Hide the code
1
2
learn = vision_learner(dls, resnet34, metrics=error_rate)
learn.lr_find()
SuggestedLRs(valley=0.001737800776027143)

Show/Hide the code
1
2
learn = vision_learner(dls, resnet34, metrics=error_rate)
learn.fine_tune(2, base_lr=3e-3)
epochtrain_lossvalid_losserror_ratetime
01.3309290.3050420.08998601:07
epochtrain_lossvalid_losserror_ratetime
00.5234400.3624860.10487101:10
10.3271840.2691030.08051401:06
Show/Hide the code
1
2
learn = vision_learner(dls, resnet34, metrics=error_rate)
learn.fit_one_cycle(3, 3e-2)
epochtrain_lossvalid_losserror_ratetime
02.3571523.6955680.57848400:12
11.1888800.5419150.16982400:11
20.6602040.3588330.10893100:11
Show/Hide the code
1
learn.unfreeze()
Show/Hide the code
1
learn.lr_find()
SuggestedLRs(valley=3.630780702224001e-05)

Show/Hide the code
1
learn.fit_one_cycle(6,  lr_max=1e-6)
epochtrain_lossvalid_losserror_ratetime
00.4745400.3567420.10893100:12
10.4728790.3532830.11028400:12
20.4715760.3470570.10351800:12
30.4643390.3453620.10081200:12
40.4689300.3427620.10622500:12
50.4526380.3435950.10351800:12
Show/Hide the code
1
2
3
4
learn = vision_learner(dls, resnet34, metrics=error_rate)
learn.fit_one_cycle(3, 3e-3)
learn.unfreeze()
learn.fit_one_cycle(12, lr_max=slice(1e-6, 1e-4))
epochtrain_lossvalid_losserror_ratetime
01.1271020.3348920.09742900:11
10.5189090.2523940.08592700:11
20.3279800.2186560.07577800:11
epochtrain_lossvalid_losserror_ratetime
00.2434550.2132500.07442500:12
10.2331220.2121310.07374800:12
20.2200110.2028110.06359900:12
30.2203710.1963180.06292300:12
40.1911080.2003520.06495300:12
50.1666880.2015820.06698200:12
60.1558570.1966640.06292300:12
70.1387410.1903740.06021700:12
80.1181070.1926740.06765900:12
90.1309650.1917350.06562900:12
100.1249630.1918180.06698200:12
110.1224790.1911970.06427600:12
Show/Hide the code
1
learn.recorder.plot_loss()

Show/Hide the code
1
2
3
from fastai.callback.fp16 import *
learn =  vision_learner(dls, resnet50, metrics=error_rate).to_fp16()
learn.fine_tune(6, freeze_epochs=3)
Downloading: "https://download.pytorch.org/models/resnet50-11ad3fa6.pth" to /root/.cache/torch/hub/checkpoints/resnet50-11ad3fa6.pth
epochtrain_lossvalid_losserror_ratetime
02.1911120.4755550.15426300:14
10.8299240.2940440.10081200:13
20.5500200.2911300.09675200:13
epochtrain_lossvalid_losserror_ratetime
00.3056280.2366690.07374800:14
10.2420620.2780920.08186700:14
20.2093510.2318920.06968900:14
30.1333530.2204060.06427600:14
40.0891820.2024630.05074400:14
50.0659770.1921520.05277400:14
使用 Hugo 构建
主题 StackJimmy 设计