{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"#Need to get working with your pretrained weights though\n#And also need to make sure it works without internet (how can I do that with the torch version?)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!pip install ../input/torchoffline/torch-1.6.0cu101-cp37-cp37m-linux_x86_64.whl\n!pip install ../input/torchoffline/torchvision-0.7.0cu101-cp37-cp37m-linux_x86_64.whl","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import torch\nimport os\nimport glob\nimport pandas as pd\nimport sys\nimport numpy as np\nfrom PIL import Image\n#1: Run prediction of testimages using pretrained weights\n\nyolo_dir = '/kaggle/input/yolosource-2/yolov5_to_zip'\nresult_dir = '/kaggle/working/inference/output'\nsubmission_dir = '/kaggle/working'\nprint(torch.__version__)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Latest version\n!mkdir utils\n!cp -r ../input/latestyolo/yolov5-master/utils/* ./utils/\n!mkdir models\n!cp -r ../input/latestyolo/yolov5-master/models/* ./models/\n!mkdir weights\n!cp -r ../input/latestyolo/yolov5-master/weights/* ./weights/\n!cp -r ../input/latestyolo/yolov5-master/detect.py ./\n!cp ../input/best-weights/full_best.pt ./weights/","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# !rm detect.py\n# !rm -r utils\n# !rm -r models\n# !rm -r weights","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# %run detect.py --weights yolov5x.pt --img 1024 --conf 0.4 --source /kaggle/input/global-wheat-detection/test --save-txt --device '0'","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!mkdir test_resized\nwidth = 1024\nheight = 1024\nfor filename in glob.glob('../input/global-wheat-detection/test/*.jpg'):\n    prefix = filename.split(\"/\")[-1].split(\".\")[0]\n    img = Image.open(filename)\n    new_img = img.resize((width,height))\n    new_img.save('./test_resized/'+prefix + '.jpg')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"%run detect.py --weights weights/full_best.pt --img 1024 --conf 0.5 --source ./test_resized --save-txt --device '0'","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\n# image_id,PredictionString\n# ce4833752,1.0 0 0 50 50\n# adcfa13da,1.0 0 0 50 50\n# 6ca7b2650,\n# 1da9078c1,0.3 0 0 50 50 0.5 10 10 30 30\n# 7640b4963,0.5 0 0 50 50\n#Have to be careful about what width and height should be depending on the size of data you test on\n# id,conf x y w h conf2 x2 y2 w2 h2. id is column under image_id, rest is column under PredictionString\n#Open all txt results\nwidth = 1024\nheight = 1024\nresults = []\nfor filename in glob.glob(os.path.join(result_dir,'*.txt')):\n    prefix = filename.split(\"/\")[-1].split(\".\")[0]\n    predict_str = \"\"\n    with open(filename, 'r') as fp:\n        for cnt, line in enumerate(fp):\n            entries = np.array(line.strip('\\n').split()).astype(np.float)\n            bbox = np.round([(entries[1]-entries[3]/2.0)*width,(entries[2]-entries[4]/2.0)*height,entries[3]*width,entries[4]*height],3).tolist()\n            conf = entries[-1]\n            predict_str = predict_str + str(conf) + \" \" + \" \".join(map(str,bbox)) + \" \" \n            \n            \n        results.append({'image_id':prefix, 'PredictionString':predict_str.rstrip()}) \nresultFrame = pd.DataFrame(results)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"temp = []\nfor filename in glob.glob('../input/global-wheat-detection/test/*.jpg'):\n    \n    prefix = filename.split(\"/\")[-1].split(\".\")[0]\n    if resultFrame['image_id'].str.contains(prefix).any():\n        pass\n    else:\n        temp.append({'image_id':prefix, 'PredictionString':\"\"})\n        \ndf = pd.DataFrame(temp)\nresultFrame = resultFrame.append(df)\n        \n   \n   \n    ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"resultFrame.to_csv(submission_dir + \"/submission.csv\", index = False)\nresultFrame.head()","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}