-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
example(dataset): add caltech-101 dataset (#1631)
add caltech-101 dataset
- Loading branch information
Showing
3 changed files
with
92 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
--- | ||
title: The `caltech-101` Dataset | ||
--- | ||
|
||
## The Caltech 101 Dataset Description | ||
|
||
- [Homepage](https://data.caltech.edu/records/mzrjq-6wc02) | ||
|
||
## The `caltech-101` dataset Structure | ||
|
||
### Data Fields | ||
|
||
- `data`: `starwhale.Image` loaded as bytes array | ||
- `annotations` of type dict: | ||
- `label`: the label for the image | ||
|
||
## Build `caltech-101` Dataset locally | ||
|
||
```shell | ||
python3 dataset.py | ||
``` | ||
|
||
## Example | ||
|
||
Output the first 1 record of the `caltech-101` dataset. | ||
|
||
```shell | ||
python3 example.py | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import json | ||
|
||
import requests | ||
|
||
from starwhale import Link, Image, dataset, MIMEType # noqa: F401 | ||
from starwhale.utils.retry import http_retry | ||
|
||
PATH_ROOT = "https://starwhale-examples.oss-cn-beijing.aliyuncs.com/dataset/caltech-101" | ||
DATA_PATH = "101_ObjectCategories" | ||
|
||
|
||
@http_retry | ||
def request_link_text(anno_link): | ||
return requests.get(anno_link, timeout=10).text | ||
|
||
|
||
def build_ds(): | ||
ds = dataset("caltech-101", create=True) | ||
tree = json.loads(request_link_text(f"{PATH_ROOT}/tree.json")) | ||
for dir in tree: | ||
if DATA_PATH not in dir.get("name", ""): | ||
continue | ||
for d in dir["contents"]: | ||
if d["type"] != "directory": | ||
continue | ||
category = d["name"] | ||
for f in d["contents"]: | ||
_name = f["name"] | ||
ds.append( | ||
( | ||
f"{category}/{_name}", | ||
Link( | ||
uri=f"{PATH_ROOT}/{DATA_PATH}/{category}/{_name}", | ||
data_type=Image( | ||
display_name=_name, mime_type=MIMEType.JPEG | ||
), | ||
with_local_fs_data=False, | ||
), | ||
{"label": category}, | ||
) | ||
) | ||
ds.commit() | ||
ds.close() | ||
|
||
|
||
if __name__ == "__main__": | ||
build_ds() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import io | ||
|
||
from PIL import Image as PILImage | ||
from PIL import ImageDraw | ||
|
||
from starwhale import dataset | ||
|
||
ds_name = "caltech-101/version/latest" | ||
ds = dataset(ds_name) | ||
row = ds.fetch_one() | ||
data = row.data | ||
annotations = row.annotations | ||
with PILImage.open(io.BytesIO(data.fp)) as img: | ||
draw = ImageDraw.Draw(img) | ||
draw.text((28, 36), annotations["label"], fill="red") | ||
img.show() |