Ben Galewsky commited on
Commit
d1eb2ce
1 Parent(s): 30457d4

Add example

Browse files
Files changed (5) hide show
  1. MLmodel +30 -0
  2. conda.yaml +16 -0
  3. inference.py +44 -0
  4. python_env.yaml +7 -0
  5. requirements.txt +9 -0
MLmodel ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ artifact_path: model
2
+ flavors:
3
+ python_function:
4
+ config:
5
+ device: null
6
+ data: data
7
+ env:
8
+ conda: conda.yaml
9
+ virtualenv: python_env.yaml
10
+ loader_module: mlflow.pytorch
11
+ pickle_module_name: mlflow.pytorch.pickle_module
12
+ python_version: 3.10.11
13
+ pytorch:
14
+ code: null
15
+ model_data: data
16
+ pytorch_version: 2.5.0+cu124
17
+ mlflow_version: 2.17.0
18
+ model_size_bytes: 176239334
19
+ model_uuid: 23fc55bd7bea4ce0b7ea15d15fb19d4f
20
+ run_id: 5bc0ffd24c5748ba94f2adc6f33d83f6
21
+ signature:
22
+ inputs: '[{"type": "tensor", "tensor-spec": {"dtype": "float32", "shape": [1, 3,
23
+ 226, 288]}}]'
24
+ outputs: '[{"name": "boxes", "type": "tensor", "tensor-spec": {"dtype": "float32",
25
+ "shape": [71, 4]}}, {"name": "labels", "type": "tensor", "tensor-spec": {"dtype":
26
+ "int64", "shape": [71]}}, {"name": "scores", "type": "tensor", "tensor-spec":
27
+ {"dtype": "float32", "shape": [71]}}, {"name": "masks", "type": "tensor", "tensor-spec":
28
+ {"dtype": "float32", "shape": [71, 1, 226, 288]}}]'
29
+ params: null
30
+ utc_time_created: '2024-10-23 22:18:33.740363'
conda.yaml ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ channels:
2
+ - conda-forge
3
+ dependencies:
4
+ - python=3.10.11
5
+ - pip<=23.1.2
6
+ - pip:
7
+ - mlflow==2.17.0
8
+ - cloudpickle==3.1.0
9
+ - colorama==0.4.6
10
+ - defusedxml==0.7.1
11
+ - nvidia-ml-py==12.560.30
12
+ - pandas==2.2.3
13
+ - torch==2.5.0
14
+ - torchvision==0.20.0
15
+ - tqdm==4.66.5
16
+ name: mlflow-env
inference.py ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import requests
3
+ import tempfile
4
+ from torchvision.io import read_image
5
+
6
+ def preprocess(image):
7
+ from torchvision.io import read_image
8
+ from torchvision.transforms import v2 as T
9
+ transforms = []
10
+
11
+ transforms.append(T.ToDtype(torch.float, scale=True))
12
+ transforms.append(T.ToPureTensor())
13
+ transform = T.Compose(transforms)
14
+
15
+ image = (255.0 * (image - image.min()) / (image.max() - image.min())).to(
16
+ torch.uint8
17
+ )
18
+ image = image[:3, ...]
19
+ transformed_image = transform(image)
20
+
21
+ x = torch.unsqueeze(transformed_image, 0)
22
+
23
+ return x # [:3, ...]
24
+
25
+ def filter_predictions(pred, score_threshold=0.5):
26
+ keep = pred["scores"] > score_threshold
27
+ return {k: v[keep] for k, v in pred.items()}
28
+
29
+
30
+ model = torch.load("model.pth", map_location=torch.device('cpu'))
31
+ image_url = "https://github.com/cyber2a/Cyber2A-RTS-ToyModel/blob/main/data/images/valtest_nitze_008.jpg?raw=true"
32
+ response = requests.get(image_url)
33
+ with tempfile.NamedTemporaryFile(delete=True, suffix='.jpg') as tmp_file:
34
+ # Write the content to the temporary file
35
+ tmp_file.write(response.content)
36
+ tmp_file_path = tmp_file.name
37
+ image = read_image(tmp_file_path)
38
+
39
+ scaled_tensor = preprocess(image)
40
+ with torch.no_grad():
41
+ output = model(scaled_tensor)
42
+
43
+
44
+ print(filter_predictions(output[0]))
python_env.yaml ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ python: 3.10.11
2
+ build_dependencies:
3
+ - pip==23.1.2
4
+ - setuptools==67.7.2
5
+ - wheel==0.40.0
6
+ dependencies:
7
+ - -r requirements.txt
requirements.txt ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ mlflow==2.17.0
2
+ cloudpickle==3.1.0
3
+ colorama==0.4.6
4
+ defusedxml==0.7.1
5
+ nvidia-ml-py==12.560.30
6
+ pandas==2.2.3
7
+ torch==2.2.2
8
+ torchvision==0.17.2
9
+ tqdm==4.66.5