{"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-11T08:19:43.010252Z","iopub.execute_input":"2025-07-11T08:19:43.011723Z","iopub.status.idle":"2025-07-11T08:19:43.0207Z","shell.execute_reply.started":"2025-07-11T08:19:43.011662Z","shell.execute_reply":"2025-07-11T08:19:43.018916Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Zipファイルの展開","metadata":{}},{"cell_type":"code","source":"import zipfile\n\n# zipファイルのパス\nzip_train_path = '/kaggle/input/dogs-vs-cats-redux-kernels-edition/train.zip'\nzip_test_path = '/kaggle/input/dogs-vs-cats-redux-kernels-edition/test.zip'\n# 展開先のパス\nextract_train_path = '/kaggle/working'\nextract_test_path = '/kaggle/working'\n# 展開処理\nwith zipfile.ZipFile(zip_train_path, 'r') as zip_ref:\n    zip_ref.extractall(extract_train_path)\nwith zipfile.ZipFile(zip_test_path, 'r') as zip_ref:\n    zip_ref.extractall(extract_test_path)\nprint(\"ファイル展開完了\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-11T08:19:43.022806Z","iopub.execute_input":"2025-07-11T08:19:43.023162Z","iopub.status.idle":"2025-07-11T08:19:55.635876Z","shell.execute_reply.started":"2025-07-11T08:19:43.023137Z","shell.execute_reply":"2025-07-11T08:19:55.634919Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# trainデータのラベル付け","metadata":{}},{"cell_type":"code","source":"train_dir = '/kaggle/working/train'  # 展開された画像の場所\n\nfilenames = os.listdir(train_dir)\nlabels = ['dog' if 'dog' in fname else 'cat' for fname in filenames]\nprint(\"ラベル付け完了\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-11T08:19:55.636847Z","iopub.execute_input":"2025-07-11T08:19:55.637115Z","iopub.status.idle":"2025-07-11T08:19:55.661652Z","shell.execute_reply.started":"2025-07-11T08:19:55.637093Z","shell.execute_reply":"2025-07-11T08:19:55.660607Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# データセットのデータフレーム化","metadata":{}},{"cell_type":"code","source":"# trainファイルのフルパス\ntrain_filepaths = [os.path.join(train_dir, fname) for fname in filenames]\n\n# train DataFrame作成\ntrain_df = pd.DataFrame({\n    'filename': filenames,\n    'filepath': train_filepaths,\n    'label': labels\n})\nprint(\"trainDF作成完了\")\n\n# testファイルのパス\ntest_dir = '/kaggle/working/test'\ntest_filenames = os.listdir(test_dir)\ntest_filepaths = [os.path.join(test_dir, fname) for fname in test_filenames]\n\n# test DataFrame作成\ntest_df = pd.DataFrame({\n    'filename': test_filenames,\n    'filepath': test_filepaths\n})\nprint(\"testDF作成完了\")\n\n# 確認（ラベルはtrainのみ）\nprint(train_df.head())\nprint(test_df.head())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-11T08:19:55.664225Z","iopub.execute_input":"2025-07-11T08:19:55.66456Z","iopub.status.idle":"2025-07-11T08:19:55.735018Z","shell.execute_reply.started":"2025-07-11T08:19:55.664537Z","shell.execute_reply":"2025-07-11T08:19:55.733804Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# データセットを128×128にリサイズ","metadata":{}},{"cell_type":"code","source":"from PIL import Image\n\n# 保存先ディレクトリ作成\ntrain_resized_dir = '/kaggle/working/train_128'\ntest_resized_dir = '/kaggle/working/test_128'\nos.makedirs(train_resized_dir, exist_ok=True)\nos.makedirs(test_resized_dir, exist_ok=True)\n\n# ----------- train画像のリサイズ -----------\nfor fname in train_df['filename']:\n    src_path = os.path.join(train_dir, fname)\n    dst_path = os.path.join(train_resized_dir, fname)\n    with Image.open(src_path) as img:\n        img_resized = img.resize((128, 128))\n        img_resized.save(dst_path)\n\n# filepathを更新\ntrain_df['filepath'] = train_df['filename'].apply(lambda x: os.path.join(train_resized_dir, x))\nprint(\"trainのリサイズ完了\")\n\n# ----------- test画像のリサイズ -----------\nfor fname in test_df['filename']:\n    src_path = os.path.join(test_dir, fname)\n    dst_path = os.path.join(test_resized_dir, fname)\n    with Image.open(src_path) as img:\n        img_resized = img.resize((128, 128))\n        img_resized.save(dst_path)\n\n# filepathを更新\ntest_df['filepath'] = test_df['filename'].apply(lambda x: os.path.join(test_resized_dir, x))\nprint(\"testのリサイズ完了\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-11T08:19:55.736269Z","iopub.execute_input":"2025-07-11T08:19:55.736577Z","iopub.status.idle":"2025-07-11T08:22:12.973735Z","shell.execute_reply.started":"2025-07-11T08:19:55.736554Z","shell.execute_reply":"2025-07-11T08:22:12.971977Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# データセットのサイズ確認","metadata":{}},{"cell_type":"code","source":"# ----------- train_128のサイズ確認 -----------\ncorrect_train_size = 0\nfor path in train_df['filepath']:\n    with Image.open(path) as img:\n        if img.size == (128, 128):\n            correct_train_size += 1\n\nprint(f\"128x128に正しくリサイズされたtrain画像：{correct_train_size} / {len(train_df)}\")\n\n# ----------- test_128のサイズ確認 -----------\ncorrect_test_size = 0\nfor path in test_df['filepath']:\n    with Image.open(path) as img:\n        if img.size == (128, 128):\n            correct_test_size += 1\n\nprint(f\"128x128に正しくリサイズされたtest画像：{correct_test_size} / {len(test_df)}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-11T08:22:12.975317Z","iopub.execute_input":"2025-07-11T08:22:12.975821Z","iopub.status.idle":"2025-07-11T08:22:15.839059Z","shell.execute_reply.started":"2025-07-11T08:22:12.975779Z","shell.execute_reply":"2025-07-11T08:22:15.837849Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# データのバッチ化 # ","metadata":{}},{"cell_type":"code","source":"import numpy as np\n\n# バッチサイズ\nbatch_size = 32\n\n# trainバッチ作成\ntrain_batches = np.array_split(train_df, np.ceil(len(train_df) / batch_size))\n\n# testバッチ作成\ntest_batches = np.array_split(test_df, np.ceil(len(test_df) / batch_size))\n\n# 例：最初のバッチを表示\nprint(f\"Train バッチ数: {len(train_batches)}\")\nprint(train_batches[0].head())\n\nprint(f\"Test バッチ数: {len(test_batches)}\")\nprint(test_batches[0].head())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-11T08:22:15.840396Z","iopub.execute_input":"2025-07-11T08:22:15.840835Z","iopub.status.idle":"2025-07-11T08:22:15.94862Z","shell.execute_reply.started":"2025-07-11T08:22:15.840795Z","shell.execute_reply":"2025-07-11T08:22:15.947391Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import numpy as np\n\n# バッチサイズ\nbatch_size = 32\n\n# trainバッチ作成\ntrain_batches = np.array_split(train_df, np.ceil(len(train_df) / batch_size))\n\n# testバッチ作成\ntest_batches = np.array_split(test_df, np.ceil(len(test_df) / batch_size))\n\n# 例：最初のバッチを表示\nprint(f\"Train バッチ数: {len(train_batches)}\")\nprint(train_batches[0].head())\n\nprint(f\"Test バッチ数: {len(test_batches)}\")\nprint(test_batches[0].head())\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-11T08:22:15.949906Z","iopub.execute_input":"2025-07-11T08:22:15.950308Z","iopub.status.idle":"2025-07-11T08:22:16.057175Z","shell.execute_reply.started":"2025-07-11T08:22:15.950273Z","shell.execute_reply":"2025-07-11T08:22:16.055896Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# 各バッチに行うループ処理 #","metadata":{}},{"cell_type":"code","source":"# 例：各trainバッチの画像ファイルパスを出力\nfor i, batch in enumerate(train_batches):\n    print(f\"Train Batch {i+1}: {len(batch)}枚\")\n    print(batch['filepath'].tolist()[:3])  # 最初の3枚だけ表示","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-07-11T08:22:16.058517Z","iopub.execute_input":"2025-07-11T08:22:16.058916Z","iopub.status.idle":"2025-07-11T08:22:16.124071Z","shell.execute_reply.started":"2025-07-11T08:22:16.058886Z","shell.execute_reply":"2025-07-11T08:22:16.122715Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_ds=train_ds.batch(32)\ntrain_ds=train_ds.map(lambda x,y:tf.py_function(convert_data,[x,y],Tout=[tf.float32,tf.int32]))\ntrain_ds=train_ds.prefetch(buffer_size=AUTOTUNE)","metadata":{"trusted":true,"_kg_hide-input":true,"execution":{"iopub.status.busy":"2025-07-11T08:22:16.12731Z","iopub.execute_input":"2025-07-11T08:22:16.129208Z","iopub.status.idle":"2025-07-11T08:22:16.166226Z","shell.execute_reply.started":"2025-07-11T08:22:16.129157Z","shell.execute_reply":"2025-07-11T08:22:16.164775Z"}},"outputs":[],"execution_count":null}]}