Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Attempting an evaluation.py file for a handwritten digit recognizer Here's the code i have so far: # Handwritten digit recognition for MNIST dataset using Convolutional
Attempting an evaluation.py file for a handwritten digit recognizer
Here's the code i have so far:
# Handwritten digit recognition for MNIST dataset using Convolutional Neural Networks
# Step : Import all required keras libraries
from keras.models import loadmodel # This is used to load your saved model
from keras.datasets import mnist # This is used to load mnist dataset later
from keras.utils import nputils # This will be used to convert your test image to a categorical class digit from to
# Step : Load and return training and test datasets
def loaddataset:
# a Load dataset Xtrain, Xtest, ytrain, ytest via imported keras library
Xtrain, ytrainXtest, ytest mnist.loaddata
Xtrain Xtrain.reshapeXtrain.shapeastypefloat
# b reshape for X train and test vars Hint: Xtrain Xtrain.reshapeXtrain.shapeastypefloat
Xtest Xtest.reshapeXtest.shapeastypefloat
# c normalize inputs from to Hint: Xtrain Xtrain
Xtrain Xtrain
Xtest Xtest
# d Convert ytrain and ytest to categorical classes Hint: ytrain nputils.tocategoricalytrain
ytrain nputils.tocategoricalytrain
ytest nputils.tocategoricalytest
# e return your Xtrain, Xtest, ytrain, ytest
return Xtrain, Xtest, ytrain, ytest
# Step : Load your saved model
from tensorflow.keras.models import loadmodel
model loadmodeldigitRecognizerh
# Step : Evaluate your model via yourmodelname.evaluateXtest, ytest, verbose function
testloss, testacc model.evaluatetestimages, testlabels
printfTest accuracy: testacc
# Code below to make a prediction for a new image.
predictions model.predicttestimages
# Step : This section below is optional and can be copied from your digitRecognizer.py file from Step onwards load required keras libraries
from keras.preprocessing.image import loadimg
from keras.preprocessing.image import imgtoarray
# Step : load and normalize new image
def loadnewimagepath:
# a load new image
newImage loadimgpath grayscaleTrue, targetsize
# b Convert image to array
newImage imgtoarraynewImage
# c reshape into a single sample with channel similar to how you reshaped in loaddataset function
# d normalize image data Hint: newImage newImage
# e return newImage
# Step : load a new image and predict its class
def testmodelperformance:
# a Call the above load image function
img loadnewimageyournewimagefilepath'
# b load your CNN model digitRecognizerh file
# c predict the class Hint: imageClass yourmodelname.predictclassesimg
# d Print prediction result
printimageClass
# Step : Test model performance here by calling the above testmodelperformance function
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started