{"nbformat":4,"nbformat_minor":0,"metadata":{"colab":{"provenance":[],"gpuType":"T4","authorship_tag":"ABX9TyNyqbZ6ZZ77buTUipo8qr9U"},"kernelspec":{"name":"python3","display_name":"Python 3"},"language_info":{"name":"python"},"accelerator":"GPU"},"cells":[{"cell_type":"markdown","source":["## 1. 掛載雲端硬碟"],"metadata":{"id":"JvFnrA5V65pO"}},{"cell_type":"code","source":["from google.colab import drive\n","drive.mount('/content/drive')"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"vu6SEPk764ES","executionInfo":{"status":"ok","timestamp":1694140704552,"user_tz":-480,"elapsed":18182,"user":{"displayName":"Hung-Hsun Chen","userId":"00679635348522298932"}},"outputId":"222bb1d7-0fa8-4e8e-c577-c8b67651929d"},"execution_count":1,"outputs":[{"output_type":"stream","name":"stdout","text":["Mounted at /content/drive\n"]}]},{"cell_type":"markdown","source":["## 2. 安裝套件"],"metadata":{"id":"eOlb0q627EZI"}},{"cell_type":"code","source":["!pip install --upgrade pyyaml==5.3.1"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"5SCKBEDB7EFG","executionInfo":{"status":"ok","timestamp":1693951265787,"user_tz":-480,"elapsed":5277,"user":{"displayName":"Hung-Hsun Chen","userId":"00679635348522298932"}},"outputId":"c07342bd-c403-444f-bbd0-29c10088837b"},"execution_count":null,"outputs":[{"output_type":"stream","name":"stdout","text":["Requirement already satisfied: pyyaml==5.3.1 in /usr/local/lib/python3.10/dist-packages (5.3.1)\n"]}]},{"cell_type":"markdown","source":["## 3. 下載程式碼"],"metadata":{"id":"ngH7Q6Kx6f7m"}},{"cell_type":"code","execution_count":null,"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"QA5CaOZY6aMM","executionInfo":{"status":"ok","timestamp":1693951269398,"user_tz":-480,"elapsed":4,"user":{"displayName":"Hung-Hsun Chen","userId":"00679635348522298932"}},"outputId":"d80494b0-1479-476b-f8bd-f4caa85b1998"},"outputs":[{"output_type":"stream","name":"stdout","text":["/content/drive/MyDrive/高科大合作案/ObjectDetection/yolov7\n","/content/drive/MyDrive/高科大合作案/ObjectDetection\n"]}],"source":["#顯示當前目錄\n","!pwd\n","\n","#切換目錄\n","%cd /content/drive/MyDrive/高科大合作案/ObjectDetection"]},{"cell_type":"code","source":["# 從git上面下載程式碼(只要執行一次)\n","!git clone https://github.com/WongKinYiu/yolov7.git"],"metadata":{"id":"VGe25V0bVkdb"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":["## 4. 下載已經使用coco dataset預先訓練好的權重\n","* 從 https://github.com/WongKinYiu/yolov7.git 上面去尋找連結"],"metadata":{"id":"zdM561AuAUZc"}},{"cell_type":"code","source":["!wget https://github.com/WongKinYiu/yolov7/releases/download/v0.1/yolov7.pt"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"ffh4H-2E6eEo","executionInfo":{"status":"ok","timestamp":1693945330488,"user_tz":-480,"elapsed":1494,"user":{"displayName":"Hung-Hsun Chen","userId":"00679635348522298932"}},"outputId":"c8ee1634-60a4-4794-ce4f-0c654283f921"},"execution_count":null,"outputs":[{"output_type":"stream","name":"stdout","text":["--2023-09-05 20:22:08-- https://github.com/WongKinYiu/yolov7/releases/download/v0.1/yolov7.pt\n","Resolving github.com (github.com)... 140.82.112.4\n","Connecting to github.com (github.com)|140.82.112.4|:443... connected.\n","HTTP request sent, awaiting response... 302 Found\n","Location: https://objects.githubusercontent.com/github-production-release-asset-2e65be/511187726/b0243edf-9fb0-4337-95e1-42555f1b37cf?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIWNJYAX4CSVEH53A%2F20230905%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20230905T202208Z&X-Amz-Expires=300&X-Amz-Signature=1f437406d5699676ef115e7090dc09dae60ec91b7f825747ea3a97de403b1866&X-Amz-SignedHeaders=host&actor_id=0&key_id=0&repo_id=511187726&response-content-disposition=attachment%3B%20filename%3Dyolov7.pt&response-content-type=application%2Foctet-stream [following]\n","--2023-09-05 20:22:08-- https://objects.githubusercontent.com/github-production-release-asset-2e65be/511187726/b0243edf-9fb0-4337-95e1-42555f1b37cf?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIWNJYAX4CSVEH53A%2F20230905%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20230905T202208Z&X-Amz-Expires=300&X-Amz-Signature=1f437406d5699676ef115e7090dc09dae60ec91b7f825747ea3a97de403b1866&X-Amz-SignedHeaders=host&actor_id=0&key_id=0&repo_id=511187726&response-content-disposition=attachment%3B%20filename%3Dyolov7.pt&response-content-type=application%2Foctet-stream\n","Resolving objects.githubusercontent.com (objects.githubusercontent.com)... 185.199.108.133, 185.199.109.133, 185.199.110.133, ...\n","Connecting to objects.githubusercontent.com (objects.githubusercontent.com)|185.199.108.133|:443... connected.\n","HTTP request sent, awaiting response... 200 OK\n","Length: 75587165 (72M) [application/octet-stream]\n","Saving to: ‘yolov7.pt’\n","\n","yolov7.pt 100%[===================>] 72.08M 64.4MB/s in 1.1s \n","\n","2023-09-05 20:22:10 (64.4 MB/s) - ‘yolov7.pt’ saved [75587165/75587165]\n","\n"]}]},{"cell_type":"markdown","source":["## 5. 上傳資料與新增設定檔"],"metadata":{"id":"usnmC9u8AtTw"}},{"cell_type":"code","source":["#切換目錄到剛下載的git專案\n","%cd yolov7"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"0FaLnNRP6eB9","executionInfo":{"status":"ok","timestamp":1693951277345,"user_tz":-480,"elapsed":255,"user":{"displayName":"Hung-Hsun Chen","userId":"00679635348522298932"}},"outputId":"a783c10f-0ee5-4b29-e123-be96d1271bfd"},"execution_count":null,"outputs":[{"output_type":"stream","name":"stdout","text":["/content/drive/MyDrive/高科大合作案/ObjectDetection/yolov7\n"]}]},{"cell_type":"markdown","source":["## 6-1. 在./yolo/data/目錄底下手動建立資料夾 [project名稱]\n","## 6-2. 上傳訓練資料到./yolo/data/train跟 ./yolo/data/val/\n","## 6-3. 複製data目錄底下的coco.yaml, 改名為[project名稱].yaml\n","## 6-4. 編輯[project名稱].yaml裡面的參數\n","* train: ./data/[project名稱]/train\n","* val: ./data/[project名稱]/val\n","* test: ./data/[project名稱]/test\n","* nc: [總共有多少類別]\n","* names: [每個類別代表的英文名稱]\n","\n","## 6-5. 複製cfg/training目錄底下的yolov7.yaml 並改名為yolov7_[project名稱].yaml\n","## 6-6. 編輯yolov7_[project名稱].yaml\n","* nc: [總共有多少類別]"],"metadata":{"id":"px5Ui1z691Gf"}},{"cell_type":"markdown","source":["## 7. 訓練模型"],"metadata":{"id":"o1qlx6w0BIY4"}},{"cell_type":"code","source":["!pwd\n","\n","%cd /content/drive/MyDrive/高科大合作案/ObjectDetection/yolov7"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"Z-vgRl_mVUGk","executionInfo":{"status":"ok","timestamp":1693951280333,"user_tz":-480,"elapsed":426,"user":{"displayName":"Hung-Hsun Chen","userId":"00679635348522298932"}},"outputId":"ede2301a-4698-4334-8539-890661dff746"},"execution_count":null,"outputs":[{"output_type":"stream","name":"stdout","text":["/content/drive/MyDrive/高科大合作案/ObjectDetection/yolov7\n","/content/drive/MyDrive/高科大合作案/ObjectDetection/yolov7\n"]}]},{"cell_type":"code","source":["!python train.py --device 0 --batch-size 16 --epochs 100 --data data/fish.yaml --img 640 640 --hyp data/hyp.scratch.custom.yaml --cfg cfg/training/yolov7_fish.yaml --weights 'yolov7.pt' --name yolov7-fish"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"TtHOAGy56c2Q","outputId":"34517cf3-5252-4083-eadd-e0de4a439987"},"execution_count":null,"outputs":[{"output_type":"stream","name":"stdout","text":["2023-09-05 22:01:25.865011: I tensorflow/core/platform/cpu_feature_guard.cc:182] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.\n","To enable the following instructions: AVX2 FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.\n","2023-09-05 22:01:26.996060: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Could not find TensorRT\n","YOLOR 🚀 v0.1-126-g84932d7 torch 2.0.1+cu118 CUDA:0 (Tesla T4, 15101.8125MB)\n","\n","Namespace(weights='yolov7.pt', cfg='cfg/training/yolov7_fish.yaml', data='data/fish.yaml', hyp='data/hyp.scratch.custom.yaml', epochs=100, batch_size=16, img_size=[640, 640], rect=False, resume=False, nosave=False, notest=False, noautoanchor=False, evolve=False, bucket='', cache_images=False, image_weights=False, device='0', multi_scale=False, single_cls=False, adam=False, sync_bn=False, local_rank=-1, workers=8, project='runs/train', entity=None, name='yolov7-fish', exist_ok=False, quad=False, linear_lr=False, label_smoothing=0.0, upload_dataset=False, bbox_interval=-1, save_period=-1, artifact_alias='latest', freeze=[0], v5_metric=False, world_size=1, global_rank=-1, save_dir='runs/train/yolov7-fish4', total_batch_size=16)\n","\u001b[34m\u001b[1mtensorboard: \u001b[0mStart with 'tensorboard --logdir runs/train', view at http://localhost:6006/\n","\u001b[34m\u001b[1mhyperparameters: \u001b[0mlr0=0.01, lrf=0.1, momentum=0.937, weight_decay=0.0005, warmup_epochs=3.0, warmup_momentum=0.8, warmup_bias_lr=0.1, box=0.05, cls=0.3, cls_pw=1.0, obj=0.7, obj_pw=1.0, iou_t=0.2, anchor_t=4.0, fl_gamma=0.0, hsv_h=0.015, hsv_s=0.7, hsv_v=0.4, degrees=0.0, translate=0.2, scale=0.5, shear=0.0, perspective=0.0, flipud=0.0, fliplr=0.5, mosaic=1.0, mixup=0.0, copy_paste=0.0, paste_in=0.0, loss_ota=1\n","\u001b[34m\u001b[1mwandb: \u001b[0mInstall Weights & Biases for YOLOR logging with 'pip install wandb' (recommended)\n","\n"," from n params module arguments \n"," 0 -1 1 928 models.common.Conv [3, 32, 3, 1] \n"," 1 -1 1 18560 models.common.Conv [32, 64, 3, 2] \n"," 2 -1 1 36992 models.common.Conv [64, 64, 3, 1] \n"," 3 -1 1 73984 models.common.Conv [64, 128, 3, 2] \n"," 4 -1 1 8320 models.common.Conv [128, 64, 1, 1] \n"," 5 -2 1 8320 models.common.Conv [128, 64, 1, 1] \n"," 6 -1 1 36992 models.common.Conv [64, 64, 3, 1] \n"," 7 -1 1 36992 models.common.Conv [64, 64, 3, 1] \n"," 8 -1 1 36992 models.common.Conv [64, 64, 3, 1] \n"," 9 -1 1 36992 models.common.Conv [64, 64, 3, 1] \n"," 10 [-1, -3, -5, -6] 1 0 models.common.Concat [1] \n"," 11 -1 1 66048 models.common.Conv [256, 256, 1, 1] \n"," 12 -1 1 0 models.common.MP [] \n"," 13 -1 1 33024 models.common.Conv [256, 128, 1, 1] \n"," 14 -3 1 33024 models.common.Conv [256, 128, 1, 1] \n"," 15 -1 1 147712 models.common.Conv [128, 128, 3, 2] \n"," 16 [-1, -3] 1 0 models.common.Concat [1] \n"," 17 -1 1 33024 models.common.Conv [256, 128, 1, 1] \n"," 18 -2 1 33024 models.common.Conv [256, 128, 1, 1] \n"," 19 -1 1 147712 models.common.Conv [128, 128, 3, 1] \n"," 20 -1 1 147712 models.common.Conv [128, 128, 3, 1] \n"," 21 -1 1 147712 models.common.Conv [128, 128, 3, 1] \n"," 22 -1 1 147712 models.common.Conv [128, 128, 3, 1] \n"," 23 [-1, -3, -5, -6] 1 0 models.common.Concat [1] \n"," 24 -1 1 263168 models.common.Conv [512, 512, 1, 1] \n"," 25 -1 1 0 models.common.MP [] \n"," 26 -1 1 131584 models.common.Conv [512, 256, 1, 1] \n"," 27 -3 1 131584 models.common.Conv [512, 256, 1, 1] \n"," 28 -1 1 590336 models.common.Conv [256, 256, 3, 2] \n"," 29 [-1, -3] 1 0 models.common.Concat [1] \n"," 30 -1 1 131584 models.common.Conv [512, 256, 1, 1] \n"," 31 -2 1 131584 models.common.Conv [512, 256, 1, 1] \n"," 32 -1 1 590336 models.common.Conv [256, 256, 3, 1] \n"," 33 -1 1 590336 models.common.Conv [256, 256, 3, 1] \n"," 34 -1 1 590336 models.common.Conv [256, 256, 3, 1] \n"," 35 -1 1 590336 models.common.Conv [256, 256, 3, 1] \n"," 36 [-1, -3, -5, -6] 1 0 models.common.Concat [1] \n"," 37 -1 1 1050624 models.common.Conv [1024, 1024, 1, 1] \n"," 38 -1 1 0 models.common.MP [] \n"," 39 -1 1 525312 models.common.Conv [1024, 512, 1, 1] \n"," 40 -3 1 525312 models.common.Conv [1024, 512, 1, 1] \n"," 41 -1 1 2360320 models.common.Conv [512, 512, 3, 2] \n"," 42 [-1, -3] 1 0 models.common.Concat [1] \n"," 43 -1 1 262656 models.common.Conv [1024, 256, 1, 1] \n"," 44 -2 1 262656 models.common.Conv [1024, 256, 1, 1] \n"," 45 -1 1 590336 models.common.Conv [256, 256, 3, 1] \n"," 46 -1 1 590336 models.common.Conv [256, 256, 3, 1] \n"," 47 -1 1 590336 models.common.Conv [256, 256, 3, 1] \n"," 48 -1 1 590336 models.common.Conv [256, 256, 3, 1] \n"," 49 [-1, -3, -5, -6] 1 0 models.common.Concat [1] \n"," 50 -1 1 1050624 models.common.Conv [1024, 1024, 1, 1] \n"," 51 -1 1 7609344 models.common.SPPCSPC [1024, 512, 1] \n"," 52 -1 1 131584 models.common.Conv [512, 256, 1, 1] \n"," 53 -1 1 0 torch.nn.modules.upsampling.Upsample [None, 2, 'nearest'] \n"," 54 37 1 262656 models.common.Conv [1024, 256, 1, 1] \n"," 55 [-1, -2] 1 0 models.common.Concat [1] \n"," 56 -1 1 131584 models.common.Conv [512, 256, 1, 1] \n"," 57 -2 1 131584 models.common.Conv [512, 256, 1, 1] \n"," 58 -1 1 295168 models.common.Conv [256, 128, 3, 1] \n"," 59 -1 1 147712 models.common.Conv [128, 128, 3, 1] \n"," 60 -1 1 147712 models.common.Conv [128, 128, 3, 1] \n"," 61 -1 1 147712 models.common.Conv [128, 128, 3, 1] \n"," 62[-1, -2, -3, -4, -5, -6] 1 0 models.common.Concat [1] \n"," 63 -1 1 262656 models.common.Conv [1024, 256, 1, 1] \n"," 64 -1 1 33024 models.common.Conv [256, 128, 1, 1] \n"," 65 -1 1 0 torch.nn.modules.upsampling.Upsample [None, 2, 'nearest'] \n"," 66 24 1 65792 models.common.Conv [512, 128, 1, 1] \n"," 67 [-1, -2] 1 0 models.common.Concat [1] \n"," 68 -1 1 33024 models.common.Conv [256, 128, 1, 1] \n"," 69 -2 1 33024 models.common.Conv [256, 128, 1, 1] \n"," 70 -1 1 73856 models.common.Conv [128, 64, 3, 1] \n"," 71 -1 1 36992 models.common.Conv [64, 64, 3, 1] \n"," 72 -1 1 36992 models.common.Conv [64, 64, 3, 1] \n"," 73 -1 1 36992 models.common.Conv [64, 64, 3, 1] \n"," 74[-1, -2, -3, -4, -5, -6] 1 0 models.common.Concat [1] \n"," 75 -1 1 65792 models.common.Conv [512, 128, 1, 1] \n"," 76 -1 1 0 models.common.MP [] \n"," 77 -1 1 16640 models.common.Conv [128, 128, 1, 1] \n"," 78 -3 1 16640 models.common.Conv [128, 128, 1, 1] \n"," 79 -1 1 147712 models.common.Conv [128, 128, 3, 2] \n"," 80 [-1, -3, 63] 1 0 models.common.Concat [1] \n"," 81 -1 1 131584 models.common.Conv [512, 256, 1, 1] \n"," 82 -2 1 131584 models.common.Conv [512, 256, 1, 1] \n"," 83 -1 1 295168 models.common.Conv [256, 128, 3, 1] \n"," 84 -1 1 147712 models.common.Conv [128, 128, 3, 1] \n"," 85 -1 1 147712 models.common.Conv [128, 128, 3, 1] \n"," 86 -1 1 147712 models.common.Conv [128, 128, 3, 1] \n"," 87[-1, -2, -3, -4, -5, -6] 1 0 models.common.Concat [1] \n"," 88 -1 1 262656 models.common.Conv [1024, 256, 1, 1] \n"," 89 -1 1 0 models.common.MP [] \n"," 90 -1 1 66048 models.common.Conv [256, 256, 1, 1] \n"," 91 -3 1 66048 models.common.Conv [256, 256, 1, 1] \n"," 92 -1 1 590336 models.common.Conv [256, 256, 3, 2] \n"," 93 [-1, -3, 51] 1 0 models.common.Concat [1] \n"," 94 -1 1 525312 models.common.Conv [1024, 512, 1, 1] \n"," 95 -2 1 525312 models.common.Conv [1024, 512, 1, 1] \n"," 96 -1 1 1180160 models.common.Conv [512, 256, 3, 1] \n"," 97 -1 1 590336 models.common.Conv [256, 256, 3, 1] \n"," 98 -1 1 590336 models.common.Conv [256, 256, 3, 1] \n"," 99 -1 1 590336 models.common.Conv [256, 256, 3, 1] \n","100[-1, -2, -3, -4, -5, -6] 1 0 models.common.Concat [1] \n","101 -1 1 1049600 models.common.Conv [2048, 512, 1, 1] \n","102 75 1 328704 models.common.RepConv [128, 256, 3, 1] \n","103 88 1 1312768 models.common.RepConv [256, 512, 3, 1] \n","104 101 1 5246976 models.common.RepConv [512, 1024, 3, 1] \n","105 [102, 103, 104] 1 44944 models.yolo.IDetect [3, [[12, 16, 19, 36, 40, 28], [36, 75, 76, 55, 72, 146], [142, 110, 192, 243, 459, 401]], [256, 512, 1024]]\n","Model Summary: 415 layers, 37207344 parameters, 37207344 gradients\n","\n","Transferred 552/566 items from yolov7.pt\n","Scaled weight_decay = 0.0005\n","Optimizer groups: 95 .bias, 95 conv.weight, 98 other\n","\u001b[34m\u001b[1mtrain: \u001b[0mScanning 'data/fish/train.cache' images and labels... 640 found, 1 missing, 0 empty, 0 corrupted: 100% 641/641 [00:00