-
Notifications
You must be signed in to change notification settings - Fork 0
/
DataUtility.py
357 lines (250 loc) · 11.1 KB
/
DataUtility.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
# Import global packages and libraries that are required
# by the methods included in the file.
import numpy as np
import pandas as pd
import os
import random
from skimage import io
from skimage.transform import resize
import datetime
import glob
import random as rn
import gc
import matplotlib.image as img
import matplotlib.pyplot as plt
import cv2
from scipy import stats
from statistics import mean
from statistics import median
import seaborn as sns
from concurrent import futures
import threading
from collections import Counter
from numpy import expand_dims
from IPython.display import Markdown
from enum import Enum
class SKIN_CANCER_TUMOR_TYPE(Enum):
'''
An ENUM class to contain the enumeration values for Red Blood Cell categories (Parasitized or Uninfected)
in this study.
Tumors can be benign (noncancerous) or malignant (cancerous).
'''
BENIGN = 'benign'
MALIGNANT = 'malignant'
class dataUtils():
'''
A Utility/Helper class that contains helper methods for some exploratry image based analysis.
The methods majorly includes extracting the statistical level details of the images and plotting
various pre-processing and augmentation level transformations on sample images.
'''
def __init__(self):
pass
@staticmethod
def PrintMarkdownText(textToDisplay):
'''
Purpose:
A static method to display markdown formatted output like bold, italic bold etc..
Parameters:
1. textToDisplay - the string message with formatting styles that is to be displayed
Return Value:
NONE
'''
display(Markdown('<br>'))
display(Markdown(textToDisplay))
def GetLabelledSkinCancerData(self):
'''
Purpose:
Creates a dataframe with the filenames of all the skin cancer images and the
corresponding labels. The dataframe has 2 columns - 'filename' and 'label'
Parameters:
NONE
Return Value:
The computed skin cancer dataframe.
'''
benign_images = glob.glob('ISIC Skin Cancer/images/benign/*.jpg')
malignant_images = glob.glob('ISIC Skin Cancer/images/malignant/*.jpg')
len(benign_images), len(malignant_images)
skin_cancer_df = pd.DataFrame({
'filename': benign_images + malignant_images,
'label': ['Benign'] * len(benign_images) + ['Malignant'] * len(malignant_images)
})
# Shuffle the rows in the dataset
skin_cancer_df = skin_cancer_df.sample(frac=1, random_state=34).reset_index(drop=True)
return skin_cancer_df
def GetImageDirectory(self, imageCategory):
'''
Purpose:
Gets the directory path for the category of image (benign/malignant) in the dataset.
Parameters:
1. imgCategory - The category of the image (benign/malignant). See SKIN_CANCER_TUMOR_TYPE ENUM.
Return Value:
The image directory path.
'''
# Get the correct directory path based on the category of image
if imageCategory == SKIN_CANCER_TUMOR_TYPE.BENIGN.value:
imgDirectory = 'ISIC Skin Cancer/images/benign/*.jpg'
else:
imgDirectory = 'ISIC Skin Cancer/images/malignant/*.jpg'
return imgDirectory
def GetAllImageShape(self, imageCategory):
'''
Purpose:
Extract the Image dimensions of the images in the dataset for the given imageCategory
(benign/malignant). As the number of images are large, this method utilizes parallel
processing using the ThreadPoolExecutor for faster computation.
Parameters:
1. imgCategory - The category of the image (benign/malignant). See SKIN_CANCER_TUMOR_TYPE ENUM.
Return Value:
A list of dinemsions of all the images of the concerned imageCategory.
'''
images = []
'''A nested function to get the image shape that is called parallely from the ThreadPoolExecutor
Parameter:
img - The image that is to be resized.'''
def GetImageShape(img):
return cv2.imread(img).shape
# Get the correct directory path based on the category of image
imgDirectory = self.GetImageDirectory(imageCategory)
for img_path in glob.glob(imgDirectory):
images.append(img_path)
# https://docs.python.org/3/library/concurrent.futures.html for details on max_workers
executer = futures.ThreadPoolExecutor(max_workers=None)
all_image_dimension_map = executer.map(GetImageShape, [img for img in images])
return Counter(all_image_dimension_map)
def ReadAllImages(self, imageList, resizeImage = False, newImageSize = None):
'''
Purpose:
Resizes all the images passed in to the new dimension defined in 'newImageSize'.
As the number of images are large, this method utilizes parallel processing using the
ThreadPoolExecutor for faster computation.
Parameters:
1. imageList - The list of all the images that are to be re-sized.
2. newImageSize - The size to which the images have to be re-sized.
Return Value:
List of the resized images.
'''
'''A nested function to resize the image to the specified new dimension
and is called parallely from the ThreadPoolExecutor.
Parameter:
img - The image that is to be resized.'''
def ResizeImage(img):
img = cv2.imread(img)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
# if resize is set to True, then resize the image to the new dimension.
if resizeImage:
img = cv2.resize(img, dsize = newImageSize, interpolation = cv2.INTER_CUBIC)
img = np.array(img, dtype=np.float32)
return img
# https://docs.python.org/3/library/concurrent.futures.html for details on max_workers
executer = futures.ThreadPoolExecutor(max_workers=None)
img_data_map = executer.map(ResizeImage, [image for image in imageList])
return np.array(list(img_data_map))
def ReadAndDisplayInputImages(self, imageCategory, numImagesToDisplay):
'''
Purpose:
Read and display the first 5 images of the imageCategory (benign/malignant)
in the dataset.
Parameters:
1. imgCategory - The category of the image (benign/malignant). See SKIN_CANCER_TUMOR_TYPE ENUM.
2. numImagesToDisplay - Total number of images to display.
Return Value:
NONE
'''
images = []
# Get the correct directory path based on the category of image
imgDirectory = self.GetImageDirectory(imageCategory)
# Read the first 5 images
for img_path in glob.glob(imgDirectory):
if len(images) < numImagesToDisplay:
images.append(img.imread(img_path))
# Display the images
plt.figure(figsize=(20,10))
columns = 5
for i, image in enumerate(images):
plt.subplot(len(images) / columns + 1, columns, i + 1)
plt.imshow(image)
plt.axis('off')
def DisplayAnnotatedImages(self, df, numImagesToDisplay):
'''
Purpose:
Display the given number of annotated images from the passed in dataframe of image
filenames and labels.
Parameters:
1. df - The dataset which contails the filenames and the corresponding labels.
2. numImagesToDisplay - Total number of images to display.
Return Value:
NONE
'''
images = []
labels = []
# Get the correct directory path based on the category of image
# imgDirectory = self.GetImageDirectory(imageCategory)
# Read the first 'numImagesToDisplay' images.
for img_path in df.filename:
if len(images) < numImagesToDisplay:
# extract the corresponding image label
label = df.loc[df['filename'] == img_path].label
labels.append(label)
images.append(img.imread(img_path))
# Display the images
plt.figure(figsize=(20,10))
columns = 5
for i, image in enumerate(images):
plt.subplot(len(images) / columns + 1, columns, i + 1)
plt.imshow(image)
plt.title(labels[i].item())
plt.axis('off')
def ComputeAndPlotImageDimensionalStatistics(self, imgCategory):
'''
Purpose:
Computed and displays the dimensional statistics of the images in the directory and
plots the distribution for the X and Y dimensional component.
Parameters:
1. imgCategory - The category of the image (benign/malignant). See SKIN_CANCER_TUMOR_TYPE ENUM.
Return Value:
NONE
'''
dim_x = []
dim_y = []
allImageDims = self.GetAllImageShape(imgCategory)
setOfUniqueDimensions = set(allImageDims)
for shape in allImageDims:
x,y,channel = shape
dim_x.append(x)
dim_y.append(y)
f, ax = plt.subplots(1, 2)
f.set_figwidth(10)
sns.distplot(dim_x, kde=True, fit=stats.gamma, ax=ax[0]);
sns.distplot(dim_y, kde=True, fit=stats.gamma, ax=ax[1]);
ax[0].title.set_text('Distribution of X Dimension')
ax[1].title.set_text('Distribution of Y Dimension')
plt.show()
print('Statistical Features - Image Dimension:')
print('---------------------------------------')
print('Max X Dimension:', max(dim_x))
print('Min X Dimension:', min(dim_x))
print('Mean X Dimension:', mean(dim_x))
print('Median X Dimension:', median(dim_x))
print('---------------------------------------')
print('Max Y Dimension:', max(dim_y))
print('Min Y Dimension:', min(dim_y))
print('Mean Y Dimension:', mean(dim_y))
print('Median Y Dimension:', median(dim_y))
print('\nTotal # Images with Unique Dimensions:', len(setOfUniqueDimensions))
def GetSampleImage(self, imgCategory):
'''
Purpose:
Reads and returns the first image of the given category - (benign/malignant).
Parameters:
1. imgCategory - The category of the image (benign/malignant). See SKIN_CANCER_TUMOR_TYPE ENUM.
Return Value:
NONE
'''
images = []
# Get the correct directory path based on the category of image
imgDirectory = self.GetImageDirectory(imgCategory)
# Read and return the first image in the directory.
for img_path in glob.glob(imgDirectory):
if len(images) < 1:
images.append(img.imread(img_path))
return images[0]