{"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":3362,"databundleVersionId":31148,"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-07T01:32:52.840816Z","iopub.execute_input":"2025-07-07T01:32:52.841204Z","iopub.status.idle":"2025-07-07T01:32:53.250919Z","shell.execute_reply.started":"2025-07-07T01:32:52.841179Z","shell.execute_reply":"2025-07-07T01:32:53.250033Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"##　ライブラリのインポート","metadata":{}},{"cell_type":"code","source":"import zipfile # zipファイルの解凍に必要\nimport tensorflow as tf # 機械学習に必要\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator # データの正規化に必要\nfrom tensorflow.keras import layers, models # レイヤークラス, 学習モデル\nfrom tensorflow.keras.preprocessing import image # 画像の前処理に必要\nimport shutil # クラス分けに必要\nimport glob # ファイルのソートに必要\nimport re","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-07T01:32:54.830359Z","iopub.execute_input":"2025-07-07T01:32:54.830840Z","iopub.status.idle":"2025-07-07T01:33:14.973047Z","shell.execute_reply.started":"2025-07-07T01:32:54.830812Z","shell.execute_reply":"2025-07-07T01:33:14.971673Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## trainデータファイルを解凍する","metadata":{}},{"cell_type":"code","source":"file_path = '/kaggle/input/dogs-vs-cats/train.zip'\nout_path = '/kaggle/working'\n\nwith zipfile.ZipFile(file_path, 'r') as file:\n    file.extractall(out_path)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-07T01:33:14.974647Z","iopub.execute_input":"2025-07-07T01:33:14.975346Z","iopub.status.idle":"2025-07-07T01:33:25.874812Z","shell.execute_reply.started":"2025-07-07T01:33:14.975307Z","shell.execute_reply":"2025-07-07T01:33:25.873921Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## trainデータをクラスで分ける\n\n犬の画像を\"/kaggle/working/train/dogs\"に、猫の画像を\"/kaggle/working/train/cats\"に分ける。\n* cat:0\n* dog:1","metadata":{}},{"cell_type":"code","source":"train_dir = '/kaggle/working/train'\ncat_dir = os.path.join(train_dir, 'cats')\ndog_dir = os.path.join(train_dir, 'dogs')\n\nos.makedirs(cat_dir, exist_ok=True)\nos.makedirs(dog_dir, exist_ok=True)\n\nfor filename in os.listdir(train_dir):\n    \n    file_path = os.path.join(train_dir, filename)\n    if not os.path.isfile(file_path):\n        continue\n    \n    if filename.lower().startswith('cat'):\n        shutil.move(os.path.join(train_dir, filename), os.path.join(cat_dir, filename))\n    elif filename.lower().startswith('dog'):\n        shutil.move(os.path.join(train_dir, filename), os.path.join(dog_dir, filename))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-07T01:33:25.875666Z","iopub.execute_input":"2025-07-07T01:33:25.875998Z","iopub.status.idle":"2025-07-07T01:33:26.871356Z","shell.execute_reply.started":"2025-07-07T01:33:25.875976Z","shell.execute_reply":"2025-07-07T01:33:26.870569Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## testデータファイルを解凍する","metadata":{}},{"cell_type":"code","source":"file_path = '/kaggle/input/dogs-vs-cats/test1.zip'\nout_path = '/kaggle/working'\n\nwith zipfile.ZipFile(file_path, 'r') as file:\n    file.extractall(out_path)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-07T01:33:26.872835Z","iopub.execute_input":"2025-07-07T01:33:26.873167Z","iopub.status.idle":"2025-07-07T01:33:32.057714Z","shell.execute_reply.started":"2025-07-07T01:33:26.873145Z","shell.execute_reply":"2025-07-07T01:33:32.056820Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## データの正規化を行う\nImageDataGeneratorは画像の前処理やデータ拡張を行うためのクラスである。\"rescale=1./255\"は画像のピクセル値（0-255）を0-1に正規化する。\n\n* train_datagen：トレーニング用の前処理設定\n* test_datagen：テスト用の前処理設定","metadata":{}},{"cell_type":"code","source":"train_datagen = ImageDataGenerator(rescale=1./255)\ntest_datagen = ImageDataGenerator(rescale=1./255)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-07T01:33:32.058690Z","iopub.execute_input":"2025-07-07T01:33:32.059014Z","iopub.status.idle":"2025-07-07T01:33:32.064708Z","shell.execute_reply.started":"2025-07-07T01:33:32.058991Z","shell.execute_reply":"2025-07-07T01:33:32.063217Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## ジェネレーターを作成する\nflow_datagen.flow_from_directory()は、指定したディレクトリ構造に基づいて、自動的に画像とラベルを生成するジェネレーターを作成する。\n\n* '/kaggle/working/train' ：トレーニング画像が保存されている親ディレクトリ\n* target_size=(150, 150) ：すべての画像を150*150にリサイズする\n* batch_size=32 ：1回の学習ステップで32枚の画像を処理する\n* class_mode='binary' ：クラスが2つ（犬、猫）のため、出力は0か1の2値","metadata":{}},{"cell_type":"code","source":"train_generator = train_datagen.flow_from_directory(\n    '/kaggle/working/train',\n    target_size=(150, 150),\n    batch_size=32,\n    class_mode='binary'\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-07T01:33:32.065708Z","iopub.execute_input":"2025-07-07T01:33:32.066114Z","iopub.status.idle":"2025-07-07T01:33:32.340576Z","shell.execute_reply.started":"2025-07-07T01:33:32.066078Z","shell.execute_reply":"2025-07-07T01:33:32.339673Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"テストデータでも同様の処理を行う。","metadata":{}},{"cell_type":"code","source":"test_generator = test_datagen.flow_from_directory(\n    '/kaggle/working/test1',\n    target_size=(150, 150),\n    batch_size=32,\n    class_mode='binary'\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-07T01:33:38.339480Z","iopub.execute_input":"2025-07-07T01:33:38.340937Z","iopub.status.idle":"2025-07-07T01:33:38.443191Z","shell.execute_reply.started":"2025-07-07T01:33:38.340844Z","shell.execute_reply":"2025-07-07T01:33:38.442195Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## CNNモデルの構築\n学習モデルを構築する。Seqential()は順番に積み重ねるタイプのニューラルネットワークである。\n\n* Conv2D（畳み込み層）\n*  * 第一引数：フィルター数\n*  * 第二引数：フィルターサイズ\n*  * 第三引数：活性化関数\n*  * 第四引数：入力サイズ\n* MaxPooling2D（プーリング層）\n*  * n*nの領域ごとに最大値を取って縮小する。\n* Flatten\n*  * 出力を1次元のベクトルに変換\n* Dense（全結合層）\n*  * 第一引数：ノード数\n*  * 第二引数：活性化関数","metadata":{}},{"cell_type":"code","source":"model = models.Sequential([\n    layers.Conv2D(32, (3, 3), activation='relu', input_shape=(150, 150, 3)), # 画像化から特徴を抽出する\n    layers.MaxPooling2D(2, 2), # 重要な情報を残しつつ、画像サイズを圧縮\n    layers.Conv2D(64, (3, 3), activation='relu'),\n    layers.MaxPooling2D(2, 2),\n    layers.Conv2D(128, (3, 3), activation='relu'),\n    layers.MaxPooling2D(2, 2),\n    layers.Flatten(), # 畳み込み層の出力を、1次元のベクトルに変換\n    layers.Dense(512, activation='relu'), # 中間特徴を学習し、分析精度を向上させる\n    layers.Dense(1, activation='sigmoid') # sigmoidにより0-1の確率を算出\n])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-07T01:33:56.500516Z","iopub.execute_input":"2025-07-07T01:33:56.501513Z","iopub.status.idle":"2025-07-07T01:33:56.834947Z","shell.execute_reply.started":"2025-07-07T01:33:56.501483Z","shell.execute_reply":"2025-07-07T01:33:56.833807Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## モデルのコンパイル","metadata":{}},{"cell_type":"code","source":"model.compile(\n    loss='binary_crossentropy', # 損失関数\n    optimizer='adam', # 最適化アルゴリズム\n    metrics=['accuracy'] # 評価指標 accuracy：予測が正解だった割合\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-07T01:34:01.223676Z","iopub.execute_input":"2025-07-07T01:34:01.224139Z","iopub.status.idle":"2025-07-07T01:34:01.246914Z","shell.execute_reply.started":"2025-07-07T01:34:01.224108Z","shell.execute_reply":"2025-07-07T01:34:01.245795Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## モデルの学習","metadata":{}},{"cell_type":"code","source":"history = model.fit(\n    train_generator,\n    epochs=10, # 訓練データを10回学習する\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-07T01:34:03.135628Z","iopub.execute_input":"2025-07-07T01:34:03.136000Z","iopub.status.idle":"2025-07-07T03:43:06.743205Z","shell.execute_reply.started":"2025-07-07T01:34:03.135975Z","shell.execute_reply":"2025-07-07T03:43:06.742139Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"file_path = '/kaggle/working/cnn.h5'\n\nmodel.save(file_path)\n# model = load_model(file_path)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-07T03:48:36.538950Z","iopub.execute_input":"2025-07-07T03:48:36.539277Z","iopub.status.idle":"2025-07-07T03:48:37.172038Z","shell.execute_reply.started":"2025-07-07T03:48:36.539253Z","shell.execute_reply":"2025-07-07T03:48:37.171077Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## testデータのソート","metadata":{}},{"cell_type":"code","source":"# ファイル一覧を所得\ntest_dir = '/kaggle/working/test1'\ntest_files = os.listdir(test_dir)\n\n# 数値順にソートする関数\ndef extract_number(filename):\n    match = re.search(r'\\d+', filename)\n    return int(match.group()) if match else -1\n\n# 数値順にソート\nsorted_files = sorted(test_files, key=extract_number)\n\n# 画像ファイルのフルパスリスト\nimage_path_list = [os.path.join(test_dir, image) for image in sorted_files]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-07T03:49:08.483990Z","iopub.execute_input":"2025-07-07T03:49:08.485094Z","iopub.status.idle":"2025-07-07T03:49:08.531171Z","shell.execute_reply.started":"2025-07-07T03:49:08.485067Z","shell.execute_reply":"2025-07-07T03:49:08.530117Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## testデータの推論\n画像の読み込んだあと、numpy配列に変換する。その後、推論を行う。","metadata":{}},{"cell_type":"code","source":"# 前処理\nimages = [image.img_to_array(image.load_img(path, target_size=(150, 150))) / 255.0 for path in image_path_list]\nbatch = np.array(images)\n\n# 推論\npreds = model.predict(batch)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-07T03:49:42.016310Z","iopub.execute_input":"2025-07-07T03:49:42.016602Z","iopub.status.idle":"2025-07-07T03:52:04.313468Z","shell.execute_reply.started":"2025-07-07T03:49:42.016583Z","shell.execute_reply":"2025-07-07T03:52:04.312293Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## 結果の出力\nCSVで結果を保存","metadata":{}},{"cell_type":"code","source":"# IDの取得\nids = [os.path.splitext(os.path.basename(path))[0] for path in image_path_list]\n\n# ラベルの取得\nlabels = [1 if p[0] > 0.5 else 0 for p in preds]\n\n# CSVで保存\ndf = pd.DataFrame({'id': ids, 'label': labels})\ndf.to_csv('submission.csv', index=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-07T03:53:35.842091Z","iopub.execute_input":"2025-07-07T03:53:35.842777Z","iopub.status.idle":"2025-07-07T03:53:35.965826Z","shell.execute_reply.started":"2025-07-07T03:53:35.842743Z","shell.execute_reply":"2025-07-07T03:53:35.964669Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}