[Keras] Personal memo to classify images by folder [Python]

Sort images by folder

environment

Python: 3.6.10 Keras: 2.2.4 Tensorflow: 1.14.0 numpy: 1.16.4 sklearn: 0.22.2

Creating an npy file from an image

create_datasets.py


from sklearn import model_selection
from PIL import Image
import os, glob
import numpy as np
import random


#Classification category
name = "name"
root_dir = "./datasets/" + name +"/"
savenpy = "./npy/"+name+".npy"
categories = os.listdir(root_dir)
nb_classes = len(categories)
image_size = 224

#Read image data for each folder
X = [] #image data
Y = [] #Label data
for idx, category in enumerate(categories):
    dir_path = root_dir + category
    search_files = os.listdir(dir_path)
    for file in search_files:
        filepath = dir_path + "/" + file
        img = Image.open(filepath)
        img = img.convert("RGB")
        #img = img.convert("L")
        img = img.resize((image_size, image_size))
        data = np.asarray(img)
        X.append(data)
        Y.append(idx)

X = np.array(X)
Y = np.array(Y)
print(len(X), len(Y))

# #Separate training data and test data
X_train, X_test, y_train, y_test = model_selection.train_test_split(X, Y, test_size=0.2)
xy = (X_train, X_test, y_train, y_test)

#Save Numpy array to file
np.save(savenpy, xy)
print("end")

Creating a model

create_model.py


import keras
from keras.models import Sequential
from keras.layers import Convolution2D, MaxPooling2D, Conv2D
from keras.layers import Activation, Dropout, Flatten, Dense
from keras.utils import np_utils
import numpy as np
from keras.callbacks import EarlyStopping

#Category to be classified
name = "name"
root_dir = "./datasets/" + name +"/"
loadnpy = "./npy/"+name+".npy"
categories = os.listdir(root_dir)
nb_classes = len(categories)

#Define the main function
def main():
    X_train,X_test,y_train,y_test = np.load(loadnpy, allow_pickle=True)
    #Image file normalization
    X_train = X_train.astype('float') / 256
    X_test = X_test.astype('float') / 256
    y_train = np_utils.to_categorical(y_train, nb_classes)
    y_test = np_utils.to_categorical(y_test, nb_classes)

    model = model_train(X_train,y_train)
    model_eval(model,X_test,y_test)

#Learn model
def model_train(X,y):
    model = Sequential()
    model.add(Conv2D(32,(3,3), padding='same',input_shape=X.shape[1:]))
    model.add(Activation('relu'))
    model.add(Conv2D(32,(3,3)))
    model.add(Activation('relu'))
    model.add(MaxPooling2D(pool_size=(2,2)))
    model.add(Dropout(0.25))

    model.add(Conv2D(64,(3,3),padding='same'))
    model.add(Activation('relu'))
    model.add(Conv2D(64,(3,3)))
    model.add(Activation('relu'))
    model.add(MaxPooling2D(pool_size=(2,2)))
    model.add(Dropout(0.25))

    model.add(Flatten())
    model.add(Dense(512))
    model.add(Activation('relu'))
    model.add(Dropout(0.5))
    model.add(Dense(nb_classes))

    model.add(Activation('softmax'))

    model.summary()

    #Optimization method
    opt = keras.optimizers.rmsprop(lr=0.0001,decay=1e-6)

    #Compiling the model
    model.compile(loss='categorical_crossentropy',
                    optimizer=opt,metrics=['accuracy'])

    #Model learning
    model.fit(X, y, batch_size=32,epochs=100)

    #Save model
    model.save('./save_model.h5')

    return model

#Evaluate the model
def model_eval(model, X, y):
    score = model.evaluate(X, y)
    print('loss=', score[0])
    print('accuracy=', score[1])

if __name__ == "__main__":
    main()

Use the created model to identify one

from keras.models import load_model
import numpy as np
from keras.preprocessing.image import img_to_array, load_img

img_path = 'Image file path you want to identify (jpg/png file)'
model_file_path='Model file path (h5 file)'

root_dir = "./datasets/" + name +"/"#Path with the name of the folder to classify
categories = os.listdir(root_dir)#Get label from folder name

model=load_model(model_file_path)

img = img_to_array(load_img(img_path, target_size=(224,224)))
img_nad = img_to_array(img)/255
img_nad = img_nad[None, ...]

pred = model.predict(img_nad, batch_size=1, verbose=0)
score = np.max(pred)
pred_label = categories[np.argmax(pred[0])]
print('name:',pred_label)
print('score:',score)

Recommended Posts

[Keras] Personal memo to classify images by folder [Python]
Function to save images by date [python3]
Challenge image classification by TensorFlow2 + Keras 5 ~ Observe images that fail to classify ~
Visualization memo by Python
I wanted to classify Shadowverse card images by reader class
[Python] Try to classify ramen shops by natural language processing
How to use cron (personal memo)
How to collect images in Python
Python learning memo for machine learning by Chainer Chapter 8 Introduction to Numpy
[Personal memo] Python virtual environment command memo
Python learning memo for machine learning by Chainer Chapter 10 Introduction to Cupy
[Python] A memo to operate ROM created by GBDK with PyBoy
Post images from Python to Tumblr
[Personal memo] Python sequence type / mapping type
Python memo ① Folder and file operations
Interval scheduling learning memo ~ by python ~
[Nanonets] How to post Memo [Python]
Python learning memo for machine learning by Chainer Chapter 9 Introduction to scikit-learn
A memo organized by renaming the file names in the folder with python
Convert memo at once with Python 2to3
Reintroduction to Python Decorators ~ Learn Decorators by Type ~
Answer to AtCoder Beginners Selection by Python3
[Learning memo] Basics of class by python
Memo to ask for KPI with python
Attempt to classify emoji fonts with Keras
Recommended books by 3 types related to Python
Try to classify O'Reilly books by clustering
Add Gaussian noise to images with python2.7
Organize data divided by folder with Python
Python & Machine Learning Study Memo ④: Machine Learning by Backpropagation
Upload images to Google Drive with Python
Python memo
How to plot multiple fits images side by side in galactic coordinates using python
python memo
Python memo
python memo
I made a module in C language to filter images loaded by Python
Python memo
Python memo
Python memo
Memo # 4 for Python beginners to read "Detailed Python Grammar"
[Personal memo] julia --Using Python library with julia using PyCall
[python] How to display list elements side by side
Convert PDFs to images in bulk with Python
Memo # 3 for Python beginners to read "Detailed Python Grammar"
Personal notes to doc Python code in Sphinx
Memo # 1 for Python beginners to read "Detailed Python Grammar"
[Python] Memo to translate Matplotlib into Japanese [Windows]
How to erase the characters output by Python
Memo # 2 for Python beginners to read "Detailed Python Grammar"
How to get the files in the [Python] folder
[I want to classify images using Tensorflow] (2) Let's classify images
[Python] How to sort instances by instance variables
I want to sell Mercari by scraping python
Memo # 7 for Python beginners to read "Detailed Python Grammar"
A memo for creating a python environment by a beginner
progate Python learning memo (updated from time to time)
Memo # 6 for Python beginners to read "Detailed Python Grammar"
[Windows] Memo to use Keras on GPU [Tensorflow-GPU]
Execute Power Query by passing arguments to Python
I tried to classify dragon ball by adaline