{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"<a id='start'></a>\n# 目次\n### 5.班として建てた仮設説明\n* [学習データとテストデータのダウンロード](#download)\n * [ダウンロードデータの確認](#show_img)\n* [データ分析](#analyze)\n * [犬と猫のラベル比](#label_num)\n\n### 6.説明変数として何を選んだか？\n* [学習データ と 検証データ と テストデータ の生成](#split)\n * [学習データの確認](#X_data)\n   * [CNNへ入力する学習データ](#traindata)\n   * [CNNへ入力する学習データの犬と猫のラベル比](#trainlabel)\n\n### 7.選んだ分析手法とその理由\n* [学習モデル構築](#build)\n\n### 8.訓練データに対する作業結果\n* [学習](#train)\n * [分類モデルへの入力データセットの作成](#dataset)\n * [学習の実行](#training)\n * [学習ログ](#log)\n\n### 9.テストデータに対する作業内容\n* [テスト](#test)\n * [検証データでの予測](#val)\n * [検証データでの精度](#acc)\n* [テストデータ](#testdata)\n * [submit サンプル](#sample)\n * [submit データの作成](#submit)\n\n### 10.Kaggleでの評価点\n* [Kaggleでのsubmit結果メモ](#memo)","metadata":{"papermill":{"duration":0.010769,"end_time":"2023-07-17T19:34:08.791985","exception":false,"start_time":"2023-07-17T19:34:08.781216","status":"completed"},"tags":[]}},{"cell_type":"markdown","source":"<a id=\"download\"></a>\n# 学習データとテストデータのダウンロード\n[目次へ戻る](#start)\n\n「dogs-vs-cats-redux-kernels-edition」を Input に設置して，train.zip と test.zip を解凍．  \nOutput に解凍結果を train と test　のディレクトリに保存","metadata":{"papermill":{"duration":0.009863,"end_time":"2023-07-17T19:34:08.812067","exception":false,"start_time":"2023-07-17T19:34:08.802204","status":"completed"},"tags":[]}},{"cell_type":"code","source":"import zipfile\nimport glob\nimport os\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport collections\nfrom PIL import Image\n\ntrain_dir = 'train'\nwith zipfile.ZipFile('/kaggle/input/dogs-vs-cats-redux-kernels-edition/train.zip', 'r') as train_zip:\n    train_zip.extractall('')\n\ntrain_list = glob.glob(os.path.join(train_dir,'*.jpg'))\nprint(f\"Train Data: {len(train_list)}\")\n\nbase_dir = '/kaggle/working'\ntrain_dir = os.path.join(base_dir, 'train')\n\ntest_dir = 'test'\nwith zipfile.ZipFile('/kaggle/input/dogs-vs-cats-redux-kernels-edition/test.zip', 'r') as test_zip:\n    test_zip.extractall('')\n\ntest_list = glob.glob(os.path.join(test_dir,'*.jpg'))\nprint(f\"Test Data: {len(test_list)}\")\n\nbase_dir = '/kaggle/working'\ntest_dir = os.path.join(base_dir, 'test')","metadata":{"execution":{"iopub.execute_input":"2023-07-17T19:34:08.834712Z","iopub.status.busy":"2023-07-17T19:34:08.833696Z","iopub.status.idle":"2023-07-17T19:34:23.538835Z","shell.execute_reply":"2023-07-17T19:34:23.537627Z"},"papermill":{"duration":14.718899,"end_time":"2023-07-17T19:34:23.540992","exception":false,"start_time":"2023-07-17T19:34:08.822093","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### 実行結果補足メモ：\nTrain Data: 25000  \n→「dogs-vs-cats-redux-kernels-edition」に 学習データ が 25000件，存在する．  \nTest Data: 12500  \n→「dogs-vs-cats-redux-kernels-edition」に テストデータ が 12500件，存在する．","metadata":{"papermill":{"duration":0.010184,"end_time":"2023-07-17T19:34:23.561712","exception":false,"start_time":"2023-07-17T19:34:23.551528","status":"completed"},"tags":[]}},{"cell_type":"markdown","source":"<a id='show_img'></a>\n## ダウンロードデータの確認\n#### 学習データとテストデータの中身を1つ確認","metadata":{"papermill":{"duration":0.010113,"end_time":"2023-07-17T19:34:23.582077","exception":false,"start_time":"2023-07-17T19:34:23.571964","status":"completed"},"tags":[]}},{"cell_type":"code","source":"print(f'学習データ数：{len(train_list)}', f'先頭のデータ：{train_list[0]}')\nprint(f'テストデータ数：{len(test_list)}', f'先頭のデータ：{test_list[0]}')","metadata":{"execution":{"iopub.execute_input":"2023-07-17T19:34:23.606049Z","iopub.status.busy":"2023-07-17T19:34:23.604429Z","iopub.status.idle":"2023-07-17T19:34:23.611124Z","shell.execute_reply":"2023-07-17T19:34:23.609797Z"},"papermill":{"duration":0.02032,"end_time":"2023-07-17T19:34:23.613163","exception":false,"start_time":"2023-07-17T19:34:23.592843","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### 実行結果補足メモ：\n学習データには，犬と猫のラベル付けされた画像パスを持つ  \nテストデータには，ラベル付けされていない画像パスを持つ\n\nラベル付けとは，画像に犬か猫かの情報を付与すること","metadata":{"papermill":{"duration":0.010109,"end_time":"2023-07-17T19:34:23.633594","exception":false,"start_time":"2023-07-17T19:34:23.623485","status":"completed"},"tags":[]}},{"cell_type":"markdown","source":"### 学習データの中身をランダムに9つを表示","metadata":{"papermill":{"duration":0.010171,"end_time":"2023-07-17T19:34:23.654423","exception":false,"start_time":"2023-07-17T19:34:23.644252","status":"completed"},"tags":[]}},{"cell_type":"code","source":"labels = [path.split('/')[-1].split('.')[0] for path in train_list]\nrandom_idx = np.random.randint(1, len(train_list), size=9)\nfig, axes = plt.subplots(3, 3, figsize=(16, 12))\n\nfor idx, ax in enumerate(axes.ravel()):\n    img = Image.open(train_list[idx])\n    ax.set_title(labels[idx])\n    ax.imshow(img)","metadata":{"execution":{"iopub.execute_input":"2023-07-17T19:34:23.676586Z","iopub.status.busy":"2023-07-17T19:34:23.676253Z","iopub.status.idle":"2023-07-17T19:34:27.099716Z","shell.execute_reply":"2023-07-17T19:34:27.098870Z"},"papermill":{"duration":3.448661,"end_time":"2023-07-17T19:34:27.113377","exception":false,"start_time":"2023-07-17T19:34:23.664716","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"analyze\"></a>\n# データ分析\n[戻る](#start)","metadata":{"papermill":{"duration":0.026522,"end_time":"2023-07-17T19:34:27.167499","exception":false,"start_time":"2023-07-17T19:34:27.140977","status":"completed"},"tags":[]}},{"cell_type":"markdown","source":"<a id='label_num'></a>\n## 犬と猫のラベル比","metadata":{"papermill":{"duration":0.025426,"end_time":"2023-07-17T19:34:27.218622","exception":false,"start_time":"2023-07-17T19:34:27.193196","status":"completed"},"tags":[]}},{"cell_type":"code","source":"c = collections.Counter(labels)\nprint(c)\n\nplt.xlabel('labels')\nplt.ylabel('counts')\n\nleft = np.array([0, 1])\nlabel = ['dog', 'cat']\nheight = np.array([c['dog'], c['cat']])\nplt.bar(left, height, tick_label=label)","metadata":{"execution":{"iopub.execute_input":"2023-07-17T19:34:27.272660Z","iopub.status.busy":"2023-07-17T19:34:27.272329Z","iopub.status.idle":"2023-07-17T19:34:27.497005Z","shell.execute_reply":"2023-07-17T19:34:27.496052Z"},"papermill":{"duration":0.254605,"end_time":"2023-07-17T19:34:27.499144","exception":false,"start_time":"2023-07-17T19:34:27.244539","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### 実行結果補足メモ：\nCounter({'cat':12500, 'dog':12500}) は猫のラベルを持つ画像が 12500件，犬のラベルを持つ画像が 12500件，存在するということを表す．  \nまた，上記のヒストグラムはそれを視覚的に表したものである．","metadata":{"papermill":{"duration":0.026588,"end_time":"2023-07-17T19:34:27.553293","exception":false,"start_time":"2023-07-17T19:34:27.526705","status":"completed"},"tags":[]}},{"cell_type":"markdown","source":"<a id=\"split\"></a>\n# 学習データ と 検証データ と テストデータ の生成\n[目次へ戻る](#start)\n\n2019年にGoogle社によって開発された事前学習済みモデル「EfficientNet」 と呼ばれる畳み込みニューラルネットワーク（CNN）を用いて，学習・分類を行うために入力データを作成する．  \nダウンロードした学習データを，CNN用の学習データと検証データに分ける．  \nその際に，画像データを縦横のサイズを224(px)に変換し，色の白黒（グレースケール）化を行う．  \n画像サイズの変換理由は，今回使う事前学習済みモデル「EfficientNet」の推奨画像サイズが縦横 224(px)となっているため．  \n色をRGBではなく，グレースケールで表すのはKaggleの実行可能メモリ数に収めるため．  \nまた，画像ラベルの dog：0 cat：1 とするのは文字のまま入力することができないため．","metadata":{"papermill":{"duration":0.027476,"end_time":"2023-07-17T19:34:27.607220","exception":false,"start_time":"2023-07-17T19:34:27.579744","status":"completed"},"tags":[]}},{"cell_type":"code","source":"from PIL import ImageFile\nfrom sklearn.model_selection import train_test_split\n\nclasses = {'dog':0, 'cat':1} # ラベルのdogを0，catを1とする\nimage_size = 224 # 画像サイズ 224px\ndim = 1 # 色の種類(0-255で白黒を表す)\n\nX_train = [] # 学習データの特徴量\ny_train = [] # 学習データのラベル\nX_test  = [] # テストデータの特徴量\n\n# 学習データと検証データの作成\ntrain_labels = [path.split('/')[-1].split('.')[0] for path in train_list]\nfor i, img in enumerate(train_list) :\n    image = Image.open(img)\n    image = image.convert(\"L\") # グレースケール化\n    image = image.resize((image_size, image_size)) # 画像サイズの変換\n    data = np.asarray(image)\n    X_train.append(data)\n    y_train.append(classes[train_labels[i]])\nX_train = np.array(X_train)\ny_train = np.array(y_train)\n# 学習データと検証データに分ける\nX_train, X_val, y_train, y_val = train_test_split(X_train, y_train, train_size=0.7, shuffle=True, random_state=0)\n\n# テストデータの作成\nfor i, img in enumerate(test_list) :\n    image = Image.open(img)\n    image = image.convert(\"L\") # グレースケール化\n    image = image.resize((image_size, image_size)) # 画像サイズの変換\n    data = np.asarray(image)\n    X_test.append(data)\nX_test  = np.array(X_test)","metadata":{"execution":{"iopub.execute_input":"2023-07-17T19:34:27.664063Z","iopub.status.busy":"2023-07-17T19:34:27.662407Z","iopub.status.idle":"2023-07-17T19:35:58.713084Z","shell.execute_reply":"2023-07-17T19:35:58.712071Z"},"papermill":{"duration":91.081554,"end_time":"2023-07-17T19:35:58.716048","exception":false,"start_time":"2023-07-17T19:34:27.634494","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('学習・検証・テストデータ')\nprint(f'学習データ数：{len(X_train)} 検証データ数:{len(X_val)} テストデータ数：{len(X_test)}')\nprint(f'学習ラベル数：{len(y_train)} 検証ラベル数:{len(y_val)}')\nprint('1つのデータ')\nprint(f'学習の次元数：{X_train[0].shape} 検証の次元数：{X_val[0].shape} テストの次元数：{X_test[0].shape}')\nprint(f'学習ラベル：{y_train[0]} 検証ラベル：{y_val[0]}')\n\n# メモリ確保のため変数の削除\ndel train_list\ndel train_labels\ndel image\ndel data","metadata":{"execution":{"iopub.execute_input":"2023-07-17T19:35:58.773884Z","iopub.status.busy":"2023-07-17T19:35:58.773524Z","iopub.status.idle":"2023-07-17T19:35:58.785944Z","shell.execute_reply":"2023-07-17T19:35:58.784990Z"},"papermill":{"duration":0.043677,"end_time":"2023-07-17T19:35:58.788608","exception":false,"start_time":"2023-07-17T19:35:58.744931","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### 実行結果補足メモ：\nそれぞれ，CNNに入力する際のデータ数を表す．  \n学習の特徴量や次元数は以下の実行結果を参考にする．","metadata":{"papermill":{"duration":0.026436,"end_time":"2023-07-17T19:35:58.841051","exception":false,"start_time":"2023-07-17T19:35:58.814615","status":"completed"},"tags":[]}},{"cell_type":"markdown","source":"<a id='X_data'></a>\n## 学習データの確認\n[目次へ戻る](#start)","metadata":{"papermill":{"duration":0.027927,"end_time":"2023-07-17T19:35:58.896046","exception":false,"start_time":"2023-07-17T19:35:58.868119","status":"completed"},"tags":[]}},{"cell_type":"markdown","source":"<a id='traindata'></a>\n### CNNへ入力する学習データ\n学習データの中身を1つ確認","metadata":{"papermill":{"duration":0.029018,"end_time":"2023-07-17T19:35:58.952939","exception":false,"start_time":"2023-07-17T19:35:58.923921","status":"completed"},"tags":[]}},{"cell_type":"code","source":"# 学習データの中身を1つ確認\nprint(f'データの中身:\\n{X_train[0]}')\nplt.imshow(X_train[0])\nplt.show()","metadata":{"execution":{"iopub.execute_input":"2023-07-17T19:35:59.016984Z","iopub.status.busy":"2023-07-17T19:35:59.016538Z","iopub.status.idle":"2023-07-17T19:35:59.350501Z","shell.execute_reply":"2023-07-17T19:35:59.349650Z"},"papermill":{"duration":0.371029,"end_time":"2023-07-17T19:35:59.354884","exception":false,"start_time":"2023-07-17T19:35:58.983855","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### 実行結果補足メモ：\nデータの中身は224×224(px)の白黒(0-255)のデータを持つ","metadata":{"papermill":{"duration":0.034269,"end_time":"2023-07-17T19:35:59.424166","exception":false,"start_time":"2023-07-17T19:35:59.389897","status":"completed"},"tags":[]}},{"cell_type":"markdown","source":"<a id='trainlabel'></a>\n### CNNへ入力する学習データの犬と猫のラベル比","metadata":{"papermill":{"duration":0.029697,"end_time":"2023-07-17T19:35:59.485283","exception":false,"start_time":"2023-07-17T19:35:59.455586","status":"completed"},"tags":[]}},{"cell_type":"code","source":"c = collections.Counter(y_train)\n# 0:犬 1:猫\nprint(c)\n\nplt.xlabel('labels')\nplt.ylabel('counts')\nleft = np.array([0, 1])\nlabel = ['dog', 'cat']\nheight = np.array([c[0], c[1]])\nplt.bar(left, height, tick_label=label)","metadata":{"execution":{"iopub.execute_input":"2023-07-17T19:35:59.548286Z","iopub.status.busy":"2023-07-17T19:35:59.547590Z","iopub.status.idle":"2023-07-17T19:35:59.754697Z","shell.execute_reply":"2023-07-17T19:35:59.753762Z"},"papermill":{"duration":0.241469,"end_time":"2023-07-17T19:35:59.756736","exception":false,"start_time":"2023-07-17T19:35:59.515267","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### 実行結果補足メモ：\nCounter({'cat':8812, 'dog':8688}) は猫のラベルを持つ画像が 8812件，犬のラベルを持つ画像が 8688件，存在するということを表す．  \nまた，上記のヒストグラムはそれを視覚的に表したものである．","metadata":{"papermill":{"duration":0.030627,"end_time":"2023-07-17T19:35:59.819668","exception":false,"start_time":"2023-07-17T19:35:59.789041","status":"completed"},"tags":[]}},{"cell_type":"markdown","source":"<a id='build'></a>\n# モデル構築\n[目次へ戻る](#start)\n\n事前学習済みモデル「EfficientNetB0」を用いて画像分類モデルを構築する．  \n「EfficientNetB0」を元に出力層に，「GlobalAveragePooling2D」と「Dense」を加える．  \n\n学習パラメータ：  \n損失関数：sparse_categorical_crossentropy  \n最適化関数：Adam  \n学習率：1e-5  \nバッチサイズ：32  \nエポック数：10  \n\n訓練データ：X_train, y_train, X_val，y_val  \nテストデータ：X_test  ","metadata":{"papermill":{"duration":0.030392,"end_time":"2023-07-17T19:35:59.880188","exception":false,"start_time":"2023-07-17T19:35:59.849796","status":"completed"},"tags":[]}},{"cell_type":"code","source":"from keras.models import Sequential ,Model\nfrom keras.layers import Conv2D, MaxPooling2D, GlobalAveragePooling2D\nfrom keras.layers import Input, Activation, Dropout, Flatten, Dense\nfrom keras.optimizers import RMSprop\nimport tensorflow as tf\nfrom sklearn.metrics import accuracy_score\n\nimport keras\nimport numpy as np","metadata":{"execution":{"iopub.execute_input":"2023-07-17T19:35:59.942394Z","iopub.status.busy":"2023-07-17T19:35:59.942041Z","iopub.status.idle":"2023-07-17T19:36:08.138112Z","shell.execute_reply":"2023-07-17T19:36:08.137143Z"},"papermill":{"duration":8.229935,"end_time":"2023-07-17T19:36:08.140519","exception":false,"start_time":"2023-07-17T19:35:59.910584","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_model(image_size, num_classes):\n    # 画像を受け取る層\n    inputs = Input(shape = image_size)\n    # EfficientNetB0\n    x = tf.keras.applications.efficientnet.EfficientNetB0(include_top = False)(inputs)\n    # １次元に変換する層\n    x = GlobalAveragePooling2D()(x) # CNNの出力を横並びにするのではなく、加算\n    \n    # 最終出力層\n    outputs = Dense(units = num_classes, activation = \"softmax\")(x)\n\n    # 定義した層を使ってモデル作成\n    model = Model(inputs = inputs, outputs = outputs)\n    # 最適化手法と損失関数を定義\n    model.compile(\n        optimizer = tf.keras.optimizers.Adam(learning_rate = 1e-5),\n        loss = \"sparse_categorical_crossentropy\",\n        metrics=['accuracy']\n    )\n    return model\n\n# モデルの呼び出し\ntf.keras.backend.clear_session()\nmodel = get_model((image_size, image_size, dim), 2)\nmodel.summary()","metadata":{"execution":{"iopub.execute_input":"2023-07-17T19:36:08.206129Z","iopub.status.busy":"2023-07-17T19:36:08.204162Z","iopub.status.idle":"2023-07-17T19:36:16.208570Z","shell.execute_reply":"2023-07-17T19:36:16.207454Z"},"papermill":{"duration":8.039512,"end_time":"2023-07-17T19:36:16.211610","exception":false,"start_time":"2023-07-17T19:36:08.172098","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id='train'></a>\n# 学習\n[目次へ戻る](#start)","metadata":{"papermill":{"duration":0.032815,"end_time":"2023-07-17T19:36:16.279074","exception":false,"start_time":"2023-07-17T19:36:16.246259","status":"completed"},"tags":[]}},{"cell_type":"markdown","source":"<a id='dataset'></a>\n### 分類モデルへの入力データセットの作成","metadata":{"papermill":{"duration":0.032332,"end_time":"2023-07-17T19:36:16.343692","exception":false,"start_time":"2023-07-17T19:36:16.311360","status":"completed"},"tags":[]}},{"cell_type":"code","source":"# 分類モデルへの入力データ用の作成\nBATCH_SIZE = 32 # 学習バッチサイズ\n\ntrain_dataset = tf.data.Dataset.from_tensor_slices((X_train, y_train)).batch(BATCH_SIZE)\n# メモリ確保のため変数の削除\ndel X_train\ndel y_train\n\ntest_dataset = tf.data.Dataset.from_tensor_slices((X_val, y_val)).batch(BATCH_SIZE)\ndel X_val # メモリ確保のため変数の削除\n\ntrain_dataset = train_dataset.shuffle(12345)","metadata":{"execution":{"iopub.execute_input":"2023-07-17T19:36:16.411175Z","iopub.status.busy":"2023-07-17T19:36:16.410781Z","iopub.status.idle":"2023-07-17T19:36:18.696525Z","shell.execute_reply":"2023-07-17T19:36:18.695559Z"},"papermill":{"duration":2.322673,"end_time":"2023-07-17T19:36:18.698917","exception":false,"start_time":"2023-07-17T19:36:16.376244","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id='training'></a>\n### 学習の実行","metadata":{"papermill":{"duration":0.032878,"end_time":"2023-07-17T19:36:18.765653","exception":false,"start_time":"2023-07-17T19:36:18.732775","status":"completed"},"tags":[]}},{"cell_type":"code","source":"#　学習中で最も損失が低いものを保存\ncheckpoint = tf.keras.callbacks.ModelCheckpoint(\n    \"best_weight.h5\",     # ファイル名\n    monitor = \"val_loss\", # 参照するloss\n    mode = \"min\",         # monitorが最小のときに保存\n    save_best_only = True,\n    save_weights_only = True\n)\n\n# 学習の実行\nhistory = model.fit(\n    train_dataset,\n    validation_data = test_dataset,\n    epochs = 10,\n    callbacks = [checkpoint]\n)","metadata":{"execution":{"iopub.execute_input":"2023-07-17T19:36:18.831414Z","iopub.status.busy":"2023-07-17T19:36:18.831081Z","iopub.status.idle":"2023-07-17T20:03:19.762309Z","shell.execute_reply":"2023-07-17T20:03:19.761310Z"},"papermill":{"duration":1620.967286,"end_time":"2023-07-17T20:03:19.764930","exception":false,"start_time":"2023-07-17T19:36:18.797644","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id='log'></a>\n### 学習ログ","metadata":{"papermill":{"duration":0.489567,"end_time":"2023-07-17T20:03:20.701397","exception":false,"start_time":"2023-07-17T20:03:20.211830","status":"completed"},"tags":[]}},{"cell_type":"code","source":"# 学習経過の出力\nplt.plot(history.history[\"loss\"], label = \"train\")\nplt.plot(history.history[\"val_loss\"], label = \"valid\")\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.execute_input":"2023-07-17T20:03:21.605534Z","iopub.status.busy":"2023-07-17T20:03:21.604527Z","iopub.status.idle":"2023-07-17T20:03:21.861670Z","shell.execute_reply":"2023-07-17T20:03:21.860737Z"},"papermill":{"duration":0.71908,"end_time":"2023-07-17T20:03:21.863738","exception":false,"start_time":"2023-07-17T20:03:21.144658","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id='test'></a>\n# テスト\n\n[目次へ戻る](#start)","metadata":{"papermill":{"duration":0.457665,"end_time":"2023-07-17T20:03:22.809507","exception":false,"start_time":"2023-07-17T20:03:22.351842","status":"completed"},"tags":[]}},{"cell_type":"markdown","source":"<a id='val'></a>\n### 検証データでの予測","metadata":{"papermill":{"duration":0.444333,"end_time":"2023-07-17T20:03:23.792517","exception":false,"start_time":"2023-07-17T20:03:23.348184","status":"completed"},"tags":[]}},{"cell_type":"code","source":"# 学習結果で良いモデルを用いる\nmodel.load_weights(\"best_weight.h5\")\npreds = model.predict(test_dataset)\nprint(f'予測確率[dog：cat]：{preds[0]}')\nprint(f'正解ラベル：{y_val[0]}')\npred_labels = preds.argmax(axis = 1)","metadata":{"execution":{"iopub.execute_input":"2023-07-17T20:03:24.701582Z","iopub.status.busy":"2023-07-17T20:03:24.701232Z","iopub.status.idle":"2023-07-17T20:03:38.910454Z","shell.execute_reply":"2023-07-17T20:03:38.909367Z"},"papermill":{"duration":14.664567,"end_time":"2023-07-17T20:03:38.912742","exception":false,"start_time":"2023-07-17T20:03:24.248175","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id='acc'></a>\n### 検証データでの精度","metadata":{"papermill":{"duration":0.465708,"end_time":"2023-07-17T20:03:39.907948","exception":false,"start_time":"2023-07-17T20:03:39.442240","status":"completed"},"tags":[]}},{"cell_type":"code","source":"print(accuracy_score(pred_labels, y_val.reshape(-1)))","metadata":{"execution":{"iopub.execute_input":"2023-07-17T20:03:40.844125Z","iopub.status.busy":"2023-07-17T20:03:40.843724Z","iopub.status.idle":"2023-07-17T20:03:40.850988Z","shell.execute_reply":"2023-07-17T20:03:40.849940Z"},"papermill":{"duration":0.478499,"end_time":"2023-07-17T20:03:40.854111","exception":false,"start_time":"2023-07-17T20:03:40.375612","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id='testdata'></a>\n# テストデータ\n[目次へ戻る](#start)","metadata":{"papermill":{"duration":0.51846,"end_time":"2023-07-17T20:03:41.839233","exception":false,"start_time":"2023-07-17T20:03:41.320773","status":"completed"},"tags":[]}},{"cell_type":"markdown","source":"<a id='sample'></a>\n### submit サンプル\n#### kaggleで提出データのサンプルと評価方法  \n提出物はログの損失に基づいてスコア付けされる．\n\\begin{align}\nLogLoss = -\\frac{1}{n}\\sum_{i=1}^{n}[y_{i}log(\\hat{y_i})+(1-y_i)log(1-\\hat{y_i})]\n\\end{align}\n\n・n はテストセット内の画像数  \n・$\\hat{y_i}$は画像が犬である確率の予測  \n・$y_i$は画像が犬の場合1，猫の場合0  \n・$log()$は自然対数(底が$e$）\n\n提出形式はサンプルの通り  \n画像ID ＋ ラベルの予測確率","metadata":{"papermill":{"duration":0.475304,"end_time":"2023-07-17T20:03:42.792877","exception":false,"start_time":"2023-07-17T20:03:42.317573","status":"completed"},"tags":[]}},{"cell_type":"code","source":"import pandas as pd\n\ndf = pd.read_csv('/kaggle/input/dogs-vs-cats-redux-kernels-edition/sample_submission.csv')\ndf.head()","metadata":{"execution":{"iopub.execute_input":"2023-07-17T20:03:43.762515Z","iopub.status.busy":"2023-07-17T20:03:43.762148Z","iopub.status.idle":"2023-07-17T20:03:43.821292Z","shell.execute_reply":"2023-07-17T20:03:43.820086Z"},"papermill":{"duration":0.526065,"end_time":"2023-07-17T20:03:43.824004","exception":false,"start_time":"2023-07-17T20:03:43.297939","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id='submit'></a>\n### submit データの作成","metadata":{"execution":{"iopub.execute_input":"2023-07-09T16:57:09.941946Z","iopub.status.busy":"2023-07-09T16:57:09.941619Z","iopub.status.idle":"2023-07-09T16:57:09.946959Z","shell.execute_reply":"2023-07-09T16:57:09.945485Z","shell.execute_reply.started":"2023-07-09T16:57:09.941919Z"},"papermill":{"duration":0.510287,"end_time":"2023-07-17T20:03:44.827375","exception":false,"start_time":"2023-07-17T20:03:44.317088","status":"completed"},"tags":[]}},{"cell_type":"code","source":"# モデルの呼び出し\ntf.keras.backend.clear_session()\nmodel = get_model((image_size, image_size, dim), 2)\nmodel.load_weights(\"best_weight.h5\")","metadata":{"execution":{"iopub.execute_input":"2023-07-17T20:03:45.768487Z","iopub.status.busy":"2023-07-17T20:03:45.768103Z","iopub.status.idle":"2023-07-17T20:03:49.031141Z","shell.execute_reply":"2023-07-17T20:03:49.030145Z"},"papermill":{"duration":3.736525,"end_time":"2023-07-17T20:03:49.033775","exception":false,"start_time":"2023-07-17T20:03:45.297250","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = model.predict(X_test)\n\ndf1 = pd.DataFrame(pred, columns=['dog', 'cat'])\nids = [path.split('/')[-1].split('.')[0] for path in test_list]\nsubmit = list(zip(ids, df1['dog'].to_list()))\ndf2 = pd.DataFrame(submit, columns=['id', 'label'])\nprint(df2.head())\ndf2.to_csv('submit_trained.csv', index=False)","metadata":{"execution":{"iopub.execute_input":"2023-07-17T20:03:49.965041Z","iopub.status.busy":"2023-07-17T20:03:49.964651Z","iopub.status.idle":"2023-07-17T20:04:14.150947Z","shell.execute_reply":"2023-07-17T20:04:14.148504Z"},"papermill":{"duration":24.654605,"end_time":"2023-07-17T20:04:14.153302","exception":false,"start_time":"2023-07-17T20:03:49.498697","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv('submit_trained.csv')\ndf.head()","metadata":{"execution":{"iopub.execute_input":"2023-07-17T20:04:15.192662Z","iopub.status.busy":"2023-07-17T20:04:15.192304Z","iopub.status.idle":"2023-07-17T20:04:15.209563Z","shell.execute_reply":"2023-07-17T20:04:15.208515Z"},"papermill":{"duration":0.510174,"end_time":"2023-07-17T20:04:15.211832","exception":false,"start_time":"2023-07-17T20:04:14.701658","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id='memo'></a>\n# Kaggleでのsubmitメモ\n[目次へ戻る](#start)\n\nだいたいスコア　0.07　ぐらいを出している  \n自身のスコアがランキングに反映されないため，順位はスコアを比較して大体の順位でいい気がする","metadata":{"papermill":{"duration":0.490533,"end_time":"2023-07-17T20:04:16.206043","exception":false,"start_time":"2023-07-17T20:04:15.715510","status":"completed"},"tags":[]}},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":0.535625,"end_time":"2023-07-17T20:04:17.237830","exception":false,"start_time":"2023-07-17T20:04:16.702205","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]}]}