{"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":"none","dataSources":[{"sourceId":7327,"databundleVersionId":861871,"sourceType":"competition"}],"dockerImageVersionId":30732,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np          # linear algebra\nimport pandas as pd         # data processing, CSV file I/O (e.g. pd.read_csv)\nfrom random import randint\nfrom tqdm import tqdm       # Progress Bars\nimport cv2\nimport matplotlib.pyplot as plt\nimport os\n\n# データディレクトリのパス\nDATA_DIR = \"../input\"\n\n# 入力データファイルのリストを表示\nprint(os.listdir(DATA_DIR))\n\n# Any results you write to the current directory are saved as output.","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-07-12T09:03:09.122223Z","iopub.execute_input":"2024-07-12T09:03:09.122657Z","iopub.status.idle":"2024-07-12T09:03:09.130091Z","shell.execute_reply.started":"2024-07-12T09:03:09.122626Z","shell.execute_reply":"2024-07-12T09:03:09.129040Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.preprocessing import image\nfrom sklearn.preprocessing import label_binarize\nfrom sklearn.model_selection import train_test_split\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPool2D\nfrom tensorflow.keras.optimizers import Adam","metadata":{"execution":{"iopub.status.busy":"2024-07-12T09:03:12.263384Z","iopub.execute_input":"2024-07-12T09:03:12.264314Z","iopub.status.idle":"2024-07-12T09:03:12.269521Z","shell.execute_reply.started":"2024-07-12T09:03:12.264280Z","shell.execute_reply":"2024-07-12T09:03:12.268319Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# データの読み込み\ndf_train = pd.read_csv(os.path.join(DATA_DIR, \"dog-breed-identification/labels.csv\"))\n\n# データの基本情報を表示\nprint(\"Shape of the training data:\", df_train.shape)\nprint(df_train.head(10))\nprint(\"\\nData Information:\")\nprint(df_train.info())\nprint(\"\\nMissing values per column:\")\nprint(df_train.isnull().sum())","metadata":{"execution":{"iopub.status.busy":"2024-07-12T09:03:14.636799Z","iopub.execute_input":"2024-07-12T09:03:14.637724Z","iopub.status.idle":"2024-07-12T09:03:14.666003Z","shell.execute_reply.started":"2024-07-12T09:03:14.637690Z","shell.execute_reply":"2024-07-12T09:03:14.664915Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import seaborn as sns\n\n# 各犬種の数を集計\nbreeds_all = df_train[\"breed\"]\nbreed_counts = breeds_all.value_counts()\n\n# 上位5犬種の表示\nprint(breed_counts.head())\n\n# 全犬種の分布を可視化\nplt.figure(figsize=(15, 8))\nsns.barplot(x=breed_counts.index, y=breed_counts.values, palette=\"viridis\")\nplt.title(\"Number of Each Dog Breed\")\nplt.xlabel(\"Breed\")\nplt.ylabel(\"Count\")\nplt.xticks(rotation=90)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-07-12T09:03:17.094137Z","iopub.execute_input":"2024-07-12T09:03:17.095144Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_images(images, classes, img_width=224, img_height=224):\n    \n    # images (list): 表示する画像のリスト\n    # classes (list): 画像に対応するクラスのリスト\n    # img_width (int): 画像の幅（デフォルト: 224）\n    # img_height (int): 画像の高さ（デフォルト: 224）\n    \n    assert len(images) == len(classes) == 9, \"imagesとclassesの長さは9である必要があります\"\n\n    # 3x3のサブプロットを作成\n    fig, axes = plt.subplots(3, 3, figsize=(15, 15), sharex=True)\n    fig.subplots_adjust(hspace=0.3, wspace=0.3)\n   \n    for i, ax in enumerate(axes.flat):\n        # 画像の色空間をBGRからRGBに変換し、リサイズ\n        img = cv2.cvtColor(images[i], cv2.COLOR_BGR2RGB)\n        img = cv2.resize(img, (img_width, img_height))\n        \n        # 画像をプロット\n        ax.imshow(img)\n        \n        # クラス名をラベルとして表示\n        xlabel = \"Breed: {0}\".format(classes[i])\n        ax.set_xlabel(xlabel)\n        ax.xaxis.label.set_size(10)\n        \n        # プロットから目盛を削除\n        ax.set_xticks([])\n        ax.set_yticks([])\n    \n    # プロットを表示\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2024-07-12T09:03:20.286026Z","iopub.execute_input":"2024-07-12T09:03:20.286443Z","iopub.status.idle":"2024-07-12T09:03:20.295169Z","shell.execute_reply.started":"2024-07-12T09:03:20.286406Z","shell.execute_reply":"2024-07-12T09:03:20.293882Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_width = 250\nimg_height = 250\nimages = []\nclasses = []\n\n# 学習画像のディレクトリ\nTRAIN_DIR = os.path.join(DATA_DIR, 'dog-breed-identification/train')\n\n# 学習画像の読み込み\nfor f, breed in tqdm(df_train.values):\n    try:\n        # 画像のファイルパス\n        img_path = os.path.join(TRAIN_DIR, f + '.jpg')\n        \n        # 画像の読み込み\n        img = cv2.imread(img_path)\n        \n        # 画像が読み込めなかった場合はスキップ\n        if img is None:\n            print(f\"Warning: Image {img_path} could not be loaded.\")\n            continue\n        \n        # 画像のリサイズ\n        img_resized = cv2.resize(img, (img_width, img_height))\n        \n        # リサイズした画像とクラスをリストに追加\n        images.append(img_resized)\n        classes.append(breed)\n        \n    except Exception as e:\n        print(f\"Error loading image {img_path}: {e}\")\n\n# 確認のために、最初の数件の画像とクラスを表示\nprint(\"Number of images loaded:\", len(images))\nprint(\"First 5 classes:\", classes[:5])","metadata":{"execution":{"iopub.status.busy":"2024-07-12T09:03:23.513888Z","iopub.execute_input":"2024-07-12T09:03:23.514294Z","iopub.status.idle":"2024-07-12T09:03:59.506052Z","shell.execute_reply.started":"2024-07-12T09:03:23.514261Z","shell.execute_reply":"2024-07-12T09:03:59.505073Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# ランダムに画像を選択\nnum_images_to_show = 9\nrandom_indices = [randint(0, len(images) - 1) for _ in range(num_images_to_show)]\nprint(\"Random indices:\", random_indices)\n\nimages_to_show = [images[i] for i in random_indices]\nclasses_to_show = [classes[i] for i in random_indices]\n\nprint(f\"Images to show: {len(images_to_show)}\")\nprint(f\"Classes to show: {len(classes_to_show)}\")\n\n# 画像をプロット\nplot_images(images_to_show, classes_to_show, img_width=img_width, img_height=img_height)","metadata":{"execution":{"iopub.status.busy":"2024-07-12T09:05:52.799740Z","iopub.execute_input":"2024-07-12T09:05:52.800154Z","iopub.status.idle":"2024-07-12T09:05:54.575695Z","shell.execute_reply.started":"2024-07-12T09:05:52.800123Z","shell.execute_reply":"2024-07-12T09:05:54.574186Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 計算能力の制約のため、最初の3つの犬種を選択\nselected_breeds = ['scottish_deerhound', 'maltese_dog', 'bernese_mountain_dog']\n\n# 選択された犬種のみを含むデータをフィルタリング\nfiltered_labels = df_train[df_train['breed'].isin(selected_breeds)]\n\n# インデックスをリセット\nfiltered_labels = filtered_labels.reset_index(drop=True)\n\n# 確認のため最初の数行を表示\nfiltered_labels.head()","metadata":{"execution":{"iopub.status.busy":"2024-07-12T09:06:00.776021Z","iopub.execute_input":"2024-07-12T09:06:00.776459Z","iopub.status.idle":"2024-07-12T09:06:00.790549Z","shell.execute_reply.started":"2024-07-12T09:06:00.776428Z","shell.execute_reply":"2024-07-12T09:06:00.789298Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.preprocessing import image\n\n# 選択された犬種のラベルデータ\nfiltered_labels = filtered_labels.copy()\n\n# 選択された犬種のユニークなリストを取得\nCLASS_NAMES = filtered_labels['breed'].unique()\n\n# 行列を作成（画像データ用）\nimg_height, img_width = 224, 224\nX_data = np.zeros((len(filtered_labels), img_height, img_width, 3), dtype='float32')\nY_data = label_binarize(filtered_labels['breed'], classes=selected_breeds)\n\n# 画像の読み込み、変換、および正規化\nfor i in tqdm(range(len(filtered_labels))):\n    img_path = f\"/kaggle/input/dog-breed-identification/train/{filtered_labels['id'][i]}.jpg\"\n    img = image.load_img(img_path, target_size=(img_height, img_width))\n    img_array = image.img_to_array(img)\n    \n    # 画像データを正規化して配列に追加\n    X_data[i] = img_array / 255.0\n    \n# 学習画像の形状とサイズを表示\nprint('\\nTrain Images shape: ', X_data.shape, ' size: {:,}'.format(X_data.size))\nprint('One-hot encoded output shape: ', Y_data.shape, ' size: {:,}'.format(Y_data.size))","metadata":{"execution":{"iopub.status.busy":"2024-07-12T09:06:03.266945Z","iopub.execute_input":"2024-07-12T09:06:03.267444Z","iopub.status.idle":"2024-07-12T09:06:04.600435Z","shell.execute_reply.started":"2024-07-12T09:06:03.267403Z","shell.execute_reply":"2024-07-12T09:06:04.599426Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# CNNでの学習\n# モデルの定義(KerasのSequentialというモデルを使用する)\nmodel = Sequential()\n\n# 畳み込み層：Conv2D\n# プーリング層：MaxPool2D\n# 全結合層：Dense\nmodel.add(Conv2D(filters = 64, kernel_size = (5,5), activation ='relu', input_shape = (224,224,3)))\nmodel.add(MaxPool2D(pool_size=(2,2)))\n\nmodel.add(Conv2D(filters = 32, kernel_size = (3,3), activation ='relu', kernel_regularizer = 'l2'))\nmodel.add(MaxPool2D(pool_size=(2,2)))\n\nmodel.add(Conv2D(filters = 16, kernel_size = (7,7), activation ='relu', kernel_regularizer = 'l2'))\nmodel.add(MaxPool2D(pool_size=(2,2)))\n\nmodel.add(Conv2D(filters = 8, kernel_size = (5,5), activation ='relu', kernel_regularizer = 'l2'))\nmodel.add(MaxPool2D(pool_size=(2,2)))\n\nmodel.add(Flatten())\nmodel.add(Dense(128, activation = \"relu\", kernel_regularizer = 'l2'))\nmodel.add(Dense(64, activation = \"relu\", kernel_regularizer = 'l2'))\nmodel.add(Dense(len(CLASS_NAMES), activation=\"softmax\"))\n\n# モデルのコンパイル\nmodel.compile(loss = 'categorical_crossentropy', optimizer = Adam(0.0001),metrics=['accuracy'])\n\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2024-07-12T09:06:07.564760Z","iopub.execute_input":"2024-07-12T09:06:07.565158Z","iopub.status.idle":"2024-07-12T09:06:07.723253Z","shell.execute_reply.started":"2024-07-12T09:06:07.565125Z","shell.execute_reply":"2024-07-12T09:06:07.722176Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\n# データセットの分割\nX_train_and_val, X_test, Y_train_and_val, Y_test = train_test_split(X_data, Y_data, test_size=0.1, random_state=42)\n\n# トレーニングデータとテストデータに分割\nX_train, X_val, Y_train, Y_val = train_test_split(X_train_and_val, Y_train_and_val, test_size=0.2, random_state=42)\n\n# 分割後のデータセットの形状を確認\nprint('Training data shape:', X_train.shape)\nprint('Validation data shape:', X_val.shape)\nprint('Testing data shape:', X_test.shape)","metadata":{"execution":{"iopub.status.busy":"2024-07-12T09:06:10.891562Z","iopub.execute_input":"2024-07-12T09:06:10.891963Z","iopub.status.idle":"2024-07-12T09:06:11.033649Z","shell.execute_reply.started":"2024-07-12T09:06:10.891933Z","shell.execute_reply":"2024-07-12T09:06:11.032583Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# モデルのトレーニング\nhistory = model.fit(X_train, Y_train, batch_size=128, epochs=100, validation_data=(X_val, Y_val))\n\n# 学習曲線のプロット\nplt.figure(figsize=(12, 6))\n\n# 学習および検証データの損失\nplt.subplot(1, 2, 1)\nplt.plot(history.history['loss'], label='Training Loss')\nplt.plot(history.history['val_loss'], label='Validation Loss')\nplt.xlabel('Epoch')\nplt.ylabel('Loss')\nplt.title('Training and Validation Loss')\nplt.legend()\n\n# 学習および検証データの精度\nplt.subplot(1, 2, 2)\nplt.plot(history.history['accuracy'], label='Training Accuracy')\nplt.plot(history.history['val_accuracy'], label='Validation Accuracy')\nplt.xlabel('Epoch')\nplt.ylabel('Accuracy')\nplt.title('Training and Validation Accuracy')\nplt.legend()\n\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-07-12T09:06:13.689001Z","iopub.execute_input":"2024-07-12T09:06:13.689784Z","iopub.status.idle":"2024-07-12T09:37:42.787058Z","shell.execute_reply.started":"2024-07-12T09:06:13.689751Z","shell.execute_reply":"2024-07-12T09:37:42.785953Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# テストデータに対する予測\nY_pred = model.predict(X_test)\n\n# テストデータでのモデルの評価\nloss, accuracy = model.evaluate(X_test, Y_test)\n\n# 精度の表示\nprint(f'Accuracy over the test set: {accuracy * 100:.2f}%')","metadata":{"execution":{"iopub.status.busy":"2024-07-12T09:58:59.039504Z","iopub.execute_input":"2024-07-12T09:58:59.039916Z","iopub.status.idle":"2024-07-12T09:59:01.086461Z","shell.execute_reply.started":"2024-07-12T09:58:59.039884Z","shell.execute_reply":"2024-07-12T09:59:01.085318Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 画像の表示\nplt.imshow(X_test[2])\nplt.axis('off')  # 軸を非表示にする\nplt.show()\n\ntrue_label = filtered_labels['breed'][np.argmax(Y_test[2])]\npredicted_label = filtered_labels['breed'][np.argmax(Y_pred[2])]\n\nprint(\"Originally:\", true_label)\nprint(\"Predicted:\", predicted_label)","metadata":{"execution":{"iopub.status.busy":"2024-07-12T10:00:08.034789Z","iopub.execute_input":"2024-07-12T10:00:08.035275Z","iopub.status.idle":"2024-07-12T10:00:08.251103Z","shell.execute_reply.started":"2024-07-12T10:00:08.035224Z","shell.execute_reply":"2024-07-12T10:00:08.250006Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# テストデータに対する予測を取得（確率値のまま）\nY_pred_prob = model.predict(X_test)\n\n# 各画像について最も確率の高いクラスを予測として取得\nY_pred_labels = [CLASS_NAMES[np.argmax(pred)] for pred in Y_pred_prob]\n\n# テストデータのインデックスを取得\ntest_indices = range(len(X_test))\n\n# テストデータのIDを取得\ntest_ids = filtered_labels.loc[test_indices, 'id']\n\n# データフレームとして結果を準備\nresults_df = pd.DataFrame({'id': test_ids, 'breed': Y_pred_labels})\n\n# CSVファイルとして出力\nresults_df.to_csv('submission.csv', index=False)\n\n# 提出用のファイルが生成されたことを確認\nprint(\"Submission file 'submission.csv' generated successfully.\")\n\n","metadata":{"execution":{"iopub.status.busy":"2024-07-12T10:09:33.223359Z","iopub.execute_input":"2024-07-12T10:09:33.224398Z","iopub.status.idle":"2024-07-12T10:09:33.961181Z","shell.execute_reply.started":"2024-07-12T10:09:33.224360Z","shell.execute_reply":"2024-07-12T10:09:33.960112Z"},"trusted":true},"execution_count":null,"outputs":[]}]}