{"cells":[{"metadata":{},"cell_type":"markdown","source":"# About this kernel\n\n- This is forked from https://www.kaggle.com/appian/panda-imagehash-to-detect-duplicate-images\n- I did a kfold split with this, grouping similar images\n    - And I used `networkx` when grouping","execution_count":null},{"metadata":{"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\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\n\n# Use Lopuhin's dataset for faster image loading.\n# https://www.kaggle.com/lopuhin/panda-2020-level-1-2\n\nimport glob\nfrom pathlib import Path\n\npaths = sorted(glob.glob('../input/panda-2020-level-1-2/train_images/train_images/*_2.jpeg'))\nprint(len(paths))\n\nimgids = [Path(p).stem.split('_')[0] for p in paths]\n\nprint(len(imgids))\nprint(len(set(imgids)))\n\nimport torch","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Use only 4000 images for demonstration.\n\n# paths = paths[:4000]\n\n# Here comes imagehash\n# https://github.com/JohannesBuchner/imagehash\n\nimport cv2\nimport imagehash\nfrom tqdm import tqdm_notebook as tqdm\nfrom PIL import Image\n\nfuncs = [\n    imagehash.average_hash,\n    imagehash.phash,\n    imagehash.dhash,\n    imagehash.whash,\n    #lambda x: imagehash.whash(x, mode='db4'),\n]\n\nhashes = []\n\nfor path in tqdm(paths, total=len(paths)):\n    image = cv2.imread(path)\n    image = Image.fromarray(image)\n    hashes.append(np.array([f(image).hash for f in funcs]).reshape(256))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# use cuda to speed up\nhashes = torch.Tensor(np.array(hashes).astype(int))\n\n# calc similarity scores\nsims = np.array([(hashes[i] == hashes).sum(dim=1).cpu().numpy()/256 for i in range(hashes.shape[0])])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sims.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sims2 = sims.copy()\nnp.fill_diagonal(sims2, 0)\n\nthreshold = 0.90\nduplicates = np.where(sims2 > threshold)\n# duplicates = np.where((sims2 > threshold))  (sims2 < (threshold + 0.1))\nprint(len(duplicates[0]))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Let's check image pairs with similarity larget than threshold.\n# You can lower threshold to find more duplicates (and more false positives).\n\nimport matplotlib.pyplot as plt\n\ncount = 20\ntmp = 0\n\npairs = {}\nfor i,j in zip(*duplicates):\n    if i == j:\n        continue\n\n    path1 = paths[i]\n    path2 = paths[j]\n    print(path1)\n    print(path2)\n    print(sims2[i, j])\n\n    image1 = cv2.imread(path1)\n    image2 = cv2.imread(path2)\n\n    if image1.shape[0] > image1.shape[1] / 2:\n        fig,ax = plt.subplots(figsize=(20,20), ncols=2)\n    elif image1.shape[1] > image1.shape[0] / 2:\n        fig,ax = plt.subplots(figsize=(20,20), nrows=2)\n    else:\n        fig,ax = plt.subplots(figsize=(20,30), nrows=2)\n    ax[0].imshow(image1)\n    ax[1].imshow(image2)\n    plt.show()\n    \n    tmp += 1\n    if tmp > count:\n        break","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"duplicates","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import networkx as nx\n\ng1 = nx.Graph()\nfor i, j in tqdm(zip(*duplicates)):\n    g1.add_edge(i, j)\n\nduplicates_groups = list(list(x) for x in nx.connected_components(g1))\n\nprint(len(duplicates_groups))\nlen_id = len(\"004dd32d9cd167d9cc31c13b704498af\")\n\ndf_dict = {\n    \"image_id\": list(),\n    \"group_id\": list(),\n    \"index_in_group\": list(),\n}\n\nfor group_idx, group in enumerate(duplicates_groups):\n    for indx, indx_path in enumerate(group):\n        p = Path(paths[indx_path])\n        img_id = p.stem.split('_')[0]\n        assert len(img_id) == 32\n        \n        df_dict[\"image_id\"].append(img_id)\n        df_dict[\"group_id\"].append(group_idx)\n        df_dict[\"index_in_group\"].append(indx)\n    \ndf = pd.DataFrame(df_dict)\ndisplay(df.head())\n\nprint(len(df))\nprint(len(df.image_id.unique()))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df.to_csv(\"duplicate_imgids_imghash_thres_090.csv\", index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":4}