2. Load from Hub and transform¶
Setup¶
In [ ]:
Copied!
%pip install detection_datasets
%pip install detection_datasets
In [2]:
Copied!
from detection_datasets import DetectionDataset, available_in_hub
from detection_datasets import DetectionDataset, available_in_hub
Load from Hub¶
First we check which datasets are available in the Hub, using the default "detection-datasets' repository.
In [3]:
Copied!
available_in_hub()
available_in_hub()
Out[3]:
['fashionpedia']
We can then load the "fashionpedia" dataset:
In [4]:
Copied!
dd = DetectionDataset().from_hub(dataset_name='fashionpedia')
dd = DetectionDataset().from_hub(dataset_name='fashionpedia')
Downloading: 0%| | 0.00/1.78k [00:00<?, ?B/s]
WARNING:datasets.builder:Using custom data configuration detection-datasets--fashionpedia-1960081b1c22758d
Downloading and preparing dataset None/None (download: 3.24 GiB, generated: 3.12 GiB, post-processed: Unknown size, total: 6.36 GiB) to /root/.cache/huggingface/datasets/detection-datasets___parquet/detection-datasets--fashionpedia-1960081b1c22758d/0.0.0/2a3b91fbd88a2c90d1dbbb32b460cf621d31bd5b05b934492fdef7d8d6f236ec...
Downloading data files: 0%| | 0/2 [00:00<?, ?it/s]
Downloading data: 0%| | 0.00/84.8M [00:00<?, ?B/s]
Downloading data: 0%| | 0.00/482M [00:00<?, ?B/s]
Downloading data: 0%| | 0.00/480M [00:00<?, ?B/s]
Downloading data: 0%| | 0.00/480M [00:00<?, ?B/s]
Downloading data: 0%| | 0.00/490M [00:00<?, ?B/s]
Downloading data: 0%| | 0.00/488M [00:00<?, ?B/s]
Downloading data: 0%| | 0.00/487M [00:00<?, ?B/s]
Downloading data: 0%| | 0.00/487M [00:00<?, ?B/s]
Extracting data files: 0%| | 0/2 [00:00<?, ?it/s]
0 tables [00:00, ? tables/s]
0 tables [00:00, ? tables/s]
Dataset parquet downloaded and prepared to /root/.cache/huggingface/datasets/detection-datasets___parquet/detection-datasets--fashionpedia-1960081b1c22758d/0.0.0/2a3b91fbd88a2c90d1dbbb32b460cf621d31bd5b05b934492fdef7d8d6f236ec. Subsequent calls will reuse this data.
0%| | 0/2 [00:00<?, ?it/s]
Moving images from memory to disk: 0%| | 0/1158 [00:00<?, ?ex/s]
Moving images from memory to disk: 0%| | 0/45623 [00:00<?, ?ex/s]
Analyze the dataset¶
In [5]:
Copied!
dd.data
dd.data
Out[5]:
image_path | width | height | split | bbox_id | bbox | category_id | category | area | |
---|---|---|---|---|---|---|---|---|---|
image_id | |||||||||
23 | /root/.detection-datasets/23.jpg | 682 | 1024 | train | [150311, 150312, 150313, 150314] | [Bbox id 150311 [445. 910. 505. 983.], Bbox id... | [23, 23, 33, 10] | [shoe, shoe, neckline, dress] | [1422, 843, 373, 56375] |
25 | /root/.detection-datasets/25.jpg | 683 | 1024 | train | [158953, 158954, 158955, 158956, 158957, 15895... | [Bbox id 158953 [182. 220. 472. 647.], Bbox id... | [2, 33, 31, 31, 13, 7, 22, 22, 23, 23] | [sweater, neckline, sleeve, sleeve, glasses, s... | [87267, 1220, 16895, 18541, 1468, 9360, 8629, ... |
26 | /root/.detection-datasets/26.jpg | 1024 | 683 | train | [169196, 169197, 169198, 169199, 169200, 16920... | [Bbox id 169196 [441. 132. 499. 150.], Bbox id... | [13, 29, 28, 32, 32, 31, 31, 0, 31, 31, 18, 4,... | [glasses, lapel, collar, pocket, pocket, sleev... | [587, 2922, 931, 262, 111, 1171, 540, 3981, 44... |
27 | /root/.detection-datasets/27.jpg | 682 | 1024 | train | [167967, 167968, 167969, 167970, 167971, 16797... | [Bbox id 167967 [300. 421. 460. 846.], Bbox id... | [6, 23, 23, 31, 31, 4, 1, 35, 32, 35, 35, 35, ... | [pants, shoe, shoe, sleeve, sleeve, jacket, to... | [44062, 2140, 2633, 9206, 5905, 44791, 12948, ... |
28 | /root/.detection-datasets/28.jpg | 853 | 1024 | train | [168041, 168042, 168043, 168044, 168045, 16804... | [Bbox id 168041 [ 238. 309. 471. 1022.], Bbo... | [10, 32, 35, 31, 4, 29, 33] | [dress, pocket, zipper, sleeve, jacket, lapel,... | [12132, 1548, 755, 43926, 178328, 9316, 136] |
... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
50525 | /root/.detection-datasets/50525.jpg | 660 | 1024 | train | [82929, 82930, 82931, 82932, 82933, 82934, 82935] | [Bbox id 82929 [419. 214. 576. 637.], Bbox id ... | [31, 31, 33, 2, 15, 8, 32] | [sleeve, sleeve, neckline, sweater, headband, ... | [39360, 26851, 2431, 262021, 1422, 34694, 294] |
50526 | /root/.detection-datasets/50526.jpg | 682 | 1024 | train | [82936, 82937, 82938, 82939, 82940] | [Bbox id 82936 [ 168. 846. 504. 1022.], Bbox... | [6, 1, 33, 32, 32] | [pants, top, t-shirt, sweatshirt, neckline, po... | [49375, 151458, 4122, 993, 371] |
50528 | /root/.detection-datasets/50528.jpg | 682 | 1024 | train | [177641, 177642, 177643, 177644, 177645, 177646] | [Bbox id 177641 [195. 256. 408. 725.], Bbox id... | [10, 33, 31, 31, 23, 23] | [dress, neckline, sleeve, sleeve, shoe, shoe] | [47419, 267, 12965, 2090, 2914, 1171] |
50530 | /root/.detection-datasets/50530.jpg | 682 | 1024 | train | [82941, 82942, 82943, 82944, 82945] | [Bbox id 82941 [157. 236. 366. 591.], Bbox id ... | [11, 33, 31, 23, 23] | [jumpsuit, neckline, sleeve, shoe, shoe] | [46325, 684, 2113, 1714, 2927] |
50531 | /root/.detection-datasets/50531.jpg | 682 | 1024 | train | [82946, 82947, 82948, 82949, 82950, 82951, 829... | [Bbox id 82946 [308. 900. 337. 976.], Bbox id ... | [23, 23, 10, 19, 34, 31, 31, 39, 33] | [shoe, shoe, dress, belt, buckle, sleeve, slee... | [919, 2813, 86771, 4029, 384, 2542, 1102, 2923... |
46781 rows × 9 columns
In [6]:
Copied!
dd.show(image_id=25)
dd.show(image_id=25)
Out[6]:
In [7]:
Copied!
dd.categories
dd.categories
Out[7]:
category | |
---|---|
category_id | |
0 | shirt, blouse |
1 | top, t-shirt, sweatshirt |
2 | sweater |
3 | cardigan |
4 | jacket |
5 | vest |
6 | pants |
7 | shorts |
8 | skirt |
9 | coat |
10 | dress |
11 | jumpsuit |
12 | cape |
13 | glasses |
14 | hat |
15 | headband, head covering, hair accessory |
16 | tie |
17 | glove |
18 | watch |
19 | belt |
20 | leg warmer |
21 | tights, stockings |
22 | sock |
23 | shoe |
24 | bag, wallet |
25 | scarf |
26 | umbrella |
27 | hood |
28 | collar |
29 | lapel |
30 | epaulette |
31 | sleeve |
32 | |
33 | neckline |
34 | buckle |
35 | zipper |
36 | applique |
37 | bead |
38 | bow |
39 | flower |
40 | fringe |
41 | ribbon |
42 | rivet |
43 | ruffle |
44 | sequin |
45 | tassel |
Transform the dataset¶
Change the categories¶
In this dataset we have very fine_grained labels, so to create an easier task we may want to focus on higher level labels, like "clothing" instead of "pants".
For example, let us assume that we are only interested in detection these 4 classes:
- Clothing
- Shoes
- Bags
- Accessories
We can start by printing a list of the existing category names, and use it to create our mapping dictionnary:
In [8]:
Copied!
dd.category_names
dd.category_names
Out[8]:
['shirt, blouse', 'top, t-shirt, sweatshirt', 'sweater', 'cardigan', 'jacket', 'vest', 'pants', 'shorts', 'skirt', 'coat', 'dress', 'jumpsuit', 'cape', 'glasses', 'hat', 'headband, head covering, hair accessory', 'tie', 'glove', 'watch', 'belt', 'leg warmer', 'tights, stockings', 'sock', 'shoe', 'bag, wallet', 'scarf', 'umbrella', 'hood', 'collar', 'lapel', 'epaulette', 'sleeve', 'pocket', 'neckline', 'buckle', 'zipper', 'applique', 'bead', 'bow', 'flower', 'fringe', 'ribbon', 'rivet', 'ruffle', 'sequin', 'tassel']
In [9]:
Copied!
mapping = {
'shirt, blouse': 'clothing',
'top, t-shirt, sweatshirt': 'clothing',
'sweater': 'clothing',
'cardigan': 'clothing',
'jacket': 'clothing',
'vest': 'clothing',
'pants': 'clothing',
'shorts': 'clothing',
'skirt': 'clothing',
'coat': 'clothing',
'dress': 'clothing',
'jumpsuit': 'clothing',
'cape': 'clothing',
'glasses': 'accessories',
'hat': 'accessories',
'headband, head covering, hair accessory': 'accessories',
'tie': 'accessories',
'glove': 'accessories',
'belt': 'accessories',
'tights, stockings': 'accessories',
'sock': 'accessories',
'shoe': 'shoes',
'bag, wallet': 'bags',
'scarf': 'accessories',
}
mapping = {
'shirt, blouse': 'clothing',
'top, t-shirt, sweatshirt': 'clothing',
'sweater': 'clothing',
'cardigan': 'clothing',
'jacket': 'clothing',
'vest': 'clothing',
'pants': 'clothing',
'shorts': 'clothing',
'skirt': 'clothing',
'coat': 'clothing',
'dress': 'clothing',
'jumpsuit': 'clothing',
'cape': 'clothing',
'glasses': 'accessories',
'hat': 'accessories',
'headband, head covering, hair accessory': 'accessories',
'tie': 'accessories',
'glove': 'accessories',
'belt': 'accessories',
'tights, stockings': 'accessories',
'sock': 'accessories',
'shoe': 'shoes',
'bag, wallet': 'bags',
'scarf': 'accessories',
}
We will then map the existing classes to these new classes with a simple Python dictionnary.
3 thinks will happen when applying this new mapping:
- The existing categories will be replaced by their mapped category names
- The existing categories that have no equivalent in this mapping will be dropped
- The category_id will be recreated using the alphabetical order of the new categories (here 'Accessories' will have id 0, 'Bags' 1 and so on)
In [10]:
Copied!
dd.map_categories(mapping=mapping)
dd.map_categories(mapping=mapping)
Out[10]:
<detection_datasets.detection_dataset.DetectionDataset at 0x7f4fd5cf7d90>
In [11]:
Copied!
dd.categories
dd.categories
Out[11]:
category | |
---|---|
category_id | |
0 | accessories |
1 | bags |
2 | clothing |
3 | shoes |
In [12]:
Copied!
dd.data
dd.data
Out[12]:
image_path | width | height | split | bbox_id | bbox | category_id | category | area | |
---|---|---|---|---|---|---|---|---|---|
image_id | |||||||||
23 | /root/.detection-datasets/23.jpg | 682 | 1024 | train | [150311, 150312, 150314] | [Bbox id 150311 [445. 910. 505. 983.], Bbox id... | [3, 3, 2] | [shoes, shoes, clothing] | [1422, 843, 56375] |
25 | /root/.detection-datasets/25.jpg | 683 | 1024 | train | [158953, 158957, 158958, 158959, 158960, 15896... | [Bbox id 158953 [182. 220. 472. 647.], Bbox id... | [2, 0, 2, 0, 0, 3, 3] | [clothing, accessories, clothing, accessories,... | [87267, 1468, 9360, 8629, 8270, 2717, 3121] |
26 | /root/.detection-datasets/26.jpg | 1024 | 683 | train | [169196, 169203, 169207, 169208, 169209, 169210] | [Bbox id 169196 [441. 132. 499. 150.], Bbox id... | [0, 2, 2, 2, 3, 3] | [accessories, clothing, clothing, clothing, sh... | [587, 3981, 26621, 16954, 2167, 1773] |
27 | /root/.detection-datasets/27.jpg | 682 | 1024 | train | [167967, 167968, 167969, 167972, 167973] | [Bbox id 167967 [300. 421. 460. 846.], Bbox id... | [2, 3, 3, 2, 2] | [clothing, shoes, shoes, clothing, clothing] | [44062, 2140, 2633, 44791, 12948] |
28 | /root/.detection-datasets/28.jpg | 853 | 1024 | train | [168041, 168045] | [Bbox id 168041 [ 238. 309. 471. 1022.], Bbo... | [2, 2] | [clothing, clothing] | [12132, 178328] |
... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
50525 | /root/.detection-datasets/50525.jpg | 660 | 1024 | train | [82932, 82933, 82934] | [Bbox id 82932 [ 6. 209. 576. 923.], Bbox id ... | [2, 0, 2] | [clothing, accessories, clothing] | [262021, 1422, 34694] |
50526 | /root/.detection-datasets/50526.jpg | 682 | 1024 | train | [82936, 82937] | [Bbox id 82936 [ 168. 846. 504. 1022.], Bbox... | [2, 2] | [clothing, clothing] | [49375, 151458] |
50528 | /root/.detection-datasets/50528.jpg | 682 | 1024 | train | [177641, 177645, 177646] | [Bbox id 177641 [195. 256. 408. 725.], Bbox id... | [2, 3, 3] | [clothing, shoes, shoes] | [47419, 2914, 1171] |
50530 | /root/.detection-datasets/50530.jpg | 682 | 1024 | train | [82941, 82944, 82945] | [Bbox id 82941 [157. 236. 366. 591.], Bbox id ... | [2, 3, 3] | [clothing, shoes, shoes] | [46325, 1714, 2927] |
50531 | /root/.detection-datasets/50531.jpg | 682 | 1024 | train | [82946, 82947, 82948, 82949] | [Bbox id 82946 [308. 900. 337. 976.], Bbox id ... | [3, 3, 2, 0] | [shoes, shoes, clothing, accessories] | [919, 2813, 86771, 4029] |
46780 rows × 9 columns
In [13]:
Copied!
dd.show(image_id=25)
dd.show(image_id=25)
Out[13]:
Change the splits¶
The fashionpedia comes with the following splits:
In [14]:
Copied!
dd.splits
dd.splits
Out[14]:
['train', 'val']
In [15]:
Copied!
dd.split_proportions
dd.split_proportions
Out[15]:
train | val | test | |
---|---|---|---|
0 | 0.975246 | 0.024754 | 0.0 |
In [16]:
Copied!
dd.split(splits=[0.9, 0.05, 0.05])
dd.split(splits=[0.9, 0.05, 0.05])
Out[16]:
<detection_datasets.detection_dataset.DetectionDataset at 0x7f4fd5cf7d90>
In [17]:
Copied!
dd.splits
dd.splits
Out[17]:
['train', 'val', 'test']
In [18]:
Copied!
dd.split_proportions
dd.split_proportions
Out[18]:
train | val | test | |
---|---|---|---|
0 | 0.9 | 0.05 | 0.05 |
Push to new dataset to the Hub¶
In [ ]:
Copied!
! huggingface-cli login
! huggingface-cli login
In [21]:
Copied!
dd.to_hub(dataset_name='fashionpedia_4_categories', repo_name='detection-datasets')
dd.to_hub(dataset_name='fashionpedia_4_categories', repo_name='detection-datasets')
WARNING:datasets.dataset_dict:Pushing split train to the Hub.
0%| | 0/7 [00:00<?, ?ba/s]
Pushing dataset shards to the dataset hub: 0%| | 0/7 [00:00<?, ?it/s]
0%| | 0/7 [00:00<?, ?ba/s]
0%| | 0/7 [00:00<?, ?ba/s]
0%| | 0/7 [00:00<?, ?ba/s]
0%| | 0/7 [00:00<?, ?ba/s]
0%| | 0/7 [00:00<?, ?ba/s]
0%| | 0/7 [00:00<?, ?ba/s]
WARNING:datasets.dataset_dict:Pushing split test to the Hub.
0%| | 0/3 [00:00<?, ?ba/s]
Pushing dataset shards to the dataset hub: 0%| | 0/1 [00:00<?, ?it/s]
WARNING:datasets.dataset_dict:Pushing split val to the Hub.
0%| | 0/3 [00:00<?, ?ba/s]
Pushing dataset shards to the dataset hub: 0%| | 0/1 [00:00<?, ?it/s]
Out[21]:
<detection_datasets.detection_dataset.DetectionDataset at 0x7f4fd5cf7d90>