-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCNNandXgboost.py
135 lines (102 loc) · 4.02 KB
/
CNNandXgboost.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
import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)
from subprocess import check_output
print(check_output(["ls", "../input"]).decode("utf8"))
import os
import re
import tensorflow as tf
import tensorflow.python.platform
from tensorflow.python.platform import gfile
import numpy as np
import pandas as pd
import sklearn
from sklearn import cross_validation
from sklearn.metrics import accuracy_score, confusion_matrix
from sklearn.svm import SVC, LinearSVC
import matplotlib.pyplot as plt
import pickle
from sklearn.calibration import CalibratedClassifierCV
from sklearn import svm
model_dir = ''
# all training images
images_dir = './train'
list_images = [images_dir+f for f in os.listdir(images_dir) if re.search('jpg|JPG', f)]
# setup tensorFlow graph initiation
def create_graph():
with gfile.FastGFile(os.path.join(model_dir, 'output.pb'), 'rb') as f:
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
_ = tf.import_graph_def(graph_def, name='')
# extract all features from pool layer of InceptionV3
def extract_features(list_images):
nb_features = 2048
features = np.empty((len(list_images),nb_features))
labels = []
create_graph()
with tf.Session() as sess:
next_to_last_tensor = sess.graph.get_tensor_by_name('pool_3:0')
for ind, image in enumerate(list_images):
print('Processing %s...' % (image))
if not gfile.Exists(image):
tf.logging.fatal('File does not exist %s', image)
image_data = gfile.FastGFile(image, 'rb').read()
predictions = sess.run(next_to_last_tensor,
{'DecodeJpeg/contents:0': image_data})
features[ind,:] = np.squeeze(predictions)
labels.append(re.split('_\d+',image.split('/')[1])[0])
return features, labels
features,labels = extract_features(list_images)
pickle.dump(features, open('features', 'wb'))
pickle.dump(labels, open('labels', 'wb'))
features = pickle.load(open('features'))
labels = pickle.load(open('labels'))
# run a 10-fold CV SVM using probabilistic outputs.
X_train, X_test, y_train, y_test = cross_validation.train_test_split(features, labels, test_size=0.1, random_state=0)
clf = svm.SVC(kernel='linear', C=1).fit(X_train, y_train)
clf.score(X_test, y_test)
# probabalistic SVM
clf = sklearn.calibration.CalibratedClassifierCV(svm)
clf.fit(X_train, y_train)
y_pred = clf.predict_proba(X_test)
k_fold = KFold(len(labels),n_folds=10, shuffle=False, random_state=0)
C_array=[0.001,0.01,0.1,1,10]
C_scores=[]
for k in C_array:
clf = svm.SVC(kernel='linear', C=k)
scores= cross_val_score(clf, features, labels, cv=k_fold, n_jobs=-1)
C_scores.append(scores.mean())
print (C_scores)
#C = 0.1 is best
#clf = svm.LinearSVC(C=0.1)
clf = svm.SVC(kernel='linear', C=0.1,probability=True)
# final_model = clf.fit(features, labels)
final_model = CalibratedClassifierCV(clf,cv=10,method='sigmoid')
final_model = clf.fit(features, labels)
test_dir='./test_stg1/'
list_images = [test_dir+f for f in os.listdir(test_dir) if re.search('jpg|JPG', f)]
def extract_features(list_images):
nb_features = 2048
features = np.empty((len(list_images),nb_features))
create_graph()
with tf.Session() as sess:
next_to_last_tensor = sess.graph.get_tensor_by_name('pool_3:0')
for ind, image in enumerate(list_images):
print('Processing %s...' % (image))
if not gfile.Exists(image):
tf.logging.fatal('File does not exist %s', image)
image_data = gfile.FastGFile(image, 'rb').read()
predictions = sess.run(next_to_last_tensor,
{'DecodeJpeg/contents:0': image_data})
features[ind,:] = np.squeeze(predictions)
return features
features_test = extract_features(list_images)
y_pred = final_model.predict_proba(features_test)
#y_pred = final_model.predict(features_test)
#y_pred = final_model.predict(features_test)
image_id = [i.split('/')[3] for i in list_images]
submit = open('submit.SVM.csv','w')
submit.write('image,ALB,BET,DOL,LAG,NoF,OTHER,SHARK,YFT\n')
for idx, id_n in enumerate(image_id):
probs=['%s' % p for p in list(y_pred[idx, :])]
submit.write('%s,%s\n' % (str(image_id[idx]),','.join(probs)))
submit.close()