{ "cells": [ { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import open_clip\n", "import torch\n", "from tqdm.notebook import tqdm\n", "import pandas as pd\n", "import os\n", "\n", "device = \"cuda\" if torch.cuda.is_available() else \"cpu\"\n", "\n", "PROMPTS = [\n", " '{0}',\n", " 'an image of {0}',\n", " 'a photo of {0}',\n", " '{0} on a photo',\n", " 'a photo of a person named {0}',\n", " 'a person named {0}',\n", " 'a man named {0}',\n", " 'a woman named {0}',\n", " 'the name of the person is {0}',\n", " 'a photo of a person with the name {0}',\n", " '{0} at a gala',\n", " 'a photo of the celebrity {0}',\n", " 'actor {0}',\n", " 'actress {0}',\n", " 'a colored photo of {0}',\n", " 'a black and white photo of {0}',\n", " 'a cool photo of {0}',\n", " 'a cropped photo of {0}',\n", " 'a cropped image of {0}',\n", " '{0} in a suit',\n", " '{0} in a dress'\n", "]\n", "MODEL_NAMES = ['ViT-B-32', 'ViT-B-16', 'ViT-L-14']\n", "SEED = 42" ] }, { "cell_type": "code", "execution_count": 2, "outputs": [], "source": [ "# init clip\n", "models = {}\n", "preprocessings = {}\n", "tokenizers = {}\n", "for model_name in MODEL_NAMES:\n", " model, _, preprocess = open_clip.create_model_and_transforms(model_name, pretrained='laion400m_e32')\n", " preprocessings[model_name] = preprocess\n", " model = model.eval()\n", " models[model_name] = model\n", " tokenizers[model_name] = open_clip.get_tokenizer(model_name)" ], "metadata": { "collapsed": false } }, { "cell_type": "code", "execution_count": 3, "outputs": [], "source": [ "# define a function to get the predictions for an actor/actress\n", "@torch.no_grad()\n", "def get_text_embeddings(model, context, context_batchsize=1_000, use_tqdm=False):\n", " context_batchsize = context_batchsize * torch.cuda.device_count()\n", " # if there is not batches for the context unsqueeze it\n", " if context.dim() < 3:\n", " context = context.unsqueeze(0)\n", "\n", " # get the batch size, the number of labels and the sequence length\n", " seq_len = context.shape[-1]\n", " viewed_context = context.view(-1, seq_len)\n", "\n", " text_features = []\n", " for context_batch_idx in tqdm(range(0, len(viewed_context), context_batchsize), desc=\"Calculating Text Embeddings\",\n", " disable=not use_tqdm):\n", " context_batch = viewed_context[context_batch_idx:context_batch_idx + context_batchsize]\n", " batch_text_features = model.encode_text(context_batch, normalize=True).cpu()\n", "\n", " text_features.append(batch_text_features)\n", " text_features = torch.cat(text_features).view(list(context.shape[:-1]) + [-1])\n", "\n", " return text_features" ], "metadata": { "collapsed": false } }, { "cell_type": "code", "execution_count": 4, "outputs": [ { "data": { "text/plain": " first_name sex last_name\n0 Eliana f Cardenas\n1 Meghann f Daniels\n2 Ada f Stevenson\n3 Elsa f Leblanc\n4 Avah f Lambert\n... ... .. ...\n9995 Kasen m Barker\n9996 Camryn m Roberts\n9997 Henry m Whitaker\n9998 Adin m Richards\n9999 Charley m Herman\n\n[10000 rows x 3 columns]", "text/html": "
\n\n\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
first_namesexlast_name
0ElianafCardenas
1MeghannfDaniels
2AdafStevenson
3ElsafLeblanc
4AvahfLambert
............
9995KasenmBarker
9996CamrynmRoberts
9997HenrymWhitaker
9998AdinmRichards
9999CharleymHerman
\n

10000 rows × 3 columns

\n
" }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# load the possible names\n", "possible_names = pd.read_csv('./full_names.csv', index_col=0)\n", "possible_names\n", "# possible_names_list = (possible_names['first_name'] + ' ' + possible_names['last_name']).tolist()\n", "# possible_names_list[:5]" ], "metadata": { "collapsed": false } }, { "cell_type": "code", "execution_count": 5, "outputs": [ { "data": { "text/plain": " first_name sex last_name prompt_0 prompt_1 \\\n0 Eliana f Cardenas Eliana Cardenas an image of Eliana Cardenas \n1 Meghann f Daniels Meghann Daniels an image of Meghann Daniels \n2 Ada f Stevenson Ada Stevenson an image of Ada Stevenson \n3 Elsa f Leblanc Elsa Leblanc an image of Elsa Leblanc \n4 Avah f Lambert Avah Lambert an image of Avah Lambert \n... ... .. ... ... ... \n9995 Kasen m Barker Kasen Barker an image of Kasen Barker \n9996 Camryn m Roberts Camryn Roberts an image of Camryn Roberts \n9997 Henry m Whitaker Henry Whitaker an image of Henry Whitaker \n9998 Adin m Richards Adin Richards an image of Adin Richards \n9999 Charley m Herman Charley Herman an image of Charley Herman \n\n prompt_2 prompt_3 \\\n0 a photo of Eliana Cardenas Eliana Cardenas on a photo \n1 a photo of Meghann Daniels Meghann Daniels on a photo \n2 a photo of Ada Stevenson Ada Stevenson on a photo \n3 a photo of Elsa Leblanc Elsa Leblanc on a photo \n4 a photo of Avah Lambert Avah Lambert on a photo \n... ... ... \n9995 a photo of Kasen Barker Kasen Barker on a photo \n9996 a photo of Camryn Roberts Camryn Roberts on a photo \n9997 a photo of Henry Whitaker Henry Whitaker on a photo \n9998 a photo of Adin Richards Adin Richards on a photo \n9999 a photo of Charley Herman Charley Herman on a photo \n\n prompt_4 \\\n0 a photo of a person named Eliana Cardenas \n1 a photo of a person named Meghann Daniels \n2 a photo of a person named Ada Stevenson \n3 a photo of a person named Elsa Leblanc \n4 a photo of a person named Avah Lambert \n... ... \n9995 a photo of a person named Kasen Barker \n9996 a photo of a person named Camryn Roberts \n9997 a photo of a person named Henry Whitaker \n9998 a photo of a person named Adin Richards \n9999 a photo of a person named Charley Herman \n\n prompt_5 prompt_6 ... \\\n0 a person named Eliana Cardenas a man named Eliana Cardenas ... \n1 a person named Meghann Daniels a man named Meghann Daniels ... \n2 a person named Ada Stevenson a man named Ada Stevenson ... \n3 a person named Elsa Leblanc a man named Elsa Leblanc ... \n4 a person named Avah Lambert a man named Avah Lambert ... \n... ... ... ... \n9995 a person named Kasen Barker a man named Kasen Barker ... \n9996 a person named Camryn Roberts a man named Camryn Roberts ... \n9997 a person named Henry Whitaker a man named Henry Whitaker ... \n9998 a person named Adin Richards a man named Adin Richards ... \n9999 a person named Charley Herman a man named Charley Herman ... \n\n prompt_11 prompt_12 \\\n0 a photo of the celebrity Eliana Cardenas actor Eliana Cardenas \n1 a photo of the celebrity Meghann Daniels actor Meghann Daniels \n2 a photo of the celebrity Ada Stevenson actor Ada Stevenson \n3 a photo of the celebrity Elsa Leblanc actor Elsa Leblanc \n4 a photo of the celebrity Avah Lambert actor Avah Lambert \n... ... ... \n9995 a photo of the celebrity Kasen Barker actor Kasen Barker \n9996 a photo of the celebrity Camryn Roberts actor Camryn Roberts \n9997 a photo of the celebrity Henry Whitaker actor Henry Whitaker \n9998 a photo of the celebrity Adin Richards actor Adin Richards \n9999 a photo of the celebrity Charley Herman actor Charley Herman \n\n prompt_13 prompt_14 \\\n0 actress Eliana Cardenas a colored photo of Eliana Cardenas \n1 actress Meghann Daniels a colored photo of Meghann Daniels \n2 actress Ada Stevenson a colored photo of Ada Stevenson \n3 actress Elsa Leblanc a colored photo of Elsa Leblanc \n4 actress Avah Lambert a colored photo of Avah Lambert \n... ... ... \n9995 actress Kasen Barker a colored photo of Kasen Barker \n9996 actress Camryn Roberts a colored photo of Camryn Roberts \n9997 actress Henry Whitaker a colored photo of Henry Whitaker \n9998 actress Adin Richards a colored photo of Adin Richards \n9999 actress Charley Herman a colored photo of Charley Herman \n\n prompt_15 \\\n0 a black and white photo of Eliana Cardenas \n1 a black and white photo of Meghann Daniels \n2 a black and white photo of Ada Stevenson \n3 a black and white photo of Elsa Leblanc \n4 a black and white photo of Avah Lambert \n... ... \n9995 a black and white photo of Kasen Barker \n9996 a black and white photo of Camryn Roberts \n9997 a black and white photo of Henry Whitaker \n9998 a black and white photo of Adin Richards \n9999 a black and white photo of Charley Herman \n\n prompt_16 prompt_17 \\\n0 a cool photo of Eliana Cardenas a cropped photo of Eliana Cardenas \n1 a cool photo of Meghann Daniels a cropped photo of Meghann Daniels \n2 a cool photo of Ada Stevenson a cropped photo of Ada Stevenson \n3 a cool photo of Elsa Leblanc a cropped photo of Elsa Leblanc \n4 a cool photo of Avah Lambert a cropped photo of Avah Lambert \n... ... ... \n9995 a cool photo of Kasen Barker a cropped photo of Kasen Barker \n9996 a cool photo of Camryn Roberts a cropped photo of Camryn Roberts \n9997 a cool photo of Henry Whitaker a cropped photo of Henry Whitaker \n9998 a cool photo of Adin Richards a cropped photo of Adin Richards \n9999 a cool photo of Charley Herman a cropped photo of Charley Herman \n\n prompt_18 prompt_19 \\\n0 a cropped image of Eliana Cardenas Eliana Cardenas in a suit \n1 a cropped image of Meghann Daniels Meghann Daniels in a suit \n2 a cropped image of Ada Stevenson Ada Stevenson in a suit \n3 a cropped image of Elsa Leblanc Elsa Leblanc in a suit \n4 a cropped image of Avah Lambert Avah Lambert in a suit \n... ... ... \n9995 a cropped image of Kasen Barker Kasen Barker in a suit \n9996 a cropped image of Camryn Roberts Camryn Roberts in a suit \n9997 a cropped image of Henry Whitaker Henry Whitaker in a suit \n9998 a cropped image of Adin Richards Adin Richards in a suit \n9999 a cropped image of Charley Herman Charley Herman in a suit \n\n prompt_20 \n0 Eliana Cardenas in a dress \n1 Meghann Daniels in a dress \n2 Ada Stevenson in a dress \n3 Elsa Leblanc in a dress \n4 Avah Lambert in a dress \n... ... \n9995 Kasen Barker in a dress \n9996 Camryn Roberts in a dress \n9997 Henry Whitaker in a dress \n9998 Adin Richards in a dress \n9999 Charley Herman in a dress \n\n[10000 rows x 24 columns]", "text/html": "
\n\n\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
first_namesexlast_nameprompt_0prompt_1prompt_2prompt_3prompt_4prompt_5prompt_6...prompt_11prompt_12prompt_13prompt_14prompt_15prompt_16prompt_17prompt_18prompt_19prompt_20
0ElianafCardenasEliana Cardenasan image of Eliana Cardenasa photo of Eliana CardenasEliana Cardenas on a photoa photo of a person named Eliana Cardenasa person named Eliana Cardenasa man named Eliana Cardenas...a photo of the celebrity Eliana Cardenasactor Eliana Cardenasactress Eliana Cardenasa colored photo of Eliana Cardenasa black and white photo of Eliana Cardenasa cool photo of Eliana Cardenasa cropped photo of Eliana Cardenasa cropped image of Eliana CardenasEliana Cardenas in a suitEliana Cardenas in a dress
1MeghannfDanielsMeghann Danielsan image of Meghann Danielsa photo of Meghann DanielsMeghann Daniels on a photoa photo of a person named Meghann Danielsa person named Meghann Danielsa man named Meghann Daniels...a photo of the celebrity Meghann Danielsactor Meghann Danielsactress Meghann Danielsa colored photo of Meghann Danielsa black and white photo of Meghann Danielsa cool photo of Meghann Danielsa cropped photo of Meghann Danielsa cropped image of Meghann DanielsMeghann Daniels in a suitMeghann Daniels in a dress
2AdafStevensonAda Stevensonan image of Ada Stevensona photo of Ada StevensonAda Stevenson on a photoa photo of a person named Ada Stevensona person named Ada Stevensona man named Ada Stevenson...a photo of the celebrity Ada Stevensonactor Ada Stevensonactress Ada Stevensona colored photo of Ada Stevensona black and white photo of Ada Stevensona cool photo of Ada Stevensona cropped photo of Ada Stevensona cropped image of Ada StevensonAda Stevenson in a suitAda Stevenson in a dress
3ElsafLeblancElsa Leblancan image of Elsa Leblanca photo of Elsa LeblancElsa Leblanc on a photoa photo of a person named Elsa Leblanca person named Elsa Leblanca man named Elsa Leblanc...a photo of the celebrity Elsa Leblancactor Elsa Leblancactress Elsa Leblanca colored photo of Elsa Leblanca black and white photo of Elsa Leblanca cool photo of Elsa Leblanca cropped photo of Elsa Leblanca cropped image of Elsa LeblancElsa Leblanc in a suitElsa Leblanc in a dress
4AvahfLambertAvah Lambertan image of Avah Lamberta photo of Avah LambertAvah Lambert on a photoa photo of a person named Avah Lamberta person named Avah Lamberta man named Avah Lambert...a photo of the celebrity Avah Lambertactor Avah Lambertactress Avah Lamberta colored photo of Avah Lamberta black and white photo of Avah Lamberta cool photo of Avah Lamberta cropped photo of Avah Lamberta cropped image of Avah LambertAvah Lambert in a suitAvah Lambert in a dress
..................................................................
9995KasenmBarkerKasen Barkeran image of Kasen Barkera photo of Kasen BarkerKasen Barker on a photoa photo of a person named Kasen Barkera person named Kasen Barkera man named Kasen Barker...a photo of the celebrity Kasen Barkeractor Kasen Barkeractress Kasen Barkera colored photo of Kasen Barkera black and white photo of Kasen Barkera cool photo of Kasen Barkera cropped photo of Kasen Barkera cropped image of Kasen BarkerKasen Barker in a suitKasen Barker in a dress
9996CamrynmRobertsCamryn Robertsan image of Camryn Robertsa photo of Camryn RobertsCamryn Roberts on a photoa photo of a person named Camryn Robertsa person named Camryn Robertsa man named Camryn Roberts...a photo of the celebrity Camryn Robertsactor Camryn Robertsactress Camryn Robertsa colored photo of Camryn Robertsa black and white photo of Camryn Robertsa cool photo of Camryn Robertsa cropped photo of Camryn Robertsa cropped image of Camryn RobertsCamryn Roberts in a suitCamryn Roberts in a dress
9997HenrymWhitakerHenry Whitakeran image of Henry Whitakera photo of Henry WhitakerHenry Whitaker on a photoa photo of a person named Henry Whitakera person named Henry Whitakera man named Henry Whitaker...a photo of the celebrity Henry Whitakeractor Henry Whitakeractress Henry Whitakera colored photo of Henry Whitakera black and white photo of Henry Whitakera cool photo of Henry Whitakera cropped photo of Henry Whitakera cropped image of Henry WhitakerHenry Whitaker in a suitHenry Whitaker in a dress
9998AdinmRichardsAdin Richardsan image of Adin Richardsa photo of Adin RichardsAdin Richards on a photoa photo of a person named Adin Richardsa person named Adin Richardsa man named Adin Richards...a photo of the celebrity Adin Richardsactor Adin Richardsactress Adin Richardsa colored photo of Adin Richardsa black and white photo of Adin Richardsa cool photo of Adin Richardsa cropped photo of Adin Richardsa cropped image of Adin RichardsAdin Richards in a suitAdin Richards in a dress
9999CharleymHermanCharley Hermanan image of Charley Hermana photo of Charley HermanCharley Herman on a photoa photo of a person named Charley Hermana person named Charley Hermana man named Charley Herman...a photo of the celebrity Charley Hermanactor Charley Hermanactress Charley Hermana colored photo of Charley Hermana black and white photo of Charley Hermana cool photo of Charley Hermana cropped photo of Charley Hermana cropped image of Charley HermanCharley Herman in a suitCharley Herman in a dress
\n

10000 rows × 24 columns

\n
" }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# populate the prompts with the possible names\n", "prompts = []\n", "for idx, row in possible_names.iterrows():\n", " df_dict = row.to_dict()\n", " name = f'{row[\"first_name\"]} {row[\"last_name\"]}'\n", " for prompt_idx, prompt in enumerate(PROMPTS):\n", " df_dict[f'prompt_{prompt_idx}'] = prompt.format(name)\n", " prompts.append(df_dict)\n", "prompts = pd.DataFrame(prompts)\n", "prompts" ], "metadata": { "collapsed": false } }, { "cell_type": "code", "execution_count": 7, "outputs": [], "source": [ "label_context_vecs = []\n", "for i in range(len(PROMPTS)):\n", " context = open_clip.tokenize(prompts[f'prompt_{i}'].to_numpy())\n", " label_context_vecs.append(context)\n", "label_context_vecs = torch.stack(label_context_vecs)" ], "metadata": { "collapsed": false } }, { "cell_type": "code", "execution_count": 8, "outputs": [ { "data": { "text/plain": "Calculating Text Embeddings: 0%| | 0/210 [00:00