{"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":"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":"2023-07-30T10:16:36.032109Z","iopub.execute_input":"2023-07-30T10:16:36.032407Z","iopub.status.idle":"2023-07-30T10:16:36.048652Z","shell.execute_reply.started":"2023-07-30T10:16:36.032380Z","shell.execute_reply":"2023-07-30T10:16:36.047770Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## zipファイルの解凍","metadata":{}},{"cell_type":"code","source":"# -qq…メッセージを表示しない\n!unzip -qq /kaggle/input/dogs-vs-cats-redux-kernels-edition/test.zip\n!unzip -qq /kaggle/input/dogs-vs-cats-redux-kernels-edition/train.zip\nprint(\"finish\")","metadata":{"execution":{"iopub.status.busy":"2023-07-30T10:16:42.307297Z","iopub.execute_input":"2023-07-30T10:16:42.308206Z","iopub.status.idle":"2023-07-30T10:16:59.750776Z","shell.execute_reply.started":"2023-07-30T10:16:42.308174Z","shell.execute_reply":"2023-07-30T10:16:59.749506Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## フォルダの作成\n学習データ用のフォルダの作成","metadata":{}},{"cell_type":"code","source":"!mkdir data\n!mkdir data/train\n!mkdir data/train/cats\n!mkdir data/train/dogs\n!mkdir data/vali\n!mkdir data/vali/cats\n!mkdir data/vali/dogs\n\nprint(\"finish\")","metadata":{"execution":{"iopub.status.busy":"2023-07-30T10:17:31.396011Z","iopub.execute_input":"2023-07-30T10:17:31.396425Z","iopub.status.idle":"2023-07-30T10:17:38.282003Z","shell.execute_reply.started":"2023-07-30T10:17:31.396389Z","shell.execute_reply":"2023-07-30T10:17:38.280847Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 画像の分別\ntrain内にdogとcatの画像が混合しているためフォルダに応じて犬と猫の画像に振り分ける","metadata":{}},{"cell_type":"code","source":"# 学習用画像枚数（変更可。合計は12500以下にすること）\ntrain_img = 11000\nvali_img = 1500\n\nprint(\"finish\")","metadata":{"execution":{"iopub.status.busy":"2023-07-30T10:17:42.821081Z","iopub.execute_input":"2023-07-30T10:17:42.821519Z","iopub.status.idle":"2023-07-30T10:17:42.828671Z","shell.execute_reply.started":"2023-07-30T10:17:42.821480Z","shell.execute_reply":"2023-07-30T10:17:42.827656Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# データの振り分け先指定（train）\n\n# 移動元のフォルダ\noriginal_dataset_dir = \"/kaggle/working/train\"\n# 移動先のtrain_catフォルダ\ntrain_cats_dir = \"/kaggle/working/data/train/cats\"\n# 移動先のtrain_dogフォルダ\ntrain_dogs_dir = \"/kaggle/working/data/train/dogs\"\n\nprint(\"finish\")","metadata":{"execution":{"iopub.status.busy":"2023-07-30T10:17:46.460412Z","iopub.execute_input":"2023-07-30T10:17:46.461082Z","iopub.status.idle":"2023-07-30T10:17:46.467356Z","shell.execute_reply.started":"2023-07-30T10:17:46.461051Z","shell.execute_reply":"2023-07-30T10:17:46.466406Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 画像振り分け\nimport shutil\n# 画像名が「cat.通し番号.jpg」のためfor分を使用して画像名のリストを作成\nfnames = [\"cat.{}.jpg\".format(i) for i in range(train_img)]\n\nfor fname in fnames:\n    # 移動元のフォルダのパスと画像名を結合させて移動元の画像のパスを作成\n    from_data = original_dataset_dir + \"/\" + fname\n    # 移動先のフォルダのパスと画像名を結合させて移動先の画像のパスを作成\n    to_data = train_cats_dir + \"/\" + fname\n    # from_dataからto_dataへデータのコピー\n    shutil.copyfile(from_data, to_data)\n    \nfnames = [\"dog.{}.jpg\".format(i) for i in range(train_img)]\n\nfor fname in fnames:\n    from_data = original_dataset_dir + \"/\" + fname\n    to_data = train_dogs_dir + \"/\" + fname\n    shutil.copyfile(from_data, to_data)\n    \nprint(\"finish\")","metadata":{"execution":{"iopub.status.busy":"2023-07-30T10:17:49.839028Z","iopub.execute_input":"2023-07-30T10:17:49.839778Z","iopub.status.idle":"2023-07-30T10:17:52.079167Z","shell.execute_reply.started":"2023-07-30T10:17:49.839741Z","shell.execute_reply":"2023-07-30T10:17:52.078087Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# データの振り分け先指定（vali）\n\n# 移動元のフォルダ\noriginal_dataset_dir = \"/kaggle/working/train\"\n# 移動先のvalidation_catフォルダ\nvali_cats_dir = \"/kaggle/working/data/vali/cats\"\n# 移動先のvalidation_catフォルダ\nvali_dogs_dir = \"/kaggle/working/data/vali/dogs\"\n\nprint(\"finish\")","metadata":{"execution":{"iopub.status.busy":"2023-07-30T10:17:57.028780Z","iopub.execute_input":"2023-07-30T10:17:57.029195Z","iopub.status.idle":"2023-07-30T10:17:57.038510Z","shell.execute_reply.started":"2023-07-30T10:17:57.029159Z","shell.execute_reply":"2023-07-30T10:17:57.037481Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fnames = [\"cat.{}.jpg\".format(i+train_img) for i in range(vali_img)]\n\nfor fname in fnames:\n    from_data = original_dataset_dir + \"/\" + fname\n    to_data = vali_cats_dir + \"/\" + fname\n    shutil.copyfile(from_data, to_data)\n    \nfnames = [\"dog.{}.jpg\".format(i+train_img) for i in range(vali_img)]\n\nfor fname in fnames:\n    from_data = original_dataset_dir + \"/\" + fname\n    to_data = vali_dogs_dir + \"/\" + fname\n    shutil.copyfile(from_data, to_data)\n    \nprint(\"finish\")","metadata":{"execution":{"iopub.status.busy":"2023-07-30T10:18:00.148161Z","iopub.execute_input":"2023-07-30T10:18:00.148552Z","iopub.status.idle":"2023-07-30T10:18:00.458259Z","shell.execute_reply.started":"2023-07-30T10:18:00.148521Z","shell.execute_reply":"2023-07-30T10:18:00.457269Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 画像枚数の確認\n# len(os.listdir(path))…os.listdirでパスのフォルダ内のファイル名のリストを作成、lenでリストの長さを返すことでフォルダ内の画像の枚数を表示\nprint(\"train cat:{}\".format(len(os.listdir(train_cats_dir))))\nprint(\"train dog:{}\".format(len(os.listdir(train_dogs_dir))))\n\nprint(\"vali cat:{}\".format(len(os.listdir(vali_cats_dir))))\nprint(\"vali dog:{}\".format(len(os.listdir(vali_dogs_dir))))\n\n# train cat:11000\n# train dog:11000\n# vali cat:1500\n# vali dog:1500","metadata":{"execution":{"iopub.status.busy":"2023-07-30T10:18:04.228297Z","iopub.execute_input":"2023-07-30T10:18:04.228718Z","iopub.status.idle":"2023-07-30T10:18:04.255463Z","shell.execute_reply.started":"2023-07-30T10:18:04.228689Z","shell.execute_reply":"2023-07-30T10:18:04.254473Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 学習用のライブラリのインポート\n必要なライブラリのインポートを行う","metadata":{}},{"cell_type":"code","source":"# ライブラリのインポート\nimport numpy as np\nimport matplotlib.pyplot as plt\n%matplotlib inline\n\nimport os\nimport math\nimport urllib.request\nimport zipfile\n\nfrom keras.models import Sequential, load_model, Model\nfrom keras.layers import Conv2D, MaxPooling2D, Dropout, Flatten, Dense\nfrom keras.layers import Activation, BatchNormalization, Input\nfrom tensorflow.keras.optimizers import Adam\n# from keras.optimizers import adam_v2\n# from keras.optimizers import adam_v2, sgd_experimental\nfrom keras.utils import np_utils\nfrom keras.callbacks import EarlyStopping\nfrom keras.applications.vgg16 import VGG16\nfrom keras.preprocessing.image import ImageDataGenerator\nimport tensorflow as tf","metadata":{"execution":{"iopub.status.busy":"2023-07-30T10:18:08.549357Z","iopub.execute_input":"2023-07-30T10:18:08.549722Z","iopub.status.idle":"2023-07-30T10:18:17.697696Z","shell.execute_reply.started":"2023-07-30T10:18:08.549693Z","shell.execute_reply":"2023-07-30T10:18:17.696629Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## VGG16モデルの取得\nVGG16は「ImageNet」と呼ばれる大規模画像データセットで学習された16層から成るCNNモデルである。  \n100万枚を超える画像で学習され、1000のクラスに分類することができる。","metadata":{}},{"cell_type":"code","source":"# モデルの取得\nmodel = VGG16(include_top=False, weights=\"imagenet\")\n# モデル構造の確認\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2023-07-30T10:18:25.988998Z","iopub.execute_input":"2023-07-30T10:18:25.989727Z","iopub.status.idle":"2023-07-30T10:18:31.412918Z","shell.execute_reply.started":"2023-07-30T10:18:25.989693Z","shell.execute_reply":"2023-07-30T10:18:31.412132Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_vgg16 = VGG16(include_top=False, weights=\"imagenet\", input_tensor=Input(shape=(224,224,3)))\n# 層の確認\nmodel_vgg16.layers\n","metadata":{"execution":{"iopub.status.busy":"2023-07-30T10:18:38.435947Z","iopub.execute_input":"2023-07-30T10:18:38.436311Z","iopub.status.idle":"2023-07-30T10:18:38.754271Z","shell.execute_reply.started":"2023-07-30T10:18:38.436281Z","shell.execute_reply":"2023-07-30T10:18:38.753264Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 学習データの作成\n画像の分別を行ったフォルダから学習用のデータを作成する。  \nImageDataGenerator…リアルタイムにデータ拡張しながら，テンソル画像データのバッチを生成する。","metadata":{}},{"cell_type":"code","source":"# クラスは犬or猫の2つ\nnum_slass = 2\nbatch_size = 128\n\n# 学習データのパス\ntrain_dir = \"/kaggle/working/data/train\"\nvalid_dir = \"/kaggle/working/data/vali\"\n\n# データ拡張しながら，テンソル画像データのバッチを生成\n# データ拡張（trainのみ）\n# rescale=1.0/255：画素値を0~255から0~1の範囲へ正規化\n# rotation_range=45：-45度~45度の範囲でランダムに画像を回転\n# horizontal_flip=True：画像をランダムに水平シフトし反転する\n# width_shift_range=0.2：ランダムに水平シフトを行う\n# height_shift_range=0.2：ランダムに垂直シフトを行う\n# zoom_range=0.5：ランダムにズームを行う\ntrain_data_generator = ImageDataGenerator(rescale=1.0/255,\n                                          rotation_range=45,\n                                          horizontal_flip=True,\n                                          width_shift_range=0.2,\n                                          height_shift_range=0.2)\n\n# 正規化のみ\nvalid_data_generator = ImageDataGenerator(rescale=1.0/255)\n\n# ディレクトリからデータを生成\n# 画像サイズはVGG16モデルにならって(224.224)\n# train_dir：対象フォルダ\n# target_size=(224,224)：画像のリサイズ\n# batch_size=batch_size：バッチサイズ（128）\n# class_mode=\"binary\"：0か1の2値分類のため\"binary\"を選択\n# shuffle=False：データをシャッフルしない\ntrain_generator = train_data_generator.flow_from_directory(\n    train_dir,\n    target_size=(224,224),\n    batch_size=batch_size,\n    class_mode=\"binary\",\n    shuffle=False\n)\n\nvalid_generator = valid_data_generator.flow_from_directory(\n    valid_dir,\n    target_size=(224,224),\n    batch_size=batch_size,\n    class_mode=\"binary\",\n    shuffle=False\n)\n","metadata":{"execution":{"iopub.status.busy":"2023-07-30T10:18:47.588751Z","iopub.execute_input":"2023-07-30T10:18:47.589112Z","iopub.status.idle":"2023-07-30T10:18:48.348751Z","shell.execute_reply.started":"2023-07-30T10:18:47.589083Z","shell.execute_reply":"2023-07-30T10:18:48.347765Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## モデル構築\n新しく全結合層を作成し、VGG16と合体させる","metadata":{}},{"cell_type":"code","source":"# VGG16をインポート\nmodel_vgg16 = VGG16(include_top=False, weights=\"imagenet\", input_tensor=Input(shape=(224,224,3)))\n\n# 全結合層を作成\nx = model_vgg16.output\nx = Flatten()(x)\nx = Dense(256)(x)\nx = Activation(\"relu\")(x)\nx = Dropout(0.5)(x)\nprediction = Dense(1, activation=\"sigmoid\")(x)\n\n# モデル合体\nmodel = Model(inputs=model_vgg16.input, outputs=prediction)\n","metadata":{"execution":{"iopub.status.busy":"2023-07-30T10:18:54.836883Z","iopub.execute_input":"2023-07-30T10:18:54.837748Z","iopub.status.idle":"2023-07-30T10:18:55.198647Z","shell.execute_reply.started":"2023-07-30T10:18:54.837706Z","shell.execute_reply":"2023-07-30T10:18:55.197569Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## ファインチューニング\nファインチューニング…事前学習した訓練済みのモデルの一部を別のデータセットを用いて再トレーニングすることで、新しいタスク向けにモデルのパラメータの微調整をする。\n100万枚を超える画像で学習したVGG16は既にうまく特徴量を抽出できるようにパラメータが調整されているため、高い識別精度、短時間での学習が行える。","metadata":{}},{"cell_type":"code","source":"# block4_pool(14層目)までの重みの更新をキャンセルし、浅い層の重みはそのまま使用する\n# 全結合層に加えてVGG16のblock5の畳み込み層の重みも更新する\nfor layer in model_vgg16.layers[:15]:\n  layer.trainable=False\n\n# 学習の設定\n# 損失はコンペのスコアと同様にクロスエントロピー損失を使用\nmodel.compile(loss=\"binary_crossentropy\",\n              optimizer=Adam(learning_rate=0.0001),\n              metrics=[\"accuracy\"])","metadata":{"execution":{"iopub.status.busy":"2023-07-30T10:19:01.805362Z","iopub.execute_input":"2023-07-30T10:19:01.805738Z","iopub.status.idle":"2023-07-30T10:19:01.828156Z","shell.execute_reply.started":"2023-07-30T10:19:01.805705Z","shell.execute_reply":"2023-07-30T10:19:01.827289Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 学習\n学習部分。学習後、モデルの保存を行う。","metadata":{}},{"cell_type":"code","source":"# 学習\n# train_generator：学習用のデータ\n# epochs=10：学習回数\n# verbose=1：ログ出力あり\n# validation_data=valid_generator：検証用のデータ\n# callbacks=[EarlyStopping(patience=10)]：過学習対策、val_lossに対して減少が停止したときに学習をストップ\nhistory = model.fit(train_generator,\n                    epochs=10,\n                    verbose=1,\n                    validation_data=valid_generator,\n                    callbacks=[EarlyStopping(patience=10)])\n\n# モデル保存\nmodel.save(\"/kaggle/working/data/CNN_VGG16.h5\")","metadata":{"execution":{"iopub.status.busy":"2023-07-30T10:19:17.205024Z","iopub.execute_input":"2023-07-30T10:19:17.205472Z","iopub.status.idle":"2023-07-30T11:21:00.219740Z","shell.execute_reply.started":"2023-07-30T10:19:17.205438Z","shell.execute_reply":"2023-07-30T11:21:00.218564Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## lossとaccuracyを可視化\n学習中のlossとaccuracyをグラフで表示する。","metadata":{}},{"cell_type":"code","source":"# lossとaccuracyを可視化\nprint(\"-\"*100)\nplt.figure(1, figsize=(13,4))\nplt.subplots_adjust(wspace=0.5)\n\n# 学習曲線（loss）\nplt.subplot(1, 2, 1)\nplt.plot(history.history[\"loss\"], label=\"train\")\nplt.plot(history.history[\"val_loss\"], label=\"valid\")\nplt.title(\"train and valid loss\")\nplt.xlabel(\"epoch\")\nplt.ylabel(\"loss\")\nplt.legend()\nplt.grid()\n\n# 精度表示（accuracy）\nplt.subplot(1, 2, 2)\nplt.plot(history.history[\"accuracy\"], label=\"train\")\nplt.plot(history.history[\"val_accuracy\"], label=\"valid\")\nplt.title(\"train and valid accuracy\")\nplt.xlabel(\"epoch\")\nplt.ylabel(\"accuracy\")\nplt.legend()\nplt.grid()\n\n# グラフの表示\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2023-07-30T11:21:58.840582Z","iopub.execute_input":"2023-07-30T11:21:58.841088Z","iopub.status.idle":"2023-07-30T11:21:59.377171Z","shell.execute_reply.started":"2023-07-30T11:21:58.841058Z","shell.execute_reply":"2023-07-30T11:21:59.376234Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## csv書き出しのライブラリのインポート\n必要なライブラリのインポートを行う","metadata":{}},{"cell_type":"code","source":"# ライブラリのインポート\nimport keras.utils as image\nfrom keras.utils import load_img, img_to_array\nimport re\nimport csv\nimport glob\nfrom keras import models","metadata":{"execution":{"iopub.status.busy":"2023-07-30T11:22:16.960562Z","iopub.execute_input":"2023-07-30T11:22:16.960931Z","iopub.status.idle":"2023-07-30T11:22:16.966951Z","shell.execute_reply.started":"2023-07-30T11:22:16.960900Z","shell.execute_reply":"2023-07-30T11:22:16.965665Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## submission.csvの書き出し\nスコア確認用にテストフォルダの画像の予測値をsubmission.csvへ書き出す。\n画像は読み込んだ後に、学習時と同様に正規化を行う。  ","metadata":{}},{"cell_type":"code","source":"# テストデータのパスを取得\np = re.compile(r'\\d+')\nimcount = 1\n# テストフォルダの画像パスをすべて取得する\ntest_list = glob.glob('/kaggle/working/test/*.jpg')\n# テストフォルダの画像パスをソートする\ntest_list = sorted(test_list,key=lambda s: int(p.search(s).group()))\n\n#モデルの読み込み\n# model_vgg = models.load_model('/kaggle/working/data/CNN_VGG16.h5')\n# print(\"load model\")\n\n#csv書き込み（テストデータ1～テストデータ6250）\nwith open('/kaggle/working/submission.csv', 'w') as f:\n  writer= csv.writer(f, lineterminator='\\n')\n  writer.writerow(['id', 'label'])\n  for i in range(6250):\n    # 画像パスの取得\n    j = test_list[i]\n    # 画像読み込み（224,224）\n    img = image.load_img(j, target_size=(224, 224))\n    # PIL 形式から ndarray に変換する\n    x = img_to_array(img)\n    # 次元の追加\n    x = np.expand_dims(x, axis=0)\n    # 正規化\n    x = x / 255.0\n    # 学習したモデルを用いて予測\n    result_predict = model.predict(x)\n    # csvファイルに書き込み\n    writer.writerow([imcount, result_predict[0][0]])\n    imcount += 1\n    print(result_predict[0][0])\n\nprint(\"finish\")","metadata":{"execution":{"iopub.status.busy":"2023-07-30T11:22:27.867571Z","iopub.execute_input":"2023-07-30T11:22:27.868013Z","iopub.status.idle":"2023-07-30T11:30:02.096970Z","shell.execute_reply.started":"2023-07-30T11:22:27.867975Z","shell.execute_reply":"2023-07-30T11:30:02.096012Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#csv書き込み（テストデータ6250～テストデータ12500）\nwith open('/kaggle/working/submission.csv', 'a') as f:\n  writer= csv.writer(f, lineterminator='\\n')\n  for i in range(6250, 12500):\n    j = test_list[i]\n    img = image.load_img(j, target_size=(224, 224))\n    x = img_to_array(img)\n    x = np.expand_dims(x, axis=0)\n    x = x / 255.0\n    result_predict = model.predict(x)\n    writer.writerow([imcount, result_predict[0][0]])\n    imcount += 1\n    print(i)\n\nprint(\"finish\")","metadata":{"execution":{"iopub.status.busy":"2023-07-30T11:31:13.165982Z","iopub.execute_input":"2023-07-30T11:31:13.166479Z","iopub.status.idle":"2023-07-30T11:38:49.005015Z","shell.execute_reply.started":"2023-07-30T11:31:13.166439Z","shell.execute_reply":"2023-07-30T11:38:49.003916Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Score: 0.17279","metadata":{}}]}