{"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":"2023-07-25T18:28:27.666456Z","iopub.execute_input":"2023-07-25T18:28:27.666906Z","iopub.status.idle":"2023-07-25T18:28:27.682127Z","shell.execute_reply.started":"2023-07-25T18:28:27.666875Z","shell.execute_reply":"2023-07-25T18:28:27.680855Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport cv2\nimport tensorflow as tf\nimport random\nimport zipfile\nimport os\nfrom PIL import Image\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import classification_report","metadata":{"execution":{"iopub.status.busy":"2023-07-25T18:28:27.685323Z","iopub.execute_input":"2023-07-25T18:28:27.686326Z","iopub.status.idle":"2023-07-25T18:28:40.123461Z","shell.execute_reply.started":"2023-07-25T18:28:27.686294Z","shell.execute_reply":"2023-07-25T18:28:40.122381Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with zipfile.ZipFile('/kaggle/input/dogs-vs-cats-redux-kernels-edition/train.zip', 'r') as train_zip:\n    train_zip.extractall('.')  \nimage_dir = \"../working/train/\"\nfilenames = os.listdir(image_dir)\nlabels = [x.split(\".\")[0] for x in filenames]\ndata = pd.DataFrame({\"filename\": filenames, \"label\": labels})\ndata.head()","metadata":{"execution":{"iopub.status.busy":"2023-07-25T18:28:40.126495Z","iopub.execute_input":"2023-07-25T18:28:40.127814Z","iopub.status.idle":"2023-07-25T18:28:50.893431Z","shell.execute_reply.started":"2023-07-25T18:28:40.127764Z","shell.execute_reply":"2023-07-25T18:28:50.892002Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig,axes = plt.subplots(1,8, figsize = (20,20))\n\nfor i in range(8):\n    image_path = os.path.join(image_dir, data.iloc[i]['filename'])\n    image = cv2.imread(image_path)\n    image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n    axes[i].imshow(image_rgb)\n    axes[i].set_title('label :' + data.iloc[i]['label'])\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-07-25T18:28:50.894771Z","iopub.execute_input":"2023-07-25T18:28:50.895472Z","iopub.status.idle":"2023-07-25T18:28:52.471987Z","shell.execute_reply.started":"2023-07-25T18:28:50.895437Z","shell.execute_reply":"2023-07-25T18:28:52.471126Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data['label'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2023-07-25T18:28:52.474834Z","iopub.execute_input":"2023-07-25T18:28:52.475171Z","iopub.status.idle":"2023-07-25T18:28:52.494479Z","shell.execute_reply.started":"2023-07-25T18:28:52.475141Z","shell.execute_reply":"2023-07-25T18:28:52.493092Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_paths = [os.path.join(image_dir, filename) for filename in os.listdir(image_dir)]\n\nimage_width = []\nimage_height = []\nfor image_path in image_paths: \n    image = cv2.imread(image_path)\n    height,width,_ = image.shape\n    image_width.append(width)\n    image_height.append(height)\nmedian_width = np.median(image_width)\nmedian_height = np.median(image_height)\nprint(median_height, median_width)\n    ","metadata":{"execution":{"iopub.status.busy":"2023-07-25T18:28:52.497488Z","iopub.execute_input":"2023-07-25T18:28:52.497801Z","iopub.status.idle":"2023-07-25T18:29:38.721209Z","shell.execute_reply.started":"2023-07-25T18:28:52.497762Z","shell.execute_reply":"2023-07-25T18:29:38.720004Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label_encoding = {'cat':'0', 'dog':'1'}\ndata['label'] = data['label'].map(label_encoding)\ndata.head()","metadata":{"execution":{"iopub.status.busy":"2023-07-25T18:29:38.722934Z","iopub.execute_input":"2023-07-25T18:29:38.723373Z","iopub.status.idle":"2023-07-25T18:29:38.743871Z","shell.execute_reply.started":"2023-07-25T18:29:38.723337Z","shell.execute_reply":"2023-07-25T18:29:38.742603Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels = data['label']\nX_train,X_temp = train_test_split(data, test_size = 0.2, random_state = 42, stratify = labels)\n\ntemp_labels = X_temp['label']\nX_test, X_val =train_test_split(X_temp, test_size = 0.5, random_state = 42, stratify = temp_labels)","metadata":{"execution":{"iopub.status.busy":"2023-07-25T18:29:38.745492Z","iopub.execute_input":"2023-07-25T18:29:38.746101Z","iopub.status.idle":"2023-07-25T18:29:38.795076Z","shell.execute_reply.started":"2023-07-25T18:29:38.746049Z","shell.execute_reply":"2023-07-25T18:29:38.794234Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.shape","metadata":{"execution":{"iopub.status.busy":"2023-07-25T18:29:38.796255Z","iopub.execute_input":"2023-07-25T18:29:38.796801Z","iopub.status.idle":"2023-07-25T18:29:38.804511Z","shell.execute_reply.started":"2023-07-25T18:29:38.796769Z","shell.execute_reply":"2023-07-25T18:29:38.803555Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test.shape","metadata":{"execution":{"iopub.status.busy":"2023-07-25T18:29:38.806490Z","iopub.execute_input":"2023-07-25T18:29:38.807275Z","iopub.status.idle":"2023-07-25T18:29:38.817749Z","shell.execute_reply.started":"2023-07-25T18:29:38.807243Z","shell.execute_reply":"2023-07-25T18:29:38.816491Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_val.shape","metadata":{"execution":{"iopub.status.busy":"2023-07-25T18:29:38.823139Z","iopub.execute_input":"2023-07-25T18:29:38.823495Z","iopub.status.idle":"2023-07-25T18:29:38.831857Z","shell.execute_reply.started":"2023-07-25T18:29:38.823455Z","shell.execute_reply":"2023-07-25T18:29:38.830860Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_size = 64\nimage_size = (370,370)\nidg = tf.keras.preprocessing.image.ImageDataGenerator(preprocessing_function = tf.keras.applications.vgg16.preprocess_input)","metadata":{"execution":{"iopub.status.busy":"2023-07-25T18:29:38.833817Z","iopub.execute_input":"2023-07-25T18:29:38.834265Z","iopub.status.idle":"2023-07-25T18:29:38.843917Z","shell.execute_reply.started":"2023-07-25T18:29:38.834230Z","shell.execute_reply":"2023-07-25T18:29:38.842708Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_idg = idg.flow_from_dataframe(X_train, directory = image_dir, x_col = 'filename', y_col = 'label' ,target_size = image_size,batch_size = batch_size, class_mode ='binary')","metadata":{"execution":{"iopub.status.busy":"2023-07-25T18:29:38.845550Z","iopub.execute_input":"2023-07-25T18:29:38.845920Z","iopub.status.idle":"2023-07-25T18:29:39.094999Z","shell.execute_reply.started":"2023-07-25T18:29:38.845888Z","shell.execute_reply":"2023-07-25T18:29:39.094041Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_idg = idg.flow_from_dataframe(X_val, directory = image_dir, x_col = 'filename', y_col = 'label',target_size = image_size,batch_size = batch_size, shuffle = False, class_mode ='binary')","metadata":{"execution":{"iopub.status.busy":"2023-07-25T18:29:39.096217Z","iopub.execute_input":"2023-07-25T18:29:39.096790Z","iopub.status.idle":"2023-07-25T18:29:39.136699Z","shell.execute_reply.started":"2023-07-25T18:29:39.096756Z","shell.execute_reply":"2023-07-25T18:29:39.135851Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_idg = idg.flow_from_dataframe(X_test, directory = image_dir, x_col = 'filename', y_col = 'label',target_size = image_size,batch_size = batch_size, shuffle = False, class_mode ='binary')","metadata":{"execution":{"iopub.status.busy":"2023-07-25T18:29:39.137923Z","iopub.execute_input":"2023-07-25T18:29:39.138363Z","iopub.status.idle":"2023-07-25T18:29:39.180479Z","shell.execute_reply.started":"2023-07-25T18:29:39.138329Z","shell.execute_reply":"2023-07-25T18:29:39.179607Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vgg16_model = tf.keras.applications.vgg16.VGG16(include_top=False, input_shape=(370, 370, 3))","metadata":{"execution":{"iopub.status.busy":"2023-07-25T18:29:39.181694Z","iopub.execute_input":"2023-07-25T18:29:39.182844Z","iopub.status.idle":"2023-07-25T18:29:44.705752Z","shell.execute_reply.started":"2023-07-25T18:29:39.182809Z","shell.execute_reply":"2023-07-25T18:29:44.704666Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for layer in vgg16_model.layers:\n  layer.trainable = False","metadata":{"execution":{"iopub.status.busy":"2023-07-25T18:29:44.707147Z","iopub.execute_input":"2023-07-25T18:29:44.707569Z","iopub.status.idle":"2023-07-25T18:29:44.714042Z","shell.execute_reply.started":"2023-07-25T18:29:44.707531Z","shell.execute_reply":"2023-07-25T18:29:44.712983Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"flat = tf.keras.layers.Flatten() (vgg16_model.output)\ndropout1 = tf.keras.layers.Dropout(0.2, name=\"Dropout1\") (flat)\ndense1 = tf.keras.layers.Dense(128, activation=\"relu\") (dropout1)\ndropout2 = tf.keras.layers.Dropout(0.2, name=\"Dropout2\")(dense1)\noutput = tf.keras.layers.Dense(1, activation=\"sigmoid\") (dropout2)\n\nfinal_model = tf.keras.models.Model(inputs=[vgg16_model.input], outputs=[output])","metadata":{"execution":{"iopub.status.busy":"2023-07-25T18:29:44.715701Z","iopub.execute_input":"2023-07-25T18:29:44.716385Z","iopub.status.idle":"2023-07-25T18:29:44.769889Z","shell.execute_reply.started":"2023-07-25T18:29:44.716335Z","shell.execute_reply":"2023-07-25T18:29:44.769036Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_model.summary()","metadata":{"execution":{"iopub.status.busy":"2023-07-25T18:29:44.771132Z","iopub.execute_input":"2023-07-25T18:29:44.771438Z","iopub.status.idle":"2023-07-25T18:29:44.823764Z","shell.execute_reply.started":"2023-07-25T18:29:44.771413Z","shell.execute_reply":"2023-07-25T18:29:44.822996Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_model.compile(optimizer='adam',\n                    loss=tf.keras.losses.binary_crossentropy,\n                    metrics = [\"acc\"])","metadata":{"execution":{"iopub.status.busy":"2023-07-25T18:29:44.824737Z","iopub.execute_input":"2023-07-25T18:29:44.825376Z","iopub.status.idle":"2023-07-25T18:29:44.858891Z","shell.execute_reply.started":"2023-07-25T18:29:44.825343Z","shell.execute_reply":"2023-07-25T18:29:44.857936Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_ckpt = tf.keras.callbacks.ModelCheckpoint(\"DogCat\",\n                                                monitor=\"val_loss\",\n                                                save_best_only=True)","metadata":{"execution":{"iopub.status.busy":"2023-07-25T18:29:44.860257Z","iopub.execute_input":"2023-07-25T18:29:44.860769Z","iopub.status.idle":"2023-07-25T18:29:44.866046Z","shell.execute_reply.started":"2023-07-25T18:29:44.860737Z","shell.execute_reply":"2023-07-25T18:29:44.865138Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = final_model.fit(train_idg, batch_size=batch_size, validation_data=val_idg, epochs = 3, callbacks=[model_ckpt])","metadata":{"execution":{"iopub.status.busy":"2023-07-25T18:29:44.867378Z","iopub.execute_input":"2023-07-25T18:29:44.868470Z","iopub.status.idle":"2023-07-25T18:45:55.108105Z","shell.execute_reply.started":"2023-07-25T18:29:44.868438Z","shell.execute_reply":"2023-07-25T18:45:55.107119Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with zipfile.ZipFile('/kaggle/input/dogs-vs-cats-redux-kernels-edition/test.zip', 'r') as test_zip:\n    test_zip.extractall('.')  \ntest_dir = \"../working/test/\"\nfilenames = os.listdir(test_dir)\ntest_data = pd.DataFrame({\"filename\": filenames})\ntest_data['label'] = 'unknown'\ntest_data.head()","metadata":{"execution":{"iopub.status.busy":"2023-07-25T18:45:55.109648Z","iopub.execute_input":"2023-07-25T18:45:55.110022Z","iopub.status.idle":"2023-07-25T18:46:00.536115Z","shell.execute_reply.started":"2023-07-25T18:45:55.109985Z","shell.execute_reply":"2023-07-25T18:46:00.535116Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_idg =  idg.flow_from_dataframe(test_data, \"test/\", x_col= \"filename\",y_col = 'label',\n                                    batch_size = batch_size,\n                                    target_size=image_size, shuffle = False)","metadata":{"execution":{"iopub.status.busy":"2023-07-25T18:46:00.537579Z","iopub.execute_input":"2023-07-25T18:46:00.537991Z","iopub.status.idle":"2023-07-25T18:46:00.690342Z","shell.execute_reply.started":"2023-07-25T18:46:00.537955Z","shell.execute_reply":"2023-07-25T18:46:00.689462Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cat_dog_model = tf.keras.models.load_model(\"DogCat\")\ntest_predict = cat_dog_model.predict(test_idg)\n\n# test_predict_argmax = np.argmax(test_predict, axis=1)\n\n# y_test_pred = test_predict_argmax\n\n# test_data['label'] = y_test_pred\ntest_data['dog_prob'] = test_predict","metadata":{"execution":{"iopub.status.busy":"2023-07-25T18:46:00.691700Z","iopub.execute_input":"2023-07-25T18:46:00.692044Z","iopub.status.idle":"2023-07-25T18:48:48.077648Z","shell.execute_reply.started":"2023-07-25T18:46:00.692012Z","shell.execute_reply":"2023-07-25T18:48:48.076550Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, axes = plt.subplots(1, 5, figsize=(20, 4))\nfor idx in range(5):\n    image_path = os.path.join(test_dir, test_data.iloc[idx]['filename'])\n    image = Image.open(image_path)\n    axes[idx].imshow(image)\n    axes[idx].set_title(\"Label: \" + test_data.iloc[idx]['label'])\n    axes[idx].axis('off')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-07-25T18:48:48.079134Z","iopub.execute_input":"2023-07-25T18:48:48.079479Z","iopub.status.idle":"2023-07-25T18:48:48.787663Z","shell.execute_reply.started":"2023-07-25T18:48:48.079445Z","shell.execute_reply":"2023-07-25T18:48:48.785108Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data['filename'] =test_data['filename'].str.split('.').str[0]\n\ntest_data.head()","metadata":{"execution":{"iopub.status.busy":"2023-07-25T18:48:48.789059Z","iopub.execute_input":"2023-07-25T18:48:48.790022Z","iopub.status.idle":"2023-07-25T18:48:48.828608Z","shell.execute_reply.started":"2023-07-25T18:48:48.789987Z","shell.execute_reply":"2023-07-25T18:48:48.827245Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_file = pd.DataFrame(columns=['ID', 'label'])\nsubmission_file['ID']= test_data['filename']\nsubmission_file['label']= test_data['dog_prob']\nsubmission_file.to_csv('submission.csv', index=False)\n\n","metadata":{"execution":{"iopub.status.busy":"2023-07-25T18:48:48.830017Z","iopub.execute_input":"2023-07-25T18:48:48.830443Z","iopub.status.idle":"2023-07-25T18:48:48.886462Z","shell.execute_reply.started":"2023-07-25T18:48:48.830412Z","shell.execute_reply":"2023-07-25T18:48:48.885245Z"},"trusted":true},"execution_count":null,"outputs":[]}]}