Update run.py
Browse files
run.py
CHANGED
@@ -5,6 +5,14 @@ import torch
|
|
5 |
from transformers import SegformerImageProcessor, SegformerForSemanticSegmentation
|
6 |
# from torchvision import transforms
|
7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
weights2load = 'segformer_ep15_loss0.00.pth'
|
9 |
id2label = {0: 'seal', 255: 'bck'}
|
10 |
label2id = {'seal': 0, 'bck': 255}
|
@@ -19,6 +27,7 @@ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
|
19 |
model.load_state_dict(torch.load(weights2load, weights_only=True, map_location=device))
|
20 |
model.to(device).eval()
|
21 |
|
|
|
22 |
|
23 |
def flip_periodically(im, interval_s=2):
|
24 |
"""
|
@@ -31,14 +40,13 @@ def flip_periodically(im, interval_s=2):
|
|
31 |
Returns:
|
32 |
The flipped image.
|
33 |
"""
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
return imout #np.flipud(im)
|
42 |
|
43 |
with gr.Blocks() as demo:
|
44 |
inp = gr.Image(sources=["webcam"], streaming=True)
|
|
|
5 |
from transformers import SegformerImageProcessor, SegformerForSemanticSegmentation
|
6 |
# from torchvision import transforms
|
7 |
|
8 |
+
class Count:
|
9 |
+
def __init__(self):
|
10 |
+
self.n = 0
|
11 |
+
|
12 |
+
def step(self):
|
13 |
+
self.n += 1
|
14 |
+
|
15 |
+
|
16 |
weights2load = 'segformer_ep15_loss0.00.pth'
|
17 |
id2label = {0: 'seal', 255: 'bck'}
|
18 |
label2id = {'seal': 0, 'bck': 255}
|
|
|
27 |
model.load_state_dict(torch.load(weights2load, weights_only=True, map_location=device))
|
28 |
model.to(device).eval()
|
29 |
|
30 |
+
counter = Count()
|
31 |
|
32 |
def flip_periodically(im, interval_s=2):
|
33 |
"""
|
|
|
40 |
Returns:
|
41 |
The flipped image.
|
42 |
"""
|
43 |
+
counter.step()
|
44 |
+
if (counter.n % 100) == 0:
|
45 |
+
pixel_values = image_processor(im, return_tensors="pt").pixel_values.to(device)
|
46 |
+
outputs = model(pixel_values=pixel_values)
|
47 |
+
logits = outputs.logits.cpu().detach().numpy() ** 2
|
48 |
+
counter.imout = (logits[0, 0] - logits[0, 0].min()) / (logits[0, 0].max() - logits[0, 0].min())
|
49 |
+
return counter.imout #np.flipud(im)
|
|
|
50 |
|
51 |
with gr.Blocks() as demo:
|
52 |
inp = gr.Image(sources=["webcam"], streaming=True)
|