{"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-02-03T08:23:18.658824Z","iopub.execute_input":"2022-02-03T08:23:18.659662Z","iopub.status.idle":"2022-02-03T08:23:18.673788Z","shell.execute_reply.started":"2022-02-03T08:23:18.659572Z","shell.execute_reply":"2022-02-03T08:23:18.673106Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Importing required libraries","metadata":{}},{"cell_type":"code","source":"import zipfile\nimport os\nimport pandas as pd\nimport tensorflow as tf\nimport cv2\nimport numpy as np\nfrom sklearn.model_selection import train_test_split\nfrom tensorflow.keras.models import Sequential, Model\nfrom tensorflow.keras.layers import Dense, Conv2D, Dropout, Flatten, MaxPooling2D\nimport warnings\nwarnings.filterwarnings('ignore')\npd.set_option(\"display.max_columns\", None)\nimport keras_tuner as kt","metadata":{"execution":{"iopub.status.busy":"2022-02-03T08:35:02.72652Z","iopub.execute_input":"2022-02-03T08:35:02.728208Z","iopub.status.idle":"2022-02-03T08:35:11.487219Z","shell.execute_reply.started":"2022-02-03T08:35:02.728141Z","shell.execute_reply":"2022-02-03T08:35:11.486013Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Extracting input files","metadata":{}},{"cell_type":"code","source":"def extract_zip_file(file_path):\n    with zipfile.ZipFile(file_path, 'r') as zip_ref:\n        zip_ref.extractall(\".\")","metadata":{"execution":{"iopub.status.busy":"2022-02-03T08:35:11.489109Z","iopub.execute_input":"2022-02-03T08:35:11.489364Z","iopub.status.idle":"2022-02-03T08:35:11.496797Z","shell.execute_reply.started":"2022-02-03T08:35:11.489334Z","shell.execute_reply":"2022-02-03T08:35:11.495784Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"extract_zip_file(\"../input/dogs-vs-cats-redux-kernels-edition/test.zip\")\nextract_zip_file(\"../input/dogs-vs-cats-redux-kernels-edition/train.zip\")","metadata":{"execution":{"iopub.status.busy":"2022-02-03T08:35:11.498657Z","iopub.execute_input":"2022-02-03T08:35:11.499098Z","iopub.status.idle":"2022-02-03T08:35:33.325576Z","shell.execute_reply.started":"2022-02-03T08:35:11.499039Z","shell.execute_reply":"2022-02-03T08:35:33.324522Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Constructing Train Dataframe","metadata":{}},{"cell_type":"code","source":"def construct_train_df():\n    image_list = []\n    for dirname, _, filenames in os.walk(\"./train\"):\n        for filename in filenames:\n            is_dog = 1 if \"dog\" in filename else 0\n            image_list.append({\"file_path\": f'./train/{filename}', 'is_dog': is_dog})\n    return pd.DataFrame(image_list)","metadata":{"execution":{"iopub.status.busy":"2022-02-03T08:35:33.331728Z","iopub.execute_input":"2022-02-03T08:35:33.332531Z","iopub.status.idle":"2022-02-03T08:35:33.342454Z","shell.execute_reply.started":"2022-02-03T08:35:33.332455Z","shell.execute_reply":"2022-02-03T08:35:33.340885Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = construct_train_df()","metadata":{"execution":{"iopub.status.busy":"2022-02-03T08:35:33.343889Z","iopub.execute_input":"2022-02-03T08:35:33.344275Z","iopub.status.idle":"2022-02-03T08:35:33.463698Z","shell.execute_reply.started":"2022-02-03T08:35:33.344221Z","shell.execute_reply":"2022-02-03T08:35:33.460254Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Constructing train and test data","metadata":{}},{"cell_type":"code","source":"x, y = [], []\nfor index, row in train_df.iterrows():\n    image = cv2.imread(row['file_path'])\n    image = cv2.resize(image,(64,64))\n    image = image / 255\n    x.append(image)\n    y.append(row['is_dog'])","metadata":{"execution":{"iopub.status.busy":"2022-02-03T08:37:56.510693Z","iopub.execute_input":"2022-02-03T08:37:56.511088Z","iopub.status.idle":"2022-02-03T08:38:57.859692Z","shell.execute_reply.started":"2022-02-03T08:37:56.511031Z","shell.execute_reply":"2022-02-03T08:38:57.858434Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Transforming to numpy array so that tensorflow could work on it","metadata":{}},{"cell_type":"code","source":"x, y  = np.array(x),np.array(y)","metadata":{"execution":{"iopub.status.busy":"2022-02-03T08:38:57.861697Z","iopub.execute_input":"2022-02-03T08:38:57.861993Z","iopub.status.idle":"2022-02-03T08:38:59.342372Z","shell.execute_reply.started":"2022-02-03T08:38:57.861931Z","shell.execute_reply":"2022-02-03T08:38:59.34118Z"},"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, random_state=1)","metadata":{"execution":{"iopub.status.busy":"2022-02-03T08:38:59.34416Z","iopub.execute_input":"2022-02-03T08:38:59.34497Z","iopub.status.idle":"2022-02-03T08:39:00.347097Z","shell.execute_reply.started":"2022-02-03T08:38:59.344908Z","shell.execute_reply":"2022-02-03T08:39:00.34634Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Building a deep learning model","metadata":{}},{"cell_type":"code","source":"model = Sequential()\n# CNN\nmodel.add(Conv2D(input_shape=(64, 64, 3), activation='relu', kernel_initializer='he_uniform', kernel_size=(6, 6), filters=12))\nmodel.add(MaxPooling2D(4, 4))\nmodel.add(Conv2D(filters=10, kernel_size=(3,3), activation='relu', kernel_initializer='he_uniform'))\nmodel.add(MaxPooling2D(2,2))\n# ANN\nmodel.add(Flatten())\nmodel.add(Dense(12, activation='relu', kernel_initializer='he_uniform'))\nmodel.add(Dense(1, activation='sigmoid', kernel_initializer='glorot_uniform'))\n\nmodel.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2022-02-03T08:39:00.34862Z","iopub.execute_input":"2022-02-03T08:39:00.349228Z","iopub.status.idle":"2022-02-03T08:39:00.551256Z","shell.execute_reply.started":"2022-02-03T08:39:00.349183Z","shell.execute_reply":"2022-02-03T08:39:00.549058Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Hyper parameter tuning","metadata":{}},{"cell_type":"code","source":"def build_model(hp):\n    inputs = tf.keras.Input(shape=(64, 64, 3))\n    x = inputs\n    for i in range(hp.Int(\"cnn_layers\", 1, 3)):\n        x = Conv2D(hp.Int(f\"filters_{i}\", 32, 128, step=32), kernel_size=(3,3), activation='relu')(x)\n        x = MaxPooling2D(pool_size=(2,2))(x)\n    x = Flatten()(x)\n    for i in range(hp.Int(\"mlp_layers\", 1, 5, 1)):\n        x = Dense(units=hp.Int(f'units_{i}', 32, 128, step=3), activation='relu')(x)\n    if hp.Boolean(\"dropout\"):\n        x = Dropout(0.5)(x)\n    outputs = Dense(units=1, activation='sigmoid')(x)\n    model = Model(inputs=inputs, outputs=outputs)\n    model.compile(loss='binary_crossentropy', metrics=['accuracy'], optimizer='adam')\n    return model","metadata":{"execution":{"iopub.status.busy":"2022-02-03T08:43:30.705647Z","iopub.execute_input":"2022-02-03T08:43:30.705992Z","iopub.status.idle":"2022-02-03T08:43:30.716565Z","shell.execute_reply.started":"2022-02-03T08:43:30.705935Z","shell.execute_reply":"2022-02-03T08:43:30.715249Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# hp = kt.HyperParameters()\n# hp.values[\"model_type\"] = \"cnn\"\n# model = build_model(hp)\n# model(x_train[:12])\n# model.summary()\n\n# hp.values[\"model_type\"] = \"mlp\"\n# model = build_model(hp)\n# model(x_train[:12])\n# model.summary()","metadata":{"execution":{"iopub.status.busy":"2022-02-03T08:43:31.865626Z","iopub.execute_input":"2022-02-03T08:43:31.866249Z","iopub.status.idle":"2022-02-03T08:43:31.870675Z","shell.execute_reply.started":"2022-02-03T08:43:31.866198Z","shell.execute_reply":"2022-02-03T08:43:31.869812Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tuner = kt.RandomSearch(build_model, max_trials=10, overwrite=True, objective='val_accuracy', directory=\"./tuning\")","metadata":{"execution":{"iopub.status.busy":"2022-02-03T08:43:32.910468Z","iopub.execute_input":"2022-02-03T08:43:32.911159Z","iopub.status.idle":"2022-02-03T08:43:32.967839Z","shell.execute_reply.started":"2022-02-03T08:43:32.911118Z","shell.execute_reply":"2022-02-03T08:43:32.966788Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tuner.search(x_train, y_train, validation_split=0.2, epochs=10, callbacks=[tf.keras.callbacks.TensorBoard(\"./tensorboard\")])","metadata":{"execution":{"iopub.status.busy":"2022-02-03T08:43:35.589539Z","iopub.execute_input":"2022-02-03T08:43:35.589857Z","iopub.status.idle":"2022-02-03T08:43:56.101038Z","shell.execute_reply.started":"2022-02-03T08:43:35.589823Z","shell.execute_reply":"2022-02-03T08:43:56.10003Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = tuner.get_best_models()[0]","metadata":{"execution":{"iopub.status.busy":"2022-02-03T08:43:56.10347Z","iopub.execute_input":"2022-02-03T08:43:56.103808Z","iopub.status.idle":"2022-02-03T08:43:56.486201Z","shell.execute_reply.started":"2022-02-03T08:43:56.103764Z","shell.execute_reply":"2022-02-03T08:43:56.484924Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2022-02-03T08:43:56.487926Z","iopub.execute_input":"2022-02-03T08:43:56.488312Z","iopub.status.idle":"2022-02-03T08:43:56.499358Z","shell.execute_reply.started":"2022-02-03T08:43:56.488265Z","shell.execute_reply":"2022-02-03T08:43:56.498615Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Fitting the model","metadata":{}},{"cell_type":"code","source":"history = model.fit(x_train, y_train, validation_data=(x_test, y_test), epochs=5)","metadata":{"execution":{"iopub.status.busy":"2022-02-03T07:02:03.970463Z","iopub.execute_input":"2022-02-03T07:02:03.971466Z","iopub.status.idle":"2022-02-03T07:15:20.824685Z","shell.execute_reply.started":"2022-02-03T07:02:03.97142Z","shell.execute_reply":"2022-02-03T07:15:20.823616Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Constructing test dataframe","metadata":{}},{"cell_type":"code","source":"def construct_test_df():\n    x = []\n    for dirname, _, filenames in os.walk(\"./test\"):\n        for filename in filenames:\n            x.append(f'./test/{filename}')\n    return pd.DataFrame({'file_path': x})","metadata":{"execution":{"iopub.status.busy":"2022-02-03T07:15:20.826713Z","iopub.execute_input":"2022-02-03T07:15:20.827087Z","iopub.status.idle":"2022-02-03T07:15:20.832013Z","shell.execute_reply.started":"2022-02-03T07:15:20.827044Z","shell.execute_reply":"2022-02-03T07:15:20.831346Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df = construct_test_df()","metadata":{"execution":{"iopub.status.busy":"2022-02-03T07:15:20.832937Z","iopub.execute_input":"2022-02-03T07:15:20.833348Z","iopub.status.idle":"2022-02-03T07:15:20.867251Z","shell.execute_reply.started":"2022-02-03T07:15:20.833302Z","shell.execute_reply":"2022-02-03T07:15:20.866352Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_images = []\nfor index, row in test_df.iterrows():\n    image = cv2.imread(row['file_path'])\n    image = cv2.resize(image, (64, 64))\n    image = image / 255\n    test_images.append(image)","metadata":{"execution":{"iopub.status.busy":"2022-02-03T07:15:20.869463Z","iopub.execute_input":"2022-02-03T07:15:20.869849Z","iopub.status.idle":"2022-02-03T07:16:04.987111Z","shell.execute_reply.started":"2022-02-03T07:15:20.869802Z","shell.execute_reply":"2022-02-03T07:16:04.985753Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_images = np.array(test_images)","metadata":{"execution":{"iopub.status.busy":"2022-02-03T07:16:04.988555Z","iopub.execute_input":"2022-02-03T07:16:04.989148Z","iopub.status.idle":"2022-02-03T07:16:05.481936Z","shell.execute_reply.started":"2022-02-03T07:16:04.989096Z","shell.execute_reply":"2022-02-03T07:16:05.480945Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Predicting the test data","metadata":{}},{"cell_type":"code","source":"y_pred = model.predict(test_images)","metadata":{"execution":{"iopub.status.busy":"2022-02-03T07:16:05.483342Z","iopub.execute_input":"2022-02-03T07:16:05.483714Z","iopub.status.idle":"2022-02-03T07:16:32.260334Z","shell.execute_reply.started":"2022-02-03T07:16:05.483668Z","shell.execute_reply":"2022-02-03T07:16:32.259615Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred.shape","metadata":{"execution":{"iopub.status.busy":"2022-02-03T07:16:32.261936Z","iopub.execute_input":"2022-02-03T07:16:32.262441Z","iopub.status.idle":"2022-02-03T07:16:32.269915Z","shell.execute_reply.started":"2022-02-03T07:16:32.262391Z","shell.execute_reply":"2022-02-03T07:16:32.269015Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dog = y_pred.reshape(-1)","metadata":{"execution":{"iopub.status.busy":"2022-02-03T07:16:32.271272Z","iopub.execute_input":"2022-02-03T07:16:32.272161Z","iopub.status.idle":"2022-02-03T07:16:32.284136Z","shell.execute_reply.started":"2022-02-03T07:16:32.272113Z","shell.execute_reply":"2022-02-03T07:16:32.283273Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Creating Submission Dataframe","metadata":{}},{"cell_type":"code","source":"submission_df = pd.DataFrame({'id':np.arange(1, len(dog)+1), 'label': (dog > 0.5).astype('int')})","metadata":{"execution":{"iopub.status.busy":"2022-02-03T07:16:32.285289Z","iopub.execute_input":"2022-02-03T07:16:32.285858Z","iopub.status.idle":"2022-02-03T07:16:32.299144Z","shell.execute_reply.started":"2022-02-03T07:16:32.285806Z","shell.execute_reply":"2022-02-03T07:16:32.298249Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df.to_csv(\"/kaggle/working/submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2022-02-03T07:16:32.303282Z","iopub.execute_input":"2022-02-03T07:16:32.303761Z","iopub.status.idle":"2022-02-03T07:16:32.331092Z","shell.execute_reply.started":"2022-02-03T07:16:32.303722Z","shell.execute_reply":"2022-02-03T07:16:32.330455Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}