A memorandum on how to use keras.preprocessing.image in Keras

I tried various things such as loading and manipulating images in keras.preprocessing.image.

Loading images

You can load the image using the load_img function. Since the image is read in PIL format, it is necessary to convert it to numpy array type when using it for NN training, prediction, etc.

python


import keras
keras.preprocessing.image.load_img(
    path, grayscale=False, color_mode="rgb", target_size=None, interpolation="nearest"
)
argument Description How to specify
path Image path(Absolute or relative) Path string
grayscale Grayscale setting but officially deprecated, color_Should be specified in mode True or False
color_mode You can specify the format of the loaded image.'grayscale','rgb','rgba'There isDefaultIs'rgb' 'grayscale'Or'rgb'Or'rgba'
target_size Specify the size of the image to be read with an int tuple ( width , height )
interpolation How to complement the image. Original image and target_Complement the image when there is a difference in size."nearest", "bilinear", "bicubic", "lanczos", "box", "hamming"Types of complementation methods are supported.DefaultThen"nearest" "nearest"Or"bilinear"Or"bicubic"Or"lanczos"Or"box"Or"hamming"

Practice

Loading images


from keras.preprocessing.image import load_img
import matplotlib.pyplot as plt

#Basically path,color_code,target_Just set the size
#The loading itself is completed only here!
img = load_img("taiju.jpg ", color_mode='rgb', target_size=(700,700))
array = img_to_array(img) 
gray_img = load_img("taiju.jpg ", color_mode='grayscale', target_size=(28,28))
gray_array = img_to_array(gray_img)

testlist=[]
testlist.append(array)
testlist.append(gray_array)
imglist=[]
imglist.append(img)
imglist.append(gray_img)

#Format when used in neural networks using mnist
print("array format:",array.shape)
print("gray_array format:",gray_array.shape)
#Displaying images using matplot
for i in range(2):
  plt.subplot(1,5,i+1)
  plt.imshow(imglist[i],'gray')
plt.show

When you do this, you get: load_img.png The first image is the image read with ** img = load_img ("taiju.jpg ", color_mode ='rgb', target_size = (700,700)) **, and is read with the original image size of 700 pixels in full color. Because it is, it is displayed clearly. The second image is the image read with ** gray_img = load_img ("taiju.jpg ", color_mode ='grayscale', target_size = (28,28)) **, converted to grayscale and reduced to 28 pixels vertically and horizontally. Is displayed. Therefore, the image looks unclear and rough. In addition, it can be converted to numpy array type by using ** img_to_array ** function, and it can be used for prediction by model created by neural network.

Recommended Posts

A memorandum on how to use keras.preprocessing.image in Keras
Memorandum on how to use gremlin python
A memorandum because I stumbled on trying to use MeCab in Python
How to use classes in Theano
How to use Dataiku on Windows
How to use SQLite in Python
Notes on how to use pywinauto
Notes on how to use featuretools
How to use homebrew on Debian
How to use Mysql in python
How to use ChemSpider in Python
How to use PubChem in Python
Notes on how to use doctest
How to use MultiIndex (personal memorandum)
How to use GitHub on a multi-person server without a password
How to use Fujifilm X-T3 as a webcam on Ubuntu 20.04
How to use the __call__ method in a Python class
How to use VS Code in venv environment on windows
A note on how to load a virtual environment in PyCharm
Notes on how to use marshmallow in the schema library
How to use calculated columns in CASTable
[Introduction to Python] How to use class in Python?
How to use mecab, neologd-ipadic on colab
How to use Google Test in C
[TF] How to use Tensorboard from Keras
How to use Google Assistant on Windows 10
How to use python put in pyenv on macOS with PyCall
[Introduction to Python] How to use the in operator in a for statement?
How to get a stacktrace in python
How to use Anaconda interpreter in PyCharm
How to use __slots__ in Python class
How to deploy a Django app on heroku in just 5 minutes
[V11 ~] A memorandum to put in Misskey
How to test on a Django-authenticated page
How to use regular expressions in Python
How to use Map in Android ViewPager
How to use is and == in Python
How to use the C library in Python
A simple example of how to use ArgumentParser
How to clear tuples in a list (Python)
How to embed a variable in a python string
How to use Python Image Library in python3 series
How to use Python Kivy ④ ~ Execution on Android ~
How to create a JSON file in Python
Summary of how to use MNIST in Python
How to implement a gradient picker in Houdini
How to notify a Discord channel in Python
How to use tkinter with python in pyenv
How to live a decent life on 2017 Windows
[Python] How to draw a histogram in Matplotlib
How to create a Rest Api in Django
How to write a named tuple document in 2020
How to count numbers in a specific range
How to read a file in a different directory
[Windows] Memo to use Keras on GPU [Tensorflow-GPU]
How to Mock a Public function in Pytest
How to use xml.etree.ElementTree
How to use virtualenv
How to use Seaboan
How to use image-match
How to use shogun