Cat vs Non-Cat classifier please point me to my mistakes

Hi,

I’m attempting on cat vs non-cat classifier but I’m stuck in this problem. I don’t know what mistake am I doing in this. I’ve read few solution on this but not able to resolve this as everyone attempts the problem differently and very less explanation is available where I can find step-by-step to connect with the right steps. Can you please point me to my mistake or changes that I should do.

https://jupyter.e.cloudxlab.com/user/manojgupta915880/notebooks/cloudxlab_jupyter_notebooks/Untitled6.ipynb

Regards
Manoj

Manoj - I don’t think we will be able to access your notebook.

Cloudx Team - please enable to option to make our notebooks Public, so that we can share the code in URL.

Srini

import os as os
import numpy as np
import matplotlib.pyplot as plt
import h5py
from PIL import Image
%matplotlib inline


def load_data( path = '/cxldata/datasets/project/cat-non-cat' ):
    train_data_path = os.path.join( path, 'train_catvnoncat.h5' )

    # Load training data and segregate Features from Labels.
    train_data = h5py.File( train_data_path, "r" )
    train_data_x = np.array( train_data["train_set_x"][:] ) # train set features (Converting image to numpy array)
    train_data_y = np.array( train_data["train_set_y"][:] ) # train set labels

    test_data_path = os.path.join( path, 'test_catvnoncat.h5' )

    # Load test data and segregate Features from Labels.
    test_data = h5py.File( test_data_path, "r" )
    test_data_x = np.array( test_data["test_set_x"][:] ) # test set features (Converting image to numpy array)
    test_data_y = np.array( test_data["test_set_y"][:] ) # test set labels

    classes = np.array( test_data["list_classes"][:] )

    return( train_data_x, train_data_y, test_data_x, test_data_y, classes )

train_set_x_orig, train_set_y_orig, test_set_x_orig, test_set_y_orig, classes = load_data( )


# Explore your dataset 
print ("Number of training examples: " + str( train_set_x_orig.shape[0] ) )
print ("Number of testing examples: " + str( test_set_x_orig.shape[0] ) )
print ("Each image is of size: " + str(train_set_x_orig.shape[1:]) )
print ("train_set_x_orig shape: " + str(train_set_x_orig.shape))
print ("train_set_y_orig shape: " + str(train_set_y_orig.shape))
print ("test_set_x_orig shape: " + str(test_set_x_orig.shape))
print ("test_set_y_orig shape: " + str(test_set_y_orig.shape))

# Reshape the train and test set labels
train_set_y = train_set_y_orig.reshape((1, train_set_y_orig.shape[0]))
test_set_y = test_set_y_orig.reshape((1, test_set_y_orig.shape[0]))

# Explore your dataset 
print ("Number of training examples: " + str( train_set_x_orig.shape[0] ) )
print ("Number of testing examples: " + str( test_set_x_orig.shape[0] ) )
print ("Each image is of size: " + str(train_set_x_orig.shape[1:]) )
print ("train_set_x_orig shape: " + str(train_set_x_orig.shape))
print ("train_set_y shape: " + str(train_set_y.shape))
print ("test_set_x_orig shape: " + str(test_set_x_orig.shape))
print ("test_set_y shape: " + str(test_set_y.shape))
classes

# Example of a picture
index = 2
plt.imshow(train_set_x_orig[index]) # You should see a cat image
y = train_set_y[0][index]
y_class = classes[train_set_y[0][index]].decode("utf-8")
print("Lable = ", y , "(", y_class,")")


from tensorflow import keras

def define_model():
    model = keras.models.Sequential([
    keras.layers.Flatten(input_shape=[64, 64,3]),
    keras.layers.Dense(300, activation=keras.activations.relu, name='Layer1'),
    keras.layers.Dense(250, activation="relu", name='Layer2'),
    keras.layers.Dense(100, activation="relu", name='Layer3'),
    keras.layers.Dense(1, activation="sigmoid", name='Layer4')
    ])
    return( model )


model = define_model()


model.layers

model.compile(loss="binary_crossentropy", optimizer="sgd", metrics=["accuracy"])
#model.compile(loss="sparse_categorical_crossentropy", optimizer="sgd", metrics=["accuracy"])

history = model.fit(train_set_x_orig, train_set_y_orig, epochs=30, validation_split=0.2)

test_pred = []
for predict_img in range(test_set_y.size):
    image = test_set_x_orig[predict_img]
    test_pred.append( model.predict_classes(image.reshape(1, 64, 64, 3)) )

# Example of a picture
test_pred[1][0]

if test_pred[0][0] == 1:
    prediction = 'cat'
else:
    prediction = 'non-cat'

index = predict_img
plt.imshow(test_set_x_orig[index]) # You should see a cat image
y_class = classes[test_set_y[0][index]].decode("utf-8")

print( "Prediction = ", prediction )
print( "Actual     = ", y_class)

Hi, Manoj.

What is your CloudXLab username?

Normalized the data by dividing with 255
train_set_x_orig, train_set_y_orig, test_set_x_orig, test_set_y_orig, classes = load_data( )
train_set_x, test_set_x = train_set_x_orig/255, test_set_x_orig/255

Reshaped so that it will (num_samples, num_targets) so it will be (209,1) and (50,1)

Reshape the train and test set labels

train_set_y = train_set_y_orig.reshape(train_set_y_orig.shape[0], -1)
test_set_y = test_set_y_orig.reshape(test_set_y_orig.shape[0], -1)

Your plt.imshow has to be changed to align with the reshaping.
I think that is the problem. If you want the full code with modifications, let me know.

I am a novice - so we should discuss this in the class with Sandeep. But I can assure you, I did the same mistake.

Srini

My User Name = manojgupta915880

Why callback gives error? I’m not able to understand what is wrong with my code. Can someone please help.

Can I use save_best_only=True when I’m using validation_split=0.2 but not using validation data set separately?

import os as os
import numpy as np
import matplotlib.pyplot as plt
import h5py
import tensorflow
from tensorflow import keras
from PIL import Image
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D
from keras.layers import Activation, Dropout, Flatten, Dense
from keras import backend as K
import numpy as np
from keras.preprocessing import image


# Model configuration
from tensorflow.keras.losses import binary_crossentropy
from tensorflow.keras.optimizers import Adam

seed_value = 42
batch_size = 50
img_width, img_height, img_num_channels = 64, 64, 3        #Image Dimentions of our images. i.e. pixel size and Colour
loss_function = binary_crossentropy
no_classes = 1
no_epochs = 25
optimizer = tensorflow.keras.optimizers.RMSprop()   #Adam()   #rmsprop
validation_split = 0.2
verbosity = 1


from keras.callbacks import EarlyStopping, ModelCheckpoint
checkpoint_cb = keras.callbacks.ModelCheckpoint("my_keras_cat_vs_non_cat_model1.h5" )

#if we use a validation set during training, you can set  save_best_only=True
#checkpoint_cb = keras.callbacks.ModelCheckpoint("my_keras_cat_vs_non_cat_model1.h5", save_best_only=True )



def load_data( path = '/cxldata/datasets/project/cat-non-cat' ):
    train_data_path = os.path.join( path, 'train_catvnoncat.h5' )

    # Load training data and segregate Features from Labels.
    train_data = h5py.File( train_data_path, "r" )
    train_data_x = train_data["train_set_x"][:] # train set features (Converting image to numpy array)
    train_data_y = train_data["train_set_y"][:] # train set labels

    test_data_path = os.path.join( path, 'test_catvnoncat.h5' )

    # Load test data and segregate Features from Labels.
    test_data = h5py.File( test_data_path, "r" )
    test_data_x = test_data["test_set_x"][:] # test set features (Converting image to numpy array)
    test_data_y = test_data["test_set_y"][:] # test set labels

    classes = np.array( test_data["list_classes"][:] )

    return( train_data_x, train_data_y, test_data_x, test_data_y, classes )


train_set_x_orig, train_set_y_orig, test_set_x_orig, test_set_y_orig, classes = load_data( )


# Explore your dataset 
print ("Number of training examples: " + str( train_set_x_orig.shape[0] ) )  #train_set_y_orig.size Data size of training data. i.e. no. of images in training set.
print ("Number of testing examples: " + str( test_set_x_orig.shape[0] ) )    #test_set_y_orig.size  Data size of validation data. i.e. no. of images in validation set.
print ("Each image is of size: " + str(train_set_x_orig.shape[1:]) )
print ("train_set_x_orig shape: " + str(train_set_x_orig.shape))
print ("train_set_y_orig shape: " + str(train_set_y_orig.shape))
print ("test_set_x_orig shape: " + str(test_set_x_orig.shape))
print ("test_set_y_orig shape: " + str(test_set_y_orig.shape))


#Augmentation for training set.
train_datagen = ImageDataGenerator(
    rescale = 1./255,
    shear_range = 2.2,
    zoom_range = 0.2,
    horizontal_flip=True)

#Augmentation for validation set. We will be using less augmentation for validation as we want our validation data to look more natural.
validation_datagen = ImageDataGenerator( rescale=1./255 )

train_datagen.fit( train_set_x_orig )
train_datagen.fit( test_set_x_orig )

# Determine shape of the data
input_shape = (img_width, img_height, img_num_channels)

def define_model1():
    model = Sequential()

    model.add( Conv2D( 32, (3,3), input_shape=input_shape) )
    model.add( Activation( 'relu' ) )
    model.add( MaxPooling2D( pool_size = (2, 2) ) )

    model.add( Conv2D( 32, (3,3) ) )
    model.add( Activation( 'relu' ) )
    model.add( MaxPooling2D( pool_size = (2, 2) ) )

    model.add( Conv2D( 64, (3,3) ) )
    model.add( Activation( 'relu' ) )
    model.add( MaxPooling2D( pool_size = (2, 2) ) )

    model.add( Flatten() )
    model.add( Dense(64) )
    model.add( Activation( 'relu' ) )
    model.add( Dropout( 0.5 ) )
    model.add( Dense( 1 ) )
    model.add( Activation( 'sigmoid' ) )
    return( model )

def define_model2():
    # Create the model
    model = Sequential()
    model.add( Conv2D(32, kernel_size=(3, 3), activation='relu', input_shape=input_shape))
    model.add( Conv2D(64, kernel_size=(3, 3), activation='relu') )
    model.add( Conv2D(128, kernel_size=(3, 3), activation='relu') )
    model.add( Flatten() )
    model.add( Dense(64, activation='relu') )
    model.add( Dense(no_classes, activation='sigmoid'))
    return( model )


model1 = define_model1()
model2 = define_model2()

model1.summary()
model2.summary()

#from keras import backend as K
#K.clear_session()
model1.compile( loss=loss_function, optimizer='rmsprop', metrics=['accuracy'] )

# Fit data to model
model1_history = model1.fit(train_set_x_orig, train_set_y_orig,
            batch_size=batch_size,
            epochs=no_epochs,
            #verbose=verbosity,
            validation_split=validation_split,
            callbacks=[checkpoint_cb]
                    )


model2.compile( loss=loss_function, optimizer='rmsprop', metrics=['accuracy'] )

# Fit data to model
model2_history = model2.fit(train_set_x_orig, train_set_y_orig,
            batch_size=batch_size,
            epochs=no_epochs,
            #verbose=verbosity,
            validation_split=validation_split
            #callbacks=[checkpoint_cb]
                    )

Where you are getting the error.
Send the screenshots of the error you are getting.

if “save_best_only=True”.
Then it will save the better model for every epoch.

Dear Satyajit,

Although I’m posting screen shot here but somehow I feel our problems raised in this forum are not getting addressed properly. That’s what I felt in only few of my posts.

Does screen shot make more sense? I’ve posted complete code here which I’m executing, data is available, execution time will be less than a minute and error will be on your screen. What else you need my dear. Screen shorts can be shared where either data cannot be shared or is not available, code cannot be shared or execution time is too high. This happens in projects that we take up while bound to certain constraints in employment contracts and confidentiality. Here everything is available there is no such constraint.

Request you to at least have a look.

Dear Manoj.

The error you are getting "AttributeError: ‘Sequential’ object has no attribute ‘_ckpt_saved_epoch’ " is an attribute error .

Problem:-

We are already importing the modules ModelCheckpoint from keras.callbacks.
from keras.callbacks import EarlyStopping, ModelCheckpoint
but in your code again you are calling “keras.callbacks.ModelCheckpoint”, this will actually calls the callbacks() function which takes the argument as the sequential elements like list as it was implemented like that.

Below Your code

checkpoint_cb = keras.callbacks.ModelCheckpoint(“my_keras_cat_vs_non_cat_model1.h5” )
Note :-

Modified Code =

Just call the ModelCheckpoint directly.
checkpoint_cb = ModelCheckpoint(“my_keras_cat_vs_non_cat_model1.h5”)

Now it should work fine for you. and you can also see the model saved in your workplace. you can explore it.

Note :- Giving the error screenshots always good than the entire code for debug for fast solving.

https://keras.io/api/callbacks/

All the best!

Thanks a lot Satyajit Ji it worked.

Sorry for delayed response.