Skip to content

Commit a54396d

Browse files
author
Jimmy Wu
committed
Minor fixes
1 parent b9e4fa5 commit a54396d

4 files changed

+27
-41
lines changed

CLIP_GradCAM_Visualization.ipynb

+8-11
Original file line numberDiff line numberDiff line change
@@ -44,23 +44,20 @@
4444
"!pip install ftfy regex tqdm matplotlib opencv-python scipy scikit-image\n",
4545
"!pip install git+https://github.com/openai/CLIP.git\n",
4646
"\n",
47+
"import urllib.request\n",
4748
"import numpy as np\n",
4849
"import torch\n",
49-
"import os\n",
50-
"import torch.nn as nn\n",
5150
"import torch.nn.functional as F\n",
52-
"import cv2\n",
53-
"import urllib.request\n",
5451
"import matplotlib.pyplot as plt\n",
5552
"import clip\n",
5653
"from PIL import Image\n",
57-
"from skimage import transform as skimage_transform\n",
58-
"from scipy.ndimage import filters"
54+
"from scipy.ndimage import filters\n",
55+
"from torch import nn"
5956
]
6057
},
6158
{
6259
"cell_type": "code",
63-
"execution_count": 34,
60+
"execution_count": 2,
6461
"metadata": {
6562
"cellView": "form",
6663
"id": "caPbAhFlRBwT"
@@ -91,23 +88,23 @@
9188
" return attn_map\n",
9289
"\n",
9390
"def viz_attn(img, attn_map, blur=True):\n",
94-
" fig, axes = plt.subplots(1, 2, figsize=(10, 5))\n",
91+
" _, axes = plt.subplots(1, 2, figsize=(10, 5))\n",
9592
" axes[0].imshow(img)\n",
9693
" axes[1].imshow(getAttMap(img, attn_map, blur))\n",
9794
" for ax in axes:\n",
9895
" ax.axis(\"off\")\n",
9996
" plt.show()\n",
10097
" \n",
10198
"def load_image(img_path, resize=None):\n",
102-
" image = Image.open(image_path).convert(\"RGB\")\n",
99+
" image = Image.open(img_path).convert(\"RGB\")\n",
103100
" if resize is not None:\n",
104101
" image = image.resize((resize, resize))\n",
105102
" return np.asarray(image).astype(np.float32) / 255."
106103
]
107104
},
108105
{
109106
"cell_type": "code",
110-
"execution_count": null,
107+
"execution_count": 3,
111108
"metadata": {
112109
"cellView": "form",
113110
"id": "XziodsCqVC2A"
@@ -209,7 +206,7 @@
209206
},
210207
{
211208
"cell_type": "code",
212-
"execution_count": 45,
209+
"execution_count": 4,
213210
"metadata": {
214211
"cellView": "form",
215212
"colab": {

CLIP_Patch_Detection.ipynb

+5-7
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,12 @@
4242
"!pip install ftfy regex tqdm matplotlib\n",
4343
"!pip install git+https://github.com/openai/CLIP.git\n",
4444
"\n",
45-
"import numpy as np\n",
46-
"import torch\n",
47-
"import torch.nn as nn\n",
48-
"import torch.nn.functional as F\n",
4945
"import math\n",
5046
"import urllib.request\n",
51-
"import matplotlib.pyplot as plt\n",
5247
"import clip\n",
48+
"import matplotlib.pyplot as plt\n",
49+
"import numpy as np\n",
50+
"import torch\n",
5351
"from PIL import Image\n",
5452
"from torchvision import transforms"
5553
]
@@ -68,7 +66,7 @@
6866
"#@markdown Some helper functions for loading, patchifying and visualizing images.\n",
6967
"\n",
7068
"def load_image(img_path, resize=None, pil=False):\n",
71-
" image = Image.open(image_path).convert(\"RGB\")\n",
69+
" image = Image.open(img_path).convert(\"RGB\")\n",
7270
" if resize is not None:\n",
7371
" image = image.resize((resize, resize))\n",
7472
" if pil:\n",
@@ -79,7 +77,7 @@
7977
" # x: num_patches, 3, patch_size, patch_size\n",
8078
" n = x.shape[0]\n",
8179
" nrows = int(math.sqrt(n))\n",
82-
" fig, axes = plt.subplots(nrows, nrows, figsize=figsize)\n",
80+
" _, axes = plt.subplots(nrows, nrows, figsize=figsize)\n",
8381
" for i, ax in enumerate(axes.flatten()): \n",
8482
" im = x[i].permute(1, 2, 0).numpy()\n",
8583
" im = (im * 255.).round().astype(np.uint8)\n",

CLIP_Zero_shot_Detector.ipynb

+8-12
Original file line numberDiff line numberDiff line change
@@ -42,19 +42,15 @@
4242
"!pip install ftfy regex tqdm matplotlib selectivesearch\n",
4343
"!pip install git+https://github.com/openai/CLIP.git\n",
4444
"\n",
45-
"import numpy as np\n",
46-
"import torch\n",
47-
"import torch.nn as nn\n",
48-
"import torch.nn.functional as F\n",
49-
"import math\n",
5045
"import urllib.request\n",
46+
"from collections import OrderedDict\n",
47+
"import clip\n",
5148
"import matplotlib.pyplot as plt\n",
5249
"import matplotlib.patches as mpatches\n",
53-
"import clip\n",
54-
"from PIL import Image\n",
55-
"from torchvision import transforms\n",
50+
"import numpy as np\n",
5651
"import selectivesearch\n",
57-
"from collections import OrderedDict"
52+
"import torch\n",
53+
"from PIL import Image"
5854
]
5955
},
6056
{
@@ -71,7 +67,7 @@
7167
"#@markdown Some helper functions for loading, patchifying and visualizing images.\n",
7268
"\n",
7369
"def load_image(img_path, resize=None, pil=False):\n",
74-
" image = Image.open(image_path).convert(\"RGB\")\n",
70+
" image = Image.open(img_path).convert(\"RGB\")\n",
7571
" if resize is not None:\n",
7672
" image = image.resize((resize, resize))\n",
7773
" if pil:\n",
@@ -118,7 +114,7 @@
118114
"\n",
119115
" top_idx = dets[:, -1].argmax()\n",
120116
"\n",
121-
" fig, ax = plt.subplots(figsize=(12, 12))\n",
117+
" _, ax = plt.subplots(figsize=(12, 12))\n",
122118
" ax.imshow(im, aspect='equal')\n",
123119
" for i in inds:\n",
124120
" bbox = dets[i, :4]\n",
@@ -231,7 +227,7 @@
231227
"candidates = candidates[:topk]\n",
232228
"\n",
233229
"# Display topk bounding boxes.\n",
234-
"fig, ax = plt.subplots(ncols=1, nrows=1, figsize=(8, 8))\n",
230+
"_, ax = plt.subplots(ncols=1, nrows=1, figsize=(8, 8))\n",
235231
"ax.imshow(img)\n",
236232
"for x, y, w, h in candidates:\n",
237233
" rect = mpatches.Rectangle(\n",

CLIP_reCAPTCHA.ipynb

+6-11
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,15 @@
4242
"!pip install ftfy regex tqdm matplotlib bs4\n",
4343
"!pip install git+https://github.com/openai/CLIP.git\n",
4444
"\n",
45+
"import urllib.request\n",
46+
"import clip\n",
47+
"import matplotlib.pyplot as plt\n",
4548
"import numpy as np\n",
4649
"import torch\n",
47-
"import torch.nn as nn\n",
4850
"import torch.nn.functional as F\n",
49-
"import math\n",
50-
"import urllib.request\n",
51-
"import matplotlib.pyplot as plt\n",
52-
"import clip\n",
51+
"from bs4 import BeautifulSoup\n",
5352
"from PIL import Image\n",
5453
"from torchvision import transforms\n",
55-
"from bs4 import BeautifulSoup\n",
5654
"from torchvision.utils import make_grid"
5755
]
5856
},
@@ -70,7 +68,7 @@
7068
"#@markdown Some helper functions for loading, patchifying and visualizing images.\n",
7169
"\n",
7270
"def load_image(img_path, resize=None, pil=False):\n",
73-
" image = Image.open(image_path).convert(\"RGB\")\n",
71+
" image = Image.open(img_path).convert(\"RGB\")\n",
7472
" if resize is not None:\n",
7573
" image = image.resize((resize, resize))\n",
7674
" if pil:\n",
@@ -81,7 +79,6 @@
8179
"def viz_patches(x, figsize=None, topk=None, t=5, title=None):\n",
8280
" color = (0, 255, 0)\n",
8381
" n = x.shape[0]\n",
84-
" nrows = int(math.sqrt(n))\n",
8582
" images = []\n",
8683
" for i in range(n):\n",
8784
" im = x[i].permute(1, 2, 0).numpy()\n",
@@ -160,7 +157,6 @@
160157
"url_contents = urllib.request.urlopen(URL).read()\n",
161158
"soup = BeautifulSoup(url_contents, \"html\")\n",
162159
"instruction = soup.find(\"div\", {\"class\": \"rc-imageselect-desc-no-canonical\"}).get_text()\n",
163-
"caption = instruction.split(' ')[-1]\n",
164160
"image = soup.find(\"img\", {\"class\": \"fbc-imageselect-payload\"})\n",
165161
"image_url = f\"https://www.google.com/{image['src']}\"\n",
166162
"image_path = 'image.png'\n",
@@ -225,8 +221,7 @@
225221
" patch_embs = patch_embs / patch_embs.norm(dim=-1, keepdim=True)\n",
226222
" text_embs = text_embs / text_embs.norm(dim=-1, keepdim=True)\n",
227223
" sim = patch_embs @ text_embs.t()\n",
228-
" idx_max = sim.argmax().item()\n",
229-
" topk_values, topk_idxs = torch.topk(sim.flatten(), topk)\n",
224+
" _, topk_idxs = torch.topk(sim.flatten(), topk)\n",
230225
" topk_idxs = topk_idxs.cpu().numpy().tolist()\n",
231226
"\n",
232227
"viz_patches(\n",

0 commit comments

Comments
 (0)