{"cells":[{"metadata":{"_uuid":"e48fca5b06135c4b0aee1ccb7295773ceb909bdf"},"cell_type":"markdown","source":"Based on [_1](https://www.kaggle.com/suniliitb96/tutorial-keras-transfer-learning-with-resnet50) and [_2](https://www.kaggle.com/abhiksark/introduction-to-transfer-learning-cats-dogs)\n# Transfer learning\nIt is a machine learning method where a model developed for a task is reused as the starting point for a model on a second task."},{"metadata":{"trusted":true,"_uuid":"6bc39767d719ce394356529f5fce8b72e2810bb1"},"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport seaborn as sns\nimport matplotlib.pyplot as plt\n\nimport os, cv2, random\nfrom tqdm import tqdm\nfrom random import shuffle\n\n%matplotlib inline\n\n####################\n# Global Constants #\n####################\nTRAIN_DIR = '../input/dogs-vs-cats-redux-kernels-edition/train'\nTEST_DIR = '../input/dogs-vs-cats-redux-kernels-edition/test'\nNUM_CLASSES = 2\nIMG_SIZE = 224\nEPOCHS = 20\n\ntrain_images = os.listdir(TRAIN_DIR)#[:10000] # For testing purposes\ntest_images = os.listdir(TEST_DIR)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"758cf4494d143da7e7683040a2b17dc5a3612567"},"cell_type":"code","source":"# Return one-hot array [cat, dog] of image\ndef label_img(img):\n    word_label = img.split('.')[-3]\n    if word_label == 'cat': return [1, 0]\n    elif word_label == 'dog' : return [0, 1]\n\n# Return a numpy array of train and test data\ndef create_train_data():\n    training_data = []\n    for img in tqdm(train_images):\n        label = label_img(img)\n        img_path = os.path.join(TRAIN_DIR, img)\n        img = cv2.imread(img_path, cv2.IMREAD_ANYCOLOR)\n        img = cv2.resize(img, (IMG_SIZE, IMG_SIZE))\n        training_data.append([np.array(img), np.array(label)])\n    shuffle(training_data)\n    return training_data\n\ndef process_test_data():\n    testing_data = []\n    for img in tqdm(test_images):\n        path = os.path.join(TEST_DIR,img)\n        img_num = img.split('.')[0]\n        img = cv2.imread(path,cv2.IMREAD_COLOR)\n        img = cv2.resize(img, (IMG_SIZE,IMG_SIZE))\n        testing_data.append([np.array(img), img_num])\n    shuffle(testing_data)\n    return testing_data\n\ntraining_data = create_train_data()\nX = np.array([i[0] for i in training_data]).reshape(-1, IMG_SIZE, IMG_SIZE, 3)\ny = np.array([i[1] for i in training_data])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ff69114b31aebd24406b02edbe93a11e19178019"},"cell_type":"code","source":"from keras.applications import ResNet50\nfrom keras.models import Sequential\nfrom keras import optimizers\nfrom keras.layers import Dense\n\n# Path of ResNet50 pre-trained weights except its TOP layer,\nRESNET_WEIGHTS_PATH = '../input/resnet50/resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5'\n\nmodel = Sequential()\nmodel.add(ResNet50(include_top = False,\n                   pooling = 'max',\n                   weights = RESNET_WEIGHTS_PATH))\nmodel.add(Dense(NUM_CLASSES, activation= 'softmax'))\nmodel.layers[0].trainable = False\n\n# Compile the Transfer Learning Model\nsgd = optimizers.SGD(lr = 1e-3, decay = 1e-6, momentum = 0.1, nesterov=True)\nmodel.compile(optimizer= sgd, loss = 'categorical_crossentropy', metrics= ['accuracy'])\n\nmodel.summary()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"6b0f0d21cdb0461f2c70e1069c05a9f5a2679953"},"cell_type":"markdown","source":"## Training"},{"metadata":{"trusted":true,"_uuid":"455ea546f78bc1294e0936029826c0c5c5fae1d1"},"cell_type":"code","source":"from keras.callbacks import ModelCheckpoint, ReduceLROnPlateau\ncheck_point = ModelCheckpoint('BestModel.hdf5', verbose=True, save_best_only=True)\nlr_reduce = ReduceLROnPlateau(monitor='val_acc', factor=0.1, min_delta=0.0001, patience=3, verbose=1)\nhistory = model.fit(X, y,\n                    validation_split=0.2,\n                    callbacks = [check_point, lr_reduce],\n                    epochs=EPOCHS,\n                    batch_size=80,\n                    verbose=1)\n\n# Plotting loss and accuracy for the model\nplt.plot(history.history['acc'])\nplt.plot(history.history['val_acc'])\nplt.title('model accuracy')\nplt.ylabel('accuracy')\nplt.xlabel('epoch')\nplt.legend(['train', 'test'], loc='upper left')\nplt.show()\n# summarize history for loss\nplt.plot(history.history['loss'])\nplt.plot(history.history['val_loss'])\nplt.title('model loss')\nplt.ylabel('loss')\nplt.xlabel('epoch')\nplt.legend(['train', 'test'], loc='upper left')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8f3a8a0fc848356e60ccbab01fbe6a22a4cb7b6a"},"cell_type":"markdown","source":"## Testing"},{"metadata":{"trusted":true,"_uuid":"0c191b7fabc05fac3434446facfb24b498504d0d"},"cell_type":"code","source":"# Free some space\nimport gc\ndel X, y, training_data\ngc.collect()\n\n# Load Best model\nfrom keras.models import load_model\nmodel = load_model('BestModel.hdf5')\n\n# Testing Model on Test Data\ntest_data = process_test_data()\nfig =plt.figure()\nfor num, data in enumerate(test_data[:12]):\n    # Cat: [1, 0]\n    # Dog: [0, 1]\n    img_num = data[1]\n    img_data = data[0]\n    \n    y = fig.add_subplot(3, 4, num+1)\n    data = img_data.reshape(-1, IMG_SIZE, IMG_SIZE, 3)\n    model_out = model.predict([data])[0]\n    \n    if np.argmax(model_out) == 1 : str_label ='Dog'\n    else: str_label = 'Cat'\n    y.imshow(img_data)\n    plt.title(str_label)\n    y.axes.get_xaxis().set_visible(False);\n    y.axes.get_yaxis().set_visible(False);\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"3aba644ae36bdafe4f720005c646adf6233846fa"},"cell_type":"markdown","source":"## Generate .csv for submission"},{"metadata":{"trusted":true,"_uuid":"1b564f68d331f27d9d85403002bd878af7d52fc1"},"cell_type":"code","source":"prob = []\nimg_list = []\nfor data in tqdm(test_data):\n        img_num = data[1]\n        img_data = data[0]\n        orig = img_data\n        data = img_data.reshape(-1,IMG_SIZE,IMG_SIZE,3)\n        model_out = model.predict([data])[0]\n        img_list.append(img_num)\n        prob.append(model_out[1])\n    \nsubmission = pd.DataFrame({'id':img_list , 'label':prob})\nprint(submission.head())\nsubmission.to_csv(\"submit.csv\", index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"226b45f9282d17819a5871c05e403e12e5b9d777"},"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.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}