File size: 8,348 Bytes
f75b087 be108c4 f75b087 7f24008 f75b087 63024cf f75b087 63024cf f75b087 63024cf f75b087 f6b05e3 f75b087 63024cf f75b087 9755f16 f75b087 f6b05e3 f75b087 1f44214 f75b087 9755f16 f75b087 e365d78 f75b087 e365d78 f75b087 7f24008 f75b087 e365d78 f75b087 e365d78 f75b087 7f24008 e365d78 f75b087 1b481c7 5d752fd 1b481c7 f75b087 f6b05e3 f75b087 1f44214 f75b087 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 |
# after SmilingWolf/wd-tagger
import gradio as gr
import torch
import torch.nn as nn
import timm
import timm.layers.ml_decoder
from transformers import AutoModel, AutoTokenizer
import torchvision
from torchvision import transforms
import PIL
from PIL import Image
import requests
from io import BytesIO
import json
import pickle
headers = {
"User-Agent": "Gradio 0-shot classification demo",
}
TITLE = "Danbooru 0-shot classifiction demo"
DESCRIPTION = """
Demo for 0-shot classification on Danbooru images.
Davit-tiny backbone, ML-Decoder classification head, Alibaba-NLP/gte-large-en-v1.5 text embedding model.
Training set includes IDs with <= 5,400,000 and last 3 digits in range [0, 899], inclusive.
Get image by uploading or fetching by post ID.
Get tag description by input box or fetching by tag name.
"""
def scrape_img(postID):
postURL = "https://danbooru.donmai.us/posts/" + str(postID) + ".json"
postData = json.loads(requests.get(postURL, headers=headers).content)
imageURL = postData['file_url']
print("Getting image from " + imageURL)
response = requests.get(imageURL, headers=headers)
image = Image.open(BytesIO(response.content))
image.load()
return image
def scrape_wiki(tagName):
wikiHistoryURL = f"https://danbooru.donmai.us/wiki_page_versions.json?search[title]={tagName}"
wikiHistory = json.loads(requests.get(wikiHistoryURL, headers=headers).content)
wikiBody = (": " + wikiHistory[0]['body'] if len(wikiHistory) > 0 else "")
return tagName + wikiBody
class Predictor:
def __init__(self):
self.img_size = (288, 288)
self.cls_model = None
self.tokenizer = None
self.text_emb_model = None
self.class_embed = None
self.tag_names = None
self.load_model()
def load_model(self):
with open('tags1588.pkl', 'rb') as f:
classes = pickle.load(f)
tagNames = classes[0].to_list()
self.tag_names = tagNames
pretrained_weights = torch.load('model.pth', map_location=torch.device('cpu'))
self.class_embed = pretrained_weights['0.head.head.class_embed.weight']
cls_model = timm.create_model('davit_tiny', num_classes=len(classes))
cls_model = timm.layers.ml_decoder.add_ml_decoder_head(
cls_model,
num_groups=len(classes),
class_embed=self.class_embed,
class_embed_merge='',
shared_fc=True)
cls_model = nn.Sequential(cls_model)
cls_model.load_state_dict(pretrained_weights, strict=True)
cls_model = cls_model.eval()
self.cls_model = cls_model
model_path = 'Alibaba-NLP/gte-large-en-v1.5'
self.tokenizer = AutoTokenizer.from_pretrained(model_path)
self.text_emb_model = AutoModel.from_pretrained(model_path, trust_remote_code=True)
self.text_emb_model = self.text_emb_model.eval()
def embed_text(self, input_strings):
with torch.no_grad():
# Tokenize the input texts
embeddingList = []
for text in input_strings:
batch_dict = self.tokenizer(text, padding=True, truncation=False, return_tensors='pt')
outputs = self.text_emb_model(**batch_dict.to(self.text_emb_model.device))
embeddings = outputs.last_hidden_state[:, 0]
embeddingList.append(embeddings.cpu())
embeddings = torch.cat(embeddingList)
return embeddings
def prepare_image(self, image):
image.load() # check if file valid
image = image.convert("RGBA")
color = (255,255,255)
background = Image.new('RGB', image.size, color)
background.paste(image, mask=image.split()[3])
image = background
image = transforms.Resize(self.img_size, interpolation = torchvision.transforms.InterpolationMode.BICUBIC)(image)
image = transforms.ToTensor()(image)
return image
def predict(
self,
image,
query,
tag_names,
):
image = self.prepare_image(image)
image_features = self.cls_model[0].forward_features(image.unsqueeze(0))
outputs = self.cls_model[0].head(image_features, q = query).sigmoid().float()
general_tag_list = list(zip(tag_names, outputs[0].tolist()))
general_tag_list.sort(key=lambda y: y[1], reverse=True)
general_tag_preds_dict = {}
for tag, prob in general_tag_list[:50]:
general_tag_preds_dict[tag] = prob
return general_tag_preds_dict
def predict_seen_tags(
self,
image,
):
return self.predict(image, self.class_embed, self.tag_names)
def predict_new_tag(
self,
image,
description,
):
return self.predict(image, self.embed_text([description]), ["embedding"])["embedding"]
def main():
predictor = Predictor()
with gr.Blocks(title=TITLE) as demo:
with gr.Column():
gr.Markdown(
value=f"<h1 style='text-align: center; margin-bottom: 1rem'>{TITLE}</h1>"
)
gr.Markdown(value=DESCRIPTION)
with gr.Row():
with gr.Column(variant="panel"):
image = gr.Image(type="pil", image_mode="RGBA", label="Input", height=600)
with gr.Row():
post_id = gr.Textbox(label="Post ID")
with gr.Column():
clear = gr.ClearButton(
value="Clear image",
components=[
image,
],
variant="secondary",
size="lg",
)
get_post = gr.Button(value="Get Post", variant="primary", size="lg")
with gr.Row():
submit = gr.Button(value="Predict known tags", variant="primary", size="lg")
with gr.Column(variant="panel"):
tag_description = gr.Textbox(label="Tag description")
with gr.Row():
tag_name = gr.Textbox(label="Tag Name")
description_prediction = gr.Textbox(label="Probability")
with gr.Row():
clear_tag_data = gr.ClearButton(value="Clear tag", variant="secondary", size="lg")
get_tag_description = gr.Button(value="Get tag description", variant="primary", size="lg")
predict_on_description = gr.Button(value="Predict described tag")
general_bars = gr.Label(label="Known tags")
clear.add(
[
general_bars,
description_prediction,
post_id,
]
)
clear_tag_data.add(
[
tag_description,
tag_name,
description_prediction,
]
)
examples = gr.Examples(
[
[
"8801249",
"short_over_long_sleeves"
],
],
inputs=[
post_id,
tag_name,
],
run_on_click=False,
cache_examples=False,
)
submit.click(
predictor.predict_seen_tags,
inputs=[
image,
],
outputs=[general_bars],
)
predict_on_description.click(
predictor.predict_new_tag,
inputs=[image, tag_description],
outputs=[description_prediction]
)
get_post.click(
scrape_img,
inputs=[post_id],
outputs=[image]
)
get_tag_description.click(
scrape_wiki,
inputs=[tag_name],
outputs=[tag_description]
)
demo.queue(max_size=10)
demo.launch()
if __name__ == "__main__":
main() |