{"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":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-05-25T09:16:39.936813Z","iopub.execute_input":"2022-05-25T09:16:39.937099Z","iopub.status.idle":"2022-05-25T09:16:39.949917Z","shell.execute_reply.started":"2022-05-25T09:16:39.937069Z","shell.execute_reply":"2022-05-25T09:16:39.949053Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Much help has been taken from the following notebooks, I'll try to mention the adapted line. But go through these too to get a better idea\n# 1.https://www.kaggle.com/code/ruchibahl18/cats-vs-dogs-basic-cnn-tutorial/notebook\n# 2.https://www.kaggle.com/code/kartik2khandelwal/beginner-s-implementation-of-deep-learning\n# 3.https://www.kaggle.com/code/uysimty/keras-cnn-dog-or-cat-classification\n# 4.https://www.kaggle.com/code/serkanpeldek/keras-cnn-transfer-learnings-on-cats-dogs-dataset","metadata":{"execution":{"iopub.status.busy":"2022-05-25T09:16:40.129190Z","iopub.execute_input":"2022-05-25T09:16:40.129506Z","iopub.status.idle":"2022-05-25T09:16:40.133901Z","shell.execute_reply.started":"2022-05-25T09:16:40.129475Z","shell.execute_reply":"2022-05-25T09:16:40.132990Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd \nimport numpy as np \nimport tensorflow as tf\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense, Dropout, Flatten, Activation, Conv2D, MaxPooling2D\nimport matplotlib.pyplot as plt\nimport cv2  #opencv, to resize and edit the images\nimport os   #for directory\nimport zipfile #for working with zipfiles\n\n%matplotlib inline","metadata":{"execution":{"iopub.status.busy":"2022-05-25T09:16:40.415797Z","iopub.execute_input":"2022-05-25T09:16:40.416371Z","iopub.status.idle":"2022-05-25T09:16:47.512127Z","shell.execute_reply.started":"2022-05-25T09:16:40.416335Z","shell.execute_reply":"2022-05-25T09:16:47.511101Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(os.listdir(\"../input\"))","metadata":{"execution":{"iopub.status.busy":"2022-05-25T09:16:47.513735Z","iopub.execute_input":"2022-05-25T09:16:47.513973Z","iopub.status.idle":"2022-05-25T09:16:47.520390Z","shell.execute_reply.started":"2022-05-25T09:16:47.513945Z","shell.execute_reply":"2022-05-25T09:16:47.519741Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with zipfile.ZipFile(\"../input/dogs-vs-cats/train.zip\",\"r\") as z:\n    z.extractall(\".\")\nwith zipfile.ZipFile(\"../input/dogs-vs-cats/test1.zip\",\"r\") as z:\n    z.extractall(\".\")","metadata":{"execution":{"iopub.status.busy":"2022-05-25T09:16:47.521405Z","iopub.execute_input":"2022-05-25T09:16:47.521741Z","iopub.status.idle":"2022-05-25T09:17:07.332326Z","shell.execute_reply.started":"2022-05-25T09:16:47.521704Z","shell.execute_reply":"2022-05-25T09:17:07.331572Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# print(os.listdir(\"/kaggle/working/train\"))","metadata":{"execution":{"iopub.status.busy":"2022-05-25T09:17:07.333986Z","iopub.execute_input":"2022-05-25T09:17:07.334384Z","iopub.status.idle":"2022-05-25T09:17:07.338956Z","shell.execute_reply.started":"2022-05-25T09:17:07.334335Z","shell.execute_reply":"2022-05-25T09:17:07.338037Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Let's look at one of the images\nmain_dir = \"/kaggle/working/\"\ntrain_dir = \"train\"\npath = os.path.join(main_dir,train_dir)\n\nfor p in os.listdir(path):\n    category = p.split(\".\")[0]  # data is given as dog.230, cat.145 etc so we split it and choose the first word. Like cat or dog \n    img_array = cv2.imread(os.path.join(path,p), cv2.IMREAD_GRAYSCALE)  #spefcify the image then read it as array in grayscale \n    new_img_array = cv2.resize(img_array, dsize = (80,80))  #resize the image \n    plt.imshow(new_img_array,cmap=\"gray\") \n    break","metadata":{"execution":{"iopub.status.busy":"2022-05-25T09:17:07.340611Z","iopub.execute_input":"2022-05-25T09:17:07.341026Z","iopub.status.idle":"2022-05-25T09:17:07.651523Z","shell.execute_reply.started":"2022-05-25T09:17:07.340996Z","shell.execute_reply":"2022-05-25T09:17:07.650794Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Now we need to split our given training set into the way we want,\n# X for the resized gray images and y for the label (1 for Dogs and 0 for cats)","metadata":{"execution":{"iopub.status.busy":"2022-05-25T09:17:07.652790Z","iopub.execute_input":"2022-05-25T09:17:07.653177Z","iopub.status.idle":"2022-05-25T09:17:07.656491Z","shell.execute_reply.started":"2022-05-25T09:17:07.653146Z","shell.execute_reply":"2022-05-25T09:17:07.655309Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = []\ny = []\nconvert = lambda x : int(x == \"dog\")\n\ndef create_dataset(path):\n    for p in os.listdir(path):\n        category = p.split(\".\")[0]\n        category = convert(category)\n        img_arr = cv2.imread(os.path.join(path,p),cv2.IMREAD_GRAYSCALE)\n        new_img_arr = cv2.resize(img_arr, dsize = (80,80))\n        X.append(new_img_arr)\n        y.append(category)","metadata":{"execution":{"iopub.status.busy":"2022-05-25T09:17:07.657851Z","iopub.execute_input":"2022-05-25T09:17:07.658174Z","iopub.status.idle":"2022-05-25T09:17:07.670491Z","shell.execute_reply.started":"2022-05-25T09:17:07.658146Z","shell.execute_reply":"2022-05-25T09:17:07.669763Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# for cnt,ele in enumerate(X):\n#     plt.imshow(ele)\n#     print(y[cnt])\n#     if cnt > 5:\n#         break\n\n# better ways to look at the dataset have been explored further on","metadata":{"execution":{"iopub.status.busy":"2022-05-25T09:17:07.671468Z","iopub.execute_input":"2022-05-25T09:17:07.672399Z","iopub.status.idle":"2022-05-25T09:17:07.683525Z","shell.execute_reply.started":"2022-05-25T09:17:07.672366Z","shell.execute_reply":"2022-05-25T09:17:07.682882Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"create_dataset(path)\nX = np.array(X).reshape(-1,80,80,1)  #for the time being i do not know why we are reshaping here\ny = np.array(y)","metadata":{"execution":{"iopub.status.busy":"2022-05-25T09:17:07.685186Z","iopub.execute_input":"2022-05-25T09:17:07.685768Z","iopub.status.idle":"2022-05-25T09:17:37.353962Z","shell.execute_reply.started":"2022-05-25T09:17:07.685714Z","shell.execute_reply":"2022-05-25T09:17:37.352643Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# normalize the data \nX = X/255.0 #the grayscale value range from 0 to 255","metadata":{"execution":{"iopub.status.busy":"2022-05-25T09:17:37.358276Z","iopub.execute_input":"2022-05-25T09:17:37.358674Z","iopub.status.idle":"2022-05-25T09:17:37.945064Z","shell.execute_reply.started":"2022-05-25T09:17:37.358622Z","shell.execute_reply":"2022-05-25T09:17:37.944118Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# we can generate more images like this to train on them\n\n# train_data_gen = keras.preprocessing.image.ImageDataGenerator(\n#     rotation_range=15,\n#     rescale=1./255,\n#     shear_range=0.1,\n#     zoom_range=0.2,\n#     horizontal_flip=True,\n#     width_shift_range=0.1,\n#     height_shift_range=0.1\n# )\n\n# train_generator = train_data_gen.flow_from_dataframe(\n#     train_data, \n#     \"/kaggle/working/train/\", \n#     x_col='filename',\n#     y_col='category',\n#     target_size=(128,128),\n#     class_mode='categorical',\n#     batch_size=15\n# )","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Let's build a rudimentary model first \nmodel = Sequential()\n\nmodel.add(Conv2D(64,(3,3), activation = 'relu', input_shape = X.shape[1:]))\nmodel.add(MaxPooling2D(pool_size = (2,2)))\n\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\nmodel.add(Dense(1, activation = 'sigmoid'))\n\nmodel.compile(optimizer='adam',\n              loss = 'binary_crossentropy',\n              metrics=['accuracy']\n)","metadata":{"execution":{"iopub.status.busy":"2022-05-25T09:17:37.946652Z","iopub.execute_input":"2022-05-25T09:17:37.946977Z","iopub.status.idle":"2022-05-25T09:17:38.173108Z","shell.execute_reply.started":"2022-05-25T09:17:37.946937Z","shell.execute_reply":"2022-05-25T09:17:38.172105Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import keras \ntf.keras.utils.plot_model(model, 'model.png')","metadata":{"execution":{"iopub.status.busy":"2022-05-25T09:19:09.295618Z","iopub.execute_input":"2022-05-25T09:19:09.295929Z","iopub.status.idle":"2022-05-25T09:19:10.486106Z","shell.execute_reply.started":"2022-05-25T09:19:09.295898Z","shell.execute_reply":"2022-05-25T09:19:10.484826Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2022-05-25T09:19:14.064784Z","iopub.execute_input":"2022-05-25T09:19:14.065616Z","iopub.status.idle":"2022-05-25T09:19:14.077130Z","shell.execute_reply.started":"2022-05-25T09:19:14.065562Z","shell.execute_reply":"2022-05-25T09:19:14.076110Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(X,y,epochs=10, batch_size = 32, validation_split=0.2)","metadata":{"execution":{"iopub.status.busy":"2022-05-25T07:23:42.591026Z","iopub.execute_input":"2022-05-25T07:23:42.59131Z","iopub.status.idle":"2022-05-25T07:24:26.603905Z","shell.execute_reply.started":"2022-05-25T07:23:42.59128Z","shell.execute_reply":"2022-05-25T07:24:26.603234Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dir = \"test1\"\npath = os.path.join(main_dir,test_dir)\n\nX_test = []\nid_line = []\n\ndef create_test_dataset(path):\n    for p in os.listdir(path):\n        id_line.append(p.split(\".\")[0])\n        img_arr = cv2.imread(os.path.join(path,p),cv2.IMREAD_GRAYSCALE)\n        new_img_arr = cv2.resize(img_arr, dsize = (80,80))\n        X_test.append(new_img_arr)\ncreate_test_dataset(path)\nX_test = np.array(X_test).reshape(-1,80,80,1)\nX_test = X_test/255.0\n    ","metadata":{"execution":{"iopub.status.busy":"2022-05-25T07:30:29.437634Z","iopub.execute_input":"2022-05-25T07:30:29.438111Z","iopub.status.idle":"2022-05-25T07:30:40.787026Z","shell.execute_reply.started":"2022-05-25T07:30:29.438073Z","shell.execute_reply":"2022-05-25T07:30:40.786208Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = model.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2022-05-25T07:31:11.747071Z","iopub.execute_input":"2022-05-25T07:31:11.747526Z","iopub.status.idle":"2022-05-25T07:31:13.184268Z","shell.execute_reply.started":"2022-05-25T07:31:11.747484Z","shell.execute_reply":"2022-05-25T07:31:13.183494Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predicted_val = [int(round(p[0])) for p in predictions] # because sigoid gives probability","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Ways to improve \n# 1. Make network architecture more complex by using batchnorm and dropouts \n# 2. Generate images for training set\n# 3. Use transfer learning \n# also print the test accuracy and plot it and all\n# also show predicted value and image corresponding to it","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df = pd.DataFrame({'id':id_line, 'label':predicted_val})\nsubmission_df.to_csv(\"submission.csv\", index=False)","metadata":{},"execution_count":null,"outputs":[]}]}