-
Notifications
You must be signed in to change notification settings - Fork 175
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
886e27d
commit f37178b
Showing
20 changed files
with
2,046 additions
and
0 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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,42 @@ | ||
import cv2, requests, json, argparse | ||
import matplotlib.pyplot as plt | ||
|
||
parser = argparse.ArgumentParser(description="Pass a name to a file to be annotated") | ||
parser.add_argument('--image_path', default="test/images/a9f16c_2_9_png.rf.c048a60764e56735d7465cdec974d102.jpg") | ||
|
||
MODEL_IMG_SIZE = 640 | ||
ENDPOINT_URL_FINETUNED = "http://localhost:5543/yolov5-s-finetuned/predict/from_files" | ||
ENDPOINT_URL_COCO = "http://localhost:5543/yolov5-s-coco/predict/from_files" | ||
|
||
def main(image_path): | ||
for endpoint_url in [ENDPOINT_URL_FINETUNED, ENDPOINT_URL_COCO]: | ||
im = cv2.imread(image_path) | ||
im_size = im.shape[:2] | ||
assert im_size[0] == im_size[1] | ||
scale_ratio = im_size[0] / MODEL_IMG_SIZE | ||
|
||
resp = requests.post( | ||
url=endpoint_url, | ||
files=[('request', open(image_path, 'rb'))] | ||
) | ||
|
||
boxes = json.loads(resp.text)['boxes'][0] | ||
for xmin, ymin, xmax, ymax in boxes: | ||
start_point = (int(xmin * scale_ratio), int(ymin * scale_ratio)) | ||
end_point = (int(xmax * scale_ratio), int(ymax * scale_ratio)) | ||
color = (0, 255, 0) | ||
thickness = 2 | ||
im = cv2.rectangle(im, start_point, end_point, color, thickness) | ||
|
||
plt.figure(figsize=(15, 15)) | ||
plt.axis("off") | ||
plt.imshow(im) | ||
|
||
if endpoint_url == ENDPOINT_URL_FINETUNED: | ||
plt.savefig("annotated-finetuned.png") | ||
else: | ||
plt.savefig("annotated-coco.png") | ||
|
||
if __name__ == '__main__': | ||
args = parser.parse_args() | ||
main(args.image_path) |
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,27 @@ | ||
import requests, os, time | ||
|
||
# the dataset we downloaded had 3 subsets in 3 folders | ||
paths = [] | ||
for folder_name in ['test', 'valid', 'train']: | ||
path = f"{folder_name}/images/" | ||
paths += [path + img_name for img_name in os.listdir(path)] | ||
|
||
|
||
# same URL for the endpoint as before | ||
ENDPOINT_URL = "http://localhost:5543/yolov5-s-coco/predict/from_files" | ||
|
||
|
||
# send each image to the endpoint | ||
i = 0 | ||
print(f"Sending {len(paths)} images to the server") | ||
for image_path in paths: | ||
if i % 60 == 0: | ||
print(i) | ||
i+=1 | ||
|
||
resp = requests.post( | ||
url=ENDPOINT_URL, | ||
files=[('request', open(image_path, 'rb'))] | ||
) | ||
print(resp) | ||
time.sleep(1) |
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,11 @@ | ||
import requests, json | ||
|
||
ENDPOINT_URL = "http://localhost:5543/yolov5-s-coco/predict/from_files" | ||
IMAGE_PATH = "test/images/4b770a_3_6_png.rf.f5d975605c1f73e1a95a1d8edc4ce5b1.jpg" | ||
|
||
resp = requests.post( | ||
url=ENDPOINT_URL, | ||
files=[('request', open(IMAGE_PATH, 'rb'))] | ||
) | ||
|
||
print(json.loads(resp.text)) |
Large diffs are not rendered by default.
Oops, something went wrong.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,2 @@ | ||
deepsparse[server,yolo] | ||
roboflow |
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,28 @@ | ||
from deepsparse.loggers import BaseLogger, MetricCategories | ||
import typing, PIL, io, requests, datetime | ||
from requests_toolbelt.multipart.encoder import MultipartEncoder | ||
|
||
class RoboflowLogger(BaseLogger): | ||
|
||
# the arguments to the construction will be defined in the config file | ||
def __init__(self, dataset_name: str, api_key: str): | ||
# per Roboflow docs | ||
self.upload_url = f"https://api.roboflow.com/dataset/{dataset_name}/upload?api_key={api_key}" | ||
super(RoboflowLogger, self).__init__() | ||
|
||
# this function will be called from DeepSparse Server, based on the config | ||
def log(self, identifier: str, value: typing.Any, category: typing.Optional[str]=None): | ||
if category == MetricCategories.DATA: | ||
# unpacks value and converts to image in a buffer | ||
img = PIL.Image.fromarray(value.images[0], mode="RGB") | ||
buffered = io.BytesIO() | ||
img.save(buffered, quality=90, format="JPEG") | ||
|
||
# packs as multipart | ||
img_name = f"production-image-{datetime.datetime.now()}.jpg" | ||
m = MultipartEncoder(fields={'file': (img_name, buffered.getvalue(), "image/jpeg")}) | ||
|
||
# uploads to roboflow | ||
r = requests.post(self.upload_url, data=m, headers={'Content-Type': m.content_type}) | ||
|
||
print("request_complete") |
9 changes: 9 additions & 0 deletions
9
examples/roboflow-logging/server/server-config-no-roboflow-logging.yaml
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,9 @@ | ||
## server-config-no-roboflow-logging.yaml | ||
loggers: | ||
python: | ||
|
||
endpoints: | ||
- task: yolo | ||
model: zoo:cv/detection/yolov5-s/pytorch/ultralytics/coco/pruned_quant-aggressive_94 | ||
name: yolov5-s-coco | ||
route: /yolov5-s-coco/predict |
18 changes: 18 additions & 0 deletions
18
examples/roboflow-logging/server/server-config-roboflow-logging.yaml
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,18 @@ | ||
# server-config-roboflow-logging.yaml | ||
loggers: | ||
roboflow_logger: | ||
path: server/roboflow-logger.py:RoboflowLogger | ||
api_key: YOUR_API_KEY | ||
dataset_name: YOUR_DATASET_NAME | ||
|
||
endpoints: | ||
- task: yolo | ||
model: zoo:cv/detection/yolov5-s/pytorch/ultralytics/coco/pruned_quant-aggressive_94 | ||
name: yolov5-s-coco | ||
route: /yolov5-s-coco/predict | ||
data_logging: | ||
pipeline_inputs: | ||
- func: identity | ||
frequency: 1 | ||
target_loggers: | ||
- roboflow_logger |