Skip to content

Commit

Permalink
Merge pull request #174 from supervisely-ecosystem/a_supporting_ui_te…
Browse files Browse the repository at this point in the history
…mplate

Supporting GUI template
  • Loading branch information
max-unfinity committed Mar 14, 2023
2 parents 13d7d5a + d0ff654 commit 0e997be
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 25 deletions.
6 changes: 1 addition & 5 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"env": {
"LOG_LEVEL": "DEBUG",
"PYTHONPATH": "${workspaceFolder}:${PYTHONPATH}",
"SLY_APP_DATA_DIR": "${workspaceFolder}/results",
}
},
{
Expand All @@ -35,13 +36,8 @@
"PYTHONPATH": "${workspaceFolder}:${PYTHONPATH}",
"LOG_LEVEL": "DEBUG",
"ENV": "production",
"TEAM_ID": "277",
"WORKSPACE_ID": "403",
"DEBUG_WITH_SLY_NET": "1",
"SLY_APP_DATA_DIR": "${workspaceFolder}/results",
"modal.state.modelWeightsOptions": "pretrained",
"modal.state.selectedModel": "YOLOv5s",
"modal.state.weightsPath": ""
}
}
]
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# pip install -r requirements.txt
supervisely==6.69.15
supervisely==6.69.70

opencv-python-headless==4.5.5.62
opencv-python==4.5.5.62
Expand Down
8 changes: 3 additions & 5 deletions supervisely/serve/local.env
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
PYTHONUNBUFFERED=1

APP_NAME="Serve YOLOv5"
SLY_APP_DATA_DIR="./data_dir"
LOG_LEVEL="trace"

context.teamId=277
context.workspaceId=403
TEAM_ID=440
WORKSPACE_ID=662

# "pretrained" or "custom"
modal.state.modelWeightsOptions="pretrained"
#"pretrained" "custom"
modal.state.selectedModel="YOLOv5s"
modal.state.weightsPath=""
22 changes: 8 additions & 14 deletions supervisely/serve/src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,21 @@
pretrained_weights = os.environ['modal.state.selectedModel'].lower()
custom_weights = os.environ['modal.state.weightsPath']

pretrained_weights_url = f"https://github.com/ultralytics/yolov5/releases/download/v5.0/{pretrained_weights}.pt"

class YOLOv5Model(sly.nn.inference.ObjectDetection):
def load_on_device(
self,
model_dir: str = None,
device: Literal["cpu", "cuda", "cuda:0", "cuda:1", "cuda:2", "cuda:3"] = "cpu",
):
# download weights
if model_weights_options == "pretrained":
self.local_weights_path = self.location
self.local_weights_path = self.download(pretrained_weights_url)
if model_weights_options == "custom":
self.local_weights_path = self.location[0]
configs_local_path = self.location[1]
self.local_weights_path = self.download(custom_weights)
cfg_path_in_teamfiles = os.path.join(Path(custom_weights).parents[1], 'opt.yaml')
configs_local_path = self.download(cfg_path_in_teamfiles)

self.device = select_device(device)
self.half = self.device.type != 'cpu' # half precision only supported on CUDA
Expand Down Expand Up @@ -138,7 +142,6 @@ def predict(

return predictions


def predict_raw(
self, image_path: str, settings: Dict[str, Any]
) -> List[sly.nn.PredictionBBox]:
Expand Down Expand Up @@ -188,20 +191,11 @@ def predict_raw(
device = "cuda" if torch.cuda.is_available() else "cpu"
print("Using device:", device)

if model_weights_options == "pretrained":
location = f"https://github.com/ultralytics/yolov5/releases/download/v5.0/{pretrained_weights}.pt"
elif model_weights_options == "custom":
location = [
custom_weights, # model weights
os.path.join(Path(custom_weights).parents[1], 'opt.yaml') # model config
]

m = YOLOv5Model(
location=location,
custom_inference_settings=os.path.join(app_source_path, "custom_settings.yaml"),
sliding_window_mode = "advanced"
)
m.load_on_device(device)
m.load_on_device(device=device)

if sly.is_production():
# this code block is running on Supervisely platform in production
Expand Down

0 comments on commit 0e997be

Please sign in to comment.