Skip to content

Commit 594d328

Browse files
authored
update: server (#121)
* update: server * update: server
1 parent 71a6739 commit 594d328

File tree

4 files changed

+57
-50
lines changed

4 files changed

+57
-50
lines changed

imcui/api/config/api.yaml

Lines changed: 12 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,15 @@
1-
# This file was generated using the `serve build` command on Ray v2.38.0.
2-
3-
proxy_location: EveryNode
4-
http_options:
5-
host: 0.0.0.0
6-
port: 8001
7-
8-
grpc_options:
9-
port: 9000
10-
grpc_servicer_functions: []
11-
12-
logging_config:
13-
encoding: TEXT
14-
log_level: INFO
15-
logs_dir: null
16-
enable_access_log: true
17-
18-
applications:
19-
- name: app1
20-
route_prefix: /
21-
import_path: api.server:service
22-
runtime_env: {}
23-
deployments:
24-
- name: ImageMatchingService
25-
num_replicas: 4
26-
ray_actor_options:
27-
num_cpus: 2.0
28-
num_gpus: 1.0
1+
service:
2+
num_replicas: 4
3+
ray_actor_options:
4+
num_cpus: 2.0
5+
num_gpus: 1.0
6+
host: &default_host
7+
"0.0.0.0"
8+
http_options:
9+
host: *default_host
10+
port: 8001
11+
route_prefix: "/"
12+
dashboard_port: 8265
2913

3014
api:
3115
feature:

imcui/api/server.py

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,38 +6,61 @@
66
import numpy as np
77
import ray
88
import torch
9-
import yaml
109
from fastapi import FastAPI, File, UploadFile
1110
from fastapi.responses import JSONResponse
1211
from PIL import Image
1312
from ray import serve
13+
import argparse
1414

1515
from . import ImagesInput, to_base64_nparray
1616
from .core import ImageMatchingAPI
1717
from ..hloc import DEVICE
18+
from ..hloc.utils.io import read_yaml
1819
from ..ui import get_version
1920

2021
warnings.simplefilter("ignore")
2122
app = FastAPI()
2223
if ray.is_initialized():
2324
ray.shutdown()
25+
26+
27+
# read some configs
28+
parser = argparse.ArgumentParser()
29+
parser.add_argument(
30+
"--config",
31+
type=Path,
32+
required=False,
33+
default=Path(__file__).parent / "config/api.yaml",
34+
)
35+
args = parser.parse_args()
36+
config_path = args.config
37+
config = read_yaml(config_path)
38+
num_gpus = 1 if torch.cuda.is_available() else 0
39+
ray_actor_options = config["service"].get("ray_actor_options", {})
40+
ray_actor_options.update({"num_gpus": num_gpus})
41+
dashboard_port = config["service"].get("dashboard_port", 8265)
42+
http_options = config["service"].get(
43+
"http_options",
44+
{
45+
"host": "0.0.0.0",
46+
"port": 8001,
47+
},
48+
)
49+
num_replicas = config["service"].get("num_replicas", 4)
2450
ray.init(
25-
dashboard_port=8265,
51+
dashboard_port=dashboard_port,
2652
ignore_reinit_error=True,
2753
)
28-
serve.start(
29-
http_options={"host": "0.0.0.0", "port": 8001},
30-
)
31-
32-
num_gpus = 1 if torch.cuda.is_available() else 0
54+
serve.start(http_options=http_options)
3355

3456

3557
@serve.deployment(
36-
num_replicas=4, ray_actor_options={"num_cpus": 2, "num_gpus": num_gpus}
58+
num_replicas=num_replicas,
59+
ray_actor_options=ray_actor_options,
3760
)
3861
@serve.ingress(app)
3962
class ImageMatchingService:
40-
def __init__(self, conf: dict, device: str):
63+
def __init__(self, conf: dict, device: str, **kwargs):
4164
self.conf = conf
4265
self.api = ImageMatchingAPI(conf=conf, device=device)
4366

@@ -137,7 +160,7 @@ def load_image(self, file_path: Union[str, UploadFile]) -> np.ndarray:
137160
image_array = np.array(img)
138161
return image_array
139162

140-
def postprocess(self, output: dict, skip_keys: list, binarize: bool = True) -> dict:
163+
def postprocess(self, output: dict, skip_keys: list, **kwargs) -> dict:
141164
pred = {}
142165
for key, value in output.items():
143166
if key in skip_keys:
@@ -152,19 +175,12 @@ def run(self, host: str = "0.0.0.0", port: int = 8001):
152175
uvicorn.run(app, host=host, port=port)
153176

154177

155-
def read_config(config_path: Path) -> dict:
156-
with open(config_path, "r") as f:
157-
conf = yaml.safe_load(f)
158-
return conf
159-
160-
161-
# api server
162-
conf = read_config(Path(__file__).parent / "config/api.yaml")
163-
service = ImageMatchingService.bind(conf=conf["api"], device=DEVICE)
164-
handle = serve.run(service, route_prefix="/")
178+
if __name__ == "__main__":
179+
# api server
180+
service = ImageMatchingService.bind(conf=config["api"], device=DEVICE)
181+
handle = serve.run(service, route_prefix="/", blocking=False)
165182

166183
# serve run api.server_ray:service
167-
168184
# build to generate config file
169185
# serve build api.server_ray:service -o api/config/ray.yaml
170186
# serve run api/config/ray.yaml

imcui/hloc/utils/io.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import numpy as np
44
import cv2
55
import h5py
6+
import yaml
67

78
from .parsers import names_to_pair, names_to_pair_old
89

@@ -75,3 +76,9 @@ def get_matches(path: Path, name0: str, name1: str) -> Tuple[np.ndarray]:
7576
matches = np.flip(matches, -1)
7677
scores = scores[idx]
7778
return matches, scores
79+
80+
81+
def read_yaml(config_path: Path) -> dict:
82+
with open(config_path, "r") as f:
83+
conf = yaml.safe_load(f)
84+
return conf

imcui/ui/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
__version__ = "1.0.1"
1+
__version__ = "1.3.0"
22

33

44
def get_version():

0 commit comments

Comments
 (0)