Wednesday, May 12, 2021

 Flower Dataset - Using ImageDataGenerator class 

#flowers dataset with some data augmentation



#not so good model with accuracy (both training and validation ) in the range of 

#appox 0.6


# rm -r "/content/flowers"


import tensorflow as tf 

import tensorflow.keras

import os 

import pathlib 

import numpy as np 

import matplotlib.pyplot as plt 



if ( os.path.exists("/content/flowers") == False) : 

  os.mkdir("/content/flowers")

url  = 


"https://storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz"

flfile = tf.keras.utils.get_file("/content/flowers/flower_photos.tgz" , origin = url, 


extract  = True)

#NOTE: The tensorflow tutorial currently available at 


https://www.tensorflow.org/tutorials/images/classification

#(as in 11-May-2021) uses the last parameter as untar = True instead of extract = True 


above. 

#but untar is deprecated and the documentation recommends to use extract instead of untar.

#further using untat=True causes errorneous behavior also , firstly it doesnt extract the 


file, secondly

#it saves the ".tgz" file by an extension ".tar.gz" to the file name 

# thus making it "flower_photos.tgz.tar.gz".

print(flfile)

flfile = pathlib.Path(flfile)

print(flfile)


#the following is not required if extract=True is used in get_file

import tarfile 

tfile = tarfile.open(flfile)

tfile.extractall("/content/flowers")

tfile.close()


batch_size = 32 

epochs = 5



flowers_gen = tf.keras.preprocessing.image.ImageDataGenerator(

    rescale = 1./255, 

    validation_split = 0.2

)


train_ds_gen = flowers_gen.flow_from_directory(

    "/content/flowers/flower_photos", 

    subset = "training", 

    seed = 123,

    target_size = (180, 180), 

    batch_size = 32, 

    class_mode = "sparse" #this is essential

)


test_ds_gen = flowers_gen.flow_from_directory(

    "/content/flowers/flower_photos", 

    subset = "validation", 

    seed = 123,

    target_size = (180, 180), 

    batch_size = 32, 

    class_mode = "sparse" #this is essential

)


data_aug = tf.keras.models.Sequential([

  tf.keras.layers.experimental.preprocessing.RandomFlip("horizontal" , input_shape = 


(180,180,3)),

  tf.keras.layers.experimental.preprocessing.RandomRotation(0.2),

  tf.keras.layers.experimental.preprocessing.RandomZoom(0.2)

])



model = tf.keras.models.Sequential([

    data_aug, 

    tf.keras.layers.Conv2D(16, 3, activation = "relu"),

    #tf.keras.layers.Conv2D(16, 3, activation = "relu" , input_shape = (180,180,3)), 

    tf.keras.layers.MaxPooling2D(),

    tf.keras.layers.Conv2D(32, 3, activation = "relu"), 

    tf.keras.layers.MaxPooling2D(),

    tf.keras.layers.Conv2D(64, 3, activation = "relu"), 

    tf.keras.layers.MaxPooling2D(),        

    tf.keras.layers.Dropout(0.2),

    tf.keras.layers.Flatten(), 

    tf.keras.layers.Dense(512, activation = "relu"),

    tf.keras.layers.Dense(5, activation = "softmax")

])




model.compile(

    optimizer = tf.keras.optimizers.Adam(),

    loss = tf.keras.losses.SparseCategoricalCrossentropy(from_logits = True), 

    metrics = ["accuracy"]

)


# history = model.fit(

#     train_ds, 

#     validation_data = test_ds, epochs = epochs

# )



history = model.fit(

    train_ds_gen, 

    validation_data = test_ds_gen, epochs = epochs

)

#Plot the loss and accuracy

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()

#Plot the loss and accuracy


correct = 0 

incorrect = 0 

maxbatchcount = 2

batchcounter = 0 

for batch in test_ds_gen : 

  #NOTE That batch is a tuple of image and label i.e. ( images, labels)

  # so batch[x][0] => images, which is array of 32 images

  # batch[x][1] => labels, which is array of 32 labels (floats)

  #len(batch) = 2 => contains two array, images and lables

  #batch[x][0].shape = (32,180,180,3) , batch[x][1].shape = (32,)

  # WE WILL WORKING ON 2 batches  maxbatchcount = 2, 10 images per batch

  

  maximagecount = 10 

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

  for imagecounter in range(maximagecount) : 

    plt.subplot(5,2, imagecounter + 1)

    plt.imshow(batch[0][imagecounter])

    img_arr = tf.keras.preprocessing.image.img_to_array(batch[0][imagecounter])

    img_arr = tf.expand_dims(img_arr, 0 )

    predictions = model.predict(img_arr)

    plt.xlabel("P={};A={}".format(np.argmax(predictions[0]),batch[1][imagecounter]))

    plt.xticks([])

    plt.yticks([])

    if ( np.argmax(predictions[0]) == batch[1][imagecounter] ) : 

      correct += 1

    else : 

      incorrect += 1 

  

  batchcounter += 1 

  if batchcounter == maxbatchcount : 

    break


print ( "Correct = {} ;  Incorrect = {} ".format(correct , incorrect))      

#this is another approach, commonly found in tutorials

#it uses (images, labels) tuple

correct = 0 

incorrect = 0 

batchcounter = 0 

for (images, labels) in test_ds_gen : 

  print(len(batch))

  print(images.shape)

  print(labels.shape)

  

  maximagecount = 10 

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

  for imagecounter in range(maximagecount) : 

    plt.subplot(5,2, imagecounter + 1)

    plt.imshow(images[imagecounter])

    img_arr = tf.keras.preprocessing.image.img_to_array(images[imagecounter])

    img_arr = tf.expand_dims(img_arr, 0 )

    predictions = model.predict(img_arr)

    plt.xlabel("P={};A={}".format(np.argmax(predictions[0]),labels[imagecounter]))    

    plt.xticks([])

    plt.yticks([])

    if ( np.argmax(predictions[0]) == labels[imagecounter] ) : 

      correct += 1

    else :

      incorrect += 1 


  batchcounter += 1 

  if batchcounter == maxbatchcount : 

    break


print ( "Correct = {} ;  Incorrect = {} ".format(correct , incorrect))          

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...