{"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":"2022-07-15T11:50:12.431021Z","iopub.execute_input":"2022-07-15T11:50:12.431556Z","iopub.status.idle":"2022-07-15T11:50:12.461384Z","shell.execute_reply.started":"2022-07-15T11:50:12.431513Z","shell.execute_reply":"2022-07-15T11:50:12.460321Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport numpy as np \nimport glob\nimport shutil","metadata":{"execution":{"iopub.status.busy":"2022-07-15T11:50:12.463299Z","iopub.execute_input":"2022-07-15T11:50:12.464426Z","iopub.status.idle":"2022-07-15T11:50:12.471259Z","shell.execute_reply.started":"2022-07-15T11:50:12.464390Z","shell.execute_reply":"2022-07-15T11:50:12.469322Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# /kaggle/inputのzipファイルを/kaggle/workingに解凍\nimport zipfile\nwith zipfile.ZipFile('/kaggle/input/dogs-vs-cats-redux-kernels-edition/train.zip') as existing_zip:\n    existing_zip.extractall()\nwith zipfile.ZipFile('/kaggle/input/dogs-vs-cats-redux-kernels-edition/test.zip') as existing_zip:\n    existing_zip.extractall()\n\n\n# '/kaggle/working'に入っているファイルを確認\nfiles = glob.glob('/kaggle/working/*')\nfor file in files:\n    print(file)","metadata":{"execution":{"iopub.status.busy":"2022-07-15T11:50:12.489863Z","iopub.execute_input":"2022-07-15T11:50:12.490212Z","iopub.status.idle":"2022-07-15T11:50:24.720203Z","shell.execute_reply.started":"2022-07-15T11:50:12.490179Z","shell.execute_reply":"2022-07-15T11:50:24.718340Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# /kaggle/working/trainの中身を確認\nfiles = sorted(glob.glob('/kaggle/working/train/*'))# 訓練画像フォルダ\nprint('訓練用フォルダの中には', len(files), '個の訓練用の画像ファイルが入っています．')\n# for file in files:\n#     print(file)\n\n\n# /kaggle/working/testの中身を確認\nfiles = sorted(glob.glob('/kaggle/working/test/*'))# テスト画像フォルダ\nprint('テスト用フォルダの中には', len(files), '個のテスト用の画像ファイルが入っています．')\n# for file in files:\n#     print(file)\n\n\n# 学習させる猫と犬の画像数を確認\nn_cat = 0# 猫の画像数\nn_dog = 0# 犬の画像数\nfiles = glob.glob('/kaggle/working/train/*')# 訓練用フォルダ\nfor file in files:\n    if 'cat' in file:\n        n_cat += 1\n    else:\n        n_dog += 1\nprint('猫の画像数：', n_cat, '枚')\nprint('犬の画像数：', n_dog, '枚')","metadata":{"execution":{"iopub.status.busy":"2022-07-15T11:50:24.722809Z","iopub.execute_input":"2022-07-15T11:50:24.723284Z","iopub.status.idle":"2022-07-15T11:50:25.013988Z","shell.execute_reply.started":"2022-07-15T11:50:24.723213Z","shell.execute_reply":"2022-07-15T11:50:25.012985Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 犬・猫の画像を，それぞれ保存するフォルダのパスを定義\ndog_dir = '/kaggle/working/train/dog'\ncat_dir = '/kaggle/working/train/cat'\n\n# # フォルダの作成\nif(os.path.exists(dog_dir) == False):\n    os.mkdir(dog_dir)\nif(os.path.exists(cat_dir) == False):\n    os.mkdir(cat_dir)\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)","metadata":{"execution":{"iopub.status.busy":"2022-07-15T11:50:25.015426Z","iopub.execute_input":"2022-07-15T11:50:25.015804Z","iopub.status.idle":"2022-07-15T11:50:26.257604Z","shell.execute_reply.started":"2022-07-15T11:50:25.015757Z","shell.execute_reply":"2022-07-15T11:50:26.256548Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\n#バリデーションフォルダのパスを定義\nval = '/kaggle/working/val'\nval_dog = '/kaggle/working/val/dog'\nval_cat = '/kaggle/working/val/cat'\n\n# フォルダの作成\nif(os.path.exists(val) == False):\n    os.mkdir(val)\n    os.mkdir(val_dog)\n    os.mkdir(val_cat)\n\n# 訓練画像の一部を，バリデーションフォルダに移動する(犬)\ndog_files = glob.glob('/kaggle/working/train/dog/*.jpg')\ndog_train, dog_val = train_test_split(dog_files, test_size=0.2, random_state=42)\n# 画像の一部をバリデーションフォルダに移動する\nfor file in dog_val:\n    file_name = os.path.basename(file)\n    shutil.move(file,'/kaggle/working/val/dog/' + file_name)\n    \n# 訓練画像の一部を，バリデーションフォルダに移動する(猫)\ncat_files = glob.glob('/kaggle/working/train/cat/*.jpg')\ncat_train, cat_val = train_test_split(cat_files, test_size=0.2, random_state=42)\n# 画像の一部をバリデーションフォルダに移動する\nfor file in cat_val:\n    file_name = os.path.basename(file)\n    shutil.move(file,'/kaggle/working/val/cat/' + file_name)","metadata":{"execution":{"iopub.status.busy":"2022-07-15T11:50:26.260370Z","iopub.execute_input":"2022-07-15T11:50:26.260752Z","iopub.status.idle":"2022-07-15T11:50:26.621824Z","shell.execute_reply.started":"2022-07-15T11:50:26.260716Z","shell.execute_reply":"2022-07-15T11:50:26.620853Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow.keras.layers as layers\nimport tensorflow.keras.optimizers as optimizers\nfrom keras import layers, models\n\n\nmodel = models.Sequential()\n\nmodel.add(layers.Conv2D(32,(3,3),activation='relu',input_shape=(150,150,3)))\nmodel.add(layers.Conv2D(32,(3,3),activation='relu'))\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.Flatten())\nmodel.add(layers.Dense(512,activation='relu'))\nmodel.add(layers.Dense(256,activation='relu'))\nmodel.add(layers.Dense(1,activation='sigmoid'))\n\nmodel.compile(loss='binary_crossentropy',optimizer=optimizers.RMSprop(learning_rate=1e-4),metrics=['acc'])\n\nmodel.summary()\n","metadata":{"execution":{"iopub.status.busy":"2022-07-15T11:50:26.623387Z","iopub.execute_input":"2022-07-15T11:50:26.624495Z","iopub.status.idle":"2022-07-15T11:50:26.705997Z","shell.execute_reply.started":"2022-07-15T11:50:26.624455Z","shell.execute_reply":"2022-07-15T11:50:26.704985Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.preprocessing.image import ImageDataGenerator\n\ntrain_data_dir = '/kaggle/working/train'\ntest_data_dir = '/kaggle/working/val'\n\n\n# 学習データのデータ拡張を設定\ntrain_datagen = ImageDataGenerator(rescale = 1.0 / 255)\n\n# 評価データのデータ拡張を設定\ntest_datagen = ImageDataGenerator(rescale = 1.0 / 255)\n\n# 学習データのジェネレータを生成\ntrain_generator = train_datagen.flow_from_directory(\n        train_data_dir,\n        target_size = (150, 150),\n        batch_size = 32,\n        class_mode = 'binary')\n\n# 評価データのジェネレータを生成\ntest_generator = test_datagen.flow_from_directory(\n        test_data_dir,\n        target_size = (150, 150),\n        batch_size = 32,\n        class_mode = 'binary')","metadata":{"execution":{"iopub.status.busy":"2022-07-15T11:50:26.707272Z","iopub.execute_input":"2022-07-15T11:50:26.708201Z","iopub.status.idle":"2022-07-15T11:50:27.377856Z","shell.execute_reply.started":"2022-07-15T11:50:26.708163Z","shell.execute_reply":"2022-07-15T11:50:27.376838Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"epoch = 10\n\n# ジェネレータを用いたモデルの学習\nhistory = model.fit_generator(\n    train_generator,\n    steps_per_epoch = 20000/32,\n    epochs=epoch,\n    validation_data = test_generator,\n    validation_steps=5000/32)\n\nmodel.save('/kaggle/working/dog_vs_cat.h5')","metadata":{"execution":{"iopub.status.busy":"2022-07-15T11:50:27.379184Z","iopub.execute_input":"2022-07-15T11:50:27.379566Z","iopub.status.idle":"2022-07-15T12:02:14.901689Z","shell.execute_reply.started":"2022-07-15T11:50:27.379529Z","shell.execute_reply":"2022-07-15T12:02:14.900702Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.preprocessing import image\nfrom keras.models import load_model\nimport re\nimport csv","metadata":{"execution":{"iopub.status.busy":"2022-07-15T12:03:39.233393Z","iopub.execute_input":"2022-07-15T12:03:39.233764Z","iopub.status.idle":"2022-07-15T12:03:39.240897Z","shell.execute_reply.started":"2022-07-15T12:03:39.233732Z","shell.execute_reply":"2022-07-15T12:03:39.239960Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"p = re.compile(r'\\d+')\nimcount = 1\ntest_list = glob.glob('/kaggle/working/test/*.jpg')\ntest_list = sorted(test_list,key=lambda s: int(p.search(s).group()))\n\n#モデルの読み込み\nmodel = load_model('/kaggle/working/dog_vs_cat.h5')\n\n\n#csv書き込み\nwith open('../working/submission.csv', 'w') as f:\n  writer= csv.writer(f, lineterminator='\\n')\n  writer.writerow(['id', 'label'])\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    result_predict = model.predict(x)\n    writer.writerow([imcount, result_predict[0][0]])\n    imcount += 1\n    ","metadata":{"execution":{"iopub.status.busy":"2022-07-15T12:03:41.230511Z","iopub.execute_input":"2022-07-15T12:03:41.231182Z","iopub.status.idle":"2022-07-15T12:12:31.535872Z","shell.execute_reply.started":"2022-07-15T12:03:41.231142Z","shell.execute_reply":"2022-07-15T12:12:31.534921Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cat ../working/submission.csv","metadata":{"execution":{"iopub.status.busy":"2022-07-15T12:13:26.831908Z","iopub.execute_input":"2022-07-15T12:13:26.832512Z","iopub.status.idle":"2022-07-15T12:13:27.583590Z","shell.execute_reply.started":"2022-07-15T12:13:26.832476Z","shell.execute_reply":"2022-07-15T12:13:27.582476Z"},"trusted":true},"execution_count":null,"outputs":[]}]}