{"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","collapsed":true,"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"%matplotlib inline\nfrom matplotlib import pyplot as plt\nfrom PIL import Image\nfrom PIL import ImageFile \nimport numpy as np\nimport os \nimport cv2\nfrom tqdm import tqdm_notebook\nfrom random import shuffle\nimport pandas as pd\nimport random\nfrom tqdm import tqdm\nimport seaborn as sns\nimport math","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e07386d77524a917c70226ef81b2fdc67161d67b"},"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\nfrom sklearn.metrics import classification_report","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"0362c90e933d99cb4cc8ce4861f53d9411e3b0cd"},"cell_type":"code","source":"batch_size = 32\nepochs =20\nnum_classes = 2\nnum_t_samples = 20000\nnum_v_samples = 5000\npath='../input/dogs-vs-cats-redux-kernels-edition/'\ndir= '../input/dogscats/data/data/'\ntrain_data_path = path+'train/'\ntest_data_path =  path+'test/'\ntrain_data_dir = dir+'train/'\nvalidation_data_dir=dir+'validation/'\ntest_dir='../input/dogscatstest/test1/test1/'\nsample_test_dir='../input/sample-testdata/sample test data/sample test data'\nresnet50_weight_path='../input/resnet50/resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5'\n#img_size=197\nimg_size=224","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"98badd9d5afa15d3cab8e19fa726d602791743c0"},"cell_type":"code","source":"#Process training data to make it ready for fitting.\ntrain_datagen = ImageDataGenerator(rotation_range=40,\n        width_shift_range=0.2,\n        height_shift_range=0.2,\n        rescale=1./255,\n        shear_range=0.2,\n        zoom_range=0.2,\n        horizontal_flip=True,\n        fill_mode='nearest')\nval_datagen = ImageDataGenerator(rescale=1. / 255)\ntest_datagen = ImageDataGenerator(rescale=1./255)\n\ntrain_generator = train_datagen.flow_from_directory(train_data_dir,\n                                                    target_size=(img_size, img_size),\n                                                    batch_size=batch_size, \n                                                    shuffle=True,seed=42,\n                                                    class_mode='categorical')\nvalidation_generator = val_datagen.flow_from_directory(validation_data_dir,\n                                                       target_size=(img_size, img_size),\n                                                        batch_size=batch_size,\n                                                       shuffle=True,seed=42,\n                                                       class_mode='categorical') \ntest_generator = test_datagen.flow_from_directory(test_dir,\n                                                  target_size=(img_size, img_size),\n                                                  batch_size=batch_size,\n                                                  class_mode=None,shuffle=False,seed=42)\nfilename=test_generator.filenames\nsample_test_generator=test_datagen.flow_from_directory(sample_test_dir,\n                                                  target_size=(img_size, img_size),\n                                                  batch_size=batch_size,\n                                                  class_mode='categorical',shuffle=False,seed=42)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a7107171ed1244ae7b12de279ab57eecbefc199c"},"cell_type":"code","source":"print ('Creating model...')\nresnet50_model = Sequential()\nresnet50_model.add(ResNet50(include_top=False, pooling='max', weights=resnet50_weight_path))\nresnet50_model.add(Dense(num_classes, activation='softmax'))\nresnet50_model.layers[0].trainable = True\nprint ('Summary of the model...')\nresnet50_model.summary()\nprint ('Compiling model...')\n#resnet50_model.compile(optimizer='sgd', loss='categorical_crossentropy', metrics=['accuracy'])\nresnet50_model.compile(optimizer=SGD(lr=1e-3), loss='categorical_crossentropy', metrics=['accuracy'])\n#resnet50_model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])\nprint ('Model is ready to be fit with training data.')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"bae3e6737fd69afe7515eb147eb7fd499e0b7401"},"cell_type":"code","source":"class LossHistory(Callback):\n    def on_train_begin(self, logs={}):\n        self.losses = []\n        self.val_losses = []\n    def on_epoch_end(self, batch, logs={}):\n        self.losses.append(logs.get('loss'))\n        self.val_losses.append(logs.get('val_loss'))\nhistory = LossHistory()\nearly_stopping = keras.callbacks.EarlyStopping(monitor='val_loss',min_delta=0,patience=10,verbose=0, mode='auto')\ncheckpoint = ModelCheckpoint('resnet50_model_Final.h5',monitor='val_loss', verbose=1, save_best_only=True,mode='auto')\ncallbacks_list = [checkpoint,history,early_stopping]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c81e4a8d31b96a3291b0fe744b0266b04249986b","collapsed":true},"cell_type":"code","source":"fitted_resnet50_model=resnet50_model.fit_generator(train_generator,\n    steps_per_epoch=math.ceil(train_generator.samples/train_generator.batch_size),\n    epochs=epochs,validation_data=validation_generator,\n    validation_steps=math.ceil(validation_generator.samples/validation_generator.batch_size),callbacks=callbacks_list,verbose=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c87526391ee6248905cc7f534b8d47f5895793ea","collapsed":true},"cell_type":"code","source":"acc = fitted_resnet50_model.history['acc']\nval_acc = fitted_resnet50_model.history['val_acc']\nloss = fitted_resnet50_model.history['loss']\nval_loss =fitted_resnet50_model.history['val_loss']\nepochs = range(len(acc)) \nplt.plot(epochs, acc, 'b', label='Training acc')\nplt.plot(epochs, val_acc, 'r', label='Validation acc')\nplt.title('Training and validation accuracy')\nplt.legend()\nplt.figure()\nplt.plot(epochs, loss, 'b', label='Training loss')\nplt.plot(epochs, val_loss, 'r', label='Validation loss')\nplt.title('Training and validation loss')\nplt.legend()\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f6a2a2fcacf163b6c94164007ec362b9f3ccc500","collapsed":true},"cell_type":"code","source":"#Get the sample test data filenames from the generator\nfnames = sample_test_generator.filenames\n# Get the ground truth from generator\nground_truth = sample_test_generator.classes\n# Get the label to class mapping from the generator\nlabel2index = sample_test_generator.class_indices\n# Getting the mapping from class index to class label\nidx2label = dict((v,k) for k,v in label2index.items())\n# Get the predictions from the model using the generator\npredictions = resnet50_model.predict_generator(sample_test_generator,\nsteps=math.ceil(sample_test_generator.samples/sample_test_generator.batch_size),verbose=1)\npredicted_classes = np.argmax(predictions,axis=1)\nerrors = np.where(predicted_classes != ground_truth)[0]\nprint(\"No of errors = {}/{}\".format(len(errors),sample_test_generator.samples))\n# Display missclassified images\nfor i in range(len(errors)):\n    pred_class = np.argmax(predictions[errors[i]])\n    pred_label = idx2label[pred_class]\n    title = 'Original label:{}, Prediction :{}, confidence : {:.3f}'.format(fnames[errors[i]].split('/')[0],pred_label,\n        predictions[errors[i]][pred_class])\n    original = load_img('{}/{}'.format(sample_test_dir,fnames[errors[i]]))\n    plt.figure(figsize=[7,7])\n    plt.axis('off')\n    plt.title(title)\n    plt.imshow(original)\n    plt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9d2400939c2ea2e612c94adda882a0dff9397d03","collapsed":true},"cell_type":"code","source":"#get the predictions for the test data\n#predicted_classes = model.predict_classes(Xt)\n#get the indices to be plotted\n#y_true = np.argmax(yt,axis=1)\ncorrect = np.nonzero(predicted_classes==ground_truth)[0]\nincorrect = np.nonzero(predicted_classes!=ground_truth)[0]\ntarget_names = [\"Class {}:\".format(i) for i in range(num_classes)]\nprint(classification_report(ground_truth, predicted_classes, target_names=target_names))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2642186e564005f5f33b83370a0e5fb450c45420","collapsed":true},"cell_type":"code","source":"test_generator.reset()\npred=resnet50_model.predict_generator(test_generator,steps=math.ceil(test_generator.samples/test_generator.batch_size),verbose=1)\npredicted_class_indices=np.argmax(pred,axis=1)\nlabels = (train_generator.class_indices)\nlabels = dict((v,k) for k,v in labels.items())\npreds= [labels[k] for k in predicted_class_indices]\nnew_preds=[]\nfor i in range(len(preds)):\n    if preds[i]=='dogs':\n        new_preds.append('dog')\n    else:\n        new_preds.append('cat')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"b51ae002c7e81ed52c5258aed07ba95260013cc7"},"cell_type":"code","source":"def display_testdata(testdata,filenames):\n    f, ax = plt.subplots(5,5, figsize=(15,15))\n    i=0\n    for a,b in zip(testdata,filenames):\n        pred_label=a\n        fname=b\n        title = 'Prediction :{}'.format(pred_label)   \n        original = load_img('{}/{}'.format(test_dir,fname))\n        ax[i//5,i%5].axis('off')\n        ax[i//5,i%5].set_title(title)\n        ax[i//5,i%5].imshow(original)\n        i=i+1\n    plt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7690b93a209a8dbf59071dd8c1b3013a0bd461e6","collapsed":true},"cell_type":"code","source":"display_testdata(new_preds[1800:1825],filename[1800:1825])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9223bb02e55e51de7c68bec7550d6545c22da87f","collapsed":true},"cell_type":"code","source":"labels=[]\nfile_index=[]\nfor a,b in tqdm(zip(predicted_class_indices,filename)):\n    pred_label=a\n    fname=b[5:]\n    fname=fname.split('.')[0]\n    labels.append(pred_label)\n    file_index.append(fname)\nresults=pd.DataFrame({\"id\":file_index,\n                      \"label\":labels})\nresults.to_csv(\"submission.csv\",index=False)","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}