<jupyter_start><jupyter_text>Neural Networks> Basic image classification- toc: true - badges: true- comments: true- categories: [jupyter]- image:  IntroductionNeural networks are very popular for image classification tasks. ObjectiveWe want to classify the images in the [CIFAR10](https://www.cs.toronto.edu/~kriz/cifar.html) dataset using a simple neural network. The dataset contains 60,000 images in 10 classes.ApproachHere we use a basic 2-layer neural network. The 2-dimensional image is flattened and each pixel of the image is fed to the network. Two hidden layers with Rectified Linear Units (ReLUs) process the pixels and adjust the network weights using the [Adam optimizer](https://www.geeksforgeeks.org/intuition-of-adam-optimizer/), allowing us to classify the learned features into ten categories. 1.Import Libraries<jupyter_code>import tensorflow as tf
import matplotlib.pyplot as plt
import numpy as np
from tensorflow.keras.optimizers import RMSprop
from tensorflow.keras import Model<jupyter_output><empty_output><jupyter_text>2. Load the datasetCIFAR10 dataset is readily accessible through keras.<jupyter_code>(train_img, train_lbl), (test_img, test_lbl) = tf.keras.datasets.cifar10.load_data()<jupyter_output><empty_output><jupyter_text>3. Scale imagesNormalize or scale the images to [0 1] scale.<jupyter_code>train_img, test_img = train_img/255.0, test_img/255.0

print('Size of training images: ', train_img.shape)
print('Size of test images: ', test_img.shape)
num_categories = len(np.unique(np.append(np.array(test_lbl),np.array(train_lbl))))
print('Number of categories: ', num_categories)<jupyter_output>Size of training images:  (50000, 32, 32, 3)
Size of test images:  (10000, 32, 32, 3)
Number of categories:  10<jupyter_text>4. Create model- Flatten the 2-dimensional image.- Add a few dense layers  (two in this case). Use the relu activation function.- Specify the output with 10 output categories and a [softmax](https://machinelearningmastery.com/softmax-activation-function-with-python/) activation function.- Build the model.- Compile the model using the Adam optimizer and using a sparse categorical crossentropy [loss function](https://www.analyticsvidhya.com/blog/2021/05/guide-for-loss-function-in-tensorflow/). Use accuracy as the metric for gauging performance.- Fit the model to the training data, and evaluare on the test images.<jupyter_code># simple model
simple_model = tf.keras.Sequential([
                             tf.keras.layers.Flatten(),
                             tf.keras.layers.Dense(128, activation = tf.nn.relu),
                             tf.keras.layers.Dense(32, activation = tf.nn.relu),
                             tf.keras.layers.Dense(num_categories, activation = tf.nn.softmax)
])
simple_model.build([None, 32, 32, 3])
simple_model.summary()
simple_model.compile(optimizer = tf.optimizers.Adam(), loss ='sparse_categorical_crossentropy', metrics=['accuracy'])
history = simple_model.fit(x = train_img, y = train_lbl, epochs = 20, validation_data = (test_img, test_lbl))<jupyter_output>Model: "sequential"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 flatten (Flatten)           (None, 3072)              0         
                                                                 
 dense (Dense)               (None, 128)               393344    
                                                                 
 dense_1 (Dense)             (None, 32)                4128      
                                                                 
 dense_2 (Dense)             (None, 10)                330       
                                                                 
=================================================================
Total params: 397,802
Trainable params: 397,802
Non-trainable params: 0
_________________________________________________________________
Epoch 1/20
1563/1563 [============================[...]<jupyter_text>5. Plot resultsPlot the response (accuracy and loss) by epochs for the training and validation sets.<jupyter_code># plot accuracy and loss
acc=history.history['accuracy']
val_acc=history.history['val_accuracy']
loss=history.history['loss']
val_loss=history.history['val_loss']
epochs=range(len(acc))

plt.plot(epochs, acc, 'o-r', label='Training')
plt.plot(epochs, val_acc, 'o--b', label='Validation')
plt.title('Accuracy: Training and Validation')
plt.legend(loc="lower right")
plt.figure()

plt.plot(epochs, loss, 'o-r', label='Training')
plt.plot(epochs, val_loss, 'o--b', label='Validation')
plt.title('Loss: Training and Validation')
plt.legend(loc="upper right")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12952500523121993, 'fraction_numerical': 0.02594685080560787, 'mean_word_length': 3.3415077202543144, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 20, 'https://': 4, 'lorem ipsum': 0, 'www.': 3, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '517907', 'license': 'Apache-2.0', 'n_tokens_mistral': 1428, 'n_tokens_neox': 1292, 'n_words': 378, 'path': '_notebooks/2022-02-05-neural-networks.ipynb', 'repo_name': 'joshisaumil/ml-blog', 'size': '4779'}