{"metadata":{"kaggle":{"accelerator":"none","dataSources":[{"sourceId":7327,"databundleVersionId":861871,"sourceType":"competition"},{"sourceId":8868919,"sourceType":"datasetVersion","datasetId":5337426}],"dockerImageVersionId":30746,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false},"kernelspec":{"name":"python3","display_name":"Python 3","language":"python"},"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"}},"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":{"execution":{"iopub.status.busy":"2024-07-19T13:47:19.605939Z","iopub.execute_input":"2024-07-19T13:47:19.606386Z","iopub.status.idle":"2024-07-19T13:47:33.818451Z","shell.execute_reply.started":"2024-07-19T13:47:19.606354Z","shell.execute_reply":"2024-07-19T13:47:33.817077Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport os\nimport cv2\nfrom sklearn.model_selection import train_test_split\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport tensorflow as tf","metadata":{"execution":{"iopub.status.busy":"2024-07-19T13:48:08.298789Z","iopub.execute_input":"2024-07-19T13:48:08.299227Z","iopub.status.idle":"2024-07-19T13:48:08.305129Z","shell.execute_reply.started":"2024-07-19T13:48:08.299195Z","shell.execute_reply":"2024-07-19T13:48:08.303958Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## トレーニングデータとテストデータのパスの設定とラベルデータの読み込み","metadata":{}},{"cell_type":"code","source":"# データのパス\ntrain_dir = '/kaggle/input/dog-breed-identification/train/'\ntest_dir = '/kaggle/input/dog-breed-identification/test/'\nlabels_file = '/kaggle/input/dog-breed-identification/labels.csv'\n  \n# ラベルの読み込み\nlabels = pd.read_csv(labels_file)\nprint(labels.head())","metadata":{"execution":{"iopub.status.busy":"2024-07-19T13:49:03.522470Z","iopub.execute_input":"2024-07-19T13:49:03.522887Z","iopub.status.idle":"2024-07-19T13:49:03.570563Z","shell.execute_reply.started":"2024-07-19T13:49:03.522857Z","shell.execute_reply":"2024-07-19T13:49:03.569363Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 画像データのパスをラベルデータに追加","metadata":{}},{"cell_type":"code","source":"from os.path import join\n\nfor dirname, _, filenames in os.walk(train_dir):\n    for filename in filenames:\n        os.path.join(dirname, filename)\n","metadata":{"execution":{"iopub.status.busy":"2024-07-19T13:49:09.267652Z","iopub.execute_input":"2024-07-19T13:49:09.268123Z","iopub.status.idle":"2024-07-19T13:49:12.237556Z","shell.execute_reply.started":"2024-07-19T13:49:09.268066Z","shell.execute_reply":"2024-07-19T13:49:12.236468Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data = labels.assign(img_path = lambda x : train_dir + x['id'] + '.jpg')\ntrain_data.head()","metadata":{"execution":{"iopub.status.busy":"2024-07-19T13:49:17.336395Z","iopub.execute_input":"2024-07-19T13:49:17.336808Z","iopub.status.idle":"2024-07-19T13:49:17.358348Z","shell.execute_reply.started":"2024-07-19T13:49:17.336774Z","shell.execute_reply":"2024-07-19T13:49:17.357007Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 画像をRGBに変換","metadata":{}},{"cell_type":"code","source":"img=cv2.imread('/kaggle/input/dog-breed-identification/test/000621fb3cbb32d8935728e48679680e.jpg')\nimg=cv2.cvtColor(img,cv2.COLOR_BGR2RGB)","metadata":{"execution":{"iopub.status.busy":"2024-07-19T13:49:40.873055Z","iopub.execute_input":"2024-07-19T13:49:40.874001Z","iopub.status.idle":"2024-07-19T13:49:40.919322Z","shell.execute_reply.started":"2024-07-19T13:49:40.873964Z","shell.execute_reply":"2024-07-19T13:49:40.918127Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 画像データに対して犬種（ラベル）が正しければ１、間違っていれば０に数値化する","metadata":{}},{"cell_type":"markdown","source":"X　画像データ、Y　ラベルデータ","metadata":{}},{"cell_type":"code","source":"from keras.preprocessing.image import load_img, img_to_array\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\n# from keras.preprocessing.image import ImageDataGenerator\n# from tensorflow.keras.preprocessing.image import ImageDataGenerator\nH=128\nW=128\nC=3\n\nX = np.array([img_to_array(load_img(img,target_size = (H,W))) for img in train_data['img_path'].values.tolist()])\nprint(X.shape)\nY = pd.get_dummies(train_data['breed'])\nprint(Y.shape)","metadata":{"execution":{"iopub.status.busy":"2024-07-19T13:49:52.722143Z","iopub.execute_input":"2024-07-19T13:49:52.722571Z","iopub.status.idle":"2024-07-19T13:50:52.198069Z","shell.execute_reply.started":"2024-07-19T13:49:52.722533Z","shell.execute_reply":"2024-07-19T13:50:52.196941Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"学習用のデータをトレーニングデータとテストデータに分類","metadata":{}},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nX_train,X_test,Y_train,Y_test  = train_test_split(X,Y,test_size = 0.25)\nprint(X_train.shape,Y_train.shape)\nprint(X_test.shape,Y_test.shape)","metadata":{"execution":{"iopub.status.busy":"2024-07-19T13:51:01.785913Z","iopub.execute_input":"2024-07-19T13:51:01.786341Z","iopub.status.idle":"2024-07-19T13:51:02.590134Z","shell.execute_reply.started":"2024-07-19T13:51:01.786307Z","shell.execute_reply":"2024-07-19T13:51:02.588805Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow import keras\nfrom keras.layers import LeakyReLU\nfrom keras.models import Sequential\nfrom keras.layers import Dense,Conv2D,MaxPool2D,Dropout,Flatten,Activation","metadata":{"execution":{"iopub.status.busy":"2024-07-19T13:51:08.199605Z","iopub.execute_input":"2024-07-19T13:51:08.199991Z","iopub.status.idle":"2024-07-19T13:51:08.208597Z","shell.execute_reply.started":"2024-07-19T13:51:08.199963Z","shell.execute_reply":"2024-07-19T13:51:08.207420Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 畳み込みニューラルネットワーク\nSequential : モデルの骨格を形成する入力層\nConv2D : 畳み込みを行う\nLeakyReLU : 勾配消失問題を緩和する\nMaxPool2D : プーリングを行う\nDropout : 過学習を防ぐ\nDence : 全結合層\nsoftmax : ソフトマックス関数","metadata":{}},{"cell_type":"code","source":"model=Sequential()\n\nmodel.add(Conv2D(32,(3,3),input_shape=(H,W,C)))\nmodel.add(LeakyReLU(alpha=0.01))\n# model.add(Activation('relu'))\n\nmodel.add(MaxPool2D((2,2)))\n\nmodel.add(Conv2D(64,(3,3)))#3×3のフィルタを64種類用いて畳み込み\nmodel.add(LeakyReLU(alpha=0.01))\n# model.add(Activation('relu'))\n\nmodel.add(MaxPool2D((2,2)))\n\nmodel.add(Conv2D(128,(3,3)))\nmodel.add(LeakyReLU(alpha=0.01))\n# model.add(Activation('relu'))\n\nmodel.add(MaxPool2D((2,2)))\n\nmodel.add(Conv2D(128,(3,3)))#3×3のフィルタを128種類用いて畳み込み\nmodel.add(LeakyReLU(alpha=0.01))\n# model.add(Activation('relu'))\n\nmodel.add(MaxPool2D((2,2)))\n\nmodel.add(Flatten())\n\nmodel.add(Dropout(0.5))\n\nmodel.add(Dense(512))\nmodel.add(LeakyReLU(alpha=0.01))\n# model.add(Activation('relu'))\n\nmodel.add(Dense(Y.shape[1]))\nmodel.add(Activation('softmax'))\n\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2024-07-19T13:51:16.582519Z","iopub.execute_input":"2024-07-19T13:51:16.582913Z","iopub.status.idle":"2024-07-19T13:51:16.915347Z","shell.execute_reply.started":"2024-07-19T13:51:16.582887Z","shell.execute_reply":"2024-07-19T13:51:16.914084Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## モデルのトレーニング","metadata":{}},{"cell_type":"code","source":"batch=32","metadata":{"execution":{"iopub.status.busy":"2024-07-19T13:51:23.129737Z","iopub.execute_input":"2024-07-19T13:51:23.130171Z","iopub.status.idle":"2024-07-19T13:51:23.135121Z","shell.execute_reply.started":"2024-07-19T13:51:23.130133Z","shell.execute_reply":"2024-07-19T13:51:23.134005Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(\n      optimizer='adam',\n      loss='categorical_crossentropy',\n      metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2024-07-19T13:51:27.602954Z","iopub.execute_input":"2024-07-19T13:51:27.603506Z","iopub.status.idle":"2024-07-19T13:51:27.621049Z","shell.execute_reply.started":"2024-07-19T13:51:27.603463Z","shell.execute_reply":"2024-07-19T13:51:27.619827Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trained_model=model.fit(X_train,Y_train,\n         epochs=20,\n         batch_size=batch,\n         steps_per_epoch=X_train.shape[0]//batch,\n         validation_steps=X_test.shape[0]//batch,\n         validation_data=(X_test,Y_test),\n         verbose=2)","metadata":{"execution":{"iopub.status.busy":"2024-07-19T13:51:34.032331Z","iopub.execute_input":"2024-07-19T13:51:34.032842Z","iopub.status.idle":"2024-07-19T14:14:06.617267Z","shell.execute_reply.started":"2024-07-19T13:51:34.032798Z","shell.execute_reply":"2024-07-19T14:14:06.615556Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## モデル予測を行う","metadata":{}},{"cell_type":"code","source":"test_datagen = ImageDataGenerator()\n\ntest_set = test_datagen.flow_from_directory(\n    '/kaggle/input/dog-breed-identification',\n    target_size = (128,128),\n    classes=['test']\n)\ny_pred = model.predict(test_set)","metadata":{"execution":{"iopub.status.busy":"2024-07-19T14:22:17.093350Z","iopub.execute_input":"2024-07-19T14:22:17.098003Z","iopub.status.idle":"2024-07-19T14:23:39.894543Z","shell.execute_reply.started":"2024-07-19T14:22:17.097956Z","shell.execute_reply":"2024-07-19T14:23:39.893470Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 画像ファイル名からIDを抽出し、予測結果をcsvファイルとして出力する","metadata":{}},{"cell_type":"code","source":"import re\nfile_list = test_set.filenames\nid_list = []\nfor name in file_list:\n    m = re.sub('test/', '', name)\n    m = re.sub('.jpg', '', m)\n    id_list.append(m)\n\n","metadata":{"execution":{"iopub.status.busy":"2024-07-19T14:30:28.884863Z","iopub.execute_input":"2024-07-19T14:30:28.887387Z","iopub.status.idle":"2024-07-19T14:30:28.931338Z","shell.execute_reply.started":"2024-07-19T14:30:28.887323Z","shell.execute_reply":"2024-07-19T14:30:28.929956Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.read_csv('/kaggle/input/dog-breed-identification/sample_submission.csv')\nsubmission.head()\nsubmission['id'] = id_list\nsubmission.iloc[:,1:] =y_pred\nsubmission.head()\n\ndf = pd.DataFrame(submission)\ndf = submission.set_index('id')\ndf.to_csv('submission.csv', index=False)\n# print(df)","metadata":{"execution":{"iopub.status.busy":"2024-07-19T14:30:44.421455Z","iopub.execute_input":"2024-07-19T14:30:44.421911Z","iopub.status.idle":"2024-07-19T14:30:47.610279Z","shell.execute_reply.started":"2024-07-19T14:30:44.421880Z","shell.execute_reply":"2024-07-19T14:30:47.608918Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(df)","metadata":{"execution":{"iopub.status.busy":"2024-07-19T14:30:51.584976Z","iopub.execute_input":"2024-07-19T14:30:51.585403Z","iopub.status.idle":"2024-07-19T14:30:51.618597Z","shell.execute_reply.started":"2024-07-19T14:30:51.585373Z","shell.execute_reply":"2024-07-19T14:30:51.616931Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.to_csv('submission3.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2024-07-19T14:32:13.875051Z","iopub.execute_input":"2024-07-19T14:32:13.875610Z","iopub.status.idle":"2024-07-19T14:32:16.302982Z","shell.execute_reply.started":"2024-07-19T14:32:13.875573Z","shell.execute_reply":"2024-07-19T14:32:16.301433Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}