aryanxxvii
commited on
Commit
·
0008ffa
1
Parent(s):
9015d82
Add config.json with model_type
Browse files- config.json +8 -1
- inference.py +0 -34
- model_info.json +0 -4
- requirements.txt +2 -1
- u2net_pipeline.py +54 -0
config.json
CHANGED
@@ -1,3 +1,10 @@
|
|
1 |
{
|
2 |
-
"model_type": "
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
}
|
|
|
1 |
{
|
2 |
+
"model_type": "u2net",
|
3 |
+
"architectures": [
|
4 |
+
"U2NET"
|
5 |
+
],
|
6 |
+
"task": "image-segmentation",
|
7 |
+
"pipeline_class": "U2NetPipeline",
|
8 |
+
"model_file": "u2net.py",
|
9 |
+
"weights_file": "u2net.pth"
|
10 |
}
|
inference.py
DELETED
@@ -1,34 +0,0 @@
|
|
1 |
-
import torch
|
2 |
-
from u2net import U2NET
|
3 |
-
from torchvision import transforms
|
4 |
-
import numpy as np
|
5 |
-
from PIL import Image
|
6 |
-
import torch.nn.functional as F
|
7 |
-
import data_transforms
|
8 |
-
|
9 |
-
# Load the model
|
10 |
-
def load_model():
|
11 |
-
model = U2NET(3, 1)
|
12 |
-
model.load_state_dict(torch.load("u2net.pth", map_location="cpu"))
|
13 |
-
model.eval()
|
14 |
-
return model
|
15 |
-
|
16 |
-
# Preprocessing function (same as you defined locally)
|
17 |
-
def preprocess(image):
|
18 |
-
transform = transforms.Compose([data_transforms.RescaleT(320), data_transforms.ToTensorLab(flag=0)])
|
19 |
-
label_3 = np.zeros(image.shape)
|
20 |
-
label = np.zeros(label_3.shape[0:2])
|
21 |
-
sample = transform({"imidx": np.array([0]), "image": image, "label": label})
|
22 |
-
return sample
|
23 |
-
|
24 |
-
# Inference function
|
25 |
-
def infer(model, image):
|
26 |
-
input_size = [1024, 1024]
|
27 |
-
im_shp = image.shape[0:2]
|
28 |
-
im_tensor = torch.tensor(image, dtype=torch.float32).permute(2, 0, 1)
|
29 |
-
im_tensor = F.upsample(torch.unsqueeze(im_tensor, 0), input_size, mode="bilinear").type(torch.uint8)
|
30 |
-
image = torch.divide(im_tensor, 255.0)
|
31 |
-
result = model(image)
|
32 |
-
result = torch.squeeze(F.upsample(result[0][0], im_shp, mode='bilinear'), 0)
|
33 |
-
result = (result - result.min()) / (result.max() - result.min())
|
34 |
-
return result.numpy()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
model_info.json
DELETED
@@ -1,4 +0,0 @@
|
|
1 |
-
{
|
2 |
-
"model_type": "image-segmentation",
|
3 |
-
"task": "image-segmentation"
|
4 |
-
}
|
|
|
|
|
|
|
|
|
|
requirements.txt
CHANGED
@@ -1,7 +1,8 @@
|
|
1 |
torch
|
2 |
torchvision
|
3 |
numpy
|
4 |
-
|
5 |
matplotlib
|
6 |
scikit-image
|
7 |
huggingface-hub
|
|
|
|
1 |
torch
|
2 |
torchvision
|
3 |
numpy
|
4 |
+
Pillow
|
5 |
matplotlib
|
6 |
scikit-image
|
7 |
huggingface-hub
|
8 |
+
transformers
|
u2net_pipeline.py
ADDED
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
from torchvision import transforms
|
3 |
+
from PIL import Image
|
4 |
+
import numpy as np
|
5 |
+
import torch.nn.functional as F
|
6 |
+
from u2net import U2NET
|
7 |
+
import data_transforms
|
8 |
+
from transformers import Pipeline
|
9 |
+
|
10 |
+
class U2NetPipeline(Pipeline):
|
11 |
+
def __init__(self, model, **kwargs):
|
12 |
+
super().__init__(model=model, **kwargs)
|
13 |
+
self.model = U2NET(3, 1)
|
14 |
+
self.model.load_state_dict(torch.load(model, map_location="cpu"))
|
15 |
+
self.model.eval()
|
16 |
+
|
17 |
+
def _sanitize_parameters(self, **kwargs):
|
18 |
+
return {}, {}, {}
|
19 |
+
|
20 |
+
def preprocess(self, image):
|
21 |
+
if isinstance(image, str):
|
22 |
+
image = Image.open(image).convert("RGB")
|
23 |
+
elif isinstance(image, Image.Image):
|
24 |
+
image = image.convert("RGB")
|
25 |
+
else:
|
26 |
+
raise ValueError("Input must be a PIL Image or a path to an image file")
|
27 |
+
|
28 |
+
image = np.array(image)
|
29 |
+
transform = transforms.Compose([data_transforms.RescaleT(320), data_transforms.ToTensorLab(flag=0)])
|
30 |
+
sample = transform({"imidx": np.array([0]), "image": image, "label": np.zeros(image.shape[:2])})
|
31 |
+
|
32 |
+
input_size = [1024, 1024]
|
33 |
+
im_tensor = sample['image'].unsqueeze(0)
|
34 |
+
im_tensor = F.interpolate(im_tensor, input_size, mode="bilinear")
|
35 |
+
image = torch.divide(im_tensor, 255.0)
|
36 |
+
image = transforms.Normalize([0.5, 0.5, 0.5], [1.0, 1.0, 1.0])(image)
|
37 |
+
|
38 |
+
return {"image": image, "original_size": image.shape[2:]}
|
39 |
+
|
40 |
+
def _forward(self, model_inputs):
|
41 |
+
with torch.no_grad():
|
42 |
+
outputs = self.model(model_inputs["image"])
|
43 |
+
return {"outputs": outputs, "original_size": model_inputs["original_size"]}
|
44 |
+
|
45 |
+
def postprocess(self, model_outputs):
|
46 |
+
result = model_outputs["outputs"][0][0]
|
47 |
+
result = F.interpolate(result, size=model_outputs["original_size"], mode='bilinear', align_corners=False)
|
48 |
+
result = result.squeeze().cpu().numpy()
|
49 |
+
ma, mi = result.max(), result.min()
|
50 |
+
result = (result - mi) / (ma - mi)
|
51 |
+
return (result * 255).astype(np.uint8)
|
52 |
+
|
53 |
+
def load_model():
|
54 |
+
return U2NetPipeline("u2net.pth")
|