Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix clip_feature_extraction.ipynb so that it can run without errors. #567

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 42 additions & 41 deletions examples/clip_feature_extraction.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -21,7 +21,7 @@
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": 2,
"metadata": {},
"outputs": [
{
Expand All @@ -37,97 +37,98 @@
],
"source": [
"raw_image = Image.open(\"../docs/_static/merlion.png\").convert(\"RGB\")\n",
"caption = \"a large fountain spewing water into the air\"\n",
"display(raw_image.resize((596, 437)))"
]
},
{
"cell_type": "code",
"execution_count": 13,
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"# setup device to use\n",
"device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Load CLIP feature extractor"
]
},
{
"cell_type": "code",
"execution_count": 14,
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"# model, vis_processors, txt_processors = load_model_and_preprocess(\"clip_feature_extractor\", model_type=\"ViT-B-32\", is_eval=True, device=device)\n",
"model, vis_processors, txt_processors = load_model_and_preprocess(\"clip_feature_extractor\", model_type=\"ViT-B-16\", is_eval=True, device=device)"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [],
"source": [
"model, vis_processors, txt_processors = load_model_and_preprocess(\"clip_feature_extractor\", model_type=\"ViT-B-16\", is_eval=True, device=device)\n",
"image = vis_processors[\"eval\"](raw_image).unsqueeze(0).to(device)\n",
"text = \"merlion, a landmark in Singapore\""
"text_input = txt_processors[\"eval\"](caption)\n",
"sample = {\"image\": image, \"text_input\": [text_input]}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Extract image embedding and class name embeddings"
"#### Unimodal features"
]
},
{
"cell_type": "code",
"execution_count": 24,
"execution_count": 17,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"torch.Size([1, 512])\n",
"torch.Size([1, 512])\n"
]
}
],
"source": [
"sample = {\"image\": image, \"text_input\": text}\n",
"# extract features\n",
"features = model.extract_features(sample)\n",
"\n",
"clip_features = model.extract_features(sample)\n",
"# image embeddings\n",
"print(features.image_embeds.shape)\n",
"# torch.Size([1, 512])\n",
"\n",
"image_features = clip_features.image_embeds_proj\n",
"text_features = clip_features.text_embeds_proj"
"# text embeddings\n",
"print(features.text_embeds.shape)\n",
"# torch.Size([1, 512])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Matching image embeddings with each class name embeddings"
"#### Normalized low-dimensional unimodal features"
]
},
{
"cell_type": "code",
"execution_count": 25,
"execution_count": 18,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"merlion: \t 40.767%\n",
"sky: \t 0.004%\n",
"giraffe: \t 0.001%\n",
"fountain: \t 0.140%\n",
"marina bay: \t 59.088%\n"
"torch.Size([1, 512])\n",
"torch.Size([1, 512])\n",
"tensor([[0.2782]], device='cuda:0', grad_fn=<MmBackward0>)\n"
]
}
],
"source": [
"sims = (image_features @ text_features.t())[0] / 0.01\n",
"probs = torch.nn.Softmax(dim=0)(sims).tolist()\n",
"\n",
"for cls_nm, prob in zip(cls_names, probs):\n",
" print(f\"{cls_nm}: \\t {prob:.3%}\")"
"# low-dimensional projected features\n",
"print(features.image_embeds_proj.shape)\n",
"# torch.Size([1, 512])\n",
"print(features.text_embeds_proj.shape)\n",
"# torch.Size([1, 512])\n",
"similarity = features.image_embeds_proj[:,:] @ features.text_embeds_proj[:,:].t()\n",
"print(similarity)\n",
"# tensor([[0.2782]])"
]
}
],
Expand All @@ -147,7 +148,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.10"
"version": "3.11.5"
},
"orig_nbformat": 4,
"vscode": {
Expand Down