ViktorDo commited on
Commit
7d12746
·
verified ·
1 Parent(s): 18764e1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +6 -3
app.py CHANGED
@@ -7,6 +7,8 @@ from PIL import Image
7
  import os
8
  from functools import partial
9
 
 
 
10
 
11
  def resize_image(image, target_size=1024):
12
  h_img, w_img = image.size
@@ -51,6 +53,7 @@ def segment_image(image, preprocessor, model, crop_size = (1024, 1024), num_clas
51
  print(x1, x2, y1, y2)
52
  with torch.no_grad():
53
  inputs = preprocessor(crop_img, return_tensors = "pt")
 
54
  outputs = model(**inputs)
55
 
56
  resized_logits = F.interpolate(
@@ -58,7 +61,7 @@ def segment_image(image, preprocessor, model, crop_size = (1024, 1024), num_clas
58
  )
59
  preds += F.pad(resized_logits,
60
  (int(x1), int(preds.shape[3] - x2), int(y1),
61
- int(preds.shape[2] - y2)))
62
  count_mat[:, :, y1:y2, x1:x2] += 1
63
 
64
  assert (count_mat == 0).sum() == 0
@@ -79,8 +82,8 @@ if __name__ == "__main__":
79
  print(device)
80
 
81
  # Load model and processor
82
- preprocessor = SegformerImageProcessor.from_pretrained("EPFL-ECEO/segformer-b2-finetuned-coralscapes-1024-1024")
83
- model = SegformerForSemanticSegmentation.from_pretrained("EPFL-ECEO/segformer-b2-finetuned-coralscapes-1024-1024").to(device)
84
  model.eval()
85
 
86
  id2label = {"1": "seagrass", "2": "trash", "3": "other coral dead", "4": "other coral bleached", "5": "sand", "6": "other coral alive", "7": "human", "8": "transect tools", "9": "fish", "10": "algae covered substrate", "11": "other animal", "12": "unknown hard substrate", "13": "background", "14": "dark", "15": "transect line", "16": "massive/meandering bleached", "17": "massive/meandering alive", "18": "rubble", "19": "branching bleached", "20": "branching dead", "21": "millepora", "22": "branching alive", "23": "massive/meandering dead", "24": "clam", "25": "acropora alive", "26": "sea cucumber", "27": "turbinaria", "28": "table acropora alive", "29": "sponge", "30": "anemone", "31": "pocillopora alive", "32": "table acropora dead", "33": "meandering bleached", "34": "stylophora alive", "35": "sea urchin", "36": "meandering alive", "37": "meandering dead", "38": "crown of thorn", "39": "dead clam"}
 
7
  import os
8
  from functools import partial
9
 
10
+ device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu")
11
+ print(device)
12
 
13
  def resize_image(image, target_size=1024):
14
  h_img, w_img = image.size
 
53
  print(x1, x2, y1, y2)
54
  with torch.no_grad():
55
  inputs = preprocessor(crop_img, return_tensors = "pt")
56
+ inputs["pixel_values"] = inputs["pixel_values"].to(device)
57
  outputs = model(**inputs)
58
 
59
  resized_logits = F.interpolate(
 
61
  )
62
  preds += F.pad(resized_logits,
63
  (int(x1), int(preds.shape[3] - x2), int(y1),
64
+ int(preds.shape[2] - y2))).cpu()
65
  count_mat[:, :, y1:y2, x1:x2] += 1
66
 
67
  assert (count_mat == 0).sum() == 0
 
82
  print(device)
83
 
84
  # Load model and processor
85
+ preprocessor = SegformerImageProcessor.from_pretrained("EPFL-ECEO/segformer-b5-finetuned-coralscapes-1024-1024")
86
+ model = SegformerForSemanticSegmentation.from_pretrained("EPFL-ECEO/segformer-b5-finetuned-coralscapes-1024-1024").to(device)
87
  model.eval()
88
 
89
  id2label = {"1": "seagrass", "2": "trash", "3": "other coral dead", "4": "other coral bleached", "5": "sand", "6": "other coral alive", "7": "human", "8": "transect tools", "9": "fish", "10": "algae covered substrate", "11": "other animal", "12": "unknown hard substrate", "13": "background", "14": "dark", "15": "transect line", "16": "massive/meandering bleached", "17": "massive/meandering alive", "18": "rubble", "19": "branching bleached", "20": "branching dead", "21": "millepora", "22": "branching alive", "23": "massive/meandering dead", "24": "clam", "25": "acropora alive", "26": "sea cucumber", "27": "turbinaria", "28": "table acropora alive", "29": "sponge", "30": "anemone", "31": "pocillopora alive", "32": "table acropora dead", "33": "meandering bleached", "34": "stylophora alive", "35": "sea urchin", "36": "meandering alive", "37": "meandering dead", "38": "crown of thorn", "39": "dead clam"}