Support for streaming
Browse filesUse "virtual-hosted–style" access to download images and metadata;
allowing the Hugging Face download manager to operate in either
regular or streaming mode.
- pest-management-opendata.py +71 -51
pest-management-opendata.py
CHANGED
@@ -4,9 +4,6 @@ from pathlib import Path
|
|
4 |
from dataclasses import dataclass, asdict
|
5 |
from urllib.parse import urlparse, urlunparse
|
6 |
|
7 |
-
import cv2
|
8 |
-
import boto3
|
9 |
-
import numpy as np
|
10 |
import pandas as pd
|
11 |
import awswrangler as wr
|
12 |
from datasets import (
|
@@ -25,19 +22,55 @@ from shapely.wkt import loads
|
|
25 |
__version__ = '20220912-2056'
|
26 |
|
27 |
SplitInfo = cl.namedtuple('SplitInfo', 'dtype, basename, split')
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
|
29 |
@dataclass
|
30 |
class SplitPayload:
|
31 |
split: str
|
32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
|
34 |
-
|
35 |
-
|
36 |
|
37 |
-
def
|
38 |
-
|
39 |
-
|
40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
|
42 |
#
|
43 |
#
|
@@ -49,20 +82,10 @@ class SplitManager:
|
|
49 |
(Split.TEST, 'test', 'test'),
|
50 |
)))
|
51 |
|
52 |
-
@staticmethod
|
53 |
-
def custom_download(url, path):
|
54 |
-
remote = urlparse(url)
|
55 |
-
name = Path(remote.path)
|
56 |
-
if name.is_absolute():
|
57 |
-
name = name.relative_to(name.parents[-1])
|
58 |
-
|
59 |
-
s3 = boto3.client(remote.scheme)
|
60 |
-
s3.download_file(remote.netloc, str(name), path)
|
61 |
-
|
62 |
@property
|
63 |
def labels(self):
|
64 |
-
path = self.
|
65 |
-
df = wr.s3.read_csv(path, compression='gzip')
|
66 |
yield from df['label'].dropna().unique()
|
67 |
|
68 |
def __init__(self, bucket):
|
@@ -71,22 +94,31 @@ class SplitManager:
|
|
71 |
|
72 |
def __call__(self, dl_manager):
|
73 |
for i in self._splits:
|
74 |
-
|
75 |
-
|
76 |
-
|
|
|
|
|
|
|
77 |
|
|
|
78 |
yield SplitGenerator(name=i.dtype, gen_kwargs=asdict(payload))
|
79 |
|
80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
81 |
path = self.path.joinpath(split).with_suffix('.csv.gz')
|
82 |
-
|
83 |
-
return urlunparse(source)
|
84 |
|
85 |
#
|
86 |
#
|
87 |
#
|
88 |
class ExampleManager:
|
89 |
-
_decode_flags = cv2.IMREAD_COLOR | cv2.IMREAD_IGNORE_ORIENTATION
|
90 |
# _Pest = cl.namedtuple('_Pest', 'label, geometry')
|
91 |
# _Feature = cl.namedtuple('_Feature', 'image, pests')
|
92 |
|
@@ -114,31 +146,19 @@ class ExampleManager:
|
|
114 |
self.payload = payload
|
115 |
|
116 |
def __iter__(self):
|
117 |
-
|
118 |
-
|
|
|
|
|
119 |
value = {
|
120 |
-
'image':
|
121 |
-
|
|
|
|
|
|
|
122 |
}
|
123 |
|
124 |
-
yield (
|
125 |
-
|
126 |
-
def load(self, url):
|
127 |
-
path = Path(url.path)
|
128 |
-
if path.is_absolute():
|
129 |
-
(*_, root) = path.parents
|
130 |
-
path = path.relative_to(root)
|
131 |
-
|
132 |
-
data = (boto3
|
133 |
-
.resource(url.scheme)
|
134 |
-
.Bucket(url.netloc)
|
135 |
-
.Object(str(path))
|
136 |
-
.get()
|
137 |
-
.get('Body')
|
138 |
-
.read())
|
139 |
-
image = np.asarray(bytearray(data))
|
140 |
-
|
141 |
-
return cv2.imdecode(image, self._decode_flags)
|
142 |
|
143 |
#
|
144 |
#
|
|
|
4 |
from dataclasses import dataclass, asdict
|
5 |
from urllib.parse import urlparse, urlunparse
|
6 |
|
|
|
|
|
|
|
7 |
import pandas as pd
|
8 |
import awswrangler as wr
|
9 |
from datasets import (
|
|
|
22 |
__version__ = '20220912-2056'
|
23 |
|
24 |
SplitInfo = cl.namedtuple('SplitInfo', 'dtype, basename, split')
|
25 |
+
Payload = cl.namedtuple('Payload', 'source, target, df')
|
26 |
+
|
27 |
+
def readsp(path, split):
|
28 |
+
return (pd
|
29 |
+
.read_csv(path, compression='gzip')
|
30 |
+
.query(f'split == "{split}"'))
|
31 |
|
32 |
@dataclass
|
33 |
class SplitPayload:
|
34 |
split: str
|
35 |
+
metadata: Path
|
36 |
+
images: dict
|
37 |
+
|
38 |
+
def __iter__(self):
|
39 |
+
df = readsp(self.metadata, self.split)
|
40 |
+
for (i, g) in df.groupby('url', sort=False):
|
41 |
+
source = urlparse(i)
|
42 |
+
target = Path(self.images[i])
|
43 |
+
|
44 |
+
yield Payload(source, target, g)
|
45 |
+
|
46 |
+
#
|
47 |
+
#
|
48 |
+
#
|
49 |
+
class AmazonStorageStyleAccess:
|
50 |
+
def __init__(self, url):
|
51 |
+
self.url = url
|
52 |
+
|
53 |
+
def __str__(self):
|
54 |
+
return urlunparse(self.url)
|
55 |
+
|
56 |
+
class StandardStyleAccess(AmazonStorageStyleAccess):
|
57 |
+
pass
|
58 |
|
59 |
+
class VirtualStyleAccess(AmazonStorageStyleAccess):
|
60 |
+
_region = 'ap-south-1'
|
61 |
|
62 |
+
def __init__(self, url):
|
63 |
+
parts = [
|
64 |
+
url.netloc,
|
65 |
+
url.scheme,
|
66 |
+
self._region,
|
67 |
+
'amazonaws',
|
68 |
+
'com',
|
69 |
+
]
|
70 |
+
netloc = '.'.join(parts)
|
71 |
+
url = url._replace(scheme='https', netloc=netloc)
|
72 |
+
|
73 |
+
super().__init__(url)
|
74 |
|
75 |
#
|
76 |
#
|
|
|
82 |
(Split.TEST, 'test', 'test'),
|
83 |
)))
|
84 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
85 |
@property
|
86 |
def labels(self):
|
87 |
+
path = StandardStyleAccess(self.metaname('dev'))
|
88 |
+
df = wr.s3.read_csv(str(path), compression='gzip')
|
89 |
yield from df['label'].dropna().unique()
|
90 |
|
91 |
def __init__(self, bucket):
|
|
|
94 |
|
95 |
def __call__(self, dl_manager):
|
96 |
for i in self._splits:
|
97 |
+
name = self.metaname(i.basename)
|
98 |
+
url = VirtualStyleAccess(name)
|
99 |
+
info = Path(dl_manager.download(str(url)))
|
100 |
+
|
101 |
+
images = self.images(i.split, info)
|
102 |
+
ipaths = dl_manager.download(dict(images))
|
103 |
|
104 |
+
payload = SplitPayload(i.split, info, ipaths)
|
105 |
yield SplitGenerator(name=i.dtype, gen_kwargs=asdict(payload))
|
106 |
|
107 |
+
@staticmethod
|
108 |
+
def images(split, info):
|
109 |
+
df = readsp(info, split)
|
110 |
+
for i in df['url'].unique():
|
111 |
+
url = VirtualStyleAccess(urlparse(i))
|
112 |
+
yield (i, str(url))
|
113 |
+
|
114 |
+
def metaname(self, split):
|
115 |
path = self.path.joinpath(split).with_suffix('.csv.gz')
|
116 |
+
return self.bucket._replace(path=str(path))
|
|
|
117 |
|
118 |
#
|
119 |
#
|
120 |
#
|
121 |
class ExampleManager:
|
|
|
122 |
# _Pest = cl.namedtuple('_Pest', 'label, geometry')
|
123 |
# _Feature = cl.namedtuple('_Feature', 'image, pests')
|
124 |
|
|
|
146 |
self.payload = payload
|
147 |
|
148 |
def __iter__(self):
|
149 |
+
for i in self.payload:
|
150 |
+
key = urlunparse(i.source)
|
151 |
+
with i.target.open('rb') as fp:
|
152 |
+
raw = fp.read()
|
153 |
value = {
|
154 |
+
'image': {
|
155 |
+
'path': i.target,
|
156 |
+
'bytes': raw,
|
157 |
+
},
|
158 |
+
'pests': list(self.pests(i.df)),
|
159 |
}
|
160 |
|
161 |
+
yield (key, value)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
162 |
|
163 |
#
|
164 |
#
|