-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy path__init__.py
More file actions
1315 lines (1145 loc) · 47.1 KB
/
__init__.py
File metadata and controls
1315 lines (1145 loc) · 47.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""Nucleus Python SDK. """
__all__ = [
"AsyncJob",
"EmbeddingsExportJob",
"BoxAnnotation",
"DeduplicationResult",
"DeduplicationStats",
"BoxPrediction",
"CameraParams",
"CategoryAnnotation",
"CategoryPrediction",
"CuboidAnnotation",
"CuboidPrediction",
"Dataset",
"DatasetInfo",
"DatasetItem",
"DatasetItemRetrievalError",
"Frame",
"Keypoint",
"KeypointsAnnotation",
"KeypointsPrediction",
"LidarPoint",
"LidarScene",
"LineAnnotation",
"LinePrediction",
"Model",
"ModelCreationError",
# "MultiCategoryAnnotation", # coming soon!
"NotFoundError",
"NucleusAPIError",
"NucleusClient",
"Point",
"Point3D",
"PolygonAnnotation",
"PolygonPrediction",
"Quaternion",
"SceneCategoryAnnotation",
"SceneCategoryPrediction",
"Segment",
"SegmentationAnnotation",
"SegmentationPrediction",
"Slice",
"VideoScene",
]
import datetime
import os
import warnings
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Tuple, Union
import requests
import tqdm
if TYPE_CHECKING:
# Backwards compatibility is even uglier with mypy
from pydantic.v1 import parse_obj_as
else:
try:
# NOTE: we always use pydantic v1 but have to do these shenanigans to support both v1 and v2
from pydantic.v1 import parse_obj_as
except ImportError:
from pydantic import parse_obj_as
from nucleus.url_utils import sanitize_string_args
from . import metrics
from .annotation import (
BoxAnnotation,
CategoryAnnotation,
CuboidAnnotation,
Keypoint,
KeypointsAnnotation,
LidarPoint,
LineAnnotation,
MultiCategoryAnnotation,
Point,
Point3D,
PolygonAnnotation,
SceneCategoryAnnotation,
Segment,
SegmentationAnnotation,
)
from .async_job import AsyncJob, EmbeddingsExportJob
from .async_utils import make_multiple_requests_concurrently
from .camera_params import CameraParams
from .connection import Connection
from .constants import (
ANNOTATION_METADATA_SCHEMA_KEY,
ANNOTATIONS_IGNORED_KEY,
ANNOTATIONS_PROCESSED_KEY,
AUTOTAGS_KEY,
DATASET_ID_KEY,
DATASET_IS_SCENE_KEY,
DATASET_PRIVACY_MODE_KEY,
DEFAULT_NETWORK_TIMEOUT_SEC,
EMBEDDING_DIMENSION_KEY,
EMBEDDINGS_URL_KEY,
ERROR_ITEMS,
ERROR_PAYLOAD,
ERRORS_KEY,
GLOB_SIZE_THRESHOLD_CHECK,
I_KEY,
IMAGE_KEY,
IMAGE_URL_KEY,
INDEX_CONTINUOUS_ENABLE_KEY,
ITEM_METADATA_SCHEMA_KEY,
ITEMS_KEY,
JOB_CREATION_TIME_KEY,
JOB_ID_KEY,
JOB_LAST_KNOWN_STATUS_KEY,
JOB_TYPE_KEY,
KEEP_HISTORY_KEY,
MESSAGE_KEY,
MODEL_RUN_ID_KEY,
MODEL_TAGS_KEY,
MODEL_TRAINED_SLICE_IDS_KEY,
NAME_KEY,
NUCLEUS_ENDPOINT,
POINTS_KEY,
PREDICTIONS_IGNORED_KEY,
PREDICTIONS_PROCESSED_KEY,
REFERENCE_IDS_KEY,
SLICE_ID_KEY,
SLICE_TAGS_KEY,
STATUS_CODE_KEY,
UPDATE_KEY,
)
from .data_transfer_object.dataset_details import DatasetDetails
from .data_transfer_object.dataset_info import DatasetInfo
from .data_transfer_object.job_status import JobInfoRequestPayload
from .dataset import Dataset
from .dataset_item import DatasetItem
from .deduplication import DeduplicationResult, DeduplicationStats
from .deprecation_warning import deprecated
from .errors import (
DatasetItemRetrievalError,
ModelCreationError,
ModelRunCreationError,
NoAPIKey,
NotFoundError,
NucleusAPIError,
)
from .job import CustomerJobTypes
from .model import Model
from .model_run import ModelRun
from .payload_constructor import (
construct_annotation_payload,
construct_append_payload,
construct_box_predictions_payload,
construct_model_creation_payload,
construct_segmentation_payload,
)
from .prediction import (
BoxPrediction,
CategoryPrediction,
CuboidPrediction,
KeypointsPrediction,
LinePrediction,
PolygonPrediction,
SceneCategoryPrediction,
SegmentationPrediction,
)
from .quaternion import Quaternion
from .retry_strategy import RetryStrategy
from .scene import Frame, LidarScene, VideoScene
from .slice import Slice
from .upload_response import UploadResponse
from .utils import create_items_from_folder_crawl
from .validate import Validate
# pylint: disable=E1101
# TODO: refactor to reduce this file to under 1000 lines.
# pylint: disable=C0302
class NucleusClient:
"""Client to interact with the Nucleus API via Python SDK.
Parameters:
api_key: One of ``api_key`` or ``limited_access_key`` must be provided; you cannot pass
both. For standard Scale API key authentication, pass the key here. Follow `this guide
<https://scale.com/docs/api-reference/authentication>`_ to retrieve API keys. If you omit
this argument and are not using ``limited_access_key``, the SDK falls back to the
``NUCLEUS_API_KEY`` environment variable.
limited_access_key: Nucleus-only API key for scoped access. Reach out to your Scale
representative to obtain a limited access key.
use_notebook: Whether the client is being used in a notebook (toggles tqdm
style). Default is ``False``.
endpoint: Base URL of the API. Default is Nucleus's current production API.
.. note::
You must provide **either** a standard Scale API key (``api_key``, or
``NUCLEUS_API_KEY`` in the environment) **or** a Nucleus-only key
(``limited_access_key``), never both. Passing both arguments, or setting
the environment variable ``NUCLEUS_API_KEY`` while also passing
``limited_access_key``, will raise an error.
Example::
# Using a basic auth key
import nucleus
client = nucleus.NucleusClient(api_key="YOUR_API_KEY", ...)
# Using only a limited access key (no Basic Auth)
import nucleus
client = nucleus.NucleusClient(limited_access_key="YOUR_LIMITED_KEY", ...)
"""
def __init__(
self,
api_key: Optional[str] = None,
use_notebook: bool = False,
endpoint: Optional[str] = None,
limited_access_key: Optional[str] = None,
):
effective_basic_key = (
api_key if api_key else os.environ.get("NUCLEUS_API_KEY")
)
if limited_access_key and effective_basic_key:
raise ValueError(
"Cannot provide both 'api_key' and 'limited_access_key'. "
"Use 'api_key' for standard Scale API key authentication, "
"or 'limited_access_key' for Nucleus-only access, but not both."
)
# Allow usage with only a limited access key
if api_key is None and limited_access_key:
self.api_key = None
else:
self.api_key = self._set_api_key(api_key)
self.tqdm_bar = tqdm.tqdm
if endpoint is None:
self.endpoint = os.environ.get(
"NUCLEUS_ENDPOINT", NUCLEUS_ENDPOINT
)
else:
self.endpoint = endpoint
self._use_notebook = use_notebook
if use_notebook:
import tqdm.notebook as tqdm_notebook
self.tqdm_bar = tqdm_notebook.tqdm
self.extra_headers: Dict[str, str] = {}
if limited_access_key:
self.extra_headers["x-limited-access-key"] = limited_access_key
self.connection = Connection(self.api_key, self.endpoint, extra_headers=self.extra_headers)
self.validate = Validate(self.api_key, self.endpoint, extra_headers=self.extra_headers)
def __repr__(self):
return f"NucleusClient(api_key='{self.api_key}', use_notebook={self._use_notebook}, endpoint='{self.endpoint}')"
def __eq__(self, other):
if self.api_key == other.api_key:
if self._use_notebook == other._use_notebook:
return True
return False
@property
def datasets(self) -> List[Dataset]:
"""List all Datasets
Returns:
List of all datasets accessible to user
"""
response = self.make_request({}, "dataset/details", requests.get)
dataset_details = (
parse_obj_as( # pylint: disable=used-before-assignment
List[DatasetDetails], response
)
)
return [
Dataset(d.id, client=self, name=d.name) for d in dataset_details
]
@property
def models(self) -> List[Model]:
# TODO: implement for Dataset, scoped just to associated models
"""Fetches all of your Nucleus models.
Returns:
List[:class:`Model`]: List of models associated with the client API key.
"""
model_objects = self.make_request({}, "models/", requests.get)
return [
Model(
model_id=model["id"],
name=model["name"],
reference_id=model["ref_id"],
metadata=model["metadata"] or None,
client=self,
tags=model.get(MODEL_TAGS_KEY, []),
trained_slice_ids=model.get(MODEL_TRAINED_SLICE_IDS_KEY, None),
)
for model in model_objects["models"]
]
@property
def jobs(
self,
) -> List[AsyncJob]:
"""Lists all jobs, see NucleusClient.list_jobs(...) for advanced options
Returns:
List of all AsyncJobs
"""
return self.list_jobs()
@property
def slices(self) -> List[Slice]:
response = self.make_request({}, "slice/", requests.get)
slices = [Slice.from_request(info, self) for info in response]
return slices
@deprecated(msg="Use the NucleusClient.models property in the future.")
def list_models(self) -> List[Model]:
return self.models
@deprecated(msg="Use the NucleusClient.datasets property in the future.")
def list_datasets(self) -> Dict[str, Union[str, List[str]]]:
return self.make_request({}, "dataset/", requests.get)
def list_jobs(
self,
show_completed: bool = False,
from_date: Optional[Union[str, datetime.datetime]] = None,
to_date: Optional[Union[str, datetime.datetime]] = None,
job_types: Optional[List[CustomerJobTypes]] = None,
limit: Optional[int] = None,
dataset_id: Optional[str] = None,
date_limit: Optional[str] = None,
) -> List[AsyncJob]:
"""Fetches all of your running jobs in Nucleus.
Parameters:
show_completed: Whether to include jobs with Completed status.
from_date: Beginning of date range filter.
to_date: End of date range filter.
job_types: Filter on set of job types. If None, fetch all types.
limit: Number of results to fetch, max 50,000.
dataset_id: Filter on a particular dataset.
date_limit: Deprecated, do not use.
Returns:
List[:class:`AsyncJob`]: List of running asynchronous jobs
associated with the client API key.
"""
if date_limit is not None:
warnings.warn(
"Argument `date_limit` is no longer supported. Consider using the `from_date` and `to_date` args."
)
payload = JobInfoRequestPayload(
dataset_id=dataset_id,
show_completed=show_completed,
from_date=from_date,
to_date=to_date,
limit=limit,
job_types=job_types,
).dict()
job_objects = self.make_request(payload, "jobs/", requests.post)
return [
AsyncJob(
job_id=job[JOB_ID_KEY],
job_last_known_status=job[JOB_LAST_KNOWN_STATUS_KEY],
job_type=job[JOB_TYPE_KEY],
job_creation_time=job[JOB_CREATION_TIME_KEY],
client=self,
)
for job in job_objects
]
@deprecated(msg="Prefer using Dataset.items")
def get_dataset_items(self, dataset_id) -> List[DatasetItem]:
dataset = self.get_dataset(dataset_id)
return dataset.items
def get_dataset(self, dataset_id: str) -> Dataset:
"""Fetches a dataset by its ID.
Parameters:
dataset_id: The ID of the dataset to fetch.
Returns:
:class:`Dataset`: The Nucleus dataset as an object.
"""
return Dataset(dataset_id, self)
def get_job(self, job_id: str) -> AsyncJob:
"""Fetches a job by its ID.
Parameters:
job_id: The ID of the job to fetch.
Returns:
:class:`AsyncJob`: The Nucleus async job as an object.
"""
payload = self.make_request(
payload={},
route=f"job/{job_id}/info",
requests_command=requests.get,
)
return AsyncJob.from_json(payload=payload, client=self)
def get_model(
self,
model_id: Optional[str] = None,
model_run_id: Optional[str] = None,
) -> Model:
"""Fetches a model by its ID.
Parameters:
model_id: You can pass either a model ID (starts with ``prj_``) or a
model run ID (starts with ``run_``). Retrieved via :meth:`list_models`
or from a Nucleus dashboard URL.
model_run_id: You can pass either a model ID (starts with ``prj_``), or a
model run ID (starts with ``run_``). This can be retrieved via
:meth:`list_models` or a Nucleus dashboard URL. Model run IDs result
from the application of a model to a dataset. In the future, we plan
to hide ``model_run_ids`` fully from users.
Returns:
:class:`Model`: The Nucleus model as an object.
"""
if model_id is None and model_run_id is None:
raise ValueError("Must pass either a model_id or a model_run_id")
if model_id is not None and model_run_id is not None:
raise ValueError("Must pass either a model_id or a model_run_id")
model_or_model_run_id = (
model_id if model_id is not None else model_run_id
)
payload = self.make_request(
payload={},
route=f"model/{model_or_model_run_id}",
requests_command=requests.get,
)
return Model.from_json(payload=payload, client=self)
@deprecated(
"Model runs have been deprecated and will be removed. Use a Model instead"
)
def get_model_run(self, model_run_id: str, dataset_id: str) -> ModelRun:
return ModelRun(model_run_id, dataset_id, self)
@deprecated(
"Model runs have been deprecated and will be removed. Use a Model instead"
)
def delete_model_run(self, model_run_id: str):
return self.make_request(
{}, f"modelRun/{model_run_id}", requests.delete
)
def create_dataset_from_project(
self,
project_id: str,
last_n_tasks: Optional[int] = None,
name: Optional[str] = None,
) -> Dataset:
"""Create a new dataset from an existing Scale or Rapid project.
If you already have Annotation, SegmentAnnotation, VideoAnnotation,
Categorization, PolygonAnnotation, ImageAnnotation, DocumentTranscription,
LidarLinking, LidarAnnotation, or VideoboxAnnotation projects with Scale,
use this endpoint to import your project directly into Nucleus.
This endpoint is asynchronous because there can be delays when the
number of tasks is larger than 1000. As a result, the endpoint returns
an instance of :class:`AsyncJob`.
Parameters:
project_id: The ID of the Scale/Rapid project (retrievable from URL).
last_n_tasks: If supplied, only pull in this number of the most recent
tasks. By default the endpoint will pull in all eligible tasks.
name: The name for your new Nucleus dataset. By default the endpoint
will use the project's name.
Returns:
:class:`Dataset`: The newly created Nucleus dataset as an object.
"""
payload = {"project_id": project_id}
if last_n_tasks:
payload["last_n_tasks"] = str(last_n_tasks)
if name:
payload["name"] = name
response = self.make_request(payload, "dataset/create_from_project")
return Dataset(response[DATASET_ID_KEY], self)
def create_dataset(
self,
name: str,
is_scene: Optional[bool] = None,
use_privacy_mode: bool = False,
item_metadata_schema: Optional[Dict] = None,
annotation_metadata_schema: Optional[Dict] = None,
) -> Dataset:
"""
Creates a new, empty dataset.
Make sure that the dataset is created for the data type you would like to support.
Be sure to set the ``is_scene`` parameter correctly.
Parameters:
name: A human-readable name for the dataset.
is_scene: Whether the dataset contains strictly :class:`scenes
<LidarScene>` or :class:`items <DatasetItem>`. This value is immutable.
Default is False (dataset of items).
use_privacy_mode: Whether the images of this dataset should be uploaded to Scale. If set to True,
customer will have to adjust their file access policy with Scale.
item_metadata_schema: Dict defining item-level metadata schema, structured as::
{
"field_name": {
"type": "category" | "number" | "text" | "json"
"choices": List[str] | None
"description": str | None
},
...
}
annotation_metadata_schema: Dict defining annotation-level metadata schema.
Same format as ``item_metadata_schema``.
Returns:
:class:`Dataset`: The newly created Nucleus dataset as an object.
"""
if is_scene is None:
warnings.warn(
"The default create_dataset('dataset_name', ...) method without the is_scene parameter will be "
"deprecated soon in favor of providing the is_scene parameter explicitly. "
"Please make sure to create a dataset with either create_dataset('dataset_name', is_scene=False, ...) "
"to upload DatasetItems or create_dataset('dataset_name', is_scene=True, ...) to upload LidarScenes.",
DeprecationWarning,
)
is_scene = False
response = self.make_request(
{
NAME_KEY: name,
DATASET_IS_SCENE_KEY: is_scene,
DATASET_PRIVACY_MODE_KEY: use_privacy_mode,
ANNOTATION_METADATA_SCHEMA_KEY: annotation_metadata_schema,
ITEM_METADATA_SCHEMA_KEY: item_metadata_schema,
},
"dataset/create",
)
return Dataset(
response[DATASET_ID_KEY],
self,
name=name,
is_scene=is_scene,
use_privacy_mode=use_privacy_mode,
)
def delete_dataset(self, dataset_id: str) -> dict:
"""
Deletes a dataset by ID.
All items, annotations, and predictions associated with the dataset will
be deleted as well. Note that if this dataset is linked to a Scale or Rapid
labeling project, the project itself will not be deleted.
Parameters:
dataset_id: The ID of the dataset to delete.
Returns:
Payload to indicate deletion invocation.
"""
return self.make_request({}, f"dataset/{dataset_id}", requests.delete)
@deprecated("Use Dataset.delete_item instead.")
def delete_dataset_item(self, dataset_id: str, reference_id) -> dict:
dataset = self.get_dataset(dataset_id)
return dataset.delete_item(reference_id)
@deprecated("Use Dataset.append instead.")
def populate_dataset(
self,
dataset_id: str,
dataset_items: List[DatasetItem],
batch_size: int = 20,
update: bool = False,
):
dataset = self.get_dataset(dataset_id)
return dataset.append(
dataset_items, batch_size=batch_size, update=update
)
@deprecated(msg="Use Dataset.ingest_tasks instead")
def ingest_tasks(self, dataset_id: str, payload: dict):
dataset = self.get_dataset(dataset_id)
return dataset.ingest_tasks(payload["tasks"])
@deprecated(msg="Use client.create_model instead.")
def add_model(
self, name: str, reference_id: str, metadata: Optional[Dict] = None
) -> Model:
return self.create_model(name, reference_id, metadata)
def create_model(
self,
name: str,
reference_id: str,
metadata: Optional[Dict] = None,
bundle_name: Optional[str] = None,
tags: Optional[List[str]] = None,
trained_slice_ids: Optional[List[str]] = None,
) -> Model:
"""Adds a :class:`Model` to Nucleus.
Parameters:
name: A human-readable name for the model.
reference_id: Unique, user-controlled ID for the model. This can be
used, for example, to link to an external storage of models which
may have its own id scheme.
metadata: An arbitrary dictionary of additional data about this model
that can be stored and retrieved. For example, you can store information
about the hyperparameters used in training this model.
bundle_name: Optional name of bundle attached to this model
tags: Optional list of tags to attach to this model
Returns:
:class:`Model`: The newly created model as an object.
"""
response = self.make_request(
construct_model_creation_payload(
name,
reference_id,
metadata,
bundle_name,
tags,
trained_slice_ids,
),
"models/add",
)
model_id = response.get("model_id", None)
if not model_id:
raise ModelCreationError(response.get("error"))
return Model(
model_id=model_id,
name=name,
reference_id=reference_id,
metadata=metadata,
bundle_name=bundle_name,
client=self,
tags=tags,
trained_slice_ids=trained_slice_ids,
)
def create_launch_model(
self,
name: str,
reference_id: str,
bundle_args: Dict[str, Any],
metadata: Optional[Dict] = None,
trained_slice_ids: Optional[List[str]] = None,
) -> Model:
"""
Adds a :class:`Model` to Nucleus, as well as a Launch bundle from a given function.
Parameters:
name: A human-readable name for the model.
reference_id: Unique, user-controlled ID for the model. This can be
used, for example, to link to an external storage of models which
may have its own ID scheme.
bundle_args: Dict of kwargs for creating a Launch bundle. See the
note below for supported keys.
metadata: An arbitrary dictionary of additional data about this model
that can be stored and retrieved. For example, you can store information
about the hyperparameters used in training this model.
Returns:
:class:`Model`: The newly created model as an object.
.. note::
A bundle consists of exactly ``{predict_fn_or_cls}``,
``{load_predict_fn + model}``, or
``{load_predict_fn + load_model_fn}``. The exact keys depend on
the Launch client version (use ``env_params`` for v0.x, or
``pytorch_image_tag``/``tensorflow_version`` otherwise).
Supported ``bundle_args`` keys:
- ``model_bundle_name``: Unique identifier for the bundle.
- ``predict_fn_or_cls``: End-to-end callable for inference.
- ``model``: Trained neural network, e.g. a PyTorch module.
- ``load_predict_fn``: Returns an inference function given a model.
- ``load_model_fn``: Loads a model.
- ``bundle_url``: Self-hosted mode only. Desired bundle location.
- ``requirements``: List of pip packages.
- ``app_config``: YAML dict or local path.
- ``env_params``: Launch v0 framework/CUDA config.
- ``globals_copy``: Global symbol table (from ``globals()``).
- ``pytorch_image_tag``: Launch v1 + PyTorch image tag.
- ``tensorflow_version``: Launch v1 + TensorFlow version.
"""
from launch import LaunchClient
launch_client = LaunchClient(api_key=self.api_key)
model_exists = any(model.name == name for model in self.list_models())
bundle_exists = any(
bundle.name == name + "-nucleus-autogen"
for bundle in launch_client.list_model_bundles()
)
if bundle_exists or model_exists:
raise ModelCreationError(
"Bundle with the given name already exists, please try a different name"
)
kwargs = {
"model_bundle_name": name + "-nucleus-autogen",
**bundle_args,
}
if hasattr(launch_client, "create_model_bundle_from_callable_v2"):
# Launch client is >= 1.0.0
bundle = launch_client.create_model_bundle_from_callable_v2(
**kwargs
)
bundle_name = (
bundle.name
) # both v0 and v1 have a .name field but are different types
else:
bundle = launch_client.create_model_bundle(**kwargs)
bundle_name = bundle.name
return self.create_model(
name,
reference_id,
metadata,
bundle_name,
trained_slice_ids=trained_slice_ids,
)
def create_launch_model_from_dir(
self,
name: str,
reference_id: str,
bundle_from_dir_args: Dict[str, Any],
metadata: Optional[Dict] = None,
trained_slice_ids: Optional[List[str]] = None,
) -> Model:
"""Adds a :class:`Model` to Nucleus, as well as a Launch bundle from a directory.
Parameters:
name: A human-readable name for the model.
reference_id: Unique, user-controlled ID for the model. This can be
used, for example, to link to an external storage of models which
may have its own id scheme.
bundle_from_dir_args: Dict of kwargs for creating a bundle from
local directories. See the note below for supported keys.
metadata: An arbitrary dictionary of additional data about this model
that can be stored and retrieved. For example, you can store information
about the hyperparameters used in training this model.
Returns:
:class:`Model`: The newly created model as an object.
.. note::
Code from one or more local filesystem folders is packaged into a
zip and uploaded to Scale Launch. Contents are unzipped relative to
the server-side ``PYTHONPATH``, so module paths should reflect the
directory structure (e.g. ``my_module.my_file.f``). The exact keys
depend on the Launch client version (use ``env_params`` for v0.x,
or ``pytorch_image_tag``/``tensorflow_version`` otherwise).
Supported ``bundle_from_dir_args`` keys:
- ``model_bundle_name``: Unique identifier for the bundle.
- ``base_paths``: Local dirs containing the bundle code.
- ``requirements_path``: Path to a ``requirements.txt`` file.
- ``env_params``: Launch v0 framework/CUDA config.
- ``load_predict_fn_module_path``: Module path for inference fn.
- ``load_model_fn_module_path``: Module path for model loader.
- ``app_config``: YAML dict or local path.
- ``pytorch_image_tag``: Launch v1 + PyTorch image tag.
- ``tensorflow_version``: Launch v1 + TensorFlow version.
.. note::
For example, given this directory structure::
my_root/
my_module1/
__init__.py
...files and directories
my_inference_file.py
my_module2/
__init__.py
...files and directories
Calling with ``base_paths=["my_module1", "my_module2"]`` creates a
zip without the root directory. Contents are unzipped relative to
the server-side ``PYTHONPATH``. If ``my_inference_file.py`` has
``def f(...)`` as the inference loading function, then
``load_predict_fn_module_path`` should be
``my_module1.my_inference_file.f``.
"""
from launch import LaunchClient
launch_client = LaunchClient(api_key=self.api_key)
model_exists = any(model.name == name for model in self.list_models())
bundle_exists = any(
bundle.name == name + "-nucleus-autogen"
for bundle in launch_client.list_model_bundles()
)
if bundle_exists or model_exists:
raise ModelCreationError(
"Bundle with the given name already exists, please try a different name"
)
kwargs = {
"model_bundle_name": name + "-nucleus-autogen",
**bundle_from_dir_args,
}
if hasattr(launch_client, "create_model_bundle_from_dirs_v2"):
# Launch client is >= 1.0.0, use new fn
bundle = launch_client.create_model_bundle_from_dirs_v2(**kwargs)
# Different code paths give different types for bundle, although both have a .name field
bundle_name = bundle.name
else:
# Launch client is < 1.0.0
bundle = launch_client.create_model_bundle_from_dirs(**kwargs)
bundle_name = bundle.name
return self.create_model(
name,
reference_id,
metadata,
bundle_name,
trained_slice_ids=trained_slice_ids,
)
@deprecated(
"Model runs have been deprecated and will be removed. Use a Model instead"
)
def create_model_run(self, dataset_id: str, payload: dict) -> ModelRun:
response = self.make_request(
payload, f"dataset/{dataset_id}/modelRun/create"
)
if response.get(STATUS_CODE_KEY, None):
raise ModelRunCreationError(response.get("error"))
return ModelRun(
response[MODEL_RUN_ID_KEY], dataset_id=dataset_id, client=self
)
@deprecated(
"Model runs have been deprecated and will be removed. Use a Model instead."
)
def commit_model_run(
self, model_run_id: str, payload: Optional[dict] = None
):
# TODO: deprecate ModelRun. this should be renamed to calculate_evaluation_metrics
# or completely removed in favor of Model class methods
if payload is None:
payload = {}
return self.make_request(payload, f"modelRun/{model_run_id}/commit")
@deprecated(msg="Prefer calling Dataset.info() directly.")
def dataset_info(self, dataset_id: str):
dataset = self.get_dataset(dataset_id)
return dataset.info()
@deprecated(
"Model runs have been deprecated and will be removed. Use a Model instead."
)
def model_run_info(self, model_run_id: str):
# TODO: deprecate ModelRun
return self.make_request(
{}, f"modelRun/{model_run_id}/info", requests.get
)
@deprecated("Prefer calling Dataset.refloc instead.")
@sanitize_string_args
def dataitem_ref_id(self, dataset_id: str, reference_id: str):
# TODO: deprecate in favor of Dataset.refloc invocation
return self.make_request(
{}, f"dataset/{dataset_id}/refloc/{reference_id}", requests.get
)
@deprecated("Prefer calling Dataset.predictions_refloc instead.")
@sanitize_string_args
def predictions_ref_id(
self, model_run_id: str, ref_id: str, dataset_id: Optional[str] = None
):
if dataset_id:
raise RuntimeError(
"Need to pass a dataset id. Or use Dataset.predictions_refloc."
)
# TODO: deprecate ModelRun
m_run = self.get_model_run(model_run_id, dataset_id)
return m_run.refloc(ref_id)
@deprecated("Prefer calling Dataset.iloc instead.")
def dataitem_iloc(self, dataset_id: str, i: int):
# TODO: deprecate in favor of Dataset.iloc invocation
return self.make_request(
{}, f"dataset/{dataset_id}/iloc/{i}", requests.get
)
@deprecated("Prefer calling Dataset.predictions_iloc instead.")
def predictions_iloc(self, model_run_id: str, i: int):
# TODO: deprecate ModelRun
return self.make_request(
{}, f"modelRun/{model_run_id}/iloc/{i}", requests.get
)
@deprecated("Prefer calling Dataset.loc instead.")
def dataitem_loc(self, dataset_id: str, dataset_item_id: str):
# TODO: deprecate in favor of Dataset.loc invocation
return self.make_request(
{}, f"dataset/{dataset_id}/loc/{dataset_item_id}", requests.get
)
@deprecated("Prefer calling Dataset.predictions_loc instead.")
def predictions_loc(self, model_run_id: str, dataset_item_id: str):
# TODO: deprecate ModelRun
return self.make_request(
{}, f"modelRun/{model_run_id}/loc/{dataset_item_id}", requests.get
)
@deprecated("Prefer calling Dataset.create_slice instead.")
def create_slice(self, dataset_id: str, payload: dict) -> Slice:
# TODO: deprecate in favor of Dataset.create_slice
dataset = self.get_dataset(dataset_id)
return dataset.create_slice(payload["name"], payload["reference_ids"])
def get_slice(self, slice_id: str) -> Slice:
# TODO: migrate to Dataset method and deprecate
"""Returns a slice object by Nucleus-generated ID.
Parameters:
slice_id: Nucleus-generated slice ID (starts with ``slc_``). This can
be retrieved via :meth:`Dataset.slices` or a Nucleus dashboard URL.
Returns:
:class:`Slice`: The Nucleus slice as an object.
"""
return Slice(slice_id, self)
@deprecated("Prefer calling Slice.info instead.")
def slice_info(self, slice_id: str) -> dict:
# TODO: deprecate in favor of Slice.info
response = self.make_request(
{},
f"slice/{slice_id}",
requests_command=requests.get,
)
return response
def delete_slice(self, slice_id: str) -> dict:
# TODO: migrate to Dataset method and deprecate
"""Deletes slice from Nucleus.
Parameters:
slice_id: Nucleus-generated slice ID (starts with ``slc_``). This can
be retrieved via :meth:`Dataset.slices` or a Nucleus dashboard URL.
Returns:
Empty payload response.
"""
response = self.make_request(
{},
f"slice/{slice_id}",
requests_command=requests.delete,
)
return response
@deprecated("Prefer calling Dataset.delete_annotations instead.")
def delete_annotations(
self,
dataset_id: str,
reference_ids: Optional[list] = None,
keep_history=True,
) -> AsyncJob:
dataset = self.get_dataset(dataset_id)
return dataset.delete_annotations(reference_ids, keep_history)
def append_to_slice(
self,
slice_id: str,
reference_ids: List[str],
dataset_id: str,
) -> dict:
# TODO: migrate to Slice method and deprecate
"""Appends dataset items or scenes to an existing slice.
Parameters:
slice_id: Nucleus-generated dataset ID (starts with ``slc_``). This can
be retrieved via :meth:`Dataset.slices` or a Nucleus dashboard URL.