learning-loop-node 0.3.2__tar.gz → 0.18.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (136) hide show
  1. learning_loop_node-0.18.1/PKG-INFO +283 -0
  2. learning_loop_node-0.18.1/README.md +242 -0
  3. learning_loop_node-0.18.1/learning_loop_node/__init__.py +11 -0
  4. learning_loop_node-0.18.1/learning_loop_node/annotation/annotator_logic.py +29 -0
  5. learning_loop_node-0.18.1/learning_loop_node/annotation/annotator_node.py +116 -0
  6. learning_loop_node-0.18.1/learning_loop_node/data_classes/__init__.py +50 -0
  7. learning_loop_node-0.18.1/learning_loop_node/data_classes/annotation_data.py +43 -0
  8. learning_loop_node-0.18.1/learning_loop_node/data_classes/annotations.py +44 -0
  9. learning_loop_node-0.18.1/learning_loop_node/data_classes/detections.py +136 -0
  10. learning_loop_node-0.18.1/learning_loop_node/data_classes/general.py +187 -0
  11. learning_loop_node-0.18.1/learning_loop_node/data_classes/image_metadata.py +56 -0
  12. learning_loop_node-0.18.1/learning_loop_node/data_classes/socket_response.py +24 -0
  13. learning_loop_node-0.18.1/learning_loop_node/data_classes/training.py +168 -0
  14. learning_loop_node-0.18.1/learning_loop_node/data_exchanger.py +190 -0
  15. learning_loop_node-0.18.1/learning_loop_node/detector/__init__.py +0 -0
  16. learning_loop_node-0.18.1/learning_loop_node/detector/detector_logic.py +79 -0
  17. learning_loop_node-0.18.1/learning_loop_node/detector/detector_node.py +704 -0
  18. learning_loop_node-0.18.1/learning_loop_node/detector/exceptions.py +7 -0
  19. learning_loop_node-0.18.1/learning_loop_node/detector/inbox_filter/__init__.py +0 -0
  20. learning_loop_node-0.18.1/learning_loop_node/detector/inbox_filter/cam_observation_history.py +82 -0
  21. learning_loop_node-0.18.1/learning_loop_node/detector/inbox_filter/relevance_filter.py +40 -0
  22. learning_loop_node-0.18.1/learning_loop_node/detector/outbox.py +323 -0
  23. learning_loop_node-0.18.1/learning_loop_node/detector/rest/__init__.py +0 -0
  24. learning_loop_node-0.18.1/learning_loop_node/detector/rest/about.py +26 -0
  25. learning_loop_node-0.18.1/learning_loop_node/detector/rest/backdoor_controls.py +37 -0
  26. learning_loop_node-0.18.1/learning_loop_node/detector/rest/detect.py +59 -0
  27. learning_loop_node-0.18.1/learning_loop_node/detector/rest/model_version_control.py +46 -0
  28. learning_loop_node-0.18.1/learning_loop_node/detector/rest/operation_mode.py +54 -0
  29. learning_loop_node-0.18.1/learning_loop_node/detector/rest/outbox_mode.py +41 -0
  30. learning_loop_node-0.18.1/learning_loop_node/detector/rest/upload.py +44 -0
  31. learning_loop_node-0.18.1/learning_loop_node/enums/__init__.py +6 -0
  32. learning_loop_node-0.18.1/learning_loop_node/enums/annotator.py +11 -0
  33. learning_loop_node-0.18.1/learning_loop_node/enums/detector.py +18 -0
  34. learning_loop_node-0.18.1/learning_loop_node/enums/general.py +9 -0
  35. learning_loop_node-0.18.1/learning_loop_node/enums/trainer.py +22 -0
  36. learning_loop_node-0.18.1/learning_loop_node/examples/novelty_score_updater.py +70 -0
  37. learning_loop_node-0.18.1/learning_loop_node/globals.py +8 -0
  38. learning_loop_node-0.18.1/learning_loop_node/helpers/__init__.py +0 -0
  39. learning_loop_node-0.18.1/learning_loop_node/helpers/background_tasks.py +77 -0
  40. learning_loop_node-0.18.1/learning_loop_node/helpers/environment_reader.py +49 -0
  41. learning_loop_node-0.18.1/learning_loop_node/helpers/gdrive_downloader.py +43 -0
  42. learning_loop_node-0.18.1/learning_loop_node/helpers/log_conf.py +39 -0
  43. learning_loop_node-0.18.1/learning_loop_node/helpers/misc.py +239 -0
  44. learning_loop_node-0.18.1/learning_loop_node/helpers/run.py +21 -0
  45. learning_loop_node-0.18.1/learning_loop_node/loop_communication.py +168 -0
  46. learning_loop_node-0.18.1/learning_loop_node/node.py +284 -0
  47. learning_loop_node-0.18.1/learning_loop_node/py.typed +0 -0
  48. learning_loop_node-0.18.1/learning_loop_node/rest.py +58 -0
  49. learning_loop_node-0.18.1/learning_loop_node/tests/__init__.py +0 -0
  50. learning_loop_node-0.18.1/learning_loop_node/tests/annotator/__init__.py +0 -0
  51. learning_loop_node-0.18.1/learning_loop_node/tests/annotator/conftest.py +55 -0
  52. learning_loop_node-0.18.1/learning_loop_node/tests/annotator/pytest.ini +10 -0
  53. learning_loop_node-0.18.1/learning_loop_node/tests/annotator/test_annotator_node.py +60 -0
  54. learning_loop_node-0.18.1/learning_loop_node/tests/detector/__init__.py +0 -0
  55. learning_loop_node-0.18.1/learning_loop_node/tests/detector/conftest.py +174 -0
  56. learning_loop_node-0.18.1/learning_loop_node/tests/detector/inbox_filter/__init__.py +0 -0
  57. learning_loop_node-0.18.1/learning_loop_node/tests/detector/inbox_filter/test_observation.py +31 -0
  58. learning_loop_node-0.18.1/learning_loop_node/tests/detector/inbox_filter/test_relevance_group.py +162 -0
  59. learning_loop_node-0.18.1/learning_loop_node/tests/detector/inbox_filter/test_unexpected_observations_count.py +35 -0
  60. learning_loop_node-0.18.1/learning_loop_node/tests/detector/pytest.ini +10 -0
  61. learning_loop_node-0.18.1/learning_loop_node/tests/detector/test.jpg +0 -0
  62. learning_loop_node-0.18.1/learning_loop_node/tests/detector/test_client_communication.py +177 -0
  63. learning_loop_node-0.18.1/learning_loop_node/tests/detector/test_detector_node.py +98 -0
  64. learning_loop_node-0.18.1/learning_loop_node/tests/detector/test_outbox.py +77 -0
  65. learning_loop_node-0.18.1/learning_loop_node/tests/detector/test_relevance_filter.py +43 -0
  66. learning_loop_node-0.18.1/learning_loop_node/tests/detector/testing_detector.py +28 -0
  67. learning_loop_node-0.18.1/learning_loop_node/tests/general/__init__.py +0 -0
  68. learning_loop_node-0.18.1/learning_loop_node/tests/general/conftest.py +68 -0
  69. learning_loop_node-0.18.1/learning_loop_node/tests/general/pytest.ini +10 -0
  70. learning_loop_node-0.18.1/learning_loop_node/tests/general/test_data/model.json +6 -0
  71. learning_loop_node-0.18.1/learning_loop_node/tests/general/test_data_classes.py +22 -0
  72. learning_loop_node-0.18.1/learning_loop_node/tests/general/test_downloader.py +84 -0
  73. learning_loop_node-0.18.1/learning_loop_node/tests/general/test_learning_loop_node.py +20 -0
  74. learning_loop_node-0.18.1/learning_loop_node/tests/test_helper.py +96 -0
  75. learning_loop_node-0.18.1/learning_loop_node/tests/trainer/__init__.py +0 -0
  76. learning_loop_node-0.18.1/learning_loop_node/tests/trainer/conftest.py +101 -0
  77. learning_loop_node-0.18.1/learning_loop_node/tests/trainer/pytest.ini +10 -0
  78. learning_loop_node-0.18.1/learning_loop_node/tests/trainer/state_helper.py +22 -0
  79. learning_loop_node-0.18.1/learning_loop_node/tests/trainer/states/__init__.py +0 -0
  80. learning_loop_node-0.18.1/learning_loop_node/tests/trainer/states/test_state_cleanup.py +27 -0
  81. learning_loop_node-0.18.1/learning_loop_node/tests/trainer/states/test_state_detecting.py +83 -0
  82. learning_loop_node-0.18.1/learning_loop_node/tests/trainer/states/test_state_download_train_model.py +67 -0
  83. learning_loop_node-0.18.1/learning_loop_node/tests/trainer/states/test_state_prepare.py +55 -0
  84. learning_loop_node-0.18.1/learning_loop_node/tests/trainer/states/test_state_sync_confusion_matrix.py +117 -0
  85. learning_loop_node-0.18.1/learning_loop_node/tests/trainer/states/test_state_train.py +65 -0
  86. learning_loop_node-0.18.1/learning_loop_node/tests/trainer/states/test_state_upload_detections.py +164 -0
  87. learning_loop_node-0.18.1/learning_loop_node/tests/trainer/states/test_state_upload_model.py +88 -0
  88. learning_loop_node-0.18.1/learning_loop_node/tests/trainer/test_errors.py +45 -0
  89. learning_loop_node-0.18.1/learning_loop_node/tests/trainer/test_trainer_states.py +39 -0
  90. learning_loop_node-0.18.1/learning_loop_node/tests/trainer/testing_trainer_logic.py +94 -0
  91. learning_loop_node-0.18.1/learning_loop_node/trainer/__init__.py +0 -0
  92. learning_loop_node-0.18.1/learning_loop_node/trainer/downloader.py +31 -0
  93. learning_loop_node-0.18.1/learning_loop_node/trainer/exceptions.py +12 -0
  94. learning_loop_node-0.18.1/learning_loop_node/trainer/executor.py +109 -0
  95. learning_loop_node-0.18.1/learning_loop_node/trainer/io_helpers.py +181 -0
  96. learning_loop_node-0.18.1/learning_loop_node/trainer/rest/__init__.py +0 -0
  97. learning_loop_node-0.18.1/learning_loop_node/trainer/rest/backdoor_controls.py +120 -0
  98. learning_loop_node-0.18.1/learning_loop_node/trainer/test_executor.py +66 -0
  99. learning_loop_node-0.18.1/learning_loop_node/trainer/trainer_logic.py +192 -0
  100. learning_loop_node-0.18.1/learning_loop_node/trainer/trainer_logic_generic.py +560 -0
  101. learning_loop_node-0.18.1/learning_loop_node/trainer/trainer_node.py +106 -0
  102. {learning_loop_node-0.3.2 → learning_loop_node-0.18.1}/pyproject.toml +25 -10
  103. learning_loop_node-0.3.2/PKG-INFO +0 -112
  104. learning_loop_node-0.3.2/README.md +0 -80
  105. learning_loop_node-0.3.2/learning_loop_node/__init__.py +0 -8
  106. learning_loop_node-0.3.2/learning_loop_node/conftest.py +0 -21
  107. learning_loop_node-0.3.2/learning_loop_node/context.py +0 -6
  108. learning_loop_node-0.3.2/learning_loop_node/converter/converter.py +0 -45
  109. learning_loop_node-0.3.2/learning_loop_node/converter/converter_node.py +0 -87
  110. learning_loop_node-0.3.2/learning_loop_node/converter/model_information.py +0 -18
  111. learning_loop_node-0.3.2/learning_loop_node/detector/about.py +0 -19
  112. learning_loop_node-0.3.2/learning_loop_node/detector/detector_node.py +0 -12
  113. learning_loop_node-0.3.2/learning_loop_node/loop.py +0 -95
  114. learning_loop_node-0.3.2/learning_loop_node/node.py +0 -115
  115. learning_loop_node-0.3.2/learning_loop_node/node_helper.py +0 -106
  116. learning_loop_node-0.3.2/learning_loop_node/singleton.py +0 -8
  117. learning_loop_node-0.3.2/learning_loop_node/status.py +0 -24
  118. learning_loop_node-0.3.2/learning_loop_node/tests/test_detector.py +0 -7
  119. learning_loop_node-0.3.2/learning_loop_node/tests/test_helper.py +0 -30
  120. learning_loop_node-0.3.2/learning_loop_node/tests/test_learning_loop_node.py +0 -16
  121. learning_loop_node-0.3.2/learning_loop_node/trainer/basic_data.py +0 -7
  122. learning_loop_node-0.3.2/learning_loop_node/trainer/capability.py +0 -5
  123. learning_loop_node-0.3.2/learning_loop_node/trainer/downloader.py +0 -74
  124. learning_loop_node-0.3.2/learning_loop_node/trainer/downloader_factory.py +0 -16
  125. learning_loop_node-0.3.2/learning_loop_node/trainer/error_configuration.py +0 -11
  126. learning_loop_node-0.3.2/learning_loop_node/trainer/model.py +0 -16
  127. learning_loop_node-0.3.2/learning_loop_node/trainer/tests/test_downloader.py +0 -75
  128. learning_loop_node-0.3.2/learning_loop_node/trainer/tests/trainer_test_helper.py +0 -28
  129. learning_loop_node-0.3.2/learning_loop_node/trainer/trainer.py +0 -76
  130. learning_loop_node-0.3.2/learning_loop_node/trainer/trainer_node.py +0 -145
  131. learning_loop_node-0.3.2/learning_loop_node/trainer/training.py +0 -24
  132. learning_loop_node-0.3.2/learning_loop_node/trainer/training_data.py +0 -17
  133. learning_loop_node-0.3.2/setup.py +0 -47
  134. {learning_loop_node-0.3.2/learning_loop_node/tests → learning_loop_node-0.18.1/learning_loop_node/annotation}/__init__.py +0 -0
  135. {learning_loop_node-0.3.2/learning_loop_node/trainer/tests → learning_loop_node-0.18.1/learning_loop_node/tests/general}/test_data/file_1.txt +0 -0
  136. {learning_loop_node-0.3.2/learning_loop_node/trainer/tests → learning_loop_node-0.18.1/learning_loop_node/tests/general}/test_data/file_2.txt +0 -0
@@ -0,0 +1,283 @@
1
+ Metadata-Version: 2.1
2
+ Name: learning-loop-node
3
+ Version: 0.18.1
4
+ Summary: Python Library for Nodes which connect to the Zauberzeug Learning Loop
5
+ Home-page: https://github.com/zauberzeug/learning_loop_node
6
+ License: MIT
7
+ Author: Zauberzeug GmbH
8
+ Author-email: info@zauberzeug.com
9
+ Requires-Python: >=3.8,<4.0
10
+ Classifier: License :: OSI Approved :: MIT License
11
+ Classifier: Programming Language :: Python :: 3
12
+ Classifier: Programming Language :: Python :: 3.8
13
+ Classifier: Programming Language :: Python :: 3.9
14
+ Classifier: Programming Language :: Python :: 3.10
15
+ Classifier: Programming Language :: Python :: 3.11
16
+ Requires-Dist: Pillow (>=10.0.0,<11.0.0)
17
+ Requires-Dist: aiofiles (>=0.7.0,<0.8.0)
18
+ Requires-Dist: aiohttp (>=3.8.4,<4.0.0)
19
+ Requires-Dist: async_generator (>=1.10,<2.0)
20
+ Requires-Dist: dacite (>=1.8.1,<2.0.0)
21
+ Requires-Dist: fastapi (>=0.93,<0.109)
22
+ Requires-Dist: fastapi-socketio (>=0.0.10,<0.0.11)
23
+ Requires-Dist: fastapi-utils (>=0.2.1,<0.3.0)
24
+ Requires-Dist: httpx (>=0.28.1,<0.29.0)
25
+ Requires-Dist: icecream (>=2.1.0,<3.0.0)
26
+ Requires-Dist: numpy (>=1.13.3,<2.0.0)
27
+ Requires-Dist: psutil (>=5.8.0,<6.0.0)
28
+ Requires-Dist: pynvml (>=11.4.1,<12.0.0)
29
+ Requires-Dist: pytest-mock (==3.6.1)
30
+ Requires-Dist: pytest-watch (>=4.2.0,<5.0.0)
31
+ Requires-Dist: python-multipart (>=0.0.5,<0.0.6)
32
+ Requires-Dist: python-socketio (>=5.7.2,<6.0.0)
33
+ Requires-Dist: requests (>=2.25.1,<3.0.0)
34
+ Requires-Dist: simplejson (>=3.17.2,<4.0.0)
35
+ Requires-Dist: tqdm (>=4.63.0,<5.0.0)
36
+ Requires-Dist: uvicorn[standard] (>=0.22.0,<0.23.0)
37
+ Requires-Dist: werkzeug (>=2.0.1,<3.0.0)
38
+ Project-URL: Repository, https://github.com/zauberzeug/learning_loop_node
39
+ Description-Content-Type: text/markdown
40
+
41
+ # Learning Loop Node
42
+
43
+ This Python library helps to write Nodes that interact with the Zauberzeug Learning Loop. There are 4 types of Nodes:
44
+
45
+ | Type | Purpose |
46
+ | --------- | ---------------------------------------------------- |
47
+ | Trainer | Runs training using the latest training data |
48
+ | Detector | Loads latest models from Loop and performs inference |
49
+ | Annotator | Used for custom annotation inside the Loop |
50
+ | Converter | Converts between different model formats |
51
+
52
+ ## General Usage
53
+
54
+ To start a node you have to implement the logic by inheriting from the corresponding base logic class. We provide samples in the 'mock' folders and recommend to follow that scheme. A complete trainer and detector example can be found [here](https://github.com/zauberzeug/yolov5_node).
55
+
56
+ #### Environment variables
57
+
58
+ You can configure connection to our Learning Loop by specifying the following environment variables before starting:
59
+
60
+ | Name | Alias | Purpose | Required by |
61
+ | ------------------------ | ------------ | ------------------------------------------------------------ | ------------------------- |
62
+ | LOOP_HOST | HOST | Learning Loop address (e.g. learning-loop.ai) | all |
63
+ | LOOP_USERNAME | USERNAME | Learning Loop user name | all besides Detector |
64
+ | LOOP_PASSWORD | PASSWORD | Learning Loop password | all besides Detector |
65
+ | LOOP_SSL_CERT_PATH | - | Path to the SSL certificate | all (opt.) |
66
+ | LOOP_ORGANIZATION | ORGANIZATION | Organization ID | Detector |
67
+ | LOOP_PROJECT | PROJECT | Project ID | Detector (opt.) |
68
+ | MIN_UNCERTAIN_THRESHOLD | - | smallest confidence (float) at which auto-upload will happen | Detector (opt.) |
69
+ | MAX_UNCERTAIN_THRESHOLD | - | largest confidence (float) at which auto-upload will happen | Detector (opt.) |
70
+ | INFERENCE_BATCH_SIZE | - | Batch size of trainer when calculating detections | Trainer (opt.) |
71
+ | RESTART_AFTER_TRAINING | - | Restart the trainer after training (set to 1) | Trainer (opt.) |
72
+ | KEEP_OLD_TRAININGS | - | Do not delete old trainings (set to 1) | Trainer (opt.) |
73
+ | TRAINER_IDLE_TIMEOUT_SEC | - | Automatically shutdown trainer after timeout (in seconds) | Trainer (opt.) |
74
+ | USE_BACKDOOR_CONTROLS | - | Always enable backdoor controls (set to 1) | Trainer / Detector (opt.) |
75
+
76
+ Note that organization and project IDs are always lower case and may differ from the names in the Learning Loop which can have uppercase letters.
77
+
78
+ #### Testing
79
+
80
+ We use github actions for CI. Tests can also be executed locally by running
81
+ `LOOP_HOST=XXXXXXXX LOOP_USERNAME=XXXXXXXX LOOP_PASSWORD=XXXXXXXX python -m pytest -v`
82
+ from learning_loop_node/learning_loop_node
83
+
84
+ ## Detector Node
85
+
86
+ Detector Nodes are normally deployed on edge devices like robots or machinery but can also run in the cloud to provide backend services for an app or similar. These nodes register themself at the Learning Loop. They provide REST and Socket.io APIs to run inference on images. The processed images can automatically be used for active learning: e.g. uncertain predictions will be send to the Learning Loop.
87
+
88
+ ### Inference API
89
+
90
+ Images can be send to the detector node via socketio or rest.
91
+ Via **REST** you may provide the following parameters:
92
+
93
+ - `camera_id`: a camera identifier (string) used to improve the autoupload filtering
94
+ - `tags`: comma separated list of tags to add to the image in the learning loop to add to the image in the learning loop
95
+ - `source`: optional source identifier (str) for the image (e.g. a robot id)
96
+ - `autoupload`: configures auto-submission to the learning loop; `filtered` (default), `all`, `disabled`
97
+ - `creation_date`: optional creation date (str) for the image in isoformat (e.g. `2023-01-30T12:34:56`)
98
+
99
+ Example usage:
100
+
101
+ `curl --request POST -F 'file=@test.jpg' -H 'autoupload: all' -H 'camera_id: front_cam' localhost:8004/detect`
102
+
103
+ To use the **SocketIO** inference EPs, the caller needs to connect to the detector node's SocketIO server and emit the `detect` or `batch_detect` event with the image data and image metadata. The `detect` endpoint receives a dictionary, with the following entries:
104
+
105
+ - `image`: The image data as dictionary with the following keys:
106
+ - `bytes`: bytes of the ndarray (retrieved via `ndarray.tobytes(order='C')`)
107
+ - `dtype`: data type of the ndarray as string (e.g. `uint8`, `float32`, etc.)
108
+ - `shape`: shape of the ndarray as tuple of ints (e.g. `(480, 640, 3)`)
109
+ - `camera_id`: optional camera identifier (string) used to improve the autoupload filtering
110
+ - `tags`: optional list of tags to add to the image in the learning loop
111
+ - `source`: optional source string
112
+ - `autoupload`: configures auto-submission to the learning loop; `filtered` (default), `all`, `disabled`
113
+ - `creation_date`: optional creation date (str) for the image in isoformat (e.g. `2023-01-30T12:34:56`)
114
+
115
+ The `batch_detect` endpoint receives a dictionary, with the same entries as the `detect` endpoint, except that the `image` entry is replaced by:
116
+
117
+ - `images`: List of image data dictionaries, each with the same structure as the `image` entry in the `detect` endpoint
118
+
119
+ Example code can be found [in the rosys implementation](https://github.com/zauberzeug/rosys/blob/main/rosys/vision/detector_hardware.py).
120
+
121
+ ### Upload API
122
+
123
+ The detector has a **REST** endpoint to upload images (and detections) to the Learning Loop. The endpoint takes a POST request with one or multiple images. The images are expected to be in jpg format. The following optional parameters may be set via headers:
124
+
125
+ - `source`: optional source identifier (str) for the image (e.g. a robot id)
126
+ - `creation_date`: optional creation date (str) for the image in isoformat (e.g. `2023-01-30T12:34:56`)
127
+ - `upload_priority`: A boolean flag to prioritize the upload (defaults to False)
128
+
129
+ Example:
130
+
131
+ `curl -X POST -F 'files=@test.jpg' "http://localhost:/upload"`
132
+
133
+ The detector also has a **SocketIO** upload endpoint that can be used to upload images and detections to the learning loop. The function receives a dictionary, with the following entries:
134
+
135
+ - `image`: the image data as dictionary with the following keys:
136
+ - `bytes`: bytes of the ndarray (retrieved via `ndarray.tobytes(order='C')`)
137
+ - `dtype`: data type of the ndarray as string (e.g. `uint8`, `float32`, etc.)
138
+ - `shape`: shape of the ndarray as tuple of ints (e.g. `(480, 640, 3)`)
139
+ - `metadata`: a dictionary representing the image metadata. If metadata contains detections and/or annotations, UUIDs for the classes are automatically determined based on the category names. Metadata should follow the schema of the `ImageMetadata` data class.
140
+ - `upload_priority`: Optional boolean flag to prioritize the upload (defaults to False)
141
+
142
+ The endpoint returns None if the upload was successful and an error message otherwise.
143
+
144
+ For both ways to upload an image, the tag `picked_by_system` is automatically added to the image metadata.
145
+
146
+ ### Changing the model versioning mode
147
+
148
+ The detector can be configured to one of the following behaviors:
149
+
150
+ - use a specific model version
151
+ - automatically update the model version according to the learning loop deployment target
152
+ - pause the model updates and use the version that was last loaded
153
+
154
+ The model versioning configuration can be accessed/changed via a REST endpoint. Example Usage:
155
+
156
+ - Fetch the current model versioning configuration: `curl http://localhost/model_version`
157
+ - Configure the detector to use a specific model version: `curl -X PUT -d "1.0" http://localhost/model_version`
158
+ - Configure the detector to automatically update the model version: `curl -X PUT -d "follow_loop" http://localhost/model_version`
159
+ - Pause the model updates: `curl -X PUT -d "pause" http://localhost/model_version`
160
+
161
+ Note that the configuration is not persistent, however, the default behavior on startup can be configured via the environment variable `VERSION_CONTROL_DEFAULT`.
162
+ If the environment variable is set to `VERSION_CONTROL_DEFAULT=PAUSE`, the detector will pause the model updates on startup. Otherwise, the detector will automatically follow the loop deployment target.
163
+
164
+ The model versioning configuration can also be changed via a socketio event:
165
+
166
+ - Configure the detector to use a specific model version: `sio.emit('set_model_version_mode', '1.0')`
167
+ - Configure the detector to automatically update the model version: `sio.emit('set_model_version_mode', 'follow_loop')`
168
+ - Pause the model updates: `sio.emit('set_model_version_mode', 'pause')`
169
+
170
+ There is also a GET endpoint to fetch the current model versioning configuration:
171
+ `sio.emit('get_model_version')` or `curl http://localhost/model_version`
172
+
173
+ ### Changing the outbox mode
174
+
175
+ If the autoupload is set to `all` or `filtered` (selected) images and the corresponding detections are saved on HDD (the outbox). A background thread will upload the images and detections to the Learning Loop. The outbox is located in the `outbox` folder in the root directory of the node. The outbox can be cleared by deleting the files in the folder.
176
+
177
+ The continuous upload can be stopped/started via a REST enpoint:
178
+
179
+ Example Usage:
180
+
181
+ - Enable upload: `curl -X PUT -d "continuous_upload" http://localhost/outbox_mode`
182
+ - Disable upload: `curl -X PUT -d "stopped" http://localhost/outbox_mode`
183
+
184
+ The current state can be queried via a GET request:
185
+ `curl http://localhost/outbox_mode`
186
+
187
+ Alternatively, the outbox mode can be changed via a socketio event:
188
+
189
+ - Enable upload: `sio.emit('set_outbox_mode', 'continuous_upload')`
190
+ - Disable upload: `sio.emit('set_outbox_mode', 'stopped')`
191
+
192
+ The outbox mode can also be queried via:
193
+
194
+ - HTTP: `curl http://localhost/outbox_mode`
195
+ - SocketIO: `sio.emit('get_outbox_mode')`
196
+
197
+ ## Trainer Node
198
+
199
+ Trainers fetch the images and anntoations from the Learning Loop to train new models.
200
+
201
+ - if the command line tool "jpeginfo" is installed, the downloader will drop corrupted images automatically
202
+
203
+ ## Converter Node
204
+
205
+ A Conveter Node converts models from one format into another.
206
+
207
+ ## Annotator Node
208
+
209
+ ...
210
+
211
+ ### Test operability
212
+
213
+ Assumend there is a Converter Node which converts models of format 'format_a' into 'format_b'.
214
+ Upload a model with
215
+ `curl --request POST -F 'files=@my_model.zip' https://learning-loop.ai/api/zauberzeug/projects/demo/format_a`
216
+ The model should now be available for the format 'format_a'
217
+ `curl "https://learning-loop.ai/api/zauberzeug/projects/demo/models?format=format_a"`
218
+
219
+ ```json
220
+ {
221
+ "models": [
222
+ {
223
+ "id": "3c20d807-f71c-40dc-a996-8a8968aa5431",
224
+ "version": "4.0",
225
+ "formats": [
226
+ "format_a"
227
+ ],
228
+ "created": "2021-06-01T06:28:21.289092",
229
+ "comment": "uploaded at 2021-06-01 06:28:21.288442",
230
+ ...
231
+ }
232
+ ]
233
+ }
234
+ ```
235
+
236
+ but not in the format_b
237
+ `curl "https://learning-loop.ai/api/zauberzeug/projects/demo/models?format=format_b"`
238
+
239
+ ```json
240
+ {
241
+ "models": []
242
+ }
243
+ ```
244
+
245
+ Connect the Node to the Learning Loop by simply starting the container.
246
+ After a short time the converted model should be available as well.
247
+ `curl https://learning-loop.ai/api/zauberzeug/projects/demo/models?format=format_b`
248
+
249
+ ```json
250
+ {
251
+ "models": [
252
+ {
253
+ "id": "3c20d807-f71c-40dc-a996-8a8968aa5431",
254
+ "version": "4.0",
255
+ "formats": [
256
+ "format_a",
257
+ "format_b",
258
+ ],
259
+ "created": "2021-06-01T06:28:21.289092",
260
+ "comment": "uploaded at 2021-06-01 06:28:21.288442",
261
+ ...
262
+ }
263
+ ]
264
+ }
265
+ ```
266
+
267
+ ## About Models (the currency between Nodes)
268
+
269
+ - Models are packed in zips and saved on the Learning Loop (one for each format)
270
+ - Nodes and users can upload and download models with which they want to work
271
+ - In each zip there is a file called `model.json` which contains the metadata to interpret the other files in the package
272
+ - for base models (pretrained models from external sources) no `model.json` has to be sent, ie. these models should simply be zipped in such a way that the respective trainer can work with them.
273
+ - the loop adds or corrects the following properties in the `model.json` after receiving; it also creates the file if it is missing:
274
+ - `host`: uri to the loop
275
+ - `organization`: the ID of the organization
276
+ - `project`: the id of the project
277
+ - `version`: the version number that the loop assigned for this model (e.g. 1.3)
278
+ - `id`: the model UUID (currently not needed by anyone, since host, org, project, version clearly identify the model)
279
+ - `format`: the format e.g. yolo, tkdnn, yolor etc.
280
+ - Nodes add properties to `model.json`, which contains all the information which are needed by subsequent nodes. These are typically the properties:
281
+ - `resolution`: resolution in which the model expects images (as `int`, since the resolution is mostly square - later, ` resolution_x`` resolution_y ` would also be conceivable or `resolutions` to give a list of possible resolutions)
282
+ - `categories`: list of categories with name, id, (later also type), in the order in which they are used by the model -- this is neccessary to be robust about renamings
283
+
@@ -0,0 +1,242 @@
1
+ # Learning Loop Node
2
+
3
+ This Python library helps to write Nodes that interact with the Zauberzeug Learning Loop. There are 4 types of Nodes:
4
+
5
+ | Type | Purpose |
6
+ | --------- | ---------------------------------------------------- |
7
+ | Trainer | Runs training using the latest training data |
8
+ | Detector | Loads latest models from Loop and performs inference |
9
+ | Annotator | Used for custom annotation inside the Loop |
10
+ | Converter | Converts between different model formats |
11
+
12
+ ## General Usage
13
+
14
+ To start a node you have to implement the logic by inheriting from the corresponding base logic class. We provide samples in the 'mock' folders and recommend to follow that scheme. A complete trainer and detector example can be found [here](https://github.com/zauberzeug/yolov5_node).
15
+
16
+ #### Environment variables
17
+
18
+ You can configure connection to our Learning Loop by specifying the following environment variables before starting:
19
+
20
+ | Name | Alias | Purpose | Required by |
21
+ | ------------------------ | ------------ | ------------------------------------------------------------ | ------------------------- |
22
+ | LOOP_HOST | HOST | Learning Loop address (e.g. learning-loop.ai) | all |
23
+ | LOOP_USERNAME | USERNAME | Learning Loop user name | all besides Detector |
24
+ | LOOP_PASSWORD | PASSWORD | Learning Loop password | all besides Detector |
25
+ | LOOP_SSL_CERT_PATH | - | Path to the SSL certificate | all (opt.) |
26
+ | LOOP_ORGANIZATION | ORGANIZATION | Organization ID | Detector |
27
+ | LOOP_PROJECT | PROJECT | Project ID | Detector (opt.) |
28
+ | MIN_UNCERTAIN_THRESHOLD | - | smallest confidence (float) at which auto-upload will happen | Detector (opt.) |
29
+ | MAX_UNCERTAIN_THRESHOLD | - | largest confidence (float) at which auto-upload will happen | Detector (opt.) |
30
+ | INFERENCE_BATCH_SIZE | - | Batch size of trainer when calculating detections | Trainer (opt.) |
31
+ | RESTART_AFTER_TRAINING | - | Restart the trainer after training (set to 1) | Trainer (opt.) |
32
+ | KEEP_OLD_TRAININGS | - | Do not delete old trainings (set to 1) | Trainer (opt.) |
33
+ | TRAINER_IDLE_TIMEOUT_SEC | - | Automatically shutdown trainer after timeout (in seconds) | Trainer (opt.) |
34
+ | USE_BACKDOOR_CONTROLS | - | Always enable backdoor controls (set to 1) | Trainer / Detector (opt.) |
35
+
36
+ Note that organization and project IDs are always lower case and may differ from the names in the Learning Loop which can have uppercase letters.
37
+
38
+ #### Testing
39
+
40
+ We use github actions for CI. Tests can also be executed locally by running
41
+ `LOOP_HOST=XXXXXXXX LOOP_USERNAME=XXXXXXXX LOOP_PASSWORD=XXXXXXXX python -m pytest -v`
42
+ from learning_loop_node/learning_loop_node
43
+
44
+ ## Detector Node
45
+
46
+ Detector Nodes are normally deployed on edge devices like robots or machinery but can also run in the cloud to provide backend services for an app or similar. These nodes register themself at the Learning Loop. They provide REST and Socket.io APIs to run inference on images. The processed images can automatically be used for active learning: e.g. uncertain predictions will be send to the Learning Loop.
47
+
48
+ ### Inference API
49
+
50
+ Images can be send to the detector node via socketio or rest.
51
+ Via **REST** you may provide the following parameters:
52
+
53
+ - `camera_id`: a camera identifier (string) used to improve the autoupload filtering
54
+ - `tags`: comma separated list of tags to add to the image in the learning loop to add to the image in the learning loop
55
+ - `source`: optional source identifier (str) for the image (e.g. a robot id)
56
+ - `autoupload`: configures auto-submission to the learning loop; `filtered` (default), `all`, `disabled`
57
+ - `creation_date`: optional creation date (str) for the image in isoformat (e.g. `2023-01-30T12:34:56`)
58
+
59
+ Example usage:
60
+
61
+ `curl --request POST -F 'file=@test.jpg' -H 'autoupload: all' -H 'camera_id: front_cam' localhost:8004/detect`
62
+
63
+ To use the **SocketIO** inference EPs, the caller needs to connect to the detector node's SocketIO server and emit the `detect` or `batch_detect` event with the image data and image metadata. The `detect` endpoint receives a dictionary, with the following entries:
64
+
65
+ - `image`: The image data as dictionary with the following keys:
66
+ - `bytes`: bytes of the ndarray (retrieved via `ndarray.tobytes(order='C')`)
67
+ - `dtype`: data type of the ndarray as string (e.g. `uint8`, `float32`, etc.)
68
+ - `shape`: shape of the ndarray as tuple of ints (e.g. `(480, 640, 3)`)
69
+ - `camera_id`: optional camera identifier (string) used to improve the autoupload filtering
70
+ - `tags`: optional list of tags to add to the image in the learning loop
71
+ - `source`: optional source string
72
+ - `autoupload`: configures auto-submission to the learning loop; `filtered` (default), `all`, `disabled`
73
+ - `creation_date`: optional creation date (str) for the image in isoformat (e.g. `2023-01-30T12:34:56`)
74
+
75
+ The `batch_detect` endpoint receives a dictionary, with the same entries as the `detect` endpoint, except that the `image` entry is replaced by:
76
+
77
+ - `images`: List of image data dictionaries, each with the same structure as the `image` entry in the `detect` endpoint
78
+
79
+ Example code can be found [in the rosys implementation](https://github.com/zauberzeug/rosys/blob/main/rosys/vision/detector_hardware.py).
80
+
81
+ ### Upload API
82
+
83
+ The detector has a **REST** endpoint to upload images (and detections) to the Learning Loop. The endpoint takes a POST request with one or multiple images. The images are expected to be in jpg format. The following optional parameters may be set via headers:
84
+
85
+ - `source`: optional source identifier (str) for the image (e.g. a robot id)
86
+ - `creation_date`: optional creation date (str) for the image in isoformat (e.g. `2023-01-30T12:34:56`)
87
+ - `upload_priority`: A boolean flag to prioritize the upload (defaults to False)
88
+
89
+ Example:
90
+
91
+ `curl -X POST -F 'files=@test.jpg' "http://localhost:/upload"`
92
+
93
+ The detector also has a **SocketIO** upload endpoint that can be used to upload images and detections to the learning loop. The function receives a dictionary, with the following entries:
94
+
95
+ - `image`: the image data as dictionary with the following keys:
96
+ - `bytes`: bytes of the ndarray (retrieved via `ndarray.tobytes(order='C')`)
97
+ - `dtype`: data type of the ndarray as string (e.g. `uint8`, `float32`, etc.)
98
+ - `shape`: shape of the ndarray as tuple of ints (e.g. `(480, 640, 3)`)
99
+ - `metadata`: a dictionary representing the image metadata. If metadata contains detections and/or annotations, UUIDs for the classes are automatically determined based on the category names. Metadata should follow the schema of the `ImageMetadata` data class.
100
+ - `upload_priority`: Optional boolean flag to prioritize the upload (defaults to False)
101
+
102
+ The endpoint returns None if the upload was successful and an error message otherwise.
103
+
104
+ For both ways to upload an image, the tag `picked_by_system` is automatically added to the image metadata.
105
+
106
+ ### Changing the model versioning mode
107
+
108
+ The detector can be configured to one of the following behaviors:
109
+
110
+ - use a specific model version
111
+ - automatically update the model version according to the learning loop deployment target
112
+ - pause the model updates and use the version that was last loaded
113
+
114
+ The model versioning configuration can be accessed/changed via a REST endpoint. Example Usage:
115
+
116
+ - Fetch the current model versioning configuration: `curl http://localhost/model_version`
117
+ - Configure the detector to use a specific model version: `curl -X PUT -d "1.0" http://localhost/model_version`
118
+ - Configure the detector to automatically update the model version: `curl -X PUT -d "follow_loop" http://localhost/model_version`
119
+ - Pause the model updates: `curl -X PUT -d "pause" http://localhost/model_version`
120
+
121
+ Note that the configuration is not persistent, however, the default behavior on startup can be configured via the environment variable `VERSION_CONTROL_DEFAULT`.
122
+ If the environment variable is set to `VERSION_CONTROL_DEFAULT=PAUSE`, the detector will pause the model updates on startup. Otherwise, the detector will automatically follow the loop deployment target.
123
+
124
+ The model versioning configuration can also be changed via a socketio event:
125
+
126
+ - Configure the detector to use a specific model version: `sio.emit('set_model_version_mode', '1.0')`
127
+ - Configure the detector to automatically update the model version: `sio.emit('set_model_version_mode', 'follow_loop')`
128
+ - Pause the model updates: `sio.emit('set_model_version_mode', 'pause')`
129
+
130
+ There is also a GET endpoint to fetch the current model versioning configuration:
131
+ `sio.emit('get_model_version')` or `curl http://localhost/model_version`
132
+
133
+ ### Changing the outbox mode
134
+
135
+ If the autoupload is set to `all` or `filtered` (selected) images and the corresponding detections are saved on HDD (the outbox). A background thread will upload the images and detections to the Learning Loop. The outbox is located in the `outbox` folder in the root directory of the node. The outbox can be cleared by deleting the files in the folder.
136
+
137
+ The continuous upload can be stopped/started via a REST enpoint:
138
+
139
+ Example Usage:
140
+
141
+ - Enable upload: `curl -X PUT -d "continuous_upload" http://localhost/outbox_mode`
142
+ - Disable upload: `curl -X PUT -d "stopped" http://localhost/outbox_mode`
143
+
144
+ The current state can be queried via a GET request:
145
+ `curl http://localhost/outbox_mode`
146
+
147
+ Alternatively, the outbox mode can be changed via a socketio event:
148
+
149
+ - Enable upload: `sio.emit('set_outbox_mode', 'continuous_upload')`
150
+ - Disable upload: `sio.emit('set_outbox_mode', 'stopped')`
151
+
152
+ The outbox mode can also be queried via:
153
+
154
+ - HTTP: `curl http://localhost/outbox_mode`
155
+ - SocketIO: `sio.emit('get_outbox_mode')`
156
+
157
+ ## Trainer Node
158
+
159
+ Trainers fetch the images and anntoations from the Learning Loop to train new models.
160
+
161
+ - if the command line tool "jpeginfo" is installed, the downloader will drop corrupted images automatically
162
+
163
+ ## Converter Node
164
+
165
+ A Conveter Node converts models from one format into another.
166
+
167
+ ## Annotator Node
168
+
169
+ ...
170
+
171
+ ### Test operability
172
+
173
+ Assumend there is a Converter Node which converts models of format 'format_a' into 'format_b'.
174
+ Upload a model with
175
+ `curl --request POST -F 'files=@my_model.zip' https://learning-loop.ai/api/zauberzeug/projects/demo/format_a`
176
+ The model should now be available for the format 'format_a'
177
+ `curl "https://learning-loop.ai/api/zauberzeug/projects/demo/models?format=format_a"`
178
+
179
+ ```json
180
+ {
181
+ "models": [
182
+ {
183
+ "id": "3c20d807-f71c-40dc-a996-8a8968aa5431",
184
+ "version": "4.0",
185
+ "formats": [
186
+ "format_a"
187
+ ],
188
+ "created": "2021-06-01T06:28:21.289092",
189
+ "comment": "uploaded at 2021-06-01 06:28:21.288442",
190
+ ...
191
+ }
192
+ ]
193
+ }
194
+ ```
195
+
196
+ but not in the format_b
197
+ `curl "https://learning-loop.ai/api/zauberzeug/projects/demo/models?format=format_b"`
198
+
199
+ ```json
200
+ {
201
+ "models": []
202
+ }
203
+ ```
204
+
205
+ Connect the Node to the Learning Loop by simply starting the container.
206
+ After a short time the converted model should be available as well.
207
+ `curl https://learning-loop.ai/api/zauberzeug/projects/demo/models?format=format_b`
208
+
209
+ ```json
210
+ {
211
+ "models": [
212
+ {
213
+ "id": "3c20d807-f71c-40dc-a996-8a8968aa5431",
214
+ "version": "4.0",
215
+ "formats": [
216
+ "format_a",
217
+ "format_b",
218
+ ],
219
+ "created": "2021-06-01T06:28:21.289092",
220
+ "comment": "uploaded at 2021-06-01 06:28:21.288442",
221
+ ...
222
+ }
223
+ ]
224
+ }
225
+ ```
226
+
227
+ ## About Models (the currency between Nodes)
228
+
229
+ - Models are packed in zips and saved on the Learning Loop (one for each format)
230
+ - Nodes and users can upload and download models with which they want to work
231
+ - In each zip there is a file called `model.json` which contains the metadata to interpret the other files in the package
232
+ - for base models (pretrained models from external sources) no `model.json` has to be sent, ie. these models should simply be zipped in such a way that the respective trainer can work with them.
233
+ - the loop adds or corrects the following properties in the `model.json` after receiving; it also creates the file if it is missing:
234
+ - `host`: uri to the loop
235
+ - `organization`: the ID of the organization
236
+ - `project`: the id of the project
237
+ - `version`: the version number that the loop assigned for this model (e.g. 1.3)
238
+ - `id`: the model UUID (currently not needed by anyone, since host, org, project, version clearly identify the model)
239
+ - `format`: the format e.g. yolo, tkdnn, yolor etc.
240
+ - Nodes add properties to `model.json`, which contains all the information which are needed by subsequent nodes. These are typically the properties:
241
+ - `resolution`: resolution in which the model expects images (as `int`, since the resolution is mostly square - later, ` resolution_x`` resolution_y ` would also be conceivable or `resolutions` to give a list of possible resolutions)
242
+ - `categories`: list of categories with name, id, (later also type), in the order in which they are used by the model -- this is neccessary to be robust about renamings
@@ -0,0 +1,11 @@
1
+ import logging
2
+
3
+ # from . import log_conf
4
+ from .detector.detector_logic import DetectorLogic
5
+ from .detector.detector_node import DetectorNode
6
+ from .globals import GLOBALS
7
+ from .trainer.trainer_node import TrainerNode
8
+
9
+ __all__ = ['TrainerNode', 'DetectorNode', 'DetectorLogic', 'GLOBALS']
10
+
11
+ logging.info('>>>>>>>>>>>>>>>>>> LOOP INITIALIZED <<<<<<<<<<<<<<<<<<<<<<<')
@@ -0,0 +1,29 @@
1
+ from abc import abstractmethod
2
+ from typing import Dict, Optional
3
+
4
+ from ..data_classes import ToolOutput, UserInput
5
+ from ..node import Node
6
+
7
+
8
+ class AnnotatorLogic():
9
+
10
+ def __init__(self) -> None:
11
+ self._node: Optional[Node] = None
12
+
13
+ def init(self, node: Node) -> None:
14
+ self._node = node
15
+
16
+ @abstractmethod
17
+ async def handle_user_input(self, user_input: UserInput, history: Dict) -> ToolOutput:
18
+ """ This method is called when a user input is received from the client.
19
+ The function should return a ToolOutput object."""
20
+
21
+ @abstractmethod
22
+ def create_empty_history(self) -> Dict:
23
+ """ This method is called when a new annotation session is started.
24
+ The function should return an empty history object."""
25
+
26
+ @abstractmethod
27
+ def logout_user(self, sid: str) -> bool:
28
+ """ This method is called when a user disconnects from the server.
29
+ The function should return True if the user was logged out successfully."""