├── .gitignore ├── model.h5 ├── ScreenShots ├── output.PNG ├── cat vs dog.JPG ├── input Image.PNG └── modelTraining.PNG ├── trained_Model.py ├── README.md ├── classifier.py └── model.json /.gitignore: -------------------------------------------------------------------------------- 1 | Dataset/ 2 | 3 | -------------------------------------------------------------------------------- /model.h5: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yatheen07/cat-dog-image-classifier/HEAD/model.h5 -------------------------------------------------------------------------------- /ScreenShots/output.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yatheen07/cat-dog-image-classifier/HEAD/ScreenShots/output.PNG -------------------------------------------------------------------------------- /ScreenShots/cat vs dog.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yatheen07/cat-dog-image-classifier/HEAD/ScreenShots/cat vs dog.JPG -------------------------------------------------------------------------------- /ScreenShots/input Image.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yatheen07/cat-dog-image-classifier/HEAD/ScreenShots/input Image.PNG -------------------------------------------------------------------------------- /ScreenShots/modelTraining.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yatheen07/cat-dog-image-classifier/HEAD/ScreenShots/modelTraining.PNG -------------------------------------------------------------------------------- /trained_Model.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Wed Jun 20 13:09:22 2018 4 | 5 | @author: yatheen! 6 | """ 7 | 8 | # Step 1: Import the packages 9 | from keras.models import model_from_json 10 | import cv2 11 | import numpy as np 12 | 13 | # Step 2: Load the Model from Json File 14 | json_file = open('./model.json', 'r') 15 | loaded_model_json = json_file.read() 16 | json_file.close() 17 | loaded_model = model_from_json(loaded_model_json) 18 | 19 | # Step 3: Load the weights 20 | loaded_model.load_weights("./model.h5") 21 | print("Loaded model from disk") 22 | 23 | # Step 4: Compile the model 24 | loaded_model.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy']) 25 | 26 | # Step 5: load the image you want to test 27 | image = cv2.imread('./Dataset/dog_test2.jpg') 28 | image = cv2.resize(image, (50,50)) 29 | image = image.reshape(1, 50, 50, 3) 30 | 31 | cv2.imshow("Input Image", image) 32 | cv2.waitKey(0) 33 | cv2.destroyAllWindows() 34 | # Step 6: Predict to which class your input image has been classified 35 | result = loaded_model.predict_classes(image) 36 | if(result[0][0] == 1): 37 | print("I guess this must be a Dog!") 38 | else: 39 | print("I guess this must be a Cat!") -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # cat-dog-image-classifier 2 | 3 | ## Complete Steps to implement a CNN to classify between cat and dog image 4 | 5 | ### Step 1 : Getting the Dataset 6 | 7 | The dataset is available in the link : https://www.kaggle.com/c/dogs-vs-cats/data 8 | Download this dataset, extract and store it in localdisk 9 | 10 | ### Step 2 : Installing Required Packages [Python 3.6] 11 | 12 | 1. OpenCV ---> '3.4.0' [ Used to handle image operations like : reading the image , resizing , reshaping] 13 | 2. numpy ---> '1.14.4' [ Image that is read will be stored in an numpy array ] 14 | 3. TensorFlow ---> '1.8.0' [ Tensorflow is the backend for Keras ] 15 | 4. Keras ---> '2.1.6' [ Keras is used to implement the CNN ] 16 | 17 | ### Step 3 : How the Model Works ?? 18 | 19 | Note : Spyder is used to develop the code. Set the working Directory Correctely. 20 | Open the trained_model.py file and set the image you want to test [Ref. Line Number 24 ] 21 | 22 | This will predict the output for the image you have specified. 23 | Once you have understood the basic working of the model, its now time to build the classifier from the scratch. 24 | 25 | ### Step 4 : Building the Classifier 26 | 27 | Steps involved in building a classifier is briefed in classifier.py 28 | 13 actual substeps are involved in building your CNN! 29 | This will build the model and save the trained model as a Json file. 30 | The weights of the trained model are stored in a seperate file. 31 | 32 | ### Step 5: How to use the trained Model ? 33 | 34 | Once you have imported the packages, you can directely load your trained classifier from the json file. 35 | Then load the weights from the h5 file saved in the previous step. 36 | This basically has 6 substeps before you can see the result. 37 | 38 | 39 | -------------------------------------------------------------------------------- /classifier.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Wed Jun 13 17:03:32 2018 4 | 5 | @author: yatheen! 6 | 7 | """ 8 | #13 Steps to build CNN 9 | 10 | #Step 1: Import the required packages 11 | 12 | from keras.models import Sequential 13 | from keras.layers import Conv2D 14 | from keras.layers import MaxPooling2D 15 | from keras.layers import Flatten 16 | from keras.layers import Dense 17 | 18 | # Step 2: Initialising the CNN 19 | model = Sequential() 20 | 21 | # Step 3: Convolution 22 | model.add(Conv2D(32, (3, 3), input_shape = (50, 50, 3), activation = 'relu')) 23 | 24 | # Step 4: Pooling 25 | model.add(MaxPooling2D(pool_size = (2, 2))) 26 | 27 | # Step 5: Second convolutional layer 28 | model.add(Conv2D(32, (3, 3), activation = 'relu')) 29 | model.add(MaxPooling2D(pool_size = (2, 2))) 30 | 31 | # Step 6: Flattening 32 | model.add(Flatten()) 33 | 34 | # Step 7: Full connection 35 | model.add(Dense(units = 128, activation = 'relu')) 36 | model.add(Dense(units = 1, activation = 'sigmoid')) 37 | 38 | # Step 8: Compiling the CNN 39 | model.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy']) 40 | 41 | # Step 9: ImageDataGenerator 42 | from keras.preprocessing.image import ImageDataGenerator 43 | 44 | train_datagen = ImageDataGenerator(rescale = 1./255, 45 | shear_range = 0.2, 46 | zoom_range = 0.2, 47 | horizontal_flip = True) 48 | 49 | # Step 10: Load the training Set 50 | training_set = train_datagen.flow_from_directory('./Dataset/training_set', 51 | target_size = (50, 50), 52 | batch_size = 32, 53 | class_mode = 'binary') 54 | # Step 11: Classifier Training 55 | model.fit_generator(training_set, 56 | steps_per_epoch = 4000, 57 | epochs = 25, 58 | validation_steps = 2000) 59 | 60 | # Step 12: Convert the Model to json 61 | model_json = model.to_json() 62 | with open("./model.json","w") as json_file: 63 | json_file.write(model_json) 64 | 65 | # Step 13: Save the weights in a seperate file 66 | model.save_weights("./model.h5") 67 | 68 | print("Classifier trained Successfully!") -------------------------------------------------------------------------------- /model.json: -------------------------------------------------------------------------------- 1 | {"class_name": "Sequential", "config": [{"class_name": "Conv2D", "config": {"name": "conv2d_3", "trainable": true, "batch_input_shape": [null, 50, 50, 3], "dtype": "float32", "filters": 32, "kernel_size": [3, 3], "strides": [1, 1], "padding": "valid", "data_format": "channels_last", "dilation_rate": [1, 1], "activation": "relu", "use_bias": true, "kernel_initializer": {"class_name": "VarianceScaling", "config": {"scale": 1.0, "mode": "fan_avg", "distribution": "uniform", "seed": null}}, "bias_initializer": {"class_name": "Zeros", "config": {}}, "kernel_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "bias_constraint": null}}, {"class_name": "MaxPooling2D", "config": {"name": "max_pooling2d_3", "trainable": true, "pool_size": [2, 2], "padding": "valid", "strides": [2, 2], "data_format": "channels_last"}}, {"class_name": "Conv2D", "config": {"name": "conv2d_4", "trainable": true, "filters": 32, "kernel_size": [3, 3], "strides": [1, 1], "padding": "valid", "data_format": "channels_last", "dilation_rate": [1, 1], "activation": "relu", "use_bias": true, "kernel_initializer": {"class_name": "VarianceScaling", "config": {"scale": 1.0, "mode": "fan_avg", "distribution": "uniform", "seed": null}}, "bias_initializer": {"class_name": "Zeros", "config": {}}, "kernel_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "bias_constraint": null}}, {"class_name": "MaxPooling2D", "config": {"name": "max_pooling2d_4", "trainable": true, "pool_size": [2, 2], "padding": "valid", "strides": [2, 2], "data_format": "channels_last"}}, {"class_name": "Flatten", "config": {"name": "flatten_2", "trainable": true, "data_format": "channels_last"}}, {"class_name": "Dense", "config": {"name": "dense_3", "trainable": true, "units": 128, "activation": "relu", "use_bias": true, "kernel_initializer": {"class_name": "VarianceScaling", "config": {"scale": 1.0, "mode": "fan_avg", "distribution": "uniform", "seed": null}}, "bias_initializer": {"class_name": "Zeros", "config": {}}, "kernel_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "bias_constraint": null}}, {"class_name": "Dense", "config": {"name": "dense_4", "trainable": true, "units": 1, "activation": "sigmoid", "use_bias": true, "kernel_initializer": {"class_name": "VarianceScaling", "config": {"scale": 1.0, "mode": "fan_avg", "distribution": "uniform", "seed": null}}, "bias_initializer": {"class_name": "Zeros", "config": {}}, "kernel_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "bias_constraint": null}}], "keras_version": "2.1.6", "backend": "tensorflow"} --------------------------------------------------------------------------------