{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":5441,"databundleVersionId":38425,"sourceType":"competition"}],"dockerImageVersionId":30732,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"ライブラリのインポート","metadata":{}},{"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-26T05:20:49.532714Z","iopub.execute_input":"2024-07-26T05:20:49.533025Z","iopub.status.idle":"2024-07-26T05:20:49.901662Z","shell.execute_reply.started":"2024-07-26T05:20:49.532991Z","shell.execute_reply":"2024-07-26T05:20:49.900743Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nfrom os import makedirs\nfrom os import listdir\nfrom shutil import copyfile\nfrom random import seed\nfrom random import random\nimport numpy as np\nimport pandas as pd\n\n# 図表\nimport seaborn as sns\nimport matplotlib.pyplot as plt\nfrom matplotlib.image import imread\nfrom PIL import Image\n\n# Scikit-learn\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import classification_report,confusion_matrix,ConfusionMatrixDisplay\n\n# Tensorflow\nimport tensorflow as tf\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator, load_img, img_to_array\nfrom tensorflow.keras.layers import Dense,MaxPooling2D,Dropout,Flatten,BatchNormalization,Conv2D\nfrom tensorflow.keras.callbacks import ReduceLROnPlateau,EarlyStopping","metadata":{"execution":{"iopub.status.busy":"2024-07-26T05:20:49.906612Z","iopub.execute_input":"2024-07-26T05:20:49.906872Z","iopub.status.idle":"2024-07-26T05:20:53.690765Z","shell.execute_reply.started":"2024-07-26T05:20:49.906847Z","shell.execute_reply":"2024-07-26T05:20:53.689977Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Zipファイルの解凍","metadata":{}},{"cell_type":"code","source":"train_path = \"/kaggle/input/dogs-vs-cats-redux-kernels-edition/test.zip\"\ntest_path = \"/kaggle/input/dogs-vs-cats-redux-kernels-edition/train.zip\"\n\nfiles = \"/kaggle/working/\"\n\nimport zipfile\n\nwith zipfile.ZipFile(train_path, 'r') as zipp:\n    zipp.extractall(files)\n    \nwith zipfile.ZipFile(test_path, 'r') as zipp:\n    zipp.extractall(files)","metadata":{"execution":{"iopub.status.busy":"2024-07-26T05:20:53.691939Z","iopub.execute_input":"2024-07-26T05:20:53.692533Z","iopub.status.idle":"2024-07-26T05:21:05.028286Z","shell.execute_reply.started":"2024-07-26T05:20:53.692507Z","shell.execute_reply":"2024-07-26T05:21:05.027409Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_dir = \"../working/train/\"\n\n# image_dirのファイル名リスト\"filenames\"の作成\nfilenames = os.listdir(image_dir)\n\n# ファイル名を\".\"で分割し、最初の部分を取り出したリスト\"labels\"を作成\nlabels = [x.split(\".\")[0] for x in filenames]\n\n# 2つのカラム（filenameとlabel）を持つDataFrame\"data\"を作成\ndata = pd.DataFrame({\"filename\": filenames, \"label\": labels})\n\n# DataFrameの最初の5行を表示\ndata.head()","metadata":{"execution":{"iopub.status.busy":"2024-07-26T05:21:05.030924Z","iopub.execute_input":"2024-07-26T05:21:05.031237Z","iopub.status.idle":"2024-07-26T05:21:05.070355Z","shell.execute_reply.started":"2024-07-26T05:21:05.031210Z","shell.execute_reply":"2024-07-26T05:21:05.069482Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"データの可視化","metadata":{}},{"cell_type":"code","source":"# 犬のデータを可視化する\n\n# グリッドサイズを20x20インチに設定\nplt.figure(figsize=(20,20))\n# サブプロット間の垂直方向のスペースを0.4インチに設定\nplt.subplots_adjust(hspace=0.4)\n\n# 犬の画像を10枚表示する\nfor i in range(10):\n    \n    # 1行10列のグリッドのうち、i+1列目にプロット\n    plt.subplot(1,10,i+1)\n    # 'train/dog.i+1.jpg'の名前を指定\n    filename = 'train/' + 'dog.' + str(i) + '.jpg'\n    # 'filename'名の画像を読み込む\n    image = imread(filename)\n    \n    plt.imshow(image)\n    # タイトルとフォントサイズの指定\n    plt.title('Dog',fontsize=12)\n    # サブプロットの軸をオフ\n    plt.axis('off')\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-07-26T05:21:05.071575Z","iopub.execute_input":"2024-07-26T05:21:05.071926Z","iopub.status.idle":"2024-07-26T05:21:06.014493Z","shell.execute_reply.started":"2024-07-26T05:21:05.071893Z","shell.execute_reply":"2024-07-26T05:21:06.013485Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 猫のデータを可視化する\n\n# グリッドサイズを20x20インチに設定\nplt.figure(figsize=(20,20))\n# サブプロット間の垂直方向のスペースを0.4インチに設定\nplt.subplots_adjust(hspace=0.4)\n\n# 猫の画像を10枚表示する\n# （犬の時と同様）\nfor i in range(10):\n    \n    plt.subplot(1,10,i+1)\n    filename = 'train/' + 'cat.' + str(i) + '.jpg'\n    image = imread(filename)\n    plt.imshow(image)\n    plt.title('Cat',fontsize=12)\n    plt.axis('off')\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-07-26T05:21:06.015702Z","iopub.execute_input":"2024-07-26T05:21:06.016009Z","iopub.status.idle":"2024-07-26T05:21:06.895561Z","shell.execute_reply.started":"2024-07-26T05:21:06.015982Z","shell.execute_reply":"2024-07-26T05:21:06.894158Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"データの分割\n","metadata":{}},{"cell_type":"code","source":"# データフレーム内のデータを分割する\n\n# データフレーム'data'から犬か猫かのラベル列を抽出し、格納する\nlabels = data['label']\n\n# データフレーム'data'を訓練データ（X_train）と一時データ（X_temp）に分割\n# 'test_size=0.2':全データの20%を一時データ（X_temp）に割り当てる\n# 'stratify=labels':分割時にクラスラベル（犬と猫）の比率を保つようにする\n# 'random_state=42': 再現性を確保するためのランダムシードを設定\nX_train, X_temp = train_test_split(data, test_size=0.2, stratify=labels, random_state = 42)\n\n# 一時データ'X_temp'から犬か猫かのラベル列を抽出し、格納する\nlabel_test_val = X_temp['label']\n\n# 一時データ'X_temp'をテストデータ'X_test'と検証データ'X_val'に分割\n# 'test_size=0.5':一時データの50%をテストデータ'X_test'に、残りの50%を検証データ'X_val'に割り当て\n# 'stratify=labels':分割時にクラスラベル（犬と猫）の比率を保つようにする\n# 'random_state=42': 再現性を確保するためのランダムシードを設定\nX_test, X_val = train_test_split(X_temp, test_size=0.5, stratify=label_test_val, random_state = 42)\n\n#訓練データ、テストデータ、検証データのサンプル数とクラス数を表示\nprint('The shape of train data',X_train.shape)\nprint('The shape of test data',X_test.shape)\nprint('The shape of validation data',X_val.shape)","metadata":{"execution":{"iopub.status.busy":"2024-07-26T05:21:06.896867Z","iopub.execute_input":"2024-07-26T05:21:06.897202Z","iopub.status.idle":"2024-07-26T05:21:06.966008Z","shell.execute_reply.started":"2024-07-26T05:21:06.897174Z","shell.execute_reply":"2024-07-26T05:21:06.964833Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"raw","source":"グラフによる可視化","metadata":{}},{"cell_type":"code","source":"# データのグラフによる可視化\n\nlabels = ['Cat','Dog']\n\n# 訓練データ、テストデータ、検証データのラベルと数をカウント\nlabel1,count1 = np.unique(X_train.label,return_counts=True)\nlabel2,count2 = np.unique(X_val.label,return_counts=True)\nlabel3,count3 = np.unique(X_test.label,return_counts=True)\n\n# データフレームの作成\nuni1 = pd.DataFrame(data=count1,index=labels,columns=['Count1'])\nuni2 = pd.DataFrame(data=count2,index=labels,columns=['Count2'])\nuni3 = pd.DataFrame(data=count3,index=labels,columns=['Count3'])\n\n# サイズ、解像度、スタイルの指定\nplt.figure(figsize=(20,6),dpi=200)\nsns.set_style('darkgrid')\n\n# 1行3列の1番目\nplt.subplot(131)\nsns.barplot(data=uni1,x=uni1.index,y='Count1',palette='icefire',width=0.2).set_title('Class distribution in Training set',fontsize=15)\nplt.xlabel('Labels',fontsize=12)\nplt.ylabel('Count',fontsize=12)\n\n# 1行3列の2番目\nplt.subplot(132)\nsns.barplot(data=uni2,x=uni2.index,y='Count2',palette='icefire',width=0.2).set_title('Class distribution in validation set',fontsize=15)\nplt.xlabel('Labels',fontsize=12)\nplt.ylabel('Count',fontsize=12)\n\n# 1行3列の3番目\nplt.subplot(133)\nsns.barplot(data=uni3,x=uni3.index,y='Count3',palette='icefire',width=0.2).set_title('Class distribution in Testing set',fontsize=15)\nplt.xlabel('Labels',fontsize=12)\nplt.ylabel('Count',fontsize=12)\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-07-26T05:21:06.967456Z","iopub.execute_input":"2024-07-26T05:21:06.967832Z","iopub.status.idle":"2024-07-26T05:21:07.781872Z","shell.execute_reply.started":"2024-07-26T05:21:06.967795Z","shell.execute_reply":"2024-07-26T05:21:07.780887Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"raw","source":"パラメータ設定","metadata":{}},{"cell_type":"code","source":"# パラメータ\n\n# リサイズ\nimage_size = 128\n# カラー画像のため、RGBの3チャンネル\nimage_channel = 3\n# バッチサイズ\nbat_size = 32","metadata":{"execution":{"iopub.status.busy":"2024-07-26T05:21:07.783019Z","iopub.execute_input":"2024-07-26T05:21:07.783285Z","iopub.status.idle":"2024-07-26T05:21:07.787979Z","shell.execute_reply.started":"2024-07-26T05:21:07.783261Z","shell.execute_reply":"2024-07-26T05:21:07.786869Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"データ拡張","metadata":{}},{"cell_type":"code","source":"# 画像データ生成器の作成（データ拡張）\n# 訓練データのみに実行\ntrain_datagen = ImageDataGenerator(rescale=1./255, #  画像のピクセル値を0から1の範囲にスケーリング\n                                    rotation_range = 15, # 画像を最大15度までランダムに回転させる\n                                    horizontal_flip = True,# 画像をランダムに左右反転させる\n                                    zoom_range = 0.2, # 画像を最大20%までランダムにズームインまたはズームアウトさせる\n                                    shear_range = 0.1, # 画像をランダムに10%までシアー変換する\n                                    fill_mode = 'reflect', # 画像を変換したときに不足するピクセルを反射パディングで埋める\n                                    width_shift_range = 0.1,# 画像を横方向に最大10%までランダムにシフトさせる\n                                    height_shift_range = 0.1) # 画像を縦方向に最大10%までランダムにシフトさせる\n    \n\n# テストデータは、画像のピクセル値を0から1の範囲にスケーリングだけする\ntest_datagen = ImageDataGenerator(rescale=1./255)","metadata":{"execution":{"iopub.status.busy":"2024-07-26T05:21:07.789568Z","iopub.execute_input":"2024-07-26T05:21:07.789880Z","iopub.status.idle":"2024-07-26T05:21:07.798172Z","shell.execute_reply.started":"2024-07-26T05:21:07.789854Z","shell.execute_reply":"2024-07-26T05:21:07.797218Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 画像データ生成器を適用させる\n\n\n\n# 訓練データ\ntrain_generator = train_datagen.flow_from_dataframe(X_train,\n                                                    directory = 'train/',\n                                                    x_col= 'filename',\n                                                    y_col= 'label',\n                                                    batch_size = bat_size,\n                                                    target_size = (image_size,image_size)\n                                                   )\n\n\n# 検証用データ\nval_generator = test_datagen.flow_from_dataframe(X_val, \n                                                 directory = 'train/',\n                                                 x_col= 'filename',\n                                                 y_col= 'label',\n                                                 batch_size = bat_size,\n                                                 target_size = (image_size,image_size),\n                                                 shuffle=False\n                                                )\n\n# テストデータ\ntest_generator = test_datagen.flow_from_dataframe(X_test, \n                                                  directory = 'train/',\n                                                  x_col= 'filename',\n                                                  y_col= 'label',\n                                                  batch_size = bat_size,\n                                                  target_size = (image_size,image_size),\n                                                  shuffle=False\n                                                 )","metadata":{"execution":{"iopub.status.busy":"2024-07-26T05:21:07.799107Z","iopub.execute_input":"2024-07-26T05:21:07.799488Z","iopub.status.idle":"2024-07-26T05:21:08.112490Z","shell.execute_reply.started":"2024-07-26T05:21:07.799446Z","shell.execute_reply":"2024-07-26T05:21:08.111584Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"拡張前後のデータ比較","metadata":{}},{"cell_type":"code","source":"# グリッドサイズを20x5インチに設定\nplt.figure(figsize=(20, 5))\n# サブプロット間の垂直方向のスペースを0.4インチに設定\nplt.subplots_adjust(hspace=0.4)\n\n# 同じ画像ファイルを元に表示\nfor i in range(10):\n    filename = X_train.iloc[i]['filename']\n    \n    # 元画像を表示\n    img_path = os.path.join(image_dir, filename)\n    img = load_img(img_path, target_size=(128, 128))\n    img_array = img_to_array(img) / 255.0\n    \n    plt.subplot(2, 10, i + 1)\n    plt.imshow(img_array)\n    plt.axis('off')\n    plt.title(\"before\")\n    \n    # 拡張された画像を表示\n    img_array = img_to_array(img)  # データ拡張のために配列に変換\n    img_array = img_array.reshape((1,) + img_array.shape)  # バッチ用に形状を変更\n    aug_iter = train_datagen.flow(img_array, batch_size=1) # データ拡張の適用\n    aug_img = next(aug_iter)[0]  # 拡張された画像を取得\n    \n    plt.subplot(2, 10, i + 11)\n    plt.imshow(aug_img)\n    plt.axis('off')\n    plt.title(\"after\")\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-07-26T05:21:08.113573Z","iopub.execute_input":"2024-07-26T05:21:08.113850Z","iopub.status.idle":"2024-07-26T05:21:10.946580Z","shell.execute_reply.started":"2024-07-26T05:21:08.113825Z","shell.execute_reply":"2024-07-26T05:21:10.945609Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"深層学習モデル","metadata":{}},{"cell_type":"code","source":"# CNNモデル\n\n# Sequential モデルは、層を順番に積み重ねていくシンプルなモデル\nmodel = Sequential()\n\n# 入力層\n# Conv2Dは32個の3x3のフィルタを使用し、ReLU活性化関数を持つ畳み込み層\nmodel.add(Conv2D(32,(3,3),activation='relu',input_shape = (image_size,image_size,image_channel))) \n# バッチ正規化\nmodel.add(BatchNormalization())\n# 2x2のマックスプーリング層で特徴マップのダウンサンプリングを行う\nmodel.add(MaxPooling2D(pool_size=(2,2)))\n# ドロップアウトを用いて過学習を防ぐ\nmodel.add(Dropout(0.2))\n\n# Bloack 1 \nmodel.add(Conv2D(64,(3,3),activation='relu'))\nmodel.add(BatchNormalization())\nmodel.add(MaxPooling2D(pool_size=(2,2)))\nmodel.add(Dropout(0.2))\n# Block 2\nmodel.add(Conv2D(128,(3,3),activation='relu'))\nmodel.add(BatchNormalization())\nmodel.add(MaxPooling2D(pool_size=(2,2)))\nmodel.add(Dropout(0.2))\n# Block 3\nmodel.add(Conv2D(256,(3,3),activation='relu'))\nmodel.add(BatchNormalization())\nmodel.add(MaxPooling2D(pool_size=(2,2)))\nmodel.add(Dropout(0.2))\n\n# 全結合層\nmodel.add(Flatten())\nmodel.add(Dense(512,activation='relu'))\nmodel.add(BatchNormalization())\nmodel.add(Dropout(0.2))\n\n# 出力層\nmodel.add(Dense(2,activation='softmax'))\n\n# 詳細の出力\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2024-07-26T05:21:10.950214Z","iopub.execute_input":"2024-07-26T05:21:10.950534Z","iopub.status.idle":"2024-07-26T05:21:11.832107Z","shell.execute_reply.started":"2024-07-26T05:21:10.950507Z","shell.execute_reply":"2024-07-26T05:21:11.831178Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"コールバック","metadata":{}},{"cell_type":"code","source":"# コールバックの定義\n# 特定の条件に基づいて学習率を動的に減少させる'ReduceLROnPlateau'コールバック\nlearning_rate_reduction = ReduceLROnPlateau(monitor = 'val_accuracy',\n                                            patience=2, # 改善が見られないエポック数の許容値\n                                            factor=0.5, # 学習率を減少させる係数\n                                            min_lr = 0.00001, # 学習率の下限値\n                                            verbose = 1) # コールバックのログ出力の詳細度\n\n# 検証データの損失 (val_loss) をモニタリングし、訓練を早期に停止させる'EarlyStopping'コールバック\nearly_stoping = EarlyStopping(monitor='val_loss',patience= 3,restore_best_weights=True,verbose=0)","metadata":{"execution":{"iopub.status.busy":"2024-07-26T05:21:11.833292Z","iopub.execute_input":"2024-07-26T05:21:11.833586Z","iopub.status.idle":"2024-07-26T05:21:11.838920Z","shell.execute_reply.started":"2024-07-26T05:21:11.833560Z","shell.execute_reply":"2024-07-26T05:21:11.837907Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"コンパイル","metadata":{}},{"cell_type":"code","source":"# モデルのコンパイル\n# optimizer:最適化アルゴリズムの指定\n# loss:損失関数の指定\n# metrics:評価指標の指定。Accuracy=\"精度\"\nmodel.compile(optimizer='adam',loss='binary_crossentropy',metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2024-07-26T05:21:11.840150Z","iopub.execute_input":"2024-07-26T05:21:11.840458Z","iopub.status.idle":"2024-07-26T05:21:11.861027Z","shell.execute_reply.started":"2024-07-26T05:21:11.840434Z","shell.execute_reply":"2024-07-26T05:21:11.860153Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"学習","metadata":{}},{"cell_type":"code","source":"# 学習\ncat_dog = model.fit(train_generator,\n                    validation_data = val_generator, \n                    callbacks=[early_stoping,learning_rate_reduction],\n                    epochs = 30,\n                    # 必要であれば設定\n                    # steps_per_epoch = len(train_generator),\n                    # validation_steps = len(val_generaotor),\n                   )","metadata":{"execution":{"iopub.status.busy":"2024-07-26T05:21:11.862136Z","iopub.execute_input":"2024-07-26T05:21:11.862469Z","iopub.status.idle":"2024-07-26T05:40:25.797657Z","shell.execute_reply.started":"2024-07-26T05:21:11.862439Z","shell.execute_reply":"2024-07-26T05:40:25.796710Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 精度と損失をエポック数毎にグラフで表示\n\nerror = pd.DataFrame(cat_dog.history)\n\nplt.figure(figsize=(18,5),dpi=200)\nsns.set_style('darkgrid')\n\nplt.subplot(121)\nplt.title('Cross Entropy Loss',fontsize=15)\nplt.xlabel('Epochs',fontsize=12)\nplt.ylabel('Loss',fontsize=12)\nplt.plot(error['loss'], label='loss')\nplt.plot(error['val_loss'], label='val_loss')\nplt.legend()\n\nplt.subplot(122)\nplt.title('Classification Accuracy',fontsize=15)\nplt.xlabel('Epochs',fontsize=12)\nplt.ylabel('Accuracy',fontsize=12)\nplt.plot(error['accuracy'], label='accuracy')\nplt.plot(error['val_accuracy'], label='val_accuracy')\nplt.legend()\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-07-26T05:40:25.799081Z","iopub.execute_input":"2024-07-26T05:40:25.799382Z","iopub.status.idle":"2024-07-26T05:40:26.588518Z","shell.execute_reply.started":"2024-07-26T05:40:25.799355Z","shell.execute_reply":"2024-07-26T05:40:26.587540Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"評価","metadata":{}},{"cell_type":"code","source":"# 訓練データの評価\nloss,acc = model.evaluate(train_generator,batch_size = bat_size, verbose = 0)\n\n# 精度\nprint('The accuracy of the model for training data is:',acc*100)\n# 損失\nprint('The Loss of the model for training data is:',loss)\n\n# 検証データの評価\nloss,acc = model.evaluate(val_generator,batch_size = bat_size, verbose = 0)\n\n# 精度\nprint('The accuracy of the model for validation data is:',acc*100)\n# 損失\nprint('The Loss of the model for validation data is:',loss)","metadata":{"execution":{"iopub.status.busy":"2024-07-26T05:40:26.589874Z","iopub.execute_input":"2024-07-26T05:40:26.590214Z","iopub.status.idle":"2024-07-26T05:42:01.453193Z","shell.execute_reply.started":"2024-07-26T05:40:26.590184Z","shell.execute_reply":"2024-07-26T05:42:01.452229Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# モデルの保存\nmodel.save(\"model.h5\")","metadata":{"execution":{"iopub.status.busy":"2024-07-26T05:42:01.454417Z","iopub.execute_input":"2024-07-26T05:42:01.454705Z","iopub.status.idle":"2024-07-26T05:42:01.604453Z","shell.execute_reply.started":"2024-07-26T05:42:01.454679Z","shell.execute_reply":"2024-07-26T05:42:01.603454Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"予測","metadata":{}},{"cell_type":"code","source":"# テストデータによる予測\nresult = model.predict(test_generator,batch_size = bat_size,verbose = 0)\n\n# 各画像の予測されたクラスを示す配列の作成\ny_pred = np.argmax(result, axis = 1)\n# テストデータの正解ラベルを取得\ny_true = test_generator.labels\n\n# テストデータに対するモデルの損失と精度を計算\nloss,acc = model.evaluate(test_generator, batch_size = bat_size, verbose = 0)\n\nprint('The accuracy of the model for testing data is:',acc*100)\nprint('The Loss of the model for testing data is:',loss)","metadata":{"execution":{"iopub.status.busy":"2024-07-26T05:42:01.605710Z","iopub.execute_input":"2024-07-26T05:42:01.606033Z","iopub.status.idle":"2024-07-26T05:42:09.337783Z","shell.execute_reply.started":"2024-07-26T05:42:01.606007Z","shell.execute_reply":"2024-07-26T05:42:09.336807Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels =['Cat','Dog']\nprint(classification_report(y_true, y_pred,target_names=labels))","metadata":{"execution":{"iopub.status.busy":"2024-07-26T05:42:09.339225Z","iopub.execute_input":"2024-07-26T05:42:09.339607Z","iopub.status.idle":"2024-07-26T05:42:09.358702Z","shell.execute_reply.started":"2024-07-26T05:42:09.339558Z","shell.execute_reply":"2024-07-26T05:42:09.357785Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 混同行列の可視化\nconfusion_mtx = confusion_matrix(y_true,y_pred) \n\nf,ax = plt.subplots(figsize = (8,4),dpi=200)\nsns.heatmap(confusion_mtx, annot=True, linewidths=0.1, cmap = \"gist_yarg_r\", linecolor=\"black\", fmt='.0f', ax=ax,cbar=False, xticklabels=labels, yticklabels=labels)\n\nplt.xlabel(\"Predicted Label\",fontsize=10)\nplt.ylabel(\"True Label\",fontsize=10)\nplt.title(\"Confusion Matrix\",fontsize=13)\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-07-26T05:42:09.359992Z","iopub.execute_input":"2024-07-26T05:42:09.360275Z","iopub.status.idle":"2024-07-26T05:42:09.584462Z","shell.execute_reply.started":"2024-07-26T05:42:09.360249Z","shell.execute_reply":"2024-07-26T05:42:09.583118Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 提出用のKaggleテストデータを予測する\n\nsize =(128,128)\n\n# データフレームの読み込み\ntest_dir = \"../working/test/\"\nfilenames = os.listdir(test_dir)\ntest_data = pd.DataFrame({\"filename\": filenames})\ntest_data['label'] = 'unknown'\n\n# テストデータ用のデータ生成器の作成\ntest_idg =  test_datagen.flow_from_dataframe(test_data, \n                                     \"test/\", \n                                     x_col= \"filename\",\n                                     y_col = 'label',\n                                     batch_size = bat_size,\n                                     target_size=size, \n                                     shuffle = False)\n\n# テスト予測\ntest_predict = model.predict(test_idg,verbose = 0)\n\ntest_predict_argmax = np.argmax(test_predict, axis=1)\n\ny_test_pred = test_predict_argmax\n\ntest_data['label'] = y_test_pred\n\n# マッピング\nlabel_mapping = {0: 'cat', 1: 'dog'}\ntest_data['label'] = test_data['label'].map(label_mapping)\ntest_data.head()\n\n# csvへの書き出し\nsub = pd.read_csv('/kaggle/input/dogs-vs-cats-redux-kernels-edition/sample_submission.csv',index_col='id')\n\nsub['label'] = y_test_pred\n\nsub.to_csv('submission.csv',index=True)","metadata":{"execution":{"iopub.status.busy":"2024-07-26T05:42:09.586582Z","iopub.execute_input":"2024-07-26T05:42:09.587322Z","iopub.status.idle":"2024-07-26T05:42:27.539541Z","shell.execute_reply.started":"2024-07-26T05:42:09.587272Z","shell.execute_reply":"2024-07-26T05:42:27.538161Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 分類された画像の視覚化\n\nfig, axes = plt.subplots(1, 10, figsize=(20, 4))\nfor idx in range(10):\n    image_path = os.path.join(test_dir, test_data.iloc[idx]['filename'])\n    image = Image.open(image_path)\n    axes[idx].imshow(image)\n    axes[idx].set_title(\"Label: \" + test_data.iloc[idx]['label'])\n    axes[idx].axis('off')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-07-26T05:42:27.540991Z","iopub.execute_input":"2024-07-26T05:42:27.541363Z","iopub.status.idle":"2024-07-26T05:42:29.251361Z","shell.execute_reply.started":"2024-07-26T05:42:27.541329Z","shell.execute_reply":"2024-07-26T05:42:29.250431Z"},"trusted":true},"execution_count":null,"outputs":[]}]}