AI01, Loading and manipulating images with deep learning framework
Back to the previous page |page management
List of posts to read before reading this article
Contents
keras
How to Load an Image with Keras
# example of loading an image with the Keras API
from keras.preprocessing.image import load_img
# load the image
img = load_img('beach.jpg')
# report details about the image
print(type(img))
print(img.format)
print(img.mode)
print(img.size)
# show the image
img.show()
OUTPUT
<class 'PIL.JpegImagePlugin.JpegImageFile'>
JPEG
RGB
(640, 427)
Argument1 : grayscale
# example of loading an image with the Keras API
from keras.preprocessing.image import load_img
# load the image
img = load_img('beach.jpg', grayscale=True)
print(img.mode)
img
OUTPUT
L
Argument2 : color_mode
# example of loading an image with the Keras API
from keras.preprocessing.image import load_img
# load the image
img = load_img('beach.jpg', color_mode='rgba')
print(img.mode)
img
OUTPUT
RGBA
Argument3 : target_size
# example of loading an image with the Keras API
from keras.preprocessing.image import load_img
# load the image
img = load_img('beach.jpg', target_size=(100,100))
print(img.size)
img
OUTPUT
(100, 100)
How to Convert an Image With Keras
# example of converting an image with the Keras API
from keras.preprocessing.image import load_img
from keras.preprocessing.image import img_to_array
from keras.preprocessing.image import array_to_img
import numpy as np
from PIL import Image
# load the image
img = load_img('beach.jpg')
print(type(img))
# convert to numpy array
img_ndarray = np.asarray(img).astype('float32')
img_array = img_to_array(img)
print(img_ndarray.dtype)
print(img_ndarray.shape)
print(img_array.dtype)
print(img_array.shape)
# convert back to image
img_pil1 = array_to_img(img_array)
img_pil2 = img_array.astype(np.uint8)
img_pil2 = Image.fromarray(img_pil2)
OUTPUT
<class 'PIL.JpegImagePlugin.JpegImageFile'>
float32
(427, 640, 3)
float32
(427, 640, 3)
print(type(img_ndarray))
print(img_ndarray.shape)
img_ndarray
OUTPUT
<class 'numpy.ndarray'>
(427, 640, 3)
array([[[ 47., 107., 195.],
[ 47., 107., 195.],
[ 46., 106., 194.],
...,
[ 31., 97., 191.],
[ 30., 96., 190.],
[ 29., 95., 189.]],
[[ 46., 106., 194.],
[ 47., 107., 195.],
[ 47., 107., 195.],
...,
[ 31., 97., 191.],
[ 31., 97., 191.],
[ 30., 96., 190.]],
[[ 46., 106., 194.],
[ 48., 108., 196.],
[ 51., 108., 197.],
...,
[ 30., 96., 190.],
[ 31., 97., 191.],
[ 30., 96., 190.]],
...,
[[ 1., 1., 3.],
[ 1., 1., 3.],
[ 3., 3., 1.],
...,
[130., 149., 155.],
[136., 155., 161.],
[135., 152., 160.]],
[[ 0., 1., 0.],
[ 1., 2., 0.],
[ 1., 2., 0.],
...,
[123., 143., 144.],
[129., 148., 152.],
[131., 148., 155.]],
[[ 1., 0., 5.],
[ 0., 0., 4.],
[ 0., 1., 0.],
...,
[122., 142., 141.],
[126., 146., 145.],
[129., 147., 149.]]], dtype=float32)
print(type(img_array))
print(img_array.shape)
img_array
OUTPUT
<class 'numpy.ndarray'>
(427, 640, 3)
array([[[ 47., 107., 195.],
[ 47., 107., 195.],
[ 46., 106., 194.],
...,
[ 31., 97., 191.],
[ 30., 96., 190.],
[ 29., 95., 189.]],
[[ 46., 106., 194.],
[ 47., 107., 195.],
[ 47., 107., 195.],
...,
[ 31., 97., 191.],
[ 31., 97., 191.],
[ 30., 96., 190.]],
[[ 46., 106., 194.],
[ 48., 108., 196.],
[ 51., 108., 197.],
...,
[ 30., 96., 190.],
[ 31., 97., 191.],
[ 30., 96., 190.]],
...,
[[ 1., 1., 3.],
[ 1., 1., 3.],
[ 3., 3., 1.],
...,
[130., 149., 155.],
[136., 155., 161.],
[135., 152., 160.]],
[[ 0., 1., 0.],
[ 1., 2., 0.],
[ 1., 2., 0.],
...,
[123., 143., 144.],
[129., 148., 152.],
[131., 148., 155.]],
[[ 1., 0., 5.],
[ 0., 0., 4.],
[ 0., 1., 0.],
...,
[122., 142., 141.],
[126., 146., 145.],
[129., 147., 149.]]], dtype=float32)
print(type(img_pil1))
print(img_pil1.format)
print(img_pil1.mode)
print(img_pil1.size)
img_pil1
OUTPUT
<class 'PIL.Image.Image'>
None
RGB
(640, 427)
print(type(img_pil2))
print(img_pil2.format)
print(img_pil2.mode)
print(img_pil2.size)
img_pil2
OUTPUT
<class 'PIL.Image.Image'>
None
RGB
(640, 427)
How to Save an Image With Keras
# example of saving an image with the Keras API
from keras.preprocessing.image import load_img
from keras.preprocessing.image import save_img
from keras.preprocessing.image import img_to_array
# load image as as grayscale
img = load_img('beach.jpg', color_mode='grayscale')
# convert image to a numpy array
img_array = img_to_array(img)
# save the image with a new filename
save_img('bondi_beach_grayscale.jpg', img_array)
# load the image to confirm it was saved correctly
img = load_img('bondi_beach_grayscale.jpg')
print(type(img))
print(img.format)
print(img.mode)
print(img.size)
img.show()
OUTPUT
<class 'PIL.Image.Image'>
None
RGB
(640, 427)
How to Progressively Load Images
flow_from_directory
from keras.preprocessing.image import ImageDataGenerator
datagen = ImageDataGenerator()
train_iterator = datagen.flow_from_directory('data/train/', class_mode='binary', batch_size=64)
val_iterator = datagen.flow_from_directory('data/validation/', class_mode='binary', batch_size=64)
test_iterator = datagen.flow_from_directory('data/test/', class_mode='binary', batch_size=64)
SUPPLEMENT
Example of proposed directory structure for the image dataset.
- data/train/
- data/train/red/
- data/train/blue/
- data/test/
- data/test/red/
- data/test/blue/
- data/validation/
- data/validation/red/
- data/validation/blue/
import keras
from keras.preprocessing.image import ImageDataGenerator
from keras.utils import plot_model
from keras.models import Model
from keras.layers import Input, Dense, Flatten
from keras.layers.convolutional import Conv2D
from keras.layers.pooling import MaxPooling2D
"""data preprocessing"""
# create a data generator
datagen = ImageDataGenerator()
# Example of creating dataset iterators from an image data generator
# load and iterate training dataset
train_iterator = datagen.flow_from_directory('data/train/', class_mode='binary', batch_size=64)
val_iterator = datagen.flow_from_directory('data/validation/', class_mode='binary', batch_size=64)
test_iterator = datagen.flow_from_directory('data/test/', class_mode='binary', batch_size=64)
"""model design"""
visible = Input(shape=(256,256,3))
conv1 = Conv2D(32, (4,4), activation='relu')(visible)
pool1 = MaxPooling2D()(conv1)
conv2 = Conv2D(16, (4,4), activation='relu')(pool1)
pool2 = MaxPooling2D()(conv2)
flat1 = Flatten()(pool2)
hidden1 = Dense(10, activation='relu')(flat1)
output = Dense(1, activation='sigmoid')(hidden1)
model = Model(inputs=visible, outputs=output)
model.compile(loss=keras.losses.binary_crossentropy, optimizer=keras.optimizers.Adadelta(), metrics=['accuracy'])
model.fit_generator(train_iterator, epochs=10,steps_per_epoch=16, validation_data=val_it, validation_steps=8)
"""evaluation"""
loss = model.evaluate_generator(test_iterator, steps=24)
yhat = model.predict_generator(test_iterator, steps=24)
OUTPUT
flow_from_dataframe
import pandas as pd
from keras.preprocessing.image import ImageDataGenerator
train_df = pd.read_csv('train_dataset.csv')
val_df = pd.read_csv('val_dataset.csv')
test_df = pd.read_csv('test_dataset.csv')
train_datagen = ImageDataGenerator(rescale=1./255, width_shift_range=0.3, zoom_range=0.2, horizontal_flip=True)
val_datagen = ImageDataGenerator(rescale=1./255)
test_datagen = ImageDataGenerator(rescale=1./255)
train_generator = train_datagen.flow_from_dataframe(train_df, x_col='path', y_col='class', target_size=(32,32,3)[:2], batch_size=32)
val_generator = val_datagen.flow_from_dataframe(val_df, x_col='path', y_col='class', target_size=(32,32,3)[:2], batch_size=32)
test_generator = test_datagen.flow_from_dataframe(test_df, x_col='path', y_col='class', target_size=(32,32,3)[:2], batch_size=32)
SUPPLEMENT
Example of proposed directory structure for the image dataset.
- data/train/
- data/test/
- data/validation/
from glob import glob
import os
import numpy as np
import pandas as pd
import tensorflow as tf
from tensorflow.keras import layers
from tensorflow.keras import datasets
from tensorflow.keras.preprocessing.image import ImageDataGenerator
def get_class_name(path):
image_name = os.path.basename(path)
image_name = image_name.split('_')[-1]
image_name = image_name.replace('.png','')
return image_name
'''data preprocessing'''
# path
train_paths = glob('cifar/train/*.png')
test_paths = glob('cifar/test/*.png')
# class name
train_classes_name = [get_class_name(path) for path in train_paths]
test_classes_name = [get_class_name(path) for path in test_paths]
# dataframe : 'path' + 'class name'
train_df = pd.DataFrame({'path':train_paths, 'class':train_classes_name})
test_df = pd.DataFrame({'path':test_paths, 'class':test_classes_name})
# save .csv(format)
train_df.iloc[:40000,:].to_csv('train_dataset.csv', index=False)
train_df.iloc[40000:50000,:].to_csv('val_dataset.csv', index=False)
test_df.to_csv('test_dataset.csv', index=False)
# load .csv(format)
train_df = pd.read_csv('train_dataset.csv')
val_df = pd.read_csv('val_dataset.csv')
test_df = pd.read_csv('test_dataset.csv')
# generator
train_datagen = ImageDataGenerator(rescale=1./255, width_shift_range=0.3, zoom_range=0.2, horizontal_flip=True)
val_datagen = ImageDataGenerator(rescale=1./255)
test_datagen = ImageDataGenerator(rescale=1./255)
train_generator = train_datagen.flow_from_dataframe(train_df, x_col='path', y_col='class', target_size=(32,32,3)[:2], batch_size=32)
val_generator = val_datagen.flow_from_dataframe(val_df, x_col='path', y_col='class', target_size=(32,32,3)[:2], batch_size=32)
test_generator = test_datagen.flow_from_dataframe(test_df, x_col='path', y_col='class', target_size=(32,32,3)[:2], batch_size=32)
'''model design'''
inputs = layers.Input((32,32,3))
net = layers.Conv2D(32, (3, 3), padding='SAME')(inputs)
net = layers.Activation('relu')(net)
net = layers.Conv2D(32, (3, 3), padding='SAME')(net)
net = layers.Activation('relu')(net)
net = layers.MaxPooling2D(pool_size=(2, 2))(net)
net = layers.Dropout(0.7)(net)
net = layers.Conv2D(64, (3, 3), padding='SAME')(net)
net = layers.Activation('relu')(net)
net = layers.Conv2D(64, (3, 3), padding='SAME')(net)
net = layers.Activation('relu')(net)
net = layers.MaxPooling2D(pool_size=(2, 2))(net)
net = layers.Dropout(0.7)(net)
net = layers.Flatten()(net)
net = layers.Dense(512)(net)
net = layers.Activation('relu')(net)
net = layers.Dropout(0.7)(net)
net = layers.Dense(10)(net)
net = layers.Activation('softmax')(net)
model = tf.keras.Model(inputs=inputs, outputs=net, name='Basic_CNN')
model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.001), # Optimization
loss='categorical_crossentropy', # Loss Function
metrics=['accuracy']) # Metrics / Accuracy
model.fit_generator(
train_generator,
steps_per_epoch=len(train_generator),
epochs=10,
validation_data=val_generator,
validation_steps=len(val_generator))
"""evaluation"""
loss = model.evaluate_generator(test_generator, steps=24)
yhat = model.predict_generator(test_generator, steps=24)
tensorflow
pytorch
List of posts followed by this article
Reference