{"cells":[{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"# !pip install -r ../input/setup-r/requirements.txt\n# !pip install numpy\n\n\n\n# !pip install --pre torch torchvision -f https://download.pytorch.org/whl/nightly/cu102/torch_nightly.html\n# !pip install iglovikov-helper-functions\n# !pip install pytorch-lightning\n# !pip install albumentations\n# !pip install pytorch-toolbelt","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# from pathlib import Path \n# import shutil\n\n\n# stored = '/root/.cache/pip/wheels'\n# wheels = list(Path(stored).rglob('*.whl'))\n\n\n# p = Path('./wheels')\n# p.mkdir(parents=True, exist_ok=True)\n\n\n# for whl in wheels: \n#     shutil.copy(whl, p)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# !pip uninstall pillow --yes","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# pip install pillow ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# !cat /etc/os-release","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!pip install ../input/offwheels/addict-2.2.1-py3-none-any.whl\n!pip install ../input/offwheels/mmcv-1.0.4-cp37-cp37m-linux_x86_64.whl\n!pip install ../input/offwheels/jpeg4py-0.1.4-py3-none-any.whl\n!pip install ../input/offwheels/jsonpointer-2.0-py2.py3-none-any.whl\n!pip install ../input/offwheels/jsonpatch-1.26-py2.py3-none-any.whl\n!pip install ../input/offwheels/torchfile-0.1.0-py3-none-any.whl \n!pip install ../input/offwheels/visdom-0.1.8.9-py3-none-any.whl\n!pip install ../input/offwheels/torchnet-0.0.4-py3-none-any.whl\n!pip install ../input/offwheels/Pillow-6.2.2-cp37-cp37m-manylinux1_x86_64.whl\n!pip install ../input/offwheels/pytorch_toolbelt-0.3.2-py3-none-any.whl\n!pip install ../input/offwheels/pytorch_lightning-0.8.5-py3-none-any.whl\n!pip install ../input/offwheels/imagecorruptions-1.1.0-py3-none-any.whl\n!pip install ../input/offwheels/albumentations-0.4.6-py3-none-any.whl\n!pip install ../input/offwheels/iglovikov_helper_functions-0.0.38-py2.py3-none-any.whl","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!cp -r ../input/retinaface . \n!cp -r ../input/configs . \n!ls ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!python -m retinaface.inference -i ../input/global-wheat-detection/test -c ../input/configs/2020-07-20.yaml -o ./test_output -w ../input/weights/epoch29.ckpt -v","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!ls test_output","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import cv2 \nimport matplotlib.pyplot as plt \nfrom PIL import Image \nfrom pathlib import Path \n\n\ndef plot_image_examples(files, rows=3, cols=3, title='Image examples'):\n    fig, axs = plt.subplots(rows, cols, figsize=(20,20))\n    \n    i = 0 \n    for row in range(rows):\n        for col in range(cols):\n            img = cv2.imread(str(files[i]))\n            axs[row, col].imshow(img)\n            axs[row, col].axis('off')\n            i += 1\n\n    plt.suptitle(title)\n\n    \nimage_files = list(Path('./test_output/viz').rglob('*.jpg'))\nplot_image_examples(image_files)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!ls ./test_output/viz","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import pandas as pd\nfrom pathlib import Path\nimport json\nfrom operator import itemgetter \n\n\ndef read_labels(labels_path): \n    label_files = Path(labels_path).rglob('*.json') \n    for label in label_files:  \n        with open(label) as f: \n            data = json.load(f)\n\n            file_id = Path(data['file_name']).stem \n\n            items = []\n            for annot in data['annotations']:\n                item = (annot['score'], *annot['bbox']) \n                items.append(item)\n            items = sorted(items, key=itemgetter(0))\n\n        yield file_id, items \n\n        \nres = []\ngen = read_labels('./test_output/labels')\nto_format = lambda x: \"{0:.4f} {1} {2} {3} {4}\".format(x[0], x[1], x[2], x[3], x[4])\nfor file_id, items in gen:\n    preds = [to_format(item) for item in items]\n    preds_str = \" \".join(preds)\n    res.append({'image_id': file_id,'PredictionString': preds_str})\n\n    \nres = pd.DataFrame(res, columns=['image_id', 'PredictionString'])\nres.sort_values(by='image_id', inplace=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sample_sbm = pd.read_csv('../input/global-wheat-detection/sample_submission.csv')\nsample_sbm = sample_sbm.drop(columns='PredictionString')\nsample_sbm ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"res = sample_sbm.merge(res, how='left', on=['image_id'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!rm -rf ./*","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"res.to_csv('submission.csv', index=False)\npd.read_csv('submission.csv')","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}