-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclassificador_de_arvores7.py
312 lines (215 loc) · 6.7 KB
/
classificador_de_arvores7.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
# -*- coding: utf-8 -*-
"""Classificador de Arvores7.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1wZheJFjKiqh9ZzLdiamrVYvHNm7SSexB
# **0-NOTEBOOK CONFIG**
INICIO DO EM CASO DE ERRO
"""
!pip install --quiet --pre torchvision -f https://download.pytorch.org/whl/nightly/cpu/torch_nightly.html
import torch
import fastai
!pip install fastai==2.0.15
!pip install fastai2==0.0.30
!pip install fastcore==1.0.16
"""FIM DO EM CASO DE ERRO
CONFIG NORMAL
"""
!pip install fastai --upgrade -q
from fastai.vision.all import *
from fastai.vision.widgets import *
"""# **1-DATASET**
"""
from google.colab import drive
drive.mount('/content/gdrive', force_remount=True)
dir = 'gdrive/My Drive/Dataset/OK'
path=Path(dir)
from google.colab import drive
drive.mount('/content/gdrive', force_remount=True)
root_dir = 'gdrive/My Drive/'
global dir
dir = root_dir + 'Dataset/OK'
path=Path(dir)
!ls "/content/gdrive/My Drive/Dataset/OK"
!ls "/content/gdrive/My Drive/Dataset/Teste"
listy = path.ls()
total = 0
for i in listy:
if os.path.isdir(i):
print("there are {0} files in {1}!".format(len(os.listdir(i)),i))
total +=len(os.listdir(i))
print("there are {} files in total!".format(total))
os.listdir(path)
from PIL import Image
import os
import progressbar
global dir
def thingy(path):
if path == "":
path = dir + path
listy = os.listdir(path)
for file in progressbar.progressbar(listy):
if os.path.isdir(path+"/"+file):
thingy(path+"/"+file)
elif not file.split(".")[1] == "jpg":
Image.open(path + "/" + file).convert('RGB').save("{0}/{1}.jpg".format(path,file.split(".")[0]))
thingy("")
get_files(path)
data = None #limpar var pra rodar de novo
data = get_image_files(path)
data[164]
splitter = RandomSplitter(valid_pct=0.2, seed=23) #altere a semente ao rodar novamente
splitter(data) #133 treino 33 validação
item_tfms = [Resize(456, method='crop')]
batch_tfms=[*aug_transforms(size=224), Normalize.from_stats(*imagenet_stats)] #,,Dihedral(p=1)
blocks=(ImageBlock, CategoryBlock)
get_image_files(path)
trees = DataBlock(blocks=blocks,
get_items=get_image_files, #equivalente ao get_x
splitter=splitter,
get_y=parent_label,
item_tfms=item_tfms,
batch_tfms=batch_tfms)
trees.summary(path, show_batch=True)
dls = trees.dataloaders(path)
dls.vocab
"""Mostrar os resultados de diversos transforms da mesma imagem"""
dls.train.show_batch(max_n=7, nrows=1, unique=True)
dls.show_batch(max_n=5)
"""---
# **2-TREINAMENTO DO MODELO - TRANSFER LEARNING**
"""
from fastai.metrics import error_rate
from fastai.callback import *
defaults.callbacks[1] = Recorder(train_metrics=True)
defaults.callbacks
cbs=[ShowGraphCallback,ActivationStats(with_hist=True),SaveModelCallback]
learn = None #clear na variavel
learn = cnn_learner(dls,resnet18,metrics=(error_rate, accuracy),cbs=cbs) #resnet34
"""fastai.vision.models)
you get
['BasicBlock', 'Darknet', 'DynamicUnet', 'ResLayer', 'ResNet', 'SqueezeNet',
'UnetBlock', 'WideResNet', 'XResNet',
'alexnet', 'darknet', 'densenet121', 'densenet161', 'densenet169',
'densenet201', 'resnet101', 'resnet152', 'resnet18', 'resnet34', 'resnet50',
'squeezenet1_0', 'squeezenet1_1', 'unet', 'vgg16_bn', 'vgg19_bn', 'wrn',
'wrn_22', 'xception', 'xresnet', 'xresnet101', 'xresnet152', 'xresnet18',
'xresnet34', 'xresnet50']
fastai.vision.models)
you get
['BasicBlock', 'Darknet', 'DynamicUnet', 'ResLayer', 'ResNet', 'SqueezeNet',
'UnetBlock', 'WideResNet', 'XResNet',
'alexnet', 'darknet', 'densenet121', 'densenet161', 'densenet169',
'densenet201', 'resnet101', 'resnet152', 'resnet18', 'resnet34', 'resnet50',
'squeezenet1_0', 'squeezenet1_1', 'unet', 'vgg16_bn', 'vgg19_bn', 'wrn',
'wrn_22', 'xception', 'xresnet', 'xresnet101', 'xresnet152', 'xresnet18',
'xresnet34', 'xresnet50']
"""
learn.show_training_loop()
list(learn.cbs)
"""##2.1 - TREINO PADRAO FIT_ONE_CYCLE"""
learn.fit_one_cycle(30,1e-2)
"""fastai.vision.models)
you get
['BasicBlock', 'Darknet', 'DynamicUnet', 'ResLayer', 'ResNet', 'SqueezeNet',
'UnetBlock', 'WideResNet', 'XResNet',
'alexnet', 'darknet', 'densenet121', 'densenet161', 'densenet169',
'densenet201', 'resnet101', 'resnet152', 'resnet18', 'resnet34', 'resnet50',
'squeezenet1_0', 'squeezenet1_1', 'unet', 'vgg16_bn', 'vgg19_bn', 'wrn',
'wrn_22', 'xception', 'xresnet', 'xresnet101', 'xresnet152', 'xresnet18',
'xresnet34', 'xresnet50']
"""
learn.save('model1')
"""## 2.2 - INICIO TREINO OTIMIZADO (FINE TUNNING) - FIT_ONE_CYCLE + SLICE DE FAIXA"""
learn.load('model1')
learn.lr_find()
learn.recorder.plot_lr_find()
learn.save('model1')
learn.unfreeze()
max_lr=slice(1e-4, 1e-3)
learn.fit_one_cycle(15,max_lr)
"""FIM DO ESTAGIO DE OTIMIZAÇÃO"""
learn.save('model1')
"""##2.3 - AVALIAÇÃO DO MODELO - TRANSFER LEARNING"""
dls.vocab
learn.load('model1')
interp = ClassificationInterpretation.from_learner(learn)
interp.plot_confusion_matrix() #33 da validacao
learn.show_results()
interp.most_confused(min_val=1)
interp.plot_top_losses(4, nrows=2, figsize = (25,5))
learn.save('model1')
"""
# **3- TESTE - ANALISE DO TESTE**"""
from fastai.vision import *
learn.load('model1')
list = None
list2 = None
import os
x = [os.path.join(r,file) for r,d,f in os.walk("/content/gdrive/My Drive/Dataset/Teste") for file in f]
print(x)
len(x)
list = x[0:len(x)]
len(list)
list2 = sorted(list,reverse=True)
len(list2)
pred_class = []
for x in range (0,len(list2)):
print(x, 'OK')
pred_class.append(learn.predict(list2[x]))
print(pred_class[1])
print(pred_class[2])
print(pred_class[18])
print(pred_class[19])
print(pred_class[20])
print(pred_class[21])
print(list2[1])
print(list2[2])
print(list2[18])
print(list2[19])
print(list2[20])
print(list2[21])
list2[4]
pred_class[4][0]
folder = []
for x in range (0,len(list2)):
print(x,'OK')
folder.append(os.path.basename(os.path.dirname(list2[x])))
folder[4]
if folder[4] == pred_class[4][0]:
print('acertou')
else:
print('diferente')
cor = 0
err = 0
for x in range(0,len(list2)):
if folder[x] == pred_class[x][0]:
print('acertou')
cor +=1
else:
print('errou')
err +=1
print(cor,err)
acc = (cor/len(list2))*100
print(acc)
predlst = []
labelst = []
for x in range (0,len(list2)):
predlst.append(pred_class[x][0])
labelst.append(folder[x])
from sklearn.metrics import accuracy_score
print(accuracy_score(labelst,predlst))
from sklearn.metrics import confusion_matrix
cm = confusion_matrix(labelst, predlst)
cm
import matplotlib.pyplot as plt
plt.matshow(cm)
plt.title('Confusion matrix')
plt.colorbar()
plt.ylabel('True label')
plt.xlabel('Predicted label')
plt.show()
learn.export()
list2
folder
pred_class