{"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-10T02:30:35.248961Z","iopub.execute_input":"2023-07-10T02:30:35.249487Z","iopub.status.idle":"2023-07-10T02:30:35.268223Z","shell.execute_reply.started":"2023-07-10T02:30:35.249459Z","shell.execute_reply":"2023-07-10T02:30:35.267055Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import zipfile\n# /kaggle/input/dogs-vs-cats-redux-kernels-edition\n#/kaggle/working/\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-10T02:30:35.270370Z","iopub.execute_input":"2023-07-10T02:30:35.270729Z","iopub.status.idle":"2023-07-10T02:30:53.580862Z","shell.execute_reply.started":"2023-07-10T02:30:35.270698Z","shell.execute_reply":"2023-07-10T02:30:53.579866Z"},"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\nfrom sklearn.model_selection import train_test_split\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\n\nimport matplotlib.pyplot as plt\n\nfrom sklearn.metrics import confusion_matrix\n\nimport itertools\nimport plotly.express as px","metadata":{"execution":{"iopub.status.busy":"2023-07-10T02:30:53.582506Z","iopub.execute_input":"2023-07-10T02:30:53.582872Z","iopub.status.idle":"2023-07-10T02:31:04.385290Z","shell.execute_reply.started":"2023-07-10T02:30:53.582817Z","shell.execute_reply":"2023-07-10T02:31:04.384220Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train data numbers\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-10T02:31:04.387959Z","iopub.execute_input":"2023-07-10T02:31:04.388327Z","iopub.status.idle":"2023-07-10T02:31:04.411159Z","shell.execute_reply.started":"2023-07-10T02:31:04.388296Z","shell.execute_reply":"2023-07-10T02:31:04.410248Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# test data numbers\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-10T02:31:04.412406Z","iopub.execute_input":"2023-07-10T02:31:04.413320Z","iopub.status.idle":"2023-07-10T02:31:04.427877Z","shell.execute_reply.started":"2023-07-10T02:31:04.413288Z","shell.execute_reply":"2023-07-10T02:31:04.426861Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Train Data\nfrom PIL import Image\n\ntargets = []\nfull_paths = []\ntrain_cats_dir = []\ntrain_dogs_dir = []\nimage_width = []\nimage_height = []\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# image size info \nfor file_name in os.listdir(train_folder_path):\n  if file_name.endswith('.jpg'):\n    image_path = os.path.join(train_folder_path, file_name)\n    with Image.open(image_path) as img:\n        width, height = img.size\n        image_width.append(width)\n        image_height.append(height)\n\ndf_train = pd.DataFrame() \ndf_train['image_path'] = full_paths \ndf_train['target'] = targets\ndf_train['image_width'] = image_width\ndf_train['image_height'] = image_height","metadata":{"execution":{"iopub.status.busy":"2023-07-10T02:31:04.429256Z","iopub.execute_input":"2023-07-10T02:31:04.429661Z","iopub.status.idle":"2023-07-10T02:31:07.264626Z","shell.execute_reply.started":"2023-07-10T02:31:04.429630Z","shell.execute_reply":"2023-07-10T02:31:07.263618Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.head(5)","metadata":{"execution":{"iopub.status.busy":"2023-07-10T02:31:07.266051Z","iopub.execute_input":"2023-07-10T02:31:07.266511Z","iopub.status.idle":"2023-07-10T02:31:07.284582Z","shell.execute_reply.started":"2023-07-10T02:31:07.266477Z","shell.execute_reply":"2023-07-10T02:31:07.283738Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import seaborn as sns\n\nprint('Total class numbers ? : ' , len(df_train.target))\nprint ('dog class numbers ? : '  , (df_train.target == 'dog').sum())\nprint ('cat class numbers ? : '  , (df_train.target == 'cat').sum())\n\nsns.countplot(df_train , x = 'target' )","metadata":{"execution":{"iopub.status.busy":"2023-07-10T02:31:07.286216Z","iopub.execute_input":"2023-07-10T02:31:07.286582Z","iopub.status.idle":"2023-07-10T02:31:07.890330Z","shell.execute_reply.started":"2023-07-10T02:31:07.286548Z","shell.execute_reply":"2023-07-10T02:31:07.889151Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# width size ?\npx.histogram(df_train , x = 'image_width' , color = 'target')","metadata":{"execution":{"iopub.status.busy":"2023-07-10T02:31:07.899872Z","iopub.execute_input":"2023-07-10T02:31:07.902959Z","iopub.status.idle":"2023-07-10T02:31:09.698090Z","shell.execute_reply.started":"2023-07-10T02:31:07.902915Z","shell.execute_reply":"2023-07-10T02:31:09.697222Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# height size \npx.histogram(df_train , x = 'image_height', color = 'target')","metadata":{"execution":{"iopub.status.busy":"2023-07-10T02:31:09.699186Z","iopub.execute_input":"2023-07-10T02:31:09.699820Z","iopub.status.idle":"2023-07-10T02:31:09.793697Z","shell.execute_reply.started":"2023-07-10T02:31:09.699787Z","shell.execute_reply":"2023-07-10T02:31:09.792725Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" # Train Data 'Cat' Class visualization\nimport matplotlib.image as img\n\nrows = 4\ncols = 4\naxes = []\nfig=plt.figure(figsize=(10,10))\ni = 0\n\nfor a in range(rows*cols):\n    b = img.imread(train_cats_dir[i])\n    axes.append(fig.add_subplot(rows,cols,a+1))\n    plt.imshow(b)\n    i+=1\nfig.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-07-10T02:31:09.795283Z","iopub.execute_input":"2023-07-10T02:31:09.795920Z","iopub.status.idle":"2023-07-10T02:31:13.304367Z","shell.execute_reply.started":"2023-07-10T02:31:09.795885Z","shell.execute_reply":"2023-07-10T02:31:13.301100Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Test Data 'Cat' Class visualization\n\ntest_folder_path = './dogs-vs-cats/test'\n\ntest_dir =[]\n\n# Target setting\nfor file_name in test_file_names:\n    path = os.path.join(test_folder_path, file_name)\n    test_dir.append(path)\n\n\nrows = 4\ncols = 4\naxes = []\nfig=plt.figure(figsize=(10,10))\ni = 0\n\nfor a in range(rows*cols):\n    b = img.imread(test_dir[i])\n    axes.append(fig.add_subplot(rows,cols,a+1))\n    plt.imshow(b)\n    i+=1\nfig.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-07-10T02:31:13.305741Z","iopub.execute_input":"2023-07-10T02:31:13.306623Z","iopub.status.idle":"2023-07-10T02:31:16.177488Z","shell.execute_reply.started":"2023-07-10T02:31:13.306589Z","shell.execute_reply":"2023-07-10T02:31:16.171997Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Data Split\ntrain_data , val_data = train_test_split(df_train, test_size=0.2, random_state=1114)","metadata":{"execution":{"iopub.status.busy":"2023-07-10T02:31:16.179053Z","iopub.execute_input":"2023-07-10T02:31:16.179463Z","iopub.status.idle":"2023-07-10T02:31:16.191510Z","shell.execute_reply.started":"2023-07-10T02:31:16.179426Z","shell.execute_reply":"2023-07-10T02:31:16.190509Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Train Data number ? ' , len(train_data))\nprint('Validation Data number? ' , len(val_data))\n\nplt.suptitle('Train Class Count')\nsns.countplot(data = train_data , x='target')","metadata":{"execution":{"iopub.status.busy":"2023-07-10T02:31:16.193001Z","iopub.execute_input":"2023-07-10T02:31:16.193698Z","iopub.status.idle":"2023-07-10T02:31:16.443257Z","shell.execute_reply.started":"2023-07-10T02:31:16.193667Z","shell.execute_reply":"2023-07-10T02:31:16.442364Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# parameter Initialization \nbatch_size = 32\nepochs = 30\nIMG_HEIGHT = 256\nIMG_WIDTH = 256","metadata":{"execution":{"iopub.status.busy":"2023-07-10T02:31:16.444581Z","iopub.execute_input":"2023-07-10T02:31:16.445722Z","iopub.status.idle":"2023-07-10T02:31:16.451105Z","shell.execute_reply.started":"2023-07-10T02:31:16.445688Z","shell.execute_reply":"2023-07-10T02:31:16.449782Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# DataGenrator\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\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-10T02:31:16.452947Z","iopub.execute_input":"2023-07-10T02:31:16.453313Z","iopub.status.idle":"2023-07-10T02:31:16.775260Z","shell.execute_reply.started":"2023-07-10T02:31:16.453279Z","shell.execute_reply":"2023-07-10T02:31:16.774275Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from 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-10T02:31:16.776658Z","iopub.execute_input":"2023-07-10T02:31:16.777563Z","iopub.status.idle":"2023-07-10T02:31:16.784039Z","shell.execute_reply.started":"2023-07-10T02:31:16.777524Z","shell.execute_reply":"2023-07-10T02:31:16.783023Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Model Build (CNN)\nmodel = Sequential()\nmodel.add(Conv2D(32, (3, 3), activation='relu', input_shape=(IMG_HEIGHT, IMG_WIDTH, 3)))\nmodel.add(MaxPooling2D((2, 2)))\nmodel.add(Conv2D(64, (3, 3), activation='relu'))\nmodel.add(MaxPooling2D((2, 2)))\nmodel.add(Conv2D(128, (3, 3), activation='relu'))\nmodel.add(MaxPooling2D((2, 2)))\nmodel.add(Conv2D(128, (3, 3), activation='relu'))\nmodel.add(MaxPooling2D((2, 2)))\nmodel.add(Dropout(0.2))\nmodel.add(Flatten())\nmodel.add(Dense(512, activation='relu'))\nmodel.add(Dense(1, activation='sigmoid'))\n\nmodel.compile(loss='binary_crossentropy', 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    callbacks = callbacks,\n    epochs = epochs,\n    validation_data= val_data_gen)","metadata":{"execution":{"iopub.status.busy":"2023-07-10T02:31:16.785375Z","iopub.execute_input":"2023-07-10T02:31:16.786325Z","iopub.status.idle":"2023-07-10T05:25:11.196733Z","shell.execute_reply.started":"2023-07-10T02:31:16.786287Z","shell.execute_reply":"2023-07-10T05:25:11.195648Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_model_history(model_history, acc='accuracy', val_acc='val_accuracy'):\n    fig, axs = plt.subplots(1,2,figsize=(15,5))\n    \n    axs[0].plot(range(1,len(model_history.history[acc])+1),model_history.history[acc])\n    axs[0].plot(range(1,len(model_history.history[val_acc])+1),model_history.history[val_acc])\n    axs[0].set_title('Model Accuracy')\n    axs[0].set_ylabel('Accuracy')\n    axs[0].set_xlabel('Epoch')\n\n    axs[0].legend(['train', 'val'], loc='best')\n    \n    axs[1].plot(range(1,len(model_history.history['loss'])+1),model_history.history['loss'])\n    axs[1].plot(range(1,len(model_history.history['val_loss'])+1),model_history.history['val_loss'])\n    axs[1].set_title('Model Loss')\n    axs[1].set_ylabel('Loss')\n    axs[1].set_xlabel('Epoch')\n\n    axs[1].legend(['train', 'val'], loc='best')\n    plt.show()\n\nplot_model_history(history)","metadata":{"execution":{"iopub.status.busy":"2023-07-10T05:25:11.198544Z","iopub.execute_input":"2023-07-10T05:25:11.199137Z","iopub.status.idle":"2023-07-10T05:25:11.906204Z","shell.execute_reply.started":"2023-07-10T05:25:11.199102Z","shell.execute_reply":"2023-07-10T05:25:11.905308Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Test Data\n\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\n\ndf_test = pd.DataFrame() \ndf_test['image_path'] = full_paths\ndf_test['filename'] = file_names ","metadata":{"execution":{"iopub.status.busy":"2023-07-10T05:25:11.910398Z","iopub.execute_input":"2023-07-10T05:25:11.912547Z","iopub.status.idle":"2023-07-10T05:25:11.969161Z","shell.execute_reply.started":"2023-07-10T05:25:11.912513Z","shell.execute_reply":"2023-07-10T05:25:11.968236Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Test Data preprocessing\ntest_datagen = ImageDataGenerator(rescale=1./255)\n\ntest_data_gen = test_datagen.flow_from_dataframe(batch_size = batch_size,\n                                                     dataframe = df_test,\n                                                     x_col = 'image_path',\n                                                     target_size=(IMG_HEIGHT, IMG_WIDTH),\n                                                     class_mode= None)","metadata":{"execution":{"iopub.status.busy":"2023-07-10T05:25:11.973229Z","iopub.execute_input":"2023-07-10T05:25:11.975334Z","iopub.status.idle":"2023-07-10T05:25:12.170365Z","shell.execute_reply.started":"2023-07-10T05:25:11.975302Z","shell.execute_reply":"2023-07-10T05:25:12.169476Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# predict\npred = model.predict(test_data_gen)\n\n# threshold \npredictions = (pred > 0.5).astype(int)\ndf_test['pred'] = predictions","metadata":{"execution":{"iopub.status.busy":"2023-07-10T05:25:12.174654Z","iopub.execute_input":"2023-07-10T05:25:12.176975Z","iopub.status.idle":"2023-07-10T05:25:53.437991Z","shell.execute_reply.started":"2023-07-10T05:25:12.176941Z","shell.execute_reply":"2023-07-10T05:25:53.436875Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = plt.figure(1, figsize=(20, 20))\n\nfor i in range(25):\n    plt.subplot(5, 5, i+1)\n    plt.imshow(Image.open(df_test.image_path[i]))\n    plt.title(f\" Predicted as {'dog' if predictions[i] == 1 else 'cat'} \\n Confidence : {round(100*(np.max(pred[i])), 2)}% \", fontsize=8)\n    \nplt.tight_layout\nplt.show","metadata":{"execution":{"iopub.status.busy":"2023-07-10T05:25:53.439402Z","iopub.execute_input":"2023-07-10T05:25:53.440368Z","iopub.status.idle":"2023-07-10T05:25:59.914961Z","shell.execute_reply.started":"2023-07-10T05:25:53.440330Z","shell.execute_reply":"2023-07-10T05:25:59.913825Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test","metadata":{"execution":{"iopub.status.busy":"2023-07-10T05:25:59.920924Z","iopub.execute_input":"2023-07-10T05:25:59.921764Z","iopub.status.idle":"2023-07-10T05:25:59.934206Z","shell.execute_reply.started":"2023-07-10T05:25:59.921729Z","shell.execute_reply":"2023-07-10T05:25:59.933409Z"},"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']\n# df_submission.head()","metadata":{"execution":{"iopub.status.busy":"2023-07-10T05:25:59.935317Z","iopub.execute_input":"2023-07-10T05:25:59.936260Z","iopub.status.idle":"2023-07-10T05:25:59.975470Z","shell.execute_reply.started":"2023-07-10T05:25:59.936228Z","shell.execute_reply":"2023-07-10T05:25:59.974747Z"},"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-10T05:25:59.976650Z","iopub.execute_input":"2023-07-10T05:25:59.977607Z","iopub.status.idle":"2023-07-10T05:25:59.989097Z","shell.execute_reply.started":"2023-07-10T05:25:59.977577Z","shell.execute_reply":"2023-07-10T05:25:59.988162Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.countplot(df_submission , x = 'label')","metadata":{"execution":{"iopub.status.busy":"2023-07-10T05:25:59.990186Z","iopub.execute_input":"2023-07-10T05:25:59.990955Z","iopub.status.idle":"2023-07-10T05:26:00.209659Z","shell.execute_reply.started":"2023-07-10T05:25:59.990926Z","shell.execute_reply":"2023-07-10T05:26:00.208704Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_submission.to_csv('submission.csv' , index=False)","metadata":{"execution":{"iopub.status.busy":"2023-07-10T05:26:00.212862Z","iopub.execute_input":"2023-07-10T05:26:00.213132Z","iopub.status.idle":"2023-07-10T05:26:00.247378Z","shell.execute_reply.started":"2023-07-10T05:26:00.213108Z","shell.execute_reply":"2023-07-10T05:26:00.246573Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}