Spaces:
Running
on
A10G
Running
on
A10G
BertChristiaens
commited on
Commit
•
1cdf8e3
1
Parent(s):
dd0ab9f
add np
Browse files- helpers.py +1 -0
- test.py +50 -0
helpers.py
CHANGED
@@ -2,6 +2,7 @@ import gc
|
|
2 |
import torch
|
3 |
from scipy.signal import fftconvolve
|
4 |
from PIL import Image
|
|
|
5 |
|
6 |
def flush():
|
7 |
gc.collect()
|
|
|
2 |
import torch
|
3 |
from scipy.signal import fftconvolve
|
4 |
from PIL import Image
|
5 |
+
import numpy as np
|
6 |
|
7 |
def flush():
|
8 |
gc.collect()
|
test.py
ADDED
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
|
3 |
+
class FondantInferenceModel:
|
4 |
+
"""FondantInferenceModel class that abstracts the model loading and inference.
|
5 |
+
User needs to implement an inference, pre/postprocess step and pass the class to the FondantInferenceComponent.
|
6 |
+
The FondantInferenceComponent will then load the model and prepare it for inference.
|
7 |
+
The examples folder can then show examples for a pytorch / huggingface / tensorflow / ... model.
|
8 |
+
"""
|
9 |
+
def __init__(self, device: str = "cpu"):
|
10 |
+
self.device = device
|
11 |
+
# load model
|
12 |
+
self.model = self.load_model()
|
13 |
+
# set model to eval mode
|
14 |
+
self.eval()
|
15 |
+
|
16 |
+
def load_model(self):
|
17 |
+
# load model
|
18 |
+
...
|
19 |
+
|
20 |
+
def eval(self):
|
21 |
+
# prepare for inference
|
22 |
+
self.model = self.model.eval()
|
23 |
+
self.model = self.model.to(self.device)
|
24 |
+
|
25 |
+
def preprocess(self, input):
|
26 |
+
# preprocess input
|
27 |
+
...
|
28 |
+
|
29 |
+
def postprocess(self, output):
|
30 |
+
# postprocess output
|
31 |
+
...
|
32 |
+
|
33 |
+
def __call__(self, *args, **kwargs):
|
34 |
+
processed_inputs = self.preprocess(*args, **kwargs)
|
35 |
+
outputs = self.model(*processed_inputs)
|
36 |
+
processed_outputs = self.postprocess(outputs)
|
37 |
+
return processed_outputs
|
38 |
+
|
39 |
+
|
40 |
+
class FondantInferenceComponent(FondantTransformComponent, FondantInferenceModel):
|
41 |
+
# loads the model and prepares it for inference
|
42 |
+
|
43 |
+
def transform(
|
44 |
+
self, args: argparse.Namespace, dataframe: dd.DataFrame
|
45 |
+
) -> dd.DataFrame:
|
46 |
+
# by using the InferenceComponent, the model is automatically loaded and prepared for inference
|
47 |
+
# you just need to call the infer method
|
48 |
+
# the self.infer method calls the model.__call__ method of the FondantInferenceModel
|
49 |
+
output = self.infer(args.image)
|
50 |
+
|