-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtomato.py
224 lines (176 loc) · 6.49 KB
/
tomato.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
import pandas as pd
import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt
import os
import seaborn as sns
DATASET="train"
DATASET2="valid"
CATEGORIES=["Tomato___Bacterial_spot","Tomato___Early_blight","Tomato___healthy","Tomato___Late_blight","Tomato___Leaf_Mold","Tomato___Septoria_leaf_spot","Tomato___Spider_mites Two-spotted_spider_mite","Tomato___Target_Spot","Tomato___Tomato_mosaic_virus","Tomato___Tomato_Yellow_Leaf_Curl_Virus"]
train_data=[]
for category in CATEGORIES:
label=CATEGORIES.index(category)
path=os.path.join(DATASET,category)
for img_file in os.listdir(path):
img=cv.imread(os.path.join(path,img_file),1)
img=cv.cvtColor(img,cv.COLOR_BGR2RGB)
img=cv.resize(img,(64,64))
train_data.append([img,label])
test_data=[]
for category in CATEGORIES:
label=CATEGORIES.index(category)
path=os.path.join(DATASET2,category)
for img_file in os.listdir(path):
img=cv.imread(os.path.join(path,img_file),1)
img=cv.cvtColor(img,cv.COLOR_BGR2RGB)
img=cv.resize(img,(64,64))
test_data.append([img,label])
print(len(train_data))
print(len(test_data))
import random
random.shuffle(train_data)
random.shuffle(test_data)
for lbl in train_data[:10]:
print(lbl[1])
X_train=[]
y_train=[]
for features,label in train_data:
X_train.append(features)
y_train.append(label)
Y=[]
for i in y_train:
if i==0:
Y.append("BACTERIAL SPOT")
elif i==1:
Y.append("EARLY BLIGHT")
elif i==2:
Y.append("HEALTHY")
elif i==3:
Y.append("LATE BLIGHT")
elif i==4:
Y.append("LEAF MOLD")
elif i==5:
Y.append("SEPTORIA LEAF SPOT")
elif i==6:
Y.append("SPIDER MITE")
elif i==7:
Y.append("TARGET SPOT")
elif i==8:
Y.append("MOSAIC VIRUS")
else:
Y.append("YELLOW LEAF CURL VIRUS")
len(X_train),len(y_train)
X_test=[]
y_test=[]
for features,label in test_data:
X_test.append(features)
y_test.append(label)
Z=[]
for i in y_test:
if i==0:
Z.append("BACTERIAL SPOT")
elif i==1:
Z.append("EARLY BLIGHT")
elif i==2:
Z.append("HEALTHY")
elif i==3:
Z.append("LATE BLIGHT")
elif i==4:
Z.append("LEAF MOLD")
elif i==5:
Z.append("SEPTORIA LEAF SPOT")
elif i==6:
Z.append("SPIDER MITE")
elif i==7:
Z.append("TARGET SPOT")
elif i==8:
Z.append("MOSAIC VIRUS")
else:
Z.append("YELLOW LEAF CURL VIRUS")
len(X_test),len(y_test)
X_train=np.array(X_train).reshape(-1,64,64,3)
X_train=X_train/255.0
X_train.shape
X_test=np.array(X_test).reshape(-1,64,64,3)
X_test=X_test/255.0
X_test.shape
order=['BACTERIAL SPOT','EARLY BLIGHT','HEALTHY','LATE BLIGHT','LEAF MOLD','SEPTORIA LEAF SPOT','SPIDER MITE','TARGET SPOT','MOSAIC VIRUS','YELLOW LEAF CURL VIRUS']
ax=sns.countplot(Y, order=order)
ax.set_xlabel("Leaf Diseases")
ax.set_xticklabels(ax.get_xticklabels(), rotation=40, ha='right')
ax.set_ylabel("Image Count")
ax=sns.countplot(Z, order=order)
ax.set_xlabel("Leaf Diseases")
ax.set_xticklabels(ax.get_xticklabels(), rotation=40, ha='right')
ax.set_ylabel("Image Count")
from keras.utils import to_categorical
one_hot_train=to_categorical(y_train)
one_hot_train
one_hot_test=to_categorical(y_test)
one_hot_test
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D,Dense,Flatten,MaxPooling2D,Dropout
classifier=Sequential()
classifier.add(Conv2D(32,(3,3), input_shape=(64,64,3), activation='relu'))
classifier.add(MaxPooling2D(pool_size=(2,2)))
classifier.add(Dropout(0.2))
classifier.add(Conv2D(64,(3,3), activation='relu'))
classifier.add(MaxPooling2D(pool_size=(2,2)))
classifier.add(Dropout(0.2))
classifier.add(Conv2D(128,(3,3), activation='relu'))
classifier.add(MaxPooling2D(pool_size=(2,2)))
classifier.add(Dropout(0.4))
classifier.add(Flatten())
classifier.add(Dense(activation='relu', units=64))
classifier.add(Dense(activation='relu', units=128))
classifier.add(Dense(activation='relu', units=64))
classifier.add(Dense(activation='softmax', units=10))
classifier.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
classifier.summary()
hist=classifier.fit(X_train,one_hot_train,epochs=75,batch_size=128,validation_split=0.2)
test_loss,test_acc=classifier.evaluate(X_test,one_hot_test)
test_loss,test_acc
plt.plot(hist.history['loss'])
plt.plot(hist.history['val_loss'])
plt.title('Classifier Loss')
plt.ylabel('Loss')
plt.xlabel('Epoch')
plt.legend(['Train','Validation'],loc='upper right')
plt.show()
plt.plot(hist.history['accuracy'])
plt.plot(hist.history['val_accuracy'])
plt.title('Classifier Accuracy')
plt.ylabel('Accuracy')
plt.xlabel('Epoch')
plt.legend(['Train','Validation'],loc='upper left')
plt.show()
y_pred=classifier.predict_classes(X_test)
y_pred
y_prob=classifier.predict_proba(X_test)
y_prob
from sklearn.metrics import roc_curve, auc
fpr = {}
tpr = {}
thresh ={}
roc_auc={}
n_class = 10
for i in range(n_class):
fpr[i], tpr[i], thresh[i] = roc_curve(y_test, y_prob[:,i], pos_label=i)
roc_auc[i] = auc(fpr[i], tpr[i])
plt.plot(fpr[0], tpr[0], color='orange',label='Bacterial Spot AUC = %0.3f' % roc_auc[0])
plt.plot(fpr[1], tpr[1], color='green',label='Early Blight AUC = %0.3f' % roc_auc[1])
plt.plot(fpr[2], tpr[2], color='blue',label='Healthy AUC = %0.3f' % roc_auc[2])
plt.plot(fpr[3], tpr[3], color='red',label='Late Blight AUC = %0.3f' % roc_auc[3])
plt.plot(fpr[4], tpr[4], color='pink',label='Leaf Mold AUC = %0.3f' % roc_auc[4])
plt.plot(fpr[5], tpr[5], color='purple',label='Septoria Leaf Spot AUC = %0.3f' % roc_auc[5])
plt.plot(fpr[6], tpr[6], color='brown',label='Spider Mites AUC = %0.3f' % roc_auc[6])
plt.plot(fpr[7], tpr[7], color='cyan',label='Target Spot AUC = %0.3f' % roc_auc[7])
plt.plot(fpr[8], tpr[8], color='yellow',label='Mosaic Virus AUC = %0.3f' % roc_auc[8])
plt.plot(fpr[9], tpr[9], color='black',label='Yellow Leaf Curl Virus AUC = %0.3f' % roc_auc[9])
plt.title('Tomato Leaves Diseases ROC curve')
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive rate')
plt.legend(loc='best')
from sklearn.metrics import confusion_matrix
sns.heatmap(confusion_matrix(y_test,y_pred))
cm=confusion_matrix(y_test,y_pred)