From 429c1862546a2cbda044f459865e6cee7d9aa314 Mon Sep 17 00:00:00 2001 From: Navan Chauhan Date: Sun, 24 May 2020 18:57:49 +0530 Subject: Publish deploy 2020-05-24 18:57 --- .../index.html | 64 ++++++++-------------- 1 file changed, 22 insertions(+), 42 deletions(-) (limited to 'posts/2019-12-08-Image-Classifier-Tensorflow/index.html') diff --git a/posts/2019-12-08-Image-Classifier-Tensorflow/index.html b/posts/2019-12-08-Image-Classifier-Tensorflow/index.html index 76ac4f8..0822e30 100644 --- a/posts/2019-12-08-Image-Classifier-Tensorflow/index.html +++ b/posts/2019-12-08-Image-Classifier-Tensorflow/index.html @@ -1,20 +1,18 @@ -Creating a Custom Image Classifier using Tensorflow 2.x and Keras for Detecting Malaria | Navan Chauhan
4 minute readCreated on December 8, 2019Last modified on January 18, 2020

Creating a Custom Image Classifier using Tensorflow 2.x and Keras for Detecting Malaria

Done during Google Code-In. Org: Tensorflow.

Imports

%tensorflow_version 2.x #This is for telling Colab that you want to use TF 2.0, ignore if running on local machine +Creating a Custom Image Classifier using Tensorflow 2.x and Keras for Detecting Malaria | Navan Chauhan
4 minute readCreated on December 8, 2019Last modified on May 24, 2020

Creating a Custom Image Classifier using Tensorflow 2.x and Keras for Detecting Malaria

Done during Google Code-In. Org: Tensorflow.

Imports

%tensorflow_version 2.x #This is for telling Colab that you want to use TF 2.0, ignore if running on local machine from PIL import Image # We use the PIL Library to resize images -import numpy as np +import numpy as np import os import cv2 -import tensorflow as tf +import tensorflow as tf from tensorflow.keras import datasets, layers, models -import pandas as pd -import matplotlib.pyplot as plt +import pandas as pd +import matplotlib.pyplot as plt from keras.models import Sequential from keras.layers import Conv2D,MaxPooling2D,Dense,Flatten,Dropout

Dataset

Fetching the Data

!wget ftp://lhcftp.nlm.nih.gov/Open-Access-Datasets/Malaria/cell_images.zip !unzip cell_images.zip -
- -

Processing the Data

We resize all the images as 50x50 and add the numpy array of that image as well as their label names (Infected or Not) to common arrays.

data = [] +

Processing the Data

We resize all the images as 50x50 and add the numpy array of that image as well as their label names (Infected or Not) to common arrays.

data = [] labels = [] Parasitized = os.listdir("./cell_images/Parasitized/") @@ -26,7 +24,7 @@ data.append(np.array(size_image)) labels.append(0) except AttributeError: - print("") + print("") Uninfected = os.listdir("./cell_images/Uninfected/") for uninfect in Uninfected: @@ -37,23 +35,17 @@ data.append(np.array(size_image)) labels.append(1) except AttributeError: - print("") -
- -

Splitting Data

df = np.array(data) + print("") +

Splitting Data

df = np.array(data) labels = np.array(labels) (X_train, X_test) = df[(int)(0.1*len(df)):],df[:(int)(0.1*len(df))] (y_train, y_test) = labels[(int)(0.1*len(labels)):],labels[:(int)(0.1*len(labels))] -
- -
s=np.arange(X_train.shape[0]) -np.random.shuffle(s) -X_train=X_train[s] -y_train=y_train[s] -X_train = X_train/255.0 -
- -

Model

Creating Model

By creating a sequential model, we create a linear stack of layers.

Note: The input shape for the first layer is 50,50 which corresponds with the sizes of the resized images

model = models.Sequential() +
s=np.arange(X_train.shape[0]) +np.random.shuffle(s) +X_train=X_train[s] +y_train=y_train[s] +X_train = X_train/255.0 +

Model

Creating Model

By creating a sequential model, we create a linear stack of layers.

Note: The input shape for the first layer is 50,50 which corresponds with the sizes of the resized images

model = models.Sequential() model.add(layers.Conv2D(filters=16, kernel_size=2, padding='same', activation='relu', input_shape=(50,50,3))) model.add(layers.MaxPooling2D(pool_size=2)) model.add(layers.Conv2D(filters=32,kernel_size=2,padding='same',activation='relu')) @@ -66,17 +58,11 @@ model.add(layers.Dropout(0.2)) model.add(layers.Dense(2,activation="softmax"))#2 represent output layer neurons model.summary() -
- -

Compiling Model

We use the adam optimiser as it is an adaptive learning rate optimization algorithm that's been designed specifically for training deep neural networks, which means it changes its learning rate automaticaly to get the best results

model.compile(optimizer="adam", +

Compiling Model

We use the adam optimiser as it is an adaptive learning rate optimization algorithm that's been designed specifically for training deep neural networks, which means it changes its learning rate automaticaly to get the best results

model.compile(optimizer="adam", loss="sparse_categorical_crossentropy", metrics=["accuracy"]) -
- -

Training Model

We train the model for 10 epochs on the training data and then validate it using the testing data

history = model.fit(X_train,y_train, epochs=10, validation_data=(X_test,y_test)) -
- -
Train on 24803 samples, validate on 2755 samples +

Training Model

We train the model for 10 epochs on the training data and then validate it using the testing data

history = model.fit(X_train,y_train, epochs=10, validation_data=(X_test,y_test)) +
Train on 24803 samples, validate on 2755 samples Epoch 1/10 24803/24803 [==============================] - 57s 2ms/sample - loss: 0.0786 - accuracy: 0.9729 - val_loss: 0.0000e+00 - val_accuracy: 1.0000 Epoch 2/10 @@ -97,25 +83,19 @@ 24803/24803 [==============================] - 58s 2ms/sample - loss: 0.0352 - accuracy: 0.9878 - val_loss: 0.0000e+00 - val_accuracy: 1.0000 Epoch 10/10 24803/24803 [==============================] - 58s 2ms/sample - loss: 0.0373 - accuracy: 0.9865 - val_loss: 0.0000e+00 - val_accuracy: 1.0000 -
- -

Results

accuracy = history.history['accuracy'][-1]*100 +

Results

accuracy = history.history['accuracy'][-1]*100 loss = history.history['loss'][-1]*100 val_accuracy = history.history['val_accuracy'][-1]*100 val_loss = history.history['val_loss'][-1]*100 -print( +print( 'Accuracy:', accuracy, '\nLoss:', loss, '\nValidation Accuracy:', val_accuracy, '\nValidation Loss:', val_loss ) -
- -
Accuracy: 98.64532351493835 +
Accuracy: 98.64532351493835 Loss: 3.732407123270176 Validation Accuracy: 100.0 Validation Loss: 0.0 -
- -

We have achieved 98% Accuracy!

Link to Colab Notebook

Tagged with:
+

We have achieved 98% Accuracy!

Link to Colab Notebook

Tagged with:
\ No newline at end of file -- cgit v1.2.3