Skip to content

Commit 912ef30

Browse files
authored
Merge pull request #1 from zeroae/f-intake-driver
Create an Intake Driver for Darknet
2 parents bab1808 + c7a51ff commit 912ef30

File tree

15 files changed

+552
-139
lines changed

15 files changed

+552
-139
lines changed

environment.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,15 @@ dependencies:
2323
- click-plugins
2424
- darknet
2525
- entrypoints
26+
- fsspec
27+
- intake
28+
- numpy
2629
- pillow
2730

2831
# Test Requirements (setup.py:test_requirements)
2932
- pytest >=3
3033
- pytest-cov
34+
- pytest-mock
3135

3236
# Documentation Requirements (setup.py:doc_requirements)
3337
- sphinx

examples/coco.ipynb

Lines changed: 35 additions & 51 deletions
Large diffs are not rendered by default.

examples/imagenet1k-intake.ipynb

Lines changed: 165 additions & 0 deletions
Large diffs are not rendered by default.

examples/imagenet1k.ipynb

Lines changed: 84 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -2,41 +2,41 @@
22
"cells": [
33
{
44
"cell_type": "code",
5-
"execution_count": 3,
5+
"execution_count": 1,
66
"metadata": {
77
"collapsed": true
88
},
99
"outputs": [],
1010
"source": [
1111
"import os\n",
12-
"import darknet\n",
13-
"import pydarknet\n",
12+
"import fsspec\n",
13+
"\n",
14+
"from PIL import Image as PILImage\n",
1415
"\n",
15-
"from PIL import Image as PILImage"
16+
"from darknet.py import ImageClassifier"
1617
]
1718
},
1819
{
1920
"cell_type": "code",
2021
"execution_count": 2,
21-
"outputs": [],
22-
"source": [
23-
"# Darknet is stupid... so all paths are relative at the moment.\n",
24-
"os.chdir(\"..\")"
25-
],
2622
"metadata": {
27-
"collapsed": false,
2823
"pycharm": {
2924
"name": "#%%\n"
3025
}
31-
}
26+
},
27+
"outputs": [],
28+
"source": [
29+
"darknet_gh_url = \"github://pjreddie:darknet@master\""
30+
]
3231
},
3332
{
3433
"cell_type": "code",
3534
"execution_count": 3,
3635
"outputs": [],
3736
"source": [
3837
"# Load the ImageNet 1k labels/metadata\n",
39-
"imagenet = darknet.Metadata(\"cfg/imagenet1k.data\")"
38+
"with fsspec.open(f\"{darknet_gh_url}/data/imagenet.shortnames.list\", mode=\"rt\") as f:\n",
39+
" labels = [line.rstrip() for line in f.readlines()[:1000]]"
4040
],
4141
"metadata": {
4242
"collapsed": false,
@@ -47,61 +47,59 @@
4747
},
4848
{
4949
"cell_type": "code",
50-
"execution_count": 4,
51-
"outputs": [],
52-
"source": [
53-
"# Load the Classifier\n",
54-
"n = pydarknet.ImageClassifier(labels=imagenet.classes,\n",
55-
" config_file=\"cfg/darknet53_448.cfg\",\n",
56-
" weights_file=\"weights/darknet53_448.weights\")"
57-
],
50+
"execution_count": 5,
5851
"metadata": {
59-
"collapsed": false,
6052
"pycharm": {
6153
"name": "#%%\n"
6254
}
63-
}
55+
},
56+
"outputs": [],
57+
"source": [
58+
"n = ImageClassifier(labels=labels,\n",
59+
" config_url=f\"{darknet_gh_url}/cfg/darknet53_448.cfg\",\n",
60+
" weights_url=\"https://pjreddie.com/media/files/darknet53_448.weights\")"
61+
]
6462
},
6563
{
6664
"cell_type": "code",
67-
"execution_count": 5,
65+
"execution_count": 6,
66+
"metadata": {
67+
"pycharm": {
68+
"name": "#%%\n"
69+
}
70+
},
6871
"outputs": [
6972
{
7073
"data": {
71-
"text/plain": "[('malamute', 0.9832896),\n ('Eskimo dog', 0.0043422417),\n ('Siberian husky', 0.0032391667),\n ('Tibetan mastiff', 0.0030777778),\n ('Great Pyrenees', 0.0022481713)]"
74+
"text/plain": "[('malamute', 0.98354006),\n ('Eskimo dog', 0.0042837244),\n ('Siberian husky', 0.0031863458),\n ('Tibetan mastiff', 0.0030448402),\n ('Great Pyrenees', 0.0022190544)]"
7275
},
73-
"execution_count": 5,
76+
"execution_count": 6,
7477
"metadata": {},
7578
"output_type": "execute_result"
7679
}
7780
],
7881
"source": [
79-
"img_filename = \"data/dog.jpg\"\n",
80-
"n.classify(\"data/dog.jpg\", top=5)\n"
81-
],
82-
"metadata": {
83-
"collapsed": false,
84-
"pycharm": {
85-
"name": "#%%\n"
86-
}
87-
}
82+
"dog_url = f\"{darknet_gh_url}/data/dog.jpg\"\n",
83+
"n.classify(dog_url, top=5)"
84+
]
8885
},
8986
{
9087
"cell_type": "code",
91-
"execution_count": 6,
88+
"execution_count": 7,
9289
"outputs": [
9390
{
9491
"data": {
95-
"text/plain": "[('malamute', 0.9832896),\n ('Eskimo dog', 0.0043422417),\n ('Siberian husky', 0.0032391667),\n ('Tibetan mastiff', 0.0030777778),\n ('Great Pyrenees', 0.0022481713)]"
92+
"text/plain": "[('malamute', 0.98354006),\n ('Eskimo dog', 0.0042837244),\n ('Siberian husky', 0.0031863458),\n ('Tibetan mastiff', 0.0030448402),\n ('Great Pyrenees', 0.0022190544)]"
9693
},
97-
"execution_count": 6,
94+
"execution_count": 7,
9895
"metadata": {},
9996
"output_type": "execute_result"
10097
}
10198
],
10299
"source": [
103-
"pil_img = PILImage.open(img_filename)\n",
104-
"n.classify(pil_img, top=5)"
100+
"with fsspec.open(dog_url) as dog:\n",
101+
" res = n.classify(PILImage.open(dog), top=5)\n",
102+
"res"
105103
],
106104
"metadata": {
107105
"collapsed": false,
@@ -112,85 +110,90 @@
112110
},
113111
{
114112
"cell_type": "code",
115-
"execution_count": 2,
116-
"outputs": [],
113+
"execution_count": 8,
114+
"metadata": {
115+
"pycharm": {
116+
"name": "#%%\n"
117+
}
118+
},
119+
"outputs": [
120+
{
121+
"data": {
122+
"text/plain": "[('malamute', 0.98354006),\n ('Eskimo dog', 0.0042837244),\n ('Siberian husky', 0.0031863458),\n ('Tibetan mastiff', 0.0030448402),\n ('Great Pyrenees', 0.0022190544)]"
123+
},
124+
"execution_count": 8,
125+
"metadata": {},
126+
"output_type": "execute_result"
127+
}
128+
],
117129
"source": [
118130
"try:\n",
119131
" import cv2\n",
120132
" # Note: Please note that cv2 nd-arrays are h*w*c ordered.\n",
121-
" cv2_img = cv2.imread(img_filename)\n",
133+
" with fsspec.open(dog_url) as dog:\n",
134+
" cv2_img = cv2.imread(dog.name)\n",
122135
" cv2_img = cv2.cvtColor(cv2_img, cv2.COLOR_BGR2RGB)\n",
123-
" n.classify(cv2_img, top=5)\n",
136+
" res = n.classify(cv2_img, top=5)\n",
124137
"except ModuleNotFoundError:\n",
125-
" pass"
126-
],
138+
" pass\n",
139+
"res"
140+
]
141+
},
142+
{
143+
"cell_type": "code",
144+
"execution_count": 9,
127145
"metadata": {
128-
"collapsed": false,
129146
"pycharm": {
130147
"name": "#%%\n"
131148
}
132-
}
133-
},
134-
{
135-
"cell_type": "code",
136-
"execution_count": 8,
149+
},
137150
"outputs": [
138151
{
139152
"data": {
140-
"text/plain": "[('bald eagle', 0.55398),\n ('vulture', 0.21862003),\n ('kite', 0.19144633),\n ('ruddy turnstone', 0.004691909),\n ('ruffed grouse', 0.0033115256)]"
153+
"text/plain": "[('bald eagle', 0.55666465),\n ('vulture', 0.21876547),\n ('kite', 0.18937683),\n ('ruddy turnstone', 0.004589723),\n ('ruffed grouse', 0.0032499917)]"
141154
},
142-
"execution_count": 8,
155+
"execution_count": 9,
143156
"metadata": {},
144157
"output_type": "execute_result"
145158
}
146159
],
147160
"source": [
148-
"n.classify(\"data/eagle.jpg\", top=5)"
149-
],
161+
"n.classify(f\"{darknet_gh_url}/data/eagle.jpg\", top=5)"
162+
]
163+
},
164+
{
165+
"cell_type": "code",
166+
"execution_count": 10,
150167
"metadata": {
151-
"collapsed": false,
152168
"pycharm": {
153169
"name": "#%%\n"
154170
}
155-
}
156-
},
157-
{
158-
"cell_type": "code",
159-
"execution_count": 9,
171+
},
160172
"outputs": [
161173
{
162174
"data": {
163-
"text/plain": "[('electric guitar', 0.9876499),\n ('acoustic guitar', 0.009498796),\n ('banjo', 0.0011737668),\n ('pick', 0.00072627096),\n ('stage', 0.0005768967)]"
175+
"text/plain": "[('electric guitar', 0.98759043),\n ('acoustic guitar', 0.009553942),\n ('banjo', 0.0011607071),\n ('pick', 0.0007309786),\n ('stage', 0.00058993115)]"
164176
},
165-
"execution_count": 9,
177+
"execution_count": 10,
166178
"metadata": {},
167179
"output_type": "execute_result"
168180
}
169181
],
170182
"source": [
171-
"import requests\n",
172183
"majesty_url=\"https://s3-us-west-2.amazonaws.com/static.music-man.com/website/images/instruments/instrument-77.png?1588624445\"\n",
173-
"img = PILImage.open(requests.get(majesty_url, stream=True).raw)\n",
174-
"n.classify(img, top=5)"
175-
],
176-
"metadata": {
177-
"collapsed": false,
178-
"pycharm": {
179-
"name": "#%%\n"
180-
}
181-
}
184+
"n.classify(majesty_url, top=5)"
185+
]
182186
},
183187
{
184188
"cell_type": "code",
185189
"execution_count": null,
186-
"outputs": [],
187-
"source": [],
188190
"metadata": {
189-
"collapsed": false,
190191
"pycharm": {
191192
"name": "#%%\n"
192193
}
193-
}
194+
},
195+
"outputs": [],
196+
"source": []
194197
}
195198
],
196199
"metadata": {
@@ -202,16 +205,16 @@
202205
"language_info": {
203206
"codemirror_mode": {
204207
"name": "ipython",
205-
"version": 2
208+
"version": 3
206209
},
207210
"file_extension": ".py",
208211
"mimetype": "text/x-python",
209212
"name": "python",
210213
"nbconvert_exporter": "python",
211-
"pygments_lexer": "ipython2",
212-
"version": "2.7.6"
214+
"pygments_lexer": "ipython3",
215+
"version": "3.8.3"
213216
}
214217
},
215218
"nbformat": 4,
216-
"nbformat_minor": 0
219+
"nbformat_minor": 1
217220
}

setup.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
"click>=7.0",
1515
"click-plugins",
1616
"entrypoints",
17+
"fsspec",
18+
"intake",
1719
"numpy",
1820
"pillow",
1921
# fmt: on
@@ -33,6 +35,7 @@
3335
# fmt: off
3436
"pytest>=3",
3537
"pytest-cov",
38+
"pytest-mock",
3639
# fmt: on
3740
]
3841

@@ -97,6 +100,12 @@
97100
"darknet.cli": [
98101
"py=darknet.py.cli:py",
99102
],
103+
"intake.drivers": [
104+
"darknet = darknet.py.intake:DarknetSource"
105+
],
106+
"intake.catalogs": [
107+
"darknet = darknet.zoo:cat"
108+
]
100109
},
101110
# fmt: on
102111
ext_modules=ext_modules,

src/darknet/py/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
"""Top-level package for DarkNet OpenSource Neural Networks in Python."""
22
from ._version import version as __version__ # noqa: F401
3-
43
from .network import Network
54
from .classifier import Classifier, ImageClassifier
65
from .detector import ImageDetector

src/darknet/py/classifier.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ class ClassifierBase(ABC):
1010
network: Network
1111
labels: list
1212

13-
def __init__(self, labels, config_file, weights_file, **kwargs):
14-
self.network = Network(config_file, weights_file, **kwargs)
13+
def __init__(self, labels, config_url, weights_url, **kwargs):
14+
self.network = Network.open(config_url, weights_url, **kwargs)
1515

1616
self.labels = range(self.network.output_size()) if labels is None else labels
1717
if len(self.labels) != self.network.output_size():

src/darknet/py/detector.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ class ImageDetector(object):
88

99
_last_image_size = None
1010

11-
def __init__(self, labels, config_file, weights_file, **kwargs):
12-
self.network = Network(config_file, weights_file, **kwargs)
11+
def __init__(self, labels, config_url, weights_url, **kwargs):
12+
self.network = Network.open(config_url, weights_url, **kwargs)
1313
self.labels = labels
1414

1515
def detect(self, image, **kwargs):

0 commit comments

Comments
 (0)