Skip to content

Commit

Permalink
modularized the code
Browse files Browse the repository at this point in the history
  • Loading branch information
sMamooler committed Jan 2, 2022
1 parent 0a833c1 commit ad105cf
Show file tree
Hide file tree
Showing 282 changed files with 5,184 additions and 3,259 deletions.
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
1 change: 0 additions & 1 deletion Ringier_Project/CLIP-VIL/README.md

This file was deleted.

812 changes: 0 additions & 812 deletions Ringier_Project/CLIP-VIL/emotion_clip.ipynb

This file was deleted.

699 changes: 0 additions & 699 deletions Ringier_Project/CLIP-VIL/gradcam_clip.ipynb

This file was deleted.

Binary file removed Ringier_Project/CLIP-VIL/image_text_grad_vis.pdf
Binary file not shown.
Binary file removed Ringier_Project/CLIP-VIL/mixed_saliency.pdf
Binary file not shown.
853 changes: 0 additions & 853 deletions Ringier_Project/CLIP-VIL/per-word_gradcam_clip.ipynb

This file was deleted.

Binary file removed Ringier_Project/CLIP-VIL/pos&neg_saliency.pdf
Binary file not shown.
Diff not rendered.
Diff not rendered.
Diff not rendered.
6 changes: 6 additions & 0 deletions code/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Ringier_Project


# TODO:
[-] complete requirement.txt

File renamed without changes.
File renamed without changes.
17 changes: 17 additions & 0 deletions Ringier_Project/CLIP-VIL/clip_.py → code/clip_.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
"""
taken from https://github.com/hila-chefer/Transformer-MM-Explainability
added similarity_score
"""

import hashlib
import os
import urllib
Expand Down Expand Up @@ -191,3 +196,15 @@ def tokenize(texts: Union[str, List[str]], context_length: int = 77) -> torch.Lo
result[i, :len(tokens)] = torch.tensor(tokens)

return result


def similarity_score(clip_model, image, target_features):

image_features = clip_model.encode_image(image)

image_features_norm = image_features.norm(dim=-1, keepdim=True)
image_features_new = image_features / image_features_norm
target_features_norm = target_features.norm(dim=-1, keepdim=True)
target_features_new = target_features / target_features_norm

return image_features_new[0].dot(target_features_new[0])*100
24 changes: 24 additions & 0 deletions code/image_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import numpy as np
import cv2

def show_cam_on_image(img, mask, neg_saliency=False):
if neg_saliency:
heatmap = cv2.applyColorMap(np.uint8(255 * mask), cv2.COLORMAP_RAINBOW)
else:
heatmap = cv2.applyColorMap(np.uint8(255 * mask), cv2.COLORMAP_JET)

heatmap = np.float32(heatmap) / 255
cam = heatmap + np.float32(img)
cam = cam / np.max(cam)
return cam

def show_overlapped_cam(img, neg_mask, pos_mask):
neg_heatmap = cv2.applyColorMap(np.uint8(255 * neg_mask), cv2.COLORMAP_RAINBOW)
pos_heatmap = cv2.applyColorMap(np.uint8(255 * pos_mask), cv2.COLORMAP_JET)
neg_heatmap = np.float32(neg_heatmap) / 255
pos_heatmap = np.float32(pos_heatmap) / 255
# try different options: sum, average, ...
heatmap = neg_heatmap + pos_heatmap
cam = heatmap + np.float32(img)
cam = cam / np.max(cam)
return cam
4 changes: 4 additions & 0 deletions Ringier_Project/CLIP-VIL/model.py → code/model.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
"""
taken from https://github.com/hila-chefer/Transformer-MM-Explainability
"""

from collections import OrderedDict
from typing import Tuple, Union

Expand Down
1 change: 1 addition & 0 deletions code/notebooks/ARTEMIS_EMOTIONS.json

Large diffs are not rendered by default.

389 changes: 389 additions & 0 deletions code/notebooks/affectnet_emotions.ipynb

Large diffs are not rendered by default.

579 changes: 579 additions & 0 deletions code/notebooks/artemis_emotions.ipynb

Large diffs are not rendered by default.

File renamed without changes.
423 changes: 423 additions & 0 deletions code/notebooks/global_vis.ipynb

Large diffs are not rendered by default.

477 changes: 477 additions & 0 deletions code/notebooks/perword_vis.ipynb

Large diffs are not rendered by default.

Loading

0 comments on commit ad105cf

Please sign in to comment.