{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","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":30527,"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-07-17T06:06:59.295693Z","iopub.execute_input":"2024-07-17T06:06:59.296112Z","iopub.status.idle":"2024-07-17T06:06:59.328934Z","shell.execute_reply.started":"2024-07-17T06:06:59.296076Z","shell.execute_reply":"2024-07-17T06:06:59.327902Z"},"trusted":true},"execution_count":1,"outputs":[{"name":"stdout","text":"/kaggle/input/dogs-vs-cats-redux-kernels-edition/sample_submission.csv\n/kaggle/input/dogs-vs-cats-redux-kernels-edition/train.zip\n/kaggle/input/dogs-vs-cats-redux-kernels-edition/test.zip\n","output_type":"stream"}]},{"cell_type":"markdown","source":"# データの準備しちゃおう！！！！！❤︎\n\n- [X] zipファイル解凍する！🧊🔥\n- [ ] 学習で使用するディレクトリを作る\n- [ ] 学習で使用するディレクトリに画像ファイルを移動させる","metadata":{}},{"cell_type":"code","source":"# Zipファイルを解凍しよう！！！\nimport zipfile\n\n# train.zipの解凍します！！！\nwith zipfile.ZipFile('/kaggle/input/dogs-vs-cats-redux-kernels-edition/train.zip', 'r') as zip_ref:\n    zip_ref.extractall('/kaggle/working/train')\n\n# test.zipの解凍します！！！\nwith zipfile.ZipFile('/kaggle/input/dogs-vs-cats-redux-kernels-edition/test.zip', 'r') as zip_ref:\n    zip_ref.extractall('/kaggle/working/test')","metadata":{"execution":{"iopub.status.busy":"2024-07-17T06:06:59.330819Z","iopub.execute_input":"2024-07-17T06:06:59.331162Z","iopub.status.idle":"2024-07-17T06:07:18.200626Z","shell.execute_reply.started":"2024-07-17T06:06:59.331133Z","shell.execute_reply":"2024-07-17T06:07:18.199534Z"},"trusted":true},"execution_count":2,"outputs":[]},{"cell_type":"markdown","source":"# ディレクトリの作成〜\n<font color='red'>**豆知識🫛**</font>：ディレクトリとはフォルダのこと！","metadata":{}},{"cell_type":"code","source":"import os\n\n# 新しいディレクトリのパスを指定します！\ndirectory_path = [\n    \"/kaggle/working/data/train/dogs\",\n    \"/kaggle/working/data/train/cats\",\n    \"/kaggle/working/data/valid/dogs\",\n    \"/kaggle/working/data/valid/cats\",\n    \"/kaggle/working/data/test\",\n]\n\n# ディレクトリを作成します！\nfor path in directory_path:\n    os.makedirs(path, exist_ok=True)","metadata":{"execution":{"iopub.status.busy":"2024-07-17T06:07:18.202184Z","iopub.execute_input":"2024-07-17T06:07:18.202597Z","iopub.status.idle":"2024-07-17T06:07:18.20963Z","shell.execute_reply.started":"2024-07-17T06:07:18.20256Z","shell.execute_reply":"2024-07-17T06:07:18.208552Z"},"trusted":true},"execution_count":3,"outputs":[]},{"cell_type":"markdown","source":"# データの移動","metadata":{}},{"cell_type":"code","source":"import os\nimport shutil\nimport glob\n\nsource_directory = \"/kaggle/working/train/train\"\ndirectory = [\n    \"/kaggle/working/data/train/dogs\",\n    \"/kaggle/working/data/train/cats\",\n]\ntest_source_directory = \"/kaggle/working/test/test\"\ntest_directory = \"/kaggle/working/data/test/\"\n\nCATEGORIES = [\"dog.\", \"cat.\"]\n\n# 訓練(train)データの画像ファイルを所定の場所へを移動\nfor key,DIR in zip(CATEGORIES,directory):\n    files = [file for file in os.listdir(source_directory) if file.startswith(key)]\n    for file in files:\n        source_path = os.path.join(source_directory, file)\n        destination_path = os.path.join(DIR, file)\n        shutil.move(source_path, destination_path)\n\n# テスト(test)データの画像ファイルを所定の場所へを移動\nfiles = [file for file in os.listdir(test_source_directory) if file.endswith(\".jpg\")]\nfor file in files:\n    source_path = os.path.join(test_source_directory, file)\n    destination_path = os.path.join(test_directory, file)\n    shutil.move(source_path, destination_path)","metadata":{"execution":{"iopub.status.busy":"2024-07-17T06:07:18.211907Z","iopub.execute_input":"2024-07-17T06:07:18.212318Z","iopub.status.idle":"2024-07-17T06:07:19.403633Z","shell.execute_reply.started":"2024-07-17T06:07:18.212289Z","shell.execute_reply":"2024-07-17T06:07:19.402559Z"},"trusted":true},"execution_count":4,"outputs":[]},{"cell_type":"markdown","source":"## 確認用","metadata":{}},{"cell_type":"code","source":"dogs_directory = \"/kaggle/working/data/train/dogs\"\ncats_directory = \"/kaggle/working/data/train/cats\"\ntest_directory = \"/kaggle/working/data/test/\"\n\n# dogsディレクトリ内のファイルを表示\nprint(\"Dogs:\")\ndog_files = os.listdir(dogs_directory)\nprint(f'file number : {len(dog_files)}')\nfor file in dog_files[:5]:\n    print(file)\n\n# catsディレクトリ内のファイルを表示\nprint(\"\\nCats:\")\ncat_files = os.listdir(cats_directory)\nprint(f'file number : {len(cat_files)}')\nfor file in cat_files[:5]:\n    print(file)\n\n# dogsディレクトリ内のファイルを表示\nprint(\"\\ntest:\")\ntest_files = os.listdir(test_directory)\nprint(f'file number : {len(test_files)}')\nfor file in test_files[:5]:\n    print(file)","metadata":{"execution":{"iopub.status.busy":"2024-07-17T06:07:19.405435Z","iopub.execute_input":"2024-07-17T06:07:19.405841Z","iopub.status.idle":"2024-07-17T06:07:19.430807Z","shell.execute_reply.started":"2024-07-17T06:07:19.40581Z","shell.execute_reply":"2024-07-17T06:07:19.429307Z"},"trusted":true},"execution_count":5,"outputs":[{"name":"stdout","text":"Dogs:\nfile number : 12500\ndog.4797.jpg\ndog.5122.jpg\ndog.11599.jpg\ndog.5966.jpg\ndog.286.jpg\n\nCats:\nfile number : 12500\ncat.5.jpg\ncat.10685.jpg\ncat.732.jpg\ncat.6400.jpg\ncat.3218.jpg\n\ntest:\nfile number : 12500\n3964.jpg\n11633.jpg\n9732.jpg\n3944.jpg\n3046.jpg\n","output_type":"stream"}]},{"cell_type":"markdown","source":"# 学習の準備\n\n## 使用するライブラリのインポート","metadata":{}},{"cell_type":"code","source":"# -------------------------------------------------------------------\n# 各種ライブラリのインポート\n# -------------------------------------------------------------------\n! pip install --upgrade tensorflow-io\nimport matplotlib.pyplot as plt\nimport glob\nimport cv2\nimport pandas as pd\nimport numpy as np\nimport random           #乱数生成\nfrom sklearn.model_selection import train_test_split\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras import layers, models\nfrom tensorflow.keras.utils import to_categorical\nfrom keras import backend as K\n\n# random_int = np.random.randint(1, 100000001)  # 1~1億の中からランダムで選ぶ\n# print(\"Random integer:\", random_int)\n\ntf.config.threading.set_intra_op_parallelism_threads(1)\ntf.config.threading.set_inter_op_parallelism_threads(1)\n\ndef seed_everything(seed): #再現性の為に乱数シードを固定する為の関数\n    random.seed(seed) #乱数のシード\n    os.environ['PYTHONHASHSEED'] = str(seed) #osのシード\n    np.random.seed(seed) #npの乱数シード\n    tf.random.set_seed(seed) #tfの乱数シード\n\nseed_everything(88710077) ","metadata":{"execution":{"iopub.status.busy":"2024-07-17T06:07:19.432283Z","iopub.execute_input":"2024-07-17T06:07:19.433192Z","iopub.status.idle":"2024-07-17T06:07:44.413335Z","shell.execute_reply.started":"2024-07-17T06:07:19.433148Z","shell.execute_reply":"2024-07-17T06:07:44.412151Z"},"trusted":true},"execution_count":6,"outputs":[{"name":"stdout","text":"Requirement already satisfied: tensorflow-io in /opt/conda/lib/python3.10/site-packages (0.31.0)\nCollecting tensorflow-io\n  Downloading tensorflow_io-0.37.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (49.6 MB)\n\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m49.6/49.6 MB\u001b[0m \u001b[31m19.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m00:01\u001b[0m00:01\u001b[0m\n\u001b[?25hCollecting tensorflow-io-gcs-filesystem==0.37.1 (from tensorflow-io)\n  Downloading tensorflow_io_gcs_filesystem-0.37.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (5.1 MB)\n\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m5.1/5.1 MB\u001b[0m \u001b[31m76.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m:00:01\u001b[0m\n\u001b[?25hInstalling collected packages: tensorflow-io-gcs-filesystem, tensorflow-io\n  Attempting uninstall: tensorflow-io-gcs-filesystem\n    Found existing installation: tensorflow-io-gcs-filesystem 0.31.0\n    Uninstalling tensorflow-io-gcs-filesystem-0.31.0:\n      Successfully uninstalled tensorflow-io-gcs-filesystem-0.31.0\n  Attempting uninstall: tensorflow-io\n    Found existing installation: tensorflow-io 0.31.0\n    Uninstalling tensorflow-io-0.31.0:\n      Successfully uninstalled tensorflow-io-0.31.0\nSuccessfully installed tensorflow-io-0.37.1 tensorflow-io-gcs-filesystem-0.37.1\n","output_type":"stream"},{"name":"stderr","text":"/opt/conda/lib/python3.10/site-packages/scipy/__init__.py:146: UserWarning: A NumPy version >=1.16.5 and <1.23.0 is required for this version of SciPy (detected version 1.23.5\n  warnings.warn(f\"A NumPy version >={np_minversion} and <{np_maxversion}\"\n","output_type":"stream"}]},{"cell_type":"markdown","source":"## モデルに入力できるようにデータの形を変形","metadata":{}},{"cell_type":"code","source":"# -------------------------------------------------------------------\n# 定数の定義\n# -------------------------------------------------------------------\nDATADIR = \"/kaggle/working/data/train\"\n# MODEL_DIR = \"/content/drive/MyDrive\"\nCATEGORIES = [\"cats\", \"dogs\"]\n\n# -------------------------------------------------------------------\n# メイン処理（画像を読込み、犬猫データセットを設定）\n# -------------------------------------------------------------------\n#犬猫データセットを定義\nfeature_data = []\nlabel_data = []\n    \n# 平均画像サイズ\ntarget_size = (128, 128)\nprint(target_size)\n\n#画像を読込み、犬猫データセットを設定\n#（0:Cat、1:Dog)…class_numはCATEGORIESの配列index／categoryはCATEGORIESの中身　\nfor class_num, category in enumerate(CATEGORIES):\n    #ディレクトリを設定（ループ1回目：./PetImages/Cat　2回目：./PetImages/Dog)\n    path = os.path.join(DATADIR, category)\n    for image_name in os.listdir(path):\n        try:\n            img_file = cv2.imread(os.path.join(path, image_name))# 画像読み込み\n            gray_image = cv2.cvtColor(img_file, cv2.COLOR_BGR2GRAY)\n            resize_file = cv2.resize(gray_image, (target_size))# 画像のリサイズ\n            feature_data.append(resize_file)\n            label_data.append(class_num)# 画像データ(猫 or 犬)、ラベル情報(0 or 1)を追加\n        except Exception as e:\n            #画像の読込みエラーはスルー（スキップして次の画像へ）\n            print(\"skiped.\")\n            pass\n    print(f\"{category}finish.\")","metadata":{"execution":{"iopub.status.busy":"2024-07-17T06:07:44.414687Z","iopub.execute_input":"2024-07-17T06:07:44.41537Z","iopub.status.idle":"2024-07-17T06:08:53.618045Z","shell.execute_reply.started":"2024-07-17T06:07:44.415336Z","shell.execute_reply":"2024-07-17T06:08:53.616879Z"},"trusted":true},"execution_count":7,"outputs":[{"name":"stdout","text":"(128, 128)\ncatsfinish.\ndogsfinish.\n","output_type":"stream"}]},{"cell_type":"markdown","source":"## 訓練データと検証データに分類","metadata":{}},{"cell_type":"code","source":"train_x, valid_x, train_y, valid_y = train_test_split(feature_data, label_data, test_size=0.1)","metadata":{"execution":{"iopub.status.busy":"2024-07-17T06:08:53.619383Z","iopub.execute_input":"2024-07-17T06:08:53.619688Z","iopub.status.idle":"2024-07-17T06:08:53.636489Z","shell.execute_reply.started":"2024-07-17T06:08:53.619662Z","shell.execute_reply":"2024-07-17T06:08:53.635498Z"},"trusted":true},"execution_count":8,"outputs":[]},{"cell_type":"markdown","source":"## モデルに入力できるようにデータの形を変形（最終調整）","metadata":{}},{"cell_type":"code","source":"import tensorflow as tf # TensorFlowライブラリをロード\n# データ変換(正規化とテンソル変換)\ndef convert_data(x,y):\n    batch,a,b = x.shape # データの形（シェイプ）を取得（32×28×28）\n    x = tf.reshape(x,(batch,a,b,1)) # 1チャンネル分増やし，4次元テンソルに変換\n    x = tf.cast(x,tf.float32) # float32に変換\n    x /= 255. # 正規化\n    return x, tf.cast(y,tf.int32)\n\n#\n# tf.Dataによるtf.Tensor変換\n#\n\ntrain_ds = tf.data.Dataset.from_tensor_slices((train_x,train_y)) # 訓練セットの入力と教師ラベルを合体させてTensorに変換\nvalid_ds = tf.data.Dataset.from_tensor_slices((valid_x,valid_y)) # 検証用も同様に処理\nAUTOTUNE = tf.data.experimental.AUTOTUNE # 自動チューニング設定 (おまじない)\n\ntrain_ds = train_ds.shuffle(len(train_x)) # 訓練データをシャッフルする\ntrain_ds = train_ds.repeat(1) # １epochで使われるデータの回数。１の場合１epochで１回しか使われない。引数を空欄にすると無限に使われる\ntrain_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) # 訓練中に次のパッチを取り出すための処理\n\nvalid_ds = valid_ds.batch(32) # 検証データはシャッフルする必要がないので,パッチ化のみの処理\nvalid_ds = valid_ds.map(lambda x, y: tf.py_function(convert_data, [x, y], Tout=[tf.float32, tf.int32]))","metadata":{"execution":{"iopub.status.busy":"2024-07-17T06:08:53.637771Z","iopub.execute_input":"2024-07-17T06:08:53.638126Z","iopub.status.idle":"2024-07-17T06:09:46.972569Z","shell.execute_reply.started":"2024-07-17T06:08:53.638096Z","shell.execute_reply":"2024-07-17T06:09:46.971422Z"},"trusted":true},"execution_count":9,"outputs":[]},{"cell_type":"markdown","source":"## 確認用","metadata":{}},{"cell_type":"code","source":"train_ds","metadata":{"execution":{"iopub.status.busy":"2024-07-17T06:09:46.977268Z","iopub.execute_input":"2024-07-17T06:09:46.977629Z","iopub.status.idle":"2024-07-17T06:09:46.985218Z","shell.execute_reply.started":"2024-07-17T06:09:46.9776Z","shell.execute_reply":"2024-07-17T06:09:46.984034Z"},"trusted":true},"execution_count":10,"outputs":[{"execution_count":10,"output_type":"execute_result","data":{"text/plain":"<_PrefetchDataset element_spec=(TensorSpec(shape=<unknown>, dtype=tf.float32, name=None), TensorSpec(shape=<unknown>, dtype=tf.int32, name=None))>"},"metadata":{}}]},{"cell_type":"markdown","source":"## モデルの作成","metadata":{}},{"cell_type":"code","source":"# 入力層\ninput = tf.keras.layers.Input(shape=(128,128,1), name='input') # ⼊⼒層の定義\n# CNN 第一層\nh = tf.keras.layers.Conv2D(32, (9, 9), activation='relu', name='cnn01')(input)\nh = tf.keras.layers.MaxPooling2D((2, 2), name='pool01')(h)\n# CNN 第二層\nh = tf.keras.layers.Conv2D(32, (5, 5), activation='relu', name='cnn02')(h)\nh = tf.keras.layers.MaxPooling2D((2, 2), name='pool02')(h)\n# CNN 第三層\nh = tf.keras.layers.Conv2D(64, (5, 5), activation='relu', name='cnn03')(h)\nh = tf.keras.layers.MaxPooling2D((2, 2), name='pool03')(h)\n# CNN 第四層\nh = tf.keras.layers.Conv2D(64, (5, 5), activation='relu', name='cnn04')(h)\nh = tf.keras.layers.MaxPooling2D((2, 2), name='pool04')(h)\n# 1次元テンソルに変換する。フラット化。\nh = tf.keras.layers.Flatten(name='flatten')(h)\n# 全結合層 隠れ層のノードは 256\nh = tf.keras.layers.Dense(256, activation='relu', name='dense01')(h)\n# 出力層\noutput = tf.keras.layers.Dense(2, activation='softmax', name='output')(h) # 出⼒層\n# この処理でモデルを実体化する。⼊⼒層と出⼒層を渡すと⾃動的にネットワークを構築してくれる。\nmodel = tf.keras.Model(inputs=input, outputs=output)\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2024-07-17T06:09:46.986453Z","iopub.execute_input":"2024-07-17T06:09:46.986758Z","iopub.status.idle":"2024-07-17T06:09:47.175909Z","shell.execute_reply.started":"2024-07-17T06:09:46.986726Z","shell.execute_reply":"2024-07-17T06:09:47.174928Z"},"trusted":true},"execution_count":11,"outputs":[{"name":"stdout","text":"Model: \"model\"\n_________________________________________________________________\n Layer (type)                Output Shape              Param #   \n=================================================================\n input (InputLayer)          [(None, 128, 128, 1)]     0         \n                                                                 \n cnn01 (Conv2D)              (None, 120, 120, 32)      2624      \n                                                                 \n pool01 (MaxPooling2D)       (None, 60, 60, 32)        0         \n                                                                 \n cnn02 (Conv2D)              (None, 56, 56, 32)        25632     \n                                                                 \n pool02 (MaxPooling2D)       (None, 28, 28, 32)        0         \n                                                                 \n cnn03 (Conv2D)              (None, 24, 24, 64)        51264     \n                                                                 \n pool03 (MaxPooling2D)       (None, 12, 12, 64)        0         \n                                                                 \n cnn04 (Conv2D)              (None, 8, 8, 64)          102464    \n                                                                 \n pool04 (MaxPooling2D)       (None, 4, 4, 64)          0         \n                                                                 \n flatten (Flatten)           (None, 1024)              0         \n                                                                 \n dense01 (Dense)             (None, 256)               262400    \n                                                                 \n output (Dense)              (None, 2)                 514       \n                                                                 \n=================================================================\nTotal params: 444,898\nTrainable params: 444,898\nNon-trainable params: 0\n_________________________________________________________________\n","output_type":"stream"}]},{"cell_type":"markdown","source":"## モデルの設定","metadata":{}},{"cell_type":"code","source":"# 学習のためのmodelの設定（楽する⽅法）\n# model.compile(optimizer='adam',loss='sparse_categorical_crossentropy',metrics='accuracy')\nmodel.compile(optimizer='adam',loss='sparse_categorical_crossentropy',metrics=[tf.keras.metrics.SparseCategoricalAccuracy()])","metadata":{"execution":{"iopub.status.busy":"2024-07-17T06:09:47.17749Z","iopub.execute_input":"2024-07-17T06:09:47.177787Z","iopub.status.idle":"2024-07-17T06:09:47.200151Z","shell.execute_reply.started":"2024-07-17T06:09:47.177762Z","shell.execute_reply":"2024-07-17T06:09:47.199117Z"},"trusted":true},"execution_count":12,"outputs":[]},{"cell_type":"markdown","source":"## モデルを学習","metadata":{}},{"cell_type":"code","source":"history = model.fit(train_ds,epochs=25,validation_data=valid_ds)","metadata":{"execution":{"iopub.status.busy":"2024-07-17T06:09:47.201382Z","iopub.execute_input":"2024-07-17T06:09:47.201677Z","iopub.status.idle":"2024-07-17T06:17:26.517995Z","shell.execute_reply.started":"2024-07-17T06:09:47.201652Z","shell.execute_reply":"2024-07-17T06:17:26.515652Z"},"trusted":true},"execution_count":13,"outputs":[{"name":"stdout","text":"Epoch 1/25\n300/704 [===========>..................] - ETA: 10:11 - loss: 0.6936 - sparse_categorical_accuracy: 0.5011","output_type":"stream"},{"traceback":["\u001b[0;31m---------------------------------------------------------------------------\u001b[0m","\u001b[0;31mKeyboardInterrupt\u001b[0m                         Traceback (most recent call last)","Cell \u001b[0;32mIn[13], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m history \u001b[38;5;241m=\u001b[39m \u001b[43mmodel\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mfit\u001b[49m\u001b[43m(\u001b[49m\u001b[43mtrain_ds\u001b[49m\u001b[43m,\u001b[49m\u001b[43mepochs\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;241;43m25\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43mvalidation_data\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mvalid_ds\u001b[49m\u001b[43m)\u001b[49m\n","File \u001b[0;32m/opt/conda/lib/python3.10/site-packages/keras/utils/traceback_utils.py:65\u001b[0m, in \u001b[0;36mfilter_traceback.<locals>.error_handler\u001b[0;34m(*args, **kwargs)\u001b[0m\n\u001b[1;32m     63\u001b[0m filtered_tb \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m\n\u001b[1;32m     64\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[0;32m---> 65\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mfn\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m     66\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mException\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m e:\n\u001b[1;32m     67\u001b[0m     filtered_tb \u001b[38;5;241m=\u001b[39m _process_traceback_frames(e\u001b[38;5;241m.\u001b[39m__traceback__)\n","File \u001b[0;32m/opt/conda/lib/python3.10/site-packages/keras/engine/training.py:1685\u001b[0m, in \u001b[0;36mModel.fit\u001b[0;34m(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, validation_batch_size, validation_freq, max_queue_size, workers, use_multiprocessing)\u001b[0m\n\u001b[1;32m   1677\u001b[0m \u001b[38;5;28;01mwith\u001b[39;00m tf\u001b[38;5;241m.\u001b[39mprofiler\u001b[38;5;241m.\u001b[39mexperimental\u001b[38;5;241m.\u001b[39mTrace(\n\u001b[1;32m   1678\u001b[0m     \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mtrain\u001b[39m\u001b[38;5;124m\"\u001b[39m,\n\u001b[1;32m   1679\u001b[0m     epoch_num\u001b[38;5;241m=\u001b[39mepoch,\n\u001b[0;32m   (...)\u001b[0m\n\u001b[1;32m   1682\u001b[0m     _r\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m1\u001b[39m,\n\u001b[1;32m   1683\u001b[0m ):\n\u001b[1;32m   1684\u001b[0m     callbacks\u001b[38;5;241m.\u001b[39mon_train_batch_begin(step)\n\u001b[0;32m-> 1685\u001b[0m     tmp_logs \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mtrain_function\u001b[49m\u001b[43m(\u001b[49m\u001b[43miterator\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m   1686\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m data_handler\u001b[38;5;241m.\u001b[39mshould_sync:\n\u001b[1;32m   1687\u001b[0m         context\u001b[38;5;241m.\u001b[39masync_wait()\n","File \u001b[0;32m/opt/conda/lib/python3.10/site-packages/tensorflow/python/util/traceback_utils.py:150\u001b[0m, in \u001b[0;36mfilter_traceback.<locals>.error_handler\u001b[0;34m(*args, **kwargs)\u001b[0m\n\u001b[1;32m    148\u001b[0m filtered_tb \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m\n\u001b[1;32m    149\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[0;32m--> 150\u001b[0m   \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mfn\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m    151\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mException\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m e:\n\u001b[1;32m    152\u001b[0m   filtered_tb \u001b[38;5;241m=\u001b[39m _process_traceback_frames(e\u001b[38;5;241m.\u001b[39m__traceback__)\n","File \u001b[0;32m/opt/conda/lib/python3.10/site-packages/tensorflow/python/eager/polymorphic_function/polymorphic_function.py:894\u001b[0m, in \u001b[0;36mFunction.__call__\u001b[0;34m(self, *args, **kwds)\u001b[0m\n\u001b[1;32m    891\u001b[0m compiler \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mxla\u001b[39m\u001b[38;5;124m\"\u001b[39m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_jit_compile \u001b[38;5;28;01melse\u001b[39;00m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mnonXla\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m    893\u001b[0m \u001b[38;5;28;01mwith\u001b[39;00m OptionalXlaContext(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_jit_compile):\n\u001b[0;32m--> 894\u001b[0m   result \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_call\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwds\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m    896\u001b[0m new_tracing_count \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mexperimental_get_tracing_count()\n\u001b[1;32m    897\u001b[0m without_tracing \u001b[38;5;241m=\u001b[39m (tracing_count \u001b[38;5;241m==\u001b[39m new_tracing_count)\n","File \u001b[0;32m/opt/conda/lib/python3.10/site-packages/tensorflow/python/eager/polymorphic_function/polymorphic_function.py:926\u001b[0m, in \u001b[0;36mFunction._call\u001b[0;34m(self, *args, **kwds)\u001b[0m\n\u001b[1;32m    923\u001b[0m   \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_lock\u001b[38;5;241m.\u001b[39mrelease()\n\u001b[1;32m    924\u001b[0m   \u001b[38;5;66;03m# In this case we have created variables on the first call, so we run the\u001b[39;00m\n\u001b[1;32m    925\u001b[0m   \u001b[38;5;66;03m# defunned version which is guaranteed to never create variables.\u001b[39;00m\n\u001b[0;32m--> 926\u001b[0m   \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_no_variable_creation_fn\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwds\u001b[49m\u001b[43m)\u001b[49m  \u001b[38;5;66;03m# pylint: disable=not-callable\u001b[39;00m\n\u001b[1;32m    927\u001b[0m \u001b[38;5;28;01melif\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_variable_creation_fn \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[1;32m    928\u001b[0m   \u001b[38;5;66;03m# Release the lock early so that multiple threads can perform the call\u001b[39;00m\n\u001b[1;32m    929\u001b[0m   \u001b[38;5;66;03m# in parallel.\u001b[39;00m\n\u001b[1;32m    930\u001b[0m   \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_lock\u001b[38;5;241m.\u001b[39mrelease()\n","File \u001b[0;32m/opt/conda/lib/python3.10/site-packages/tensorflow/python/eager/polymorphic_function/tracing_compiler.py:143\u001b[0m, in \u001b[0;36mTracingCompiler.__call__\u001b[0;34m(self, *args, **kwargs)\u001b[0m\n\u001b[1;32m    140\u001b[0m \u001b[38;5;28;01mwith\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_lock:\n\u001b[1;32m    141\u001b[0m   (concrete_function,\n\u001b[1;32m    142\u001b[0m    filtered_flat_args) \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_maybe_define_function(args, kwargs)\n\u001b[0;32m--> 143\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mconcrete_function\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_call_flat\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m    144\u001b[0m \u001b[43m    \u001b[49m\u001b[43mfiltered_flat_args\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mcaptured_inputs\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mconcrete_function\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mcaptured_inputs\u001b[49m\u001b[43m)\u001b[49m\n","File \u001b[0;32m/opt/conda/lib/python3.10/site-packages/tensorflow/python/eager/polymorphic_function/monomorphic_function.py:1757\u001b[0m, in \u001b[0;36mConcreteFunction._call_flat\u001b[0;34m(self, args, captured_inputs, cancellation_manager)\u001b[0m\n\u001b[1;32m   1753\u001b[0m possible_gradient_type \u001b[38;5;241m=\u001b[39m gradients_util\u001b[38;5;241m.\u001b[39mPossibleTapeGradientTypes(args)\n\u001b[1;32m   1754\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m (possible_gradient_type \u001b[38;5;241m==\u001b[39m gradients_util\u001b[38;5;241m.\u001b[39mPOSSIBLE_GRADIENT_TYPES_NONE\n\u001b[1;32m   1755\u001b[0m     \u001b[38;5;129;01mand\u001b[39;00m executing_eagerly):\n\u001b[1;32m   1756\u001b[0m   \u001b[38;5;66;03m# No tape is watching; skip to running the function.\u001b[39;00m\n\u001b[0;32m-> 1757\u001b[0m   \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_build_call_outputs(\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_inference_function\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mcall\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m   1758\u001b[0m \u001b[43m      \u001b[49m\u001b[43mctx\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mcancellation_manager\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mcancellation_manager\u001b[49m\u001b[43m)\u001b[49m)\n\u001b[1;32m   1759\u001b[0m forward_backward \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_select_forward_and_backward_functions(\n\u001b[1;32m   1760\u001b[0m     args,\n\u001b[1;32m   1761\u001b[0m     possible_gradient_type,\n\u001b[1;32m   1762\u001b[0m     executing_eagerly)\n\u001b[1;32m   1763\u001b[0m forward_function, args_with_tangents \u001b[38;5;241m=\u001b[39m forward_backward\u001b[38;5;241m.\u001b[39mforward()\n","File \u001b[0;32m/opt/conda/lib/python3.10/site-packages/tensorflow/python/eager/polymorphic_function/monomorphic_function.py:381\u001b[0m, in \u001b[0;36m_EagerDefinedFunction.call\u001b[0;34m(self, ctx, args, cancellation_manager)\u001b[0m\n\u001b[1;32m    379\u001b[0m \u001b[38;5;28;01mwith\u001b[39;00m _InterpolateFunctionError(\u001b[38;5;28mself\u001b[39m):\n\u001b[1;32m    380\u001b[0m   \u001b[38;5;28;01mif\u001b[39;00m cancellation_manager \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[0;32m--> 381\u001b[0m     outputs \u001b[38;5;241m=\u001b[39m \u001b[43mexecute\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mexecute\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m    382\u001b[0m \u001b[43m        \u001b[49m\u001b[38;5;28;43mstr\u001b[39;49m\u001b[43m(\u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43msignature\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mname\u001b[49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m    383\u001b[0m \u001b[43m        \u001b[49m\u001b[43mnum_outputs\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_num_outputs\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m    384\u001b[0m \u001b[43m        \u001b[49m\u001b[43minputs\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m    385\u001b[0m \u001b[43m        \u001b[49m\u001b[43mattrs\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mattrs\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m    386\u001b[0m \u001b[43m        \u001b[49m\u001b[43mctx\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mctx\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m    387\u001b[0m   \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m    388\u001b[0m     outputs \u001b[38;5;241m=\u001b[39m execute\u001b[38;5;241m.\u001b[39mexecute_with_cancellation(\n\u001b[1;32m    389\u001b[0m         \u001b[38;5;28mstr\u001b[39m(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39msignature\u001b[38;5;241m.\u001b[39mname),\n\u001b[1;32m    390\u001b[0m         num_outputs\u001b[38;5;241m=\u001b[39m\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_num_outputs,\n\u001b[0;32m   (...)\u001b[0m\n\u001b[1;32m    393\u001b[0m         ctx\u001b[38;5;241m=\u001b[39mctx,\n\u001b[1;32m    394\u001b[0m         cancellation_manager\u001b[38;5;241m=\u001b[39mcancellation_manager)\n","File \u001b[0;32m/opt/conda/lib/python3.10/site-packages/tensorflow/python/eager/execute.py:52\u001b[0m, in \u001b[0;36mquick_execute\u001b[0;34m(op_name, num_outputs, inputs, attrs, ctx, name)\u001b[0m\n\u001b[1;32m     50\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[1;32m     51\u001b[0m   ctx\u001b[38;5;241m.\u001b[39mensure_initialized()\n\u001b[0;32m---> 52\u001b[0m   tensors \u001b[38;5;241m=\u001b[39m \u001b[43mpywrap_tfe\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mTFE_Py_Execute\u001b[49m\u001b[43m(\u001b[49m\u001b[43mctx\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_handle\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mdevice_name\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mop_name\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m     53\u001b[0m \u001b[43m                                      \u001b[49m\u001b[43minputs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mattrs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mnum_outputs\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m     54\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m core\u001b[38;5;241m.\u001b[39m_NotOkStatusException \u001b[38;5;28;01mas\u001b[39;00m e:\n\u001b[1;32m     55\u001b[0m   \u001b[38;5;28;01mif\u001b[39;00m name \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n","\u001b[0;31mKeyboardInterrupt\u001b[0m: "],"ename":"KeyboardInterrupt","evalue":"","output_type":"error"}]},{"cell_type":"markdown","source":"## 検証用データで正解率を予測","metadata":{}},{"cell_type":"code","source":"# # 予測結果の正解率の計算\n# valid_pd = pd.DataFrame(valid_ds)\n\n# VALID = valid_pd.drop(['label'], axis=1, inplace=True)\n# VALID_LABEL = valid_pd['label'] # 予測する特徴量\n# Predictions = tf.argmax(VALID, axis=1)\n# Predictions\n# valid_accuracy = tf.reduce_mean(tf.cast(Predictions == VALID_LABEL, tf.float32))\n# print(\"Valid Accuracy:\", valid_accuracy.numpy())\n\nimport matplotlib.pyplot as plt\n\n# 学習曲線の描画\nplt.figure(figsize=(12, 4))\n\nplt.subplot(1, 2, 1)\nplt.plot(history.history['loss'], label='Train Loss')\nplt.plot(history.history['val_loss'], label='Validation Loss')\nplt.legend()\nplt.grid()\nplt.title('Loss evolution')\n\nplt.subplot(1, 2, 2)\nplt.plot(history.history['sparse_categorical_accuracy'], label='Train Accuracy')\nplt.plot(history.history['val_sparse_categorical_accuracy'], label='Validation Accuracy')\nplt.legend()\nplt.grid()\nplt.title('Accuracy evolution')\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-07-17T06:17:26.519891Z","iopub.status.idle":"2024-07-17T06:17:26.520331Z","shell.execute_reply.started":"2024-07-17T06:17:26.520142Z","shell.execute_reply":"2024-07-17T06:17:26.520163Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Train Accuracy:\", history.history['sparse_categorical_accuracy'][-1])\nprint(\"Validation Accuracy:\", history.history['val_sparse_categorical_accuracy'][-1])\nprint(len(train_x),len(valid_x))","metadata":{"execution":{"iopub.status.busy":"2024-07-17T06:17:26.52257Z","iopub.status.idle":"2024-07-17T06:17:26.522999Z","shell.execute_reply.started":"2024-07-17T06:17:26.522784Z","shell.execute_reply":"2024-07-17T06:17:26.522805Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# テストデータの準備","metadata":{}},{"cell_type":"code","source":"# -------------------------------------------------------------------\n# 定数の定義\n# -------------------------------------------------------------------\nTESTDIR = \"/kaggle/working/data/test\"\n# MODEL_DIR = \"/content/drive/MyDrive\"\nCATEGORIES = [\"cats\", \"dogs\"]\n\n# -------------------------------------------------------------------\n# メイン処理（画像を読込み、犬猫データセットを設定）\n# -------------------------------------------------------------------\n#犬猫データセットを定義\nfeature_test_data = []\nlabel_test_data = []\nfeature = []\n    \n# 平均画像サイズ\ntarget_size = (128, 128)\nprint(f'target_size = {target_size}')\n\n#画像を読込み、犬猫データセットを設定\nfor image_name in os.listdir(TESTDIR):\n    try:\n        img_file = cv2.imread(os.path.join(TESTDIR, image_name))# 画像読み込み\n        gray_image = cv2.cvtColor(img_file, cv2.COLOR_BGR2GRAY)\n        resize_file = cv2.resize(gray_image, (target_size))# 画像のリサイズ\n        feature_test_data.append(resize_file)\n        #label_test_data.append(class_num)# 画像データ(犬 or 猫)、ラベル情報(0 or 1)を追加\n    except Exception as e:\n        #画像の読込みエラーはスルー（スキップして次の画像へ）\n        print(\"skiped.\")\n        pass\nprint(f\"finish.\")","metadata":{"execution":{"iopub.status.busy":"2024-07-17T06:17:26.524768Z","iopub.status.idle":"2024-07-17T06:17:26.525206Z","shell.execute_reply.started":"2024-07-17T06:17:26.525Z","shell.execute_reply":"2024-07-17T06:17:26.525022Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## モデルに入力できるようにデータの形を変形（最終調整）","metadata":{}},{"cell_type":"code","source":"# テストデータの変換\n# X_test = X_test.reshape(X_test.shape[0], 28, 28)\n\ndef convert_data_test(x):\n    batch,a,b = x.shape # データの形（シェイプ）を取得（32×28×28）\n    x = tf.reshape(x,(batch,a,b,1)) # 1チャンネル分増やし，4次元テンソルに変換\n    x = tf.cast(x,tf.float32) # float32に変換\n    x /= 255. # 正規化\n    return x\n\ntest_ds = tf.data.Dataset.from_tensor_slices(feature_test_data) # 検証用も同様に処理\nAUTOTUNE = tf.data.experimental.AUTOTUNE # 自動チューニング設定 (おまじない)\n\ntest_ds = test_ds.batch(32) # 検証データはシャッフルする必要がないので,パッチ化のみの処理\ntest_ds = test_ds.map(lambda x: tf.py_function(convert_data_test, [x], Tout=[tf.float32]))\n\n#X_test, _ = convert_data(X_test, y_test)","metadata":{"execution":{"iopub.status.busy":"2024-07-17T06:17:26.527915Z","iopub.status.idle":"2024-07-17T06:17:26.528586Z","shell.execute_reply.started":"2024-07-17T06:17:26.528289Z","shell.execute_reply":"2024-07-17T06:17:26.528317Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 確認用","metadata":{}},{"cell_type":"code","source":"test_ds","metadata":{"execution":{"iopub.status.busy":"2024-07-17T06:17:26.53058Z","iopub.status.idle":"2024-07-17T06:17:26.531414Z","shell.execute_reply.started":"2024-07-17T06:17:26.531121Z","shell.execute_reply":"2024-07-17T06:17:26.531152Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# データの予測","metadata":{}},{"cell_type":"code","source":"# テストデータの予測\ny_pred = model.predict(test_ds)\n\n# 予測結果の正解率の計算\npredictions = tf.argmax(y_pred, axis=1)\npredictions\n\n","metadata":{"execution":{"iopub.status.busy":"2024-07-17T06:17:26.533136Z","iopub.status.idle":"2024-07-17T06:17:26.534127Z","shell.execute_reply.started":"2024-07-17T06:17:26.533807Z","shell.execute_reply":"2024-07-17T06:17:26.533836Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(predictions)","metadata":{"execution":{"iopub.status.busy":"2024-07-17T06:17:26.535817Z","iopub.status.idle":"2024-07-17T06:17:26.536237Z","shell.execute_reply.started":"2024-07-17T06:17:26.536039Z","shell.execute_reply":"2024-07-17T06:17:26.536059Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 内容確認","metadata":{}},{"cell_type":"code","source":"y_pred","metadata":{"execution":{"iopub.status.busy":"2024-07-17T06:17:26.538211Z","iopub.status.idle":"2024-07-17T06:17:26.538722Z","shell.execute_reply.started":"2024-07-17T06:17:26.538517Z","shell.execute_reply":"2024-07-17T06:17:26.538538Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 結果を提出できる形に修正","metadata":{}},{"cell_type":"code","source":"pred_pd = pd.DataFrame(y_pred)\n# id列を作成\npred_pd.insert(0, 'id', range(1, len(pred_pd) + 1))\npred_pd.columns\npred_pd = pred_pd.drop([0], axis=1)\npred_pd = pred_pd.rename(columns={1: 'label'})\npred_pd","metadata":{"execution":{"iopub.status.busy":"2024-07-17T06:17:26.540663Z","iopub.status.idle":"2024-07-17T06:17:26.541209Z","shell.execute_reply.started":"2024-07-17T06:17:26.540919Z","shell.execute_reply":"2024-07-17T06:17:26.540945Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred_pd[:16]","metadata":{"execution":{"iopub.status.busy":"2024-07-17T06:17:26.542756Z","iopub.status.idle":"2024-07-17T06:17:26.543294Z","shell.execute_reply.started":"2024-07-17T06:17:26.54303Z","shell.execute_reply":"2024-07-17T06:17:26.543057Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 結果を保存","metadata":{}},{"cell_type":"code","source":"sub = pd.read_csv('/kaggle/input/dogs-vs-cats-redux-kernels-edition/sample_submission.csv') # ひな形ファイルを読み込む\nprint(sub.shape)\n# subのlabel列をtest_predictedのlabel列で上書きする\nsub['label'] = pred_pd['label']\n# 結果を表示\nsub\nsub.to_csv('submission.csv', index=False) # 変換したファイルを保存","metadata":{"execution":{"iopub.status.busy":"2024-07-17T06:17:26.544661Z","iopub.status.idle":"2024-07-17T06:17:26.545185Z","shell.execute_reply.started":"2024-07-17T06:17:26.544905Z","shell.execute_reply":"2024-07-17T06:17:26.54493Z"},"trusted":true},"execution_count":null,"outputs":[]}]}