![]() |
The primary objective will be to build a classification model which will be able to identify the different categories of the fashion industry from the Fashion MNIST dataset using Tensorflow and Keras To complete our objective, we will create a CNN model to identify the image categories and train it on the dataset. We are using deep learning as a method of choice since the dataset consists of images, and CNN’s have been the choice of algorithm for image classification tasks. We will use Keras to create CNN and Tensorflow for data manipulation tasks. The task will be divided into three steps data analysis, model training and prediction. Let us start with data analysis. Data AnalysisStep 1: Importing the required libraries We will first import all the required libraries to complete our objective. To show images, we will use matplotlib, and for array manipulations, we will use NumPy. Tensorflow and Keras will be used for ML and deep learning stuff. Python3
The Fashion MNIST dataset is readily made available in the keras.dataset library, so we have just imported it from there. The dataset consists of 70,000 images, of which 60,000 are for training, and the remaining are for testing purposes. The images are in grayscale format. Each image consists of 28×28 pixels, and the number of categories is 10. Hence there are 10 labels available to us, and they are as follows:
Step 2: Loading data and auto-splitting it into training and test We will load out data using the load_dataset function. It will return us with the training and testing dataset split mentioned above. Python3
The train contains data from 60,000 images, and the test contains data from 10,000 images Step 3: Visualise the data As we have loaded the data, we will visualize some sample images from it. To view the images, we will use the iterator to iterate and, in Matplotlib plot the images. Python3
With this, we have come to the end of the data analysis. Now we will move forward to model training. Model trainingStep 1: Creating a CNN architecture We will create a basic CNN architecture from scratch to classify the images. We will be using 3 convolution layers along with 3 max-pooling layers. At last, we will add a softmax layer of 10 nodes as we have 10 labels to be identified. Python3
Now we will see the model summary. To do that, we will first compile our model and set out loss to sparse categorical crossentropy and metrics as sparse categorical accuracy. Python3
![]() Model summary Step 2: Train the data on the model As we have compiled the model, we will now train our model. To do this, we will use mode.fit() function and set the epochs to 10. We will also perform a validation split of 33% to get better test accuracy and have a minimum loss. Python3
Step 3: Save the model We will now save the model in the .h5 format so it can be bundled with any web framework or any other development domain. Python3
Step 4: Plotting the training and loss functions Training and loss functions are important functions in any ML project. they tell us how well the model performs under how many epochs and how much time the model takes actually to converge. Python3
Output: Python3
Output: PredictionNow we will use model.predict() to get the prediction. It will return an array of size 10, consisting of the labels’ probabilities. The max probability of the label will be the answer. Python3
Output: ![]()
|
Reffered: https://www.geeksforgeeks.org
AI ML DS |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 13 |