{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"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 in \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 \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory\n\nimport os\n\n# Dataset path\nDataDir = \"../input/\"\n\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport os\nimport cv2","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9dab03b4c6272c933e10624e8b4d60b687af17ab"},"cell_type":"code","source":"train_dir = \"train/train\"\npath = os.path.join(DataDir, train_dir)\nfor p in os.listdir(path):\n    category = p.split(\".\")[0]\n    img_array = cv2.imread(os.path.join(path,p),cv2.IMREAD_GRAYSCALE)\n    plt.imshow(img_array,cmap=\"gray\")\n    print(category, img_array.shape)\n    break","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ee7b3fca8efca8fcbb19b6d429fe700718501196"},"cell_type":"code","source":"# Dog = 0, Cat = 1\nIMG_SIZ = 64\n\ntraining_data = []\n\nfor p in os.listdir(path):\n    category = p.split(\".\")[0]\n    try:\n        img_array = cv2.imread(os.path.join(path, p),cv2.IMREAD_GRAYSCALE)\n        new_array = cv2.resize(img_array, (IMG_SIZ, IMG_SIZ))\n        if category == 'dog':\n            training_data.append([new_array, 0])\n        else:\n            training_data.append([new_array, 1])\n    except Exception as e:\n        pass","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"db00e1a52b748f9929cecf02cdac8c2834b65c44"},"cell_type":"code","source":"print(training_data[0][0], training_data[0][1])\nplt.imshow(training_data[0][0], cmap=\"gray\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b2b78124db47d120196de888df448a78a4331df7"},"cell_type":"code","source":"print(len(training_data))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"69795beb36f0269a018c40b226f8dda5f7d165ee"},"cell_type":"code","source":"X = []\ny = []\n\nfor features, label in training_data:\n    X.append(features)\n    y.append(label)\n\nX = np.array(X).reshape(-1, IMG_SIZ, IMG_SIZ, 1)\nX = X / 255.0\n\nprint(X.shape[1:])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c9afc70e4b322a36409865bae673d7cefe34af52"},"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense, Dropout, Activation, Flatten, Conv2D, MaxPooling2D","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9bf0ae6eff079b77dd1714c4322a66f443b92880"},"cell_type":"code","source":"model = Sequential()\nmodel.add(Conv2D(64, (3, 3), activation = 'relu', input_shape = X.shape[1:]))\nmodel.add(Dropout(rate = 0.2))\nmodel.add(MaxPooling2D(2, 2))\n\nmodel.add(Conv2D(128, (3, 3), activation = 'relu'))\nmodel.add(Dropout(rate = 0.2))\nmodel.add(MaxPooling2D(2, 2))\n\nmodel.add(Conv2D(256, (3, 3), activation = 'relu'))\nmodel.add(Dropout(rate = 0.2))\nmodel.add(MaxPooling2D(2, 2))\n\nmodel.add(Flatten())\nmodel.add(Dense(256))\n\nmodel.add(Dense(1, activation = 'sigmoid'))\n\nmodel.compile(loss=\"binary_crossentropy\",\n             optimizer=\"adam\",\n             metrics=['accuracy'])\n\nmodel.fit(X, y, batch_size = 32, epochs = 30, validation_split = 0.1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f8a65d364345c53f1871c62c7708f9d889d74c35"},"cell_type":"code","source":"test_dir = \"test1/test1\"\npath = os.path.join(DataDir, test_dir)\n\ntest_data = []\nid = []\n\nfor p in os.listdir(path):\n    try:\n        id.append((p.split(\".\"))[0])\n        img_array = cv2.imread(os.path.join(path, p), cv2.IMREAD_GRAYSCALE)\n        new_array = cv2.resize(img_array, (IMG_SIZ, IMG_SIZ))\n        test_data.append(new_array)\n    except Exception as e:\n        pass","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a82e1054bd5b978b51df26f9f7b8ba95f027cc59"},"cell_type":"code","source":"for i in range(0, 5):\n    print(id[i])\n    plt.imshow(test_data[i], cmap=\"gray\")\n    plt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c1171b11b83e09caa3e3c310a6ae5b061c781354"},"cell_type":"code","source":"test_data = np.array(test_data).reshape(-1, IMG_SIZ, IMG_SIZ, 1)\ntest_data = test_data / 255.0","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4f92e77b6db341c1cfb307e1d8f3838c431ffabf"},"cell_type":"code","source":"y_pred = model.predict(test_data)\ny_pred = np.round(y_pred, decimals = 2)\npred_labels = [1 if value > 0.5 else 0 for value in y_pred]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4645884462bd766bf80ccdbfcf9d5a19ec9a24cd"},"cell_type":"code","source":"print(pred_labels)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ce1e7bfaf7368d294711c87ef08be4e8bd7e2731"},"cell_type":"code","source":"df_submission=pd.DataFrame({\"id\":id})\ndf_submission[\"label\"]=pred_labels\ndf_submission.info()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"30bdf35fea0425839b8453c568eeb5d1631167f8"},"cell_type":"code","source":"for i in range(0, len(id)):\n    print(id[i], pred_labels[i])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"54a710088a4239f1da63b10cbabf75b9e06134c3"},"cell_type":"code","source":"df_submission.to_csv(\"cnn_submission.csv\",index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"94d218b228379672e792fd1712be54601b934c23"},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}