Creating Datasets at Scale
Creating large Deep Lake datasets with high performance and reliability
Last updated
Was this helpful?
Was this helpful?
coco_bucket = <bucket_containing_the_source_data>
deeplake_bucket = <bucket_for_storing_the_deep_lake_dataset>
creds = {'aws_access_key_id': os.environ.get('aws_access_key_id'),
'aws_secret_access_key': os.environ.get('aws_secret_access_key')}
# Create the connection to the source data
s3 = boto3.resource('s3',
aws_access_key_id = creds['aws_access_key_id'],
aws_secret_access_key = creds['aws_secret_access_key'])
s3_bucket = s3.Bucket(coco_bucket)cloud_ann_path = 'coco/annotations/instances_train2017.json'
local_ann_path = 'anns_train.json'
s3_bucket.download_file(ann_path, local_ann_path)
coco = COCO(local_ann_path)
category_info = coco.loadCats(coco.getCatIds())ds = deeplake.empty('s3://{}/coco-train'.format(deeplake_bucket), creds = creds, overwrite = True)
creds_key = <managed_creds_key>
ds.connect(org_id = <org_id>, creds_key = creds_key, token = <your_token>)
ds.add_creds_key(creds_key, managed = True)category_names = [category['name'] for category in category_info]with ds:
ds.create_tensor('images', htype = 'link[image]', sample_compression = 'jpg')
ds.create_tensor('categories', htype = 'class_label', class_names = category_names)
ds.create_tensor('boxes', htype = 'bbox')
ds.create_tensor('masks', htype = 'binary_mask', sample_compression = 'lz4')img_ids = sorted(coco.getImgIds())@deeplake.compute
def coco_2_deeplake(img_id, sample_out, coco_api, category_names, category_info, bucket, creds_key):
anns = coco_api.loadAnns(coco_api.getAnnIds(img_id))
img_coco = coco_api.loadImgs(img_id)[0]
# First create empty arrays for all annotations
categories = np.zeros((len(anns)), dtype = np.uint32)
boxes = np.zeros((len(anns),4), dtype = np.float32)
masks = np.zeros((img_coco['height'], img_coco['width'], len(anns)), dtype = bool)
# Then populate the arrays with the annotations data
for i, ann in enumerate(anns):
mask = coco.annToMask(ann) # Convert annotation to binary mask
masks[:, :, i] = mask
boxes[i,:] = ann['bbox']
# Find the deep lake category_names index from the coco category_id
categories[i] = category_names.index([category_info[i]['name'] for i in range(len(category_info)) if category_info[i]['id']==ann['category_id']][0])
# Append the data to a deeplake sample
sample_out.append({'images': deeplake.link('s3://{}/coco/train2017/{}'.format(bucket, img_coco['file_name']), creds_key = creds_key),
'categories': categories,
'boxes': boxes,
'masks': masks})coco_2_deeplake(coco_api = coco,
bucket = coco_bucket,
category_names = category_names,
category_info = category_info,
creds_key = creds_key).eval(img_ids,
ds,
num_workers = 8,
checkpoint_interval=25000)Commit : firstdbf9474d461a19e9333c2fd19b46115348f (main)
Author : <username>
Time : 2023-03-27 19:18:14
Message: Auto-commit during deeplake.compute of coco_2_deeplake after 20.0% progress