https://www.tensorflow.org/api_docs/python/tf/keras/preprocessing/image/ImageDataGenerator
import sys
import os
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.preprocessing.image import ImageDataGenerator, load_img
sys.path.append("/home/ia-serveur/ia/my_lib_functions")
#sys.path.append("/home/ia/ia/my_lib_functions")
from my_functions import *
assert hasattr(tf, "function") # Be sure to use tensorflow 2.X
print('tensorflow :',tf.__version__)
first_function()
tensorflow : 2.3.1 This is the first function
filenames = os.listdir("./data/")
categories_none = ['none']
Img_Data_Gen_df = pd.DataFrame(
{
'filename' : filenames,
'category' : categories_none
}
)
abstract(Img_Data_Gen_df)
print('\nImg_Data_Gen_df',Img_Data_Gen_df)
Img_Data_Gen_df.head()
# reset the index of the DataFrame (Pandas)
Img_Data_Gen_df = Img_Data_Gen_df.reset_index(drop = True)
Abstract: Class=pandas DataFrame Shape=(1, 2) Type=int64 Nb_Bytes=16 Img_Data_Gen_df filename category 0 datagenerator.jpg none
targets_names = []
Images_With = 128 #128
Images_Height = 128 #128
Images_channel = 3
Images_Size = (Images_With, Images_Height)
print('Images_With =',Images_With,'- Images_Height =',Images_Height,'- Images_channel =',Images_channel)
Images_With = 128 - Images_Height = 128 - Images_channel = 3
images_generator_1 = ImageDataGenerator(rescale = 1.0/255)
images_generator_1 = images_generator_1.flow_from_dataframe(Img_Data_Gen_df,
"./data/",
x_col = 'filename',
y_col = 'category',
batch_size = 1)
Found 1 validated image filenames belonging to 1 classes.
get_firts_from_ImageDataGenerator(images_generator_1, targets_names)
Distribution Data First Image From ImageDataGenerator type ImageDataGenerator = <class 'tuple'> - Shape= (1, 256, 256, 3) Abstract: Class=ndarray Shape=(256, 256, 3) Type=float32 Nb_Bytes=786 Kb Abstract: Class=ndarray Shape=(1,) Type=float32 Nb_Bytes=4 (Max) = 1.00 (Min) = 0.00 (Mean) = 0.26 (Std) = 0.21
images_generator_2 = ImageDataGenerator(rescale = 1.0/255,
samplewise_center = True)
images_generator_2 = images_generator_2.flow_from_dataframe(Img_Data_Gen_df,
"./data/",
x_col = 'filename',
y_col = 'category',
batch_size = 1)
Found 1 validated image filenames belonging to 1 classes.
def get_firts_from_ImageDataGenerator_1(resultdata, label):
import numpy as np
import matplotlib.pyplot as plt
x0 = resultdata
a0 = x0[0]
x = resultdata.next()
a = x[0]
b = x[1]
print("Distribution Data First Image From ImageDataGenerator\n")
print('type ImageDataGenerator =',type(x)," - Shape=",a0[0].shape)
abstract(a[0])
abstract(b[0])
print("\n")
print("(Max) = {t:3.2f}".format(t=a[0].max()))
print("(Min) = {t:3.2f}".format(t=a[0].min()))
print("(Mean) = {t:3.2f}".format(t=a[0].mean()))
print("(Std) = {t:3.2f}".format(t=a[0].std()))
plt.figure(figsize=(16, 4))
plt.subplot(1, 2, 1)
#plt.imshow(a[0])
t = (a[0] * 255).astype(np.uint8)
#t = np.array(a[0],np.int32)
plt.imshow(t)
if len(b[0]) <= 1:
plt.title('none')
else:
index_label = np.argmax(b[0])
plt.title(label[index_label])
plt.colorbar()
plt.subplot(1, 2, 2)
plt.hist(a[0].ravel(), color='gray', bins=100)
plt.title('Histogram Distribution Data')
plt.xlabel('Valeur Data')
plt.ylabel('Frequency')
plt.grid(True)
plt.show()
get_firts_from_ImageDataGenerator(images_generator_2, targets_names)
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
Distribution Data First Image From ImageDataGenerator type ImageDataGenerator = <class 'tuple'> - Shape= (1, 256, 256, 3) Abstract: Class=ndarray Shape=(256, 256, 3) Type=float32 Nb_Bytes=786 Kb Abstract: Class=ndarray Shape=(1,) Type=float32 Nb_Bytes=4 (Max) = 0.74 (Min) = -0.26 (Mean) = -0.00 (Std) = 0.21
images_generator_3 = ImageDataGenerator(rescale = 1.0/255,
samplewise_std_normalization = True)
/home/ia/anaconda3/envs/ia/lib/python3.7/site-packages/keras_preprocessing/image/image_data_generator.py:356: UserWarning: This ImageDataGenerator specifies `samplewise_std_normalization`, which overrides setting of `samplewise_center`. warnings.warn('This ImageDataGenerator specifies '
images_generator_3 = images_generator_3.flow_from_dataframe(Img_Data_Gen_df,
"./data/",
x_col = 'filename',
y_col = 'category',
batch_size = 1)
Found 1 validated image filenames belonging to 1 classes.
get_firts_from_ImageDataGenerator(images_generator_3, targets_names)
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
Distribution Data First Image From ImageDataGenerator type ImageDataGenerator = <class 'tuple'> - Shape= (1, 256, 256, 3) Abstract: Class=ndarray Shape=(256, 256, 3) Type=float32 Nb_Bytes=786 Kb Abstract: Class=ndarray Shape=(1,) Type=float32 Nb_Bytes=4 (Max) = 3.47 (Min) = -1.23 (Mean) = -0.00 (Std) = 1.00
def result_img(resultat):
plt.figure(figsize=(15,15))
for i in range(6):
plt.subplot(5,6,i+1)
batch = resultat.next()
img = batch[0]
plt.imshow(img[0])
plt.show()
images_generator_4 = ImageDataGenerator(rescale = 1.0/255,
width_shift_range = [-0.5, 0.5])
images_generator_4 = images_generator_4.flow_from_dataframe(Img_Data_Gen_df,
"./data/",
x_col = 'filename',
y_col = 'category',
batch_size = 1)
Found 1 validated image filenames belonging to 1 classes.
result_img(images_generator_4)
images_generator_5 = ImageDataGenerator(rescale = 1.0/255,
height_shift_range = [-100, 100])
images_generator_5 = images_generator_5.flow_from_dataframe(Img_Data_Gen_df,
"./data/",
x_col = 'filename',
y_col = 'category',
batch_size = 1)
Found 1 validated image filenames belonging to 1 classes.
result_img(images_generator_5)
images_generator_6 = ImageDataGenerator(rescale = 1.0/255,
horizontal_flip = True)
images_generator_6 = images_generator_6.flow_from_dataframe(Img_Data_Gen_df,
"./data/",
x_col = 'filename',
y_col = 'category',
batch_size = 1)
Found 1 validated image filenames belonging to 1 classes.
result_img(images_generator_6)
images_generator_7 = ImageDataGenerator(rescale = 1.0/255,
vertical_flip = True)
images_generator_7 = images_generator_7.flow_from_dataframe(Img_Data_Gen_df,
"./data/",
x_col = 'filename',
y_col = 'category',
batch_size = 1)
Found 1 validated image filenames belonging to 1 classes.
result_img(images_generator_7)
images_generator_8 = ImageDataGenerator(rescale = 1.0/255,
rotation_range = 90)
images_generator_8 = images_generator_8.flow_from_dataframe(Img_Data_Gen_df,
"./data/",
x_col = 'filename',
y_col = 'category',
batch_size = 1)
Found 1 validated image filenames belonging to 1 classes.
result_img(images_generator_8)
images_generator_9 = ImageDataGenerator(rescale = 1.0/255,
brightness_range = [0.2, 1.0])
images_generator_9 = images_generator_9.flow_from_dataframe(Img_Data_Gen_df,
"./data/",
x_col = 'filename',
y_col = 'category',
batch_size = 1)
Found 1 validated image filenames belonging to 1 classes.
result_img(images_generator_9)
images_generator_10 = ImageDataGenerator(rescale = 1.0/255,
zoom_range = [0.5, 1.0])
images_generator_10 = images_generator_10.flow_from_dataframe(Img_Data_Gen_df,
"./data/",
x_col = 'filename',
y_col = 'category',
batch_size = 1)
Found 1 validated image filenames belonging to 1 classes.
result_img(images_generator_10)