Spaces:
Sleeping
Sleeping
check model is on device
Browse files
app.py
CHANGED
|
@@ -10,6 +10,14 @@ import time
|
|
| 10 |
feature_extractor = SegformerFeatureExtractor.from_pretrained("nvidia/segformer-b5-finetuned-cityscapes-1024-1024")
|
| 11 |
model = SegformerForSemanticSegmentation.from_pretrained("nvidia/segformer-b5-finetuned-cityscapes-1024-1024")
|
| 12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
# https://github.com/NielsRogge/Transformers-Tutorials/blob/master/SegFormer/Segformer_inference_notebook.ipynb
|
| 14 |
|
| 15 |
def cityscapes_palette():
|
|
@@ -53,23 +61,23 @@ def annotation(image:ImageDraw, color_seg:np.array):
|
|
| 53 |
def call(image): #nparray
|
| 54 |
start = time.time()
|
| 55 |
|
| 56 |
-
print(f"Is CUDA available: {torch.cuda.is_available()}")
|
| 57 |
-
if (torch.cuda.is_available()):
|
| 58 |
-
print(f"CUDA device: {torch.cuda.get_device_name(torch.cuda.current_device())}")
|
| 59 |
-
|
| 60 |
resized = Image.fromarray(image).resize((1024,1024))
|
| 61 |
resized_image = np.array(resized)
|
| 62 |
print(f"{np.array(resized_image).shape=}") # 1024, 1024, 3
|
| 63 |
|
|
|
|
| 64 |
# resized_image = Image.fromarray(resized_image_np)
|
| 65 |
# print(f"{resized_image=}")
|
| 66 |
|
| 67 |
inputs = feature_extractor(images=resized_image, return_tensors="pt")
|
| 68 |
|
|
|
|
|
|
|
| 69 |
outputs = model(**inputs)
|
| 70 |
print(f"{outputs.logits.shape=}") # shape (batch_size, num_labels, height/4, width/4) -> 3, 19, 256 ,256
|
| 71 |
# print(f"{logits}")
|
| 72 |
|
|
|
|
| 73 |
# First, rescale logits to original image size
|
| 74 |
interpolated_logits = nn.functional.interpolate(
|
| 75 |
outputs.logits,
|
|
@@ -91,6 +99,8 @@ def call(image): #nparray
|
|
| 91 |
color_seg = color_seg[..., ::-1]
|
| 92 |
print(f"{color_seg.shape=}")
|
| 93 |
|
|
|
|
|
|
|
| 94 |
# Show image + mask
|
| 95 |
img = np.array(resized_image) * 0.5 + color_seg * 0.5
|
| 96 |
img = img.astype(np.uint8)
|
|
@@ -98,8 +108,7 @@ def call(image): #nparray
|
|
| 98 |
out_im_file = Image.fromarray(img)
|
| 99 |
annotation(out_im_file, color_seg)
|
| 100 |
|
| 101 |
-
|
| 102 |
-
print(f"processing time: {(end - start):.2f} s")
|
| 103 |
|
| 104 |
return out_im_file
|
| 105 |
|
|
|
|
| 10 |
feature_extractor = SegformerFeatureExtractor.from_pretrained("nvidia/segformer-b5-finetuned-cityscapes-1024-1024")
|
| 11 |
model = SegformerForSemanticSegmentation.from_pretrained("nvidia/segformer-b5-finetuned-cityscapes-1024-1024")
|
| 12 |
|
| 13 |
+
device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
| 14 |
+
|
| 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
|
| 22 |
|
| 23 |
def cityscapes_palette():
|
|
|
|
| 61 |
def call(image): #nparray
|
| 62 |
start = time.time()
|
| 63 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
resized = Image.fromarray(image).resize((1024,1024))
|
| 65 |
resized_image = np.array(resized)
|
| 66 |
print(f"{np.array(resized_image).shape=}") # 1024, 1024, 3
|
| 67 |
|
| 68 |
+
print(f"*processing time: {(time.time() - start):.2f} s")
|
| 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 |
print(f"{outputs.logits.shape=}") # shape (batch_size, num_labels, height/4, width/4) -> 3, 19, 256 ,256
|
| 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 |
outputs.logits,
|
|
|
|
| 99 |
color_seg = color_seg[..., ::-1]
|
| 100 |
print(f"{color_seg.shape=}")
|
| 101 |
|
| 102 |
+
print(f"****processing time: {(time.time() - start):.2f} s")
|
| 103 |
+
|
| 104 |
# Show image + mask
|
| 105 |
img = np.array(resized_image) * 0.5 + color_seg * 0.5
|
| 106 |
img = img.astype(np.uint8)
|
|
|
|
| 108 |
out_im_file = Image.fromarray(img)
|
| 109 |
annotation(out_im_file, color_seg)
|
| 110 |
|
| 111 |
+
print(f"--> processing time: {(time.time() - start):.2f} s")
|
|
|
|
| 112 |
|
| 113 |
return out_im_file
|
| 114 |
|