{ "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 | first_name | \nsex | \nlast_name | \n
---|---|---|---|
0 | \nEliana | \nf | \nCardenas | \n
1 | \nMeghann | \nf | \nDaniels | \n
2 | \nAda | \nf | \nStevenson | \n
3 | \nElsa | \nf | \nLeblanc | \n
4 | \nAvah | \nf | \nLambert | \n
... | \n... | \n... | \n... | \n
9995 | \nKasen | \nm | \nBarker | \n
9996 | \nCamryn | \nm | \nRoberts | \n
9997 | \nHenry | \nm | \nWhitaker | \n
9998 | \nAdin | \nm | \nRichards | \n
9999 | \nCharley | \nm | \nHerman | \n
10000 rows × 3 columns
\n\n | first_name | \nsex | \nlast_name | \nprompt_0 | \nprompt_1 | \nprompt_2 | \nprompt_3 | \nprompt_4 | \nprompt_5 | \nprompt_6 | \n... | \nprompt_11 | \nprompt_12 | \nprompt_13 | \nprompt_14 | \nprompt_15 | \nprompt_16 | \nprompt_17 | \nprompt_18 | \nprompt_19 | \nprompt_20 | \n
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | \nEliana | \nf | \nCardenas | \nEliana Cardenas | \nan image of Eliana Cardenas | \na photo of Eliana Cardenas | \nEliana Cardenas on a photo | \na photo of a person named Eliana Cardenas | \na person named Eliana Cardenas | \na man named Eliana Cardenas | \n... | \na photo of the celebrity Eliana Cardenas | \nactor Eliana Cardenas | \nactress Eliana Cardenas | \na colored photo of Eliana Cardenas | \na black and white photo of Eliana Cardenas | \na cool photo of Eliana Cardenas | \na cropped photo of Eliana Cardenas | \na cropped image of Eliana Cardenas | \nEliana Cardenas in a suit | \nEliana Cardenas in a dress | \n
1 | \nMeghann | \nf | \nDaniels | \nMeghann Daniels | \nan image of Meghann Daniels | \na photo of Meghann Daniels | \nMeghann Daniels on a photo | \na photo of a person named Meghann Daniels | \na person named Meghann Daniels | \na man named Meghann Daniels | \n... | \na photo of the celebrity Meghann Daniels | \nactor Meghann Daniels | \nactress Meghann Daniels | \na colored photo of Meghann Daniels | \na black and white photo of Meghann Daniels | \na cool photo of Meghann Daniels | \na cropped photo of Meghann Daniels | \na cropped image of Meghann Daniels | \nMeghann Daniels in a suit | \nMeghann Daniels in a dress | \n
2 | \nAda | \nf | \nStevenson | \nAda Stevenson | \nan image of Ada Stevenson | \na photo of Ada Stevenson | \nAda Stevenson on a photo | \na photo of a person named Ada Stevenson | \na person named Ada Stevenson | \na man named Ada Stevenson | \n... | \na photo of the celebrity Ada Stevenson | \nactor Ada Stevenson | \nactress Ada Stevenson | \na colored photo of Ada Stevenson | \na black and white photo of Ada Stevenson | \na cool photo of Ada Stevenson | \na cropped photo of Ada Stevenson | \na cropped image of Ada Stevenson | \nAda Stevenson in a suit | \nAda Stevenson in a dress | \n
3 | \nElsa | \nf | \nLeblanc | \nElsa Leblanc | \nan image of Elsa Leblanc | \na photo of Elsa Leblanc | \nElsa Leblanc on a photo | \na photo of a person named Elsa Leblanc | \na person named Elsa Leblanc | \na man named Elsa Leblanc | \n... | \na photo of the celebrity Elsa Leblanc | \nactor Elsa Leblanc | \nactress Elsa Leblanc | \na colored photo of Elsa Leblanc | \na black and white photo of Elsa Leblanc | \na cool photo of Elsa Leblanc | \na cropped photo of Elsa Leblanc | \na cropped image of Elsa Leblanc | \nElsa Leblanc in a suit | \nElsa Leblanc in a dress | \n
4 | \nAvah | \nf | \nLambert | \nAvah Lambert | \nan image of Avah Lambert | \na photo of Avah Lambert | \nAvah Lambert on a photo | \na photo of a person named Avah Lambert | \na person named Avah Lambert | \na man named Avah Lambert | \n... | \na photo of the celebrity Avah Lambert | \nactor Avah Lambert | \nactress Avah Lambert | \na colored photo of Avah Lambert | \na black and white photo of Avah Lambert | \na cool photo of Avah Lambert | \na cropped photo of Avah Lambert | \na cropped image of Avah Lambert | \nAvah Lambert in a suit | \nAvah Lambert in a dress | \n
... | \n... | \n... | \n... | \n... | \n... | \n... | \n... | \n... | \n... | \n... | \n... | \n... | \n... | \n... | \n... | \n... | \n... | \n... | \n... | \n... | \n... | \n
9995 | \nKasen | \nm | \nBarker | \nKasen Barker | \nan image of Kasen Barker | \na photo of Kasen Barker | \nKasen Barker on a photo | \na photo of a person named Kasen Barker | \na person named Kasen Barker | \na man named Kasen Barker | \n... | \na photo of the celebrity Kasen Barker | \nactor Kasen Barker | \nactress Kasen Barker | \na colored photo of Kasen Barker | \na black and white photo of Kasen Barker | \na cool photo of Kasen Barker | \na cropped photo of Kasen Barker | \na cropped image of Kasen Barker | \nKasen Barker in a suit | \nKasen Barker in a dress | \n
9996 | \nCamryn | \nm | \nRoberts | \nCamryn Roberts | \nan image of Camryn Roberts | \na photo of Camryn Roberts | \nCamryn Roberts on a photo | \na photo of a person named Camryn Roberts | \na person named Camryn Roberts | \na man named Camryn Roberts | \n... | \na photo of the celebrity Camryn Roberts | \nactor Camryn Roberts | \nactress Camryn Roberts | \na colored photo of Camryn Roberts | \na black and white photo of Camryn Roberts | \na cool photo of Camryn Roberts | \na cropped photo of Camryn Roberts | \na cropped image of Camryn Roberts | \nCamryn Roberts in a suit | \nCamryn Roberts in a dress | \n
9997 | \nHenry | \nm | \nWhitaker | \nHenry Whitaker | \nan image of Henry Whitaker | \na photo of Henry Whitaker | \nHenry Whitaker on a photo | \na photo of a person named Henry Whitaker | \na person named Henry Whitaker | \na man named Henry Whitaker | \n... | \na photo of the celebrity Henry Whitaker | \nactor Henry Whitaker | \nactress Henry Whitaker | \na colored photo of Henry Whitaker | \na black and white photo of Henry Whitaker | \na cool photo of Henry Whitaker | \na cropped photo of Henry Whitaker | \na cropped image of Henry Whitaker | \nHenry Whitaker in a suit | \nHenry Whitaker in a dress | \n
9998 | \nAdin | \nm | \nRichards | \nAdin Richards | \nan image of Adin Richards | \na photo of Adin Richards | \nAdin Richards on a photo | \na photo of a person named Adin Richards | \na person named Adin Richards | \na man named Adin Richards | \n... | \na photo of the celebrity Adin Richards | \nactor Adin Richards | \nactress Adin Richards | \na colored photo of Adin Richards | \na black and white photo of Adin Richards | \na cool photo of Adin Richards | \na cropped photo of Adin Richards | \na cropped image of Adin Richards | \nAdin Richards in a suit | \nAdin Richards in a dress | \n
9999 | \nCharley | \nm | \nHerman | \nCharley Herman | \nan image of Charley Herman | \na photo of Charley Herman | \nCharley Herman on a photo | \na photo of a person named Charley Herman | \na person named Charley Herman | \na man named Charley Herman | \n... | \na photo of the celebrity Charley Herman | \nactor Charley Herman | \nactress Charley Herman | \na colored photo of Charley Herman | \na black and white photo of Charley Herman | \na cool photo of Charley Herman | \na cropped photo of Charley Herman | \na cropped image of Charley Herman | \nCharley Herman in a suit | \nCharley Herman in a dress | \n
10000 rows × 24 columns
\n