{"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-05-15T15:16:00.692894Z","iopub.execute_input":"2022-05-15T15:16:00.693432Z","iopub.status.idle":"2022-05-15T15:16:00.714813Z","shell.execute_reply.started":"2022-05-15T15:16:00.693339Z","shell.execute_reply":"2022-05-15T15:16:00.714064Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data","metadata":{}},{"cell_type":"code","source":"import os\nimport zipfile\nimport pandas as pd\nfrom tqdm import tqdm\nimport tensorflow as tf\nimport matplotlib.pyplot as plt\nimport matplotlib.image as mpimg\nfrom tensorflow.keras.optimizers import RMSprop","metadata":{"execution":{"iopub.status.busy":"2022-05-15T15:16:24.423811Z","iopub.execute_input":"2022-05-15T15:16:24.424113Z","iopub.status.idle":"2022-05-15T15:16:30.249200Z","shell.execute_reply.started":"2022-05-15T15:16:24.424080Z","shell.execute_reply":"2022-05-15T15:16:30.248251Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"work_path = './cats_and_dogs_filtered'\nos.mkdir(work_path)","metadata":{"execution":{"iopub.status.busy":"2022-05-15T15:16:30.250752Z","iopub.execute_input":"2022-05-15T15:16:30.251341Z","iopub.status.idle":"2022-05-15T15:16:30.259414Z","shell.execute_reply.started":"2022-05-15T15:16:30.251311Z","shell.execute_reply":"2022-05-15T15:16:30.257794Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"local_zip = '../input/dogs-vs-cats/test1.zip'\nzip_ref = zipfile.ZipFile(local_zip,'r')\nzip_ref.extractall(work_path)\n\nlocal_zip = '../input/dogs-vs-cats/train.zip'\nzip_ref = zipfile.ZipFile(local_zip,'r')\nzip_ref.extractall(work_path)\n\nzip_ref.close()","metadata":{"execution":{"iopub.status.busy":"2022-05-15T15:16:32.382709Z","iopub.execute_input":"2022-05-15T15:16:32.382982Z","iopub.status.idle":"2022-05-15T15:16:48.944633Z","shell.execute_reply.started":"2022-05-15T15:16:32.382951Z","shell.execute_reply":"2022-05-15T15:16:48.943886Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_path = os.path.join(work_path, 'train')\ntest_path = os.path.join(work_path, 'test1')","metadata":{"execution":{"iopub.status.busy":"2022-05-15T15:16:48.946231Z","iopub.execute_input":"2022-05-15T15:16:48.946492Z","iopub.status.idle":"2022-05-15T15:16:48.951631Z","shell.execute_reply.started":"2022-05-15T15:16:48.946458Z","shell.execute_reply":"2022-05-15T15:16:48.950813Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.DataFrame({'image_name':os.listdir(train_path)})\ntrain_df['label'] =train_df['image_name'].apply(lambda x: x.split('.')[0])\ntrain_df","metadata":{"execution":{"iopub.status.busy":"2022-05-15T15:16:48.953305Z","iopub.execute_input":"2022-05-15T15:16:48.953553Z","iopub.status.idle":"2022-05-15T15:16:49.006318Z","shell.execute_reply.started":"2022-05-15T15:16:48.953521Z","shell.execute_reply":"2022-05-15T15:16:49.005671Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df = pd.DataFrame({'image_name':os.listdir(test_path)})\ntest_df['label'] =test_df['image_name'].apply(lambda x: x.split('.')[0])\ntest_df","metadata":{"execution":{"iopub.status.busy":"2022-05-15T15:16:49.007933Z","iopub.execute_input":"2022-05-15T15:16:49.008188Z","iopub.status.idle":"2022-05-15T15:16:49.034605Z","shell.execute_reply.started":"2022-05-15T15:16:49.008156Z","shell.execute_reply":"2022-05-15T15:16:49.033885Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dog_path_train = os.path.join(train_path, 'dog')\nos.mkdir(dog_path_train)\ndog_df_train = train_df[train_df.label=='dog']\nfor n in tqdm(dog_df_train.image_name):\n    os.rename((os.path.join(train_path, n)), (os.path.join(dog_path_train, n)))","metadata":{"execution":{"iopub.status.busy":"2022-05-15T15:16:59.332613Z","iopub.execute_input":"2022-05-15T15:16:59.332865Z","iopub.status.idle":"2022-05-15T15:16:59.645767Z","shell.execute_reply.started":"2022-05-15T15:16:59.332838Z","shell.execute_reply":"2022-05-15T15:16:59.645080Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cat_path_train = os.path.join(train_path, 'cat')\nos.mkdir(cat_path_train)\ncat_df_train = train_df[train_df.label=='cat']\nfor n in tqdm(cat_df_train.image_name):\n    os.rename((os.path.join(train_path, n)), (os.path.join(cat_path_train, n)))","metadata":{"execution":{"iopub.status.busy":"2022-05-15T15:17:05.276702Z","iopub.execute_input":"2022-05-15T15:17:05.276952Z","iopub.status.idle":"2022-05-15T15:17:05.572307Z","shell.execute_reply.started":"2022-05-15T15:17:05.276925Z","shell.execute_reply":"2022-05-15T15:17:05.571445Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#check\n\nbase_dir = './cats_and_dogs_filtered'\n\nprint(' Contents of base directory')\nprint(os.listdir(base_dir))\n\nprint('\\n Contents of Train directory')\ntrain_path = f'{base_dir}/train'\nprint(os.listdir(train_path))\n\nprint('\\n Contents of validation directory')\nprint(os.listdir(test_path)[:5])","metadata":{"execution":{"iopub.status.busy":"2022-05-15T15:17:15.302790Z","iopub.execute_input":"2022-05-15T15:17:15.303067Z","iopub.status.idle":"2022-05-15T15:17:15.319631Z","shell.execute_reply.started":"2022-05-15T15:17:15.303033Z","shell.execute_reply":"2022-05-15T15:17:15.318667Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dir = os.path.join(base_dir,'train')\nvalidation_dir = os.path.join(base_dir,'test1')\n\ntrain_cats_dir = os.path.join(train_dir,'cat')\ntrain_dogs_dir = os.path.join(train_dir,'dog')","metadata":{"execution":{"iopub.status.busy":"2022-05-15T15:17:35.141980Z","iopub.execute_input":"2022-05-15T15:17:35.142812Z","iopub.status.idle":"2022-05-15T15:17:35.147308Z","shell.execute_reply.started":"2022-05-15T15:17:35.142768Z","shell.execute_reply":"2022-05-15T15:17:35.146622Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_cats_names = os.listdir(train_cats_dir)\ntrain_dogs_names = os.listdir(train_dogs_dir)\n\nprint(train_cats_names[:5])\nprint(train_dogs_names[:5])","metadata":{"execution":{"iopub.status.busy":"2022-05-15T15:17:42.325190Z","iopub.execute_input":"2022-05-15T15:17:42.325487Z","iopub.status.idle":"2022-05-15T15:17:42.345832Z","shell.execute_reply.started":"2022-05-15T15:17:42.325454Z","shell.execute_reply":"2022-05-15T15:17:42.345085Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#number\n\nprint(f'numbers of cats in training set = {len(train_cats_names)}')\nprint(f'numbers of dogs in training set = {len(train_dogs_names)}')\nprint(f'numbers of cats and dogs in validation set = {len(os.listdir(validation_dir))}')","metadata":{"execution":{"iopub.status.busy":"2022-05-15T15:18:00.492845Z","iopub.execute_input":"2022-05-15T15:18:00.493554Z","iopub.status.idle":"2022-05-15T15:18:00.507841Z","shell.execute_reply.started":"2022-05-15T15:18:00.493515Z","shell.execute_reply":"2022-05-15T15:18:00.507075Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#image 확인\n\n%matplotlib inline\n\nnrows = 4\nncols = 4\n\npic_index = 0\n\nfig = plt.gcf()\nfig.set_size_inches(nrows*4,ncols*4)\n\nnext_cat_pic = [os.path.join(train_cats_dir,fname) for fname in train_cats_names[pic_index:pic_index+8]]\n\nnext_dog_pic = [os.path.join(train_dogs_dir,fname) for fname in train_dogs_names[pic_index:pic_index+8]]\n\nfor i ,img_path in enumerate(next_cat_pic+next_dog_pic):\n    sp = plt.subplot(nrows,ncols,i+1)\n    sp.axis('off')\n    \n    img = mpimg.imread(img_path)\n    plt.imshow(img)\nplt.show() ","metadata":{"execution":{"iopub.status.busy":"2022-05-15T15:18:21.919131Z","iopub.execute_input":"2022-05-15T15:18:21.919889Z","iopub.status.idle":"2022-05-15T15:18:23.235561Z","shell.execute_reply.started":"2022-05-15T15:18:21.919849Z","shell.execute_reply":"2022-05-15T15:18:23.234752Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model","metadata":{}},{"cell_type":"code","source":"def create_model():\n\n  model = tf.keras.models.Sequential([ \n    tf.keras.layers.Conv2D(16,(3,3), activation = 'relu', input_shape=(150,150,3)),\n    tf.keras.layers.MaxPooling2D(2,2),\n\n    tf.keras.layers.Conv2D(32,(3,3), activation = 'relu'),\n    tf.keras.layers.MaxPooling2D(2,2),\n\n    tf.keras.layers.Conv2D(64,(3,3), activation = 'relu'),\n    tf.keras.layers.MaxPooling2D(2,2),\n\n    tf.keras.layers.Flatten(),\n    tf.keras.layers.Dense(512, activation = 'relu'),\n    tf.keras.layers.Dense(1, activation='sigmoid')\n  ])\n\n  \n  model.compile(optimizer=RMSprop(lr=0.001),\n                loss='binary_crossentropy',\n                metrics=['accuracy']) \n    \n  return model","metadata":{"execution":{"iopub.status.busy":"2022-05-15T15:18:51.203924Z","iopub.execute_input":"2022-05-15T15:18:51.204604Z","iopub.status.idle":"2022-05-15T15:18:51.211435Z","shell.execute_reply.started":"2022-05-15T15:18:51.204567Z","shell.execute_reply":"2022-05-15T15:18:51.210777Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = create_model()\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2022-05-15T15:18:57.318281Z","iopub.execute_input":"2022-05-15T15:18:57.318957Z","iopub.status.idle":"2022-05-15T15:18:59.969399Z","shell.execute_reply.started":"2022-05-15T15:18:57.318921Z","shell.execute_reply":"2022-05-15T15:18:59.968691Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#이미지전처리\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\n\ntrain_datagen = ImageDataGenerator(rescale=1./255,\n      rotation_range=40,\n      width_shift_range=0.2,\n      height_shift_range=0.2,\n      shear_range=0.2,\n      zoom_range=0.2,\n      horizontal_flip=True,\n      fill_mode='nearest',\n      validation_split=0.2\n                                  )\n\ntrain_generator = train_datagen.flow_from_directory(\n    train_dir,\n    target_size=(150,150),\n    batch_size=50,\n    class_mode='binary',\n    subset='training'\n) \n\n\nvalidation_generator = train_datagen.flow_from_directory(\n    train_dir, # same directory as training data\n    target_size=(150, 150),\n    batch_size=50,\n    class_mode='binary',\n    subset='validation')","metadata":{"execution":{"iopub.status.busy":"2022-05-15T15:20:40.607497Z","iopub.execute_input":"2022-05-15T15:20:40.608104Z","iopub.status.idle":"2022-05-15T15:20:42.217414Z","shell.execute_reply.started":"2022-05-15T15:20:40.608066Z","shell.execute_reply":"2022-05-15T15:20:42.216570Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class mycallback(tf.keras.callbacks.Callback):\n    def on_epoch_end(self,epoch,logs={}):\n        if(logs.get('val_accuracy')>=0.90):\n            self.model.stop_training = True\n            \ncallback = mycallback()","metadata":{"execution":{"iopub.status.busy":"2022-05-15T15:20:46.859703Z","iopub.execute_input":"2022-05-15T15:20:46.860301Z","iopub.status.idle":"2022-05-15T15:20:46.867212Z","shell.execute_reply.started":"2022-05-15T15:20:46.860260Z","shell.execute_reply":"2022-05-15T15:20:46.865898Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Training","metadata":{}},{"cell_type":"code","source":"history = model.fit(\n    train_generator,\n    steps_per_epoch = train_generator.samples//50,#batch_size, \n    epochs = 30,\n    verbose=1,\n    validation_data = validation_generator,\n    validation_steps = validation_generator.samples//50,#batch_size,\n    callbacks=[callback]\n)","metadata":{"execution":{"iopub.status.busy":"2022-05-15T15:21:02.179879Z","iopub.execute_input":"2022-05-15T15:21:02.180570Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Accuracy","metadata":{}},{"cell_type":"code","source":"acc = history.history['accuracy']\nval_acc = history.history['val_accuracy']\n\nloss = history.history['loss']\nval_loss = history.history['val_loss']\n\nplt.figure(figsize=(8, 8))\nplt.subplot(2, 1, 1)\nplt.plot(acc, label='Training Accuracy')\nplt.plot(val_acc, label='Validation Accuracy')\nplt.legend(loc='lower right')\nplt.ylabel('Accuracy')\nplt.ylim([min(plt.ylim()),1])\nplt.title('Training and Validation Accuracy')\n\nplt.subplot(2, 1, 2)\nplt.plot(loss, label='Training Loss')\nplt.plot(val_loss, label='Validation Loss')\nplt.legend(loc='upper right')\nplt.ylabel('Cross Entropy')\nplt.ylim([0,1.0])\nplt.title('Training and Validation Loss')\nplt.xlabel('epoch')\nplt.show()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}