Spaces:
Running
on
Zero
Running
on
Zero
update gpu
Browse files
app.py
CHANGED
|
@@ -40,8 +40,6 @@ class MobileSAM(nn.Module):
|
|
| 40 |
with open(sam_checkpoint, 'wb') as f:
|
| 41 |
f.write(r.content)
|
| 42 |
|
| 43 |
-
device = 'cuda' if USE_CUDA else 'cpu'
|
| 44 |
-
|
| 45 |
mobile_sam = sam_model_registry[model_type](checkpoint=sam_checkpoint)
|
| 46 |
|
| 47 |
def new_forward_fn(self, x):
|
|
@@ -123,7 +121,6 @@ class MobileSAM(nn.Module):
|
|
| 123 |
setattr(mobile_sam.image_encoder.layers[1].blocks[0].__class__, "forward", new_forward_fn2)
|
| 124 |
|
| 125 |
|
| 126 |
-
mobile_sam.to(device=device)
|
| 127 |
mobile_sam.eval()
|
| 128 |
self.image_encoder = mobile_sam.image_encoder
|
| 129 |
|
|
@@ -150,7 +147,8 @@ def image_mobilesam_feature(
|
|
| 150 |
images,
|
| 151 |
node_type="block",
|
| 152 |
layer=-1,
|
| 153 |
-
):
|
|
|
|
| 154 |
global USE_CUDA
|
| 155 |
if USE_CUDA:
|
| 156 |
images = images.cuda()
|
|
@@ -160,6 +158,7 @@ def image_mobilesam_feature(
|
|
| 160 |
if USE_CUDA:
|
| 161 |
feat_extractor = feat_extractor.cuda()
|
| 162 |
|
|
|
|
| 163 |
# attn_outputs, mlp_outputs, block_outputs = [], [], []
|
| 164 |
outputs = []
|
| 165 |
for i in range(images.shape[0]):
|
|
@@ -228,8 +227,6 @@ class SAM(torch.nn.Module):
|
|
| 228 |
|
| 229 |
self.image_encoder = sam.image_encoder
|
| 230 |
self.image_encoder.eval()
|
| 231 |
-
if USE_CUDA:
|
| 232 |
-
self.image_encoder = self.image_encoder.cuda()
|
| 233 |
|
| 234 |
@torch.no_grad()
|
| 235 |
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
|
@@ -291,8 +288,6 @@ class DiNOv2(torch.nn.Module):
|
|
| 291 |
self.dinov2 = torch.hub.load("facebookresearch/dinov2", ver)
|
| 292 |
self.dinov2.requires_grad_(False)
|
| 293 |
self.dinov2.eval()
|
| 294 |
-
if USE_CUDA:
|
| 295 |
-
self.dinov2 = self.dinov2.cuda()
|
| 296 |
|
| 297 |
def new_block_forward(self, x: torch.Tensor) -> torch.Tensor:
|
| 298 |
def attn_residual_func(x):
|
|
@@ -372,8 +367,6 @@ class CLIP(torch.nn.Module):
|
|
| 372 |
model = CLIPModel.from_pretrained("openai/clip-vit-base-patch16")
|
| 373 |
# processor = CLIPProcessor.from_pretrained("openai/clip-vit-base-patch16")
|
| 374 |
self.model = model.eval()
|
| 375 |
-
if USE_CUDA:
|
| 376 |
-
self.model = self.model.cuda()
|
| 377 |
|
| 378 |
def new_forward(
|
| 379 |
self,
|
|
|
|
| 40 |
with open(sam_checkpoint, 'wb') as f:
|
| 41 |
f.write(r.content)
|
| 42 |
|
|
|
|
|
|
|
| 43 |
mobile_sam = sam_model_registry[model_type](checkpoint=sam_checkpoint)
|
| 44 |
|
| 45 |
def new_forward_fn(self, x):
|
|
|
|
| 121 |
setattr(mobile_sam.image_encoder.layers[1].blocks[0].__class__, "forward", new_forward_fn2)
|
| 122 |
|
| 123 |
|
|
|
|
| 124 |
mobile_sam.eval()
|
| 125 |
self.image_encoder = mobile_sam.image_encoder
|
| 126 |
|
|
|
|
| 147 |
images,
|
| 148 |
node_type="block",
|
| 149 |
layer=-1,
|
| 150 |
+
):
|
| 151 |
+
print("Running MobileSAM")
|
| 152 |
global USE_CUDA
|
| 153 |
if USE_CUDA:
|
| 154 |
images = images.cuda()
|
|
|
|
| 158 |
if USE_CUDA:
|
| 159 |
feat_extractor = feat_extractor.cuda()
|
| 160 |
|
| 161 |
+
print("images shape:", images.shape)
|
| 162 |
# attn_outputs, mlp_outputs, block_outputs = [], [], []
|
| 163 |
outputs = []
|
| 164 |
for i in range(images.shape[0]):
|
|
|
|
| 227 |
|
| 228 |
self.image_encoder = sam.image_encoder
|
| 229 |
self.image_encoder.eval()
|
|
|
|
|
|
|
| 230 |
|
| 231 |
@torch.no_grad()
|
| 232 |
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
|
|
|
| 288 |
self.dinov2 = torch.hub.load("facebookresearch/dinov2", ver)
|
| 289 |
self.dinov2.requires_grad_(False)
|
| 290 |
self.dinov2.eval()
|
|
|
|
|
|
|
| 291 |
|
| 292 |
def new_block_forward(self, x: torch.Tensor) -> torch.Tensor:
|
| 293 |
def attn_residual_func(x):
|
|
|
|
| 367 |
model = CLIPModel.from_pretrained("openai/clip-vit-base-patch16")
|
| 368 |
# processor = CLIPProcessor.from_pretrained("openai/clip-vit-base-patch16")
|
| 369 |
self.model = model.eval()
|
|
|
|
|
|
|
| 370 |
|
| 371 |
def new_forward(
|
| 372 |
self,
|