{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.11.11","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":31040,"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","trusted":true,"execution":{"iopub.status.busy":"2025-07-04T09:14:09.403098Z","iopub.execute_input":"2025-07-04T09:14:09.403574Z","iopub.status.idle":"2025-07-04T09:14:09.424376Z","shell.execute_reply.started":"2025-07-04T09:14:09.403532Z","shell.execute_reply":"2025-07-04T09:14:09.422545Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"データのダウンロード","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport os, glob, time, copy, zipfile\nfrom PIL import Image\nfrom sklearn.model_selection import train_test_split\nfrom tqdm import tqdm\nimport torch\nimport torch.nn as nn\nimport torch.optim as optim\nimport torch.utils.data as data\nimport torch.nn.functional as F\nfrom torchvision import models, transforms","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-04T09:14:13.167614Z","iopub.execute_input":"2025-07-04T09:14:13.167941Z","iopub.status.idle":"2025-07-04T09:14:13.174871Z","shell.execute_reply.started":"2025-07-04T09:14:13.167918Z","shell.execute_reply":"2025-07-04T09:14:13.173712Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"動かすために必要なライブラリーをインポート","metadata":{}},{"cell_type":"code","source":"# データを初期化\n!rm -rf ../data","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-04T09:14:16.133243Z","iopub.execute_input":"2025-07-04T09:14:16.133565Z","iopub.status.idle":"2025-07-04T09:14:16.335049Z","shell.execute_reply.started":"2025-07-04T09:14:16.133542Z","shell.execute_reply":"2025-07-04T09:14:16.333321Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"同じ名前のものがあったら影響がでるので","metadata":{}},{"cell_type":"code","source":"import os\nimport zipfile\nimport glob\nfrom sklearn.model_selection import train_test_split\n\n# ディレクトリ定義\nbase_dir = '/kaggle/input/dogs-vs-cats-redux-kernels-edition'\ndata_dir = '/kaggle/working/data/'\ntrain_dir = os.path.join(data_dir, 'train')\ntest_dir = os.path.join(data_dir, 'test')\n\n# 書き込み先ディレクトリ作成\nos.makedirs(data_dir, exist_ok=True)\n\n# zipファイル解凍\nwith zipfile.ZipFile(os.path.join(base_dir, 'train.zip')) as train_zip:\n    train_zip.extractall(data_dir)\nwith zipfile.ZipFile(os.path.join(base_dir, 'test.zip')) as test_zip:\n    test_zip.extractall(data_dir)\n\n# ファイル一覧取得\ntrain_list_all = glob.glob(os.path.join(train_dir, '*.jpg'))\ntest_list = glob.glob(os.path.join(test_dir, '*.jpg'))\n\n# train/val分割（再現性あり）\ntrain_list, val_list = train_test_split(train_list_all, test_size=0.2, random_state=42)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-04T09:14:19.124541Z","iopub.execute_input":"2025-07-04T09:14:19.124944Z","iopub.status.idle":"2025-07-04T09:14:33.313791Z","shell.execute_reply.started":"2025-07-04T09:14:19.124908Z","shell.execute_reply":"2025-07-04T09:14:33.312838Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"zipの解凍\n学習のためのフォルダ作成\n学習するためにテストデータ(8)、トレインデータ(2)に分ける.検証データ","metadata":{}},{"cell_type":"code","source":"import os\nimport pandas as pd\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\n\ndef get_label_from_path(path):\n    filename = os.path.basename(path)\n    return 'dog' if filename.startswith('dog') else 'cat'\n\ntrain_labels = [get_label_from_path(p) for p in train_list]\nval_labels = [get_label_from_path(p) for p in val_list]\n\ndf_train = pd.DataFrame({'filename': train_list, 'label': train_labels})\ndf_val = pd.DataFrame({'filename': val_list, 'label': val_labels})\n\nimg_size = 128\n\ntrain_datagen = ImageDataGenerator(\n    rescale=1./255,\n    horizontal_flip=True,\n    rotation_range=10\n)\nval_datagen = ImageDataGenerator(rescale=1./255)\n\ntrain_gen = train_datagen.flow_from_dataframe(\n    df_train, \n    x_col='filename',\n    y_col='label',\n    target_size=(img_size, img_size),\n    class_mode='binary',\n    batch_size=32,\n    shuffle=True\n)\nval_gen = val_datagen.flow_from_dataframe(\n    df_val,\n    x_col='filename',\n    y_col='label',\n    target_size=(img_size, img_size),\n    class_mode='binary',\n    batch_size=32,\n    shuffle=False\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-04T09:17:33.235526Z","iopub.execute_input":"2025-07-04T09:17:33.235841Z","iopub.status.idle":"2025-07-04T09:17:33.511889Z","shell.execute_reply.started":"2025-07-04T09:17:33.23582Z","shell.execute_reply":"2025-07-04T09:17:33.510588Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#ラベル作成データフレイム化\ndef get_label_from_path(path):\n    filename = os.path.basename(path)\n    return 'dog' if filename.startswith('dog') else 'cat'\n\ntrain_labels = [get_label_from_path(p) for p in train_list]\nval_labels = [get_label_from_path(p) for p in val_list]\n\ndf_train = pd.DataFrame({'filename': train_list, 'label': train_labels})\ndf_val = pd.DataFrame({'filename': val_list, 'label': val_labels})","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-04T09:36:11.082241Z","iopub.execute_input":"2025-07-04T09:36:11.083158Z","iopub.status.idle":"2025-07-04T09:36:11.121079Z","shell.execute_reply.started":"2025-07-04T09:36:11.083124Z","shell.execute_reply":"2025-07-04T09:36:11.120073Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"ラベルを作る。\n画像に犬であることを示している。","metadata":{}},{"cell_type":"code","source":"#データローダーの作成\ntrain_datagen = ImageDataGenerator(\n    rescale=1./255,\n    horizontal_flip=True,\n    rotation_range=10\n)\nval_datagen = ImageDataGenerator(rescale=1./255)\n\ntrain_gen = train_datagen.flow_from_dataframe(\n    df_train, \n    x_col='filename',\n    y_col='label',\n    target_size=(img_size, img_size),\n    class_mode='binary',\n    batch_size=8,\n    shuffle=True\n)\nval_gen = val_datagen.flow_from_dataframe(\n    df_val,\n    x_col='filename',\n    y_col='label',\n    target_size=(img_size, img_size),\n    class_mode='binary',\n    batch_size=8,\n    shuffle=False\n)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-04T09:36:16.982579Z","iopub.execute_input":"2025-07-04T09:36:16.98294Z","iopub.status.idle":"2025-07-04T09:36:17.22824Z","shell.execute_reply.started":"2025-07-04T09:36:16.982878Z","shell.execute_reply":"2025-07-04T09:36:17.227169Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"実際にAIに学習させるための最後の準備\nパラメータ設定","metadata":{}},{"cell_type":"code","source":"#EfficientNetB0でモデル構築\nfrom tensorflow.keras.applications import EfficientNetB0\nfrom tensorflow.keras.layers import GlobalAveragePooling2D, Dense, Dropout\nfrom tensorflow.keras.models import Model\nfrom tensorflow.keras.optimizers import Adam\n\nbase_model = EfficientNetB0(\n    weights='imagenet',\n    include_top=False,\n    input_shape=(img_size, img_size, 3)\n)\nbase_model.trainable = False  # 転移学習なのでまずは特徴抽出層を凍結\n\nx = GlobalAveragePooling2D()(base_model.output)\nx = Dropout(0.5)(x)\noutput = Dense(1, activation='sigmoid')(x)\nmodel = Model(inputs=base_model.input, outputs=output)\n\nmodel.compile(\n    optimizer=Adam(learning_rate=1e-3),\n    loss='binary_crossentropy',\n    metrics=['accuracy']\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-04T09:36:20.925095Z","iopub.execute_input":"2025-07-04T09:36:20.925444Z","iopub.status.idle":"2025-07-04T09:36:22.45595Z","shell.execute_reply.started":"2025-07-04T09:36:20.925421Z","shell.execute_reply":"2025-07-04T09:36:22.454928Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"使うAIの決定","metadata":{}},{"cell_type":"code","source":"#学習\nhistory = model.fit(\n    train_gen,\n    validation_data=val_gen,\n    epochs=20\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-04T09:36:33.416934Z","iopub.execute_input":"2025-07-04T09:36:33.417316Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"50回学習している\n学習回数で変わるのか試している。","metadata":{}},{"cell_type":"code","source":"#推論\ndf_test = pd.DataFrame({'filename': test_list})\n\ntest_datagen = ImageDataGenerator(rescale=1./255)\ntest_gen = test_datagen.flow_from_dataframe(\n    df_test,\n    x_col='filename',\n    target_size=(img_size, img_size),\n    class_mode=None,\n    batch_size=1,\n    shuffle=False\n)\n\npred = model.predict(test_gen, steps=len(test_gen), verbose=1)\nlabels = (pred > 0.5).astype(int).flatten()\n\nids = [int(os.path.basename(x).split('.')[0]) for x in test_list]\nsubmission = pd.DataFrame({'id': ids, 'label': labels})\nsubmission = submission.sort_values('id')\n\nsubmission.to_csv('submission.csv', index=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-04T08:18:39.958076Z","iopub.execute_input":"2025-07-04T08:18:39.958458Z","iopub.status.idle":"2025-07-04T08:24:47.509662Z","shell.execute_reply.started":"2025-07-04T08:18:39.958432Z","shell.execute_reply":"2025-07-04T08:24:47.508732Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"実際の仕分け","metadata":{}},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\n# 精度の推移\nplt.plot(history.history['accuracy'], label='train_acc')\nplt.plot(history.history['val_accuracy'], label='val_acc')\nplt.xlabel('Epoch')\nplt.ylabel('Accuracy')\nplt.legend()\nplt.show()\n\n# 損失の推移\nplt.plot(history.history['loss'], label='train_loss')\nplt.plot(history.history['val_loss'], label='val_loss')\nplt.xlabel('Epoch')\nplt.ylabel('Loss')\nplt.legend()\nplt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-04T08:25:16.814866Z","iopub.execute_input":"2025-07-04T08:25:16.815258Z","iopub.status.idle":"2025-07-04T08:25:17.16425Z","shell.execute_reply.started":"2025-07-04T08:25:16.815232Z","shell.execute_reply":"2025-07-04T08:25:17.163346Z"}},"outputs":[],"execution_count":null}]}