Created
July 20, 2021 07:16
-
-
Save therealnaveenkamal/67e3a3145988fd539eb3df0c0f224d0b to your computer and use it in GitHub Desktop.
This code standardizes and normalizes the images using ImageDataGenerator Class of Keras to make the intensity mean 0 and intensity standard deviation 1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from keras.preprocessing.image import ImageDataGenerator | |
#To standardize the images, we use samplewise_center = True and samplewize_std_normalization = True | |
traingen = ImageDataGenerator(samplewise_center=True, samplewise_std_normalization= True) | |
traingenerator = traingen.flow_from_dataframe( | |
dataframe=trainset, | |
directory="images", | |
x_col="Image", | |
y_col= labels, | |
class_mode="raw", | |
batch_size= 1, | |
shuffle=False, | |
target_size=(512,512) | |
) | |
#Standardizing featurewise as we don't process images as groups rather one-by-one | |
imagegen = ImageDataGenerator().flow_from_dataframe(dataframe = trainset, directory = "images", x_col = "Image", y_col = labels, class_mode = "raw", batch_size= 1, shuffle=False, target_size=(512,512)) | |
train_sample = imagegen.next()[0] | |
imagegen = ImageDataGenerator(featurewise_center=True, featurewise_std_normalization= True) | |
imagegen.fit(train_sample) | |
valgenerator = imagegen.flow_from_dataframe(dataframe = valset, directory = "images", x_col = "Image", y_col = labels, class_mode = "raw", batch_size= 1, shuffle=False, target_size=(512,512)) | |
testgenerator = imagegen.flow_from_dataframe(dataframe = testset, directory = "images", x_col = "Image", y_col = labels, class_mode = "raw", batch_size= 1, shuffle=False, target_size=(512,512)) | |
#Taking a random sample standardized image | |
item, value = traingenerator.__getitem__(num) | |
plt.figure(figsize=(15, 15)) | |
plt.imshow(item[0], cmap = 'gray') | |
plt.colorbar() | |
#Plotting the histogram of original and standardized pixel intensities | |
fig, ax = plt.subplots(figsize=(25, 10)) | |
plt.xlabel("Pixel Values") | |
print("Mean of Pixel Values - Standardized: ", item[0].mean()) | |
print("Standard Deviation of Pixel Values - Standardized: ", item[0].std()) | |
print("Mean of Pixel Values - Sample: ", sample.mean()) | |
print("Standard Deviation of Pixel Values - Sample: ", sample.std()) | |
sns.histplot(item[0].ravel(), ax = ax, kde = False) | |
sns.histplot(sample.ravel(), ax = ax, kde = False, color = "red") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment