-
Notifications
You must be signed in to change notification settings - Fork 0
/
CHARROUD ANAS 2.py
462 lines (280 loc) · 9.82 KB
/
CHARROUD ANAS 2.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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
#!/usr/bin/env python
# coding: utf-8
# In[502]:
import numpy as np
import cv2
import os
import pandas as pd
import itertools
import mahotas as mh
import pickle
import requests,json
from sklearn.metrics import accuracy_score
from sklearn.metrics import classification_report
from sklearn.ensemble import RandomForestClassifier
from sklearn.svm import SVC
from sklearn.neighbors import KNeighborsClassifier
from sklearn.naive_bayes import MultinomialNB
from sklearn.tree import DecisionTreeClassifier
from sklearn.decomposition import PCA
import skimage.segmentation as seg
import skimage.color as color
from sklearn.feature_selection import SelectKBest
from sklearn.feature_selection import chi2
from sklearn.preprocessing import MinMaxScaler
import skimage
from skimage.feature import greycomatrix, greycoprops
from sklearn.ensemble import ExtraTreesClassifier
from sklearn.feature_selection import SelectFromModel
import csv
csv.register_dialect('myDialect',quoting=csv.QUOTE_ALL,skipinitialspace=True)
from skimage.filters import prewitt_h,prewitt_v
from sklearn import preprocessing
# In[503]:
def histogram(image, mask):
#extraire l'histogramme 3D de la region masqué d'image
hist1 = cv2.calcHist([image], [0, 1, 2], mask, [8,8,8],[0, 180, 0, 256, 0, 256])
hist = cv2.normalize(hist1, hist1).flatten()
# retourner l'histogramme
return hist
def describe(image):
# convertire l image de RVB ====> HSV
image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
vectDescripteur = []
# calculer le centre d'image
(h, w) = image.shape[:2]
(cX, cY) = (int(w * 0.5), int(h * 0.5))
#diviser l'image en 4 rectangle
segments = [(0, cX, 0, cY), (cX, w, 0, cY), (cX, w, cY, h),
(0, cX, cY, h)]
# boucler sur le segment
for (startX, endX, startY, endY) in segments:
# construire un masque pour chaque coin de l'image
cornerMask = np.zeros(image.shape[:2], dtype = "uint8")
cv2.rectangle(cornerMask, (startX, startY), (endX, endY), 255, -1)
#extraire l'histogramme d'image et modifier le vecteur descripteur
hist = histogram(image, cornerMask)
vectDescripteur.extend(hist)
# retourner le vecteur descripteur
return vectDescripteur
def textureFeatures(img):
img = color.rgb2gray(img)
img = skimage.img_as_ubyte(img)
glcm = greycomatrix(img, [1], [0], 256, symmetric=True, normed=True)
feature = greycoprops(glcm, 'dissimilarity')[0]
feature = np.concatenate([feature,greycoprops(glcm, 'correlation')[0]])
feature = np.concatenate([feature,greycoprops(glcm, 'contrast')[0]])
feature = np.concatenate([feature,greycoprops(glcm, 'energy')[0]])
feature = feature/np.sum(feature)
#print(feature)
return feature
# In[504]:
#ExtraTreesClassifier
def features_normal2(features,y):
etc = ExtraTreesClassifier(n_estimators=10)
kk=etc.fit(features,y)
ettc = SelectFromModel(etc, prefit=True)
features= ettc.transform(features)
return features
#pca
def features_normal(features):
scaler=MinMaxScaler(feature_range=(0,1))
pca = PCA(n_components=13)
feat=features.astype('float')
feat=scaler.fit_transform(feat)
features=pca.fit_transform(feat)
return features
#SelectKBest
def features_normal1(features,y):
#SelectPercentile
#GenericUnivariateSelect d'autre test de reduction de nombre des features
bestfeatures = SelectKBest(score_func=chi2, k=100)
fit = bestfeatures.fit(abs(features),y)
features=fit.transform(features )
return features
# In[505]:
names=[name for name in os.listdir("ISIC-2017_Training_Data")]
names = np.array(names)
names1=[name for name in os.listdir("ISIC-2017_Training_Part1_GroundTruth")]
names1 = np.array(names)
# In[506]:
names1[0]
# In[507]:
a=cv2.imread(os.path.join('ISIC-2017_Training_Data\\'+ names1[10]))
type(a)
# In[510]:
def load_features_from_folder_for_train():
i=0
for j in range(2000):
img = cv2.imread(os.path.join('ISIC-2017_Training_Data\\'+ names[j]))
mask1= cv2.imread(os.path.join('ISIC-2017_Training_Part1_GroundTruth\\'+ names[j]),0)
img1= cv2.bitwise_and(img,img,mask=mask1)
#mean,std,6
(means, stds) = cv2.meanStdDev(img)
mean_std=np.array(list(zip(means, stds))).flatten()
#shape
gray = cv2.cvtColor(np.uint8(img1), cv2.COLOR_BGR2GRAY)
shape=cv2.HuMoments(cv2.moments(gray)).flatten()
shape = -np.sign(shape) * np.log10(np.abs(shape))
#histogram
hist =describe(img)
#texture
texture =textureFeatures(img)
#feature=np.hstack((texture,shape,hist))
#feature=np.hstack((texture))
feature=np.hstack((hist))
if i==0:
features = np.zeros(feature.shape[0])
i=i+1
features = np.vstack((features,feature))
return np.delete(features, (0), axis=0)
# In[509]:
feat_x=load_features_from_folder_for_train()
# In[472]:
feat_x.shape
# In[511]:
df = pd.read_csv('ISIC-2017_Training_Part3_GroundTruth.csv',header=None)
# In[512]:
aa=df[1][:]
# In[513]:
aa=np.array(aa)
aa=np.delete(aa, (0), axis=0)
# In[514]:
s=list(aa)
# In[515]:
names2=[name for name in os.listdir("ISIC-2017_Validation_Data")]
names2 = np.array(names2)
names3=[name for name in os.listdir("ISIC-2017_Validation_Part1_GroundTruth")]
names3 = np.array(names3)
# In[516]:
a=cv2.imread(os.path.join('ISIC-2017_Validation_Part1_GroundTruth\\'+ names3[0] ), 0)
type(a)
# In[ ]:
# In[517]:
def load_features_from_folder_for_validation():
i=0
for j in range(150):
img = cv2.imread(os.path.join('ISIC-2017_Validation_Data\\'+ names2[j]))
mask2= cv2.imread(os.path.join('ISIC-2017_Validation_Part1_GroundTruth\\'+ names3[j] ),0)
img1= cv2.bitwise_and(img,img,mask=mask2)
#img = segmentation(img)
#mean,std,6
(means, stds) = cv2.meanStdDev(img)
mean_std=np.array(list(zip(means, stds))).flatten()
#shape
gray = cv2.cvtColor(np.uint8(img1), cv2.COLOR_BGR2GRAY)
shape=cv2.HuMoments(cv2.moments(gray)).flatten()
shape = -np.sign(shape) * np.log10(np.abs(shape))
#histogram
hist =describe(img)
#texture
texture =textureFeatures(img)
#feature=np.hstack((texture,shape,hist))
#feature=np.hstack((texture))
feature=np.hstack((hist))
if i==0:
features = np.zeros(feature.shape[0])
i=i+1
features = np.vstack((features,feature))
return np.delete(features, (0), axis=0)
# In[518]:
features1=load_features_from_folder_for_validation()
# In[519]:
df = pd.read_csv('ISIC-2017_Validation_Part3_GroundTruth.csv',header=None)
# In[520]:
bb=df[1][:]
bb=np.array(bb)
bb=np.delete(bb, (0), axis=0)
# In[521]:
s1=list(bb)
# In[522]:
# normalize the data attributes
feat_x= preprocessing.normalize(feat_x)
feat_x1=preprocessing.normalize(features1)
# standardize the data attributes
#feat_x= preprocessing.scale(feat_x)
#feat_x1= preprocessing.scale(features1)
# In[523]:
rfc = RandomForestClassifier(n_estimators=10)
rfc.fit(feat_x,s)
# In[524]:
rfc_y=rfc.predict(feat_x1)
print("accuracy of random forrest is",accuracy_score(rfc_y,s1))
print(classification_report(rfc_y,s1))
# In[525]:
svm = SVC(C=100, gamma=0.0001, kernel='rbf',max_iter=300)
svm.fit(feat_x, s)
# In[526]:
svm_y = svm.predict(feat_x1)
print("accuracy of svm is",accuracy_score(svm_y,s1))
print(classification_report(svm_y,s1))
# In[527]:
nby=MultinomialNB()
nby.fit(abs(feat_x),s)
# In[528]:
nby_y = nby.predict(feat_x1)
print("accuracy of naive_bayes is",accuracy_score(nby_y,s1))
print(classification_report(nby_y,s1))
# In[529]:
dect = DecisionTreeClassifier()
dect.fit(feat_x, s)
# In[530]:
dect_y = dect.predict(feat_x1)
print("accuracy of decision trees is",accuracy_score(dect_y,s1))
print(classification_report(dect_y,s1))
# In[531]:
knn = KNeighborsClassifier(n_neighbors=7,weights='distance')
knn.fit(feat_x,s)
knn_y=knn.predict(feat_x1)
print("accuracy of knn is",accuracy_score(knn_y,s1))
print(classification_report(knn_y,s1))
# In[532]:
def load_features_from_folder_for_test(folder):
i=0
for filename in os.listdir(os.path.join(folder)):
img = cv2.imread(os.path.join(os.path.join(folder),filename))
#img = segmentation(img)
#mean,std,6
(means, stds) = cv2.meanStdDev(img)
mean_std=np.array(list(zip(means, stds))).flatten()
#shape
gray = cv2.cvtColor(np.uint8(img), cv2.COLOR_BGR2GRAY)
shape=cv2.HuMoments(cv2.moments(gray)).flatten()
shape = -np.sign(shape) * np.log10(np.abs(shape))
#histogram
hist =describe(img)
#texture
texture =textureFeatures(img)
#feature=np.hstack((texture,shape,hist))
#feature=np.hstack((texture))
feature=np.hstack((hist))
if i==0:
features = np.zeros(feature.shape[0])
i=i+1
features = np.vstack((features,feature))
return np.delete(features, (0), axis=0)
# In[533]:
feat_x2=load_features_from_folder_for_test("ISIC-2017_Test_v2_Data")
# In[534]:
knn_y=knn.predict(feat_x2)
dect_y = dect.predict(feat_x2)
nby_y = nby.predict(feat_x2)
svm_y = svm.predict(feat_x2)
rfc_y=rfc.predict(feat_x2)
# In[ ]:
# In[536]:
names=[name for name in os.listdir("ISIC-2017_Test_v2_Data")]
import csv
def cret(y_pred , imgs):
csv_file='CHARROUD ANAS12.csv'
with open(csv_file, 'w',newline='') as csvfile:
field=['image_id','melanoma']
writer=csv.DictWriter(csvfile, fieldnames=field)
writer.writeheader()
for i in range(len(y_pred)):
filename = imgs[i]
classe = y_pred[i]
writer.writerow({'image_id':filename, 'melanoma':classe})
cret(knn_y, names)
# In[ ]:
# In[ ]: