{"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":"# 画像から犬か猫に分類","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","_kg_hide-output":true,"_kg_hide-input":true,"execution":{"iopub.status.busy":"2021-11-08T07:52:11.555015Z","iopub.execute_input":"2021-11-08T07:52:11.555731Z","iopub.status.idle":"2021-11-08T07:52:11.583181Z","shell.execute_reply.started":"2021-11-08T07:52:11.555641Z","shell.execute_reply":"2021-11-08T07:52:11.582535Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## zipファイルを解凍し，DataFrameに格納\nimport os, shutil, zipfile\n\ndata = ['train', 'test1']\n\nfor x in data:\n    with zipfile.ZipFile('../input/dogs-vs-cats/' + x + '.zip', 'r') as z:\n        z.extractall(\".\")","metadata":{"_kg_hide-input":true,"_kg_hide-output":true,"execution":{"iopub.status.busy":"2021-11-08T07:52:14.598866Z","iopub.execute_input":"2021-11-08T07:52:14.599328Z","iopub.status.idle":"2021-11-08T07:52:32.356452Z","shell.execute_reply.started":"2021-11-08T07:52:14.59929Z","shell.execute_reply":"2021-11-08T07:52:32.355682Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## ライブラリ読み込み\nimport numpy as np\nimport pandas as pd\nfrom keras.preprocessing.image import ImageDataGenerator,load_img\nfrom tensorflow.keras.utils import to_categorical\nfrom sklearn.model_selection import train_test_split\nimport matplotlib.pyplot as plt\nimport random\nimport os","metadata":{"execution":{"iopub.status.busy":"2021-11-08T07:52:32.358014Z","iopub.execute_input":"2021-11-08T07:52:32.358301Z","iopub.status.idle":"2021-11-08T07:52:38.499754Z","shell.execute_reply.started":"2021-11-08T07:52:32.358262Z","shell.execute_reply":"2021-11-08T07:52:38.499042Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Image_Width=128\nImage_Height=128\nImage_Size=(Image_Width,Image_Height)\nImage_Channels=3","metadata":{"execution":{"iopub.status.busy":"2021-11-08T07:52:38.50119Z","iopub.execute_input":"2021-11-08T07:52:38.501466Z","iopub.status.idle":"2021-11-08T07:52:38.506619Z","shell.execute_reply.started":"2021-11-08T07:52:38.50142Z","shell.execute_reply":"2021-11-08T07:52:38.50604Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## 画像のファイル名から正解ラベル生成\nimport pandas as pd\n\nfilenames = os.listdir('./train/')\ncategories = []\n\nfor Fname in filenames:\n    DogCat = Fname.split('.')[0]\n    if DogCat == 'dog':\n        categories.append(1)\n    else:\n        categories.append(0)\n\ndf = pd.DataFrame({\n    'filename': filenames,\n    'category': categories\n})\n\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-08T07:52:38.508244Z","iopub.execute_input":"2021-11-08T07:52:38.508639Z","iopub.status.idle":"2021-11-08T07:52:38.577608Z","shell.execute_reply.started":"2021-11-08T07:52:38.508601Z","shell.execute_reply":"2021-11-08T07:52:38.576897Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## モデル作成\nfrom keras.models import Sequential\nfrom keras.layers import Conv2D,MaxPooling2D,\\\n     Dropout,Flatten,Dense,Activation,\\\n     BatchNormalization\n\nmodel=Sequential()\n\nmodel.add(Conv2D(32,(3,3),activation='relu',input_shape=(Image_Width,Image_Height,Image_Channels)))\nmodel.add(BatchNormalization())\nmodel.add(MaxPooling2D(pool_size=(2,2)))\nmodel.add(Dropout(0.25))\n\nmodel.add(Conv2D(64,(3,3),activation='relu'))\nmodel.add(BatchNormalization())\nmodel.add(MaxPooling2D(pool_size=(2,2)))\nmodel.add(Dropout(0.25))\n\nmodel.add(Conv2D(128,(3,3),activation='relu'))\nmodel.add(BatchNormalization())\nmodel.add(MaxPooling2D(pool_size=(2,2)))\nmodel.add(Dropout(0.25))\n\nmodel.add(Flatten())\nmodel.add(Dense(512,activation='relu'))\nmodel.add(BatchNormalization())\nmodel.add(Dropout(0.5))\nmodel.add(Dense(2,activation='softmax'))\n\nmodel.compile(loss='categorical_crossentropy',\n  optimizer='rmsprop',metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2021-11-08T07:52:38.57867Z","iopub.execute_input":"2021-11-08T07:52:38.578922Z","iopub.status.idle":"2021-11-08T07:52:41.378078Z","shell.execute_reply.started":"2021-11-08T07:52:38.578888Z","shell.execute_reply":"2021-11-08T07:52:41.377398Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## 棒グラフ作成\nprint(len(df['category']))\ndf['category'].value_counts().plot.bar()","metadata":{"execution":{"iopub.status.busy":"2021-11-08T07:52:41.381854Z","iopub.execute_input":"2021-11-08T07:52:41.385114Z","iopub.status.idle":"2021-11-08T07:52:41.697476Z","shell.execute_reply.started":"2021-11-08T07:52:41.385075Z","shell.execute_reply":"2021-11-08T07:52:41.696806Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## モデルの要約を出力\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2021-11-08T07:52:41.698631Z","iopub.execute_input":"2021-11-08T07:52:41.698876Z","iopub.status.idle":"2021-11-08T07:52:41.71351Z","shell.execute_reply.started":"2021-11-08T07:52:41.698841Z","shell.execute_reply":"2021-11-08T07:52:41.712611Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### ランダムに選択した画像を出力","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import load_img\nimport matplotlib.pyplot as plt\nimport random\n%matplotlib inline\n\nsample = random.sample(filenames, 16)\nplt.figure(figsize = (12, 12))\n\nfor i in range(0, 16):\n    plt.subplot(4, 4, i+1)\n    fname = sample[i]\n    image = load_img('./train/' + fname)\n    plt.imshow(image)\n    plt.axis('off')\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-11-01T04:09:23.11047Z","iopub.execute_input":"2021-11-01T04:09:23.112637Z","iopub.status.idle":"2021-11-01T04:09:25.423753Z","shell.execute_reply.started":"2021-11-01T04:09:23.112561Z","shell.execute_reply":"2021-11-01T04:09:25.42286Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 回転・反転・引き伸ばしした画像を出力","metadata":{}},{"cell_type":"code","source":"from keras.preprocessing.image import ImageDataGenerator\nfrom keras.preprocessing.image import array_to_img, img_to_array, load_img\n\ninput_path = './train/cat.477.jpg'\ncount = 10\n\ntrain_datagen = ImageDataGenerator(rotation_range=15,\n                                rescale=1./255,\n                                shear_range=0.1,\n                                zoom_range=0.2,\n                                horizontal_flip=True,\n                                width_shift_range=0.1,\n                                height_shift_range=0.1\n                                )\nplt.figure(figsize = (16, 6))\nimage = img_to_array(load_img(input_path))\n\nimage = image.reshape((1,) + image.shape)\n\nimages_flow = train_datagen.flow(image, batch_size=1)\nfor i in range(8):\n    batches = images_flow.next()\n    \n    # 画像として表示するため、４次元から3次元データにし、配列から画像に変換\n    gen_img = array_to_img(batches[0])\n \n    plt.subplot(2, 4, i + 1)\n    plt.imshow(gen_img)\n    plt.axis('off')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-11-08T07:53:38.792122Z","iopub.execute_input":"2021-11-08T07:53:38.792692Z","iopub.status.idle":"2021-11-08T07:53:39.885669Z","shell.execute_reply.started":"2021-11-08T07:53:38.792655Z","shell.execute_reply":"2021-11-08T07:53:39.885038Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.callbacks import EarlyStopping, ReduceLROnPlateau\nearlystop = EarlyStopping(patience = 10)\nlearning_rate_reduction = ReduceLROnPlateau(monitor = 'val_acc',patience = 2,verbose = 1,factor = 0.5,min_lr = 0.00001)\ncallbacks = [earlystop,learning_rate_reduction]","metadata":{"execution":{"iopub.status.busy":"2021-10-31T07:35:39.050641Z","iopub.execute_input":"2021-10-31T07:35:39.051093Z","iopub.status.idle":"2021-10-31T07:35:39.057492Z","shell.execute_reply.started":"2021-10-31T07:35:39.051057Z","shell.execute_reply":"2021-10-31T07:35:39.056196Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df[\"category\"] = df[\"category\"].replace({0:'cat',1:'dog'})\ntrain_df,validate_df = train_test_split(df,test_size=0.20,random_state=42)\n\ntrain_df = train_df.reset_index(drop=True)\nvalidate_df = validate_df.reset_index(drop=True)\n\ntotal_train=train_df.shape[0]\ntotal_validate=validate_df.shape[0]\nbatch_size=15","metadata":{"execution":{"iopub.status.busy":"2021-10-31T07:35:44.394693Z","iopub.execute_input":"2021-10-31T07:35:44.395572Z","iopub.status.idle":"2021-10-31T07:35:44.417315Z","shell.execute_reply.started":"2021-10-31T07:35:44.395538Z","shell.execute_reply":"2021-10-31T07:35:44.41603Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_generator = train_datagen.flow_from_dataframe(train_df,\n                                                 \"./train/\",x_col='filename',y_col='category',\n                                                 target_size=Image_Size,\n                                                 class_mode='categorical',\n                                                 batch_size=batch_size)\n\nvalidation_datagen = ImageDataGenerator(rescale=1./255)\nvalidation_generator = validation_datagen.flow_from_dataframe(\n    validate_df, \n    \"./train/\", \n    x_col='filename',\n    y_col='category',\n    target_size=Image_Size,\n    class_mode='categorical',\n    batch_size=batch_size\n)\n\ntest_datagen = ImageDataGenerator(rotation_range=15,\n                                rescale=1./255,\n                                shear_range=0.1,\n                                zoom_range=0.2,\n                                horizontal_flip=True,\n                                width_shift_range=0.1,\n                                height_shift_range=0.1)\n\ntest_generator = train_datagen.flow_from_dataframe(train_df,\n                                                 \"./test/\",x_col='filename',y_col='category',\n                                                 target_size=Image_Size,\n                                                 class_mode='categorical',\n                                                 batch_size=batch_size)\n","metadata":{"execution":{"iopub.status.busy":"2021-10-31T07:35:46.675081Z","iopub.execute_input":"2021-10-31T07:35:46.675431Z","iopub.status.idle":"2021-10-31T07:35:47.135073Z","shell.execute_reply.started":"2021-10-31T07:35:46.675397Z","shell.execute_reply":"2021-10-31T07:35:47.134002Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"epochs=10\nhistory = model.fit_generator(\n    train_generator, \n    epochs=epochs,\n    validation_data=validation_generator,\n    validation_steps=total_validate//batch_size,\n    steps_per_epoch=total_train//batch_size,\n    callbacks=callbacks\n)","metadata":{"execution":{"iopub.status.busy":"2021-10-31T07:35:49.746573Z","iopub.execute_input":"2021-10-31T07:35:49.74702Z","iopub.status.idle":"2021-10-31T08:04:53.60342Z","shell.execute_reply.started":"2021-10-31T07:35:49.746982Z","shell.execute_reply":"2021-10-31T08:04:53.602423Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save(\"model1_catsVSdogs_10epoch.h5\")","metadata":{"execution":{"iopub.status.busy":"2021-10-31T08:04:53.606388Z","iopub.execute_input":"2021-10-31T08:04:53.606715Z","iopub.status.idle":"2021-10-31T08:04:53.890589Z","shell.execute_reply.started":"2021-10-31T08:04:53.606672Z","shell.execute_reply":"2021-10-31T08:04:53.889372Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_filenames = os.listdir(\"./test1/\")\ntest_df = pd.DataFrame({\n    'filename': test_filenames\n})\nnb_samples = test_df.shape[0]\nnb_samples","metadata":{"execution":{"iopub.status.busy":"2021-10-31T08:04:53.892135Z","iopub.execute_input":"2021-10-31T08:04:53.89257Z","iopub.status.idle":"2021-10-31T08:04:53.914302Z","shell.execute_reply.started":"2021-10-31T08:04:53.892524Z","shell.execute_reply":"2021-10-31T08:04:53.913309Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_generator = train_datagen.flow_from_dataframe(test_df,\n                                                   \"./test1/\", \n                                                   x_col='filename', \n                                                   y_col=None,\n                                                   target_size=Image_Size,\n                                                   class_mode=None,\n                                                   batch_size=batch_size)","metadata":{"execution":{"iopub.status.busy":"2021-10-31T08:04:53.918327Z","iopub.execute_input":"2021-10-31T08:04:53.918596Z","iopub.status.idle":"2021-10-31T08:04:54.053493Z","shell.execute_reply.started":"2021-10-31T08:04:53.918566Z","shell.execute_reply":"2021-10-31T08:04:54.052371Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## モデル保存\npredict = model.predict_generator(test_generator, steps=np.ceil(nb_samples/batch_size))","metadata":{"execution":{"iopub.status.busy":"2021-10-31T08:04:54.054916Z","iopub.execute_input":"2021-10-31T08:04:54.055208Z","iopub.status.idle":"2021-10-31T08:06:09.741545Z","shell.execute_reply.started":"2021-10-31T08:04:54.055168Z","shell.execute_reply":"2021-10-31T08:06:09.740077Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df['category'] = 0\ntest_df['category'] = predict\ntest_df.head()","metadata":{"execution":{"iopub.status.busy":"2021-10-31T08:06:09.743302Z","iopub.execute_input":"2021-10-31T08:06:09.74366Z","iopub.status.idle":"2021-10-31T08:06:09.765731Z","shell.execute_reply.started":"2021-10-31T08:06:09.74362Z","shell.execute_reply":"2021-10-31T08:06:09.764338Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 学習結果を画像を出力","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import load_img\nimport matplotlib.pyplot as plt\nimport random\n%matplotlib inline\n\nsample = random.sample(test_filenames, 16)\nfig = plt.figure(figsize = (12, 12))\n\nfor i in range(0, 16):\n    ax = fig.add_subplot(4, 4, i+1)\n    fname = sample[i]\n    image = load_img('./test1/' + fname)\n    valu = test_df['category'][test_df['filename']==fname]\n    if valu.iloc[0] < 0.5:\n        ax.set_title('cat'+'  '+str(valu.iloc[0]))\n    else:\n        ax.set_title('dog'+'  '+str(valu.iloc[0]))\n    ax.imshow(image)\n    ax.axis('off')\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-10-31T08:29:47.38548Z","iopub.execute_input":"2021-10-31T08:29:47.385843Z","iopub.status.idle":"2021-10-31T08:29:49.12072Z","shell.execute_reply.started":"2021-10-31T08:29:47.385806Z","shell.execute_reply":"2021-10-31T08:29:49.11902Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#test_df['filename'] = test_df['filename'].str.extract('(\\d+)', expand=True)\n#test_df = test_df.rename(columns={'filename': 'id', 'category': 'label'})\n\n#test_df.head()","metadata":{"execution":{"iopub.status.busy":"2021-10-31T07:32:18.187581Z","iopub.status.idle":"2021-10-31T07:32:18.18855Z","shell.execute_reply.started":"2021-10-31T07:32:18.188129Z","shell.execute_reply":"2021-10-31T07:32:18.188171Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#test_df.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2021-10-31T07:32:18.190241Z","iopub.status.idle":"2021-10-31T07:32:18.191194Z","shell.execute_reply.started":"2021-10-31T07:32:18.190783Z","shell.execute_reply":"2021-10-31T07:32:18.190825Z"},"trusted":true},"execution_count":null,"outputs":[]}]}