<jupyter_start><jupyter_text>Getting Started with Transfer Learning<jupyter_code>from IPython.display import YouTubeVideo

YouTubeVideo('mPFq5KMxKVw', width=800, height=450)<jupyter_output><empty_output><jupyter_text>Beginner's intro to Malaria ![Malaria_cycle](https://i1.wp.com/www.malariasite.com/wp-content/uploads/2015/02/EID_lec17_slide8-large.jpg?resize=799%2C664&ssl=1)<jupyter_code>YouTubeVideo('3_2TnCqBFcY', width=800, height=450)<jupyter_output><empty_output><jupyter_text>Importing Prerequisite Libraries  Loading Libraries<jupyter_code># This Python 3 environment comes with many helpful analytics libraries installed
# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python
# For example, here's several helpful packages to load

import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

# Input data files are available in the read-only "../input/" directory
# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory

import os
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.applications import MobileNetV2, VGG19
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Conv2D, MaxPool2D, Flatten, Dropout
import time
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from sklearn.model_selection import cross_validate, train_test_split
import matplotlib.pyplot as plt
# You can write up to 5GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using "Save & Run All" 
# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session<jupyter_output><empty_output><jupyter_text>Image Augmentation<jupyter_code>data_generator = ImageDataGenerator(rescale=1.0/255.0, validation_split=0.20)
#valid_data_generator = ImageDataGenerator(rescale=1.0/255.0)
train_generator = data_generator.flow_from_directory(directory= '../input/cell-images-for-detecting-malaria/cell_images/cell_images' ,             
                                                     target_size=(128, 128),
                                                     class_mode='binary',
                                                     subset='training',
                                                           shuffle=True,
                                                     batch_size=64
                                 )

valid_generator = data_generator.flow_from_directory(directory= '../input/cell-images-for-detecting-malaria/cell_images/cell_images',             
                                                     target_size=(128, 128),
                                                     class_mode='binary',
                                                           shuffle = True,
                                                     subset='validation',
                                                     batch_size=64,
                                                    
                                                     )


classes = ['Parasitized', 'Uninfected']<jupyter_output>Found 22048 images belonging to 2 classes.
Found 5510 images belonging to 2 classes.<jupyter_text>Displaying The Images<jupyter_code>sample_training_images, train_label = next(train_generator)
def plotImages(images_arr):
    fig, axes = plt.subplots(1, 5, figsize=(20,20))
    axes = axes.flatten()
    for img, ax in zip(images_arr, axes):
        ax.imshow(img)
        ax.axis('off')
    plt.tight_layout() 
    plt.show()
print('Random Display of Cell images')
plotImages(sample_training_images[:5])<jupyter_output>Random Display of Cell images<jupyter_text>MobileNet Model Developement***Here I will be using MobileNetV2 architecture below I will be showing architecture of different MobileNet models***![mobilenet](https://miro.medium.com/max/1882/1*bqE59FvgpvoAQUMQ0WEoUA.png)<jupyter_code>model = Sequential()
model.add(MobileNetV2(include_top=False, pooling='avg', weights='imagenet', input_shape=(128, 128, 3), classes=2))
model.add(Dense(1, activation='sigmoid'))
model.layers[0].trainable = False
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
model.summary()

early_stop = tf.keras.callbacks.EarlyStopping(monitor='val_loss', patience=2)
history = model.fit_generator(train_generator,
                              steps_per_epoch = len(train_generator),
                              epochs=20,
                              validation_steps = len(valid_generator),
                              validation_data=valid_generator,
                              callbacks = [early_stop]
                              )<jupyter_output>Epoch 1/20
345/345 [==============================] - 81s 234ms/step - loss: 0.3122 - accuracy: 0.8707 - val_loss: 0.2435 - val_accuracy: 0.9029
Epoch 2/20
345/345 [==============================] - 43s 126ms/step - loss: 0.2120 - accuracy: 0.9233 - val_loss: 0.2202 - val_accuracy: 0.9171
Epoch 3/20
345/345 [==============================] - 43s 124ms/step - loss: 0.1920 - accuracy: 0.9314 - val_loss: 0.2075 - val_accuracy: 0.9221
Epoch 4/20
345/345 [==============================] - 42s 123ms/step - loss: 0.1810 - accuracy: 0.9357 - val_loss: 0.2112 - val_accuracy: 0.9227
Epoch 5/20
345/345 [==============================] - 44s 128ms/step - loss: 0.1753 - accuracy: 0.9384 - val_loss: 0.2027 - val_accuracy: 0.9274
Epoch 6/20
345/345 [==============================] - 43s 126ms/step - loss: 0.1689 - accuracy: 0.9410 - val_loss: 0.2034 - val_accuracy: 0.9225
Epoch 7/20
345/345 [==============================] - 43s 123ms/step - loss: 0.1638 - accuracy: 0.9432 - val_loss: 0.1991 - val_ac[...]<jupyter_text>Model metrics plot<jupyter_code>def visualize_training(history, lw = 3):
    plt.figure(figsize=(10,6))
    plt.plot(history.history['accuracy'], label = 'training', marker = '*', linewidth = lw)
    plt.plot(history.history['val_accuracy'], label = 'validation', marker = 'o', linewidth = lw)
    plt.title('Training Accuracy vs Validation Accuracy')
    plt.xlabel('Epochs')
    plt.ylabel('Accuracy')
    plt.legend(fontsize = 'x-large')
    plt.show()

    plt.figure(figsize=(10,6))
    plt.plot(history.history['loss'], label = 'training', marker = '*', linewidth = lw)
    plt.plot(history.history['val_loss'], label = 'validation', marker = 'o', linewidth = lw)
    plt.title('Training Loss vs Validation Loss')
    plt.xlabel('Epochs')
    plt.ylabel('Loss')
    plt.legend(fontsize = 'x-large')
    plt.show()
visualize_training(history)<jupyter_output><empty_output><jupyter_text>Predictions<jupyter_code>label = valid_generator.classes
pred= model.predict(valid_generator)

predicted_class_indices=np.argmax(pred,axis=1)

labels = (valid_generator.class_indices)
labels2 = dict((v,k) for k,v in labels.items())
predictions = [labels2[k] for k in predicted_class_indices]<jupyter_output><empty_output><jupyter_text>Save Model<jupyter_code>model_name = 'malaria_predsmodel.h5'
model.save_weights(model_name)<jupyter_output><empty_output><jupyter_text>Random Image Prediction<jupyter_code>image_path = '../input/cell-images-for-detecting-malaria/cell_images/Parasitized/C100P61ThinF_IMG_20150918_144104_cell_162.png'
image = tf.keras.preprocessing.image.load_img(image_path)
input_arr = tf.keras.preprocessing.image.img_to_array(image)
input_arr = np.array([input_arr])  # Convert single image to a batch.
preds1 = model.predict_classes(input_arr)

preds1<jupyter_output><empty_output><jupyter_text>**So our model correctly predicts the Parasitized cell**<jupyter_code>image_path = '../input/cell-images-for-detecting-malaria/cell_images/Uninfected/C100P61ThinF_IMG_20150918_144104_cell_128.png'
image = tf.keras.preprocessing.image.load_img(image_path)
input_arr = tf.keras.preprocessing.image.img_to_array(image)
input_arr = np.array([input_arr])  # Convert single image to a batch.
pred_uninfected = model.predict_classes(input_arr)
pred_uninfected<jupyter_output><empty_output><jupyter_text>Confusion Matrix And Classification Report<jupyter_code>from sklearn.metrics import confusion_matrix, classification_report, roc_curve, mean_absolute_error

rc = roc_curve(predicted_class_indices,label)
cf_matrix = confusion_matrix(predicted_class_indices,label)
cf_report = classification_report(predicted_class_indices,label)
print('Confusion matrix report of the model : \n{}'.format(cf_matrix))
print('Classification report of the model : \n{}'.format(cf_report))
print('ROC metrics estimates : \n{}'.format(rc))
exp_series = pd.Series(label)
pred_series = pd.Series(predicted_class_indices)
pd.crosstab(exp_series, pred_series, rownames=['Actual'], colnames=['Predicted'],margins=True)<jupyter_output><empty_output><jupyter_text>MobileNet MAE<jupyter_code>mae = mean_absolute_error(predicted_class_indices,label)
print('Mean Absolute Error : {}'.format(mae))
import seaborn as sns

matrix_index = ["Parasitic", "uninfected"]

preds = model.predict(valid_generator)
classpreds = np.argmax(preds, axis=1) # predicted classes 
#y_testclass = np.argmax(valida, axis=1) # true classes

cm = confusion_matrix(predicted_class_indices,label)
print(classification_report(predicted_class_indices,label, target_names=matrix_index))

# Get percentage value for each element of the matrix
cm_sum = np.sum(cm, axis=1, keepdims=True)
cm_perc = cm / cm_sum.astype(float) * 100
annot = np.empty_like(cm).astype(str)
nrows, ncols = cm.shape
for i in range(nrows):
    for j in range(ncols):
        c = cm[i, j]
        p = cm_perc[i, j]
        if i == j:
            s = cm_sum[i]
            annot[i, j] = '%.1f%%\n%d/%d' % (p, c, s)
        elif c == 0:
            annot[i, j] = ''
        else:
            annot[i, j] = '%.1f%%\n%d' % (p, c)


# Display confusion matrix 
df_cm = pd.DataFrame(cm, index = matrix_index, columns = matrix_index)
df_cm.index.name = 'Actual'
df_cm.columns.name = 'Predicted'
fig, ax = plt.subplots(figsize=(10,7))
sns.heatmap(df_cm, annot=annot, fmt='')<jupyter_output>precision    recall  f1-score   support

   Parasitic       1.00      0.50      0.67      5510
  uninfected       0.00      0.00      0.00         0

    accuracy                           0.50      5510
   macro avg       0.50      0.25      0.33      5510
weighted avg       1.00      0.50      0.67      5510<jupyter_text>VGG-19![vgg_architecture](https://www.researchgate.net/profile/Clifford_Yang/publication/325137356/figure/fig2/AS:670371271413777@1536840374533/llustration-of-the-network-architecture-of-VGG-19-model-conv-means-convolution-FC-means.jpg)<jupyter_code>vgg_model = Sequential()
vgg_model.add(VGG19(include_top=False, pooling='avg', weights='imagenet', input_shape=(128, 128, 3), classes=2))
vgg_model.add(Flatten())
vgg_model.add(Dense(256,activation='relu'))
vgg_model.add(Dense(64,activation='relu'))
vgg_model.add(Dense(1,activation = 'sigmoid'))

vgg_model.layers[0].trainable = False
vgg_model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
vgg_model.summary()
vgg_history = vgg_model.fit_generator(train_generator,
                              steps_per_epoch = len(train_generator),
                              epochs=20,
                              validation_steps = len(valid_generator),
                              validation_data=valid_generator,
                              callbacks = [early_stop]
                                     )<jupyter_output>Epoch 1/20
345/345 [==============================] - 46s 134ms/step - loss: 0.3164 - accuracy: 0.8650 - val_loss: 0.2714 - val_accuracy: 0.8893
Epoch 2/20
345/345 [==============================] - 47s 136ms/step - loss: 0.2224 - accuracy: 0.9116 - val_loss: 0.2372 - val_accuracy: 0.9083
Epoch 3/20
345/345 [==============================] - 47s 135ms/step - loss: 0.2022 - accuracy: 0.9231 - val_loss: 0.3190 - val_accuracy: 0.8613
Epoch 4/20
345/345 [==============================] - 46s 134ms/step - loss: 0.2008 - accuracy: 0.9224 - val_loss: 0.2207 - val_accuracy: 0.9116
Epoch 5/20
345/345 [==============================] - 46s 132ms/step - loss: 0.1932 - accuracy: 0.9247 - val_loss: 0.2297 - val_accuracy: 0.9096
Epoch 6/20
345/345 [==============================] - 47s 135ms/step - loss: 0.1866 - accuracy: 0.9266 - val_loss: 0.2615 - val_accuracy: 0.8989<jupyter_text>Save VGG model<jupyter_code>vgg_model_name = 'vgg_malaria_predsmodel.h5'
model.save_weights(vgg_model_name)
def visualize_training(vgg_history, lw = 3):
    plt.figure(figsize=(10,6))
    plt.plot(vgg_history.history['accuracy'], label = 'training', marker = '*', linewidth = lw)
    plt.plot(vgg_history.history['val_accuracy'], label = 'validation', marker = 'o', linewidth = lw)
    plt.title('Training Accuracy vs Validation Accuracy')
    plt.xlabel('Epochs')
    plt.ylabel('Accuracy')
    plt.legend(fontsize = 'x-large')
    plt.show()

    plt.figure(figsize=(10,6))
    plt.plot(vgg_history.history['loss'], label = 'training', marker = '*', linewidth = lw)
    plt.plot(vgg_history.history['val_loss'], label = 'validation', marker = 'o', linewidth = lw)
    plt.title('Training Loss vs Validation Loss')
    plt.xlabel('Epochs')
    plt.ylabel('Loss')
    plt.legend(fontsize = 'x-large')
    plt.show()
visualize_training(vgg_history)<jupyter_output><empty_output><jupyter_text>VGG-19 predictions<jupyter_code>label = valid_generator.classes
pred= vgg_model.predict(valid_generator)

predicted_class_indices=np.argmax(pred,axis=1)

labels = (valid_generator.class_indices)
labels2 = dict((v,k) for k,v in labels.items())
vgg_predictions = [labels2[k] for k in predicted_class_indices]
image_path = '../input/cell-images-for-detecting-malaria/cell_images/Parasitized/C100P61ThinF_IMG_20150918_144104_cell_162.png'
image = tf.keras.preprocessing.image.load_img(image_path)
input_arr = tf.keras.preprocessing.image.img_to_array(image)
input_arr = np.array([input_arr])  # Convert single image to a batch.
preds1 = vgg_model.predict_classes(input_arr)

preds1
rc = roc_curve(predicted_class_indices,label)
cf_matrix = confusion_matrix(predicted_class_indices,label)
cf_report = classification_report(predicted_class_indices,label)
print('Confusion matrix report of the model : \n{}'.format(cf_matrix))
print('Classification report of the VGG19 model : \n{}'.format(cf_report))<jupyter_output>Classification report of the VGG19 model : 
              precision    recall  f1-score   support

           0       1.00      0.50      0.67      5510
           1       0.00      0.00      0.00         0

    accuracy                           0.50      5510
   macro avg       0.50      0.25      0.33      5510
weighted avg       1.00      0.50      0.67      5510<jupyter_text>Visualizing the model Classification Report<jupyter_code>from sklearn.metrics import roc_auc_score, classification_report, confusion_matrix
import seaborn as sns

matrix_index = ["Parasitic", "uninfected"]

preds = vgg_model.predict(valid_generator)
classpreds = np.argmax(preds, axis=1) # predicted classes 
#y_testclass = np.argmax(valida, axis=1) # true classes

cm = confusion_matrix(predicted_class_indices,label)
print(classification_report(predicted_class_indices,label, target_names=matrix_index))

# Get percentage value for each element of the matrix
cm_sum = np.sum(cm, axis=1, keepdims=True)
cm_perc = cm / cm_sum.astype(float) * 100
annot = np.empty_like(cm).astype(str)
nrows, ncols = cm.shape
for i in range(nrows):
    for j in range(ncols):
        c = cm[i, j]
        p = cm_perc[i, j]
        if i == j:
            s = cm_sum[i]
            annot[i, j] = '%.1f%%\n%d/%d' % (p, c, s)
        elif c == 0:
            annot[i, j] = ''
        else:
            annot[i, j] = '%.1f%%\n%d' % (p, c)


# Display confusion matrix 
df_cm = pd.DataFrame(cm, index = matrix_index, columns = matrix_index)
df_cm.index.name = 'Actual'
df_cm.columns.name = 'Predicted'
fig, ax = plt.subplots(figsize=(10,7))
sns.heatmap(df_cm, annot=annot, fmt='')<jupyter_output>precision    recall  f1-score   support

   Parasitic       1.00      0.50      0.67      5510
  uninfected       0.00      0.00      0.00         0

    accuracy                           0.50      5510
   macro avg       0.50      0.25      0.33      5510
weighted avg       1.00      0.50      0.67      5510<jupyter_text>VGG-19 MAE<jupyter_code>mae = mean_absolute_error(predicted_class_indices,label)
print('Mean Absolute Error : {}'.format(mae))<jupyter_output>Mean Absolute Error : 0.5<jupyter_text>InceptionV3![architecture](https://miro.medium.com/max/960/1*gqKM5V-uo2sMFFPDS84yJw.png)<jupyter_code>inception_model = Sequential()
inception_model.add(tf.keras.applications.InceptionV3(include_top=False, pooling='avg', weights='imagenet', input_shape=(128, 128, 3), classes=2))
inception_model.add(Flatten())
inception_model.add(Dense(64,activation='relu'))
inception_model.add(Dense(1,activation = 'sigmoid'))

inception_model.layers[0].trainable = False
inception_model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
inception_model.summary()
inception_history = inception_model.fit_generator(train_generator,
                              steps_per_epoch = len(train_generator),
                              epochs=20,
                              validation_steps = len(valid_generator),
                              validation_data=valid_generator,
                              callbacks = [early_stop]
                                     )<jupyter_output>Epoch 1/20
345/345 [==============================] - 46s 134ms/step - loss: 0.2710 - accuracy: 0.8944 - val_loss: 0.2336 - val_accuracy: 0.9049
Epoch 2/20
345/345 [==============================] - 44s 127ms/step - loss: 0.1995 - accuracy: 0.9233 - val_loss: 0.2236 - val_accuracy: 0.9102
Epoch 3/20
345/345 [==============================] - 44s 129ms/step - loss: 0.1850 - accuracy: 0.9290 - val_loss: 0.2241 - val_accuracy: 0.9103
Epoch 4/20
345/345 [==============================] - 45s 129ms/step - loss: 0.1744 - accuracy: 0.9341 - val_loss: 0.2121 - val_accuracy: 0.9149
Epoch 5/20
345/345 [==============================] - 44s 128ms/step - loss: 0.1672 - accuracy: 0.9357 - val_loss: 0.2234 - val_accuracy: 0.9120
Epoch 6/20
345/345 [==============================] - 45s 130ms/step - loss: 0.1618 - accuracy: 0.9367 - val_loss: 0.2163 - val_accuracy: 0.9147<jupyter_text>Save InceptionV3<jupyter_code>inception_model_name = 'inceptionv3_malaria_predsmodel.h5'
model.save_weights(inception_model_name)<jupyter_output><empty_output><jupyter_text>InceptionV3 metrics plot<jupyter_code>def visualize_training(inception_history, lw = 3):
    plt.figure(figsize=(10,6))
    plt.plot(inception_history.history['accuracy'], label = 'training', marker = '*', linewidth = lw)
    plt.plot(inception_history.history['val_accuracy'], label = 'validation', marker = 'o', linewidth = lw)
    plt.title('Training Accuracy vs Validation Accuracy')
    plt.xlabel('Epochs')
    plt.ylabel('Accuracy')
    plt.legend(fontsize = 'x-large')
    plt.show()

    plt.figure(figsize=(10,6))
    plt.plot(inception_history.history['loss'], label = 'training', marker = '*', linewidth = lw)
    plt.plot(inception_history.history['val_loss'], label = 'validation', marker = 'o', linewidth = lw)
    plt.title('Training Loss vs Validation Loss')
    plt.xlabel('Epochs')
    plt.ylabel('Loss')
    plt.legend(fontsize = 'x-large')
    plt.show()
visualize_training(inception_history)<jupyter_output><empty_output><jupyter_text>InceptionV3 predictions<jupyter_code>label = valid_generator.classes
pred= inception_model.predict(valid_generator)

predicted_class_indices=np.argmax(pred,axis=1)

labels = (valid_generator.class_indices)
labels2 = dict((v,k) for k,v in labels.items())
vgg_predictions = [labels2[k] for k in predicted_class_indices]
image_path = '../input/cell-images-for-detecting-malaria/cell_images/Parasitized/C100P61ThinF_IMG_20150918_144104_cell_162.png'
image = tf.keras.preprocessing.image.load_img(image_path)
input_arr = tf.keras.preprocessing.image.img_to_array(image)
input_arr = np.array([input_arr])  # Convert single image to a batch.
preds2 = inception_model.predict_classes(input_arr)
preds2
rc = roc_curve(predicted_class_indices,label)
cf_matrix = confusion_matrix(predicted_class_indices,label)
cf_report = classification_report(predicted_class_indices,label)
print('Confusion matrix report of the model : \n{}'.format(cf_matrix))
print('Classification report of the VGG19 model : \n{}'.format(cf_report))<jupyter_output>Classification report of the VGG19 model : 
              precision    recall  f1-score   support

           0       1.00      0.50      0.67      5510
           1       0.00      0.00      0.00         0

    accuracy                           0.50      5510
   macro avg       0.50      0.25      0.33      5510
weighted avg       1.00      0.50      0.67      5510<jupyter_text>Inception Model Classification Report<jupyter_code>from sklearn.metrics import roc_auc_score, classification_report, confusion_matrix, mean_absolute_error
import seaborn as sns

matrix_index = ["Parasitic", "uninfected"]

preds = inception_model.predict(valid_generator)
classpreds = np.argmax(preds, axis=1) # predicted classes 
#y_testclass = np.argmax(valida, axis=1) # true classes

cm = confusion_matrix(predicted_class_indices,label)
print(classification_report(predicted_class_indices,label, target_names=matrix_index))

# Get percentage value for each element of the matrix
cm_sum = np.sum(cm, axis=1, keepdims=True)
cm_perc = cm / cm_sum.astype(float) * 100
annot = np.empty_like(cm).astype(str)
nrows, ncols = cm.shape
for i in range(nrows):
    for j in range(ncols):
        c = cm[i, j]
        p = cm_perc[i, j]
        if i == j:
            s = cm_sum[i]
            annot[i, j] = '%.1f%%\n%d/%d' % (p, c, s)
        elif c == 0:
            annot[i, j] = ''
        else:
            annot[i, j] = '%.1f%%\n%d' % (p, c)


# Display confusion matrix 
df_cm = pd.DataFrame(cm, index = matrix_index, columns = matrix_index)
df_cm.index.name = 'Actual'
df_cm.columns.name = 'Predicted'
fig, ax = plt.subplots(figsize=(10,7))
sns.heatmap(df_cm, annot=annot, fmt='')<jupyter_output>precision    recall  f1-score   support

   Parasitic       1.00      0.50      0.67      5510
  uninfected       0.00      0.00      0.00         0

    accuracy                           0.50      5510
   macro avg       0.50      0.25      0.33      5510
weighted avg       1.00      0.50      0.67      5510<jupyter_text>InceptionNet MAE<jupyter_code>mae = mean_absolute_error(predicted_class_indices,label)
print('Mean Absolute Error : {}'.format(mae))<jupyter_output>Mean Absolute Error : 0.5
{'fraction_non_alphanumeric': 0.13969716919025674, 'fraction_numerical': 0.057493965328066714, 'mean_word_length': 3.422748447204969, 'pattern_counts': {'":': 0, '<': 85, '<?xml version=': 0, '>': 85, 'https://': 5, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 41, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '24', 'dir': 'jupyter-structured-clean-dedup', 'id': '459044', 'license': 'Apache-2.0', 'n_tokens_mistral': 8490, 'n_tokens_neox': 7528, 'n_words': 1479, 'path': 'simple-deep-learning-on-malaria.ipynb', 'repo_name': 'digs1998/Malaria-with-Deep-Learning', 'size': '22785'}