{"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":"# Unzip the zip file","metadata":{}},{"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport cv2\nimport matplotlib.pyplot as plt\nimport tensorflow as tf\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense, Flatten, Dropout, Activation, Conv2D, MaxPooling2D\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import zipfile\n\nwith zipfile.ZipFile(\"../input/dogs-vs-cats/train.zip\",\"r\") as  z:   # we unzip the files into output of the kaggle\n    z.extractall(\".\")                                                # and we can reach this file with path of output of the kaggle.\n    \nwith zipfile.ZipFile(\"../input/dogs-vs-cats/test1.zip\",\"r\") as  z:\n    z.extractall(\".\")\n\nimport os\nprint(os.listdir(\"../input/\")) \nprint(os.listdir(\"../input/dogs-vs-cats\"))\n\nprint(len(os.listdir(\"./test1\")))  # Our output path = \"./\" ./\nprint(len(os.listdir(\"./train\")))\n","metadata":{"execution":{"iopub.status.busy":"2022-07-05T19:47:41.861951Z","iopub.execute_input":"2022-07-05T19:47:41.863252Z","iopub.status.idle":"2022-07-05T19:48:05.203511Z","shell.execute_reply.started":"2022-07-05T19:47:41.863194Z","shell.execute_reply":"2022-07-05T19:48:05.201092Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path = \"./\"\npath_train = \"/kaggle/working/train\"\npath_test1 = \"./test1\"\n\nfiles_train = os.listdir(path_train)\nfiles_test1 = os.listdir(path_test1)\n\nprint(type(path_train))\nprint(path_train)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T19:48:05.206731Z","iopub.execute_input":"2022-07-05T19:48:05.207408Z","iopub.status.idle":"2022-07-05T19:48:05.238922Z","shell.execute_reply.started":"2022-07-05T19:48:05.207361Z","shell.execute_reply":"2022-07-05T19:48:05.23765Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"condition = []\nfor i in files_train:\n    if i.split(\".\")[0] == \"cat\":\n        condition.append(\"cat\")\n    elif i.split(\".\")[0] == \"dog\":\n        condition.append(\"dog\")\n    else:\n        condition.append(\"neither\")\n\ndata_train = pd.DataFrame({\"file name\":files_train, \"label\":condition} )\n\nprint(data_train)\nprint(data_train.groupby(\"label\").count())\n\nprint(files_train[4])","metadata":{"execution":{"iopub.status.busy":"2022-07-05T19:48:05.241092Z","iopub.execute_input":"2022-07-05T19:48:05.2415Z","iopub.status.idle":"2022-07-05T19:48:05.358422Z","shell.execute_reply.started":"2022-07-05T19:48:05.241451Z","shell.execute_reply":"2022-07-05T19:48:05.357209Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.path.join(path_train,files_train[2])","metadata":{"execution":{"iopub.status.busy":"2022-07-05T20:15:35.858205Z","iopub.execute_input":"2022-07-05T20:15:35.858711Z","iopub.status.idle":"2022-07-05T20:15:35.867855Z","shell.execute_reply.started":"2022-07-05T20:15:35.85867Z","shell.execute_reply":"2022-07-05T20:15:35.866333Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nfor p in range(len(files_train)):    \n    img_array = cv2.imread(os.path.join(path_train,files_train[p]),cv2.IMREAD_GRAYSCALE)\n    new_img_array = cv2.resize(img_array,dsize=(80,80))\n    plt.imshow(new_img_array,cmap=\"gray\")\n    break\n    \n","metadata":{"execution":{"iopub.status.busy":"2022-07-05T19:48:05.363203Z","iopub.execute_input":"2022-07-05T19:48:05.364046Z","iopub.status.idle":"2022-07-05T19:48:05.82871Z","shell.execute_reply.started":"2022-07-05T19:48:05.363995Z","shell.execute_reply":"2022-07-05T19:48:05.818727Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"\n\n","metadata":{}},{"cell_type":"markdown","source":"Declare your training array X and your target array y. Here X will be the array of pixels and y will be value 0 or 1 indicating its a dog or cat Write convert function to map category \"dog\" or \"cat\" into 1 and 0\n","metadata":{}},{"cell_type":"code","source":"X = []\ny = []\n\nconvert = lambda category : int(category == 'dog')\n\n\ndef create_test_data(path_train):\n    for p in files_train:\n        category = p.split(\".\")[0]\n        category = convert(category)\n        img_array = cv2.imread(os.path.join(path_train,p),cv2.IMREAD_GRAYSCALE)\n        new_img_array = cv2.resize(img_array, dsize=(80,80))\n        X.append(new_img_array)\n        y.append(category)\n        ","metadata":{"execution":{"iopub.status.busy":"2022-07-05T19:48:05.831447Z","iopub.execute_input":"2022-07-05T19:48:05.83224Z","iopub.status.idle":"2022-07-05T19:48:05.871085Z","shell.execute_reply.started":"2022-07-05T19:48:05.832186Z","shell.execute_reply":"2022-07-05T19:48:05.868632Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"But also later convert X and y into numpy array We also have to reshape X with the below code\n\nnp.array(X).reshape(-1, 80,80,1)","metadata":{}},{"cell_type":"code","source":"create_test_data(path_train)\nX = np.array(X).reshape(-1,80,80,1)\ny = np.array(y)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T19:48:05.874159Z","iopub.execute_input":"2022-07-05T19:48:05.874916Z","iopub.status.idle":"2022-07-05T19:48:38.057055Z","shell.execute_reply.started":"2022-07-05T19:48:05.874877Z","shell.execute_reply":"2022-07-05T19:48:38.055988Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"If you want to save your processed training (X) and target (y) you can use pickle. ","metadata":{}},{"cell_type":"code","source":"import pickle \n\npickle.dump(X, open(\"train_x\", \"wb\"))\npickle.dump(y, open(\"train_y\", \"wb\"))\n\n","metadata":{"execution":{"iopub.status.busy":"2022-07-05T19:48:38.0585Z","iopub.execute_input":"2022-07-05T19:48:38.059099Z","iopub.status.idle":"2022-07-05T19:48:38.661855Z","shell.execute_reply.started":"2022-07-05T19:48:38.059063Z","shell.execute_reply":"2022-07-05T19:48:38.660682Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Normalize\n","metadata":{}},{"cell_type":"code","source":"#Normalize data\nX = X/255.0","metadata":{"execution":{"iopub.status.busy":"2022-07-05T19:48:38.663402Z","iopub.execute_input":"2022-07-05T19:48:38.664004Z","iopub.status.idle":"2022-07-05T19:48:39.254184Z","shell.execute_reply.started":"2022-07-05T19:48:38.663968Z","shell.execute_reply":"2022-07-05T19:48:39.252726Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Build CNN model","metadata":{}},{"cell_type":"markdown","source":"Define a Sequential model","metadata":{}},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Start adding layers to it.\nAdds a densely-connected layer with 64 units to the model:","metadata":{}},{"cell_type":"code","source":"model = Sequential()\n# Adds a densely-connected layer with 64 units to the model:\nmodel.add(Conv2D(64,(3,3), activation = 'relu', input_shape = X.shape[1:]))\nmodel.add(MaxPooling2D(pool_size = (2,2)))\n# Add another:\nmodel.add(Conv2D(64,(3,3), activation = 'relu'))\nmodel.add(MaxPooling2D(pool_size = (2,2)))\n\nmodel.add(Flatten())\nmodel.add(Dense(64, activation='relu'))\n# Add a softmax layer with 10 output units:\nmodel.add(Dense(1, activation='sigmoid'))\n\nmodel.compile(optimizer=\"adam\",\n              loss='binary_crossentropy',\n              metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2022-07-05T19:48:39.255836Z","iopub.execute_input":"2022-07-05T19:48:39.2562Z","iopub.status.idle":"2022-07-05T19:48:39.508748Z","shell.execute_reply.started":"2022-07-05T19:48:39.256167Z","shell.execute_reply":"2022-07-05T19:48:39.507264Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Now we will fit our model with training data.\n\nEpochs :- How many times our model will go through data\n\nBatch size :- How much amount of data at once you wanna pass through the model\n\nvalidation_split :- How much amount of data (in this case its 20 %) you will need to check cross validation error","metadata":{}},{"cell_type":"code","source":"model.fit(X, y, epochs=10, batch_size=32, validation_split=0.2)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T19:48:39.510527Z","iopub.execute_input":"2022-07-05T19:48:39.510981Z","iopub.status.idle":"2022-07-05T20:05:24.9703Z","shell.execute_reply.started":"2022-07-05T19:48:39.510941Z","shell.execute_reply":"2022-07-05T20:05:24.968981Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Now we have to preporcess our test data also same as that our training data.","metadata":{}},{"cell_type":"code","source":"X_test = []\nid_line = []\ndef create_test1_data(path_test1):\n    for p in files_test1:\n        id_line.append(p.split(\".\")[0])\n        img_array = cv2.imread(os.path.join(path_test1,p),cv2.IMREAD_GRAYSCALE)\n        new_img_array = cv2.resize(img_array, dsize=(80, 80))\n        X_test.append(new_img_array)\ncreate_test1_data(path_test1)\nprint(type(X_test))\nX_test = np.array(X_test).reshape(-1,80,80,1)\nprint(type(X_test))\nX_test = X_test/255\nprint(type(X_test))","metadata":{"execution":{"iopub.status.busy":"2022-07-05T20:05:24.972681Z","iopub.execute_input":"2022-07-05T20:05:24.974179Z","iopub.status.idle":"2022-07-05T20:05:39.487974Z","shell.execute_reply.started":"2022-07-05T20:05:24.974121Z","shell.execute_reply":"2022-07-05T20:05:39.486665Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Ok. Now the time has come to finally PREDICT\n\nso feed your model with test data to predict","metadata":{}},{"cell_type":"code","source":"predictions = model.predict(X_test)\nprint(type(predictions))\nprint(predictions)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T20:05:39.489471Z","iopub.execute_input":"2022-07-05T20:05:39.490465Z","iopub.status.idle":"2022-07-05T20:05:55.547863Z","shell.execute_reply.started":"2022-07-05T20:05:39.490426Z","shell.execute_reply":"2022-07-05T20:05:55.546562Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"We are rounding the result here as we used sigmoid function and we got the probablity values in our predicted dataset","metadata":{}},{"cell_type":"code","source":"predicted_val = [int(round(p[0])) for p in predictions]\nprint(type(predicted_val))\n#print(predicted_val)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T20:05:55.553003Z","iopub.execute_input":"2022-07-05T20:05:55.553405Z","iopub.status.idle":"2022-07-05T20:05:55.593639Z","shell.execute_reply.started":"2022-07-05T20:05:55.55337Z","shell.execute_reply":"2022-07-05T20:05:55.592658Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Now you have to make submission data frame to submit your resultse","metadata":{}},{"cell_type":"code","source":"submission_df = pd.DataFrame({'id':id_line, 'label':predicted_val})\n\nprint(submission_df.info())\nprint(submission_df.groupby(\"label\").count())\nprint(submission_df.head())\n\n","metadata":{"execution":{"iopub.status.busy":"2022-07-05T20:05:55.595072Z","iopub.execute_input":"2022-07-05T20:05:55.59566Z","iopub.status.idle":"2022-07-05T20:05:55.638657Z","shell.execute_reply.started":"2022-07-05T20:05:55.59562Z","shell.execute_reply":"2022-07-05T20:05:55.637605Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Write your data frame to a csv file","metadata":{}},{"cell_type":"code","source":"submission_df.to_csv(\"submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-05T20:05:55.640268Z","iopub.execute_input":"2022-07-05T20:05:55.641764Z","iopub.status.idle":"2022-07-05T20:05:55.676437Z","shell.execute_reply.started":"2022-07-05T20:05:55.641657Z","shell.execute_reply":"2022-07-05T20:05:55.674902Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(files_test1[3])","metadata":{"execution":{"iopub.status.busy":"2022-07-05T20:05:55.678309Z","iopub.execute_input":"2022-07-05T20:05:55.679379Z","iopub.status.idle":"2022-07-05T20:05:55.685143Z","shell.execute_reply.started":"2022-07-05T20:05:55.679333Z","shell.execute_reply":"2022-07-05T20:05:55.683596Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"\"dog\" = 1 and \"cat\" = 0","metadata":{}},{"cell_type":"code","source":"img_array = cv2.imread(os.path.join(path_test1,files_test1[3]),cv2.IMREAD_GRAYSCALE)\nnew_img_array = cv2.resize(img_array, dsize=(80, 80))\nplt.imshow(new_img_array,cmap=\"gray\")\nlabel = submission_df[[\"label\"]]\nprint(np.array(label)[3])","metadata":{"execution":{"iopub.status.busy":"2022-07-05T20:05:55.687191Z","iopub.execute_input":"2022-07-05T20:05:55.688437Z","iopub.status.idle":"2022-07-05T20:05:55.920543Z","shell.execute_reply.started":"2022-07-05T20:05:55.688397Z","shell.execute_reply":"2022-07-05T20:05:55.919111Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_array = cv2.imread(os.path.join(path_test1,files_test1[5]),cv2.IMREAD_GRAYSCALE)\nnew_img_array = cv2.resize(img_array, dsize=(80, 80))\nplt.imshow(new_img_array,cmap=\"gray\")\nlabel = submission_df[[\"label\"]]\nprint(np.array(label)[5])","metadata":{"execution":{"iopub.status.busy":"2022-07-05T20:05:55.922349Z","iopub.execute_input":"2022-07-05T20:05:55.92273Z","iopub.status.idle":"2022-07-05T20:05:56.139824Z","shell.execute_reply.started":"2022-07-05T20:05:55.922695Z","shell.execute_reply":"2022-07-05T20:05:56.13834Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_array = cv2.imread(os.path.join(path_test1,files_test1[44]),cv2.IMREAD_GRAYSCALE)\nnew_img_array = cv2.resize(img_array, dsize=(80, 80))\nplt.imshow(new_img_array,cmap=\"gray\")\nlabel = submission_df[[\"label\"]]\nprint(np.array(label)[44])","metadata":{"execution":{"iopub.status.busy":"2022-07-05T20:05:56.141287Z","iopub.execute_input":"2022-07-05T20:05:56.142379Z","iopub.status.idle":"2022-07-05T20:05:56.353978Z","shell.execute_reply.started":"2022-07-05T20:05:56.142338Z","shell.execute_reply":"2022-07-05T20:05:56.352526Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}