-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBT.py
96 lines (88 loc) · 2.56 KB
/
BT.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
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
import os
import cv2
from sklearn.linear_model import LogisticRegression
from sklearn.svm import SVC
import warnings
from sklearn.decomposition import PCA
import joblib
path = os.listdir('./archive/Training')
classes = {'notumor':0, 'pituitary':1}
X = []
Y = []
for cls in classes:
pth = './archive/Training/'+cls
for j in os.listdir(pth):
img = cv2.imread(pth+'/'+j, 0)
img = cv2.resize(img, (200,200))
X.append(img)
Y.append(classes[cls])
X = np.array(X)
Y = np.array(Y)
np.unique(Y)
pd.Series(Y).value_counts()
X.shape
plt.imshow(X[0], cmap='gray')
X_updated = X.reshape(len(X), -1)
X_updated.shape
xtrain, xtest, ytrain, ytest = train_test_split(X_updated, Y, random_state=10,
test_size=.20)
xtrain.shape, xtest.shape
print(xtrain.max(), xtrain.min())
print(xtest.max(), xtest.min())
xtrain = xtrain/255
xtest = xtest/255
print(xtrain.max(), xtrain.min())
print(xtest.max(), xtest.min())
print(xtrain.shape, xtest.shape)
pca = PCA(.98)
# pca_train = pca.fit_transform(xtrain)
# pca_test = pca.transform(xtest)
pca_train = xtrain
pca_test = xtest
warnings.filterwarnings('ignore')
lg = LogisticRegression(C=0.1)
lg.fit(pca_train, ytrain)
sv = SVC()
sv.fit(pca_train, ytrain)
print("Training Score:", lg.score(pca_train, ytrain))
print("Testing Score:", lg.score(pca_test, ytest))
print("Training Score:", sv.score(pca_train, ytrain))
print("Testing Score:", sv.score(pca_test, ytest))
pred = sv.predict(pca_test)
np.where(ytest!=pred)
pred[36]
ytest[36]
dec = {0:'No Tumor', 1:'Positive Tumor'}
plt.figure(figsize=(12,8))
p = os.listdir('./archive/Testing')
c=1
for i in os.listdir('./archive/Testing/notumor/')[:9]:
plt.subplot(3,3,c)
img = cv2.imread('./archive/Testing/notumor/'+i,0)
img1 = cv2.resize(img, (200,200))
img1 = img1.reshape(1,-1)/255
p = sv.predict(img1)
plt.title(dec[p[0]])
plt.imshow(img, cmap='gray')
plt.axis('off')
c+=1
plt.figure(figsize=(12,8))
p = os.listdir('./archive/Testing/')
# c=1
# for i in os.listdir('./archive/Testing/pituitary')[:16]:
# plt.subplot(4,4,c)
# img = cv2.imread('./archive/Testing/pituitary'+i,0)
# img1 = cv2.resize(img, (200,200))
# img1 = img1.reshape(1,-1)/255
# p = sv.predict(img1)
# plt.title(dec[p[0]])
# plt.imshow(img, cmap='gray')
# plt.axis('off')
# c+=1
# Save the trained model
joblib.dump(sv, 'trained_model.pkl')