{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"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 in \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 \"../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# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"# トレーニングデータのファイル名一覧を取得\nfilenames = os.listdir(\"/kaggle/input/dogs-vs-cats/train/train\")\ncategories = []  # 正解ラベルを格納するリスト\n\n# 取得したファイル名の数だけ処理を繰り返す\nfor filename in filenames:\n    # ファイル名から文字列を切り取る\n    category = filename.split('.')[0]\n    # 切り取った文字列にdogが含まれていれば'1'\n    # そうでなければ'0'をcategoriesに格納\n    whichCategories = '1' if category == 'dog' else '0'\n    categories.append(whichCategories)\n    \n# 教師データのDataFrameを作成\ndf = pd.DataFrame({\n    'filename': filenames,\n    'category': categories\n})","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(len(os.listdir(\"/kaggle/input/dogs-vs-cats/train/train\")))\nprint(len(os.listdir(\"/kaggle/input/dogs-vs-cats/test/test\")))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport random\nimport time\nfrom keras import layers\nfrom keras.layers import Dense, Dropout, GlobalMaxPooling2D, Flatten\nfrom keras.preprocessing.image import load_img\nfrom keras.applications import VGG16\nfrom keras.models import Model, Sequential\nfrom keras.optimizers import SGD\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.callbacks import ModelCheckpoint, EarlyStopping\nfrom sklearn.model_selection import train_test_split\n%matplotlib inline\n\nplt.figure(figsize=(12,12))\n\nTRAIN_DATA = \"/kaggle/input/dogs-vs-cats/train/train\"\n\n# 9枚の画像を表示してみる\nfor i in range(9):\n    plt.subplot(3, 3, i+1)\n    # データからランダムに画像を読み込む\n    image = load_img(TRAIN_DATA+'/'+random.choice(df.filename))\n    plt.imshow(image)\nplt.tight_layout()\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"image_size = 224\ninput_shape = (image_size, image_size, 3)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"epochs = 7  # エポック数\nbatch_size = 16  # バッチサイズ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"VGG16model = VGG16(input_shape=input_shape, include_top=False, weights=\"imagenet\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for layer in VGG16model.layers[:15]:\n    layer.trainable = False","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\nlast_layer = VGG16model.get_layer('block5_pool')\nlast_output = last_layer.output\n\n# 512ノードのプーリング層を追加\nnew_last_layers = GlobalMaxPooling2D()(last_output)\n# 512ノードの全結合層を追加、活性化関数はReLU\nnew_last_layers = Dense(512, activation='relu')(new_last_layers)\n# 過学習防止のためドロップアウトを追加、レートは0.5\nnew_last_layers = Dropout(0.5)(new_last_layers)\n# 最後に犬猫を示す2ノードの出力層を追加、活性化関数はシグモイド関数\nnew_last_layers = layers.Dense(2, activation='sigmoid')(new_last_layers)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# VGG16に定義したblockAの部分を組み込む\nmodel = Model(VGG16model.input, new_last_layers)\n# モデルのコンパイル\nmodel.compile(loss=\"categorical_crossentropy\", optimizer=SGD(lr=1e-4, momentum=0.9),metrics=['accuracy'])\n# サマリーの表示\n# 最後の出力層が2ノードになっていることを確認\nmodel.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df, validate_df = train_test_split(df, test_size=0.1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df = train_df.reset_index()\nvalidate_df = validate_df.reset_index()\ntotal_train = train_df.shape[0]\ntotal_validate = validate_df.shape[0]\n\n# 画像加工による画像の水増し定義\ntrain_datagen = ImageDataGenerator(\n  # ここでは回転や拡大、反転等、画像加工に係る\n  # 各種パラメータを設定している\n  rotation_range=15,\n  rescale=1./255,\n  shear_range=0.2,\n  zoom_range=0.2,\n  horizontal_flip=True,\n  fill_mode='nearest',\n  width_shift_range=0.1,\n  height_shift_range=0.1\n)\n# 学習データのジェネレータを作成\ntrain_generator = train_datagen.flow_from_dataframe(\n  train_df,\n  TRAIN_DATA,\n  x_col='filename',\n  y_col='category',\n  class_mode='categorical',\n  target_size=(image_size, image_size),\n  batch_size=batch_size\n)\n# 検証データのジェネレータ作成\nvalidation_datagen = ImageDataGenerator(rescale=1./255)\nvalidation_generator = validation_datagen.flow_from_dataframe(\n  validate_df,\n  TRAIN_DATA,\n  x_col='filename',\n  y_col='category',\n  class_mode='categorical',\n  target_size=(image_size, image_size),\n  batch_size=batch_size\n)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"history = model.fit_generator(\n  train_generator,  # 学習データのジェネレータ\n  epochs=epochs,  # エポック数\n  # 検証データのジェネレータ\n  validation_data=validation_generator,\n  validation_steps=total_validate//batch_size,\n  steps_per_epoch=total_train//batch_size)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"TEST_DATA = \"/kaggle/input/dogs-vs-cats/test/test\"\n\nfilenames = os.listdir(TEST_DATA)\nsample = random.choice(filenames)\nimg = load_img(TEST_DATA+'/'+sample, target_size=(224,224))\nplt.imshow(img)\nimg = np.asarray(img)\nimg = np.expand_dims(img, axis=0)\n\npredict = model.predict(img)  # 犬か猫か分類\ndog_vs_cat = np.argmax(predict, axis=1)\nprint('dog') if dog_vs_cat==1 else print('cat')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_filenames = os.listdir(TEST_DATA)\ntest_df = pd.DataFrame({\n    'filename' : test_filenames\n})\nnb_samples = test_df.shape[0]\n\n# テストデータのジェネレータを作成\ntest_gen = ImageDataGenerator(rescale=1./255)\ntest_generator = test_gen.flow_from_dataframe(\n  test_df,\n  TEST_DATA,\n  x_col='filename',\n  y_col=None,\n  class_mode=None,\n  batch_size=batch_size,\n  target_size=(image_size, image_size),\n  shuffle=False\n)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"predict = model.predict_generator(test_generator, steps=np.ceil(nb_samples/batch_size))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"dog_vs_cat = np.argmax(predict, axis=1)\nsubmission_df = test_df.copy()\nsubmission_df['id'] = submission_df['filename'].str.split('.').str[0]\nsubmission_df['label'] = dog_vs_cat\nsubmission_df.drop(['filename'], axis=1, inplace=True)\n# ファイルに出力\nsubmission_df.to_csv('submission.csv', index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"f = open('submission.csv')\nprint(f.read())\nf.close()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"img = load_img(TEST_DATA+'/5713.jpg')\nplt.imshow(img)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":1}