Wednesday, May 12, 2021

 InvalidArgumentError: logits and labels must have the same first dimension, got logits shape [32,5] and labels shape [160]



This happens when the class_mode of ImageDataGenerator is not in sync with num of classes in the dataset.

Suppose you are working on 5 to 10 classes, you should ideally specify class_mode = "sparse"

If you don't, the above error is flashed.


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

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

ValueError: Input 0 of layer random_flip_6 is incompatible with the layer: expected ndim=4, found ndim=3. Full shape received: (None, 180, 180)



62 tf.keras.layers.experimental.preprocessing.RandomFlip("horizontal" , input_shape = (180,180)), 63 tf.keras.layers.experimental.preprocessing.RandomRotation(0.2), ---> 64 tf.keras.layers.experimental.preprocessing.RandomZoom(0.2) 65 ])


Corrected working code:


*-> 62 tf.keras.layers.experimental.preprocessing.RandomFlip("horizontal" , input_shape = (180,180,3)),😀 63 tf.keras.layers.experimental.preprocessing.RandomRotation(0.2), ---> 64 tf.keras.layers.experimental.preprocessing.RandomZoom(0.2) 65 ])

 Flowers Dataset - using image_dataset_from_directory

#flowers dataset with some data augmentation


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

# 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


train_ds  = tf.keras.preprocessing.image_dataset_from_directory(
    "/content/flowers/flower_photos",
    image_size = ( 180, 180),
    batch_size = batch_size, 
    subset = "training",
    seed = 123,
    validation_split = 0.2
)


test_ds  = tf.keras.preprocessing.image_dataset_from_directory(
    "/content/flowers/flower_photos",
    image_size = ( 180, 180),
    batch_size = batch_size, 
    subset = "validation",
    seed = 123,
    validation_split = 0.2
)


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
)

#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


#NOTE : Images CANNOT be read like img = test_ds.take(1)[0][0]
#you must use a loop with test_ds.take(x) for that

#prediction on images using the dataset
correct = 0 
incorrect = 0 
i = 0 
images= test_ds.take(1)
plt.figure(figsize=(10,10))
for images, labels in test_ds.take(1):
  print(images.shape)
  for i in range(25):
    ax = plt.subplot(5, 5, i + 1)

    plt.imshow(images[i].numpy().astype("uint8"))
    plt.xticks([])
    plt.yticks([])
    img_arr = tf.keras.preprocessing.image.img_to_array(images[i].numpy().astype("uint8"))
    img_arr = tf.expand_dims(img_arr,0)
    predictions = model.predict(img_arr)
    
    plt.xlabel("P={};A={}".format(np.argmax(predictions[0]) ,labels[i]))
    if (np.argmax(predictions[0]) == labels[i] ) : 
      correct += 1 
    else : 
      incorrect += 1 
    #plt.axis("off")
print ( "correct = {} ; incorrect = {}".format(correct, incorrect))    
#prediction on images using the dataset



#prediction on images by directly reading images from storage
correct = 0 
incorrect = 0 
i = 0 
tulips = os.listdir("/content/flowers/flower_photos/tulips")
tulips_label = 4  #this you wil have to infer from somewhere because labels are not available to you 
                  #when you are directly reading from disk. Mostly the labels are in the order of 
                  #subdirectories, starting with 0. But to confirm, you can plot a few figures using dataset
                  #and infer the label 
plt.figure(figsize=(10,10))
for i in range(25) : 
  img_path = "/content/flowers/flower_photos/tulips/" + tulips[i]

  img = tf.keras.preprocessing.image.load_img(img_path, target_size=(180,180))
  plt.subplot(5,5,i + 1)
  plt.xticks([])
  plt.yticks([])
  plt.imshow(img)
  img_arr = tf.keras.preprocessing.image.img_to_array(img)
  img_arr = tf.expand_dims(img_arr, 0)
  predictions = model.predict(img_arr)
  plt.xlabel("P={};A={}".format(np.argmax(predictions[0]) ,tulips_label))
  if (np.argmax(predictions[0]) == tulips_label) : 
    correct += 1 
  else : 
    incorrect += 1   

print ( "correct = {} ; incorrect = {}".format(correct, incorrect))      
#prediction on images by directly reading images from storage


 https://stanford.edu/~shervine/


Shervine and Afshine Amidi blog page at stanford


Contains various useful cheat sheets


Tuesday, May 11, 2021

 ValueError: Asked to retrieve element 0, but the Sequence has length 0


This error  is thrown by ImageDatasetGenerator.flow_from_directory if the given directory is empty, it does not contain any images.

Interpolation methods of image resizing


When images are resized to a larger size from originally smaller size, some new data needs to be generated. Interpolation is used to generate new data. 


There are two types of interpolation methods : adaptive and non-adaptive. All the adaptive methods are licensed. 


Non-adaptive methods are supported by ImageDatasetGenerator. We can set its interpolation method by using the parameter "interpolation". 


The default value of interpolation parameter is bilinear. 

The other supported values are : nearest, bicubic, area, lanczos3, lanczos5, gaussian, mitchellcubic


You can get a good information about interpolation here

Comparing features of LangGraph vs CrewAI

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