Tuesday, May 11, 2021

 Silly Typo😁



TypeError: __init__() takes from 1 to 3 positional arguments but 10 were given



The "TypeError" above is really a "TYPE" error (means TYPING , not data type😆😆)


I actually forgot to put a square brackets for array inside model.Sequential.


model = tf.keras.models.Sequential(
    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.Flatten(), 
    tf.keras.layers.Dense(512, activation = "relu"),
    tf.keras.layers.Dense(5, activation = "softmax")
)


They all look like parameters rather than array of layers, isn't it ?

Below is the correct code:
model = tf.keras.models.Sequential([
    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.Flatten(), 
    tf.keras.layers.Dense(512, activation = "relu"),
    tf.keras.layers.Dense(5, activation = "softmax")
])

The compiler does some pun when it says Type Error, I think😉

 What is the difference between image_dataset_from_directory and ImageDataSet.flow_from_directory ?


keras provides two different methods to generate Image datasets : 

1. tf.keras.preprocessing.image_dataset_from_directory

2. tf.keras.preprocessing.image.ImageDataGenerator.flow_from_directory


The main difference between these two methods is that image_dataset_from_directory (first method above) does not have any provisions for data augmentation. Where as ImageDataGenerator.flow_from_directory has the provision for on-the-fly data augmentation. 

In fact ImageDataGenerator is the class provided by keras library that has the capability of on the fly data augmentation and it can augment existing keras datasets also.  It provides three methods, 

  • flow
  • flow_from_dataframe
  • flow_from_directory
The flow method geneate batches of real time augmented data from (data, label) arrays, which can be existing built-in datasets provided by keras like fashion_mnist/cifar10. 

In contrast, image_dataset_from_directory works only on directories and lacks the functionality of real time data augmentation. 

Apart from this logical difference, we can notice following minor syntactical differences also: 

1. The first method is contained in the module tf.keras.preprocessing, where as the second method ( and the ImageDataGenerator class itself) is contained in tf.keras.preprocessing.image module. 

2. Another very minor difference is to specify image size, we have to use image_size parameter in image_dataset_from_directory. Where as in ImageDatasetGenerator.flow_from_directory, we have to use the parameter target_size. 


3. Validation_split: image_dataset_from_directory has a parameter "validation_split". But in case of ImageDatasetGenerator, it is a property of the class itself. You have to specify this parameter while creating the class instance and then pass the subset parameter in flow_from_directory. 

If you specify validation_split as a parameter to flow_from_directory instead, you will get following error: 

TypeError: flow_from_directory() got an unexpected keyword argument 'validation_split'

Monday, May 10, 2021

 Tensorflow Google Tutorial on Image Classification (Flowers) : A note on use of untar = True in get_file method


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


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 erroneous behavior also , firstly it doesn't extract the file, 
secondly it saves the ".tgz" file by adding an extension ".tar.gz" to the file name 
 thus making it "flower_photos.tgz.tar.gz".

Clustering algorithms 

Clustering algos are the algos for unsupervised learning. 

Data is provided to them without labels and the algo clusters them into groups. 


There are 3 types of clustering algorithms : 

Desnsity Based : DBSCAN , OPTICS

Distribution Based : 

Centroid Based : K-means, Gaussian Mixture Model, BIRCH

BIRCH-> Balance Iterative Reducing and Clustering using Hierarchies

OPTICS->Ordering Points to Identify Clustering Structure 

Saturday, May 8, 2021

Revisiting the good old fashion_mnist 


#The CNN model does well, does not overfit, and predicts fairly accurately
#with may be just one/two wrong predictions.


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


fashion_mnist = tf.keras.datasets.fashion_mnist
(train_images , train_labels), (test_images, test_labels) = fashion_mnist.load_data()


class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat',
               'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']

print(train_images.shape)
print(train_labels.shape)
print(test_images.shape)
print(test_labels.shape)

#image printing below and shapes printing above is not strictly required, it 
#is merely done here to be in sync with the tensorflow google tutorials!

#print a single image
plt.figure()
plt.imshow(train_images[0])
plt.xticks([])
plt.yticks([])
plt.xlabel(class_names[train_labels[0]])
plt.show()



i = 0 
plt.figure(figsize=(6,6))
for i in range(25): 
  plt.subplot(5,5,i+1)
  plt.xticks([])
  plt.yticks([])
  plt.grid(False)
  plt.imshow(train_images[i])
  plt.xlabel(class_names[train_labels[i]])
plt.show()

train_images = train_images / 255.0
test_images = test_images /255.0

train_images = train_images.reshape(train_images.shape[0],28, 28,1)
test_images = test_images.reshape(test_images.shape[0],28, 28,1)


model = tf.keras.models.Sequential([
  tf.keras.layers.Conv2D(16 , 3, activation = "relu" , input_shape =(28,28,1)), 
  tf.keras.layers.MaxPooling2D(), 
  tf.keras.layers.Conv2D(16 , 3, activation = "relu"), 
  tf.keras.layers.MaxPooling2D(), 
  tf.keras.layers.Conv2D(16 , 3, activation = "relu"), 
  tf.keras.layers.MaxPooling2D(),     
  tf.keras.layers.Flatten(),
  tf.keras.layers.Dense(512, activation = "relu"),
  tf.keras.layers.Dense(10, activation= "softmax")
])


model.compile(optimizer = "adam" , loss = tf.keras.losses.SparseCategoricalCrossentropy(from_logits = True), metrics = ["accuracy"])
epochs=10
history = model.fit(train_images , train_labels , epochs = epochs , validation_data = (test_images, test_labels))


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

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

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

plt.show()


#predict for a single image
img_index = 10
img = tf.expand_dims(test_images[img_index],0) 
predictions = model.predict(img)
print(predictions[0])



print(class_names[np.argmax(predictions[0])])
print(class_names[test_labels[img_index]])
plt.imshow(test_images[img_index].reshape(28,28))


#predict for multiple images
image_index = 0 
for image_index in range(1 , 25 , 1) :
  img_arr = tf.expand_dims(train_images[image_index],0)
  predictions = model.predict(img_arr)
  print ("Predicted class {} ; actual class {} ".format(class_names[np.argmax(predictions[0])], class_names[train_labels[image_index]]))

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 = (150, 150), 
    subset = "training", 
    class_mode = "binary" #most important
)


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


model = tf.keras.models.Sequential([
  tf.keras.layers.Conv2D(32 , 3, activation = "relu" , input_shape = (150, 150, 3)), 
  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 = ( 150, 150))
  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 = ( 150, 150))
  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 = ( 150, 150))
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)
Dataset from ImageDataGenerator 
I have created a Image Data Generator. Now can I convert it to a keras dataset ? 

Yes. You can use tf.data.Dataset.from_generator:


 tf.data.Dataset.from_generator(lambda: ImageDataGenerator().flow_from_directory('folder_path'),(tf.float32, tf.float32))

Comparing features of LangGraph vs CrewAI

LangGraph Orchestration vs CrewAI Orchestration The following table compares LangGraph and CrewAI orchestration across their menta...