{"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":{"execution":{"iopub.status.busy":"2023-07-28T17:56:48.442196Z","iopub.execute_input":"2023-07-28T17:56:48.442589Z","iopub.status.idle":"2023-07-28T17:56:48.454402Z","shell.execute_reply.started":"2023-07-28T17:56:48.442539Z","shell.execute_reply":"2023-07-28T17:56:48.453273Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## インプットデータの解凍","metadata":{}},{"cell_type":"code","source":"# 必要ライブラリのインポート\nimport shutil\nimport glob\n\n# インプットデータ内、「train.zip」「test.zip」をアウトプットデータとして使用ディレクトリで解凍\nshutil.unpack_archive('/kaggle/input/dogs-vs-cats-redux-kernels-edition/train.zip', '/kaggle/working')\nshutil.unpack_archive('/kaggle/input/dogs-vs-cats-redux-kernels-edition/test.zip', '/kaggle/working')\n\n# インプットデータ内、「sample_submission.csv」をアウトプットデータとして使用ディレクトリにコピー\nshutil.copyfile('/kaggle/input/dogs-vs-cats-redux-kernels-edition/sample_submission.csv','/kaggle/working/sample_submission.csv')\n     \n# /kaggle/workingフォルダに入っているファイルを確認\nfiles = glob.glob('/kaggle/working/*')\nfor file in files:\n    print(file)","metadata":{"execution":{"iopub.status.busy":"2023-07-28T17:56:55.329023Z","iopub.execute_input":"2023-07-28T17:56:55.329383Z","iopub.status.idle":"2023-07-28T17:57:12.298357Z","shell.execute_reply.started":"2023-07-28T17:56:55.329351Z","shell.execute_reply":"2023-07-28T17:57:12.297263Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## インプットデータの確認","metadata":{}},{"cell_type":"code","source":"Train_data_list = glob.glob(os.path.join(\"train\",'*.jpg'))\nprint(f\"Train_data_num: {len(Train_data_list)}\")\n\nTest_data_list = glob.glob(os.path.join(\"test\",'*.jpg'))\nprint(f\"Tset_Data_num: {len(Test_data_list)}\")","metadata":{"execution":{"iopub.status.busy":"2023-07-28T17:57:18.438479Z","iopub.execute_input":"2023-07-28T17:57:18.438870Z","iopub.status.idle":"2023-07-28T17:57:18.578018Z","shell.execute_reply.started":"2023-07-28T17:57:18.438837Z","shell.execute_reply":"2023-07-28T17:57:18.577063Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 追加ライブラリのインポート","metadata":{}},{"cell_type":"code","source":"from tensorflow import keras\nfrom tensorflow.keras import models, optimizers, layers\nfrom tensorflow.keras.preprocessing import image\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nimport tensorflow.keras.layers as layers\nfrom tensorflow.keras.layers import Conv2D, MaxPooling2D, Dropout, Flatten, Dense, GlobalAveragePooling2D\nfrom tensorflow.keras.layers import Activation, BatchNormalization, Input\nfrom tensorflow.keras.applications.vgg16 import VGG16\nfrom tensorflow.keras.models import Sequential, load_model, Model\nfrom tensorflow.keras.optimizers import SGD\n\nimport re\nimport csv\nimport tqdm","metadata":{"execution":{"iopub.status.busy":"2023-07-28T17:57:21.803076Z","iopub.execute_input":"2023-07-28T17:57:21.803439Z","iopub.status.idle":"2023-07-28T17:57:21.810417Z","shell.execute_reply.started":"2023-07-28T17:57:21.803408Z","shell.execute_reply":"2023-07-28T17:57:21.809319Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"markdown","source":"## 学習データの前処理","metadata":{}},{"cell_type":"code","source":"#犬・猫の画像を，それぞれ保存するフォルダのパスを定義\ndog_dir = '/kaggle/working/train/dog'\ncat_dir = '/kaggle/working/train/cat'\n\n#フォルダの作成\nos.makedirs(dog_dir, exist_ok = True)\nos.makedirs(cat_dir, exist_ok = True)\n\n# 犬・猫の画像を，それぞれのフォルダに移動する\nfiles = glob.glob('/kaggle/working/train/*.jpg')# 訓練用フォルダ内の全ファイルパスを取得\nfor file in files:\n    file_name = os.path.basename(file)\n    if 'cat' in file:\n        shutil.move(file,'/kaggle/working/train/cat/' + file_name)\n    else:\n        shutil.move(file,'/kaggle/working/train/dog/' + file_name)\n\n\n#データ拡張の設定\ntrain_datagen = ImageDataGenerator(\n                rescale = 1.0 / 255,\n#                 rotation_range = 90,\n#                 shear_range = 0.1,\n#                 zoom_range = 0.1,\n                horizontal_flip = True,\n                vertical_flip = True,\n                fill_mode = 'reflect',\n                validation_split = 0.2)\n\n\n#ディレクトリの設定\n#学習データ\ntrain_dir = '/kaggle/working/train'\n\n#テストデータ\ntest_dir = 'kaggle/working/test'\n\nbatch_size = 32\n\ntrain_generator = train_datagen.flow_from_directory(\n                    train_dir,\n                    target_size = (224,224),\n                    batch_size = batch_size,\n                    class_mode = 'binary',\n                    subset = \"training\")\n\nvalid_generator = train_datagen.flow_from_directory(\n                    train_dir,\n                    target_size = (224, 224),\n                    batch_size = batch_size,\n                    class_mode = 'binary',\n                    subset = \"validation\")","metadata":{"execution":{"iopub.status.busy":"2023-07-28T17:57:41.246399Z","iopub.execute_input":"2023-07-28T17:57:41.246765Z","iopub.status.idle":"2023-07-28T17:57:43.323530Z","shell.execute_reply.started":"2023-07-28T17:57:41.246735Z","shell.execute_reply":"2023-07-28T17:57:43.322616Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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.1)(x)\nprediction = Dense(1, activation=\"sigmoid\")(x)\n\n# モデル合体\nmodel = Model(inputs=model_vgg16.input, outputs=prediction)\nmodel.compile(loss = 'binary_crossentropy',\n             optimizer = optimizers.Adam(learning_rate = 1e-4),\n             metrics = ['acc'])\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2023-07-28T17:57:50.282607Z","iopub.execute_input":"2023-07-28T17:57:50.282966Z","iopub.status.idle":"2023-07-28T17:57:50.752915Z","shell.execute_reply.started":"2023-07-28T17:57:50.282939Z","shell.execute_reply":"2023-07-28T17:57:50.751986Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"markdown","source":"## 学習モデルの作成","metadata":{}},{"cell_type":"code","source":"# model = models.Sequential()\n# model.add(layers.Conv2D(32, (3, 3), activation = 'relu', input_shape = (150, 150,3)))\n# model.add(layers.MaxPooling2D((2, 2)))\n\n# model.add(layers.Conv2D(64, (3, 3), activation = 'relu'))\n# model.add(layers.MaxPooling2D((2, 2)))\n\n# model.add(layers.Conv2D(128, (3, 3), activation = 'relu'))\n# model.add(layers.MaxPooling2D((2, 2)))\n\n# model.add(layers.Conv2D(256, (3, 3), activation = 'relu'))\n# model.add(layers.MaxPooling2D((2, 2)))\n\n# model.add(layers.Flatten())\n\n# model.add(layers.Dense(512, activation = 'relu'))\n# model.add(layers.Dense(1, activation = 'sigmoid'))\n\n# model.compile(loss = 'binary_crossentropy',\n#              optimizer = optimizers.Adam(learning_rate = 1e-4),\n#              metrics = ['acc'])\n\n# #作成したモデルを表示して確認\n# model.summary()","metadata":{"execution":{"iopub.status.busy":"2023-07-28T17:57:59.623653Z","iopub.execute_input":"2023-07-28T17:57:59.624012Z","iopub.status.idle":"2023-07-28T17:57:59.629648Z","shell.execute_reply.started":"2023-07-28T17:57:59.623977Z","shell.execute_reply":"2023-07-28T17:57:59.628360Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 学習フェーズ","metadata":{}},{"cell_type":"code","source":"# block5_pool(18層目)までの重みの更新をキャンセル\nfor layer in model_vgg16.layers[:15]:\n  layer.trainable=False\n\n\nhistory = model.fit_generator(\n            train_generator,\n            steps_per_epoch = 20000 / batch_size,\n            epochs = 5,\n            validation_data = valid_generator,\n            validation_steps = 5000 / batch_size\n            )\n\nmodel.save('dog_vs_cat.h5')","metadata":{"execution":{"iopub.status.busy":"2023-07-28T17:58:07.294829Z","iopub.execute_input":"2023-07-28T17:58:07.295711Z","iopub.status.idle":"2023-07-28T18:10:06.146110Z","shell.execute_reply.started":"2023-07-28T17:58:07.295669Z","shell.execute_reply":"2023-07-28T18:10:06.144661Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 学習済みモデルを用いて分類予測","metadata":{}},{"cell_type":"code","source":"p = re.compile(r'\\d+')\n\nimcount = 1\n\ntest_list = glob.glob('test/*.jpg')\ntest_list = sorted(test_list, key = lambda s: int(p.search(s).group()))\n# print(test_list)\n\n#モデルの読み込み\nmodel = models.load_model('dog_vs_cat.h5')\n\n#csvに書き込み\nwith open('/kaggle/working/sample_submission.csv', 'w') as f:\n    writer = csv.writer(f, lineterminator='\\n')\n    writer.writerow(['id', 'label'])\n    \n    print(\" ~ テスト開始 ~ \")\n    for i in tqdm.tqdm(range(len(test_list))):\n        img = image.load_img(test_list[i], target_size = (224, 224))\n        x = image.img_to_array(img)\n        x = np.expand_dims(x, axis=0)\n        x = x / 255.0   \n        result_predict = model.predict(x,verbose=0)\n        writer.writerow([imcount, result_predict[0][0]])\n        imcount += 1\n        \n#     for i in test_list:\n#         img = image.load_img(i, target_size = (224, 224))\n#         x = image.img_to_array(img)\n#         x = np.expand_dims(x, axis=0)\n#         x = x / 255.0\n        \n#         result_predict = model.predict(x)\n#         print(result_predict)\n#         writer.writerow([imcount, result_predict[0][0]])\n#         imcount += 1","metadata":{"execution":{"iopub.status.busy":"2023-07-28T18:12:53.732845Z","iopub.execute_input":"2023-07-28T18:12:53.733220Z","iopub.status.idle":"2023-07-28T18:25:39.337835Z","shell.execute_reply.started":"2023-07-28T18:12:53.733187Z","shell.execute_reply":"2023-07-28T18:25:39.336834Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"\n","metadata":{}}]}