{"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 the files in the input directory\n\nimport os\nprint(os.listdir(\"../input\"))\n\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"import os, cv2, random\nimport numpy as np\nimport pandas as pd\nimport seaborn as sns\nimport matplotlib.pyplot as plt\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import classification_report\nfrom tqdm import tqdm    #Helps in vis\nfrom random import shuffle \nfrom tensorflow.python.keras.applications import ResNet50\nfrom tensorflow.python.keras.models import Sequential\nfrom tensorflow.python.keras.layers import Dense, Flatten, GlobalAveragePooling2D\n%matplotlib inline ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"49ec82d41ca619d5458566d5541e488c7166cea7"},"cell_type":"code","source":"import keras\nfrom keras import applications\nfrom keras import optimizers\nfrom keras import backend as K\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.models import Sequential\nfrom keras.layers import Conv2D, MaxPooling2D,GlobalMaxPooling2D\nfrom keras.layers import Activation, Dropout, Flatten, Dense\nfrom keras.constraints import maxnorm\nfrom keras.optimizers import SGD\nfrom keras.utils import np_utils\nfrom keras.callbacks import Callback,ModelCheckpoint\nfrom keras.applications import VGG16\nfrom keras.applications import ResNet50\nfrom keras.models import load_model\nfrom keras.preprocessing.image import load_img","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4e242501d480b2cc35f4f96a297a75bf8343032b","collapsed":true},"cell_type":"code","source":"val_size = 0.5\nrandom_state = 1972\nbatch_size = 32\nepochs= 10\nnum_classes = 2\nsample_size = 20000\npath = '../input/dogs-vs-cats-redux-kernels-edition/'\ntrain_dir = path+'train/'\ntest_dir =  path+'test/'\nsample_path='../input/test-data/test_data/'\nsample_test_dir=sample_path+'test_data/'\nimg_size = 224\nresnet_weight_path = '../input/resnet50/resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5'","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"aab7ec085471f24d56975b63a5ebd56a5fb67660","collapsed":true},"cell_type":"code","source":"train_images = os.listdir(train_dir)[0:sample_size]\ntest_images= os.listdir(test_dir)\nsample_test_images=os.listdir(sample_test_dir)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"859052ff472f05ec97c1156a87584feabae843b7","collapsed":true},"cell_type":"code","source":"def create_pet_label_sample(img):\n    #pet = img.split('.')[-3]\n    pet = img[:3]\n    if pet == 'cat': return [1,0]\n    elif pet == 'dog': return [0,1]\n\ndef create_pet_label_train(img):\n    pet = img.split('.')[-3]\n    #pet = img[:2]\n    if pet == 'cat': return [1,0]\n    elif pet == 'dog': return [0,1]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"70d453ed1cb2a1c60471674ba4d07c58a0095aaf","collapsed":true},"cell_type":"code","source":"def process_images(image_list, image_dir,image_type):\n    data_df = []\n    for img in tqdm(image_list):\n        path = os.path.join(image_dir,img)\n        if image_type=='train':\n            label = create_pet_label_train(img)\n        if image_type=='sample':\n            label = create_pet_label_sample(img)    \n        else:\n            label = img.split('.')[0]\n        img = cv2.imread(path,cv2.IMREAD_COLOR)\n        img = cv2.resize(img, (img_size,img_size))\n        data_df.append([np.array(img),np.array(label)])\n    shuffle(data_df)\n    return data_df","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"cb2bb50fb5e9b683bef85a7a3bfb55745a734dfa"},"cell_type":"code","source":"sample_test=process_images(sample_test_images, sample_test_dir,'sample')","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"97ecc3aa465dc33bde469ed60e5976ed7e467f07"},"cell_type":"markdown","source":"## Generating arrays of train and sample test image data"},{"metadata":{"trusted":true,"_uuid":"32d301e6bf5ae20c11a48a05ef3b55167f673708","collapsed":true},"cell_type":"code","source":"Xt = np.array([i[0] for i in sample_test]).reshape(-1,img_size,img_size,3)\nyt = np.array([i[1] for i in sample_test])","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d8e463ee1906cd75be83ed6970808fcd2faaf180"},"cell_type":"markdown","source":"## Loading pretrained model from file"},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"486ce526cf292bbfa2ed3a93d99ac3e4ccaca0d4"},"cell_type":"code","source":"model=load_model('../input/dogs-vs-cats-capstone-final-rn-50/resnet50.h5')","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"cc436234d38ea3fd37a4500b7d095071df5b6447"},"cell_type":"markdown","source":"## Generate number of errors in predictions on sample test data"},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"067ffd8203eaf4f33aa8dd8d199373c5d6fbb297"},"cell_type":"code","source":"#get the predictions for the test data\npredicted_classes = model.predict_classes(Xt)\n#get the indices to be plotted\ny_true = np.argmax(yt,axis=1)\ncorrect = np.nonzero(predicted_classes==y_true)[0]\nincorrect = np.nonzero(predicted_classes!=y_true)[0]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f1e732ac0c669305f14fc9af82be3599504647fb"},"cell_type":"code","source":"target_names = [\"Class {}:\".format(i) for i in range(num_classes)]\nprint(classification_report(y_true, predicted_classes, target_names=target_names))","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"786a03273ac63b47fb1a1575b8aaf108b43cbf24"},"cell_type":"markdown","source":"## Print misclassified images"},{"metadata":{"trusted":true,"_uuid":"772a12060ac9d10483755ec22c3d5456d4df1673"},"cell_type":"code","source":"# Get the filenames\nfnames=sample_test_images\n# Get the label to class mapping from the generator\nlabel2index = {'cat': 0, 'dog': 1}\n # Getting the mapping from class index to class label\nidx2label = dict((v,k) for k,v in label2index.items())\nground_truth = np.argmax(yt,axis=1)\n# Show the errors\npredictions = model.predict(Xt)\npredicted_class = np.argmax(predictions,axis=1)\nerrors = np.where(predicted_class != ground_truth)[0]\nprint(\"No of errors = {}/{}\".format(len(errors),len(Xt)))\n#f, ax = plt.subplots(5,5, figsize=(15,15))\nfor i in range(len(errors)):\n    pred_class = np.argmax(predictions[errors[i]])\n    pred_label = idx2label[pred_class]\n    title = 'y_true:{},Original label:{}, Prediction :{}, confidence : {:.3f}'.format(ground_truth[i],\n        fnames[errors[i]][:3],\n        pred_label,\n        predictions[errors[i]][pred_class])\n    original = load_img('{}/{}'.format(sample_test_dir,fnames[errors[i]]))\n    plt.figure(figsize=[5,5])\n    plt.axis('off')\n    plt.title(title)\n    plt.imshow(original)\n    plt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a8c45bab9d740c587bb8bdc241ac895db1560395","collapsed":true},"cell_type":"code","source":"f, ax = plt.subplots(5,5, figsize=(15,15))\nfor i,data in enumerate(sample_test[:25]):\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    \n    if np.argmax(model_out) == 1: \n        str_predicted='Dog'\n    else: \n        str_predicted='Cat'\n    ax[i//5, i%5].imshow(orig)\n    ax[i//5, i%5].axis('off')\n    ax[i//5, i%5].set_title(\"Predicted:{}\".format(str_predicted))    \nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"d98da2183fab693f068b6fd252d09cdffc2c8a5c"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"8cbf94a3fa07ebda7363b58fdbce9f3e4af90b91"},"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":1}