-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathproject-main (final).py
989 lines (587 loc) · 26.2 KB
/
project-main (final).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
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
import numpy as np
import scipy.cluster.vq as vq
import cv2
import os,random,shutil
import cPickle
from skimage.feature import hog,local_binary_pattern
from matplotlib import pyplot as plt
from sklearn import svm
from sklearn.decomposition import PCA
# from mahotas.features import surf
from sklearn import preprocessing,metrics
#from sklearn.model_selection import GridSearchCV
from sklearn.pipeline import make_pipeline
from sklearn.cluster import KMeans
from data_augment import Data_Augementaion
from data_augment import config_file as Data_Aug_Cfg
from anmial_classifier import anmial_classifier
import config as cfg
class object_classfication():
def __init__(self,config):
self.cfg = config
self.train_imgs = None
self.train_labels = None
self.train_features = None
self.test_imgs = None
self.test_labels = None
self.test_features = None
self.label_dict = None
self.label_dict_inverse = None
def train_val_make(self,data_root,dst_root,trian_test_ratio = 0.75):
if not os.path.exists(data_root):
print "data_root is not correct"
return
if os.path.exists(dst_root):
shutil.rmtree(dst_root)
train_root = os.path.join(dst_root,"train_imgs")
test_root = os.path.join(dst_root,"test_imgs")
shutil.copytree(data_root,train_root)
for root,dirs,files in os.walk(train_root):
for subdir in dirs:
files = os.listdir(os.path.join(root,subdir))
examples = random.sample(files,int((1-trian_test_ratio)*len(files)))
if os.path.exists(os.path.join(test_root,subdir)) == False:
os.makedirs(os.path.join(test_root,subdir))
for example in examples:
shutil.move(os.path.join(root,subdir,example),os.path.join(test_root,subdir,example))
def imgs_preprocess(self):
data_root = self.cfg.data_root
train_val_root = self.cfg.train_val_root
trian_val_ratio = self.cfg.train_val_ratio
self.train_val_make(data_root,train_val_root,trian_val_ratio)
train_root = os.path.join(self.cfg.train_val_root,"train_imgs")
test_root = os.path.join(self.cfg.train_val_root,"test_imgs")
label_txt = self.cfg.label_txt
data_augment_root = self.cfg.data_augment_root
img_size = self.cfg.img_size
label_list = np.loadtxt(label_txt,dtype=str,delimiter='\n')
self.label_dict = dict(zip(label_list,[i for i in range(len(label_list))])) #get label_dict
self.label_dict_inverse = dict(zip([i for i in range(len(label_list))],label_list)) #get label_dict
# data_file = "./data_processed.npz"
# if os.path.exists(data_file):
# data = np.load(data_file)
# self.train_imgs = data['train_imgs']
# self.train_labels = data['train_labels']
# self.test_imgs = data['test_imgs']
# self.test_labels = data['test_labels']
# return
train_data = self.get_data(train_root,self.label_dict,img_size)
test_data = self.get_data(test_root,self.label_dict,img_size)
self.train_imgs = train_data[:,0:-1]
self.train_labels = train_data[:,-1].astype(int)
self.test_imgs = test_data[:,0:-1]
self.test_labels = test_data[:,-1].astype(int)
data_aug_cfg_obj = Data_Aug_Cfg()
data_aug_cfg_obj.src_root = train_root
data_aug_cfg_obj.dst_root = data_augment_root
data_aug_cfg_obj.TARGET_HEIGHT = img_size[0]
data_aug_cfg_obj.TARGET_WIDTH = img_size[1]
data_aug_obj = Data_Augementaion(data_aug_cfg_obj)
data_aug_obj.creat_data()
print "creat_augment_data_done........."
train_data = self.get_data(data_augment_root,self.label_dict,img_size)
np.random.shuffle(train_data)
self.train_imgs = train_data[:,0:-1]
self.train_labels = train_data[:,-1].astype(int)
# np.savez(data_file,train_imgs = self.train_imgs,train_labels = self.train_labels,
# test_imgs = self.test_imgs,test_labels = self.test_labels)
return self.train_imgs,self.train_labels,self.test_imgs,self.test_labels,self.label_dict
def get_data(self,file_root,label_dict,size):
if not os.path.exists(file_root):
print "fille not exists,please check path"
data = None
for root,dirs,files in os.walk(file_root):
for subdir in dirs:
files = os.listdir(os.path.join(root,subdir))
for file in files:
img = cv2.imread(os.path.join(root,subdir,file),0)
# img_gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
# img_gray = cv2.cvtColor(img,cv2.COLOR_BGR2HSV_FULL)
# img_gray = img_gray[:,:,0]+1
# print img_gray.dtype,img_gray.min(),img_gray.max()
img_gray_resize = cv2.resize(img,size)
# cv2.imshow("%s"%file,img_gray_resize)
# print img_gray_resize.shape
img_gray_resize = img_gray_resize.reshape(1,-1)
# print img_gray_resize.shape
if data is not None:
img_gray_resize = np.concatenate((img_gray_resize,np.array([label_dict[subdir]],dtype=np.uint8).reshape(1,1)),axis = 1)
data = np.concatenate((data,img_gray_resize),axis = 0)
else:
img_gray_resize = np.concatenate((img_gray_resize,np.array([label_dict[subdir]],dtype=np.uint8).reshape(1,1)),axis = 1)
data = np.copy(img_gray_resize)
return data
def compute_mean(self,data):
mean = np.mean(data)
print "imgs_mean:%5f"%mean
return mean
def imgs_normalization(self,train_data,test_data):
# mean =133.368
# # mean = compute_mean(data)
# train_data = train_data - mean
# for i in range(train_data.shape[0]):
# min = np.min(train_data[i])
# max = np.max(train_data[i])
# train_data[i] = (train_data[i]-min)/(max-min)
# test_data = test_data - mean
# for i in range(test_data.shape[0]):
# min = np.min(test_data[i])
# max = np.max(test_data[i])
# test_data[i] = (test_data[i]-min)/(max-min)
# mean = np.mean(train_data,axis = 0)
# train_data = train_data - mean
# test_data = test_data - mean
mean = np.mean(train_data,axis = 1)
for i in range(train_data.shape[0]):
train_data[i] = train_data[i] - mean[i]
# mean = np.mean(test_data,axis = 1)
for i in range(test_data.shape[0]):
test_data[i] = test_data[i] - mean[i]
# train_data = preprocessing.minmax_scale(train_data,axis = 1)
# test_data = preprocessing.minmax_scale(test_data,axis = 1)
return train_data,test_data
def img_Standardization(self,train_data,test_data):
train_data = train_data.T
scalar = preprocessing.StandardScaler()
train_data = scalar.fit_transform(train_data)
test_data = test_data.T
test_data = scalar.fit_transform(test_data)
return train_data.T,test_data.T
def data_Standardization(self,train_data,test_data):
train_data = preprocessing.scale(train_data)
test_data = preprocessing.scale(test_data)
return train_data,test_data
def data_normalizer(self,train_data,test_data):
train_data = preprocessing.normalize(train_data)
test_data = preprocessing.normalize(test_data)
return train_data,test_data
def hog_meathod(self,data,size,orientations,pixels_per_cell,cells_per_block,block_norm,visualise,transform_sqrt):
num_examples = data.shape[0]
feat = None
for i in range(num_examples):
ft = hog(data[i,:].reshape(size),orientations= orientations, pixels_per_cell= pixels_per_cell,cells_per_block=cells_per_block,
block_norm = block_norm,visualise = visualise, transform_sqrt = transform_sqrt)
ft = ft.reshape(1,-1)
# cv2.imshow("src",data[i,:].reshape(size))
# cv2.imshow("hog",vis_img)
# cv2.waitKey()
if feat is not None:
feat = np.concatenate((feat,ft),axis = 0)
else :
feat = np.copy(ft)
return feat
def surf_meathod(self,data,size):
num_examples = data.shape[0]
feat = None
for i in range(num_examples):
ft = surf.surf(data[i,:].reshape(size))
print "orgin",ft.shape
ft = ft.reshape(1,-1)
print ft.shape
if feat != None:
feat = np.concatenate((feat,ft),axis = 0)
else:
feat = np.copy(ft)
return feat
def LBP_meathod(self,data,size):
radius = 2
n_point = radius * 8
feat = None
num_examples = data.shape[0]
for i in range(num_examples):
lbp = local_binary_pattern(data[i,:].reshape(size),n_point,radius,'ror')
# max_bins = int(lbp.max()+1)
max_bins = 256
ft,_ = np.histogram(lbp,bins = max_bins,normed = True,range =(0,max_bins))
ft = ft.reshape(1,-1)
if feat is not None:
feat = np.concatenate((feat,ft),axis = 0)
else:
feat = np.copy(ft)
return feat
def sift_meathod(self,data,size,name):
# data_file = "./%s_feature_data.npz"%name
# if os.path.exists(data_file):
# data = np.load(data_file)
# feat = data["feat"]
# img_descriptor_index = data["img_descriptor_index"]
# return feat,img_descriptor_index
num_examples = data.shape[0]
feat = None
img_descriptor_index = []
sift = cv2.SIFT()
for i in range(num_examples):
img = data[i,:].reshape(size)
kp,ft= sift.detectAndCompute(img,None)
img_descriptor_index.append(ft.shape[0])
# print "orgin",ft.shape[0]
if feat is not None:
feat = np.concatenate((feat,ft),axis = 0)
else:
feat = np.copy(ft)
img_descriptor_index = np.array(img_descriptor_index,dtype = np.int)
img_descriptor_index = np.cumsum(img_descriptor_index)
print "all sift feature",feat.shape,num_examples
# print "img_descriptor_index.shape",img_descriptor_index.shape
# print "img_descriptor_index",img_descriptor_index
if img_descriptor_index[-1] != feat.shape[0]:
print "img_descriptor_index is fail"
# data = np.savez(data_file,feat = feat,img_descriptor_index = img_descriptor_index)
return feat,img_descriptor_index
def get_codebook(self,feat,val):
codebook_path = "./codebook.file"
if val != True:
n_clusters = 100
K_THRESH = 1e-5
# whitened = vq.whiten(feat)
# codebook,distortion = vq.kmeans(whitened,n_clusters,thresh = K_THRESH)
kmeans = KMeans(n_clusters = n_clusters).fit(feat)
# codebook = kmenas.cluster_centers_
# print codebook.shape
if os.path.exists(codebook_path):
os.remove(codebook_path)
with open(codebook_path,'wb') as f:
cPickle.dump(kmeans,f)
else:
with open(codebook_path,'rb') as f:
kmeans = cPickle.load(f)
return kmeans
def get_img_represent(self,feat,img_descriptor_index,codebook):
bow_feat = None
# whitened = vq.whiten(feat)
# code,dist = vq.vq(whitened,codebook)
code = codebook.predict(feat)
len_cluster_centers = codebook.cluster_centers_.shape[0]
for i in range(img_descriptor_index.shape[0]):
if i == 0:
img_code = code[0:img_descriptor_index[i]]
else:
img_code = code[img_descriptor_index[i-1]:img_descriptor_index[i]]
histogram_of_words, bin_edges = np.histogram(img_code,bins = range(len_cluster_centers+1),normed = True)
# print "histogram_of_words shape",histogram_of_words.shape,histogram_of_words[10]
histogram_of_words = histogram_of_words.reshape(1,-1)
if bow_feat is not None:
bow_feat = np.concatenate((bow_feat,histogram_of_words),axis= 0)
else:
bow_feat = np.copy(histogram_of_words)
return bow_feat
def get_img_represent_vq(self,feat,img_descriptor_index,codebook):
bow_feat = None
# whitened = vq.whiten(feat)
code,dist = vq.vq(feat,codebook)
len_cluster_centers = codebook.shape[0]
for i in range(img_descriptor_index.shape[0]):
if i == 0:
img_code = code[0:img_descriptor_index[i]]
else:
img_code = code[img_descriptor_index[i-1]:img_descriptor_index[i]]
histogram_of_words, bin_edges = np.histogram(img_code,bins = range(len_cluster_centers+1),normed = True)
# print "histogram_of_words shape",histogram_of_words.shape,histogram_of_words[10]
histogram_of_words = histogram_of_words.reshape(1,-1)
if bow_feat is not None:
bow_feat = np.concatenate((bow_feat,histogram_of_words),axis= 0)
else:
bow_feat = np.copy(histogram_of_words)
return bow_feat
def bag_of_visual_word(self,data,size,val,name):
sift_feat,img_descriptor_index = self.sift_meathod(data,size,name)
codebook = self.get_codebook(sift_feat,val)
feat = self.get_img_represent(sift_feat,img_descriptor_index,codebook)
return feat
# print kmeans.labels_ ,kmeans.cluster_centers_
def bag_of_visual_word_category(self,data,size,val,name):
category_num = len(self.label_dict)
print "category num ",category_num
codebook = None
n_clusters = 100
codebook_path = self.cfg.codebook_save_path
if val != True:
for i in range(category_num):
category_imgs = self.train_imgs[np.where(self.train_labels == i)]
sift_feat,img_descriptor_index = self.sift_meathod(category_imgs,size,name)
# kmeans = KMeans(n_clusters = n_clusters).fit(sift_feat)
codebook_part,distortion = vq.kmeans(sift_feat,n_clusters ,thresh = 0.001)
if codebook is not None:
codebook = np.concatenate((codebook,codebook_part),axis = 0)
else :
codebook = np.copy(codebook_part)
print "codebook.shape",codebook.shape
if os.path.exists(codebook_path):
os.remove(codebook_path)
with open(codebook_path,'wb') as f:
cPickle.dump(codebook,f)
else:
if not os.path.exists(codebook_path):
print "do not exists codebook file ..please train data first"
return
with open(codebook_path,'rb') as f:
codebook = cPickle.load(f)
print "codebook.shape",codebook.shape
sift_feat,img_descriptor_index = self.sift_meathod(data,size,name)
feat = self.get_img_represent_vq(sift_feat,img_descriptor_index,codebook)
return feat
def point_distribute(self,point,cord_list):
x = point[0]
y = point[1]
for i in range(len(cord_list)):
if x >= cord_list[i][0] and x < cord_list[i][1] \
and y >= cord_list[i][2] and y < cord_list[i][3]:
return i
def points_distribute(self,points,ft,cord_list,num_partion_axis):
num_point = ft.shape[0]
ft_list = [[] for i in range(num_partion_axis*num_partion_axis)]
for i in range(num_point):
index = self.point_distribute((points[i].pt[0],points[i].pt[1]),cord_list)
ft_list[index].append(ft[i])
return ft_list
def get_codebook_partion(self,imgs,size):
partion_weight = self.cfg.weights_matrix
num_points = self.cfg.num_points_each_image
num_partion_axis = partion_weight.shape[0]
h,w = size
h_list = list(np.linspace(0,h,num_partion_axis+1))
w_list = list(np.linspace(0,w,num_partion_axis+1))
cord_list = []
for i in range(num_partion_axis):
for j in range(num_partion_axis):
x1 = w_list[j]
x2 = w_list[j+1]
y1 = h_list[i]
y2 = h_list[i+1]
cord_list.append([x1,x2,y1,y2])
# print cord_list
num_imgs = imgs.shape[0]
sift = cv2.SIFT()
img_feat = None
for i in range(num_imgs):
img = imgs[i].reshape(size)
kp,ft = sift.detectAndCompute(img,None)
#one image gets kpoints and features , select kpoints
ft_list = self.points_distribute(kp,ft,cord_list,num_partion_axis)
num_points_partion = partion_weight * num_points
num_points_partion = num_points_partion.flatten().astype(np.int)
# one image to get fearute,use regular grid (weights)
for i in range(num_partion_axis*num_partion_axis):
num_ft_list_element = len(ft_list[i])
num_sample_part = num_points_partion[i] if num_ft_list_element >= num_points_partion[i] else num_ft_list_element
if num_ft_list_element != 0 and num_sample_part != 0:
img_feat_part_list = random.sample(ft_list[i],num_sample_part)
img_feat_part = np.vstack(img_feat_part_list)
if img_feat is not None:
img_feat = np.concatenate((img_feat,img_feat_part),axis = 0)
else:
img_feat = np.copy(img_feat_part)
codebook = img_feat
print "category_codebook_shape:",codebook.shape
return codebook
def bag_of_visual_word_category_partion(self,data,size,val,name):
category_num = len(self.label_dict)
print "category num",category_num
codebook = None
codebook_path = self.cfg.codebook_save_path
if val != True:
for i in range(category_num):
category_imgs = self.train_imgs[np.where(self.train_labels == i)]
codebook_part = self.get_codebook_partion(category_imgs,size)
if codebook is not None:
codebook = np.concatenate((codebook,codebook_part),axis = 0)
else :
codebook = np.copy(codebook_part)
print "created........ all_codebook_shape",codebook.shape
if os.path.exists(codebook_path):
os.remove(codebook_path)
with open(codebook_path,'wb') as f:
cPickle.dump(codebook,f)
else:
if not os.path.exists(codebook_path):
print "do not exists codebook file ..please train data first"
return
with open(codebook_path,'rb') as f:
codebook = cPickle.load(f)
print "load codebook.......all_codebook_shape",codebook.shape
sift_feat,img_descriptor_index = self.sift_meathod(data,size,name)
feat = self.get_img_represent_vq(sift_feat,img_descriptor_index,codebook)
return feat
def bag_of_visual_word_category_partion_cluster(self,data,size,val,name):
category_num = len(self.label_dict)
print "category num",category_num
codebook = None
codebook_path = self.cfg.codebook_save_path
n_clusters = self.cfg.num_category_cluster
if val != True:
for i in range(category_num):
category_imgs = self.train_imgs[np.where(self.train_labels == i)]
pre_codebook_part = self.get_codebook_partion(category_imgs,size)
codebook_part,distortion = vq.kmeans(pre_codebook_part,n_clusters ,thresh = 0.001)
if codebook is not None:
codebook = np.concatenate((codebook,codebook_part),axis = 0)
else :
codebook = np.copy(codebook_part)
print "created........ all_codebook_shape",codebook.shape
if os.path.exists(codebook_path):
os.remove(codebook_path)
codebook_path_dirname = os.path.dirname(codebook_path)
if not os.path.exists(codebook_path_dirname):
os.makedirs(codebook_path_dirname)
with open(codebook_path,'wb') as f:
cPickle.dump(codebook,f)
else:
if not os.path.exists(codebook_path):
print "do not exists codebook file ..please train data first"
return
with open(codebook_path,'rb') as f:
codebook = cPickle.load(f)
print "load codebook.......all_codebook_shape",codebook.shape
sift_feat,img_descriptor_index = self.sift_meathod(data,size,name)
feat = self.get_img_represent_vq(sift_feat,img_descriptor_index,codebook)
return feat
def extract_features(self,data,size,val,name):
fearute_meathod = self.cfg.feature_meathod[0]
if fearute_meathod == "hog_meathod":
feat = self.hog_meathod(data,size,orientations=9, pixels_per_cell=(8,8), cells_per_block=(2,2),
block_norm='L2-Hys', visualise=False, transform_sqrt=True)
elif fearute_meathod == "LBP_meathod":
feat = LBP_meathod(data,size)
elif fearute_meathod == "bag_of_visual_word_category_partion_cluster":
feat = self.bag_of_visual_word_category_partion_cluster(data,size,val = val,name = name)
elif fearute_meathod == "bag_of_visual_word_category_partion":
feat = self.bag_of_visual_word_category_partion(data,size,val = val,name = name)
else :
print "please chose correct feature meathod"
return
return feat
def knn_classifier(self,train_x, train_y,test_x,test_y,label_dict,val = False):
from sklearn.neighbors import KNeighborsClassifier
model = KNeighborsClassifier()
model.fit(train_x, train_y)
result = model.predict(test_x)
label_dict_inverse = dict([(v,k) for k,v in label_dict.iteritems()])
target_names = [label_dict_inverse[i] for i in xrange(len(label_dict_inverse))]
print metrics.classification_report(test_y,result,target_names = target_names)
print "(lab,pre):",zip([label_dict_inverse[i] for i in list(test_y)],[label_dict_inverse[i] for i in list(result)])
# SVM Classifier
def svm_classifier(self,train_x, train_y,test_x,test_y,label_dict,val = False):
model_save_path = self.cfg.classifier_save_path
#model = svm.SVC(C=10,gamma=0.0001)
model = svm.SVC()
model.fit(train_x, train_y)
label_dict_inverse = dict([(v,k) for k,v in label_dict.iteritems()])
target_names = [label_dict_inverse[i] for i in xrange(len(label_dict_inverse))]
if val == True:
result = model.predict(train_x)
print metrics.classification_report(train_y,result,target_names = target_names)
result = model.predict(test_x)
print metrics.classification_report(test_y,result,target_names = target_names)
print "(lab,pre):",zip([label_dict_inverse[i] for i in list(test_y)],[label_dict_inverse[i] for i in list(result)])
# # prob = model.predict_proba(test_x)
# test_score = model.score(test_x,test_y)
# # print test_y
# # print "prob:\n",prob
# if val:
# train_score = model.score(train_x,train_y)
# print "train_accuracy:",train_score
# print "test_accuracy:",test_score
# model = svm.LinearSVC()#kernel="linear",probability = True)
# param_grid = {'C': [1e3, 5e3, 1e4, 5e4, 1e5],
# 'gamma': [0.0001, 0.0005, 0.001, 0.005, 0.01, 0.1]}
# model = GridSearchCV(svm.SVC(kernel='rbf'), param_grid)
# model = svm.SVC(probability = False)
# model.fit(train_x, train_y)
# result = model.predict(test_x)
# print "%s"%(metrics.classification_report(test_y,result))
# correct_prediction = np.equal(test_y,result)
# accuracy = np.mean(correct_prediction.astype(float))
# print correct_prediction
# print accuracy
def PCA_meathod(self,test):
if test == False:
PCA_save_path = self.cfg.PCA_save_path
#n_components = 500
# pca = PCA(n_components = 420)
pca = PCA()
self.train_features = pca.fit_transform(self.train_features)
self.test_features = pca.transform(self.test_features)
explained_variance_ratio_ = np.cumsum(pca.explained_variance_ratio_)
print explained_variance_ratio_
print np.where(abs(explained_variance_ratio_-1.0) < 1e-5)
if not os.path.exists(os.path.dirname(PCA_save_path)):
os.makedirs(os.path.dirname(PCA_save_path))
cPickle.dump(pca,open(PCA_save_path,'wb'))
else:
PCA_save_path = self.cfg.PCA_save_path
if not os.path.exists(PCA_save_path):
print "does not exists pca model,please train data first"
pca = cPickle.load(open(PCA_save_path,'rb'))
self.test_features = pca.transform(self.test_features)
def train(self):
print "start img preprocess......"
self.imgs_preprocess()
print "start extact features......"
print "start extact train set features......"
self.train_features = self.extract_features(self.train_imgs,self.cfg.img_size,val = False,name = "train")
print "start extact val set features......"
self.test_features = self.extract_features(self.test_imgs,self.cfg.img_size,val = True,name = "val")
# self.train_features = self.bag_of_visual_word(self.train_imgs,self.cfg.img_size,val = False,name = "train")
# self.test_features = self.bag_of_visual_word(self.test_imgs,self.cfg.img_size,val = True,name = "val")
# self.train_features = self.bag_of_visual_word_category(self.train_imgs,self.cfg.img_size,val = False,name = "train")
# self.test_features = self.bag_of_visual_word_category(self.test_imgs,self.cfg.img_size,val = True,name = "val")
# self.train_features = self.bag_of_visual_word_category_partion(self.train_imgs,self.cfg.img_size,val = False,name = "train")
# self.test_features = self.bag_of_visual_word_category_partion(self.test_imgs,self.cfg.img_size,val = True,name = "val")
# self.train_features = self.bag_of_visual_word_category_partion_cluster(self.train_imgs,self.cfg.img_size,val = False,name = "train")
# self.test_features = self.bag_of_visual_word_category_partion_cluster(self.test_imgs,self.cfg.img_size,val = True,name = "val")
# self.train_features, self.test_features = self.data_Standardization(self.train_features,self.test_features)
self.PCA_meathod(test = False)
self.train_features, self.test_features = self.data_Standardization(self.train_features,self.test_features)
print "train feature and labels:",self.train_features.shape,self.train_labels.shape
print "test feature and labels",self.test_features.shape,self.test_labels.shape
print "start classifiy features......."
# # # select_classifiers = ['NB', 'KNN', 'LR', 'RF', 'DT', 'SVM','SVMCV', 'GBDT']
# select_classifiers = ['SVM']
select_classifiers = self.cfg.select_classifiers
anmial_classifier(self.cfg,select_classifiers,self.train_features,self.train_labels,self.test_features,self.test_labels,self.label_dict,val = True,test = False)
# self.svm_classifier(self.train_features,self.train_labels,self.test_features,self.test_labels,self.label_dict,val = True)
# self.knn_classifier(self.train_features,self.train_labels,self.test_features,self.test_labels,self.label_dict,val = True)
# pipe = make_pipeline(PCA(),svm.SVC())
# param_grid = {'pca__n_components':[10,20,50,80,100,200,300,400,500,600,700,800,900,1000,1500],
# 'svc__C': [1e-3, 1e-2, 1e-1, 1, 10, 100, 1000,10000], 'svc__gamma': [1,0.1,0.01,0.001, 0.0001]}
# grid_search = GridSearchCV(pipe, param_grid, n_jobs = 1, verbose=1)
# grid_search.fit(train_features, train_labels)
# best_parameters = grid_search.best_estimator_.get_params()
# for para, val in list(best_parameters.items()):
# print(para, val)
# predit = grid_search.predict(test_features,test_labels)
# accuracy = metrics.accuracy_score(test_labels,predict)
# print('accuracy: %.2f%%' % (100 * accuracy))
def test(self):
# get label_dict
label_txt = self.cfg.label_txt
label_list = np.loadtxt(label_txt,dtype=str,delimiter='\n')
self.label_dict = dict(zip(label_list,[i for i in range(len(label_list))])) #get label_dict
self.label_dict_inverse = dict(zip([i for i in range(len(label_list))],label_list)) #get label_dict
#get test_data and label
test_data = self.get_data(self.cfg.test_root,self.label_dict,self.cfg.img_size)
self.test_imgs = test_data[:,0:-1]
self.test_labels = test_data[:,-1].astype(int)
#get feature
self.test_features = self.extract_features(self.test_imgs,self.cfg.img_size,val = True,name = "val")
self.PCA_meathod(test = True)
self.test_features = preprocessing.scale(self.test_features)
print "test feature and labels",self.test_features.shape,self.test_labels.shape
# classifiy
select_classifiers = self.cfg.select_classifiers
anmial_classifier(self.cfg,select_classifiers,None,None,self.test_features,self.test_labels,self.label_dict,val = True,test = True)
def main():
# cfg = config_file()
if cfg.state == 'TRAIN':
anmial_clf = object_classfication(cfg)
anmial_clf.train()
elif cfg.state == "TEST":
if not os.path.exists(cfg.test_root):
print "test_root is not exists,or rename the test_root"
return
anmial_clf = object_classfication(cfg)
anmial_clf.test()
else:
print "config state must be 'TRAIN' or 'TEST'"
if __name__ == "__main__":
main()