Skip to main content

Cat and dog classification

 



# %%  Importing the libraries

from keras.models import Sequential
from keras.layers import Conv2D 
from keras.layers import MaxPooling2D
from keras.layers import Flatten
from keras.layers import Dense

# %%  Initializing the CNN

classifier = Sequential()

# %%  Step-1 Convolution

classifier.add(Conv2D(32, (33), input_shape = (64643), activation = 'relu'))
# All the images come to us does'nt have same size and shape so we have to fix the
 dimensions of the pixels rgb color channel in input shape parameter

# %%  Step - 2 Pooling

classifier.add(MaxPooling2D(pool_size=(22))) 

# %%  Adding a second convolution layer

#classifier.add(Convolution2D(32, 3, 3, activation='relu'))
classifier.add(Conv2D(32, (33), input_shape = (64643),  activation = 'relu'))
classifier.add(MaxPooling2D(pool_size=(22)))

# %%  Step-3 Flattening

classifier.add(Flatten())


# %%  Step-4 Full Connection

# output_dim = random (mostly give it according to input variables but here those are
 too many)

classifier.add(Dense(units=68, activation='relu'))

classifier.add(Dense(units=120, activation='relu'))

# output layer
#classifier.add(Dense(output_dim=1, activation='sigmoid'))
classifier.add(Dense(units=1, activation='sigmoid'))

# %%  Compiling CNN

# adam -> stochastic gradient descent
# binary -> as we have only two categories otherwise 'categorical_crossentropy'
classifier.compile(optimizer='adam', loss="binary_crossentropy", metrics=['accuracy'])


# %%  Fitting CNN to images


from keras.preprocessing.image import ImageDataGenerator

# %%  Rescaling for feature scaling

train_datagen = ImageDataGenerator(rescale=1./255,
                                    shear_range=0.2,
                                    zoom_range=0.2,
                                    horizontal_flip=True


test_datagen = ImageDataGenerator(rescale=1./255)

training_set = train_datagen.flow_from_directory('dataset/training_set',
                                                 target_size=(6464),
                                                 batch_size=32,
                                                 class_mode='binary')

test_set = test_datagen.flow_from_directory('dataset/test_set',
                                            target_size=(6464),
                                            batch_size=32,
                                            class_mode='binary')

classifier.fit_generator(training_set,
               steps_per_epoch=8000,
               epochs=2,
               validation_data=test_set,
               validation_steps=2000)




Comments

Popular posts from this blog

Classification & Confusion Matrix & Accuracy Paradox

Classification  work on voting the object belongs from which classes has more probability  There are two types of classification : Binary classification : There are two classes we have ex: male-female , cat-dog , yes-not  Multiple classification :   There are classes more than two we have ex: traffic signs , face recognition , flower race  , Digit Recognition Confusion matrix :  Confusion matrix is one type of technique to evaluate the model accuracy for classification problem. In this technique we consider how many of positive and negative data points we predict correctly. The main consideration terms are accuracy, precision and recall The accuracy was an appealing matric, because it was a single number. Here precision and recall(sensitivity) are two numbers. So to get the final score (accuracy) of our model we use F1 score, so that we have a single number. Here is the F1 score's mathematical formula: F1 = 2x precision x recall / (precision ...

Python program to check if variable is of integer or string

Let's say if you want to input something of any datatype and want to get datatype only of it. So... Whenever you input some data whether it is string, integer or float like this: i = input('enter something here: ') means without int, str or float put before the syntax, that time your given input is always consider as string  or if you make it like this to add int before syntax; i = int(input('enter something here: '))  it always consider as integer and gives value error when you input string and same thing happens with float, So here is a program to solve this problem of input and get datatype var = input('input to check if variable is of integer or string: ') if var.isdigit() == False:     print(type(var)) else:     var1 = int(var)     print(type(var1))

Multiple classification from many of directories

  # %%  Import nessacary libraries import  numpy  as  np import  pandas  as  pd import  cv2 import  matplotlib.pyplot  as  plt import  os import  glob # %%   Keras Tensorflow libraries from  keras  import  layers from  keras.models  import  Model from  keras.optimizers  import  RMSprop , Adam , Nadam from  keras.preprocessing.image  import  ImageDataGenerator from  keras.layers  import  Input, BatchNormalization, Dense, Dropout, Conv2D, Flatten, GlobalAveragePooling2D, LeakyReLU from  keras.preprocessing.image  import  ImageDataGenerator, img_to_array, load_img # %%  Path path  =   r 'G:/Machine Learning/Project/Lego Mnifigures Classification/dataset' open_dir  =  os....