{"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":"markdown","source":"The data was very big so i could not uploded it on github,\nbut you can download the data from here: https://www.kaggle.com/competitions/dogs-vs-cats/data","metadata":{}},{"cell_type":"code","source":"import tensorflow as tf\nfrom keras import layers, Sequential, losses\nimport sys\nimport os\nfrom sklearn.utils import shuffle\nimport cv2\nimport glob\nimport zipfile\nfrom matplotlib import pyplot as plt\nimport seaborn as sns \nimport pandas as pd\nimport numpy as np\nimport pickle\nfrom keras_preprocessing.image import array_to_img","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Extracting Data","metadata":{}},{"cell_type":"code","source":"trainpath = '../input/dogs-vs-cats/train.zip'\ntestpath = '../input/dogs-vs-cats/test1.zip'\nfilespath = '/kaggle/files/unzipped'\n\nwith zipfile.ZipFile(trainpath, 'r') as zipp:\n    zipp.extractall(filespath)\n    \nwith zipfile.ZipFile(testpath, 'r') as zipp:\n    zipp.extractall(filespath)\n\n\ntrainpath = '/kaggle/files/unzipped/train'\ntestpath = '/kaggle/files/unzipped/test1'","metadata":{"execution":{"iopub.status.busy":"2022-09-17T05:17:52.142562Z","iopub.execute_input":"2022-09-17T05:17:52.143161Z","iopub.status.idle":"2022-09-17T05:18:10.779840Z","shell.execute_reply.started":"2022-09-17T05:17:52.143125Z","shell.execute_reply":"2022-09-17T05:18:10.778879Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f'Total {len(os.listdir(trainpath))} images found in diretory')","metadata":{"execution":{"iopub.status.busy":"2022-09-17T05:18:24.538127Z","iopub.execute_input":"2022-09-17T05:18:24.538837Z","iopub.status.idle":"2022-09-17T05:18:24.583687Z","shell.execute_reply.started":"2022-09-17T05:18:24.538801Z","shell.execute_reply":"2022-09-17T05:18:24.582534Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Set the image size\nIMG_SIZE = 180","metadata":{"execution":{"iopub.status.busy":"2022-09-17T05:18:53.473436Z","iopub.execute_input":"2022-09-17T05:18:53.473791Z","iopub.status.idle":"2022-09-17T05:18:53.478271Z","shell.execute_reply.started":"2022-09-17T05:18:53.473760Z","shell.execute_reply":"2022-09-17T05:18:53.477222Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"all_files = glob.glob(pathname= str(trainpath+'/*.jpg'))","metadata":{"execution":{"iopub.status.busy":"2022-09-17T05:18:55.132168Z","iopub.execute_input":"2022-09-17T05:18:55.133190Z","iopub.status.idle":"2022-09-17T05:18:55.205520Z","shell.execute_reply.started":"2022-09-17T05:18:55.133143Z","shell.execute_reply":"2022-09-17T05:18:55.204601Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Preprocessing the train data","metadata":{}},{"cell_type":"code","source":"X_Train = []\nY_Train = []\nX_Title = []\n\nfor file in all_files:\n    img_array = cv2.imread(file)\n    img_array = cv2.resize(img_array, (IMG_SIZE, IMG_SIZE))\n    X_Train.append(list(img_array))\n    bkslsh = file.split('/')[-1]\n    sepu = bkslsh.split('.')[0]\n    X_Title.append(sepu)\n    if sepu == 'dog':\n        Y_Train.append(1)\n    else:\n        Y_Train.append(0)","metadata":{"execution":{"iopub.status.busy":"2022-09-17T05:18:58.513293Z","iopub.execute_input":"2022-09-17T05:18:58.514196Z","iopub.status.idle":"2022-09-17T05:19:51.083327Z","shell.execute_reply.started":"2022-09-17T05:18:58.514145Z","shell.execute_reply":"2022-09-17T05:19:51.082278Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(X_Train), len(Y_Train)","metadata":{"execution":{"iopub.status.busy":"2022-09-17T05:19:54.382503Z","iopub.execute_input":"2022-09-17T05:19:54.383091Z","iopub.status.idle":"2022-09-17T05:19:54.396822Z","shell.execute_reply.started":"2022-09-17T05:19:54.383048Z","shell.execute_reply":"2022-09-17T05:19:54.394316Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"all_test_files = glob.glob(pathname=str(testpath+'/*.jpg'))","metadata":{"execution":{"iopub.status.busy":"2022-09-17T05:19:57.812312Z","iopub.execute_input":"2022-09-17T05:19:57.813465Z","iopub.status.idle":"2022-09-17T05:19:57.853442Z","shell.execute_reply.started":"2022-09-17T05:19:57.813418Z","shell.execute_reply":"2022-09-17T05:19:57.852529Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Preprocessing test Data","metadata":{}},{"cell_type":"code","source":"X_Test = []\n\nfor file in all_test_files:\n    img_array = cv2.imread(file)\n    img_array = cv2.resize(img_array, (IMG_SIZE, IMG_SIZE))\n    X_Test.append(list(img_array))\n    ","metadata":{"execution":{"iopub.status.busy":"2022-09-17T05:20:00.052554Z","iopub.execute_input":"2022-09-17T05:20:00.053461Z","iopub.status.idle":"2022-09-17T05:20:25.952520Z","shell.execute_reply.started":"2022-09-17T05:20:00.053425Z","shell.execute_reply":"2022-09-17T05:20:25.951474Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(X_Test)","metadata":{"execution":{"iopub.status.busy":"2022-09-17T05:58:37.064419Z","iopub.execute_input":"2022-09-17T05:58:37.065518Z","iopub.status.idle":"2022-09-17T05:58:37.074357Z","shell.execute_reply.started":"2022-09-17T05:58:37.065477Z","shell.execute_reply":"2022-09-17T05:58:37.070717Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = np.array(X_Train)\nY_train = np.array(Y_Train)\nX_test = np.array(X_Test)\n\nprint(f'type: \\n{type(X_train)}\\nshape: \\n{X_train.shape}\\nFirst Array: \\n{X_train[0]}')\nprint(f'type: \\n{type(X_test)}\\nshape: \\n{X_test.shape}\\nFirst Array: \\n{X_test[0]}')","metadata":{"execution":{"iopub.status.busy":"2022-09-17T05:29:01.118454Z","iopub.execute_input":"2022-09-17T05:29:01.119368Z","iopub.status.idle":"2022-09-17T05:29:07.285339Z","shell.execute_reply.started":"2022-09-17T05:29:01.119329Z","shell.execute_reply":"2022-09-17T05:29:07.284299Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, Y_train = shuffle(X_train, Y_train, random_state=True)","metadata":{"execution":{"iopub.status.busy":"2022-09-17T05:29:32.664090Z","iopub.execute_input":"2022-09-17T05:29:32.664830Z","iopub.status.idle":"2022-09-17T05:29:36.747274Z","shell.execute_reply.started":"2022-09-17T05:29:32.664792Z","shell.execute_reply":"2022-09-17T05:29:36.746274Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y_train = tf.one_hot(Y_train, 2)","metadata":{"execution":{"iopub.status.busy":"2022-09-17T05:29:43.104059Z","iopub.execute_input":"2022-09-17T05:29:43.104448Z","iopub.status.idle":"2022-09-17T05:29:45.486630Z","shell.execute_reply.started":"2022-09-17T05:29:43.104412Z","shell.execute_reply":"2022-09-17T05:29:45.485536Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_Train=X_train[:22000]\nY_Train=Y_train[:22000]\nX_val=X_train[22000:]\ny_val=Y_train[22000:]","metadata":{"execution":{"iopub.status.busy":"2022-09-17T05:29:49.343373Z","iopub.execute_input":"2022-09-17T05:29:49.344320Z","iopub.status.idle":"2022-09-17T05:29:49.642804Z","shell.execute_reply.started":"2022-09-17T05:29:49.344276Z","shell.execute_reply":"2022-09-17T05:29:49.641873Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_Title = np.array(X_Title)","metadata":{"execution":{"iopub.status.busy":"2022-09-17T05:29:52.768453Z","iopub.execute_input":"2022-09-17T05:29:52.768812Z","iopub.status.idle":"2022-09-17T05:29:52.786091Z","shell.execute_reply.started":"2022-09-17T05:29:52.768781Z","shell.execute_reply":"2022-09-17T05:29:52.785034Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.Series(X_Title).value_counts().plot.bar()","metadata":{"execution":{"iopub.status.busy":"2022-09-17T05:29:57.593477Z","iopub.execute_input":"2022-09-17T05:29:57.593834Z","iopub.status.idle":"2022-09-17T05:29:57.837403Z","shell.execute_reply.started":"2022-09-17T05:29:57.593802Z","shell.execute_reply":"2022-09-17T05:29:57.836493Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Callbacks","metadata":{}},{"cell_type":"code","source":"from keras.callbacks import EarlyStopping, ReduceLROnPlateau","metadata":{"execution":{"iopub.status.busy":"2022-09-17T05:30:03.023654Z","iopub.execute_input":"2022-09-17T05:30:03.024023Z","iopub.status.idle":"2022-09-17T05:30:03.028749Z","shell.execute_reply.started":"2022-09-17T05:30:03.023990Z","shell.execute_reply":"2022-09-17T05:30:03.027662Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"EarlyStop = EarlyStopping(patience=10)","metadata":{"execution":{"iopub.status.busy":"2022-09-17T05:30:05.843463Z","iopub.execute_input":"2022-09-17T05:30:05.843822Z","iopub.status.idle":"2022-09-17T05:30:05.848340Z","shell.execute_reply.started":"2022-09-17T05:30:05.843792Z","shell.execute_reply":"2022-09-17T05:30:05.847298Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learning_rate_reduction = ReduceLROnPlateau(monitor='val_acc', \n                                            lr=1e-3,\n                                            patience=2, \n                                            verbose=1, \n                                            factor=0.5, \n                                            min_lr=0.00001)","metadata":{"execution":{"iopub.status.busy":"2022-09-17T05:30:18.484736Z","iopub.execute_input":"2022-09-17T05:30:18.485109Z","iopub.status.idle":"2022-09-17T05:30:18.490672Z","shell.execute_reply.started":"2022-09-17T05:30:18.485075Z","shell.execute_reply":"2022-09-17T05:30:18.489633Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"callbacks = [EarlyStop, learning_rate_reduction]","metadata":{"execution":{"iopub.status.busy":"2022-09-17T05:30:32.264292Z","iopub.execute_input":"2022-09-17T05:30:32.264650Z","iopub.status.idle":"2022-09-17T05:30:32.272251Z","shell.execute_reply.started":"2022-09-17T05:30:32.264619Z","shell.execute_reply":"2022-09-17T05:30:32.271255Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data Augmentation","metadata":{}},{"cell_type":"code","source":"data_augmentation_layer = Sequential([                                    \n  layers.RandomFlip('horizontal',input_shape=(IMG_SIZE, IMG_SIZE, 3)),\n  layers.RandomRotation(0.15),\n  layers.RandomZoom(0.2),\n  layers.RandomWidth(0.1),\n  layers.RandomHeight(0.1),\n], name='data_augmentation')","metadata":{"execution":{"iopub.status.busy":"2022-09-17T05:30:35.398347Z","iopub.execute_input":"2022-09-17T05:30:35.398710Z","iopub.status.idle":"2022-09-17T05:30:35.657547Z","shell.execute_reply.started":"2022-09-17T05:30:35.398677Z","shell.execute_reply":"2022-09-17T05:30:35.656587Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow import keras","metadata":{"execution":{"iopub.status.busy":"2022-09-17T05:30:40.018094Z","iopub.execute_input":"2022-09-17T05:30:40.018474Z","iopub.status.idle":"2022-09-17T05:30:40.023198Z","shell.execute_reply.started":"2022-09-17T05:30:40.018439Z","shell.execute_reply":"2022-09-17T05:30:40.022085Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Convolutional Neural Networks","metadata":{}},{"cell_type":"code","source":"model = keras.Sequential([\ndata_augmentation_layer,\n    \nkeras.layers.Conv2D(filters=16,kernel_size = (3, 3),strides=1),\nkeras.layers.Activation(activation='relu'),\nkeras.layers.MaxPool2D(),\n    \nkeras.layers.Conv2D(filters=32,kernel_size = (3, 3),strides=1),\nkeras.layers.Activation(activation='relu'),\nkeras.layers.MaxPool2D(),    \nkeras.layers.BatchNormalization(),\n    \nkeras.layers.Conv2D(filters=64,kernel_size = (3, 3),strides=1),\nkeras.layers.Activation(activation='relu'),\nkeras.layers.MaxPool2D(),    \n    \nkeras.layers.Conv2D(filters=128,kernel_size =(3, 3), strides=1),\nkeras.layers.Activation(activation='relu'),\nkeras.layers.MaxPool2D(),\n  \nkeras.layers.Conv2D(filters=256,kernel_size =(3, 3), strides=1),\nkeras.layers.Activation(activation='relu'),\n   \nkeras.layers.GlobalMaxPool2D(),   \n\nkeras.layers.Dense(256,activation='relu') ,\n\nkeras.layers.BatchNormalization(),\nkeras.layers.Dense(128,activation='relu') ,\n    \n\nkeras.layers.Dense(64,activation='relu') ,\n    \nkeras.layers.Dense(32,activation='relu') ,\nkeras.layers.Dense(16,activation='relu') ,\n    \nkeras.layers.Dense(units=2,activation='softmax') ])\nmodel.compile(loss='categorical_crossentropy', optimizer='rmsprop', metrics=['accuracy'])\n\nprint(model.summary())","metadata":{"execution":{"iopub.status.busy":"2022-09-17T05:30:43.738501Z","iopub.execute_input":"2022-09-17T05:30:43.738886Z","iopub.status.idle":"2022-09-17T05:30:44.337282Z","shell.execute_reply.started":"2022-09-17T05:30:43.738847Z","shell.execute_reply":"2022-09-17T05:30:44.336274Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Train the model","metadata":{}},{"cell_type":"code","source":"EPOCHES = 10\nBATCHES = 32\n\nhistory = model.fit(X_Train, Y_Train, epochs=EPOCHES,batch_size=BATCHES,verbose=1,callbacks=callbacks, validation_data=(X_val, y_val))","metadata":{"execution":{"iopub.status.busy":"2022-09-17T05:30:52.434057Z","iopub.execute_input":"2022-09-17T05:30:52.434431Z","iopub.status.idle":"2022-09-17T05:34:20.355184Z","shell.execute_reply.started":"2022-09-17T05:30:52.434398Z","shell.execute_reply":"2022-09-17T05:34:20.354180Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Save the model with wieghts","metadata":{}},{"cell_type":"code","source":"model_json = model.to_json()\nwith open('/kaggle/working/model.json', 'w') as json_file:\n    json_file.write(model_json)","metadata":{"execution":{"iopub.status.busy":"2022-09-17T05:34:47.374441Z","iopub.execute_input":"2022-09-17T05:34:47.375520Z","iopub.status.idle":"2022-09-17T05:34:47.389956Z","shell.execute_reply.started":"2022-09-17T05:34:47.375470Z","shell.execute_reply":"2022-09-17T05:34:47.388919Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save_weights('/kaggle/working/model_weights.h5')","metadata":{"execution":{"iopub.status.busy":"2022-09-17T05:36:09.978745Z","iopub.execute_input":"2022-09-17T05:36:09.979114Z","iopub.status.idle":"2022-09-17T05:36:10.080259Z","shell.execute_reply.started":"2022-09-17T05:36:09.979082Z","shell.execute_reply":"2022-09-17T05:36:10.079276Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Check our model's history","metadata":{}},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n%matplotlib inline\naccuracy = history.history['accuracy']\nval_accuracy = history.history['val_accuracy']\nloss = history.history['loss']\nval_loss = history.history['val_loss']\nepochs = range(len(accuracy))\nplt.plot(epochs, accuracy, 'bo', label='Training accuracy')\nplt.plot(epochs, val_accuracy, 'b', label='Validation accuracy')\nplt.title('Training and validation accuracy')\nplt.legend()\nplt.figure()\nplt.plot(epochs, loss, 'bo', label='Training loss')\nplt.plot(epochs, val_loss, 'b', label='Validation loss')\nplt.title('Training and validation loss')\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-09-17T05:39:09.184200Z","iopub.execute_input":"2022-09-17T05:39:09.184912Z","iopub.status.idle":"2022-09-17T05:39:09.588928Z","shell.execute_reply.started":"2022-09-17T05:39:09.184862Z","shell.execute_reply":"2022-09-17T05:39:09.587770Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Predict the test images","metadata":{}},{"cell_type":"code","source":"predicted_images = model.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2022-09-17T05:41:37.194006Z","iopub.execute_input":"2022-09-17T05:41:37.194399Z","iopub.status.idle":"2022-09-17T05:41:40.610294Z","shell.execute_reply.started":"2022-09-17T05:41:37.194363Z","shell.execute_reply":"2022-09-17T05:41:40.609281Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"CATEGORIES = ['Cat', 'Dog']","metadata":{"execution":{"iopub.status.busy":"2022-09-17T05:42:20.839067Z","iopub.execute_input":"2022-09-17T05:42:20.839716Z","iopub.status.idle":"2022-09-17T05:42:20.845080Z","shell.execute_reply.started":"2022-09-17T05:42:20.839677Z","shell.execute_reply":"2022-09-17T05:42:20.844003Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(17, 17))\nfor n, i in enumerate(list(np.random.randint(0, len(X_test), 25))):\n    plt.subplot(5, 5, n+1)\n    plt.imshow(array_to_img(X_test[i]))\n    plt.axis('off')\n    plt.title(CATEGORIES[np.argmax(predicted_images[i])])","metadata":{"execution":{"iopub.status.busy":"2022-09-17T06:03:48.159691Z","iopub.execute_input":"2022-09-17T06:03:48.160060Z","iopub.status.idle":"2022-09-17T06:03:50.035971Z","shell.execute_reply.started":"2022-09-17T06:03:48.160026Z","shell.execute_reply":"2022-09-17T06:03:50.033812Z"},"trusted":true},"execution_count":null,"outputs":[]}]}