{"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-11-11T06:03:11.159607Z","iopub.execute_input":"2022-11-11T06:03:11.159952Z","iopub.status.idle":"2022-11-11T06:03:11.183326Z","shell.execute_reply.started":"2022-11-11T06:03:11.159875Z","shell.execute_reply":"2022-11-11T06:03:11.182450Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub=pd.read_csv('/kaggle/input/dogs-vs-cats-redux-kernels-edition/sample_submission.csv')\nsub","metadata":{"execution":{"iopub.status.busy":"2022-11-11T06:03:59.893555Z","iopub.execute_input":"2022-11-11T06:03:59.893993Z","iopub.status.idle":"2022-11-11T06:03:59.937314Z","shell.execute_reply.started":"2022-11-11T06:03:59.893952Z","shell.execute_reply":"2022-11-11T06:03:59.936502Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!unzip /kaggle/input/dogs-vs-cats-redux-kernels-edition/train.zip -d .","metadata":{"execution":{"iopub.status.busy":"2022-11-11T06:05:45.101610Z","iopub.execute_input":"2022-11-11T06:05:45.101974Z","iopub.status.idle":"2022-11-11T06:05:57.536077Z","shell.execute_reply.started":"2022-11-11T06:05:45.101944Z","shell.execute_reply":"2022-11-11T06:05:57.534976Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!unzip /kaggle/input/dogs-vs-cats-redux-kernels-edition/test.zip -d .","metadata":{"execution":{"iopub.status.busy":"2022-11-11T06:07:03.823776Z","iopub.execute_input":"2022-11-11T06:07:03.824150Z","iopub.status.idle":"2022-11-11T06:07:10.141173Z","shell.execute_reply.started":"2022-11-11T06:07:03.824113Z","shell.execute_reply":"2022-11-11T06:07:10.140082Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pwd","metadata":{"execution":{"iopub.status.busy":"2022-11-11T06:07:41.679997Z","iopub.execute_input":"2022-11-11T06:07:41.680534Z","iopub.status.idle":"2022-11-11T06:07:42.658395Z","shell.execute_reply.started":"2022-11-11T06:07:41.680476Z","shell.execute_reply":"2022-11-11T06:07:42.657187Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dir='./train/'\ntest_dir='./test/'","metadata":{"execution":{"iopub.status.busy":"2022-11-11T06:08:30.962547Z","iopub.execute_input":"2022-11-11T06:08:30.962950Z","iopub.status.idle":"2022-11-11T06:08:30.968187Z","shell.execute_reply.started":"2022-11-11T06:08:30.962915Z","shell.execute_reply":"2022-11-11T06:08:30.967104Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"a=os.listdir(train_dir)","metadata":{"execution":{"iopub.status.busy":"2022-11-11T06:09:36.188887Z","iopub.execute_input":"2022-11-11T06:09:36.189273Z","iopub.status.idle":"2022-11-11T06:09:36.207980Z","shell.execute_reply.started":"2022-11-11T06:09:36.189243Z","shell.execute_reply":"2022-11-11T06:09:36.207058Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(a)","metadata":{"execution":{"iopub.status.busy":"2022-11-11T06:09:36.561427Z","iopub.execute_input":"2022-11-11T06:09:36.561769Z","iopub.status.idle":"2022-11-11T06:09:36.568187Z","shell.execute_reply.started":"2022-11-11T06:09:36.561743Z","shell.execute_reply":"2022-11-11T06:09:36.567188Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"a[0]","metadata":{"execution":{"iopub.status.busy":"2022-11-11T06:09:52.059322Z","iopub.execute_input":"2022-11-11T06:09:52.060029Z","iopub.status.idle":"2022-11-11T06:09:52.066184Z","shell.execute_reply.started":"2022-11-11T06:09:52.059993Z","shell.execute_reply":"2022-11-11T06:09:52.065226Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train=pd.DataFrame()\ntrain['file']=a\ntrain","metadata":{"execution":{"iopub.status.busy":"2022-11-11T06:12:18.203394Z","iopub.execute_input":"2022-11-11T06:12:18.204088Z","iopub.status.idle":"2022-11-11T06:12:18.228148Z","shell.execute_reply.started":"2022-11-11T06:12:18.204054Z","shell.execute_reply":"2022-11-11T06:12:18.227300Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test=pd.DataFrame()\ntest['file']=os.listdir(test_dir)\ntest","metadata":{"execution":{"iopub.status.busy":"2022-11-11T06:13:11.883969Z","iopub.execute_input":"2022-11-11T06:13:11.884323Z","iopub.status.idle":"2022-11-11T06:13:11.906367Z","shell.execute_reply.started":"2022-11-11T06:13:11.884294Z","shell.execute_reply":"2022-11-11T06:13:11.905414Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub.dtypes","metadata":{"execution":{"iopub.status.busy":"2022-11-11T06:15:28.830524Z","iopub.execute_input":"2022-11-11T06:15:28.830977Z","iopub.status.idle":"2022-11-11T06:15:28.846490Z","shell.execute_reply.started":"2022-11-11T06:15:28.830935Z","shell.execute_reply":"2022-11-11T06:15:28.845493Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test1=sub.copy()\ntest1['file']=sub['id'].astype('str')+'.jpg'\ntest1","metadata":{"execution":{"iopub.status.busy":"2022-11-11T06:15:52.464916Z","iopub.execute_input":"2022-11-11T06:15:52.465276Z","iopub.status.idle":"2022-11-11T06:15:52.495903Z","shell.execute_reply.started":"2022-11-11T06:15:52.465246Z","shell.execute_reply":"2022-11-11T06:15:52.494969Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['file'][0].split('.')[0]","metadata":{"execution":{"iopub.status.busy":"2022-11-11T06:18:56.804420Z","iopub.execute_input":"2022-11-11T06:18:56.805329Z","iopub.status.idle":"2022-11-11T06:18:56.812453Z","shell.execute_reply.started":"2022-11-11T06:18:56.805283Z","shell.execute_reply":"2022-11-11T06:18:56.811509Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"target=[]\nfor i in range(len(train)):\n    a=train['file'][i].split('.')[0]\n    if a=='dog':\n        target.append('1')\n    else: \n        target.append('0')","metadata":{"execution":{"iopub.status.busy":"2022-11-11T06:19:36.721782Z","iopub.execute_input":"2022-11-11T06:19:36.722139Z","iopub.status.idle":"2022-11-11T06:19:36.880234Z","shell.execute_reply.started":"2022-11-11T06:19:36.722109Z","shell.execute_reply":"2022-11-11T06:19:36.879165Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"target","metadata":{"execution":{"iopub.status.busy":"2022-11-11T06:19:42.575949Z","iopub.execute_input":"2022-11-11T06:19:42.576297Z","iopub.status.idle":"2022-11-11T06:19:42.601087Z","shell.execute_reply.started":"2022-11-11T06:19:42.576268Z","shell.execute_reply":"2022-11-11T06:19:42.600018Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['label']=target\ntrain","metadata":{"execution":{"iopub.status.busy":"2022-11-11T06:20:28.369847Z","iopub.execute_input":"2022-11-11T06:20:28.370204Z","iopub.status.idle":"2022-11-11T06:20:28.386337Z","shell.execute_reply.started":"2022-11-11T06:20:28.370176Z","shell.execute_reply":"2022-11-11T06:20:28.385387Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"check=[]\ncnt=0\nfor i in range(len(train)):\n    a=train['file'][i].split('.')[-1]\n    if a!='jpg':\n        check.append(train['file'][i])\n    else:\n        cnt+=1","metadata":{"execution":{"iopub.status.busy":"2022-11-11T06:25:33.607240Z","iopub.execute_input":"2022-11-11T06:25:33.608333Z","iopub.status.idle":"2022-11-11T06:25:33.765302Z","shell.execute_reply.started":"2022-11-11T06:25:33.608295Z","shell.execute_reply":"2022-11-11T06:25:33.764443Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"check","metadata":{"execution":{"iopub.status.busy":"2022-11-11T06:25:34.053758Z","iopub.execute_input":"2022-11-11T06:25:34.054393Z","iopub.status.idle":"2022-11-11T06:25:34.061614Z","shell.execute_reply.started":"2022-11-11T06:25:34.054354Z","shell.execute_reply":"2022-11-11T06:25:34.060477Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cnt","metadata":{"execution":{"iopub.status.busy":"2022-11-11T06:25:34.380827Z","iopub.execute_input":"2022-11-11T06:25:34.381432Z","iopub.status.idle":"2022-11-11T06:25:34.387577Z","shell.execute_reply.started":"2022-11-11T06:25:34.381381Z","shell.execute_reply":"2022-11-11T06:25:34.386675Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Image Check","metadata":{}},{"cell_type":"code","source":"from PIL import Image\nimport matplotlib.pyplot as plt\nimg1=Image.open(train_dir+'cat.1022.jpg')\nimg2=Image.open(train_dir+'dog.11329.jpg')","metadata":{"execution":{"iopub.status.busy":"2022-11-11T06:27:55.398013Z","iopub.execute_input":"2022-11-11T06:27:55.398366Z","iopub.status.idle":"2022-11-11T06:27:55.412023Z","shell.execute_reply.started":"2022-11-11T06:27:55.398335Z","shell.execute_reply":"2022-11-11T06:27:55.410971Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(img1)\nplt.show()\nplt.imshow(img2)\nplt.show()\nprint(img1.size, img2.size)","metadata":{"execution":{"iopub.status.busy":"2022-11-11T06:28:54.764450Z","iopub.execute_input":"2022-11-11T06:28:54.764884Z","iopub.status.idle":"2022-11-11T06:28:55.192476Z","shell.execute_reply.started":"2022-11-11T06:28:54.764846Z","shell.execute_reply":"2022-11-11T06:28:55.191375Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Image Loader","metadata":{}},{"cell_type":"code","source":"isize=(224,224)\nishape=(224,224,3)\nbs=16","metadata":{"execution":{"iopub.status.busy":"2022-11-11T06:30:30.498316Z","iopub.execute_input":"2022-11-11T06:30:30.498999Z","iopub.status.idle":"2022-11-11T06:30:30.503487Z","shell.execute_reply.started":"2022-11-11T06:30:30.498964Z","shell.execute_reply":"2022-11-11T06:30:30.502457Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import ImageDataGenerator\nigen=ImageDataGenerator(rescale=1/255, validation_split=0.2)\ntrain0=igen.flow_from_dataframe(train,directory=train_dir,x_col='file', \n                                y_col='label',target_size=isize,class_mode='binary',\n                               batch_size=bs, subset='training')\nval0=igen.flow_from_dataframe(train,directory=train_dir,x_col='file', \n                                y_col='label',target_size=isize,class_mode='binary',\n                               batch_size=bs, subset='validation')\ntest0=igen.flow_from_dataframe(test1, directory=test_dir, x_col='file', y_col=None, target_size=isize,\n                               class_mode=None, shuffle=False)","metadata":{"execution":{"iopub.status.busy":"2022-11-11T06:37:46.991922Z","iopub.execute_input":"2022-11-11T06:37:46.992283Z","iopub.status.idle":"2022-11-11T06:37:47.458686Z","shell.execute_reply.started":"2022-11-11T06:37:46.992252Z","shell.execute_reply":"2022-11-11T06:37:47.457669Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Data Generator 확인","metadata":{}},{"cell_type":"code","source":"x, y=train0.next()","metadata":{"execution":{"iopub.status.busy":"2022-11-11T06:42:12.206181Z","iopub.execute_input":"2022-11-11T06:42:12.206573Z","iopub.status.idle":"2022-11-11T06:42:12.258564Z","shell.execute_reply.started":"2022-11-11T06:42:12.206539Z","shell.execute_reply":"2022-11-11T06:42:12.257604Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(bs):\n    plt.imshow((x[i]*255).astype('int'))\n    plt.show()\n    print(y[i])","metadata":{"execution":{"iopub.status.busy":"2022-11-11T06:42:13.244340Z","iopub.execute_input":"2022-11-11T06:42:13.245482Z","iopub.status.idle":"2022-11-11T06:42:16.804291Z","shell.execute_reply.started":"2022-11-11T06:42:13.245411Z","shell.execute_reply":"2022-11-11T06:42:16.803473Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Transfer Learning","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.models import Sequential\nfrom tensorflow.keras import Model, Input\nfrom tensorflow.keras.layers import Dense, Flatten, GlobalAveragePooling2D, Dropout\nfrom tensorflow.keras.applications import MobileNetV2","metadata":{"execution":{"iopub.status.busy":"2022-11-11T06:48:02.424515Z","iopub.execute_input":"2022-11-11T06:48:02.425644Z","iopub.status.idle":"2022-11-11T06:48:02.432524Z","shell.execute_reply.started":"2022-11-11T06:48:02.425600Z","shell.execute_reply":"2022-11-11T06:48:02.431621Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"base=MobileNetV2(include_top=False, weights='imagenet', input_shape=ishape)\nx= base.output\nx= GlobalAveragePooling2D()(x)\nx= Dropout(0.4)(x)\nx= Dense(128, activation='relu')(x)\nx= Dropout(0.3)(x)\nx= Dense(16, activation='relu')(x)\npred= Dense(1, activation='sigmoid')(x)\nmodel=Model(inputs = base.input, outputs=pred)\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2022-11-11T06:52:43.380415Z","iopub.execute_input":"2022-11-11T06:52:43.380919Z","iopub.status.idle":"2022-11-11T06:52:47.300681Z","shell.execute_reply.started":"2022-11-11T06:52:43.380880Z","shell.execute_reply":"2022-11-11T06:52:47.299710Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(loss='binary_crossentropy', optimizer='adam', metrics='acc')","metadata":{"execution":{"iopub.status.busy":"2022-11-11T06:55:10.337047Z","iopub.execute_input":"2022-11-11T06:55:10.337669Z","iopub.status.idle":"2022-11-11T06:55:10.365265Z","shell.execute_reply.started":"2022-11-11T06:55:10.337614Z","shell.execute_reply":"2022-11-11T06:55:10.364361Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"hist=model.fit(train0, epochs=3, validation_data=val0)","metadata":{"execution":{"iopub.status.busy":"2022-11-11T06:55:48.750482Z","iopub.execute_input":"2022-11-11T06:55:48.751176Z","iopub.status.idle":"2022-11-11T07:02:40.025994Z","shell.execute_reply.started":"2022-11-11T06:55:48.751139Z","shell.execute_reply":"2022-11-11T07:02:40.025055Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"res=model.predict(test0)\nsub['label']=res\nsub.to_csv('dc1.csv', index=False)\nsub['label']=(res>0.5)*1\nsub.to_csv('dc2.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-11-11T07:02:40.028160Z","iopub.execute_input":"2022-11-11T07:02:40.029945Z","iopub.status.idle":"2022-11-11T07:03:21.009548Z","shell.execute_reply.started":"2022-11-11T07:02:40.029901Z","shell.execute_reply":"2022-11-11T07:03:21.008384Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}