{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# Convolutional Neural Network\n\n# Importing the libraries\nimport tensorflow as tf\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator","metadata":{"execution":{"iopub.status.busy":"2021-10-01T01:41:20.323203Z","iopub.execute_input":"2021-10-01T01:41:20.323567Z","iopub.status.idle":"2021-10-01T01:41:25.438408Z","shell.execute_reply.started":"2021-10-01T01:41:20.323477Z","shell.execute_reply":"2021-10-01T01:41:25.437475Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Preprocessing the training dataset\ntrain_datagen = ImageDataGenerator(rescale = 1./255,\n                                   shear_range = 0.2,\n                                   zoom_range = 0.2,\n                                   horizontal_flip = True)\n# Preprocessing the test dataset\ntest_datagen = ImageDataGenerator(rescale = 1./255)","metadata":{"execution":{"iopub.status.busy":"2021-10-01T01:41:25.440007Z","iopub.execute_input":"2021-10-01T01:41:25.441587Z","iopub.status.idle":"2021-10-01T01:41:25.448909Z","shell.execute_reply.started":"2021-10-01T01:41:25.441551Z","shell.execute_reply":"2021-10-01T01:41:25.447923Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Now to read the data from the datasets\ntraining_set = train_datagen.flow_from_directory('Datasets/train',\n                                                 target_size = (64, 64),\n                                                 batch_size = 32,\n                                                 class_mode = 'binary')\ntest_set = test_datagen.flow_from_directory('Datasets/test',\n                                            target_size = (64, 64),\n                                            batch_size = 32,\n                                            class_mode = 'binary')","metadata":{"execution":{"iopub.status.busy":"2021-10-01T01:43:01.700014Z","iopub.execute_input":"2021-10-01T01:43:01.700300Z","iopub.status.idle":"2021-10-01T01:43:01.727864Z","shell.execute_reply.started":"2021-10-01T01:43:01.700271Z","shell.execute_reply":"2021-10-01T01:43:01.726882Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.layers import Conv2D\nfrom tensorflow.keras.layers import Dense\nfrom tensorflow.keras.regularizers import l2","metadata":{"execution":{"iopub.status.busy":"2021-10-01T01:41:25.726474Z","iopub.status.idle":"2021-10-01T01:41:25.727004Z","shell.execute_reply.started":"2021-10-01T01:41:25.726824Z","shell.execute_reply":"2021-10-01T01:41:25.726843Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Now its time for CCFE(Create, Compile, Fit, Evaluate)\n# tf.random.set_seed(42)\n# cnn_model = tf.keras.Sequential([\n#     tf.keras.layers.Conv2D(filters=32,padding=\"same\",kernel_size=3, activation='relu', strides=2, input_shape=[64, 64, 3]),\n#     tf.keras.layers.MaxPool2D(pool_size=2, strides=2),\n    \n#     tf.keras.layers.Conv2D(filters=32,padding=\"same\",kernel_size=3, activation='relu'),\n#     tf.keras.layers.MaxPool2D(pool_size=2, strides=2),\n    \n#     tf.keras.layers.Flatten(),\n    \n#     tf.keras.layers.Dense(units=128, activation='relu')\n# ])\n\n# Initialising the CNN\ncnn = tf.keras.models.Sequential()\n\n# Step 1 - Convolution\ncnn.add(tf.keras.layers.Conv2D(filters=32,padding=\"same\",kernel_size=3, activation='relu', strides=2, input_shape=[64, 64, 3]))\n\n# Step 2 - Pooling\ncnn.add(tf.keras.layers.MaxPool2D(pool_size=2, strides=2))\n\n# Adding a second convolutional layer\ncnn.add(tf.keras.layers.Conv2D(filters=32,padding='same',kernel_size=3, activation='relu'))\ncnn.add(tf.keras.layers.MaxPool2D(pool_size=2, strides=2))\n\n# Step 3 - Flattening\ncnn.add(tf.keras.layers.Flatten())\n\n# Step 4 - Full Connection\ncnn.add(tf.keras.layers.Dense(units=128, activation='relu'))\n\n# Step 5 - Output Layer\n#cnn.add(tf.keras.layers.Dense(units=1, activation='sigmoid'))\n## For Binary Classification\ncnn.add(Dense(1, kernel_regularizer=tf.keras.regularizers.l2(0.01),activation\n             ='linear'))","metadata":{"execution":{"iopub.status.busy":"2021-10-01T01:41:25.728007Z","iopub.status.idle":"2021-10-01T01:41:25.728287Z","shell.execute_reply.started":"2021-10-01T01:41:25.728147Z","shell.execute_reply":"2021-10-01T01:41:25.728162Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cnn.summary()","metadata":{"execution":{"iopub.status.busy":"2021-10-01T01:41:25.729007Z","iopub.status.idle":"2021-10-01T01:41:25.729280Z","shell.execute_reply.started":"2021-10-01T01:41:25.729142Z","shell.execute_reply":"2021-10-01T01:41:25.729156Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Part 3 - Training the CNN\n\n# Compiling the CNN\ncnn.compile(optimizer = 'adam', loss = 'hinge', metrics = ['accuracy'])\n\n# Training the CNN on the Training set and evaluating it on the Test set\nr=cnn.fit(x = training_set, validation_data = test_set, epochs = 15)","metadata":{"execution":{"iopub.status.busy":"2021-10-01T01:41:25.730052Z","iopub.status.idle":"2021-10-01T01:41:25.730317Z","shell.execute_reply.started":"2021-10-01T01:41:25.730181Z","shell.execute_reply":"2021-10-01T01:41:25.730195Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# plot the loss\nimport matplotlib.pyplot as plt\nplt.plot(r.history['loss'], label='train loss')\nplt.plot(r.history['val_loss'], label='val loss')\nplt.legend()\nplt.show()\nplt.savefig('LossVal_loss')\n\n# plot the accuracy\nplt.plot(r.history['accuracy'], label='train acc')\nplt.plot(r.history['val_accuracy'], label='val acc')\nplt.legend()\nplt.show()\nplt.savefig('AccVal_acc')","metadata":{"execution":{"iopub.status.busy":"2021-10-01T01:41:25.731511Z","iopub.status.idle":"2021-10-01T01:41:25.731789Z","shell.execute_reply.started":"2021-10-01T01:41:25.731644Z","shell.execute_reply":"2021-10-01T01:41:25.731658Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# save it as a h5 file\n\n\nfrom tensorflow.keras.models import load_model\n\ncnn.save('model_rcat_dog.h5')","metadata":{"execution":{"iopub.status.busy":"2021-10-01T01:41:25.732757Z","iopub.status.idle":"2021-10-01T01:41:25.733026Z","shell.execute_reply.started":"2021-10-01T01:41:25.732886Z","shell.execute_reply":"2021-10-01T01:41:25.732900Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.models import load_model\n \n# load model\nmodel = load_model('model_rcat_dog.h5')","metadata":{"execution":{"iopub.status.busy":"2021-10-01T01:41:25.733951Z","iopub.status.idle":"2021-10-01T01:41:25.734226Z","shell.execute_reply.started":"2021-10-01T01:41:25.734083Z","shell.execute_reply":"2021-10-01T01:41:25.734096Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2021-10-01T01:41:25.735149Z","iopub.status.idle":"2021-10-01T01:41:25.735460Z","shell.execute_reply.started":"2021-10-01T01:41:25.735291Z","shell.execute_reply":"2021-10-01T01:41:25.735311Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Part 4 - Making a single prediction\n\nimport numpy as np\nfrom tensorflow.keras.preprocessing import image\ntest_image = image.load_img('Datasets/test/test1/4251.jpg', target_size = (64,64))\ntest_image = image.img_to_array(test_image)\ntest_image = test_image/255\ntest_image = np.expand_dims(test_image, axis = 0)\nresult = cnn.predict(test_image)","metadata":{"execution":{"iopub.status.busy":"2021-10-01T01:41:25.736984Z","iopub.status.idle":"2021-10-01T01:41:25.737429Z","shell.execute_reply.started":"2021-10-01T01:41:25.737178Z","shell.execute_reply":"2021-10-01T01:41:25.737200Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result","metadata":{"execution":{"iopub.status.busy":"2021-10-01T01:41:25.738960Z","iopub.status.idle":"2021-10-01T01:41:25.739416Z","shell.execute_reply.started":"2021-10-01T01:41:25.739159Z","shell.execute_reply":"2021-10-01T01:41:25.739181Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if result[0]<0:\n    print(\"The image classified is cat\")\nelse:\n    print(\"The image classified is dog\")","metadata":{"execution":{"iopub.status.busy":"2021-10-01T01:41:25.740631Z","iopub.status.idle":"2021-10-01T01:41:25.741077Z","shell.execute_reply.started":"2021-10-01T01:41:25.740843Z","shell.execute_reply":"2021-10-01T01:41:25.740867Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}