Friday, May 7, 2021

Classification of images from Laurence Moroney's Dataset Happy or Sad faces

#observations
#skipping class_mode = "binary" in flow_from_directory results in 
#wrong outcomes 

#tried following things to avoid overfitting
#1. Added Dropout
#2. added parameters to optimizer lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.0, amsgrad=False
#3. added horizontal flip
#4. in an hope to increase validation accuracy,increased number of dense layers
#5. increased convolution layers

#each of the above attempt resulted in slight improvement but still 
#a lot of overfitting exists, not able to contain it. 

#however, if we ignore the plot of loss functions which indicate overfitting, 
#the outcome is fairly OK , with out of 40 images evaluated, only one 
#image, which is really confusing to humans also, always getting wrong


import tensorflow as tf 
import tensorflow.keras 
import os 
import matplotlib.pyplot as plt 
import numpy as np 

hsdir = "/content/h-or-s"
hszipfile = "/content/h-or-s/hs.zip"
url = "https://storage.googleapis.com/laurencemoroney-blog.appspot.com/happy-or-sad.zip"

if ( os.path.exists(hsdir) == False) : 
  os.mkdir(hsdir)

if ( os.path.exists(hszipfile) == False) : 
  tf.keras.utils.get_file(hszipfile , origin = url , extract = True)
  import zipfile 
  hsfileref = zipfile.ZipFile(hszipfile , "r")
  hsfileref.extractall(hsdir)
  hsfileref.close()

hs_datagen = tf.keras.preprocessing.image.ImageDataGenerator(
    rescale = 1./255 , 
    validation_split = 0.2,
    horizontal_flip = True
    )

train_gen = hs_datagen.flow_from_directory(
    hsdir, 
    batch_size = 1
    target_size = (150150), 
    subset = "training"
    class_mode = "binary" #most important
)


test_gen = hs_datagen.flow_from_directory(
    hsdir, 
    batch_size = 1
    target_size = (150150), 
    subset = "validation"
    class_mode = "binary" #most important
)


model = tf.keras.models.Sequential([
  tf.keras.layers.Conv2D(32 , 3, activation = "relu" , input_shape = (1501503)), 
  tf.keras.layers.MaxPooling2D(),
  tf.keras.layers.Conv2D(64 , 3, activation = "relu"), 
  tf.keras.layers.MaxPooling2D(),
  tf.keras.layers.Conv2D(128 , 3, activation = "relu"), 
  tf.keras.layers.MaxPooling2D(),    
  tf.keras.layers.Flatten(), 
  tf.keras.layers.Dropout(0.2),
  tf.keras.layers.Dense(1024, activation = "relu"),
  tf.keras.layers.Dense(1, activation = "sigmoid")
])

adam = tf.keras.optimizers.Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.0, amsgrad=False)


model.compile(optimizer = adam , 
              loss = tf.keras.losses.BinaryCrossentropy(), 
              metrics  = ["accuracy"]
              )

epochs  = 2
history = model.fit ( train_gen , epochs = epochs , validation_data = test_gen)


plt.figure(figsize = (8,8))

plt.subplot(1,2,1)
plt.plot(range(epochs), history.history["accuracy"] , "r" , label  =  "Training Accuracy")
plt.plot(range(epochs), history.history["val_accuracy"] , "b" , label  = "Validation Accuracy")
plt.legend(loc="upper left")
plt.title("Accuracy")


plt.subplot(1,2,2)
plt.plot(range(epochs), history.history["loss"] , "r" , label = "Training Loss")
plt.plot(range(epochs), history.history["val_loss"] , "b" , label =  "Validation Loss")
plt.legend(loc="upper right")
plt.title("Loss")

plt.show()


#prediction for 40 images, ignore the repeatation for now
plt.figure(figsize=(20,20))
happyfiles = os.listdir("/content/h-or-s/happy")
sadfiles = os.listdir("/content/h-or-s/sad")
nrows = 20 
for i in range(nrows) : 
  plt.subplot(nrows,2,(i*2)+1)
  hfile = hsdir + "/happy/" + happyfiles[i]
  img = tf.keras.preprocessing.image.load_img(hfile, target_size = ( 150150))
  img_arr = tf.keras.preprocessing.image.img_to_array(img)
  img_arr = tf.expand_dims(img_arr, 0 )
  predictions = model.predict(img_arr)
  plt.xticks([])
  plt.yticks([])
  plt.xlabel(happyfiles[i])
  plt.ylabel(predictions)
  plt.imshow(img)


  sfile = hsdir + "/sad/" + sadfiles[i]
  img = tf.keras.preprocessing.image.load_img(sfile, target_size = ( 150150))
  img_arr = tf.keras.preprocessing.image.img_to_array(img)
  img_arr = tf.expand_dims(img_arr, 0 )
  predictions = model.predict(img_arr)
  plt.subplot(nrows,2,(i+1)*2)
  plt.xticks([])
  plt.yticks([])
  plt.xlabel(sadfiles[i])
  plt.ylabel(predictions)
  plt.imshow(img)
plt.show()




#prediction for a single image
img = tf.keras.preprocessing.image.load_img("/content/h-or-s/happy/happy1-01.png", target_size = ( 150150))
img_arr = tf.keras.preprocessing.image.img_to_array(img)
img_arr = tf.expand_dims(img_arr, 0 )
predictions = model.predict(img_arr)
plt.imshow(img)
print(predictions)

No comments:

Post a Comment

 using Microsoft.AspNetCore.Mvc; using System.Xml.Linq; using System.Xml.XPath; //<table class="common-table medium js-table js-stre...