{"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# 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 numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\nimport os\n\nfrom tensorflow import keras\nfrom tensorflow.keras import models, optimizers, layers\nimport tensorflow.keras.layers as layers\nfrom tensorflow.keras.preprocessing import image\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\n\nimport re\nimport csv\nimport glob\n\nimport cv2\n\nimport shutil","metadata":{"execution":{"iopub.status.busy":"2022-07-11T15:44:48.973657Z","iopub.execute_input":"2022-07-11T15:44:48.974322Z","iopub.status.idle":"2022-07-11T15:44:54.408126Z","shell.execute_reply.started":"2022-07-11T15:44:48.974219Z","shell.execute_reply":"2022-07-11T15:44:54.407165Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 使用データについて","metadata":{}},{"cell_type":"code","source":"#学習データ・テストデータを使用ディレクトリで解凍(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#/kaggle/inputのcsvファイルをkaggle/workingにコピー\nshutil.copyfile('/kaggle/input/dogs-vs-cats-redux-kernels-edition/sample_submission.csv','/kaggle/working/submission.csv')\n\n#使用データの確認\n#/kaggle/inputに入っているファイルを確認\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n        \n#/kaggle/workingに入っているファイルを確認\nfiles = glob.glob('/kaggle/working/*')\nfor file in files:\n    print(file)","metadata":{"execution":{"iopub.status.busy":"2022-07-11T15:44:54.409906Z","iopub.execute_input":"2022-07-11T15:44:54.410537Z","iopub.status.idle":"2022-07-11T15:45:10.721863Z","shell.execute_reply.started":"2022-07-11T15:44:54.410498Z","shell.execute_reply":"2022-07-11T15:45:10.720962Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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.2,\n                zoom_range = 0.2,\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 = (150,150),\n                    batch_size = batch_size,\n                    class_mode = 'binary',\n                    subset = \"training\")\n\ntest_generator = train_datagen.flow_from_directory(\n                    train_dir,\n                    target_size = (150, 150),\n                    batch_size = batch_size,\n                    class_mode = 'binary',\n                    subset = \"validation\")","metadata":{"execution":{"iopub.status.busy":"2022-07-11T15:45:10.726761Z","iopub.execute_input":"2022-07-11T15:45:10.729344Z","iopub.status.idle":"2022-07-11T15:45:12.710564Z","shell.execute_reply.started":"2022-07-11T15:45:10.729296Z","shell.execute_reply":"2022-07-11T15:45:12.709542Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 学習モデルの作成","metadata":{}},{"cell_type":"code","source":"model = models.Sequential()\nmodel.add(layers.Conv2D(32, (3, 3), activation = 'relu', input_shape = (150, 150,3)))\nmodel.add(layers.MaxPooling2D((2, 2)))\n\nmodel.add(layers.Conv2D(64, (3, 3), activation = 'relu'))\nmodel.add(layers.MaxPooling2D((2, 2)))\n\nmodel.add(layers.Conv2D(128, (3, 3), activation = 'relu'))\nmodel.add(layers.MaxPooling2D((2, 2)))\n\nmodel.add(layers.Conv2D(256, (3, 3), activation = 'relu'))\nmodel.add(layers.MaxPooling2D((2, 2)))\n\nmodel.add(layers.Flatten())\n\nmodel.add(layers.Dense(512, activation = 'relu'))\nmodel.add(layers.Dense(1, activation = 'sigmoid'))\n\nmodel.compile(loss = 'binary_crossentropy',\n             optimizer = optimizers.Adam(learning_rate = 1e-4),\n             metrics = ['acc'])\n\n#作成したモデルを表示して確認\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2022-07-11T15:45:12.712902Z","iopub.execute_input":"2022-07-11T15:45:12.713366Z","iopub.status.idle":"2022-07-11T15:45:15.665420Z","shell.execute_reply.started":"2022-07-11T15:45:12.713327Z","shell.execute_reply":"2022-07-11T15:45:15.664479Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 学習","metadata":{}},{"cell_type":"code","source":"history = model.fit_generator(\n            train_generator,\n            steps_per_epoch = 20000 / batch_size,\n            epochs = 20,\n            validation_data = test_generator,\n            validation_steps = 5000 / batch_size\n            )\n\nmodel.save('dog_vs_cat.h5')\n","metadata":{"execution":{"iopub.status.busy":"2022-07-11T15:45:15.666866Z","iopub.execute_input":"2022-07-11T15:45:15.667664Z","iopub.status.idle":"2022-07-11T16:44:19.022127Z","shell.execute_reply.started":"2022-07-11T15:45:15.667625Z","shell.execute_reply":"2022-07-11T16:44:19.021147Z"},"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\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    for i in test_list:\n        img = image.load_img(i, target_size = (150, 150))\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        writer.writerow([imcount, result_predict[0][0]])\n        imcount += 1","metadata":{"execution":{"iopub.status.busy":"2022-07-11T16:44:19.023528Z","iopub.execute_input":"2022-07-11T16:44:19.023950Z","iopub.status.idle":"2022-07-11T16:52:12.154911Z","shell.execute_reply.started":"2022-07-11T16:44:19.023911Z","shell.execute_reply":"2022-07-11T16:52:12.153775Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}