{"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\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 5GB 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","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"##　ZIP解凍"},{"metadata":{"trusted":true},"cell_type":"code","source":"import zipfile\n\nwith zipfile.ZipFile('/kaggle/input/dogs-vs-cats-redux-kernels-edition/train.zip') as existing_zip:\n    existing_zip.extractall()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(os.listdir('./train'))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import zipfile\n\nwith zipfile.ZipFile('/kaggle/input/dogs-vs-cats-redux-kernels-edition/test.zip') as existing_zip:\n    existing_zip.extractall()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(os.listdir('./test'))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(os.listdir('./'))","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### パッケージ群のインポート"},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"import tensorflow\nfrom tensorflow import keras\n%matplotlib inline\nimport matplotlib.pyplot as plt","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"%matplotlib inline\nimport matplotlib.pyplot as plt\n\nimport os, cv2, random\nimport numpy as np\nimport pandas as pd","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from matplotlib import ticker\nimport seaborn as sns","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### トレーニング・テストデータの作成"},{"metadata":{"trusted":true},"cell_type":"code","source":"TRAIN_DIR = \"./train/\"\nTEST_DIR = \"./test/\"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"ROWS = 64\nCOLS = 64\nCHANNELS = 3","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_images = [TRAIN_DIR+i for i in os.listdir(TRAIN_DIR)]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_images","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"len(train_images)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_images[1000]","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### リストの内包表記"},{"metadata":{"trusted":true},"cell_type":"code","source":"train_dogs =   [TRAIN_DIR+i for i in os.listdir(TRAIN_DIR) if 'dog' in i]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_cats =   [TRAIN_DIR+i for i in os.listdir(TRAIN_DIR) if 'cat' in i]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"len(train_dogs)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"len(train_cats)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_images =  [TEST_DIR+i for i in os.listdir(TEST_DIR)]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"len(test_images)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### リストの確認"},{"metadata":{"trusted":true},"cell_type":"code","source":"train_images = train_dogs[:12500] + train_cats[:12500]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"len(train_images)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"random.shuffle(train_images)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_images=test_images[:25000]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"len(test_images)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### 画像ファイルを多次元配列に変換"},{"metadata":{"trusted":true},"cell_type":"code","source":"def read_image(file_path):\n    img = cv2.imread(file_path, cv2.IMREAD_COLOR)#カラー画像読込\n    return cv2.resize(img, (ROWS, COLS), interpolation=cv2.INTER_CUBIC)#リサイズ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#画像ファイル群を渡してndArray配列に替える\ndef prep_data(images):\n    count = len(images)\n    data = np.ndarray((count, CHANNELS, ROWS, COLS), dtype=np.uint8)#dtype 濃度\n    \n    for i, image_file in enumerate(images):\n        image = read_image(image_file)\n        data[i] = image.T #転置行列（縦横逆）\n        if i%250 == 0: print('Processed {} of {}'.format(i, count))\n            \n    return data","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train = prep_data(train_images)\ntest = prep_data(test_images)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test.shape","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### ラベル一覧をリスト型変数に格納\n##各ファイルが、Dog、Cat のどちらかを分ける（dog→1, cat→0）"},{"metadata":{"trusted":true},"cell_type":"code","source":"labels = []\nfor i in train_images:#train_imagesフォルダから　i　を1づつ取り出し、「i」に入れる\n    if 'dog.' in i:\n        labels.append(1)\n    else:\n        labels.append(0)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"labels[0:25000]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_images[0:25000]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sns.countplot(labels)\nplt.title('Cats and Dogs')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### 画像データ抜取確認"},{"metadata":{"trusted":true},"cell_type":"code","source":"def show_cats_and_dogs(idx):\n    cat = read_image(train_cats[idx])\n    dog = read_image(train_dogs[idx])\n    pair = np.concatenate((cat, dog), axis=1)\n    plt.figure(figsize=(10,5))\n    plt.imshow(pair)\n    plt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for idx in range(100,115):\n    show_cats_and_dogs(idx)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### model定義"},{"metadata":{"trusted":true},"cell_type":"code","source":"from keras.models import Sequential\nfrom keras.layers import Input, Dropout, Flatten, Conv2D, MaxPooling2D, Dense, Activation\nfrom keras.optimizers import RMSprop\nfrom keras.callbacks import ModelCheckpoint, Callback, EarlyStopping\nfrom keras.utils import np_utils","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import keras.backend.tensorflow_backend as tfback","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import tensorflow as tf","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def _get_available_gpus():  \n\n    if tfback._LOCAL_DEVICES is None:  \n        devices = tf.config.list_logical_devices()  \n        tfback._LOCAL_DEVICES = [x.name for x in devices]  \n    return [x for x in tfback._LOCAL_DEVICES if 'device:gpu' in x.lower()]\n\n\ntfback._get_available_gpus = _get_available_gpus","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"optimizer = RMSprop(lr=1e-4)\nobjective = 'binary_crossentropy'\n\ndef catdog():\n    model = Sequential()\n    \n    model.add(Conv2D(32, (3, 3), padding='same', input_shape=(3, ROWS, COLS), activation='relu'))\n    model.add(Conv2D(32, (3, 3), padding='same', activation='relu'))\n    model.add(MaxPooling2D(data_format=\"channels_first\", pool_size=(2, 2)))\n    \n    model.add(Conv2D(64, (3, 3), padding='same', activation='relu'))\n    model.add(Conv2D(64, (3, 3), padding='same', activation='relu'))\n    model.add(MaxPooling2D(data_format=\"channels_first\", pool_size=(2, 2)))\n    \n    model.add(Conv2D(128, (3, 3), padding='same', activation='relu'))\n    model.add(Conv2D(128, (3, 3), padding='same', activation='relu'))\n    model.add(MaxPooling2D(data_format=\"channels_first\", pool_size=(2, 2)))\n    \n    model.add(Conv2D(256, (3, 3), padding='same', activation='relu'))\n    model.add(Conv2D(256, (3, 3), padding='same', activation='relu'))\n#     model.add(Convolution2D(256, 3, 3, border_mode='same', activation='relu'))\n    model.add(MaxPooling2D(data_format=\"channels_first\", pool_size=(2, 2)))\n    \n    model.add(Flatten())\n    model.add(Dense(256, activation='relu'))\n    model.add(Dropout(0.5))\n    \n    model.add(Dense(256, activation='relu'))\n    model.add(Dropout(0.5))\n    \n    model.add(Dense(1))\n    model.add(Activation('sigmoid'))\n\n    model.compile(loss=objective, optimizer=optimizer, metrics=['accuracy'])\n    return model\n\nmodel = catdog()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### トレーニング実行"},{"metadata":{"trusted":true},"cell_type":"code","source":"nb_epoch=10\nbatch_size=250\n\nclass LossHistory(Callback):\n    def on_train_begin(self, logs={}):\n        self.losses = []\n        self.val_losses = []\n        \n    def on_epoch_end(self, batch, logs={}):\n        self.losses.append(logs.get('loss'))\n        self.val_losses.append(logs.get('val_loss'))\n\n# monitor: 監視対象\n# patience: 訓練が停止し，値が改善しなくなってからのエポック数．\nearly_stopping = EarlyStopping(monitor='val_loss', patience=3, verbose=1, mode='auto')\n\ndef run_catdog():\n    \n    history = LossHistory()\n    model.fit(train, labels, batch_size=batch_size, epochs=nb_epoch,\n              validation_split=0.25, verbose=0, shuffle=True, callbacks=[history, early_stopping])\n    \n\n    predictions = model.predict(test, verbose=0)\n    return predictions, history\n\npredictions, history = run_catdog()\n# タプルで受け取る\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### ロスをプロット"},{"metadata":{"trusted":true},"cell_type":"code","source":"loss = history.losses\nval_loss = history.val_losses\n\nplt.xlabel('Epochs')\nplt.ylabel('Loss')\n\nplt.title('CatdogNet Loss Trend')\nplt.plot(loss, 'blue', label='Training Loss')\nplt.plot(val_loss, 'green', label='Validation Loss')\nplt.xticks(range(0,nb_epoch)[0::2])\nplt.legend()\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(len(predictions))","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### 推定結果を抜取可視化"},{"metadata":{"trusted":true},"cell_type":"code","source":"for i in range(0,10):\n    if predictions[i, 0] >= 0.5:\n        print('I am {:.2%} sure this is a Dog'.format(predictions[i][0]))\n    else:\n        print('I am {:.2%} sure this is a Cat'.format(1-predictions[i][0]))\n        \n    plt.imshow(test[i].T)\n    plt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_pred=model.predict(test, verbose=0)\nprint(test_pred)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### 提出ファイル作成"},{"metadata":{"trusted":true},"cell_type":"code","source":"test_pred = (test_pred > 0.5).astype(int)\ntest_pred[0:]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"len(test_pred)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(os.listdir('/kaggle/input/dogs-vs-cats-redux-kernels-edition/'))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sample=pd.read_csv('/kaggle/input/dogs-vs-cats-redux-kernels-edition/sample_submission.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submit11=sample[[\"id\"]].copy()\nsubmit11['label']=test_pred","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submit11.to_csv(path_or_buf=\"submit11.csv\", sep=\",\", index=False,header=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submit11","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.4","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":4}