{"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","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import zipfile as zp\nos.makedirs('../data', exist_ok=True)\n\nbase_dir = '../input/dogs-vs-cats-redux-kernels-edition'\ntrain_dir = '../data/train'\ntest_dir = '../data/test'\n\ntrain_zip = zp.ZipFile(os.path.join(base_dir, 'train.zip'))\ntrain_zip.extractall('../data')\n\ntest_zip = zp.ZipFile(os.path.join(base_dir, 'test.zip'))\ntest_zip.extractall('../data')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.linear_model import LogisticRegression\nfrom sklearn.model_selection import GridSearchCV\nfrom sklearn.metrics import classification_report\nfrom keras.applications import VGG16\nfrom keras.applications import imagenet_utils\nfrom keras.preprocessing.image import img_to_array\nfrom keras.preprocessing.image import load_img\nfrom sklearn.preprocessing import LabelEncoder\nfrom sklearn.preprocessing import LabelBinarizer\nfrom sklearn.model_selection import train_test_split\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.optimizers import SGD\nfrom keras.optimizers import RMSprop\nfrom keras.applications import VGG16\nfrom keras.layers import Input\nfrom keras.models import Model\nfrom keras.layers.core import Dense\nfrom keras.layers.core import Dropout\nfrom keras.layers.core import Flatten\nimport numpy as np\nimport random\nimport os","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tqdm import tqdm\nimport cv2","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_path = list(os.listdir(train_dir))\nrandom.shuffle(image_path)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels = [p.split('.')[-3] for p in image_path]\n\nle = LabelEncoder()\nlabels = le.fit_transform(labels)\n\nlb = LabelBinarizer()\nlabels = lb.fit_transform(labels)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"help(enumerate)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"list_image = []\nfor imagePath in image_path:\n    path = os.path.join(train_dir,imagePath)\n    image = cv2.imread(path)\n    image = cv2.resize(image, (128,128))\n    image = np.array(image)\n    image = np.expand_dims(image, 0)\n    image = imagenet_utils.preprocess_input(image)\n    list_image.append(image)\n    \nlist_image = np.vstack(list_image)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load model VGG 16 của ImageNet dataset, include_top=False để bỏ phần Fully connected layer ở cuối.\nbaseModel = VGG16(weights='imagenet', include_top=False, input_tensor=Input(shape=(128, 128, 3)))\n\n# Xây thêm các layer\n# Lấy output của ConvNet trong VGG16\nfcHead = baseModel.output\n\n# Flatten trước khi dùng FCs\nfcHead = Flatten(name='flatten')(fcHead)\n\n# Thêm FC\nfcHead = Dense(256, activation='relu')(fcHead)\nfcHead = Dropout(0.5)(fcHead)\n\n# Output layer với softmax activation\nfcHead = Dense(2, activation='softmax')(fcHead)\n\n# Xây dựng model bằng việc nối ConvNet của VGG16 và fcHead\nmodel = model = Model(inputs=baseModel.input, outputs=fcHead)\n\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, y_train = list_image[:20000], labels[:20000]","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test, y_test = list_image[20000:25000], labels[20000:25000]","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# augmentation cho training data\naug_train = ImageDataGenerator(rescale=1./255, rotation_range=30, width_shift_range=0.1, height_shift_range=0.1, shear_range=0.2, \n                         zoom_range=0.2, horizontal_flip=True, fill_mode='nearest')\n# augementation cho test\naug_test= ImageDataGenerator(rescale=1./255)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for layer in baseModel.layers:\n    layer.trainable = False\n\nopt = RMSprop(0.001)\nmodel.compile(opt, 'categorical_crossentropy', ['accuracy'])\nnumOfEpoch = 25\nH = model.fit_generator(aug_train.flow(X_train, y_train, batch_size=32), \n                        steps_per_epoch=34,\n                        validation_data=(aug_test.flow(X_test, y_test, batch_size=32)),\n                        validation_steps=34,\n                        epochs=numOfEpoch)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for layer in baseModel.layers[15:]:\n    layer.trainable = True\n\nnumOfEpoch = 35\nopt = SGD(0.001)\nmodel.compile(opt, 'categorical_crossentropy', ['accuracy'])\nH = model.fit_generator(aug_train.flow(X_train, y_train, batch_size=32), \n                        steps_per_epoch=32,\n                        validation_data=(aug_test.flow(X_test, y_test, batch_size=32)),\n                        validation_steps=32,\n                        epochs=numOfEpoch)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nfrom matplotlib import gridspec","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nhistory_frame = pd.DataFrame(H.history)\nhistory_frame.loc[:, ['loss', 'val_loss']].plot()\nhistory_frame.loc[:, ['accuracy', 'val_accuracy']].plot();","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}