{"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-10T05:38:29.785480Z","iopub.execute_input":"2023-07-10T05:38:29.786253Z","iopub.status.idle":"2023-07-10T05:38:29.820720Z","shell.execute_reply.started":"2023-07-10T05:38:29.786219Z","shell.execute_reply":"2023-07-10T05:38:29.819570Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport sys\nimport tensorflow as tf\nimport os\nimport pandas as pd\nfrom keras.models import Sequential\nfrom keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPooling2D , BatchNormalization\nfrom sklearn.model_selection import train_test_split\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\n\n%matplotlib inline\nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2023-07-10T05:39:28.706503Z","iopub.execute_input":"2023-07-10T05:39:28.707028Z","iopub.status.idle":"2023-07-10T05:39:46.004775Z","shell.execute_reply.started":"2023-07-10T05:39:28.706991Z","shell.execute_reply":"2023-07-10T05:39:46.003652Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import zipfile\n\nzip_paths = ['/kaggle/input/dogs-vs-cats-redux-kernels-edition/train.zip' , '/kaggle/input/dogs-vs-cats-redux-kernels-edition/test.zip']  # 압축 파일의 경로와 파일명\nextract_dir = './dogs-vs-cats/'  # 압축 해제할 폴더의 경로와 이름\n\nfor zip_path in zip_paths:\n  with zipfile.ZipFile(zip_path, 'r') as zip_ref:\n      zip_ref.extractall(extract_dir)","metadata":{"execution":{"iopub.status.busy":"2023-07-10T05:40:15.673586Z","iopub.execute_input":"2023-07-10T05:40:15.674275Z","iopub.status.idle":"2023-07-10T05:40:31.337228Z","shell.execute_reply.started":"2023-07-10T05:40:15.674221Z","shell.execute_reply":"2023-07-10T05:40:31.336106Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train 폴더 확인\ntrain_folder_path = './dogs-vs-cats/train'\n\ntrain_file_names = os.listdir(train_folder_path)\nprint(\"train file name\" , train_file_names[:5] )\nprint(\"train file numbers\" , len(train_file_names))","metadata":{"execution":{"iopub.status.busy":"2023-07-10T05:40:56.280968Z","iopub.execute_input":"2023-07-10T05:40:56.281336Z","iopub.status.idle":"2023-07-10T05:40:56.304786Z","shell.execute_reply.started":"2023-07-10T05:40:56.281306Z","shell.execute_reply":"2023-07-10T05:40:56.303800Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# test 폴더 확인\ntest_folder_path = './dogs-vs-cats/test'\n\ntest_file_names = os.listdir(test_folder_path)\nprint(\"test file name\" , test_file_names[:5] )\nprint(\"test file numbers\" , len(test_file_names))","metadata":{"execution":{"iopub.status.busy":"2023-07-10T05:41:11.944602Z","iopub.execute_input":"2023-07-10T05:41:11.944961Z","iopub.status.idle":"2023-07-10T05:41:11.959871Z","shell.execute_reply.started":"2023-07-10T05:41:11.944930Z","shell.execute_reply":"2023-07-10T05:41:11.958695Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Train dataframe\n\ntargets = []\nfull_paths = []\ntrain_cats_dir = []\ntrain_dogs_dir = []\n\n\n# Target setting\nfor file_name in train_file_names:\n    target = file_name.split(\".\")[0] # target name\n    full_path = os.path.join(train_folder_path, file_name)\n\n    if(target == \"dog\"):\n        train_dogs_dir.append(full_path)\n    if(target == \"cat\"):\n        train_cats_dir.append(full_path)\n\n    full_paths.append(full_path)\n    targets.append(target)\n\n\ndf_train = pd.DataFrame()\ndf_train['image_path'] = full_paths\ndf_train['target'] = targets","metadata":{"execution":{"iopub.status.busy":"2023-07-10T05:41:29.202568Z","iopub.execute_input":"2023-07-10T05:41:29.202930Z","iopub.status.idle":"2023-07-10T05:41:29.303589Z","shell.execute_reply.started":"2023-07-10T05:41:29.202900Z","shell.execute_reply":"2023-07-10T05:41:29.302649Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Test Data dataframe\nfull_paths = []\nfile_names = []\n\nfor file_name in test_file_names:\n    full_path = os.path.join(test_folder_path, file_name)\n\n    file_names.append(file_name)\n    full_paths.append(full_path)\n\n\ndf_test = pd.DataFrame()\ndf_test['image_path'] = full_paths\ndf_test['filename'] = file_names","metadata":{"execution":{"iopub.status.busy":"2023-07-10T05:41:46.193071Z","iopub.execute_input":"2023-07-10T05:41:46.194034Z","iopub.status.idle":"2023-07-10T05:41:46.236128Z","shell.execute_reply.started":"2023-07-10T05:41:46.193987Z","shell.execute_reply":"2023-07-10T05:41:46.235278Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data , val_data = train_test_split(df_train, test_size=0.2, random_state=123)","metadata":{"execution":{"iopub.status.busy":"2023-07-10T05:42:13.600442Z","iopub.execute_input":"2023-07-10T05:42:13.601329Z","iopub.status.idle":"2023-07-10T05:42:13.620225Z","shell.execute_reply.started":"2023-07-10T05:42:13.601285Z","shell.execute_reply":"2023-07-10T05:42:13.619279Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import seaborn as sns\nprint('학습 데이터 개수 ' , len(train_data))\nprint('검증 데이터 개수 ' , len(val_data))\n\nplt.suptitle('Train Class Count')\nsns.countplot(data = df_train , x='target')","metadata":{"execution":{"iopub.status.busy":"2023-07-10T05:42:28.734970Z","iopub.execute_input":"2023-07-10T05:42:28.735379Z","iopub.status.idle":"2023-07-10T05:42:29.371176Z","shell.execute_reply.started":"2023-07-10T05:42:28.735347Z","shell.execute_reply":"2023-07-10T05:42:29.370148Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 테스트 parameter Initialization\nbatch_size = 64\nepochs = 30\nIMG_HEIGHT = 256\nIMG_WIDTH = 256","metadata":{"execution":{"iopub.status.busy":"2023-07-10T05:45:40.943992Z","iopub.execute_input":"2023-07-10T05:45:40.944382Z","iopub.status.idle":"2023-07-10T05:45:40.949059Z","shell.execute_reply.started":"2023-07-10T05:45:40.944348Z","shell.execute_reply":"2023-07-10T05:45:40.948148Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.applications.resnet50 import preprocess_input as resnet50_preprocess_input\n\n# DataGenrator 를 사용해서 Train , Val , Test data 를 생성 하도록 한다.\nimage_gen_train = ImageDataGenerator(rescale=1./255,\n                                     rotation_range=10,\n                                     width_shift_range=0.1,\n                                     height_shift_range=0.1,\n                                     zoom_range=0.1,\n                                     horizontal_flip=True,\n                                     preprocessing_function = resnet50_preprocess_input)\n\ntrain_data_gen = image_gen_train.flow_from_dataframe(batch_size = batch_size,\n                                                     dataframe = train_data,\n                                                     x_col = 'image_path',\n                                                     y_col = 'target',\n                                                     shuffle=True,\n                                                     target_size=(IMG_HEIGHT, IMG_WIDTH),\n                                                     class_mode='binary')\n\nval_datagen = ImageDataGenerator(rescale=1./255)\n\nval_data_gen = val_datagen.flow_from_dataframe(batch_size = batch_size,\n                                                     dataframe = val_data,\n                                                     x_col = 'image_path',\n                                                     y_col = 'target',\n                                                     target_size=(IMG_HEIGHT, IMG_WIDTH),\n                                                     class_mode='binary')","metadata":{"execution":{"iopub.status.busy":"2023-07-10T05:45:51.929626Z","iopub.execute_input":"2023-07-10T05:45:51.929989Z","iopub.status.idle":"2023-07-10T05:45:52.231535Z","shell.execute_reply.started":"2023-07-10T05:45:51.929958Z","shell.execute_reply":"2023-07-10T05:45:52.230515Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# callback 선언.\nfrom keras.callbacks import EarlyStopping , ReduceLROnPlateau\n\nearly_stopping = EarlyStopping(monitor='val_loss', patience=5, verbose=1, mode='min')\nreduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.2, patience=3, verbose=1, mode='min')\n\ncallbacks = [early_stopping, reduce_lr]","metadata":{"execution":{"iopub.status.busy":"2023-07-10T05:45:56.415073Z","iopub.execute_input":"2023-07-10T05:45:56.415970Z","iopub.status.idle":"2023-07-10T05:45:56.422394Z","shell.execute_reply.started":"2023-07-10T05:45:56.415924Z","shell.execute_reply":"2023-07-10T05:45:56.421197Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.applications import ResNet50\nfrom keras import models, layers\n\n# ResNet50 모델 불러오기\nresnet50 = ResNet50(input_shape=(IMG_HEIGHT,IMG_WIDTH,3), include_top=False, weights=\"imagenet\")\n\n# freeze_index 설정하기\nfreeze_index = 143 # conv5_1 레이어의 인덱스\n\n# freeze_index 이전의 층은 고정하고, 이후의 층은 학습 가능하게 설정하기\nfor layer in resnet50.layers[:freeze_index]:\n    layer.trainable = False\nfor layer in resnet50.layers[freeze_index:]:\n    layer.trainable = True\n\n# 배치 정규화 레이어는 자동 조정 적용하기\nfor layer in resnet50.layers:\n    if isinstance(layer, BatchNormalization):\n        layer.trainable = True\n\n# FC Layer 추가하기\nmodel = models.Sequential()\nmodel.add(resnet50)\nmodel.add(layers.Flatten())\nmodel.add(layers.Dense(256, activation='relu'))\nmodel.add(layers.Dense(1, activation='sigmoid'))\n\n\nmodel.compile(loss = tf.keras.losses.binary_crossentropy,\n              optimizer = tf.keras.optimizers.RMSprop(learning_rate=1e-4), metrics=['accuracy'])\n\nhistory = model.fit(\n  train_data_gen,\n  steps_per_epoch= train_data.shape[0]/batch_size,\n  validation_steps= val_data.shape[0]/batch_size,\n  batch_size = batch_size,\n  epochs=epochs,\n  callbacks = callbacks,\n  validation_data= val_data_gen)","metadata":{"execution":{"iopub.status.busy":"2023-07-10T05:45:58.664728Z","iopub.execute_input":"2023-07-10T05:45:58.665173Z","iopub.status.idle":"2023-07-10T07:52:39.764408Z","shell.execute_reply.started":"2023-07-10T05:45:58.665137Z","shell.execute_reply":"2023-07-10T07:52:39.763409Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# model.to_save('./content/dogs-vs-cats/resnet_ver1.h5')","metadata":{"execution":{"iopub.status.busy":"2023-07-10T08:00:47.405344Z","iopub.execute_input":"2023-07-10T08:00:47.405722Z","iopub.status.idle":"2023-07-10T08:00:47.455824Z","shell.execute_reply.started":"2023-07-10T08:00:47.405690Z","shell.execute_reply":"2023-07-10T08:00:47.454555Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_datagen = ImageDataGenerator(rescale=1./255)\n\ntest_data_gen = test_datagen.flow_from_dataframe(batch_size = batch_size,\ndataframe = df_test,\nx_col = 'image_path',\ny_col = 'target',\ntarget_size=(IMG_HEIGHT, IMG_WIDTH),\nclass_mode=None)","metadata":{"execution":{"iopub.status.busy":"2023-07-10T08:07:09.460156Z","iopub.execute_input":"2023-07-10T08:07:09.460554Z","iopub.status.idle":"2023-07-10T08:07:09.585998Z","shell.execute_reply.started":"2023-07-10T08:07:09.460525Z","shell.execute_reply":"2023-07-10T08:07:09.585047Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 모델예측하기\n\npred = model.predict(test_data_gen)\n\n# 임계값 적용하기\npredictions = (pred > 0.5).astype(int)\ndf_test['pred'] = predictions","metadata":{"execution":{"iopub.status.busy":"2023-07-10T08:07:59.640792Z","iopub.execute_input":"2023-07-10T08:07:59.641185Z","iopub.status.idle":"2023-07-10T08:08:53.693623Z","shell.execute_reply.started":"2023-07-10T08:07:59.641154Z","shell.execute_reply":"2023-07-10T08:08:53.692636Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_submission = df_test.copy()\ndf_submission['id'] = df_submission['filename'].str.split('.').str[0]\ndf_submission['label'] = df_submission['pred']","metadata":{"execution":{"iopub.status.busy":"2023-07-10T08:09:31.376228Z","iopub.execute_input":"2023-07-10T08:09:31.376609Z","iopub.status.idle":"2023-07-10T08:09:31.405328Z","shell.execute_reply.started":"2023-07-10T08:09:31.376578Z","shell.execute_reply":"2023-07-10T08:09:31.404459Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_submission.drop(['filename', 'image_path','pred'], axis=1, inplace=True)\ndf_submission.head()","metadata":{"execution":{"iopub.status.busy":"2023-07-10T08:09:45.624281Z","iopub.execute_input":"2023-07-10T08:09:45.624805Z","iopub.status.idle":"2023-07-10T08:09:45.642587Z","shell.execute_reply.started":"2023-07-10T08:09:45.624767Z","shell.execute_reply":"2023-07-10T08:09:45.641294Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ndf_submission.head()\ndf_submission.to_csv('submission.csv' , index=False)","metadata":{"execution":{"iopub.status.busy":"2023-07-10T08:11:24.719029Z","iopub.execute_input":"2023-07-10T08:11:24.719406Z","iopub.status.idle":"2023-07-10T08:11:24.753048Z","shell.execute_reply.started":"2023-07-10T08:11:24.719373Z","shell.execute_reply":"2023-07-10T08:11:24.752143Z"},"trusted":true},"execution_count":null,"outputs":[]}]}