forked from Ridhya483/Pneumonia-Classification
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_preprocessing.py
161 lines (93 loc) · 3.38 KB
/
data_preprocessing.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
import numpy as np
import cv2
import os
TRAIN_DIR = "C:/Users/RIDZZ ZOLDYCK/Desktop/pneumonia_data/chest_xray/train"
VAL_DIR = "C:/Users/RIDZZ ZOLDYCK/Desktop/pneumonia_data/chest_xray/val"
TEST_DIR = "C:/Users/RIDZZ ZOLDYCK/Desktop/pneumonia_data/chest_xray/test"
CATEGORIES = ["NORMAL" , "PNEUMONIA"]
img_size = 150
train_data = []
test_data = []
val_data = []
def create_training_data():
for category in CATEGORIES:
path = os.path.join(TRAIN_DIR,category)
class_num = CATEGORIES.index(category)
for img in os.listdir(path):
img_arr = cv2.imread(os.path.join(path,img) , cv2.IMREAD_GRAYSCALE)
img_arr = cv2.resize(img_arr , (img_size , img_size))
train_data.append([img_arr , class_num])
def create_validation_data():
for category in CATEGORIES:
path = os.path.join(VAL_DIR,category)
class_num = CATEGORIES.index(category)
for img in os.listdir(path):
img_arr = cv2.imread(os.path.join(path,img) , cv2.IMREAD_GRAYSCALE)
img_arr = cv2.resize(img_arr , (img_size , img_size))
val_data.append([img_arr , class_num])
def create_test_data():
for category in CATEGORIES:
path = os.path.join(TEST_DIR,category)
class_num = CATEGORIES.index(category)
for img in os.listdir(path):
img_arr = cv2.imread(os.path.join(path,img) , cv2.IMREAD_GRAYSCALE)
img_arr = cv2.resize(img_arr , (img_size , img_size))
test_data.append([img_arr , class_num])
create_training_data()
create_validation_data()
create_test_data()
print(" \n\nNumber of images in Training Dataset : " , len(train_data))
print(" \n\nNumber of images in Validation Dataset : " , len(val_data))
print(" \n\nNumber of images in Test Dataset : " , len(test_data))
import random
random.shuffle(train_data)
random.shuffle(test_data)
random.shuffle(val_data)
print(" \n\n Some labels after shuffling : " )
print("\nTrain Data : ")
for sample in train_data[:5]:
print(sample[1])
print("\nTest Data : ")
for sample in test_data[:5]:
print(sample[1])
print("\nValidation Data : ")
for sample in val_data[:5]:
print(sample[1])
x_train = []
y_train = []
x_test = []
y_test = []
x_val = []
y_val = []
for features , label in train_data :
x_train.append(features)
y_train.append(label)
for features , label in test_data :
x_test.append(features)
y_test.append(label)
for features , label in val_data :
x_val.append(features)
y_val.append(label)
x_train = np.array(x_train).reshape(-1,img_size,img_size,1)
x_test = np.array(x_test).reshape(-1,img_size,img_size,1)
x_val = np.array(x_val).reshape(-1,img_size,img_size,1)
import pickle
pickle_out = open("X_TRAIN.pickle" , "wb")
pickle.dump(x_train , pickle_out)
pickle_out.close()
pickle_out = open("X_TEST.pickle" , "wb")
pickle.dump(x_test , pickle_out)
pickle_out.close()
pickle_out = open("X_VAL.pickle" , "wb")
pickle.dump(x_val , pickle_out)
pickle_out.close()
pickle_out = open("Y_TRAIN.pickle" , "wb")
pickle.dump(y_train , pickle_out)
pickle_out.close()
pickle_out = open("Y_TEST.pickle" , "wb")
pickle.dump(y_test , pickle_out)
pickle_out.close()
pickle_out = open("Y_VAL.pickle" , "wb")
pickle.dump(y_val , pickle_out)
pickle_out.close()
print(" \n\n Features and labels of all datasets have been saved ")