{"nbformat":4,"nbformat_minor":0,"metadata":{"colab":{"provenance":[],"gpuType":"T4"},"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":1718263954899,"user_tz":-480,"elapsed":26444,"user":{"displayName":"黃致宇","userId":"05257376044901631491"}},"outputId":"9f1d7a68-f087-4b87-ec1d-47413057c232"},"execution_count":2,"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":1718263957162,"user_tz":-480,"elapsed":2268,"user":{"displayName":"黃致宇","userId":"05257376044901631491"}},"outputId":"d7c7230a-0d22-4348-9de4-9e1ee4d39b30"},"execution_count":3,"outputs":[{"output_type":"stream","name":"stdout","text":["Collecting pyyaml==5.3.1\n"," Downloading PyYAML-5.3.1.tar.gz (269 kB)\n","\u001b[?25l \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m0.0/269.4 kB\u001b[0m \u001b[31m?\u001b[0m eta \u001b[36m-:--:--\u001b[0m\r\u001b[2K \u001b[91m━━━━━━━━━━━━━\u001b[0m\u001b[91m╸\u001b[0m\u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m92.2/269.4 kB\u001b[0m \u001b[31m2.6 MB/s\u001b[0m eta \u001b[36m0:00:01\u001b[0m\r\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m269.4/269.4 kB\u001b[0m \u001b[31m5.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n","\u001b[?25h \u001b[1;31merror\u001b[0m: \u001b[1msubprocess-exited-with-error\u001b[0m\n"," \n"," \u001b[31m×\u001b[0m \u001b[32mpython setup.py egg_info\u001b[0m did not run successfully.\n"," \u001b[31m│\u001b[0m exit code: \u001b[1;36m1\u001b[0m\n"," \u001b[31m╰─>\u001b[0m See above for output.\n"," \n"," \u001b[1;35mnote\u001b[0m: This error originates from a subprocess, and is likely not a problem with pip.\n"," Preparing metadata (setup.py) ... \u001b[?25l\u001b[?25herror\n","\u001b[1;31merror\u001b[0m: \u001b[1mmetadata-generation-failed\u001b[0m\n","\n","\u001b[31m×\u001b[0m Encountered error while generating package metadata.\n","\u001b[31m╰─>\u001b[0m See above for output.\n","\n","\u001b[1;35mnote\u001b[0m: This is an issue with the package mentioned above, not pip.\n","\u001b[1;36mhint\u001b[0m: See above for details.\n"]}]},{"cell_type":"markdown","source":["## 3. 下載程式碼"],"metadata":{"id":"ngH7Q6Kx6f7m"}},{"cell_type":"code","execution_count":4,"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"QA5CaOZY6aMM","executionInfo":{"status":"ok","timestamp":1718263957163,"user_tz":-480,"elapsed":22,"user":{"displayName":"黃致宇","userId":"05257376044901631491"}},"outputId":"5043cf2d-c098-4711-fe52-8c1573dc315c"},"outputs":[{"output_type":"stream","name":"stdout","text":["/content\n","/content/drive/MyDrive/data3\n"]}],"source":["#顯示當前目錄\n","!pwd\n","\n","#切換目錄\n","%cd /content/drive/MyDrive/data3"]},{"cell_type":"code","source":["# 從git上面下載程式碼(只要執行一次)\n","!git clone https://github.com/WongKinYiu/yolov7.git"],"metadata":{"id":"VGe25V0bVkdb","colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"status":"ok","timestamp":1718263957163,"user_tz":-480,"elapsed":19,"user":{"displayName":"黃致宇","userId":"05257376044901631491"}},"outputId":"61428a98-5a00-4ee8-c5b5-aa86c68e337a"},"execution_count":5,"outputs":[{"output_type":"stream","name":"stdout","text":["fatal: destination path 'yolov7' already exists and is not an empty directory.\n"]}]},{"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":1718263960458,"user_tz":-480,"elapsed":3312,"user":{"displayName":"黃致宇","userId":"05257376044901631491"}},"outputId":"fa564d2c-0ce8-492e-9888-96413319d8b3"},"execution_count":6,"outputs":[{"output_type":"stream","name":"stdout","text":["--2024-06-13 07:32:36-- https://github.com/WongKinYiu/yolov7/releases/download/v0.1/yolov7.pt\n","Resolving github.com (github.com)... 20.205.243.166\n","Connecting to github.com (github.com)|20.205.243.166|: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=releaseassetproduction%2F20240613%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20240613T073236Z&X-Amz-Expires=300&X-Amz-Signature=fedc5bb14cb442783ac47bd5c56b16a49c0532d5acaf9a495b8c19e615cf1cb2&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","--2024-06-13 07:32:36-- https://objects.githubusercontent.com/github-production-release-asset-2e65be/511187726/b0243edf-9fb0-4337-95e1-42555f1b37cf?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=releaseassetproduction%2F20240613%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20240613T073236Z&X-Amz-Expires=300&X-Amz-Signature=fedc5bb14cb442783ac47bd5c56b16a49c0532d5acaf9a495b8c19e615cf1cb2&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.2’\n","\n","yolov7.pt.2 100%[===================>] 72.08M 39.1MB/s in 1.8s \n","\n","2024-06-13 07:32:39 (39.1 MB/s) - ‘yolov7.pt.2’ 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":1718263960458,"user_tz":-480,"elapsed":5,"user":{"displayName":"黃致宇","userId":"05257376044901631491"}},"outputId":"dcd37812-f866-40bb-ec2e-7610153e6aa0"},"execution_count":7,"outputs":[{"output_type":"stream","name":"stdout","text":["/content/drive/MyDrive/data3/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/data3/yolov7"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"Z-vgRl_mVUGk","executionInfo":{"status":"ok","timestamp":1718263961320,"user_tz":-480,"elapsed":865,"user":{"displayName":"黃致宇","userId":"05257376044901631491"}},"outputId":"2c7c4bf2-a7b6-415c-f4e5-fe7c02d623ff"},"execution_count":8,"outputs":[{"output_type":"stream","name":"stdout","text":["/content/drive/MyDrive/data3/yolov7\n","/content/drive/MyDrive/data3/yolov7\n"]}]},{"cell_type":"code","source":["!python train.py --device 0 --batch-size 16 --epochs 100 --data /content/drive/MyDrive/data3/data.yaml --img 640 640 --hyp data/hyp.scratch.custom.yaml --cfg /content/drive/MyDrive/data3/yolov7/cfg/training/triffict.yaml --weights 'yolov7.pt' --name yolov7-fish"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"TtHOAGy56c2Q","outputId":"ee71429e-56bd-496d-c370-3db0b973adbc","executionInfo":{"status":"ok","timestamp":1718267228144,"user_tz":-480,"elapsed":3266826,"user":{"displayName":"黃致宇","userId":"05257376044901631491"}}},"execution_count":9,"outputs":[{"output_type":"stream","name":"stdout","text":["2024-06-13 07:32:45.970999: E external/local_xla/xla/stream_executor/cuda/cuda_dnn.cc:9261] Unable to register cuDNN factory: Attempting to register factory for plugin cuDNN when one has already been registered\n","2024-06-13 07:32:45.971073: E external/local_xla/xla/stream_executor/cuda/cuda_fft.cc:607] Unable to register cuFFT factory: Attempting to register factory for plugin cuFFT when one has already been registered\n","2024-06-13 07:32:45.972920: E external/local_xla/xla/stream_executor/cuda/cuda_blas.cc:1515] Unable to register cuBLAS factory: Attempting to register factory for plugin cuBLAS when one has already been registered\n","2024-06-13 07:32:45.983169: 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 AVX512F FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.\n","2024-06-13 07:32:47.516969: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Could not find TensorRT\n","YOLOR 🚀 v0.1-128-ga207844 torch 2.3.0+cu121 CUDA:0 (Tesla T4, 15102.0625MB)\n","\n","Namespace(weights='yolov7.pt', cfg='/content/drive/MyDrive/data3/yolov7/cfg/training/triffict.yaml', data='/content/drive/MyDrive/data3/data.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-fish3', 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 61126 models.yolo.IDetect [6, [[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, 37223526 parameters, 37223526 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 '/content/drive/MyDrive/data3/train/labels.cache' images and labels... 338 found, 0 missing, 0 empty, 0 corrupted: 100% 338/338 [00:00