bthndmn12 commited on
Commit
0a65b5f
·
1 Parent(s): e57025f

Added weights and code fixed

Browse files
app.py CHANGED
@@ -1,7 +1,63 @@
1
  import gradio as gr
 
 
 
 
2
 
3
- def greet(name):
4
- return "Hello " + name + "!"
5
 
6
- iface = gr.Interface(fn= greet, inputs="text", outputs="text", title="Greeter")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
  iface.launch()
 
1
  import gradio as gr
2
+ import torch
3
+ import numpy as np
4
+ from transformers import AutoModel
5
+ from transformers import SamModel, SamConfig, SamProcessor
6
 
 
 
7
 
8
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
9
+ model_config = SamConfig.from_pretrained("./checkpoint",local_files_only=True)
10
+ processor = SamProcessor.from_pretrained("./checkpoint",local_files_only=True)
11
+ model = SamModel.from_pretrained("./checkpoint",local_files_only=True)
12
+
13
+
14
+
15
+ def get_bbox(gt_map):
16
+
17
+ if np.sum(gt_map) == 0:
18
+ return [0, 0, gt_map.shape[1], gt_map.shape[0]]
19
+
20
+ y_indices, x_indices = np.where(gt_map > 0)
21
+ x_min, x_max = np.min(x_indices), np.max(x_indices)
22
+ y_min, y_max = np.min(y_indices), np.max(y_indices)
23
+
24
+ H, W = gt_map.shape
25
+ x_min = max(0, x_min - np.random.randint(0, 20))
26
+ x_max = min(W, x_max + np.random.randint(0, 20))
27
+ y_min = max(0, y_min - np.random.randint(0, 20))
28
+ y_max = min(H, y_max + np.random.randint(0, 20))
29
+
30
+ bbox = [x_min,y_min,x_max,y_max]
31
+
32
+ return bbox
33
+
34
+
35
+
36
+ def greet(image):
37
+
38
+ image = Image.open(image)
39
+ image = image.resize((256, 256))
40
+
41
+ gt_mask = np.array(image)
42
+ prompt = get_bbox(gt_mask)
43
+
44
+ inputs = processor(images=image, input_boxes=[[prompt]], return_tensors="pt")
45
+ inputs = {k: v.to(device) for k, v in inputs.items()}
46
+
47
+ model.eval()
48
+ with torch.no_grad():
49
+ outputs = model(**inputs,multimask_outputs=False)
50
+
51
+ # outputs = outputs.logits[0].cpu().numpy()
52
+ # outputs = np.argmax(outputs, axis=0)
53
+ # outputs = Image.fromarray(outputs)
54
+ # return outputs
55
+ seg_prob = torch.sigmoid(outputs.pred_masks.squeeze(0))
56
+ seg_prob = seg_prob.cpu().numpy().squeeze()
57
+ seg_prob = (seg_prob > 0.5).astype(np.uint8)
58
+
59
+ return seg_prob
60
+
61
+
62
+ iface = gr.Interface(fn= greet, inputs="image", outputs="image", title="Greeter")
63
  iface.launch()
checkpoint/config.json ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "_name_or_path": "./MainDir/Untitled Folder/checkpoint_sam_torch",
3
+ "architectures": [
4
+ "SamModel"
5
+ ],
6
+ "initializer_range": 0.02,
7
+ "mask_decoder_config": {
8
+ "model_type": ""
9
+ },
10
+ "model_type": "sam",
11
+ "prompt_encoder_config": {
12
+ "model_type": ""
13
+ },
14
+ "torch_dtype": "float32",
15
+ "transformers_version": "4.37.0",
16
+ "vision_config": {
17
+ "dropout": 0.0,
18
+ "initializer_factor": 1.0,
19
+ "intermediate_size": 6144,
20
+ "model_type": "",
21
+ "projection_dim": 512
22
+ }
23
+ }
checkpoint/model.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:5a481b414a5dbe9cba508256e6100dba23eb4e9225f132add55176d51e102215
3
+ size 374979376
checkpoint/preprocessor_config.json ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "do_convert_rgb": true,
3
+ "do_normalize": true,
4
+ "do_pad": true,
5
+ "do_rescale": true,
6
+ "do_resize": true,
7
+ "image_mean": [
8
+ 0.485,
9
+ 0.456,
10
+ 0.406
11
+ ],
12
+ "image_processor_type": "SamImageProcessor",
13
+ "image_std": [
14
+ 0.229,
15
+ 0.224,
16
+ 0.225
17
+ ],
18
+ "pad_size": {
19
+ "height": 1024,
20
+ "width": 1024
21
+ },
22
+ "processor_class": "SamProcessor",
23
+ "resample": 2,
24
+ "rescale_factor": 0.00392156862745098,
25
+ "size": {
26
+ "longest_edge": 1024
27
+ }
28
+ }
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ gradio
2
+ torch
3
+ numpy
4
+ transformers
5
+ Pillow