{"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-21T05:03:13.278472Z","iopub.execute_input":"2022-07-21T05:03:13.278773Z","iopub.status.idle":"2022-07-21T05:03:13.305747Z","shell.execute_reply.started":"2022-07-21T05:03:13.278695Z","shell.execute_reply":"2022-07-21T05:03:13.304826Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport glob\nimport shutil","metadata":{"execution":{"iopub.status.busy":"2022-07-21T05:03:13.307674Z","iopub.execute_input":"2022-07-21T05:03:13.308328Z","iopub.status.idle":"2022-07-21T05:03:13.313435Z","shell.execute_reply.started":"2022-07-21T05:03:13.308292Z","shell.execute_reply":"2022-07-21T05:03:13.312103Z"},"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-21T05:03:13.317493Z","iopub.execute_input":"2022-07-21T05:03:13.319065Z","iopub.status.idle":"2022-07-21T05:03:31.530402Z","shell.execute_reply.started":"2022-07-21T05:03:13.319029Z","shell.execute_reply":"2022-07-21T05:03:31.529362Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2\n\n# 犬・猫の画像を，それぞれ保存するフォルダのパスを定義\ntrain_dir = '/kaggle/working/train_edge'\ntest_dir = '/kaggle/working/test_edge'\ndog_dir = train_dir + '/dog'\ncat_dir = train_dir + '/cat'\n\n# # フォルダの作成\nif(os.path.exists(train_dir) == False):\n    os.mkdir(train_dir)\nif(os.path.exists(test_dir) == False):\n    os.mkdir(test_dir)\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    \ndef make_sharp_kernel(k: int):\n  return np.array([\n    [-k / 9, -k / 9, -k / 9],\n    [-k / 9, 1 + 8 * k / 9, k / 9],\n    [-k / 9, -k / 9, -k / 9]\n  ], np.float32)\n\nkernel = make_sharp_kernel(1)\n    \n# 犬・猫の画像を，それぞれのフォルダに移動する\nfiles = glob.glob('/kaggle/working/train/*.jpg')# 訓練用フォルダ内の全ファイルパスを取得\nfor file in files:\n    file_name = os.path.basename(file)\n    #画像読み込み\n    img = cv2.imread(file)\n    #エッジ強調\n    img = cv2.filter2D(img, -1, kernel).astype(\"uint8\")\n    \n    if 'cat' in file:\n        cv2.imwrite(cat_dir +'/' + file_name,img)\n    else:\n        cv2.imwrite(dog_dir +'/' + file_name,img)\n\nfiles = glob.glob('/kaggle/working/test/*.jpg')\n\nfor file in files:\n    file_name = os.path.basename(file)\n    #画像読み込み\n    img = cv2.imread(file)\n    #エッジ強調\n    img = cv2.filter2D(img,-1,kernel).astype(\"uint8\")\n    cv2.imwrite(test_dir + '/' + file_name,img)\n","metadata":{"execution":{"iopub.status.busy":"2022-07-21T05:06:35.071879Z","iopub.execute_input":"2022-07-21T05:06:35.072873Z","iopub.status.idle":"2022-07-21T05:09:48.571004Z","shell.execute_reply.started":"2022-07-21T05:06:35.072809Z","shell.execute_reply":"2022-07-21T05:09:48.570047Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ls","metadata":{"execution":{"iopub.status.busy":"2022-07-21T05:13:40.214890Z","iopub.execute_input":"2022-07-21T05:13:40.215587Z","iopub.status.idle":"2022-07-21T05:13:40.884114Z","shell.execute_reply.started":"2022-07-21T05:13:40.215551Z","shell.execute_reply":"2022-07-21T05:13:40.883069Z"},"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(dog_dir + '/*.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,val_dog + '/' + file_name)\n    \n# 訓練画像の一部を，バリデーションフォルダに移動する(猫)\ncat_files = glob.glob(cat_dir + '/*.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,val_cat +'/' + file_name)","metadata":{"execution":{"iopub.status.busy":"2022-07-21T05:13:45.531918Z","iopub.execute_input":"2022-07-21T05:13:45.532812Z","iopub.status.idle":"2022-07-21T05:13:46.134821Z","shell.execute_reply.started":"2022-07-21T05:13:45.532771Z","shell.execute_reply":"2022-07-21T05:13:46.133769Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ls","metadata":{"execution":{"iopub.status.busy":"2022-07-21T05:13:50.770766Z","iopub.execute_input":"2022-07-21T05:13:50.771492Z","iopub.status.idle":"2022-07-21T05:13:51.460267Z","shell.execute_reply.started":"2022-07-21T05:13:50.771455Z","shell.execute_reply":"2022-07-21T05:13:51.459158Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.applications.vgg16 import VGG16\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.models import Sequential , Model\nfrom keras.layers import Dense, Dropout, Flatten , Input\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras import optimizers\nfrom keras.callbacks import ModelCheckpoint\n\nimg_width, img_height = 224, 224\n\ntrain_data_dir = '/kaggle/working/train_edge'\ntest_data_dir = '/kaggle/working/val'\n\n# エポック数の設定\nepoch = 20\n\n# 分類クラス名の設定\nclasses = ['dog','cat']\nnb_classes = len(classes)\n\n# VGG16モデルのロード\nvgg_model = VGG16(\n        include_top = False,\n        weights = 'imagenet',\n        input_shape = (img_height, img_width, 3))\n\n\n# VGG16モデルの下に全結合層を追加\ntop_model = Sequential()\ntop_model.add(Flatten(input_shape= vgg_model.output_shape[1:]))\ntop_model.add(Dense(256, activation='relu'))\ntop_model.add(Dropout(0.5))\ntop_model.add(Dense(nb_classes, activation='softmax'))\n\nmodel = Model(vgg_model.input,top_model(vgg_model.output))\n\n# VGG16モデルの上位15層のパラメータを凍結\nfor layer in vgg_model.layers[:15]:\n    layer.trainable = False\n\nmodel.summary()\n\n# 最適化関数のパラメータ設定\nsgd = optimizers.SGD(lr = 0.001, momentum = 0.1, decay = 0.0)\n\n# 損失関数は交差エントロピー、最適化関数は確率的勾配法\nmodel.compile(\n        loss = 'categorical_crossentropy',\n        optimizer = sgd,\n        metrics = ['accuracy'])\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 = (img_height, img_width),\n        classes = classes,\n        batch_size = 32,\n        class_mode = 'categorical')\n\n# 評価データのジェネレータを生成\ntest_generator = test_datagen.flow_from_directory(\n        test_data_dir,\n        target_size = (img_height, img_width),\n        classes = classes,\n        batch_size = 32,\n        class_mode = 'categorical')\n\n# コールバック関数（モデルの保存）の設定\nmc_cb = ModelCheckpoint(\n        filepath = '/kaggle/working/dog_vs_cat.h5',\n        monitor = 'val_loss',\n        verbose = 1,\n        save_best_only = True)","metadata":{"execution":{"iopub.status.busy":"2022-07-21T05:13:55.883360Z","iopub.execute_input":"2022-07-21T05:13:55.883836Z","iopub.status.idle":"2022-07-21T05:14:05.918413Z","shell.execute_reply.started":"2022-07-21T05:13:55.883787Z","shell.execute_reply":"2022-07-21T05:14:05.917315Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# ジェネレータを用いたモデルの学習\nhistory = model.fit_generator(\n    train_generator,\n    epochs=epoch,\n    validation_data = test_generator,\n    callbacks = [mc_cb])","metadata":{"execution":{"iopub.status.busy":"2022-07-21T05:14:13.762674Z","iopub.execute_input":"2022-07-21T05:14:13.763137Z","iopub.status.idle":"2022-07-21T05:17:13.511436Z","shell.execute_reply.started":"2022-07-21T05:14:13.763097Z","shell.execute_reply":"2022-07-21T05:17:13.509771Z"},"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_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=(img_width, img_height))\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","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ls","metadata":{},"execution_count":null,"outputs":[]}]}