-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreprocessing.py
More file actions
144 lines (119 loc) · 5.07 KB
/
preprocessing.py
File metadata and controls
144 lines (119 loc) · 5.07 KB
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
##############################Importing modules
import re
import os
import numpy as np
import pandas as pd
import seaborn as sns
import tensorflow as tf
import keras.backend as K
import matplotlib.pyplot as plt
import glob
import warnings
import tqdm
from tensorflow.keras.preprocessing.image import ImageDataGenerator, load_img
#####taking note of NON GPU ENABLED DEVICES
os.environ['CUDA_VISIBLE_DEVICES'] = '-1'
if tf.test.gpu_device_name():
print('GPU found')
else:
print("No GPU found")
warnings.filterwarnings("ignore")
class Preprocessing():
def __init__(self):
################## DATA INFORMATION
# Data directory of the project
self.DATA_DIR = '/mnt/d6cf3633-27d8-4655-8a8c-7fc0daf3bb07/1/Documents/machine_learning/Pneumonia detection/Dataset/data-task1'
# dataset respective directory
self.TRAIN_DIR = os.path.join(self.DATA_DIR, "train")
self.TEST_DIR = os.path.join(self.DATA_DIR, "test")
self.VAL_DIR = os.path.join(self.DATA_DIR, "val")
################# HYPERPARAMETER TUNING
self.IMAGE_MIN_DIM = 224
self.IMAGE_MAX_DIM = 224
self.EPOCHS = 4
self.BATCH_SIZE = 30
def plot_random_dataset_images(self) -> None:
'''
Arguments:None
Purpose: helps plot our classes of images for our train,test and val datasets
Return : None
'''
fig, ax = plt.subplots(2, 3, figsize=(15, 7))
ax = ax.ravel()
plt.tight_layout()
#plotting random non-pneumonia and pneumonia images from our train, test, and val to see how much different they look to our eye
for i, dir_ in enumerate(glob.glob(f'{self.DATA_DIR}/*')):
ax[i].imshow(plt.imread(dir_ + '/no_pneumonia/' +next(os.walk(dir_+'/no_pneumonia/'))[2][0]))
ax[i].set_title(f'Set: {dir_.split("/")[-1]}, Condition: no_pneumonia')
ax[i+3].imshow(plt.imread(dir_ + '/pneumonia/' +next(os.walk(dir_+'/pneumonia/'))[2][0]))
ax[i+3].set_title(f'Set: {dir_.split("/")[-1]}, Condition: pneumonia')
def basic_descriptive_of_images(self):
'''
'''
#parsing and storing datasets into variable
non_pneumonia_train = glob.glob(f'{self.TRAIN_DIR}/no_pneumonia/*.png')
pneumonia_train = glob.glob(f'{self.TRAIN_DIR}/no_pneumonia/*.png')
#Basic EDA -Descriptive
for i in glob.glob(f'{self.DATA_DIR}/*'):
non_pneumonia_images_count = len(glob.glob(i+'/no_pneumonia/*.png'))
pneumonia_images_count = len(glob.glob(i+'/pneumonia/*.png'))
# print(f'{i.split("/")[-1]} Dataset \n Non-pneumonia images: {non_pneumonia_images_count} \n pneumonia images: {pneumonia_images_count} \n')
x = ['pneumonia', 'Non-pneumonia']
y = [pneumonia_images_count, non_pneumonia_images_count]
plt.bar(x, y)
plt.title(f'Set:{i.split("/")[-1]} pneumonia vs non pneumonia')
plt.ylabel('Count')
plt.xlabel('Classes')
plt.show()
def data_augmentation(self):
'''
argument:None
purpose:Augmentation expands the size of the dataset by creating a modified version of the existing training set images that helps to increase dataset variation and ultimately improve the ability of the model to predict new images.
return: train_image_gen, test_image_gen
'''
train_image_gen = ImageDataGenerator(
horizontal_flip = True,
vertical_flip = False,
rescale = 1./255,
shear_range = 0.2,
zoom_range = 0.05,
height_shift_range = 0.02,
width_shift_range = 0.02,
rotation_range = 3,
fill_mode = 'nearest'
)
test_image_gen = ImageDataGenerator(
rescale = 1./255
)
return train_image_gen, test_image_gen
def dataset_splitting(self, train_image_gen, test_image_gen):
'''
argument:augmented train images and augmented test images
purpose:get images batches into respective variable sets
return: train_images, test_images, validation_images
'''
train_set = train_image_gen.flow_from_directory(
self.TRAIN_DIR,
target_size=(self.IMAGE_MIN_DIM, self.IMAGE_MAX_DIM),
batch_size = self.BATCH_SIZE,
class_mode = 'binary',
# shuffle=True,
# color_mode="grayscale"
)
test_set = test_image_gen.flow_from_directory(
self.TEST_DIR,
target_size=(self.IMAGE_MIN_DIM, self.IMAGE_MAX_DIM),
batch_size = self.BATCH_SIZE-10,
class_mode = 'binary',
# shuffle=True,
# color_mode="grayscale"
)
valid_set = train_image_gen.flow_from_directory(
self.VAL_DIR,
target_size=(self.IMAGE_MIN_DIM, self.IMAGE_MAX_DIM),
batch_size = self.BATCH_SIZE,
class_mode = 'binary',
# shuffle=True,
# color_mode="grayscale"
)
return train_set, test_set, valid_set