Nguyen Thai Thao Uyen commited on
Commit
c1565a6
·
1 Parent(s): abbb1a2

Update run.py

Browse files
Files changed (1) hide show
  1. run.py +36 -28
run.py CHANGED
@@ -4,38 +4,46 @@ import numpy as np
4
  import matplotlib.pyplot as plt
5
  import app
6
  import os
7
- import PIL
 
8
 
9
  def pred(src):
10
- # os.environ['HUGGINGFACE_HUB_HOME'] = './.cache'
11
- # Load the model configuration
12
- device = "cuda" if torch.cuda.is_available() else "cpu"
13
- model.to(device)
14
-
15
- cache_dir = "/code/cache"
16
- model_config = SamConfig.from_pretrained("facebook/sam-vit-base",
17
- cache_dir=cache_dir)
18
- processor = SamProcessor.from_pretrained("facebook/sam-vit-base",
19
- cache_dir=cache_dir)
20
 
21
- # Create an instance of the model architecture with the loaded configuration
 
 
 
 
22
  model = SamModel(config=model_config)
23
- #Update the model by loading the weights from saved file.
24
- model.load_state_dict(torch.load("sam_model.pth",
25
- map_location=torch.device('cpu')))
 
 
 
 
 
 
 
 
 
 
26
 
27
- new_image = np.array(Image.open(src))
28
  inputs = processor(new_image, return_tensors="pt")
29
- inputs = {k: v.to(device) for k, v in inputs.items()}
30
- x = 1
31
- # model.eval()
32
- # # forward pass
33
- # with torch.no_grad():
34
- # outputs = model(**inputs, multimask_output=False)
35
-
36
- # # apply sigmoid
37
- # single_patch_prob = torch.sigmoid(outputs.pred_masks.squeeze(1))
38
- # # convert soft mask to hard mask
39
- # single_patch_prob = single_patch_prob.cpu().numpy().squeeze()
40
- # single_patch_prediction = (single_patch_prob > 0.5).astype(np.uint8)
 
 
 
 
41
  return x
 
4
  import matplotlib.pyplot as plt
5
  import app
6
  import os
7
+ import json
8
+ from PIL import Image
9
 
10
  def pred(src):
 
 
 
 
 
 
 
 
 
 
11
 
12
+ # -- load model configuration
13
+ MODEL_FILE = "sam_model.pth"
14
+ model_config = SamConfig.from_pretrained("facebook/sam-vit-base")
15
+ processor = SamProcessor.from_pretrained("facebook/sam-vit-base")
16
+
17
  model = SamModel(config=model_config)
18
+ model.load_state_dict(torch.load(MODEL_FILE))
19
+
20
+ with open("sam-config.json", "r") as f: # modified config json file
21
+ modified_config_dict = json.load(f)
22
+
23
+ processor = SamProcessor.from_pretrained("facebook/sam-vit-base",
24
+ **modified_config_dict)
25
+
26
+ # -- process image
27
+ image = Image.open(src)
28
+ rgbim = image.convert("RGB")
29
+ new_image = np.array(rgbim)
30
+ print("Shape:",new_image.shape)
31
 
 
32
  inputs = processor(new_image, return_tensors="pt")
33
+ model.eval()
34
+
35
+ # forward pass
36
+ with torch.no_grad():
37
+ outputs = model(pixel_values=inputs["pixel_values"],
38
+ multimask_output=False)
39
+
40
+ # apply sigmoid
41
+ pred_prob = torch.sigmoid(outputs.pred_masks.squeeze(1))
42
+
43
+ # convert soft mask to hard mask
44
+ PROBABILITY_THRES = 0.30
45
+ pred_prob = pred_prob.cpu().numpy().squeeze()
46
+ pred_prediction = (pred_prob > PROBABILITY_THRES).astype(np.uint8)
47
+
48
+ x=1
49
  return x