-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfruitclassifier.py
More file actions
59 lines (36 loc) · 1.28 KB
/
fruitclassifier.py
File metadata and controls
59 lines (36 loc) · 1.28 KB
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
# -*- coding: utf-8 -*-(open in google colab or jupyter notebook)
"""fruitclassifier.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/16-dEdH40cf424lABQebMHv4ipd0mK0Q4
"""
from fastai.vision import *
from fastai import *
from imutils import paths
!pip install imutils
from google.colab import drive
drive.mount('/content/drive')
classes=['apples','mango','grapes','orange']
path = Path('/content/drive/My Drive/classification')
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(),bs=2,size=224,num_workers=5).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=accuracy)
learn.fit_one_cycle(6)
learn.save('stage-1')
learn.unfreeze()
learn.fit_one_cycle(10)
learn.load('stage-1');
interp = ClassificationInterpretation.from_learner(learn)
interp.plot_confusion_matrix()
learn.export()
learn = load_learner(path)
img = open_image('/content/drive/My Drive/classification/appletest.jpg')
img
pred_class,pred_idx,outputs = learn.predict(img)
pred_class