{"cells":[{"metadata":{"trusted":true},"cell_type":"code","source":"!pip install ../input/faiss-163/faiss_cpu-1.6.3-cp37-cp37m-manylinux2010_x86_64.whl","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# load gallery features\n\nimport faiss\nimport numpy as np\nimport sys\n# sys.path.insert(2, '/kaggle/working/input/cassavacodes/Cassava/classification')\nsys.path.insert(0, '/kaggle/working/input/cassavacodes/Cassava/retrieval')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# write image filenames in a txt\n\nimport os\n\ntest_img_lst = os.listdir('../input/cassava-leaf-disease-classification/test_images')\n\nf = open('test_images.txt', 'w')\nfor img_file in test_img_lst:\n    f.write('/kaggle/input/cassava-leaf-disease-classification/test_images/' + img_file + ',5\\n')\nf.close()\n\n# os.path.exists('/kaggle/working/test_images.txt')\n# f=open('test_images.txt')\n# f.readlines()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!mkdir ./log\n!mkdir ./features","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# 抽特征\ntry:\n    !cd ../input; python -m cassavacodes.Cassava.retrieval.extract.run --config-file ./configs/extract_r50_gq_infer.py --load-path ../input/wwwwww/weight_epoch_13.pth --log-dir /kaggle/working/log -device 0\nexcept:\n    pass","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!ls /kaggle/working/features/extract_r50_gq_infer/","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"np.load('/kaggle/working/features/extract_r50_gq_infer/gallery_targets.npy')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"gallery_features = np.load('../input/gallery/gallery_features.npy')\ngallery_targets = np.load('../input/gallery/gallery_targets.npy')\n\nquery_features = np.load('/kaggle/working/features/extract_r50_gq_infer/gallery_features.npy')\nquery_names = np.load('/kaggle/working/features/extract_r50_gq_infer/gallery_names.npy')\n\nfaiss.normalize_L2(gallery_features)\nfaiss.normalize_L2(query_features)\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"index = faiss.IndexFlatL2(gallery_features.shape[1])\nindex.add(gallery_features)\n\nk=3\nD, I = index.search(gallery_features, k)\n\nprint(gallery_targets[I[-10:]])\noutliers = []\n\nfor i, indices in enumerate(I):\n    if gallery_targets[indices[0]] != gallery_targets[indices[1]]:\n        outliers.append(i)\n        \n        \n        \nprint(gallery_features.shape)\ngallery_features = np.delete(gallery_features , outliers, axis=0)\ngallery_targets = np.delete(gallery_targets , outliers, axis=0)\nprint(gallery_features.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"index = faiss.IndexFlatL2(gallery_features.shape[1])\nindex.add(gallery_features)\nk=1\nD, I = index.search(query_features, k)\n\n\nf = open('submission.csv', 'w')\nf.write('image_id,label\\n')\n\n\nfor fn, i in zip(query_names, I):\n    fn = fn.split('/')[-1]\n    pred = gallery_targets[i][0]\n    f.write(fn + ',' + str(pred) + '\\n')\nf.close()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"f = open('submission.csv', 'r')\nprint(f.readlines())\nf.close()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","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}