{"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":"markdown","source":"### 6장 심화문제 : CNN 모델 Dogs vs Cats\n\n### 경진대회 링크 https://www.kaggle.com/c/dogs-vs-cats-redux-kernels-edition\n\n#### (먼저 경진대회 Rules에서 I Understand and Accept 해야 참여가 가능합니다)","metadata":{}},{"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))","metadata":{"execution":{"iopub.status.busy":"2022-11-07T05:31:44.110678Z","iopub.execute_input":"2022-11-07T05:31:44.111089Z","iopub.status.idle":"2022-11-07T05:31:44.122618Z","shell.execute_reply.started":"2022-11-07T05:31:44.111003Z","shell.execute_reply":"2022-11-07T05:31:44.121578Z"},"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', dtype=str) #,sep=' ',header=None)","metadata":{"_uuid":"57bed3ff-4456-4e10-b8bc-346e69fb7033","_cell_guid":"b54f16a0-d60a-4f2f-aae4-73c500a309a8","execution":{"iopub.status.busy":"2022-11-07T05:31:44.124308Z","iopub.execute_input":"2022-11-07T05:31:44.124741Z","iopub.status.idle":"2022-11-07T05:31:44.148495Z","shell.execute_reply.started":"2022-11-07T05:31:44.124696Z","shell.execute_reply":"2022-11-07T05:31:44.147770Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub","metadata":{"_uuid":"fe87b9be-8170-400e-bee4-63429ce21f66","_cell_guid":"af2480d0-2a3d-4894-9afb-eeaaa09e6c67","execution":{"iopub.status.busy":"2022-11-07T05:31:44.150042Z","iopub.execute_input":"2022-11-07T05:31:44.150388Z","iopub.status.idle":"2022-11-07T05:31:44.171772Z","shell.execute_reply.started":"2022-11-07T05:31:44.150355Z","shell.execute_reply":"2022-11-07T05:31:44.170711Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 데이터 압축 풀기","metadata":{}},{"cell_type":"code","source":"!unzip ../input/dogs-vs-cats-redux-kernels-edition/train.zip -d . #train","metadata":{"_uuid":"69df9245-b5be-44c2-bc00-b2451e688805","_cell_guid":"f3514a3c-068e-41a5-96b0-986f3a7f857a","execution":{"iopub.status.busy":"2022-11-07T05:31:44.173523Z","iopub.execute_input":"2022-11-07T05:31:44.173929Z","iopub.status.idle":"2022-11-07T05:31:56.548519Z","shell.execute_reply.started":"2022-11-07T05:31:44.173895Z","shell.execute_reply":"2022-11-07T05:31:56.547577Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!unzip ../input/dogs-vs-cats-redux-kernels-edition/test.zip -d . #test","metadata":{"_uuid":"bf3c4c35-2d7c-49a2-a555-6733d2d779ff","_cell_guid":"8c09a5e2-035d-42dd-9abd-8f47bdbb0b6b","execution":{"iopub.status.busy":"2022-11-07T05:31:56.550076Z","iopub.execute_input":"2022-11-07T05:31:56.550341Z","iopub.status.idle":"2022-11-07T05:32:03.318975Z","shell.execute_reply.started":"2022-11-07T05:31:56.550313Z","shell.execute_reply":"2022-11-07T05:32:03.318091Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.listdir('../working/train')","metadata":{"_uuid":"d15ec89a-faa2-4fe6-b412-6546dcbdc98c","_cell_guid":"4eab4c63-5794-48da-82af-1f876823cbfd","execution":{"iopub.status.busy":"2022-11-07T05:32:03.320956Z","iopub.execute_input":"2022-11-07T05:32:03.321211Z","iopub.status.idle":"2022-11-07T05:32:03.355497Z","shell.execute_reply.started":"2022-11-07T05:32:03.321181Z","shell.execute_reply":"2022-11-07T05:32:03.354531Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.listdir('../working/test')","metadata":{"_uuid":"c9b50c45-4408-4060-963e-c69391aa3d98","_cell_guid":"d44ec8b8-b626-4ba1-af8c-46d6740c0ef6","execution":{"iopub.status.busy":"2022-11-07T05:32:03.358131Z","iopub.execute_input":"2022-11-07T05:32:03.358466Z","iopub.status.idle":"2022-11-07T05:32:03.383308Z","shell.execute_reply.started":"2022-11-07T05:32:03.358431Z","shell.execute_reply":"2022-11-07T05:32:03.382399Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test1=sub.copy()","metadata":{"_uuid":"bba945a5-e5ea-4c43-ba16-cd3a750583bc","_cell_guid":"ab79700f-b38a-4b0b-8555-107e60d4cb8d","execution":{"iopub.status.busy":"2022-11-07T05:32:03.385224Z","iopub.execute_input":"2022-11-07T05:32:03.385562Z","iopub.status.idle":"2022-11-07T05:32:03.390312Z","shell.execute_reply.started":"2022-11-07T05:32:03.385529Z","shell.execute_reply":"2022-11-07T05:32:03.389343Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test1['id']=test1['id']+'.jpg'","metadata":{"_uuid":"d7dda6fd-675c-4dd6-92c3-e1b73406e80d","_cell_guid":"35f15633-31fc-423b-a877-1510f70cc9ad","execution":{"iopub.status.busy":"2022-11-07T05:32:03.391664Z","iopub.execute_input":"2022-11-07T05:32:03.392007Z","iopub.status.idle":"2022-11-07T05:32:03.417795Z","shell.execute_reply.started":"2022-11-07T05:32:03.391974Z","shell.execute_reply":"2022-11-07T05:32:03.417088Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test1","metadata":{"_uuid":"b4746b36-e1de-4c46-ac1f-3a457208420c","_cell_guid":"75fa4c72-1c3a-4b2d-8500-3a7e9fa8e7be","execution":{"iopub.status.busy":"2022-11-07T05:32:03.420311Z","iopub.execute_input":"2022-11-07T05:32:03.420648Z","iopub.status.idle":"2022-11-07T05:32:03.433176Z","shell.execute_reply.started":"2022-11-07T05:32:03.420622Z","shell.execute_reply":"2022-11-07T05:32:03.432687Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train=pd.DataFrame()\ntest=pd.DataFrame()","metadata":{"_uuid":"a198223c-d71d-46a2-b0d5-3e725060bded","_cell_guid":"62e49348-9cd6-4a6d-af10-5a8e5a72c68d","execution":{"iopub.status.busy":"2022-11-07T05:32:03.433985Z","iopub.execute_input":"2022-11-07T05:32:03.434602Z","iopub.status.idle":"2022-11-07T05:32:03.439283Z","shell.execute_reply.started":"2022-11-07T05:32:03.434568Z","shell.execute_reply":"2022-11-07T05:32:03.438456Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['file']=os.listdir('../working/train')\ntest['file']=os.listdir('../working/test')","metadata":{"_uuid":"7fc5002c-8a77-4328-95bd-26b4c4e39ad6","_cell_guid":"fe26e76f-9b22-47a0-bd62-042a83006399","execution":{"iopub.status.busy":"2022-11-07T05:32:03.440458Z","iopub.execute_input":"2022-11-07T05:32:03.441003Z","iopub.status.idle":"2022-11-07T05:32:03.474271Z","shell.execute_reply.started":"2022-11-07T05:32:03.440969Z","shell.execute_reply":"2022-11-07T05:32:03.473774Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test","metadata":{"_uuid":"5e9999c9-82aa-40ad-bb3c-13a980fd6289","_cell_guid":"deedcf25-7bd0-4ad3-8714-97dbffe4bdf5","execution":{"iopub.status.busy":"2022-11-07T05:32:03.475274Z","iopub.execute_input":"2022-11-07T05:32:03.475749Z","iopub.status.idle":"2022-11-07T05:32:03.485638Z","shell.execute_reply.started":"2022-11-07T05:32:03.475714Z","shell.execute_reply":"2022-11-07T05:32:03.484729Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train","metadata":{"_uuid":"c61a58f7-4998-4bb1-b2aa-b7f315bec995","_cell_guid":"130fea12-3786-459a-a957-e22e2c438b98","execution":{"iopub.status.busy":"2022-11-07T05:32:03.486951Z","iopub.execute_input":"2022-11-07T05:32:03.487306Z","iopub.status.idle":"2022-11-07T05:32:03.499578Z","shell.execute_reply.started":"2022-11-07T05:32:03.487274Z","shell.execute_reply":"2022-11-07T05:32:03.498819Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pwd","metadata":{"_uuid":"a2938608-8678-43db-94c6-178e8637fefc","_cell_guid":"acf2f2bf-b68b-4283-a380-616b6b38b59a","execution":{"iopub.status.busy":"2022-11-07T05:32:03.500561Z","iopub.execute_input":"2022-11-07T05:32:03.501531Z","iopub.status.idle":"2022-11-07T05:32:04.436511Z","shell.execute_reply.started":"2022-11-07T05:32:03.501496Z","shell.execute_reply":"2022-11-07T05:32:04.435589Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!ls","metadata":{"_uuid":"41ef8ae6-b037-416b-9ddd-2b22fc2089d9","_cell_guid":"69592d16-1317-4ea8-ad9a-236cf9d5ddba","execution":{"iopub.status.busy":"2022-11-07T05:32:04.439672Z","iopub.execute_input":"2022-11-07T05:32:04.439945Z","iopub.status.idle":"2022-11-07T05:32:05.376939Z","shell.execute_reply.started":"2022-11-07T05:32:04.439916Z","shell.execute_reply":"2022-11-07T05:32:05.376030Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dir ='../working/train/' \ntest_dir='../working/test/'","metadata":{"_uuid":"14902f90-05e5-46fe-813d-01b0aa091868","_cell_guid":"9b7414f2-db77-46dc-9198-e98bc7a69c7d","execution":{"iopub.status.busy":"2022-11-07T05:32:05.379601Z","iopub.execute_input":"2022-11-07T05:32:05.379931Z","iopub.status.idle":"2022-11-07T05:32:05.384353Z","shell.execute_reply.started":"2022-11-07T05:32:05.379897Z","shell.execute_reply":"2022-11-07T05:32:05.383489Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dir","metadata":{"_uuid":"6189568f-2705-4292-9e25-8eddc28e57af","_cell_guid":"50abcaea-de5d-4492-8126-5ab3826c60c4","execution":{"iopub.status.busy":"2022-11-07T05:32:05.387153Z","iopub.execute_input":"2022-11-07T05:32:05.387794Z","iopub.status.idle":"2022-11-07T05:32:05.400617Z","shell.execute_reply.started":"2022-11-07T05:32:05.387751Z","shell.execute_reply":"2022-11-07T05:32:05.399772Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#file 확장자 (jpg)확인\npath_dir = test_dir #train_dir\nfile_list = os.listdir(path_dir)\nnjpg_list=[]\nfor i in file_list:\n    a = i.split(\".\")\n    if a[-1] != \"jpg\":\n        njpg_list.append(i)\nprint(njpg_list)","metadata":{"_uuid":"8ffba263-c995-4cc0-8e30-f8076540a828","_cell_guid":"610b40ca-7472-441a-a0f9-c5921787c0c6","execution":{"iopub.status.busy":"2022-11-07T05:32:05.404336Z","iopub.execute_input":"2022-11-07T05:32:05.404943Z","iopub.status.idle":"2022-11-07T05:32:05.421919Z","shell.execute_reply.started":"2022-11-07T05:32:05.404908Z","shell.execute_reply":"2022-11-07T05:32:05.421108Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# check image\nfrom PIL import Image\nimport matplotlib.pyplot as plt\nimg1 = Image.open(train_dir + '/cat.1022.jpg') \nimg2 = Image.open(train_dir + '/dog.11329.jpg')\n\nplt.imshow(img1)\nplt.show()\nplt.imshow(img2)\nplt.show()\nprint(img1.size, img2.size)","metadata":{"_uuid":"8a2d34e3-cfe5-411d-8717-f30a05013107","_cell_guid":"2611229d-3b50-48ee-90f2-3496f4c41421","execution":{"iopub.status.busy":"2022-11-07T05:32:05.423768Z","iopub.execute_input":"2022-11-07T05:32:05.424282Z","iopub.status.idle":"2022-11-07T05:32:05.728807Z","shell.execute_reply.started":"2022-11-07T05:32:05.424247Z","shell.execute_reply":"2022-11-07T05:32:05.727870Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### train 파일명에서 label 추출","metadata":{"_uuid":"9f130366-a16a-4527-87c7-6b99ee089da4","_cell_guid":"49e0c672-f8be-4c39-9729-59fd17f98ae4","trusted":true}},{"cell_type":"code","source":"# make target (1 = dog, 0 = cat)\npath_dir = train_dir\nfile_list = os.listdir(path_dir)\ntarget=[]\nprint(len(file_list))\n\nfor file in file_list:\n    a = file.split(\".\")\n    if a[0] == \"dog\":\n        target.append(1)\n    else:\n        target.append(0)\n\nprint(a)","metadata":{"_uuid":"fa4ffca8-03d7-49ec-8709-a93434e60a6b","_cell_guid":"49ff18c7-d948-4a2f-a859-c79b75db0a88","execution":{"iopub.status.busy":"2022-11-07T05:32:05.739291Z","iopub.execute_input":"2022-11-07T05:32:05.739705Z","iopub.status.idle":"2022-11-07T05:32:05.771953Z","shell.execute_reply.started":"2022-11-07T05:32:05.739670Z","shell.execute_reply":"2022-11-07T05:32:05.771089Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"target","metadata":{"_uuid":"ede3fd2a-a5a4-440b-aa06-7db318149e6a","_cell_guid":"6248e629-5aa6-4657-8487-8956e5463388","execution":{"iopub.status.busy":"2022-11-07T05:32:05.773077Z","iopub.execute_input":"2022-11-07T05:32:05.773424Z","iopub.status.idle":"2022-11-07T05:32:05.792228Z","shell.execute_reply.started":"2022-11-07T05:32:05.773390Z","shell.execute_reply":"2022-11-07T05:32:05.791345Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['label']=target","metadata":{"_uuid":"33df66f2-b6c3-4c5b-9783-3b1924df75fb","_cell_guid":"6d23ae9e-6644-47c3-a059-e58e771eaf24","execution":{"iopub.status.busy":"2022-11-07T05:32:05.793524Z","iopub.execute_input":"2022-11-07T05:32:05.793946Z","iopub.status.idle":"2022-11-07T05:32:05.807647Z","shell.execute_reply.started":"2022-11-07T05:32:05.793912Z","shell.execute_reply":"2022-11-07T05:32:05.806933Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.dtypes","metadata":{"_uuid":"bc648be1-6c5b-4c63-94c0-2dd053359c91","_cell_guid":"2238cb10-40bc-4805-9646-1f82ddea7cfb","execution":{"iopub.status.busy":"2022-11-07T05:32:05.808773Z","iopub.execute_input":"2022-11-07T05:32:05.809141Z","iopub.status.idle":"2022-11-07T05:32:05.819590Z","shell.execute_reply.started":"2022-11-07T05:32:05.809097Z","shell.execute_reply":"2022-11-07T05:32:05.818639Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['label']=train['label'].astype('str')","metadata":{"_uuid":"16240556-08e9-421f-8a12-bb8f30c19710","_cell_guid":"90d298ef-26ba-46a8-8555-1881e7c9b192","execution":{"iopub.status.busy":"2022-11-07T05:32:05.820942Z","iopub.execute_input":"2022-11-07T05:32:05.821303Z","iopub.status.idle":"2022-11-07T05:32:05.845131Z","shell.execute_reply.started":"2022-11-07T05:32:05.821267Z","shell.execute_reply":"2022-11-07T05:32:05.844333Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.dtypes","metadata":{"_uuid":"b977e2fd-31c3-4a45-b801-60108c8d6fcc","_cell_guid":"a02d37b7-fc14-4b12-8e2f-b4d9114ac4a2","execution":{"iopub.status.busy":"2022-11-07T05:32:05.846233Z","iopub.execute_input":"2022-11-07T05:32:05.846618Z","iopub.status.idle":"2022-11-07T05:32:05.857223Z","shell.execute_reply.started":"2022-11-07T05:32:05.846592Z","shell.execute_reply":"2022-11-07T05:32:05.856454Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Image Loader","metadata":{"_uuid":"365a1b2d-df0e-4511-9ca9-83ffce39a37a","_cell_guid":"0728855a-f7bf-4f6b-b3b7-114016c3c485","trusted":true}},{"cell_type":"code","source":"input_sh=(256, 256, 3)\ninput_size=(256, 256)","metadata":{"_uuid":"757617e6-ed39-425f-a46f-8179c7719d16","_cell_guid":"8f0a8d25-9b1d-4238-a4dd-329a2d698fa0","execution":{"iopub.status.busy":"2022-11-07T05:32:05.858595Z","iopub.execute_input":"2022-11-07T05:32:05.858909Z","iopub.status.idle":"2022-11-07T05:32:05.865132Z","shell.execute_reply.started":"2022-11-07T05:32:05.858874Z","shell.execute_reply":"2022-11-07T05:32:05.864333Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train","metadata":{"execution":{"iopub.status.busy":"2022-11-07T05:32:05.866852Z","iopub.execute_input":"2022-11-07T05:32:05.867249Z","iopub.status.idle":"2022-11-07T05:32:05.882285Z","shell.execute_reply.started":"2022-11-07T05:32:05.867207Z","shell.execute_reply":"2022-11-07T05:32:05.881454Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import ImageDataGenerator \nbs=16 #batch size 너무 크면 학습이 어려움\n\ndatagen = ImageDataGenerator(rescale=1./255, validation_split=0.2)\n# train data\ntrain0 = datagen.flow_from_dataframe(\n    train, directory=train_dir, \n    x_col=train.columns[0], y_col=train.columns[1], \n    target_size=input_size, \n    subset='training',\n    class_mode=\"binary\", batch_size=bs)\n# validation data\nval0 = datagen.flow_from_dataframe(\n    train,directory=train_dir,\n    subset='validation',\n    x_col=train.columns[0],y_col=train.columns[1],\n    target_size=input_size,\n    class_mode=\"binary\", batch_size=bs)\n# test data\ntest0 = datagen.flow_from_dataframe(\n    test1, directory=test_dir, \n    x_col=test1.columns[0], y_col=None, \n    target_size=input_size, \n    shuffle=False, class_mode=None)","metadata":{"_uuid":"4fa0ec7a-3339-4f82-b751-4706a64e2aeb","_cell_guid":"1b4c8afc-40bb-4451-982e-d40de4ed99a4","execution":{"iopub.status.busy":"2022-11-07T05:32:05.883553Z","iopub.execute_input":"2022-11-07T05:32:05.883907Z","iopub.status.idle":"2022-11-07T05:32:11.572036Z","shell.execute_reply.started":"2022-11-07T05:32:05.883872Z","shell.execute_reply":"2022-11-07T05:32:11.571006Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# datagen 확인\nx_batch, y_batch = train0.next()\nfor i in range(0, bs):\n    plt.imshow(((x_batch[i])*255).astype('int'))\n    plt.show()\n    print(x_batch[i].shape, y_batch[i])","metadata":{"_uuid":"178cf2fc-d241-43e2-9f01-1d977f81ca18","_cell_guid":"7a156617-f8cf-4efa-af0f-8c5822b9d81d","execution":{"iopub.status.busy":"2022-11-07T05:32:11.573401Z","iopub.execute_input":"2022-11-07T05:32:11.573936Z","iopub.status.idle":"2022-11-07T05:32:14.241045Z","shell.execute_reply.started":"2022-11-07T05:32:11.573896Z","shell.execute_reply":"2022-11-07T05:32:14.240189Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### model 설정","metadata":{"_uuid":"0164df4e-9d7a-46aa-b92a-edd52e13c0cc","_cell_guid":"2e1a32f7-c96a-4638-ac8f-7480ba9d5e8f","trusted":true}},{"cell_type":"code","source":"from tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense, Conv2D, MaxPooling2D, Flatten\n#from tensorflow.keras.layers import Dropout, BatchNormalization\n\nmodel = Sequential()\nmodel.add(Conv2D(8, kernel_size=(3, 3), strides=(1, 1), padding='same', activation='relu', input_shape=input_sh))\nmodel.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))\nmodel.add(Conv2D(8, (3, 3), activation='relu'))\n#model.add(Conv2D(8, (3, 3), activation='relu'))\nmodel.add(MaxPooling2D(pool_size=(2, 2)))\nmodel.add(Conv2D(16, (3, 3), activation='relu'))\n#model.add(Conv2D(16, (3, 3), activation='relu'))\nmodel.add(MaxPooling2D(pool_size=(2, 2)))\nmodel.add(Conv2D(32, (3, 3), activation='relu'))\n#model.add(Conv2D(32, (3, 3), activation='relu'))\nmodel.add(MaxPooling2D(pool_size=(2, 2)))\n\nmodel.add(Conv2D(64, (3, 3), activation='relu'))\n#model.add(Conv2D(64, (3, 3), activation='relu'))\nmodel.add(MaxPooling2D(pool_size=(2, 2)))\n#model.add(Dropout(0.25))\n\nmodel.add(Flatten())\nmodel.add(Dense(400, activation='relu'))\n#model.add(Dropout(0.5))\nmodel.add(Dense(80, activation='relu'))\n#model.add(Dropout(0.5))\nmodel.add(Dense(16, activation='relu'))\nmodel.add(Dense(1, activation='sigmoid')) \nmodel.compile(optimizer='adam', loss='binary_crossentropy', metrics=['acc'])\nmodel.summary()","metadata":{"_uuid":"58b97b5f-3f91-4dac-a880-43f06a420db0","_cell_guid":"78a4d773-6a8f-417a-9803-706338b9cc43","execution":{"iopub.status.busy":"2022-11-07T05:32:14.242325Z","iopub.execute_input":"2022-11-07T05:32:14.242845Z","iopub.status.idle":"2022-11-07T05:32:16.610516Z","shell.execute_reply.started":"2022-11-07T05:32:14.242808Z","shell.execute_reply":"2022-11-07T05:32:16.609715Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.utils import plot_model\nplot_model(model)","metadata":{"execution":{"iopub.status.busy":"2022-11-07T05:32:16.611770Z","iopub.execute_input":"2022-11-07T05:32:16.612089Z","iopub.status.idle":"2022-11-07T05:32:17.122361Z","shell.execute_reply.started":"2022-11-07T05:32:16.612054Z","shell.execute_reply":"2022-11-07T05:32:17.121396Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history=model.fit(train0, epochs=5, validation_data=val0)","metadata":{"_uuid":"ee9094ab-fbf6-42ee-b2b9-09cf39642165","_cell_guid":"b35f446f-7640-405e-b743-546f96703146","execution":{"iopub.status.busy":"2022-11-07T05:32:17.124113Z","iopub.execute_input":"2022-11-07T05:32:17.124722Z","iopub.status.idle":"2022-11-07T05:39:24.080911Z","shell.execute_reply.started":"2022-11-07T05:32:17.124671Z","shell.execute_reply":"2022-11-07T05:39:24.079943Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result=model.predict(test0)","metadata":{"_uuid":"400f1994-275c-43f0-8a37-d0d8ef12292e","_cell_guid":"685e096c-6432-4467-8ee7-4a8976ac24ba","execution":{"iopub.status.busy":"2022-11-07T05:39:24.084157Z","iopub.execute_input":"2022-11-07T05:39:24.084443Z","iopub.status.idle":"2022-11-07T05:40:05.773063Z","shell.execute_reply.started":"2022-11-07T05:39:24.084409Z","shell.execute_reply":"2022-11-07T05:40:05.772025Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#result1= 1*(result >0.5 )","metadata":{"_uuid":"934e5741-5c4c-434e-abfd-2c134ce89987","_cell_guid":"abc2f08c-fe12-4d77-b0bf-6459b6d5ef0d","execution":{"iopub.status.busy":"2022-11-07T05:40:05.777441Z","iopub.execute_input":"2022-11-07T05:40:05.777776Z","iopub.status.idle":"2022-11-07T05:40:05.785105Z","shell.execute_reply.started":"2022-11-07T05:40:05.777742Z","shell.execute_reply":"2022-11-07T05:40:05.784066Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result2=pd.DataFrame(result)","metadata":{"_uuid":"bc5b9129-d69b-473b-ada5-8579c3bbe249","_cell_guid":"ac224f25-4c53-4671-bff9-7a45cce763e3","execution":{"iopub.status.busy":"2022-11-07T05:40:05.788159Z","iopub.execute_input":"2022-11-07T05:40:05.790599Z","iopub.status.idle":"2022-11-07T05:40:05.797002Z","shell.execute_reply.started":"2022-11-07T05:40:05.790564Z","shell.execute_reply":"2022-11-07T05:40:05.796216Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result2[0].value_counts()","metadata":{"_uuid":"7d4a3a7e-521c-4107-87b3-60672ed27f55","_cell_guid":"9212d24e-7070-4087-9931-d515a55d2a1d","execution":{"iopub.status.busy":"2022-11-07T05:40:05.798207Z","iopub.execute_input":"2022-11-07T05:40:05.798784Z","iopub.status.idle":"2022-11-07T05:40:05.813632Z","shell.execute_reply.started":"2022-11-07T05:40:05.798749Z","shell.execute_reply":"2022-11-07T05:40:05.812934Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub['label']=result","metadata":{"_uuid":"56e2bd29-8e54-422c-ba3b-20a496266d28","_cell_guid":"44caee3b-aecb-4f24-9afa-8d345a7f0906","execution":{"iopub.status.busy":"2022-11-07T05:40:05.817098Z","iopub.execute_input":"2022-11-07T05:40:05.818938Z","iopub.status.idle":"2022-11-07T05:40:05.825241Z","shell.execute_reply.started":"2022-11-07T05:40:05.818899Z","shell.execute_reply":"2022-11-07T05:40:05.824445Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub.to_csv('sub2.csv', index=False)","metadata":{"_uuid":"3494cad7-a58a-4315-ab1c-228aa2f1cf42","_cell_guid":"a05cd86c-7843-4ec0-b034-937a967ddf64","execution":{"iopub.status.busy":"2022-11-07T05:40:05.828858Z","iopub.execute_input":"2022-11-07T05:40:05.831119Z","iopub.status.idle":"2022-11-07T05:40:06.102617Z","shell.execute_reply.started":"2022-11-07T05:40:05.831081Z","shell.execute_reply":"2022-11-07T05:40:06.101810Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 그래프 그리기","metadata":{}},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\nacc = history.history['acc']\nval_acc = history.history['val_acc']\nloss = history.history['loss']\nval_loss = history.history['val_loss']\n\nepochs = range(1, len(acc) + 1)\n\nfig= plt.figure(figsize=(10, 5))\nplt.xlim(1, len(acc)+1)\nplt.plot(epochs, acc, 'b', label='Training acc')\nplt.plot(epochs, val_acc, 'r', label='Validation acc')\nplt.title('Training and validation acc')\nplt.legend()\n\nfig= plt.figure(figsize=(10, 5))\nplt.xlim(1, len(acc)+1)\nplt.plot(epochs, loss, 'b', label='Training loss')\nplt.plot(epochs, val_loss, 'r', label='Validation loss')\nplt.title('Training and validation loss')\nplt.legend()","metadata":{"_uuid":"39c9cee8-6dcd-4b2f-b10e-ee3886d3a4d0","_cell_guid":"20578a56-3dd2-422e-8aab-9d91d16cae19","execution":{"iopub.status.busy":"2022-11-07T05:40:06.103900Z","iopub.execute_input":"2022-11-07T05:40:06.104242Z","iopub.status.idle":"2022-11-07T05:40:06.382461Z","shell.execute_reply.started":"2022-11-07T05:40:06.104189Z","shell.execute_reply":"2022-11-07T05:40:06.381514Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Transfer Learning\n### pretrained model\n가벼운 모델로 MobileNetV2, EfficientNetB1~B3 등이 있고, Xception 모델로도 학습이 잘된다.","metadata":{"_uuid":"9c91e04b-a9bd-4520-87e7-c20329033301","_cell_guid":"9bc660c8-e8dd-4245-b244-57d50bf37035","trusted":true}},{"cell_type":"code","source":"from tensorflow.keras.applications import MobileNetV2\nfrom tensorflow.keras import Model, Input\nfrom tensorflow.keras.layers import GlobalAveragePooling2D, Dropout","metadata":{"_uuid":"87c61e92-5514-4c9f-824e-a9ef01a5b4c1","_cell_guid":"8d9b1bf5-2366-4d58-b84e-e55e41ee76a0","execution":{"iopub.status.busy":"2022-11-07T05:40:06.383938Z","iopub.execute_input":"2022-11-07T05:40:06.384312Z","iopub.status.idle":"2022-11-07T05:40:06.389011Z","shell.execute_reply.started":"2022-11-07T05:40:06.384273Z","shell.execute_reply":"2022-11-07T05:40:06.388007Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"base=MobileNetV2(include_top=False,\n                 weights=\"imagenet\",\n                 input_shape=input_sh)\n\nx = base.output\nx = GlobalAveragePooling2D()(x)\nx = Dropout(0.5)(x)\nx=Dense(128, activation='relu')(x)\nx=Dense(48, activation='relu')(x)\nx=Dense(8, activation='relu')(x)\npredictions = Dense(1, activation= 'sigmoid')(x)\nmodelr = Model(inputs = base.input, outputs = predictions)\nmodelr.summary()\nmodelr.compile(optimizer='adam', \n             loss='binary_crossentropy', \n              metrics=['acc'])","metadata":{"_uuid":"b0d9fd9e-1f62-49ca-9a27-a34c686d7bac","_cell_guid":"9ed15a8d-e759-4f55-b93e-bcc3f37afe27","execution":{"iopub.status.busy":"2022-11-07T05:40:06.390353Z","iopub.execute_input":"2022-11-07T05:40:06.390825Z","iopub.status.idle":"2022-11-07T05:40:07.825671Z","shell.execute_reply.started":"2022-11-07T05:40:06.390791Z","shell.execute_reply":"2022-11-07T05:40:07.824818Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"historyr=modelr.fit(train0, epochs=5, validation_data=val0)","metadata":{"_uuid":"d73fe5e2-e327-49cd-a582-c3ba4b127856","_cell_guid":"ab79f66d-36d9-4b89-9f15-77b65746f94e","execution":{"iopub.status.busy":"2022-11-07T05:40:07.826761Z","iopub.execute_input":"2022-11-07T05:40:07.827109Z","iopub.status.idle":"2022-11-07T05:54:47.621966Z","shell.execute_reply.started":"2022-11-07T05:40:07.827072Z","shell.execute_reply":"2022-11-07T05:54:47.621203Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"resultr=modelr.predict(test0)","metadata":{"_uuid":"17be8c75-1c10-40fb-9164-393c03a4e365","_cell_guid":"93a59884-4c04-44a3-821a-88455844ea13","execution":{"iopub.status.busy":"2022-11-07T05:54:47.623415Z","iopub.execute_input":"2022-11-07T05:54:47.623776Z","iopub.status.idle":"2022-11-07T05:55:30.974268Z","shell.execute_reply.started":"2022-11-07T05:54:47.623737Z","shell.execute_reply":"2022-11-07T05:55:30.973404Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"subr=sub.copy()\nsubr['label']=resultr","metadata":{"_uuid":"540c88a7-54be-4bdb-8ea8-dc07f2ecb3d0","_cell_guid":"92fb7148-8c65-452f-a91c-ca02ff018a18","execution":{"iopub.status.busy":"2022-11-07T05:55:39.615250Z","iopub.execute_input":"2022-11-07T05:55:39.615574Z","iopub.status.idle":"2022-11-07T05:55:39.620628Z","shell.execute_reply.started":"2022-11-07T05:55:39.615544Z","shell.execute_reply":"2022-11-07T05:55:39.619585Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"subr.to_csv('subr.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2022-11-07T05:55:41.252234Z","iopub.execute_input":"2022-11-07T05:55:41.252565Z","iopub.status.idle":"2022-11-07T05:55:41.289984Z","shell.execute_reply.started":"2022-11-07T05:55:41.252533Z","shell.execute_reply":"2022-11-07T05:55:41.289212Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#result1= 1*(result >0.5 ) 이 결과와 비교해서 더 점수가 잘 나오는 방법을 선택","metadata":{},"execution_count":null,"outputs":[]}]}