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

Friday, January 29, 2021

Activation Functions in Keras


relu
sigmoidmainly used for binary classification
softmax
softplus
tanh
selu
elu
expoenential

Saturday, January 23, 2021



Steps in Tensorflow ML Program
1Create ModelCreate a Sequential model using Dense Layers or Convolutional Layers
2Compile ModelCompile the model by specifying Optimizer and Loss function
3Load the Dataset
4Train the modelCall model.fit by giving the training set.
5EvaluateKnow the accuracy and loss
6Predict
7Display Output

 

Sunday, February 2, 2014

Requirements for using EF in a layered project

A layered project will typically have EF model in different project
and UI and other logic in different projects. There are some wierd things
required to do this.



Steps to be followed when accessing EF model in a different project.

1. Add a reference to "System.Data.Entity" in the refering project.
2. Add the following line to web.config of refering application
 <add assembly="System.Data.Entity, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
This is required in addition to step 1 above.
3. Add the same connection string in refering project as that of the
refered project.
4. In both the connections, change the file path to actual full path.



=============================================================
A TUTORIAL FOR THE ABOVE
=============================================================

1. Create a new mvc app, say MvcApplication1.

2. Create a new class lib project, say ClassLibrary1.

3. Add a new "Service Based Database"  to class library project, say Database1.mdf.

4. Add a new table to the .mdf file, say "Table1". Create some fields and add a couple of rows.

5. Add new ADO.NET Entity Data Model, say "Model1.edmx" and generate it from database.
   By default, it will create "Database1Entities" and corresponding connection string.
   For now, do not touch the connection string.
   Accept the defaults and go ahead.
6. In the default class, now add a method to return all records from table1 :

namespace ClassLibrary1
{
    public class Class1
    {
        public List<Table1> GetTable1()
        {
            return (new Database1Entities()).Table1.ToList();
        }
    }
}

7. Build ClassLibrary1.

8. In MVCApplication1, access Class1.GetTable, may be in homecontroller.
namespace MvcApplication1.Controllers
{
    [HandleError]
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            ViewData["Message"] = "!";
            ClassLibrary1.Class1 cls = new ClassLibrary1.Class1();
            return View(cls.GetTable1());
        }

        public ActionResult About()
        {
            return View();
        }
    }
}

9. Now build solution and run.

10. The first error you will encounter will be :

The type 'System.Data.Objects.DataClasses.EntityObject' is defined in an assembly
that is not referenced. You must add a reference to assembly
'System.Data.Entity, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.

To solve this error, right click MvcApplication1, and "Add Reference" to
"System.Data.Entity"

Build and run the project.

11. When you run the project, you will encounter next error:
"The specified named connection is either not found in the
configuration, not intended to be used with the EntityClient
provider, or not valid."

To solve this error, copy the connection string from class library's
app.config to MvcApplication's outer web.config.

(If there are two connection strings found in app.config,you have
to copy the one which has metadata information.)

Build and run the project.

12. When you run, you will encounter the following error:

"The underlying provider failed on Open.", with following InnerException:
"An attempt to attach an auto-named database for file
e:\Documents and Settings\rajesh\My Documents\Visual Studio 2008\Projects\MvcApplication1\MvcApplication1\App_Data\Database1.mdf failed.
 A database with the same name exists, or specified file cannot be opened, or it is located on UNC share."


This error is due the path of the mdf file, which MVC or ASP.NET normally try to find
in App_Data.

To solve this error, go to properties of mdf file and copy the
"Primary File Path".

Replace the path of the mdf file with this one in both places, i.e. class lib and mvc app.

Build and run the app.

13. Now when you run the code which accesses the library function,
you will get following error :


CS0012: The type 'System.Data.Objects.DataClasses.EntityObject' is
defined in an assembly that is not referenced. You must add a
reference to assembly
'System.Data.Entity, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.

To solve this error, add the following line in the <assemblies> section of MvcApplication1 :

<add assembly="System.Data.Entity, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>


Build and run the app.

Everything should work as expected.

Comparing features of LangGraph vs CrewAI

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