{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "b0ac36bc-f1cb-cd31-b51e-9c4372aa5446"
      },
      "source": [
        "In the forum **efg2** commented about test images present in the training data set. Here's the confirmation."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "b5f31534-e83a-27c7-38e1-ac6d133fc26d"
      },
      "outputs": [],
      "source": [
        "%matplotlib inline\n",
        "import os\n",
        "import numpy as np \n",
        "import pandas as pd \n",
        "import glob\n",
        "import hashlib\n",
        "from scipy.misc import imresize\n",
        "import matplotlib.pyplot as plt\n",
        "import seaborn as sns\n",
        "\n",
        "hashes = {}\n",
        "labels = {} #map fname -> label\n",
        "\n",
        "print('computing md5 of training data:')\n",
        "for base_dir in [\"additional\", \"train\"]:\n",
        "    print(base_dir)\n",
        "    for fname in glob.glob(\"../input/{}/**/*.jpg\".format(base_dir)):\n",
        "        labels[fname] = fname.split('/')[-2]\n",
        "        h = hashlib.md5(open(fname, 'rb').read()).digest()        \n",
        "        if h in hashes:\n",
        "            hashes[h].append(fname)            \n",
        "        else:\n",
        "            hashes[h] = [fname]\n",
        "print(len(hashes))   "
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "993a62af-adf5-e5fb-2721-399eedcec33c"
      },
      "outputs": [],
      "source": [
        "repeated = sum(1 for k,v in hashes.items() if len(v) > 1)\n",
        "print(\"files appearing more than once in train + additional:\")\n",
        "print(repeated)\n",
        "        "
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "bf607ca7-9171-0fc7-f444-8a750dca6aac"
      },
      "outputs": [],
      "source": [
        "print(\"identical files with different labels:\")\n",
        "for k,v in hashes.items():\n",
        "    if len(v) > 1:\n",
        "        c = set([labels[x] for x in v])\n",
        "        if len(c) > 1:\n",
        "            print(v, c)\n",
        "            "
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "048da96e-b338-6974-c98c-e329effca6aa"
      },
      "outputs": [],
      "source": [
        "# find test files also present in training data\n",
        "leaks = []\n",
        "for fname in glob.glob(\"../input/test/*jpg\"):\n",
        "    h = hashlib.md5(open(fname, 'rb').read()).digest()\n",
        "    if h in hashes:\n",
        "        leaks.append((fname, hashes[h]))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "3e347042-02c4-81fe-0116-15c17ec1b85c"
      },
      "outputs": [],
      "source": [
        "leaks"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "d2caa394-76c0-a6d4-1ff4-7bb74a02f5fe"
      },
      "outputs": [],
      "source": [
        "for t1, t2 in leaks:\n",
        "    plt.figure()\n",
        "    plt.title(\"{}, {} - {}\".format(t2[0].split('/')[3], t1, t2))\n",
        "    plt.imshow(np.hstack([plt.imread(t1), plt.imread(t2[0])]))\n",
        "    "
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "2e5689c9-134d-fed8-c244-786a7576f10b"
      },
      "source": [
        "This corresponds to 4% of the test set (22/512 samples). Interestingly, it appears that these images are incorrectly labeled (can be confirmed easily via LB probing)."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "479b9387-3e6b-6e96-bc93-55e43c359aa8"
      },
      "outputs": [],
      "source": [
        ""
      ]
    }
  ],
  "metadata": {
    "_change_revision": 0,
    "_is_fork": false,
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.6.0"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}