|
emotions = [ |
|
'tart', 'acidic', 'bitter', 'tangy', 'vinegary', 'sharp', |
|
'thankful', 'appreciative', 'obliged', 'indebted', 'gratified', 'recognizant', |
|
'dignified', 'haughty', 'arrogant', 'self-satisfied', 'vain', 'honored', |
|
'repulsed', 'appalled', 'revolted', 'nauseated', 'repelled', 'sickened', 'ebullient', 'merry', 'jovial', 'cheerful', 'lighthearted', 'joyful', 'beaming', 'grinning', 'elated', 'gleeful', 'happy', 'hopeful', 'gratitude', 'thankful', 'buoyant', 'upbeat', 'vibrant', 'radiant', 'exuberant', 'zestful', 'chirpy', 'peppy', 'jaunty', 'sprightly', 'brisk', 'lively', 'animated', 'energized', 'revitalized', 'invigorated', 'activated', 'energetic', 'dynamic', 'electrified', 'bouncy', 'effervescent', 'chipper', 'jubilant', |
|
'mindful', 'unruffled', 'coolheaded', 'level headed', 'poised', 'self-possessed', 'unflappable', 'collected', 'unperturbed', 'untroubled', 'unrattled', 'unshaken', 'unflustered', 'composed', 'relaxed', 'tranquil', 'serene', 'calm', 'centered', 'peaceful', 'imperturbable', 'reposeful', 'grounded', 'equanimous', 'harmonious', |
|
'engaging', 'focused', 'watchful', 'attentive', 'heedful', 'scrutinizing', 'investigating', 'alert', 'studious', 'analyzing', 'examining', 'cognizant', 'inquiring', 'questioning', 'probing', 'introspecting', 'introspective', 'observant', |
|
'wondering', 'awe', 'intrigued', 'spellbinding', 'fascinated', 'mesmerized', 'captivated', 'bewitching', 'beguiling', 'agog', 'marveling', 'gazing', 'mystified', 'curious', 'riveted', 'enrapturing', 'entrancing', 'hypnotic', 'mesmerizing', 'alluring', 'enthralled', |
|
'pensive', 'ruminative', 'brooding', 'contemplating', 'meditative', 'reflective', 'pondering', 'cogitating', 'speculative', |
|
'trembling', 'shuddery', 'afraid', 'spooked', 'apprehensive', 'fearful', 'terrorized', 'petrified', 'scared', 'horror-struck', 'quavering', 'shuddering', 'frightened', 'trepid', 'distraught', 'alarmed', 'fear-stricken', 'quaking', 'anxious', 'nervous', 'uneasy', 'worried', 'tense', 'jittery', 'jumpy', 'startled', 'edgy', 'antsy', 'rattled', 'distracted', 'disquieted', 'skittish', 'restless', 'restive', 'panic-stricken', 'panicked', |
|
'dumbstruck', 'bewildered', 'dumbfounded', 'stunned', 'stupefied', 'thunderstruck', 'staggered', 'amazed', 'astonished', 'astounded', 'surprised', 'shocked', 'flabbergasted', 'befuddled', 'perplexed', 'puzzled', 'confounded', 'baffled', 'discombobulated', 'flummoxed', |
|
'sad', 'dismal', 'forlorn', 'depressed', 'woebegone', 'plaintive', 'sorrowful', 'gloomy', 'lugubrious', 'melancholic', 'blue', 'desolate', 'miserable', 'downhearted', 'morose', 'somber', 'despairing', 'woeful', 'heartbroken', 'crestfallen', 'dispirited', |
|
'romantic', 'amorous', 'passionate', 'sensual', 'erotic', 'sultry', 'salacious', 'libidinous', 'sensuous', 'carnal', 'lustful', 'infatuated', 'desirous', 'lecherous', 'lust-driven', 'prurient', 'enflamed', 'voluptuous', 'sizzling', 'torrid', 'steaminess', |
|
'seductive', 'titillating', 'awakened', 'ravishing', 'enticing', 'charming', 'irresistible', 'provoked', 'craving', 'stimulated', 'aroused', 'magnetic', 'compelling', 'flirty', 'bellicose', |
|
'aggravated', 'perturbed', 'enraged', 'furious', 'irate', 'incensed', 'infuriated', 'wrathful', 'livid', 'cross', 'galled', 'resentful', 'bitter', 'indignant', 'outraged', 'exasperated', 'maddened', 'angry', 'annoyed', 'vexed', 'truculent', 'spiky', 'prickly', 'snarly', 'huffy', 'nettled', 'irritable', 'piqued', 'snappish', 'irascible', 'testy', 'nerved', |
|
'persistent', 'resilient', 'determined', 'unfailing', 'unyielding', 'tenacious', 'steadfast', 'adamant', 'resolute', 'undaunted', 'unwavering', 'unswerving', 'unflinching', 'unrelenting', 'enduring', 'indefatigable', 'motivated', 'driven', |
|
'discomposed', 'nonplussed', 'disconcerted', 'disturbed', 'ruffled', 'troubled', 'stressed', 'fractious', 'cringing', 'quailing', 'cowering', 'daunted', 'dread-filled', 'intimidated', 'unnerved', 'unsettled', 'fretful', 'ticked-off', 'flustered', |
|
'belligerent', 'pugnacious', 'contentious', 'quarrelsome', 'grumpy', 'grouchy', 'sulky', 'cranky', 'crabby', 'cantankerous', 'curmudgeonly', 'waspy', 'combative', 'argumentative', 'scrappy' |
|
] |
|
|
|
|
|
|
|
|
|
import torch |
|
import open_clip |
|
import requests |
|
from PIL import Image |
|
from torchvision.transforms.functional import to_pil_image |
|
import matplotlib.pyplot as plt |
|
import seaborn as sns |
|
import numpy as np |
|
import requests |
|
from PIL import Image |
|
from IPython.display import display |
|
from io import BytesIO |
|
|
|
|
|
|
|
model_clip, preprocess_train, preprocess_val = open_clip.create_model_and_transforms('hf-hub:apple/DFN5B-CLIP-ViT-H-14-378') |
|
tokenizer = open_clip.get_tokenizer('hf-hub:apple/DFN5B-CLIP-ViT-H-14-378') |
|
|
|
|
|
def download_image(image_url): |
|
response = requests.get(image_url, timeout=1) |
|
response.raise_for_status() |
|
return Image.open(requests.get(image_url, stream=True).raw) |
|
|
|
|
|
|
|
|
|
def tensor_to_array(tensor): |
|
array = tensor.detach().cpu().numpy() |
|
del tensor |
|
return array |
|
|
|
|
|
def softmax(x): |
|
e_x = np.exp(x - np.max(x)) |
|
return e_x / e_x.sum(axis=-1, keepdims=True) |
|
|
|
|
|
|
|
|
|
''' |
|
|
|
# Tokenize the prompts |
|
text = tokenizer(emotions) |
|
|
|
with torch.no_grad(): |
|
|
|
text_features = model_clip.encode_text(text) |
|
|
|
text_features /= text_features.norm(dim=-1, keepdim=True) |
|
text_features = tensor_to_array(text_features) |
|
|
|
# Save the NumPy array to a file |
|
np.save('text_features.npy', text_features) |
|
''' |
|
|
|
|
|
text_features = np.load('text_features.npy') |
|
|
|
|
|
|
|
def zeroshot_classifier(image_url): |
|
|
|
|
|
image = download_image(image_url) |
|
image_preprocessed = preprocess_val(image).unsqueeze(0) |
|
image_features = model_clip.encode_image(image_preprocessed) |
|
image_features = tensor_to_array(image_features) |
|
image_features /= np.linalg.norm(image_features, axis=-1, keepdims=True) |
|
|
|
|
|
|
|
loaded_mean_sims = np.load('mean_sims.npy') |
|
|
|
loaded_stdev_sims = np.load('std_dev_sims.npy') |
|
sims =np.matmul(image_features, text_features.T) |
|
normalized_sims = (sims - loaded_mean_sims) / loaded_stdev_sims |
|
|
|
|
|
|
|
|
|
text_probs = softmax(100.0 * sims) |
|
return text_probs, sims |
|
|
|
|
|
|
|
def display_image_from_url(url, base_width=300): |
|
try: |
|
|
|
response = requests.get(url) |
|
|
|
response.raise_for_status() |
|
|
|
|
|
img = Image.open(BytesIO(response.content)) |
|
|
|
|
|
w_percent = (base_width / float(img.size[0])) |
|
h_size = int((float(img.size[1]) * float(w_percent))) |
|
|
|
|
|
img = img.resize((base_width, h_size), Image.ANTIALIAS) |
|
|
|
|
|
|
|
except: |
|
pass |
|
|
|
|
|
import pandas as pd |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
urls=["https://i.imgur.com/lQCGbw9.png","https://i.imgur.com/saUX1yc.png", 'https://media.gettyimages.com/id/1027697458/de/foto/nostalgische-frau.jpg?s=1024x1024&w=gi&k=20&c=eJlr2c7K1_nFAfv0Sdt6sn4yhz6K_Y78rKbJMvoXlFs=', "https://i.imgur.com/BI3zkNG.jpg", "https://i.imgur.com/3WbnImZ.jpg","https://i.imgur.com/78IlUDZ.png","https://i.imgur.com/29FZiD9.jpg","https://i.imgur.com/2fun8N3.png","https://i.imgur.com/lGLpebl.jpg","https://imagizer.imageshack.com/img924/7428/HH6wua.png","https://i.imgur.com/F22ZjZw.jpg","https://i.imgur.com/HPJQCEp.jpg","https://i.imgur.com/XtSd4pO.png"] |
|
simlist=[] |
|
for url in urls: |
|
|
|
print("##############") |
|
print(url) |
|
try: |
|
probs, sims = zeroshot_classifier(url) |
|
except: |
|
continue |
|
|
|
simlist.append(sims) |
|
for i in range (probs.shape[1]): |
|
if probs[0][i]>0.05: |
|
print(probs[0][i],emotions[i]) |
|
|
|
|
|
simlist_array = np.array(simlist) |
|
|
|
|
|
std_dev = np.std(simlist_array, axis=0) |
|
|
|
print("Standard Deviation of similarity scores:", std_dev) |
|
|
|
mean_sims = np.mean(np.array(simlist), axis=0) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|