Spaces:
Sleeping
Sleeping
logits to cpu
Browse files
app.py
CHANGED
@@ -15,7 +15,7 @@ device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
|
15 |
print(f"Is CUDA available: {torch.cuda.is_available()} --> {device=}")
|
16 |
if (torch.cuda.is_available()):
|
17 |
print(f"CUDA device: {torch.cuda.get_device_name(torch.cuda.current_device())}")
|
18 |
-
|
19 |
model.to(device)
|
20 |
|
21 |
# https://github.com/NielsRogge/Transformers-Tutorials/blob/master/SegFormer/Segformer_inference_notebook.ipynb
|
@@ -69,22 +69,24 @@ def call(image): #nparray
|
|
69 |
# resized_image = Image.fromarray(resized_image_np)
|
70 |
# print(f"{resized_image=}")
|
71 |
|
72 |
-
inputs = feature_extractor(images=resized_image, return_tensors="pt")
|
73 |
-
|
74 |
print(f"**processing time: {(time.time() - start):.2f} s")
|
75 |
|
76 |
outputs = model(**inputs)
|
77 |
-
|
|
|
|
|
78 |
# print(f"{logits}")
|
79 |
|
80 |
print(f"***processing time: {(time.time() - start):.2f} s")
|
81 |
# First, rescale logits to original image size
|
82 |
interpolated_logits = nn.functional.interpolate(
|
83 |
-
|
84 |
size=[1024, 1024], #resized_image.size[::-1], # (height, width)
|
85 |
mode='bilinear',
|
86 |
align_corners=False)
|
87 |
-
print(f"{interpolated_logits.shape=}, {
|
88 |
|
89 |
# Second, apply argmax on the class dimension
|
90 |
seg = interpolated_logits.argmax(dim=1)[0]
|
|
|
15 |
print(f"Is CUDA available: {torch.cuda.is_available()} --> {device=}")
|
16 |
if (torch.cuda.is_available()):
|
17 |
print(f"CUDA device: {torch.cuda.get_device_name(torch.cuda.current_device())}")
|
18 |
+
|
19 |
model.to(device)
|
20 |
|
21 |
# https://github.com/NielsRogge/Transformers-Tutorials/blob/master/SegFormer/Segformer_inference_notebook.ipynb
|
|
|
69 |
# resized_image = Image.fromarray(resized_image_np)
|
70 |
# print(f"{resized_image=}")
|
71 |
|
72 |
+
inputs = feature_extractor(images=resized_image, return_tensors="pt").to(device)
|
73 |
+
|
74 |
print(f"**processing time: {(time.time() - start):.2f} s")
|
75 |
|
76 |
outputs = model(**inputs)
|
77 |
+
logits = outputs.logits.cpu()
|
78 |
+
|
79 |
+
print(f"{logits.shape=}") # shape (batch_size, num_labels, height/4, width/4) -> 3, 19, 256 ,256
|
80 |
# print(f"{logits}")
|
81 |
|
82 |
print(f"***processing time: {(time.time() - start):.2f} s")
|
83 |
# First, rescale logits to original image size
|
84 |
interpolated_logits = nn.functional.interpolate(
|
85 |
+
logits,
|
86 |
size=[1024, 1024], #resized_image.size[::-1], # (height, width)
|
87 |
mode='bilinear',
|
88 |
align_corners=False)
|
89 |
+
print(f"{interpolated_logits.shape=}, {logits.shape=}") # 1, 19, 1024, 1024
|
90 |
|
91 |
# Second, apply argmax on the class dimension
|
92 |
seg = interpolated_logits.argmax(dim=1)[0]
|