{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":5441,"databundleVersionId":38425,"sourceType":"competition"}],"dockerImageVersionId":30698,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"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":"2024-05-28T00:03:30.278526Z","iopub.execute_input":"2024-05-28T00:03:30.279000Z","iopub.status.idle":"2024-05-28T00:03:31.616155Z","shell.execute_reply.started":"2024-05-28T00:03:30.278951Z","shell.execute_reply":"2024-05-28T00:03:31.614456Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train.zip 과 test.zip 압축파일 풀어주기\n!unzip -q -o /kaggle/input/dogs-vs-cats-redux-kernels-edition/train.zip\n!unzip -q -o /kaggle/input/dogs-vs-cats-redux-kernels-edition/test.zip","metadata":{"execution":{"iopub.status.busy":"2024-05-28T00:03:31.618932Z","iopub.execute_input":"2024-05-28T00:03:31.619661Z","iopub.status.idle":"2024-05-28T00:03:52.798522Z","shell.execute_reply.started":"2024-05-28T00:03:31.619611Z","shell.execute_reply":"2024-05-28T00:03:52.796934Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Python glob 모듈의 glob 함수 이용하여 train/...jpg 파일명들 리스트로 변환\nimport glob\nglob.glob('train/*')[:5]","metadata":{"execution":{"iopub.status.busy":"2024-05-28T00:03:52.807471Z","iopub.execute_input":"2024-05-28T00:03:52.808028Z","iopub.status.idle":"2024-05-28T00:03:52.934423Z","shell.execute_reply.started":"2024-05-28T00:03:52.807972Z","shell.execute_reply":"2024-05-28T00:03:52.932896Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# python 이미지 라이브러리 pillow (PIL) 이용하여 해당 경로의 이미지 열어볼 수 있음\nfrom PIL import Image\nImage.open('train/cat.5978.jpg')","metadata":{"execution":{"iopub.status.busy":"2024-05-28T00:03:52.936042Z","iopub.execute_input":"2024-05-28T00:03:52.936489Z","iopub.status.idle":"2024-05-28T00:03:53.050422Z","shell.execute_reply.started":"2024-05-28T00:03:52.936453Z","shell.execute_reply":"2024-05-28T00:03:53.048983Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 데이터 프레임 만들기","metadata":{}},{"cell_type":"code","source":"train = pd.DataFrame({'path': glob.glob('train/*')})\ntrain","metadata":{"execution":{"iopub.status.busy":"2024-05-28T00:03:53.052061Z","iopub.execute_input":"2024-05-28T00:03:53.052507Z","iopub.status.idle":"2024-05-28T00:03:53.200478Z","shell.execute_reply.started":"2024-05-28T00:03:53.052470Z","shell.execute_reply":"2024-05-28T00:03:53.198773Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"- path 값 보면 cat 또는 dog 로 이미지의 정답 값이 path 값 안에 포함되어있음.\n    - 이 값을 편집하여 추출해 라벨링 함","metadata":{}},{"cell_type":"code","source":"train['label'] = train['path'].apply(lambda x:x.split('/')[-1].split('.')[0])\ntrain","metadata":{"execution":{"iopub.status.busy":"2024-05-28T00:03:53.202450Z","iopub.execute_input":"2024-05-28T00:03:53.202983Z","iopub.status.idle":"2024-05-28T00:03:53.245484Z","shell.execute_reply.started":"2024-05-28T00:03:53.202934Z","shell.execute_reply":"2024-05-28T00:03:53.243832Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 이미지 제네레이터\n```\n- 많은 이미지를 전부 다 돌리고 늘리고 한 결과들을 RAM에 캐시할 수 없을 것임.\n- 메모리상엔 기본 이미지만 올려놓고 학습 시 배치단위로 입력될 때 살짝 변조해서 데이터를 입력해주는 방식으로 \n  큰 메모리 손실 없이 데이터를 학습하게 하는 생성기임\n\n- karas 에서 제공하는 이미지 제네레이터를 이용함\n```","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import ImageDataGenerator\nidg = ImageDataGenerator()\n\ntrain_generator = idg.flow_from_dataframe(train, x_col='path', y_col='label', \n                                          batch_size=64, target_size=(100,100))  # batch 단위로 나눠서 함","metadata":{"execution":{"iopub.status.busy":"2024-05-28T00:03:53.247445Z","iopub.execute_input":"2024-05-28T00:03:53.247926Z","iopub.status.idle":"2024-05-28T00:04:10.302845Z","shell.execute_reply.started":"2024-05-28T00:03:53.247890Z","shell.execute_reply":"2024-05-28T00:04:10.301243Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 모델 정의","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras import *\nfrom tensorflow.keras.layers import *\n\nfrom tensorflow.keras.applications.inception_v3 import InceptionV3\n\nmodel = Sequential()\n\niv = InceptionV3(include_top=False, pooling='avg')\n\nmodel.add(iv)\nmodel.add(Dense(2, activation='softmax'))","metadata":{"execution":{"iopub.status.busy":"2024-05-28T00:06:12.888037Z","iopub.execute_input":"2024-05-28T00:06:12.888499Z","iopub.status.idle":"2024-05-28T00:06:19.522878Z","shell.execute_reply.started":"2024-05-28T00:06:12.888465Z","shell.execute_reply":"2024-05-28T00:06:19.521735Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"- InceptionV3\n: 48개 계층으로 구성된 CNN(Convolutional Neural Network, 컨벌루션 신경망, 합성곱 신경망) 임.\n    - include_top : 네트워크 마지막 계층으로 맨 위에 완전 연결 계층을 포함할지 여부를 나타내는 부울. 기본값은 True\n    - pooling : include_top 이 False 인 경우 기능 추출을 위한 선택적 풀링 모드\n    - None (기본값)은 모델의 출력이 마지막 컨벌루션 블록의 4D 텐서 출력이됨을 의미\n    - avg 는 글로벌 평균 풀링이 마지막 컨볼루션 블록의 출력에 적용되므로 모델의 출력이 2D 텐서가 됨을 의미함\n    - max 는 글로벌 최대 풀링이 적용됨을 의미함","metadata":{}},{"cell_type":"markdown","source":"- Dense\n: units 양의 정수, 출력 공간의 차원, activation 사용할 활성화 기능. 아무것도 지정하지 않으면 활성화가 적용되지 않음 (ex. \"선형\"활성화 : a(x) = x)\n\n- softmax\n: 입력받은 값을 출력으로 0 ~ 1 사이의 값으로 모두 정규화하며, 출력 값들의 총 합은 항상 1이 되는 특성을 가진 함수임\n\n- Keras Sequential model\n: 레이어를 선형으로 연결하여 구성함. 레이어 인스턴스를 생성자에게 넘겨줌으로써 Sequential 모델을 구성할 수 있음","metadata":{}},{"cell_type":"code","source":"model.compile(metrics=['acc'], loss='categorical_crossentropy', optimizer='adam') \n\nmodel.fit(train_generator)","metadata":{"execution":{"iopub.status.busy":"2024-05-28T00:14:09.764842Z","iopub.execute_input":"2024-05-28T00:14:09.765309Z","iopub.status.idle":"2024-05-28T00:32:54.404610Z","shell.execute_reply.started":"2024-05-28T00:14:09.765274Z","shell.execute_reply":"2024-05-28T00:32:54.402531Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### fit( )\n- X : 입력 데이터\n- Y : 결과(Label 값) 데이터\n- batch_size : 한 번에 학습할 때 사용하는 데이터 개수\n- epochs : 학습 데이터 반복 횟수","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}