[PYTHON] Creating a Tensorflow Sequential model with original images added to MNIST

I created a model with the number symbol "-" added to the MNIST image.

SnapCrab_NoName_2020-6-7_19-10-52_No-00.png

mk_model.py


from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import Dropout
from keras.layers import Flatten
from keras.layers.convolutional import Conv2D
from keras.layers.convolutional import MaxPooling2D
from keras.optimizers import Adam
from keras.utils import np_utils
from keras import backend as Keras
from PIL import Image, ImageFilter
import numpy as np
import numpy
import os
import matplotlib.pyplot as plt
import cv2
import keras




def load_images_to_data(image_label, image_directory, features_data, label_data):
    ####The contents of the function will be published in a paid note! !!####

(X_train, y_train), (X_test, y_test) = mnist.load_data()

# Reshaping to format which CNN expects (batch, height, width, channels)
X_train = X_train.reshape(X_train.shape[0], X_train.shape[1], X_train.shape[2], 1).astype('float32')
X_test = X_test.reshape(X_test.shape[0], X_test.shape[1], X_test.shape[2], 1).astype('float32')


X_train, y_train = load_images_to_data('10', '-', X_train, y_train)
X_test, y_test = load_images_to_data('10', '-', X_test, y_test)


# normalize inputs from 0-255 to 0-1
X_train/=255
X_test/=255

# one hot encode
number_of_classes = 11
y_train = np_utils.to_categorical(y_train, number_of_classes)
y_test = np_utils.to_categorical(y_test, number_of_classes)

# create model
model = Sequential()
model.add(Conv2D(32, (5, 5), input_shape=(X_train.shape[1], X_train.shape[2], 1), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(32, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.5))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(number_of_classes, activation='softmax'))

# Compile model
model.compile(loss='categorical_crossentropy', optimizer=Adam(), metrics=['accuracy'])

# Fit the model
model.fit(X_train, y_train, validation_data=(X_test, y_test), epochs=7, batch_size=200)

model.save('my_model.h5')

-For the symbol image, first create a model with only 10 added, and then simply create it.

SnapCrab_NoName_2020-6-7_18-19-16_No-00.png

The test has been completed successfully.

SnapCrab_NoName_2020-6-7_19-47-59_No-00.png

Recommended Posts

Creating a Tensorflow Sequential model with original images added to MNIST
Creating a learning model using MNIST
Try TensorFlow RNN with a basic model
I tried to make something like a chatbot with the Seq2Seq model of TensorFlow
[Day 9] Creating a model
The first step to creating a serverless application with Zappa
User is not added successfully after creating a custom User model
[Introduction to Tensorflow] Understand Tensorflow properly and try to make a model
I tried to divide with a deep learning language model
Try TensorFlow MNIST with RNN
I came up with a way to create a 3D model from a photo Part 01 Creating an environment
Python: Introduction to Flask: Creating a number identification app using MNIST
TensorFlow To learn from a large number of images ... ~ (almost) solution ~
A note I was addicted to when creating a table with SQLAlchemy
Set up a node to do MNIST on ROS using Tensorflow
MNIST (DCNN) with Keras (TensorFlow backend)
Customize Model / Layer / Metric with TensorFlow
Creating a decision tree with scikit-learn
Creating a Flask server with Docker
Creating a simple app with flask
How to convert Tensorflow model to Lite
Make a model iterator with PySide
How to process camera images with Teams and Zoom Sentiment analysis with Tensorflow
I was addicted to creating a Python venv environment with VS Code
A series of amateur infrastructure engineers touching Django with Docker (2): Creating a model
I came up with a way to make a 3D model from a photo.
TensorFlow To learn from a large number of images ... (Unsolved problem) → 12/18 Solved