<jupyter_start><jupyter_text>Building a Image Classification Pipeline<jupyter_code># Importing various libraries and dependencies

import tensorflow as tf
!pip install mitdeeplearning
import mitdeeplearning as mdl

import matplotlib.pyplot as plt
import numpy as np
import random
from tqdm import tqdm   # genearates the progressbar in the notebook

# Check that we are using a GPU, if not switch runtimes
#   using Runtime > Change Runtime Type > GPU
#assert len(tf.config.list_physical_devices('GPU')) > 0<jupyter_output>Requirement already satisfied: mitdeeplearning in /usr/local/lib/python3.7/dist-packages (0.2.0)
Requirement already satisfied: gym in /usr/local/lib/python3.7/dist-packages (from mitdeeplearning) (0.17.3)
Requirement already satisfied: numpy in /usr/local/lib/python3.7/dist-packages (from mitdeeplearning) (1.19.5)
Requirement already satisfied: tqdm in /usr/local/lib/python3.7/dist-packages (from mitdeeplearning) (4.41.1)
Requirement already satisfied: regex in /usr/local/lib/python3.7/dist-packages (from mitdeeplearning) (2019.12.20)
Requirement already satisfied: pyglet<=1.5.0,>=1.4.0 in /usr/local/lib/python3.7/dist-packages (from gym->mitdeeplearning) (1.5.0)
Requirement already satisfied: scipy in /usr/local/lib/python3.7/dist-packages (from gym->mitdeeplearning) (1.4.1)
Requirement already satisfied: cloudpickle<1.7.0,>=1.2.0 in /usr/local/lib/python3.7/dist-packages (from gym->mitdeeplearning) (1.3.0)
Requirement already satisfied: future in /usr/local/lib/python3.7/dist-packag[...]<jupyter_text>1.1 MNIST dataset : Hand Written Data Set<jupyter_code># 1. Importing and arranging the dataset with its corresponding labels.
mnist = tf.keras.datasets.mnist
(train_images, train_labels),(test_images,test_labels) = mnist.load_data()
train_images = (np.expand_dims(train_images,axis=-1)/255.).astype(np.float32)
train_labels = (train_labels).astype(np.int64)
test_images =  (np.expand_dims(test_images,axis=-1)/255.).astype(np.float32)
test_labels = (test_labels).astype(np.int64)


# 2. Plotting the imported dataset.

plt.figure(figsize=(10,10))
random_inds = np.random.choice(60000 ,36)
for i in range(36):
  plt.subplot(6,6,i+1)
  plt.xticks([])
  plt.yticks([])
  plt.grid(False)
  image_ind = random_inds[i]
  plt.imshow(np.squeeze(train_images[image_ind]), cmap=plt.cm.binary)
  plt.xlabel(train_labels[image_ind])
  
# Neural Network for Handwritten digit Classification
# using the "Keras API" =  open-source deep learning library  for tf, "Sequential Class" =linear model
# flatten to be fed into the model


def build_fc_model():
  fc_model = tf.keras.Sequential([
        tf.keras.layers.Flatten(), # converting 28x28 into a 784 pixels
        tf.keras.layers.Dense(128, activation= tf.nn.relu),
        #building the second layer
         tf.keras.layers.Dense(10, activation= tf.nn.softmax)

  ])
  return fc_model

model = build_fc_model()<jupyter_output><empty_output><jupyter_text>Compile the modelBefore training the model, we need to define a few more settings. These are added during the model's [`compile`](https://www.tensorflow.org/api_docs/python/tf/keras/models/Sequentialcompile) step:* *Loss function* — This defines how we measure how accurate the model is during training. As was covered in lecture, during training we want to minimize this function, which will "steer" the model in the right direction.* *Optimizer* — This defines how the model is updated based on the data it sees and its loss function.* *Metrics* — Here we can define metrics used to monitor the training and testing steps. In this example, we'll look at the *accuracy*, the fraction of the images that are correctly classified.We'll start out by using a stochastic gradient descent (SGD) optimizer initialized with a learning rate of 0.1. Since we are performing a categorical classification task, we'll want to use the [cross entropy loss](https://www.tensorflow.org/api_docs/python/tf/keras/metrics/sparse_categorical_crossentropy).You'll want to experiment with both the choice of optimizer and learning rate and evaluate how these affect the accuracy of the trained model.<jupyter_code># Compiling the model which was assembled before

model.compile(optimizer=tf.keras.optimizers.SGD(learning_rate=1e-1), 
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])
# Define the batch size and the number of epochs to use during training
BATCH_SIZE = 64         #Batch size is a term used in machine learning and refers to the number of training examples utilized in one iteration
EPOCHS = 5              #iterations

model.fit(train_images, train_labels, batch_size=BATCH_SIZE, epochs=EPOCHS)
# Evaluating the accuracy of the model 
test_loss , test_acc = model.evaluate(test_images,test_labels)

print('Test Accuracy:', test_acc)<jupyter_output>313/313 [==============================] - 1s 1ms/step - loss: 0.1015 - accuracy: 0.9697
Test Accuracy: 0.9696999788284302
{'fraction_non_alphanumeric': 0.1057633973710819, 'fraction_numerical': 0.030940343781597573, 'mean_word_length': 4.937575030012005, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 19, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '53480', 'license': 'MIT', 'n_tokens_mistral': 1629, 'n_tokens_neox': 1534, 'n_words': 478, 'path': 'OCRrecognisition_CNN.ipynb', 'repo_name': 'RutvijP/MachineLearningResources', 'size': '4945'}