{"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":7327,"databundleVersionId":861871,"sourceType":"competition"},{"sourceId":8868919,"sourceType":"datasetVersion","datasetId":5337426}],"dockerImageVersionId":30747,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"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-26T08:55:57.136267Z","iopub.execute_input":"2024-07-26T08:55:57.136545Z","iopub.status.idle":"2024-07-26T08:56:11.279026Z","shell.execute_reply.started":"2024-07-26T08:55:57.13652Z","shell.execute_reply":"2024-07-26T08:56:11.278049Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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())\n","metadata":{"execution":{"iopub.status.busy":"2024-07-26T08:56:11.280475Z","iopub.execute_input":"2024-07-26T08:56:11.281014Z","iopub.status.idle":"2024-07-26T08:56:11.319246Z","shell.execute_reply.started":"2024-07-26T08:56:11.280987Z","shell.execute_reply":"2024-07-26T08:56:11.31847Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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-26T08:56:11.320242Z","iopub.execute_input":"2024-07-26T08:56:11.320522Z","iopub.status.idle":"2024-07-26T08:56:20.239315Z","shell.execute_reply.started":"2024-07-26T08:56:11.320499Z","shell.execute_reply":"2024-07-26T08:56:20.238532Z"},"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-26T08:56:20.241477Z","iopub.execute_input":"2024-07-26T08:56:20.241764Z","iopub.status.idle":"2024-07-26T08:56:20.259015Z","shell.execute_reply.started":"2024-07-26T08:56:20.241739Z","shell.execute_reply":"2024-07-26T08:56:20.258074Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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-26T08:56:20.260127Z","iopub.execute_input":"2024-07-26T08:56:20.260409Z","iopub.status.idle":"2024-07-26T08:56:20.29679Z","shell.execute_reply.started":"2024-07-26T08:56:20.260386Z","shell.execute_reply":"2024-07-26T08:56:20.296079Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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-26T08:56:20.297695Z","iopub.execute_input":"2024-07-26T08:56:20.297959Z","iopub.status.idle":"2024-07-26T08:57:20.278404Z","shell.execute_reply.started":"2024-07-26T08:56:20.297937Z","shell.execute_reply":"2024-07-26T08:57:20.27747Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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-26T08:57:20.279749Z","iopub.execute_input":"2024-07-26T08:57:20.280197Z","iopub.status.idle":"2024-07-26T08:57:20.818291Z","shell.execute_reply.started":"2024-07-26T08:57:20.280164Z","shell.execute_reply":"2024-07-26T08:57:20.817272Z"},"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-26T08:57:20.819875Z","iopub.execute_input":"2024-07-26T08:57:20.820208Z","iopub.status.idle":"2024-07-26T08:57:20.826629Z","shell.execute_reply.started":"2024-07-26T08:57:20.820182Z","shell.execute_reply":"2024-07-26T08:57:20.82562Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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-26T08:57:20.828092Z","iopub.execute_input":"2024-07-26T08:57:20.828416Z","iopub.status.idle":"2024-07-26T08:57:21.567668Z","shell.execute_reply.started":"2024-07-26T08:57:20.828387Z","shell.execute_reply":"2024-07-26T08:57:21.566658Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch=32","metadata":{"execution":{"iopub.status.busy":"2024-07-26T08:57:21.571414Z","iopub.execute_input":"2024-07-26T08:57:21.571753Z","iopub.status.idle":"2024-07-26T08:57:21.57565Z","shell.execute_reply.started":"2024-07-26T08:57:21.571729Z","shell.execute_reply":"2024-07-26T08:57:21.574645Z"},"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-26T08:57:21.576855Z","iopub.execute_input":"2024-07-26T08:57:21.577461Z","iopub.status.idle":"2024-07-26T08:57:21.592278Z","shell.execute_reply.started":"2024-07-26T08:57:21.577431Z","shell.execute_reply":"2024-07-26T08:57:21.591554Z"},"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-26T08:57:21.593251Z","iopub.execute_input":"2024-07-26T08:57:21.593506Z","iopub.status.idle":"2024-07-26T08:58:24.684569Z","shell.execute_reply.started":"2024-07-26T08:57:21.593485Z","shell.execute_reply":"2024-07-26T08:58:24.683832Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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-26T08:58:24.685824Z","iopub.execute_input":"2024-07-26T08:58:24.686079Z","iopub.status.idle":"2024-07-26T08:59:36.129079Z","shell.execute_reply.started":"2024-07-26T08:58:24.686057Z","shell.execute_reply":"2024-07-26T08:59:36.128165Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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-26T08:59:36.132288Z","iopub.execute_input":"2024-07-26T08:59:36.132577Z","iopub.status.idle":"2024-07-26T08:59:36.179682Z","shell.execute_reply.started":"2024-07-26T08:59:36.132553Z","shell.execute_reply":"2024-07-26T08:59:36.179028Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_datagen = ImageDataGenerator()\n\ntrain_set = train_datagen.flow_from_directory(\n    '/kaggle/input/dog-breed-identification',\n    target_size = (128,128),\n    classes=['train']\n)\ny_pred_train = model.predict(train_set)","metadata":{"execution":{"iopub.status.busy":"2024-07-26T08:59:36.180628Z","iopub.execute_input":"2024-07-26T08:59:36.180898Z","iopub.status.idle":"2024-07-26T09:00:02.510691Z","shell.execute_reply.started":"2024-07-26T08:59:36.180875Z","shell.execute_reply":"2024-07-26T09:00:02.509829Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import log_loss\nlog_loss(Y, y_pred_train)","metadata":{"execution":{"iopub.status.busy":"2024-07-26T09:00:02.512002Z","iopub.execute_input":"2024-07-26T09:00:02.512289Z","iopub.status.idle":"2024-07-26T09:00:02.627881Z","shell.execute_reply.started":"2024-07-26T09:00:02.512266Z","shell.execute_reply":"2024-07-26T09:00:02.627053Z"},"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-26T09:00:02.629022Z","iopub.execute_input":"2024-07-26T09:00:02.62933Z","iopub.status.idle":"2024-07-26T09:00:05.699155Z","shell.execute_reply.started":"2024-07-26T09:00:02.629304Z","shell.execute_reply":"2024-07-26T09:00:05.698131Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(df)","metadata":{"execution":{"iopub.status.busy":"2024-07-26T09:00:05.700391Z","iopub.execute_input":"2024-07-26T09:00:05.700762Z","iopub.status.idle":"2024-07-26T09:00:05.724686Z","shell.execute_reply.started":"2024-07-26T09:00:05.700729Z","shell.execute_reply":"2024-07-26T09:00:05.723799Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.to_csv('submission3.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2024-07-26T09:00:05.725855Z","iopub.execute_input":"2024-07-26T09:00:05.726126Z","iopub.status.idle":"2024-07-26T09:00:08.287498Z","shell.execute_reply.started":"2024-07-26T09:00:05.726102Z","shell.execute_reply":"2024-07-26T09:00:08.286524Z"},"trusted":true},"execution_count":null,"outputs":[]}]}