{"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":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for dirname, _, filenames in os.walk('./'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# File I/O\nimport csv\nimport subprocess\nimport shutil\nimport os\nfrom glob import glob\nfrom datetime import datetime\nfrom PIL import Image\n\n# Data processing\nimport pandas as pd\nimport numpy as np\nfrom sklearn.model_selection import StratifiedKFold\n\n# Image processing\nimport cv2\nfrom scipy.ndimage import rotate\nimport scipy.misc\n\n# Graph\nimport matplotlib.pyplot as plt\n\nfrom sklearn.model_selection import train_test_split\n\nimport tensorflow as tf\nfrom keras import Model\nfrom keras import optimizers\nfrom keras.models import Sequential\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.layers.convolutional import Conv2D\nfrom keras.layers.convolutional import MaxPooling2D\nfrom keras.layers import GlobalAveragePooling2D\nfrom keras.layers.core import Activation\nfrom keras.layers.core import Flatten, Dropout\nfrom keras.layers.core import Dense\nfrom keras.utils.np_utils import to_categorical\nfrom keras.utils import np_utils\nfrom keras.optimizers import Adam\nfrom keras.optimizers import Adagrad\nfrom keras.callbacks import TensorBoard\nfrom keras.callbacks import ModelCheckpoint\nfrom keras.callbacks import ReduceLROnPlateau\nfrom keras.callbacks import EarlyStopping\n# EfficientNet\nfrom tensorflow.keras.applications import EfficientNetB0\n\nfrom tensorflow.keras.optimizers import RMSprop\n\nfrom IPython.display import display\nimport ipywidgets as widgets\nfrom keras.optimizers import SGD\n","metadata":{"execution":{"iopub.status.busy":"2021-07-04T17:24:22.771962Z","iopub.execute_input":"2021-07-04T17:24:22.772288Z","iopub.status.idle":"2021-07-04T17:24:22.781361Z","shell.execute_reply.started":"2021-07-04T17:24:22.77226Z","shell.execute_reply":"2021-07-04T17:24:22.780562Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!unzip -q '../input/dogs-vs-cats-redux-kernels-edition/train.zip' -d '/kaggle/working/' # ->'/kaggle/working/train'\n!unzip -q '../input/dogs-vs-cats-redux-kernels-edition/test.zip' -d '/kaggle/working/test/' # ->'/kaggle/working/test/test1'\n!unzip -q '../input/dogs-vs-cats-redux-kernels-edition/testing.zip' -d '/kaggle/working/testing/' # ->'/kaggle/working/testing'","metadata":{"execution":{"iopub.status.busy":"2021-07-04T16:22:38.047476Z","iopub.execute_input":"2021-07-04T16:22:38.04799Z","iopub.status.idle":"2021-07-04T16:22:56.09101Z","shell.execute_reply.started":"2021-07-04T16:22:38.047945Z","shell.execute_reply":"2021-07-04T16:22:56.089997Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"shutil.move('/kaggle/working/train/', '/kaggle/working/train_raw/all_pics')\n","metadata":{"execution":{"iopub.status.busy":"2021-07-04T16:22:59.419484Z","iopub.execute_input":"2021-07-04T16:22:59.41984Z","iopub.status.idle":"2021-07-04T16:22:59.427767Z","shell.execute_reply.started":"2021-07-04T16:22:59.419793Z","shell.execute_reply":"2021-07-04T16:22:59.426498Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"root_path = '/kaggle/working'\n\n\n# Path of Unzipping data stored (root)\ntrain_raw_root_path = '/kaggle/working/train_raw/'\n\n\n# Path of Unzipping data stored \ntrain_raw_path = '/kaggle/working/train_raw/all_pics'\n\n# Path for training\ntrain_root_path = '/kaggle/working/train_root'\ntest_path = '/kaggle/working/test'\nsubmit_file = 'submission.csv'\n\n# Path for Imagegenerator\ntrain_dir = os.path.join(train_root_path, 'train')\nvalid_dir = os.path.join(train_root_path, 'valid')\n\nprint(train_dir)\nprint(valid_dir)","metadata":{"execution":{"iopub.status.busy":"2021-07-04T16:23:01.7488Z","iopub.execute_input":"2021-07-04T16:23:01.749147Z","iopub.status.idle":"2021-07-04T16:23:01.755124Z","shell.execute_reply.started":"2021-07-04T16:23:01.749117Z","shell.execute_reply":"2021-07-04T16:23:01.754022Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_img_list = [os.path.basename(f) for f in glob(f'{train_raw_path}/*.jpg')]\ntest_img_list = [os.path.basename(f) for f in glob(f'{test_path}/test/*.jpg')]\n","metadata":{"execution":{"iopub.status.busy":"2021-07-04T16:23:04.654385Z","iopub.execute_input":"2021-07-04T16:23:04.654726Z","iopub.status.idle":"2021-07-04T16:23:04.812645Z","shell.execute_reply.started":"2021-07-04T16:23:04.654695Z","shell.execute_reply":"2021-07-04T16:23:04.811883Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# target labels list\ntarget_labels = ['cat', 'dog']\n\n# directories in the train_root path\ndirs = ['train', 'valid']\n\n# make dirctories for data store\nfor d in dirs:\n    for label in target_labels:\n        os.makedirs(os.path.join(train_root_path, d, label), exist_ok=True)","metadata":{"execution":{"iopub.status.busy":"2021-07-04T16:23:06.810574Z","iopub.execute_input":"2021-07-04T16:23:06.810918Z","iopub.status.idle":"2021-07-04T16:23:06.815716Z","shell.execute_reply.started":"2021-07-04T16:23:06.810888Z","shell.execute_reply":"2021-07-04T16:23:06.814911Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Initializing instance\nimg_cat_list = []\nimg_dog_list = []\n\n# make dog and cat file name list\nfor f in data_img_list:\n    label = f.split('.')[0]\n    if label == target_labels[0]:\n        img_cat_list.append(f)\n    elif label == target_labels[1]:\n        img_dog_list.append(f)\n    else:\n        print('abnormal file name is found', f)\n\n# Split data to for train and for valid\ntrain_list_cat, val_list_cat, train_list_dog, val_list_dog = train_test_split(img_cat_list, img_dog_list, \n                                                                  test_size=0.3,\n                                                                 random_state=46)\n# directories list to join to train_root path\ncopy_to_dirs = ['train/cat', 'valid/cat', 'train/dog', 'valid/dog']\nimg_lists = [train_list_cat, val_list_cat, train_list_dog, val_list_dog] \n\n# Copy to each directories\nfor to_dir in copy_to_dirs:\n    for f in img_lists[copy_to_dirs.index(to_dir)]:\n        shutil.copyfile(os.path.join(train_raw_path,f), os.path.join(train_root_path, to_dir,f))","metadata":{"execution":{"iopub.status.busy":"2021-07-04T16:23:09.84464Z","iopub.execute_input":"2021-07-04T16:23:09.844982Z","iopub.status.idle":"2021-07-04T16:23:13.899439Z","shell.execute_reply.started":"2021-07-04T16:23:09.844952Z","shell.execute_reply":"2021-07-04T16:23:13.89855Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Check file quantity \nprint('train image quantity:', len(data_img_list))\nprint('cat:', len(img_cat_list))\nprint('dog:', len(img_dog_list))\nprint()\nprint('prediction target test image quantity:', len(test_img_list))","metadata":{"execution":{"iopub.status.busy":"2021-07-04T16:23:13.900766Z","iopub.execute_input":"2021-07-04T16:23:13.901124Z","iopub.status.idle":"2021-07-04T16:23:13.910897Z","shell.execute_reply.started":"2021-07-04T16:23:13.901085Z","shell.execute_reply":"2021-07-04T16:23:13.909877Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Check file number\nfor d in dirs:\n    for label in target_labels:\n        print(os.path.join(train_root_path, d, label))\n        print(len([file for file in os.listdir(os.path.join(train_root_path, d, label))]))","metadata":{"execution":{"iopub.status.busy":"2021-07-04T16:23:15.53343Z","iopub.execute_input":"2021-07-04T16:23:15.533755Z","iopub.status.idle":"2021-07-04T16:23:15.555199Z","shell.execute_reply.started":"2021-07-04T16:23:15.533727Z","shell.execute_reply":"2021-07-04T16:23:15.554249Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Check data directry tree structure\n!tree -d '/kaggle/working/'","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.layers import Dense, GlobalAveragePooling2D,Input\nfrom keras.optimizers import SGD\nfrom keras.models import Model\nfrom keras.preprocessing.image import ImageDataGenerator\nimport matplotlib.pyplot as plt\nfrom keras.callbacks import ModelCheckpoint\nfrom keras.optimizers import RMSprop\n# from keras.applications.inception_v3 import InceptionV3\n# from keras.applications.vgg16 import VGG16\nfrom keras.applications.vgg16 import VGG19\n\nepochs = 10\nvalidation_epock = 29\ntrain_epock = 68\nn_categories = 2\nbatch_size = 256\ntrain_dir = '/kaggle/working/train_root/train'\nvalidation_dir = '/kaggle/working/train_root/valid'\nfile_name = 'VGG19_eeg_fine'\nimage_size = 224\n\nbase_model = VGG19(weights = 'imagenet', include_top=False,\n                 input_tensor = Input(shape=(image_size, image_size, 3)))\n\n#add new layers instead of FC networks\nx = base_model.output\nx = GlobalAveragePooling2D()(x)\nx = Dense(512, activation = 'relu')(x)\nprediction = Dense(n_categories, activation = 'softmax')(x)\nmodel = Model(inputs = base_model.input, outputs=prediction)\n\n#fix weights before VGG16 14layers\nfor layer in base_model.layers[:17]:\n    layer.trainable = False\n\n\nmodel.compile(optimizer = SGD(lr=0.0001,momentum=0.9),\n              loss = 'categorical_crossentropy',\n              metrics = ['accuracy'])\n\nmodel.summary()\n\ntrain_datagen = ImageDataGenerator(rescale=1.0 / 255,)\n\nvalidation_datagen = ImageDataGenerator(rescale=1.0 / 255)\n\nclasses = ['cat', 'dog']\n\ntrain_generator = train_datagen.flow_from_directory(\n    train_dir,\n    target_size=(image_size, image_size),\n    batch_size=batch_size,\n    classes=classes,\n    class_mode='categorical',\n    shuffle=True\n)\n\nvalidation_generator = validation_datagen.flow_from_directory(\n    validation_dir,\n    target_size=(image_size, image_size),\n    batch_size=batch_size,\n    classes=classes,\n    class_mode='categorical',\n    shuffle=True\n)\n\nmodel_checkpoint = ModelCheckpoint(\n    filepath=file_name+'.h5',\n    monitor='val_loss',\n    save_best_only=True,\n    verbose=0)\n\n\n# ・steps_per_epochとvalidation_stepsは、1エポックあたりのステップ数。すなわち、全サンプル数=バッチ数*ステップ数。(1426 = 32*step(45))\nhist = model.fit_generator(\n                           train_generator,\n                           epochs = epochs,\n                           verbose=1,\n                           validation_steps=validation_epock,#validationData/バッチサイズ\n                           validation_data=validation_generator,\n                           steps_per_epoch=train_epock,#trainData/バッチサイズ\n                          callbacks=[model_checkpoint]\n                          )\n\n\nacc = hist.history[\"accuracy\"]\nval_acc = hist.history[\"val_accuracy\"]\nloss = hist.history[\"loss\"]\nval_loss = hist.history[\"val_loss\"]\nepochs = range(1, len(acc) + 1)\n\n#plot accuracy\nplt.plot(epochs, acc, label = \"Training acc\" )\nplt.plot(epochs, val_acc, label = \"Validation acc\")\nplt.title(\"Training and Validation accuracy\")\nplt.legend()\nplt.show()\n\n#plot loss\nplt.plot(epochs, loss,  label = \"Training loss\" )\nplt.plot(epochs, val_loss, label = \"Validation loss\")\nplt.title(\"Training and Validation loss\")\nplt.legend()\nplt.show()\n# save weights\nmodel.save(file_name + '.h5')\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# load model\nfrom keras.models import load_model\nmodel = load_model('/kaggle/input/vgg16-fine/VGG16_eeg_fine.h5')\nmodel.summary()\n","metadata":{"execution":{"iopub.status.busy":"2021-07-04T17:39:06.708052Z","iopub.execute_input":"2021-07-04T17:39:06.708393Z","iopub.status.idle":"2021-07-04T17:39:09.797129Z","shell.execute_reply.started":"2021-07-04T17:39:06.708364Z","shell.execute_reply":"2021-07-04T17:39:09.796312Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_path = '/kaggle/input/testing'\nclasses = ['cat', 'dog']\n\n\nmodel.compile(optimizer = SGD(lr=0.0001,momentum=0.9),\n              loss = 'categorical_crossentropy',\n              metrics = ['accuracy'])\n\n\ntest_datagen = ImageDataGenerator(rescale=1./255)\ntest_generator = test_datagen.flow_from_directory(\n    test_path,\n    target_size=(224, 224),\n    batch_size=1,\n    class_mode=None,\n    shuffle=False)\n\nprint('test data quantity：', len(test_img_list))\n\ntest_pred_proba = model.predict(\n    test_generator,\n    steps=len(test_img_list),\n    verbose=0)\n\nprint(test_pred_proba)\ntest_pred_class = test_pred_proba\n","metadata":{"execution":{"iopub.status.busy":"2021-07-04T17:39:24.441148Z","iopub.execute_input":"2021-07-04T17:39:24.441478Z","iopub.status.idle":"2021-07-04T18:08:41.679921Z","shell.execute_reply.started":"2021-07-04T17:39:24.441447Z","shell.execute_reply":"2021-07-04T18:08:41.678981Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_pred_proba = model.predict(\n    test_generator,\n    steps=len(test_img_list),\n    verbose=0)\n\ntest_pred_class = test_pred_proba[:, 0]\n","metadata":{"execution":{"iopub.status.busy":"2021-07-04T16:54:08.245444Z","iopub.execute_input":"2021-07-04T16:54:08.245785Z","iopub.status.idle":"2021-07-04T16:54:37.901044Z","shell.execute_reply.started":"2021-07-04T16:54:08.245749Z","shell.execute_reply":"2021-07-04T16:54:37.898731Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(test_pred_class)\ntest_pred_class = test_pred_class[:, 0]\nprint(test_pred_class)\ntest_filename_list=[]\nfor f in test_img_list:\n    test_filename_list.append(f.split('.')[0])\n    ","metadata":{"execution":{"iopub.status.busy":"2021-07-04T18:09:46.972693Z","iopub.execute_input":"2021-07-04T18:09:46.973044Z","iopub.status.idle":"2021-07-04T18:09:46.999992Z","shell.execute_reply.started":"2021-07-04T18:09:46.97301Z","shell.execute_reply":"2021-07-04T18:09:46.998705Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result_id = pd.DataFrame(test_filename_list, columns=['id'])\nresult_label = pd.DataFrame(test_pred_class, columns=['label'])\nresult_for_submit = pd.concat([result_id, result_label], axis=1)\nresult_for_submit.head()","metadata":{"execution":{"iopub.status.busy":"2021-07-04T18:09:51.679316Z","iopub.execute_input":"2021-07-04T18:09:51.679638Z","iopub.status.idle":"2021-07-04T18:09:51.695663Z","shell.execute_reply.started":"2021-07-04T18:09:51.67961Z","shell.execute_reply":"2021-07-04T18:09:51.694907Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result_for_submit.to_csv('/kaggle/working/submit6.csv', sep=\",\", header=True, index=False)\n","metadata":{"execution":{"iopub.status.busy":"2021-07-04T18:09:59.977222Z","iopub.execute_input":"2021-07-04T18:09:59.977596Z","iopub.status.idle":"2021-07-04T18:10:00.025838Z","shell.execute_reply.started":"2021-07-04T18:09:59.977564Z","shell.execute_reply":"2021-07-04T18:10:00.025019Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}