text_plain_output_1.png
from blip.models import blip

from clip_interrogator import clip_interrogator

from pathlib import Path

from sentence_transformers import SentenceTransformer, models

import importlib

import inspect

import numpy as np

import numpy as np # linear algebra

import open_clip

import os

import os

import pandas as pd

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

import sys

blip_path = inspect.getfile(blip)
fin = open(blip_path, 'rt')
data = fin.read()
data = data.replace("BertTokenizer.from_pretrained('bert-base-uncased')", "BertTokenizer.from_pretrained('/kaggle/input/clip-interrogator-models-x/bert-base-uncased')")
fin.close()
fin = open(blip_path, 'wt')
fin.write(data)
fin.close()
importlib.reload(blip)

clip_interrogator_path = inspect.getfile(clip_interrogator.Interrogator)
fin = open(clip_interrogator_path, 'rt')
data = fin.read()
data = data.replace('open_clip.get_tokenizer(clip_model_name)', 'open_clip.get_tokenizer(config.clip_model_name.split("/", 2)[0])')
fin.close()
fin = open(clip_interrogator_path, 'wt')
fin.write(data)
fin.close()
importlib.reload(clip_interrogator)

import os
import sys
from PIL import Image
from pathlib import Path
import matplotlib.pyplot as plt
from transformers import AutoProcessor, BlipForConditionalGeneration
import numpy as np
import pandas as pd
import torch
import open_clip
sys.path.append('../input/sentence-transformers-222/sentence-transformers')
from sentence_transformers import SentenceTransformer, models
comp_path = Path('/kaggle/input/stable-diffusion-image-to-prompts/')

df_submission = pd.read_csv(comp_path / 'sample_submission.csv', index_col='imgId_eId')

images = os.listdir(comp_path / 'images')
imgIds = [i.split('.')[0] for i in images]
eIds = list(range(CFG.embedding_length))
imgId_eId = ['_'.join(map(str, i)) for i in zip(np.repeat(imgIds, CFG.embedding_length), np.tile(range(CFG.embedding_length), len(imgIds)))]
assert sorted(imgId_eId) == sorted(df_submission.index)

st_model = SentenceTransformer(CFG.sentence_model_path)

model_config = clip_interrogator.Config(clip_model_name=CFG.ci_clip_model_name)
model_config.cache_path = CFG.cache_path

configs_path = os.path.join(os.path.dirname(os.path.dirname(blip_path)), 'configs')
med_config = os.path.join(configs_path, 'med_config.json')
blip_model = blip.blip_decoder(pretrained=CFG.blip_model_path, image_size=model_config.blip_image_eval_size, vit=model_config.blip_model_type, med_config=med_config)
blip_model.eval()
blip_model = blip_model.to(model_config.device)
model_config.blip_model = blip_model

clip_model = open_clip.create_model(CFG.clip_model_name, precision='fp16' if model_config.device == 'cuda' else 'fp32')
open_clip.load_checkpoint(clip_model, CFG.clip_model_path)
clip_model.to(model_config.device).eval()
model_config.clip_model = clip_model

clip_preprocess = open_clip.image_transform(clip_model.visual.image_size, is_train=False, mean=getattr(clip_model.visual, 'image_mean', None), std=getattr(clip_model.visual, 'image_std', None))
model_config.clip_preprocess = clip_preprocess

ci = clip_interrogator.Interrogator(model_config)