forked from THUDM/CogVLM
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
84 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import torch | ||
import torchvision.transforms as T | ||
import torchvision.models as models | ||
from PIL import Image | ||
import h5py | ||
import os | ||
import datetime | ||
|
||
if torch.cuda.is_available(): | ||
device = "cuda" | ||
else: | ||
device = "cpu" | ||
|
||
model = models.resnet50(pretrained=True) # Ensure pretrained=True to load the ImageNet trained weights | ||
model.eval() | ||
model.to(device) | ||
|
||
transforms = T.Compose([T.Resize(256), | ||
T.CenterCrop(224), | ||
T.ToTensor()]) | ||
|
||
image_path = '/home/venky/CogVLM/test_set/d5.jpg' | ||
image = Image.open(image_path).convert('RGB') | ||
preprocessed_image = transforms(image).reshape(-1, 3, 224, 224) | ||
preprocessed_image = preprocessed_image.to(device) | ||
|
||
with torch.no_grad(): | ||
embedding = model(preprocessed_image * 255.0) | ||
|
||
print(embedding.shape) | ||
|
||
# Saving the embedding, similar to the original script | ||
img_name = os.path.splitext(os.path.basename(image_path))[0] | ||
current_time = datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S") | ||
run_name = f"imagenet_{img_name}_{current_time}" | ||
run_dir_path = os.path.join('/home/venky/CogVLM/baselines/logs', run_name) | ||
os.makedirs(run_dir_path, exist_ok=True) | ||
with h5py.File(os.path.join(run_dir_path, 'embeddings.h5'), 'w') as f: | ||
f.create_dataset('embeddings', data=embedding.cpu().numpy()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,35 @@ | ||
import torch | ||
import mvp | ||
|
||
model = mvp.load("vitb-mae-egosoup") | ||
import os | ||
import h5py | ||
import numpy as np | ||
from PIL import Image | ||
import datetime | ||
import torchvision.transforms as T | ||
import torchvision.models as models | ||
|
||
model = mvp. | ||
model.freeze() | ||
model.to("cuda") | ||
|
||
transforms = T.Compose([T.Resize(256), | ||
T.CenterCrop(224), | ||
T.ToTensor()]) | ||
|
||
image_path = '/home/venky/CogVLM/test_set/d1.jpg' | ||
image = Image.open(image_path).convert('RGB') | ||
preprocessed_image = transforms(image).reshape(-1, 3, 224, 224) | ||
preprocessed_image = preprocessed_image.to("cuda") | ||
|
||
with torch.no_grad(): | ||
embedding = model(preprocessed_image) | ||
print(embedding.shape) | ||
|
||
import ipdb; ipdb.set_trace() | ||
img_name = os.path.splitext(os.path.basename(image_path))[0] | ||
current_time = datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S") | ||
run_name = f"mvp_{img_name}_{current_time}" | ||
run_dir_path = os.path.join('/home/venky/CogVLM/baselines/logs', run_name) | ||
os.makedirs(run_dir_path, exist_ok=True) | ||
with h5py.File(os.path.join(run_dir_path, 'embeddings.h5'), 'w') as f: | ||
f.create_dataset('embeddings', data=embedding.cpu().numpy()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import mvp | ||
|
||
model = mvp.load("vitb-mae-egosoup") | ||
model.freeze() | ||
import ipdb; ipdb.set_trace() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters