-
Notifications
You must be signed in to change notification settings - Fork 26
/
testing.py
699 lines (592 loc) · 22.9 KB
/
testing.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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
import numpy as np
import cv2
from numba import jit
import os
from matplotlib import pyplot as plt
import math
import csv
from sklearn import preprocessing
def writeResults(DestinationFolder,resultFolder,name_array,classifier,Y_predicted):
print(len(name_array))
if not os.path.exists(resultFolder):
os.mkdir(resultFolder)
size_m = 0
i = 0
j = 0
lc = 0
while size_m < len(name_array):
current = cv2.imread(DestinationFolder+name_array[size_m]+"_final_candidates.bmp")
#print(DestinationFolder+name_array[size_m]+"_final_candidates.bmp.bmp")
#print("current ka size",current.shape)
x,current_m,z = cv2.split(current)
#print(count_ones(current_m,255),"now again check",name_array[size_m])
i = 0
while i < current_m.shape[0]:
j = 0
while j < current_m.shape[1]:
if current_m[i,j] == 255:
current_m[i,j] = 255*Y_predicted[lc]
lc = lc + 1
j = j + 1
i = i + 1
cv2.imwrite(resultFolder+name_array[size_m]+classifier+"_result.bmp",current_m)
size_m = size_m + 1
@jit
def standard_deviation_image(image):
clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8,8))
clahe_output = clahe.apply(image)
result = clahe_output.copy()
i = 0
j = 0
while i < image.shape[0]:
j = 0
while j < image.shape[1]:
sub_image = clahe_output[i:i+20,j:j+25]
var = np.var(sub_image)
result[i:i+20,j:j+25] = var
j = j+25
i = i+20
return result
@jit
def deviation_from_mean(image):
clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8,8))
clahe_output = clahe.apply(image)
print(clahe_output)
result = clahe_output.copy()
result = result.astype('int')
i = 0
j = 0
while i < image.shape[0]:
j = 0
while j < image.shape[1]:
sub_image = clahe_output[i:i+5,j:j+5]
mean = np.mean(sub_image)
sub_image = sub_image - mean
result[i:i+5,j:j+5] = sub_image
j = j+5
i = i+5
return result
def get_DistanceFromOD_data(image, centre):
my_image = image.copy()
x_cor = centre[0]
y_cor = centre[1]
feature_5 = np.reshape(image, (image.size,1))
k = 0
i = 0
j = 0
while i < image.shape[0]:
j = 0
while j < image.shape[1]:
feature_5[k] = math.fabs(x_cor-i) + math.fabs(y_cor-j)
j = j+1
k = k+1
i = i+1
return feature_5
def count_ones(image,value):
i = 0
j = 0
k = 0
while i < image.shape[0]:
j = 0
while j < image.shape[1]:
if int(image[i,j]) == value:
k = k+1
j = j + 1
i = i+1
return k
def get_average_intensity(green_channel):
average_intensity = green_channel.copy()
i = 0
j = 0
while i < green_channel.shape[0]:
j = 0
while j < green_channel.shape[1]:
sub_image = green_channel[i:i+20,j:j+25]
mean = np.mean(sub_image)
average_intensity[i:i+20,j:j+25] = mean
j = j+25
i = i+20
result = np.reshape(average_intensity, (average_intensity.size,1))
return result
def get_average_hue(hue_image):
average_hue = hue_image.copy()
i = 0
j = 0
while i < hue_image.shape[0]:
j = 0
while j < hue_image.shape[1]:
sub_image = hue_image[i:i+20,j:j+25]
mean = np.mean(sub_image)
average_hue[i:i+20,j:j+25] = mean
j = j+25
i = i+20
result = np.reshape(average_hue, (average_hue.size,1))
return result
def get_average_saturation(hue_image):
average_hue = hue_image.copy()
i = 0
j = 0
while i < hue_image.shape[0]:
j = 0
while j < hue_image.shape[1]:
sub_image = hue_image[i:i+20,j:j+25]
mean = np.mean(sub_image)
average_hue[i:i+20,j:j+25] = mean
j = j+25
i = i+20
result = np.reshape(average_hue, (average_hue.size,1))
return result
def get_SD_data(sd_image):
feature_1 = np.reshape(sd_image, (sd_image.size,1))
#print(feature_1.shape,"feature1")
return feature_1
def get_HUE_data(hue_image):
feature_2 = np.reshape(hue_image,(hue_image.size,1))
#print(feature_2.shape,"feature2")
return feature_2
def get_saturation_data(s_image):
feature = np.reshape(s_image,(s.size,1))
#print(feature.shape)
return feature
def get_INTENSITY_data(intensity_image):
feature_3 = np.reshape(intensity_image,(intensity_image.size,1))
#print(feature_3.shape,"feature3")
return feature_3
def get_EDGE_data(edge_candidates_image):
feature_4 = np.reshape(edge_candidates_image,(edge_candidates_image.size,1))
#print(feature_4.shape,"feature4")
return feature_4
def get_RED_data(red_channel):
feature_5 = np.reshape(red_channel, (red_channel.size,1))
#print(feature_5.shape,"feature5")
return feature_5
def get_GREEN_data(green_channel):
feature_6 = np.reshape(green_channel, (green_channel.size,1))
#print(feature_6.shape,"feature6")
return feature_6
def line_of_symmetry(image):
image_v = image.copy()
line = 0
prev_diff = image_v.size
for i in range(20,image_v.shape[0]-20):
x1, y1 = image_v[0:i,:].nonzero()
x2, y2 = image_v[i+1:image_v.shape[0],:].nonzero()
diff = abs(x1.shape[0] - x2.shape[0])
if diff < prev_diff:
prev_diff = diff
line = i
i = i + 35
return line
def identify_OD(image):
newfin = cv2.dilate(image, cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(5,5)), iterations=2)
mask = np.ones(newfin.shape[:2], dtype="uint8") * 255
y1, ycontours, yhierarchy = cv2.findContours(newfin.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
prev_contour = ycontours[0]
for cnt in ycontours:
if cv2.contourArea(cnt) >= cv2.contourArea(prev_contour):
prev_contour = cnt
cv2.drawContours(mask, [cnt], -1, 0, -1)
M = cv2.moments(prev_contour)
cx = int(M['m10']/M['m00'])
cy = int(M['m01']/M['m00'])
#print(cx,cy)
return (cx,cy)
def identify_OD_bv_density(blood_vessel_image):
los = line_of_symmetry(blood_vessel_image)
sub_image = blood_vessel_image[los-100:los+100,:]
i = 0
index = 0
density = -1
rr = 0
while i < sub_image.shape[1]:
x1,y1 = sub_image[:,i:i+50].nonzero()
count = x1.shape[0]
if(density < count):
density = count
index = i
i = i + 30
print(los,index)
return (index,los)
@jit
def calculate_entropy(image):
entropy = image.copy()
sum = 0
i = 0
j = 0
while i < entropy.shape[0]:
j = 0
while j < entropy.shape[1]:
sub_image = entropy[i:i+10,j:j+10]
histogram = cv2.calcHist([sub_image],[0],None,[256],[0,256])
sum = 0
for k in range(256):
if histogram[k] != 0:
sum = sum + (histogram[k] * math.log(histogram[k]))
k = k + 1
entropy[i:i+10,j:j+10] = sum
j = j+10
i = i+10
ret2,th2 = cv2.threshold(entropy,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
newfin = cv2.erode(th2, cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(3,3)), iterations=1)
return newfin
@jit
def edge_pixel_image(image,bv_image):
edge_result = image.copy()
edge_result = cv2.Canny(edge_result,30,100)
i = 0
j = 0
while i < image.shape[0]:
j = 0
while j < image.shape[1]:
if edge_result[i,j] == 255 and bv_image[i,j] == 255:
edge_result[i,j] = 0
j = j+1
i = i+1
newfin = cv2.dilate(edge_result, cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(3,3)), iterations=1)
return newfin
@jit
def remove_bv_image(image,bv_image):
edge_result = image[:,:,0]
i = 0
j = 0
while i < image.shape[0]:
j = 0
while j < image.shape[1]:
if edge_result[i,j] == 255 and bv_image[i,j] == 255:
edge_result[i,j] = 0
j = j+1
i = i+1
return edge_result
def extract_bv(image):
clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8,8))
contrast_enhanced_green_fundus = clahe.apply(image)
# applying alternate sequential filtering (3 times closing opening)
r1 = cv2.morphologyEx(contrast_enhanced_green_fundus, cv2.MORPH_OPEN, cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(5,5)), iterations = 1)
R1 = cv2.morphologyEx(r1, cv2.MORPH_CLOSE, cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(5,5)), iterations = 1)
r2 = cv2.morphologyEx(R1, cv2.MORPH_OPEN, cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(11,11)), iterations = 1)
R2 = cv2.morphologyEx(r2, cv2.MORPH_CLOSE, cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(11,11)), iterations = 1)
r3 = cv2.morphologyEx(R2, cv2.MORPH_OPEN, cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(23,23)), iterations = 1)
R3 = cv2.morphologyEx(r3, cv2.MORPH_CLOSE, cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(23,23)), iterations = 1)
f4 = cv2.subtract(R3,contrast_enhanced_green_fundus)
f5 = clahe.apply(f4)
# removing very small contours through area parameter noise removal
ret,f6 = cv2.threshold(f5,15,255,cv2.THRESH_BINARY)
mask = np.ones(f5.shape[:2], dtype="uint8") * 255
im2, contours, hierarchy = cv2.findContours(f6.copy(),cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)
for cnt in contours:
if cv2.contourArea(cnt) <= 200:
cv2.drawContours(mask, [cnt], -1, 0, -1)
im = cv2.bitwise_and(f5, f5, mask=mask)
ret,fin = cv2.threshold(im,15,255,cv2.THRESH_BINARY_INV)
newfin = cv2.erode(fin, cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(3,3)), iterations=1)
# removing blobs of microaneurysm & unwanted bigger chunks taking in consideration they are not straight lines like blood
# vessels and also in an interval of area
fundus_eroded = cv2.bitwise_not(newfin)
xmask = np.ones(image.shape[:2], dtype="uint8") * 255
x1, xcontours, xhierarchy = cv2.findContours(fundus_eroded.copy(),cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)
for cnt in xcontours:
shape = "unidentified"
peri = cv2.arcLength(cnt, True)
approx = cv2.approxPolyDP(cnt, 0.04 * peri, False)
if len(approx) > 4 and cv2.contourArea(cnt) <= 3000 and cv2.contourArea(cnt) >= 100:
shape = "circle"
else:
shape = "veins"
if(shape=="circle"):
cv2.drawContours(xmask, [cnt], -1, 0, -1)
finimage = cv2.bitwise_and(fundus_eroded,fundus_eroded,mask=xmask)
blood_vessels = cv2.bitwise_not(finimage)
dilated = cv2.erode(blood_vessels, cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(7,7)), iterations=1)
#dilated1 = cv2.dilate(blood_vessels, cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(3,3)), iterations=1)
blood_vessels_1 = cv2.bitwise_not(dilated)
return blood_vessels_1
if __name__ == "__main__":
pathFolder = "/home/sherlock/Internship@iit/exudate-detection/testing/"
filesArray = [x for x in os.listdir(pathFolder) if os.path.isfile(os.path.join(pathFolder,x))]
DestinationFolder = "/home/sherlock/Internship@iit/exudate-detection/testing-results/"
LabelFolder = "/home/sherlock/Internship@iit/exudate-detection/diaretdb1-label/"
name_array = []
if not os.path.exists(DestinationFolder):
os.mkdir(DestinationFolder)
qq = 0
OD_data = np.genfromtxt('OD_info.txt', delimiter=',', dtype=None, names=('name','x-coordinate','y-coordinate'))
coordinates = []
name = []
counterd = 0
for t in OD_data:
coordinates.append((t[1],t[2]))
name.append(t[0].decode("utf-8"))
counterd = counterd + 1
for file_name in filesArray:
file_name_no_extension = os.path.splitext(file_name)[0]
coordinates_OD = coordinates[name.index(file_name_no_extension+"_resized")]
#print(coordinates_OD,"coordinates of OPTIC DISK")
name_array.append(file_name_no_extension)
print(pathFolder+'/'+file_name,"Read this image",file_name_no_extension)
fundus1 = cv2.imread(pathFolder+'/'+file_name)
fundus = cv2.resize(fundus1,(800,615))
fundus_mask = cv2.imread("fmask.tif")
fundus_mask = cv2.resize(fundus_mask,(800,615))
f1 = cv2.bitwise_and(fundus[:,:,0],fundus_mask[:,:,0])
f2 = cv2.bitwise_and(fundus[:,:,1],fundus_mask[:,:,1])
f3 = cv2.bitwise_and(fundus[:,:,2],fundus_mask[:,:,2])
fundus_dash = cv2.merge((f1,f2,f3))
cv2.imwrite(DestinationFolder+file_name_no_extension+"_resized_fundus.bmp",fundus_dash)
b,g,r = cv2.split(fundus_dash)
hsv_fundus = cv2.cvtColor(fundus_dash,cv2.COLOR_BGR2HSV)
h,s,v = cv2.split(hsv_fundus)
gray_scale = cv2.cvtColor(fundus_dash,cv2.COLOR_BGR2GRAY)
clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8,8))
contrast_enhanced_fundus = clahe.apply(gray_scale)
contrast_enhanced_green_fundus = clahe.apply(g)
average_intensity = get_average_intensity(contrast_enhanced_green_fundus)/255
average_hue = get_average_hue(h)/255
average_saturation = get_average_saturation(s)/255
bv_image_dash = extract_bv(g)
bv_image = extract_bv(gray_scale)
cv2.imwrite(DestinationFolder+file_name_no_extension+"_blood_vessels.bmp",bv_image_dash)
var_fundus = standard_deviation_image(contrast_enhanced_fundus)
edge_feature_output = edge_pixel_image(gray_scale,bv_image)
newfin = cv2.dilate(edge_feature_output, cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(5,5)), iterations=1)
edge_candidates = cv2.erode(newfin, cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(3,3)), iterations=1)
edge_candidates = np.uint8(edge_candidates)
label_image = cv2.imread(LabelFolder+'/'+file_name_no_extension+"_final_label.bmp")
deviation_matrix = deviation_from_mean(gray_scale)
feature1 = get_SD_data(var_fundus)/255
feature2 = get_HUE_data(h)/255
feature3 = get_saturation_data(s)/255
feature4 = get_INTENSITY_data(contrast_enhanced_fundus)/255
feature5 = get_RED_data(r)/255
feature6 = get_GREEN_data(g)/255
feature7 = get_DistanceFromOD_data(bv_image,coordinates_OD)/(var_fundus.shape[0]+var_fundus.shape[1])
feature8 = get_HUE_data(deviation_matrix)/255
print(feature8.shape,"deviation data shape")
Z = np.hstack((feature2,feature3)) #HUE and SATURATION
Z = np.float32(Z)
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 50, 0.01)
ret,label,center=cv2.kmeans(Z,6,None,criteria,50,cv2.KMEANS_RANDOM_CENTERS)
u, indices, counts = np.unique(label, return_index=True, return_counts=True)
#u, indices, counts = np.unique(label, return_index=True, return_counts=True)
center_t = [(t[0]*255,t[1]*255) for t in center]
ex_color = (40,230)
distance = [(abs(t[0]- ex_color[0]),t) for t in center_t]
index1 = distance.index((min(distance)))
if counts[distance.index((min(distance)))] > 0.20*gray_scale.shape[0]*gray_scale.shape[1]:
index1 = -1
distance2 = [(abs(t[0]- ex_color[0])+abs(t[1]-ex_color[1]),t) for t in center_t]
index2 = -1
if min(distance2)[0] <=25:
index2 = distance2.index((min(distance2)))
if counts[distance2.index((min(distance2)))] > 0.20*gray_scale.shape[0]*gray_scale.shape[1]:
index2 = -1
green = [0,255,0]
blue = [255,0,0]
red = [0,0,255]
white = [255,255,255]
black = [0,0,0]
pink = [220,30,210]
sky = [30,240,230]
yellow = [230,230,30]
color = [white,black,red,green,blue,pink]
color = np.array(color,np.uint8)
label = np.reshape(label, gray_scale.shape)
test = label.copy()
if index1 == -1:
test.fill(0)
else:
test[test!=distance.index((min(distance)))] = -1
test[test==distance.index((min(distance)))] = 255
test[test==-1] = 0
test2 = label.copy()
if index2 == -1:
test2.fill(0)
else:
test2[test2!=index2] = -1
test2[test2==index2] = 255
test2[test2==-1] = 0
y = color[label]
y = np.uint8(y)
res_from_clustering = np.bitwise_or(test2,test)
cv2.imwrite(DestinationFolder+file_name_no_extension+"_candidate_exudates.bmp",edge_candidates)
cv2.imwrite(DestinationFolder+file_name_no_extension+"_result_exudates_kmeans.bmp",y)
cv2.imwrite(DestinationFolder+file_name_no_extension+"_test_result.bmp",test)
cv2.imwrite(DestinationFolder+file_name_no_extension+"_test2_result.bmp",test2)
final_candidates = np.bitwise_or(edge_candidates,res_from_clustering)
OD_loc = gray_scale.copy()
cv2.circle(OD_loc,coordinates_OD, 70, (0,0,0), -10)
cv2.imwrite(DestinationFolder+file_name_no_extension+"_OD_.bmp",OD_loc)
cl_res_dev = cv2.imread("/home/sherlock/Internship@iit/exudate-detection/testing-result-kmeans-deviation/"+file_name_no_extension+"_final_candidates.bmp")
cl_res_dev = remove_bv_image(cl_res_dev,bv_image_dash)
print("/home/sherlock/Internship@iit/exudate-detection/testing-result-kmeans-deviation/"+file_name_no_extension+"_final_candidates.bmp")
final_candidates = np.bitwise_or(final_candidates,cl_res_dev)
cv2.circle(final_candidates,coordinates_OD, 70, (0,0,0), -10)
maskk = cv2.imread("MASK.bmp")
final_candidates = np.bitwise_and(final_candidates,maskk[:,:,0])
final_candidates = final_candidates.astype('uint8')
final_candidates = cv2.dilate(final_candidates, cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(3,3)), iterations=1)
cv2.imwrite(DestinationFolder+file_name_no_extension+"_final_candidates.bmp",final_candidates)
candidates_vector = np.reshape(final_candidates,(final_candidates.size,1))/255
b,gg,r = cv2.split(label_image)
label = np.reshape(gg,(gg.size,1))/255
co3 = count_ones(edge_candidates,255)
no_of_white = count_ones(label,1)
print(no_of_white,"no of white pixels")
print(co3,"check me")
temp = 0
counter = 0
this_image_rows = 0
with open('testing.csv', 'a') as csvfile:
filewriter = csv.writer(csvfile, delimiter=',',quotechar='|', quoting=csv.QUOTE_MINIMAL)
while counter < feature1.shape[0]:
if candidates_vector[counter,0] == 1:
qq = qq + 1
temp = counter
this_image_rows = this_image_rows+1
filewriter.writerow([feature2[counter,0],feature3[counter,0],feature4[counter,0],feature5[counter,0],feature6[counter,0],feature7[counter,0],feature8[counter,0],average_intensity[counter,0],average_hue[counter,0],average_saturation[counter,0],int(label[counter,0])])
counter = counter + 1
#print(feature1[temp,0],feature2[temp,0],feature3[temp,0],int(label[temp,0]))
print("no of rows addded : ", this_image_rows)
print("no of pixxels in total", qq)
os.rename("testing.csv","testing.txt")
import numpy as np
from sklearn import preprocessing
import random
import pandas as pd
from sklearn.metrics import confusion_matrix
from sklearn.svm import SVC
from sklearn.metrics import accuracy_score
from sklearn.ensemble import RandomForestClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.ensemble import AdaBoostClassifier
from sklearn.tree import DecisionTreeClassifier
print("fcuuk")
dataset_train = np.loadtxt('training.txt', delimiter=",")
#dataset_train = pd.read_csv("train.csv")
print(dataset_train.shape,"train_shape")
dataset_test = np.loadtxt('testing.txt', delimiter=",")
#dataset_test = pd.read_csv("test.csv")
print(dataset_test.shape,"test_shape")
X_train = dataset_train[:,0:9]
Y_train = dataset_train[:,10]
X_test = dataset_test[:,0:9]
Y_test = dataset_test[:,10]
print(Y_test)
from collections import Counter
print("initially unbalanced classes : ")
print(sorted(Counter(Y_train).items()))
print("RANDOM FOREST")
clf_name = "rf"
resultFolder = "/home/sherlock/Internship@iit/exudate-detection/"+clf_name+"_results-exudates/"
clf = RandomForestClassifier(n_estimators=10)
clf.fit(X_train, Y_train)
Y_predicted = clf.predict(X_test)
print("accuracy")
print(accuracy_score(Y_test, Y_predicted))
print("confusion matrix")
print (confusion_matrix(Y_test,Y_predicted))
writeResults(DestinationFolder,resultFolder,name_array,clf_name,Y_predicted)
print("ADABOOST")
clf_name = "ab"
resultFolder = "/home/sherlock/Internship@iit/exudate-detection/"+clf_name+"_results-exudates/"
clf = AdaBoostClassifier(DecisionTreeClassifier(max_depth = 5),algorithm="SAMME",n_estimators=100)
clf.fit(X_train, Y_train)
Y_predicted = clf.predict(X_test)
print("accuracy")
print(accuracy_score(Y_test, Y_predicted))
print("confusion matrix")
print (confusion_matrix(Y_test,Y_predicted))
writeResults(DestinationFolder,resultFolder,name_array,clf_name,Y_predicted)
print("ADABOOST CLASSIFIER")
clf_name = "abc"
resultFolder = "/home/sherlock/Internship@iit/exudate-detection/"+clf_name+"_results-exudates/"
clf = AdaBoostClassifier()
clf.fit(X_train, Y_train)
Y_predicted = clf.predict(X_test)
print("accuracy")
print(accuracy_score(Y_test, Y_predicted))
print("confusion matrix")
print (confusion_matrix(Y_test,Y_predicted))
writeResults(DestinationFolder,resultFolder,name_array,clf_name,Y_predicted)
print("SUPPORT VECTOR MACHINES")
clf_name = "svc"
resultFolder = "/home/sherlock/Internship@iit/exudate-detection/"+clf_name+"_results-exudates/"
clf = SVC(kernel = 'linear')
clf.fit(X_train, Y_train)
Y_predicted = clf.predict(X_test)
print("accuracy")
print(accuracy_score(Y_test, Y_predicted))
print("confusion matrix")
print (confusion_matrix(Y_test,Y_predicted))
writeResults(DestinationFolder,resultFolder,name_array,clf_name,Y_predicted)
print("KNN_ NEAREST NEIGHBOURS")
clf_name = "knn"
resultFolder = "/home/sherlock/Internship@iit/exudate-detection/"+clf_name+"_results-exudates/"
clf = KNeighborsClassifier(n_neighbors = 10)
clf.fit(X_train, Y_train)
Y_predicted = clf.predict(X_test)
print("accuracy")
print(accuracy_score(Y_test, Y_predicted))
print("confusion matrix")
print (confusion_matrix(Y_test,Y_predicted))
writeResults(DestinationFolder,resultFolder,name_array,clf_name,Y_predicted)
print("DONE_-------------------x----xxxxx-xx-x")
from imblearn.over_sampling import RandomOverSampler
ros = RandomOverSampler(random_state=0)
X_resampled, Y_resampled = ros.fit_sample(X_train, Y_train)
print("when balanced classes : ")
print(sorted(Counter(Y_resampled).items()))
print("RANDOM FOREST")
clf_name = "rf"
resultFolder = "/home/sherlock/Internship@iit/exudate-detection/"+clf_name+"_BAL_results-exudates/"
clf = RandomForestClassifier(n_estimators=10)
clf.fit(X_resampled, Y_resampled)
Y_predicted = clf.predict(X_test)
print("accuracy")
print(accuracy_score(Y_test, Y_predicted))
print("confusion matrix")
print (confusion_matrix(Y_test,Y_predicted))
writeResults(DestinationFolder,resultFolder,name_array,clf_name,Y_predicted)
print("ADABOOST")
clf_name = "ab"
resultFolder = "/home/sherlock/Internship@iit/exudate-detection/"+clf_name+"_BAL_results-exudates/"
clf = AdaBoostClassifier(DecisionTreeClassifier(max_depth = 5),algorithm="SAMME",n_estimators=100)
clf.fit(X_resampled, Y_resampled)
Y_predicted = clf.predict(X_test)
print("accuracy")
print(accuracy_score(Y_test, Y_predicted))
print("confusion matrix")
print (confusion_matrix(Y_test,Y_predicted))
writeResults(DestinationFolder,resultFolder,name_array,clf_name,Y_predicted)
print("ADABOOST CLASSIFIER")
clf_name = "abc"
resultFolder = "/home/sherlock/Internship@iit/exudate-detection/"+clf_name+"_BAL_results-exudates/"
clf = AdaBoostClassifier()
clf.fit(X_resampled, Y_resampled)
Y_predicted = clf.predict(X_test)
print("accuracy")
print(accuracy_score(Y_test, Y_predicted))
print("confusion matrix")
print (confusion_matrix(Y_test,Y_predicted))
writeResults(DestinationFolder,resultFolder,name_array,clf_name,Y_predicted)
print("SUPPORT VECTOR MACHINES")
clf_name = "svc"
resultFolder = "/home/sherlock/Internship@iit/exudate-detection/"+clf_name+"_BAL_results-exudates/"
clf = SVC(kernel = 'linear')
clf.fit(X_resampled, Y_resampled)
Y_predicted = clf.predict(X_test)
print("accuracy")
print(accuracy_score(Y_test, Y_predicted))
print("confusion matrix")
print (confusion_matrix(Y_test,Y_predicted))
writeResults(DestinationFolder,resultFolder,name_array,clf_name,Y_predicted)
print("KNN_ NEAREST NEIGHBOURS")
clf_name = "knn"
resultFolder = "/home/sherlock/Internship@iit/exudate-detection/"+clf_name+"_BAL_results-exudates/"
clf = KNeighborsClassifier(n_neighbors = 10)
clf.fit(X_resampled, Y_resampled)
Y_predicted = clf.predict(X_test)
print("accuracy")
print(accuracy_score(Y_test, Y_predicted))
print("confusion matrix")
print (confusion_matrix(Y_test,Y_predicted))
writeResults(DestinationFolder,resultFolder,name_array,clf_name,Y_predicted)