-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuntitled0.py
80 lines (54 loc) · 1.63 KB
/
untitled0.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
# -*- coding: utf-8 -*-
"""Untitled0.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1pK6Uua94RzuOthjwcAt95ugWmuI1WAeN
"""
# Commented out IPython magic to ensure Python compatibility.
# %reload_ext autoreload
# %autoreload 2
# %matplotlib inline
import torch
import torchvision
from fastai import*
from fastai.vision import *
from fastai.metrics import error_rate
folder = 'black'
file = 'black_cat.csv'
path = Path('data')
dest = path/folder
dest.mkdir(parents=True, exist_ok=True)
folder = 'white'
file = 'white_cat.csv'
path = Path('data')
dest = path/folder
dest.mkdir(parents=True, exist_ok=True)
folder = 'grey'
file = 'grey_cat.csv'
path = Path('data')
dest = path/folder
dest.mkdir(parents=True, exist_ok=True)
path.ls()
path = Path('data/')
classes = ['black','white','grey']
download_images(path/file, dest, max_pics=200)
for c in classes:
print(c)
verify_images(path/c, delete=True, max_size=500)
np.random.seed(42)
data = ImageDataBunch.from_folder(path, train=".", valid_pct=0.2,
ds_tfms=get_transforms(), size=224, num_workers=4).normalize(imagenet_stats)
data.classes
data.show_batch(rows=3, figsize=(7,8))
data.classes, data.c, len(data.train_ds), len(data.valid_ds)
learn = cnn_learner(data, models.resnet34, metrics=error_rate)
learn.fit_one_cycle(4)
learn.save('stage-1')
learn.unfreeze()
learn.lr_find(start_lr=1e-5, end_lr=1e-1)
learn.fit_one_cycle(2, max_lr=slice(3e-5,3e-4))
learn.recorder.plot()
learn.save('stage-2')
learn.load('stage-2');
interp = ClassificationInterpretation.from_learner(learn)
interp.plot_confusion_matrix()