{"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":"import os\nimport cv2\nimport numpy as np\nimport pandas as pd\nfrom glob import glob\nfrom zipfile import ZipFile\nimport matplotlib.pyplot as plt\nfrom sklearn.model_selection import train_test_split\nimport tensorflow as tf\n%matplotlib inline","metadata":{"execution":{"iopub.status.busy":"2022-01-12T09:03:24.823384Z","iopub.execute_input":"2022-01-12T09:03:24.824097Z","iopub.status.idle":"2022-01-12T09:03:29.779516Z","shell.execute_reply.started":"2022-01-12T09:03:24.823948Z","shell.execute_reply":"2022-01-12T09:03:29.778721Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path = r\"H:\\archive.zip\"\n\ndestination = r\"H:\\archive.zip\" # Extract train and test folders here","metadata":{"execution":{"iopub.status.busy":"2022-01-12T09:28:34.551666Z","iopub.execute_input":"2022-01-12T09:28:34.552226Z","iopub.status.idle":"2022-01-12T09:28:34.555585Z","shell.execute_reply.started":"2022-01-12T09:28:34.552188Z","shell.execute_reply":"2022-01-12T09:28:34.554823Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Unzipping train and test folders\nfor folder in os.listdir(path = path):\n    if folder.split(\".\")[1] == \"zip\": \n        with ZipFile(path + folder, 'r') as zip:\n            zip.extractall(destination)\n            print(f'{folder} extracted done!')","metadata":{"execution":{"iopub.status.busy":"2022-01-12T09:28:37.752238Z","iopub.execute_input":"2022-01-12T09:28:37.752957Z","iopub.status.idle":"2022-01-12T09:28:37.772933Z","shell.execute_reply.started":"2022-01-12T09:28:37.752910Z","shell.execute_reply":"2022-01-12T09:28:37.772101Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for folder in os.listdir(destination):\n    files = glob(destination + folder + \"/*.jpg\")\n    print(f'({folder}): {len(files)} image')","metadata":{"execution":{"iopub.status.busy":"2021-12-25T17:14:44.319109Z","iopub.execute_input":"2021-12-25T17:14:44.319382Z","iopub.status.idle":"2021-12-25T17:14:44.451771Z","shell.execute_reply.started":"2021-12-25T17:14:44.319353Z","shell.execute_reply":"2021-12-25T17:14:44.450994Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Shape of training images\n\ntrain_images_size = []\n\nfiles = glob(destination + \"train/\" + \"/*.jpg\")\nfor file in files:\n    img = cv2.imread(file)\n    train_images_size.append(img.shape)\n        \npd.Series(train_images_size).value_counts()","metadata":{"execution":{"iopub.status.busy":"2021-12-25T16:42:30.182075Z","iopub.execute_input":"2021-12-25T16:42:30.182487Z","iopub.status.idle":"2021-12-25T16:43:24.236922Z","shell.execute_reply.started":"2021-12-25T16:42:30.18245Z","shell.execute_reply":"2021-12-25T16:43:24.236217Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Shape of test images\n\ntest_images_size = []\n\nfiles = glob(destination + \"test1\" + \"/*.jpg\")\nfor file in files:\n    img = cv2.imread(file)\n    train_images_size.append(img.shape)\n        \npd.Series(train_images_size).value_counts()","metadata":{"execution":{"iopub.status.busy":"2021-12-25T16:43:48.869991Z","iopub.execute_input":"2021-12-25T16:43:48.870933Z","iopub.status.idle":"2021-12-25T16:44:15.395947Z","shell.execute_reply.started":"2021-12-25T16:43:48.870864Z","shell.execute_reply":"2021-12-25T16:44:15.395257Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"categorical = {\"cat\":0, \"dog\":1} \nimage_size  = 150","metadata":{"execution":{"iopub.status.busy":"2021-12-25T17:28:12.787967Z","iopub.execute_input":"2021-12-25T17:28:12.788213Z","iopub.status.idle":"2021-12-25T17:28:12.792106Z","shell.execute_reply.started":"2021-12-25T17:28:12.788185Z","shell.execute_reply":"2021-12-25T17:28:12.791392Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def getKey(myValue):\n    for key, value in categorical.items():\n        if myValue == value:\n            return key","metadata":{"execution":{"iopub.status.busy":"2021-12-25T17:15:01.375236Z","iopub.execute_input":"2021-12-25T17:15:01.375838Z","iopub.status.idle":"2021-12-25T17:15:01.380424Z","shell.execute_reply.started":"2021-12-25T17:15:01.375799Z","shell.execute_reply":"2021-12-25T17:15:01.379588Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = []\ny = []\n\nfiles = glob(destination + \"train/*.jpg\")\nfor file in files:\n        \n    if \"dog\" in file:\n        y.append(1)\n    else:\n        y.append(0)\n        \n    img = cv2.imread(file)\n    new_train_size = cv2.resize(img, (image_size, image_size))\n    X.append(new_train_size)","metadata":{"execution":{"iopub.status.busy":"2021-12-25T17:15:11.606769Z","iopub.execute_input":"2021-12-25T17:15:11.60703Z","iopub.status.idle":"2021-12-25T17:15:59.747339Z","shell.execute_reply.started":"2021-12-25T17:15:11.607001Z","shell.execute_reply":"2021-12-25T17:15:59.746601Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)","metadata":{"execution":{"iopub.status.busy":"2021-12-25T17:16:04.443613Z","iopub.execute_input":"2021-12-25T17:16:04.443862Z","iopub.status.idle":"2021-12-25T17:16:04.466939Z","shell.execute_reply.started":"2021-12-25T17:16:04.443835Z","shell.execute_reply":"2021-12-25T17:16:04.466314Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f\"X_train: {len(X_train)} , y_train: {len(y_train)}\")\nprint(f\"X_test: {len(X_test)} , y_test: {len(y_test)}\")","metadata":{"execution":{"iopub.status.busy":"2021-12-25T17:16:08.013653Z","iopub.execute_input":"2021-12-25T17:16:08.014184Z","iopub.status.idle":"2021-12-25T17:16:08.018449Z","shell.execute_reply.started":"2021-12-25T17:16:08.014148Z","shell.execute_reply":"2021-12-25T17:16:08.017688Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10,10))\nfor n , i in enumerate(list(np.random.randint(0,len(X_train),9))) : \n    plt.subplot(3,3,n+1)\n    plt.imshow(X_train[i])   \n    plt.axis('off')\n    plt.title(getKey(y_train[i]))\n    ","metadata":{"execution":{"iopub.status.busy":"2021-12-25T18:01:43.781265Z","iopub.execute_input":"2021-12-25T18:01:43.781828Z","iopub.status.idle":"2021-12-25T18:01:44.353696Z","shell.execute_reply.started":"2021-12-25T18:01:43.781792Z","shell.execute_reply":"2021-12-25T18:01:44.353104Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = []\n\nfiles = glob(destination + \"test1/\" + \"/*.jpg\")\nfor file in files:\n    img = cv2.imread(file)\n    new_test_size = cv2.resize(img, (image_size, image_size))\n    pred.append(new_train_size)","metadata":{"execution":{"iopub.status.busy":"2021-12-25T18:16:12.861853Z","iopub.execute_input":"2021-12-25T18:16:12.862105Z","iopub.status.idle":"2021-12-25T18:16:37.242101Z","shell.execute_reply.started":"2021-12-25T18:16:12.862078Z","shell.execute_reply":"2021-12-25T18:16:37.241338Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f\"pred: {len(pred)}\")","metadata":{"execution":{"iopub.status.busy":"2021-12-25T18:10:41.687795Z","iopub.execute_input":"2021-12-25T18:10:41.688038Z","iopub.status.idle":"2021-12-25T18:10:41.695326Z","shell.execute_reply.started":"2021-12-25T18:10:41.688009Z","shell.execute_reply":"2021-12-25T18:10:41.694549Z"},"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)\ny_test  = np.array(y_test)\nX_pred  = np.array(pred)","metadata":{"execution":{"iopub.status.busy":"2021-12-25T17:17:31.084255Z","iopub.execute_input":"2021-12-25T17:17:31.084561Z","iopub.status.idle":"2021-12-25T17:17:31.816405Z","shell.execute_reply.started":"2021-12-25T17:17:31.084531Z","shell.execute_reply":"2021-12-25T17:17:31.81534Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f'X_train shape  is {X_train.shape}')\nprint(f'y_train shape  is {y_train.shape} \\n')\nprint(f'X_test  shape  is {X_test.shape}')\nprint(f'y_test  shape  is {y_test.shape} \\n')\nprint(f'X_pred  shape  is {X_pred.shape}')","metadata":{"execution":{"iopub.status.busy":"2021-12-25T17:17:34.85905Z","iopub.execute_input":"2021-12-25T17:17:34.859371Z","iopub.status.idle":"2021-12-25T17:17:34.865133Z","shell.execute_reply.started":"2021-12-25T17:17:34.85933Z","shell.execute_reply":"2021-12-25T17:17:34.864084Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Model = tf.keras.models.Sequential([\n        tf.keras.layers.Conv2D(128,kernel_size=(3,3),activation='relu',input_shape=(image_size,image_size,3)),\n        tf.keras.layers.Conv2D(64,kernel_size=(3,3),activation='relu'),\n        #tf.keras.layers.AveragePooling2D(3,3),\n        tf.keras.layers.MaxPool2D(4,4),\n    \n        tf.keras.layers.Conv2D(128,kernel_size=(3,3),activation='relu'),    \n        tf.keras.layers.Conv2D(64,kernel_size=(3,3),activation='relu'),\n        #tf.keras.layers.AveragePooling2D(3,3),\n        tf.keras.layers.MaxPool2D(4,4),\n    \n        tf.keras.layers.Flatten() ,    \n        tf.keras.layers.Dense(512,activation='relu') ,    \n        #tf.keras.layers.Dense(64,activation='relu') ,\n        #tf.keras.layers.Dense(16,activation='relu') ,\n        tf.keras.layers.Dropout(rate=0.3),\n        tf.keras.layers.Dense(1, activation='sigmoid'),    \n        ])\n","metadata":{"execution":{"iopub.status.busy":"2021-12-25T18:27:29.639648Z","iopub.execute_input":"2021-12-25T18:27:29.639914Z","iopub.status.idle":"2021-12-25T18:27:29.703284Z","shell.execute_reply.started":"2021-12-25T18:27:29.639885Z","shell.execute_reply":"2021-12-25T18:27:29.702639Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Model.compile(optimizer =tf.keras.optimizers.Adam(learning_rate=0.0001),\n                   loss='binary_crossentropy',\n                   metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2021-12-25T18:27:36.209546Z","iopub.execute_input":"2021-12-25T18:27:36.211912Z","iopub.status.idle":"2021-12-25T18:27:36.224673Z","shell.execute_reply.started":"2021-12-25T18:27:36.211869Z","shell.execute_reply":"2021-12-25T18:27:36.223932Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = Model.fit(X_train, y_train, epochs=50)","metadata":{"execution":{"iopub.status.busy":"2021-12-25T18:27:40.450156Z","iopub.execute_input":"2021-12-25T18:27:40.450824Z","iopub.status.idle":"2021-12-25T18:50:47.179858Z","shell.execute_reply.started":"2021-12-25T18:27:40.450787Z","shell.execute_reply":"2021-12-25T18:50:47.179095Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.DataFrame(history.history).plot()\nplt.ylabel(\"Value\")\nplt.xlabel(\"Epochs\");","metadata":{"execution":{"iopub.status.busy":"2021-12-25T18:50:56.392691Z","iopub.execute_input":"2021-12-25T18:50:56.393085Z","iopub.status.idle":"2021-12-25T18:50:56.712655Z","shell.execute_reply.started":"2021-12-25T18:50:56.393051Z","shell.execute_reply":"2021-12-25T18:50:56.711911Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Loss, Accuracy = Model.evaluate(X_test, y_test)\n\nprint('Loss is: ', Loss)\nprint('Accuracy is: ', Accuracy)","metadata":{"execution":{"iopub.status.busy":"2021-12-25T18:51:28.157024Z","iopub.execute_input":"2021-12-25T18:51:28.157293Z","iopub.status.idle":"2021-12-25T18:51:30.872659Z","shell.execute_reply.started":"2021-12-25T18:51:28.157249Z","shell.execute_reply":"2021-12-25T18:51:30.871928Z"},"trusted":true},"execution_count":null,"outputs":[]}]}