You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 10 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. # Using Incremental Learning Job in Helmet Detection Scenario
  2. This document introduces how to use incremental learning job in helmet detection scenario.
  3. Using the incremental learning job, our application can automatically retrains, evaluates,
  4. and updates models based on the data generated at the edge.
  5. ## Helmet Detection Experiment
  6. ### Install Sedna
  7. Follow the [Sedna installation document](/docs/setup/install.md) to install Sedna.
  8. ### Prepare Model
  9. In this example, we need to prepare base model and deploy model in advance.
  10. download [models](https://kubeedge.obs.cn-north-1.myhuaweicloud.com/examples/helmet-detection/model.tar.gz), including base model and deploy model.
  11. ```
  12. cd /
  13. wget https://kubeedge.obs.cn-north-1.myhuaweicloud.com/examples/helmet-detection/models.tar.gz
  14. tar -zxvf models.tar.gz
  15. ```
  16. ### Prepare for Inference Worker
  17. in this example, we simulate a inference worker for helmet detection, the worker will upload hard examples to `HE_SAVED_URL`, while
  18. it inferences data from local video. we need to make following preparations:
  19. * make sure following localdirs exist
  20. ```
  21. mkdir -p /incremental_learning/video/
  22. mkdir -p /incremental_learning/he/
  23. mkdir -p /data/helmet_detection
  24. mkdir /output
  25. ```
  26. * download [video](https://kubeedge.obs.cn-north-1.myhuaweicloud.com/examples/helmet-detection/video.tar.gz), unzip video.tar.gz, and put it into `/incremental_learning/video/`
  27. ```
  28. cd /incremental_learning/video/
  29. wget https://kubeedge.obs.cn-north-1.myhuaweicloud.com/examples/helmet-detection/video.tar.gz
  30. tar -zxvf video.tar.gz
  31. ```
  32. ### Prepare Image
  33. this example use the image:
  34. ```
  35. kubeedge/sedna-example-incremental-learning-helmet-detection:v0.3.0
  36. ```
  37. This image is generated by the script [build_images.sh](/examples/build_image.sh), used for creating training, eval and inference worker.
  38. ### Create Incremental Job
  39. in this example, `$WORKER_NODE` is a custom node, you can fill it which you actually run.
  40. ```
  41. WORKER_NODE="edge-node"
  42. ```
  43. Create Dataset
  44. ```
  45. kubectl create -f - <<EOF
  46. apiVersion: sedna.io/v1alpha1
  47. kind: Dataset
  48. metadata:
  49. name: incremental-dataset
  50. spec:
  51. url: "/data/helmet_detection/train_data/train_data.txt"
  52. format: "txt"
  53. nodeName: $WORKER_NODE
  54. EOF
  55. ```
  56. Create Initial Model to simulate the initial model in incremental learning scenario.
  57. ```
  58. kubectl create -f - <<EOF
  59. apiVersion: sedna.io/v1alpha1
  60. kind: Model
  61. metadata:
  62. name: initial-model
  63. spec:
  64. url : "/models/base_model"
  65. format: "ckpt"
  66. EOF
  67. ```
  68. Create Deploy Model
  69. ```
  70. kubectl create -f - <<EOF
  71. apiVersion: sedna.io/v1alpha1
  72. kind: Model
  73. metadata:
  74. name: deploy-model
  75. spec:
  76. url : "/models/deploy_model/saved_model.pb"
  77. format: "pb"
  78. EOF
  79. ```
  80. Start The Incremental Learning Job
  81. ```
  82. kubectl create -f - <<EOF
  83. apiVersion: sedna.io/v1alpha1
  84. kind: IncrementalLearningJob
  85. metadata:
  86. name: helmet-detection-demo
  87. spec:
  88. initialModel:
  89. name: "initial-model"
  90. dataset:
  91. name: "incremental-dataset"
  92. trainProb: 0.8
  93. trainSpec:
  94. template:
  95. spec:
  96. nodeName: $WORKER_NODE
  97. containers:
  98. - image: kubeedge/sedna-example-incremental-learning-helmet-detection:v0.3.0
  99. name: train-worker
  100. imagePullPolicy: IfNotPresent
  101. args: ["train.py"]
  102. env:
  103. - name: "batch_size"
  104. value: "32"
  105. - name: "epochs"
  106. value: "1"
  107. - name: "input_shape"
  108. value: "352,640"
  109. - name: "class_names"
  110. value: "person,helmet,helmet-on,helmet-off"
  111. - name: "nms_threshold"
  112. value: "0.4"
  113. - name: "obj_threshold"
  114. value: "0.3"
  115. trigger:
  116. checkPeriodSeconds: 60
  117. timer:
  118. start: 02:00
  119. end: 20:00
  120. condition:
  121. operator: ">"
  122. threshold: 500
  123. metric: num_of_samples
  124. evalSpec:
  125. template:
  126. spec:
  127. nodeName: $WORKER_NODE
  128. containers:
  129. - image: kubeedge/sedna-example-incremental-learning-helmet-detection:v0.3.0
  130. name: eval-worker
  131. imagePullPolicy: IfNotPresent
  132. args: ["eval.py"]
  133. env:
  134. - name: "input_shape"
  135. value: "352,640"
  136. - name: "class_names"
  137. value: "person,helmet,helmet-on,helmet-off"
  138. deploySpec:
  139. model:
  140. name: "deploy-model"
  141. trigger:
  142. condition:
  143. operator: ">"
  144. threshold: 0.1
  145. metric: precision_delta
  146. hardExampleMining:
  147. name: "IBT"
  148. parameters:
  149. - key: "threshold_img"
  150. value: "0.9"
  151. - key: "threshold_box"
  152. value: "0.9"
  153. template:
  154. spec:
  155. nodeName: $WORKER_NODE
  156. containers:
  157. - image: kubeedge/sedna-example-incremental-learning-helmet-detection:v0.3.0
  158. name: infer-worker
  159. imagePullPolicy: IfNotPresent
  160. args: ["inference.py"]
  161. env:
  162. - name: "input_shape"
  163. value: "352,640"
  164. - name: "video_url"
  165. value: "file://video/video.mp4"
  166. - name: "HE_SAVED_URL"
  167. value: "/he_saved_url"
  168. volumeMounts:
  169. - name: localvideo
  170. mountPath: /video/
  171. - name: hedir
  172. mountPath: /he_saved_url
  173. resources: # user defined resources
  174. limits:
  175. memory: 2Gi
  176. volumes: # user defined volumes
  177. - name: localvideo
  178. hostPath:
  179. path: /incremental_learning/video/
  180. type: Directory
  181. - name: hedir
  182. hostPath:
  183. path: /incremental_learning/he/
  184. type: Directory
  185. outputDir: "/output"
  186. EOF
  187. ```
  188. 1. The `Dataset` describes data with labels and `HE_SAVED_URL` indicates the address of the deploy container for uploading hard examples. Users will mark label for the hard examples in the address.
  189. 2. Ensure that the path of outputDir in the YAML file exists on your node. This path will be directly mounted to the container.
  190. ### Check Incremental Learning Job
  191. query the service status
  192. ```
  193. kubectl get incrementallearningjob helmet-detection-demo
  194. ```
  195. In the `IncrementalLearningJob` resource helmet-detection-demo, the following trigger is configured:
  196. ```
  197. trigger:
  198. checkPeriodSeconds: 60
  199. timer:
  200. start: 02:00
  201. end: 20:00
  202. condition:
  203. operator: ">"
  204. threshold: 500
  205. metric: num_of_samples
  206. ```
  207. ### Hard Example Labeling
  208. In a real word, we need to label the hard examples in `HE_SAVED_URL` with annotation tools and then put the examples to `Dataset`'s url.
  209. you can use Open-Source annotation tools to label hard examples, such as [MAKE SENSE](https://www.makesense.ai), which has following main advantages:
  210. * Open source and free to use under GPLv3 license
  211. * Support outputfile formats like YOLO, VOC XML, VGG JSON, CSV
  212. * No advanced installation required, just open up your browser
  213. * Use AI to make your work more productive
  214. * Offline running as a container, ensuring data security
  215. ![img.png](image/make-sense.png)
  216. the details labeling are not described here, main steps in this demo are as follows:
  217. * import unlabeled hard example to anonotation tools
  218. ![img_1.png](image/label-interface.png)
  219. * label and export annotations
  220. ![img_2.png](image/export-label.png)![img_3.png](image/label-result.png)
  221. * you will get YOLO format annotations, so you need convert them to the type which can be used by your own training code. in this example, the following scripts are
  222. provided for reference:
  223. ```
  224. import os
  225. annotation_dir_path = "C:/Users/Administrator/Desktop/labeled_data"
  226. save_path = "C:/Users/Administrator/Desktop/labeled_data/save_label.txt"
  227. def convert_single_line(line):
  228. line_list = []
  229. line = line.split(" ")
  230. for i in range(1, len(line)):
  231. line[i] = float(line[i])
  232. line[i] = line[i] * 1000
  233. line_list.append(str(int(line[i])))
  234. line_list.append(line[0])
  235. return ",".join(line_list)
  236. if __name__ == '__main__':
  237. results = []
  238. g = os.walk(annotation_dir_path)
  239. for path, dir_list, file_list in g:
  240. for file_name in file_list:
  241. file_path = os.path.join(path, file_name)
  242. file_name = file_name.split("txt")
  243. file_name = file_name[0] + 'jpg'
  244. single_label_string = file_name
  245. f = open(file_path)
  246. lines = f.readlines()
  247. for line in lines:
  248. line = line.strip('\n')
  249. single_label_string = single_label_string + " " + convert_single_line(line)
  250. results.append(single_label_string)
  251. save_file = open(save_path, "w")
  252. for result in results:
  253. save_file.write(result + "\n")
  254. save_file.close()
  255. ```
  256. How to use:
  257. `annotation_dir_path`: location for labeled annotations from MAKESENSE
  258. `save_path`: location for label txt which converted from annotations
  259. * run above script, you can get a txt which includes all label information
  260. * put the text with examples in the same dir
  261. * you will get labeled examples which meet training requirements
  262. ![img_1.png](image/reverted_label.png)
  263. * put these examples and annotations above to `Dataset`'s url
  264. Without annotation tools, we can simulate the condition of `num_of_samples` in the following ways:
  265. Download [dataset](https://kubeedge.obs.cn-north-1.myhuaweicloud.com/examples/helmet-detection/dataset.tar.gz) to $WORKER_NODE.
  266. ```
  267. cd /data/helmet_detection
  268. wget https://kubeedge.obs.cn-north-1.myhuaweicloud.com/examples/helmet-detection/dataset.tar.gz
  269. tar -zxvf dataset.tar.gz
  270. ```
  271. The LocalController component will check the number of the sample, realize trigger conditions are met and notice the GlobalManager Component to start train worker.
  272. When the train worker finish, we can view the updated model in the `/output` directory in `$WORKER_NODE` node.
  273. Then the eval worker will start to evaluate the model that train worker generated.
  274. If the eval result satisfy the `deploySpec`'s trigger
  275. ```
  276. trigger:
  277. condition:
  278. operator: ">"
  279. threshold: 0.1
  280. metric: precision_delta
  281. ```
  282. the deploy worker will load the new model and provide service.
  283. ### Effect Display
  284. in this example, false and failed detections occur at stage of inference before incremental learning, after incremental learning,
  285. all targets are correctly detected.
  286. ![img_1.png](image/effect_comparison.png)