{"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_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# Download YOLOv7 repository and install requirements\n!git clone https://github.com/WongKinYiu/yolov7\n%cd yolov7\n!pip install -r requirements.txt","metadata":{"id":"nD-uPyQ_2jiN","execution":{"iopub.status.busy":"2022-12-11T16:22:50.185463Z","iopub.execute_input":"2022-12-11T16:22:50.185956Z","iopub.status.idle":"2022-12-11T16:23:07.001243Z","shell.execute_reply.started":"2022-12-11T16:22:50.185868Z","shell.execute_reply":"2022-12-11T16:23:07.000084Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install roboflow\n\nfrom roboflow import Roboflow\nrf = Roboflow(api_key=\"WBbyS0AAtP2hdF7yu1I7\")\nproject = rf.workspace(\"rathinam-college-of-arts-and-science-l6xbt\").project(\"breasy_cancer\")\ndataset = project.version(1).download(\"yolov7\")","metadata":{"id":"ovKgrVN8ygdW","execution":{"iopub.status.busy":"2022-12-11T16:23:32.344142Z","iopub.execute_input":"2022-12-11T16:23:32.344535Z","iopub.status.idle":"2022-12-11T16:23:56.416841Z","shell.execute_reply.started":"2022-12-11T16:23:32.344502Z","shell.execute_reply":"2022-12-11T16:23:56.415602Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# download COCO starting checkpoint\n%cd /content/yolov7\n!wget https://github.com/WongKinYiu/yolov7/releases/download/v0.1/yolov7_training.pt","metadata":{"id":"bUbmy674bhpD","execution":{"iopub.status.busy":"2022-12-11T16:24:10.835336Z","iopub.execute_input":"2022-12-11T16:24:10.836229Z","iopub.status.idle":"2022-12-11T16:24:13.915149Z","shell.execute_reply.started":"2022-12-11T16:24:10.836191Z","shell.execute_reply":"2022-12-11T16:24:13.913995Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install wandb","metadata":{"execution":{"iopub.status.busy":"2022-12-11T16:24:17.541624Z","iopub.execute_input":"2022-12-11T16:24:17.542001Z","iopub.status.idle":"2022-12-11T16:24:28.448748Z","shell.execute_reply.started":"2022-12-11T16:24:17.541962Z","shell.execute_reply":"2022-12-11T16:24:28.44749Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from kaggle_secrets import UserSecretsClient\nsecret_label = \"wand_api\"\nsecret_value = UserSecretsClient().get_secret(secret_label)","metadata":{"execution":{"iopub.status.busy":"2022-12-11T17:14:06.065903Z","iopub.execute_input":"2022-12-11T17:14:06.06629Z","iopub.status.idle":"2022-12-11T17:14:06.302267Z","shell.execute_reply.started":"2022-12-11T17:14:06.066258Z","shell.execute_reply":"2022-12-11T17:14:06.301253Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import wandb\n\ntry:\n    user_secrets = UserSecretsClient()\n    wandb_api_key = user_secrets.get_secret(\"wand_api\")\n    wandb.login(key=wandb_api_key)\n    anonymous = None\nexcept:\n    wandb.login(anonymous='must')\n    print('To use your W&B account,\\nGo to Add-ons -> Secrets and provide your W&B access token. Use the Label name as WANDB. \\nGet your W&B access token from here: https://wandb.ai/authorize')\n    wandb.init(project=\"breast_cancer\", entity=\"adharsh1623\")\n    \n","metadata":{"execution":{"iopub.status.busy":"2022-12-11T17:14:08.86483Z","iopub.execute_input":"2022-12-11T17:14:08.865297Z","iopub.status.idle":"2022-12-11T17:14:09.175011Z","shell.execute_reply.started":"2022-12-11T17:14:08.865259Z","shell.execute_reply":"2022-12-11T17:14:09.174081Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# run this cell to begin training\n%cd /content/yolov7\n!python train.py --batch 32 --epochs 55 --data /kaggle/working/yolov7/breasy_cancer-1/data.yaml --weights 'yolov7_training.pt' --device 0,1 \n","metadata":{"id":"1iqOPKjr22mL","execution":{"iopub.status.busy":"2022-12-11T17:14:16.645733Z","iopub.execute_input":"2022-12-11T17:14:16.646107Z","iopub.status.idle":"2022-12-11T17:15:36.40452Z","shell.execute_reply.started":"2022-12-11T17:14:16.646072Z","shell.execute_reply":"2022-12-11T17:15:36.403304Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Run evaluation\n!ipython detect.py --weights runs/train/exp5/weights/best.pt --conf 0.1 --source  /kaggle/working/yolov7/breasy_cancer-1/test/images\n","metadata":{"id":"N4cfnLtTCIce","execution":{"iopub.status.busy":"2022-12-11T17:15:55.236446Z","iopub.execute_input":"2022-12-11T17:15:55.237457Z","iopub.status.idle":"2022-12-11T17:16:15.445591Z","shell.execute_reply.started":"2022-12-11T17:15:55.237403Z","shell.execute_reply":"2022-12-11T17:16:15.444204Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#display inference on ALL test images\n\nimport glob\nfrom IPython.display import Image, display\n\ni = 0\nlimit = 10000 # max images to print\nfor imageName in glob.glob('/kaggle/working/yolov7/runs/detect/exp2/*.jpg'): #assuming JPG\n    if i < limit:\n      display(Image(filename=imageName))\n      print(\"\\n\")\n    i = i + 1\n    ","metadata":{"id":"6AGhNOSSHY4_","outputId":"b0e7593f-5c5b-4807-82ab-57ffc65a8ca2","execution":{"iopub.status.busy":"2022-12-11T17:16:18.106263Z","iopub.execute_input":"2022-12-11T17:16:18.106972Z","iopub.status.idle":"2022-12-11T17:16:18.173353Z","shell.execute_reply.started":"2022-12-11T17:16:18.106887Z","shell.execute_reply":"2022-12-11T17:16:18.17246Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Reparameterize for Inference\n\nhttps://github.com/WongKinYiu/yolov7/blob/main/tools/reparameterization.ipynb","metadata":{"id":"aMumI7a2JDAN"}},{"cell_type":"markdown","source":"# OPTIONAL: Deployment\n\nTo deploy, you'll need to export your weights and save them to use later.","metadata":{"id":"4jn4kCtgKiGO"}},{"cell_type":"code","source":"# optional, zip to download weights and results locally\n\n!zip -r export.zip runs/detect\n!zip -r export.zip runs/train/exp/weights/best.pt\n!zip export.zip runs/train/exp/*","metadata":{"id":"wWOok8abrCsL","execution":{"iopub.status.busy":"2022-12-11T16:23:22.350171Z","iopub.status.idle":"2022-12-11T16:23:22.35128Z","shell.execute_reply.started":"2022-12-11T16:23:22.351019Z","shell.execute_reply":"2022-12-11T16:23:22.351044Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# OPTIONAL: Active Learning Example\n\nOnce our first training run is complete, we should use our model to help identify which images are most problematic in order to investigate, annotate, and improve our dataset (and, therefore, model).\n\nTo do that, we can execute code that automatically uploads images back to our hosted dataset if the image is a specific class or below a given confidence threshold.\n","metadata":{"id":"f41PvE5gKhYw"}},{"cell_type":"code","source":"# # setup access to your workspace\n# rf = Roboflow(api_key=\"YOUR_API_KEY\")                               # used above to load data\n# inference_project =  rf.workspace().project(\"YOUR_PROJECT_NAME\")    # used above to load data\n# model = inference_project.version(1).model\n\n# upload_project = rf.workspace().project(\"YOUR_PROJECT_NAME\")\n\n# print(\"inference reference point: \", inference_project)\n# print(\"upload destination: \", upload_project)","metadata":{"id":"mcINqQS7Kt3-","execution":{"iopub.status.busy":"2022-12-11T16:23:22.352558Z","iopub.status.idle":"2022-12-11T16:23:22.356101Z","shell.execute_reply.started":"2022-12-11T16:23:22.355845Z","shell.execute_reply":"2022-12-11T16:23:22.355869Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# # example upload: if prediction is below a given confidence threshold, upload it \n\n# confidence_interval = [10,70]                                   # [lower_bound_percent, upper_bound_percent]\n\n# for prediction in predictions:                                  # predictions list to loop through\n#   if(prediction['confidence'] * 100 >= confidence_interval[0] and \n#           prediction['confidence'] * 100 <= confidence_interval[1]):\n        \n#           # upload on success!\n#           print(' >> image uploaded!')\n#           upload_project.upload(image, num_retry_uploads=3)     # upload image in question","metadata":{"id":"cEl1NVE3LSD_","execution":{"iopub.status.busy":"2022-12-11T16:23:22.357634Z","iopub.status.idle":"2022-12-11T16:23:22.359476Z","shell.execute_reply.started":"2022-12-11T16:23:22.359204Z","shell.execute_reply":"2022-12-11T16:23:22.359227Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Next steps\n\nCongratulations, you've trained a custom YOLOv7 model! Next, start thinking about deploying and [building an MLOps pipeline](https://docs.roboflow.com) so your model gets better the more data it sees in the wild.","metadata":{"id":"LVpCFeU-K4gb"}}]}