Views
No views yet
pip install transformers==4.45.0).1import requests
2from PIL import Image
3import torch
4from transformers import AutoProcessor, LlavaOnevisionForConditionalGeneration
5
6model_id = "zhangzicheng/q-sit-mini"
7# if you want to use primary version, switch to q-sit
8# model_id = "zhangzicheng/q-sit"
9
10model = LlavaOnevisionForConditionalGeneration.from_pretrained(
11 model_id,
12 torch_dtype=torch.float16,
13 low_cpu_mem_usage=True,
14).to(0)
15
16processor = AutoProcessor.from_pretrained(model_id)
17
18
19conversation = [
20 {
21 "role": "user",
22 "content": [
23 {"type": "text", "text": "How is the clarity of the human in this image?"},
24 {"type": "image"},
25 ],
26 },
27]
28prompt = processor.apply_chat_template(conversation, add_generation_prompt=True)
29
30raw_image = Image.open(requests.get("https://github.com/Q-Future/Q-SiT/blob/main/44009500.jpg?raw=true",stream=True).raw)
31
32inputs = processor(images=raw_image, text=prompt, return_tensors='pt').to(0, torch.float16)
33
34output = model.generate(**inputs, max_new_tokens=200, do_sample=False)
35print(processor.decode(output[0][2:], skip_special_tokens=True).split("assistant")[-1])
36# very low1import torch
2import requests
3from PIL import Image
4from transformers import AutoProcessor, LlavaOnevisionForConditionalGeneration, AutoTokenizer
5import numpy as np
6
7def wa5(logits):
8 logprobs = np.array([logits["Excellent"], logits["Good"], logits["Fair"], logits["Poor"], logits["Bad"]])
9 probs = np.exp(logprobs) / np.sum(np.exp(logprobs))
10 return np.inner(probs, np.array([1, 0.75, 0.5, 0.25, 0]))
11
12model_id = "zhangzicheng/q-sit-mini"
13model = LlavaOnevisionForConditionalGeneration.from_pretrained(
14 model_id,
15 torch_dtype=torch.float16,
16 low_cpu_mem_usage=True,
17).to(0)
18
19processor = AutoProcessor.from_pretrained(model_id)
20tokenizer = AutoTokenizer.from_pretrained(model_id)
21
22# Define rating tokens
23toks = ["Excellent", "Good", "Fair", "Poor", "Bad"]
24ids_ = [id_[0] for id_ in tokenizer(toks)["input_ids"]]
25print("Rating token IDs:", ids_)
26
27conversation = [
28 {
29 "role": "user",
30 "content": [
31 {"type": "text", "text": "Assume you are an image quality evaluator.
32Your rating should be chosen from the following five categories: Excellent, Good, Fair, Poor, and Bad (from high to low).
33How would you rate the quality of this image?"},
34 {"type": "image"},
35 ],
36 },
37]
38prompt = processor.apply_chat_template(conversation, add_generation_prompt=True)
39
40# Load image
41raw_image = Image.open(requests.get("https://github.com/Q-Future/Q-SiT/blob/main/44009500.jpg?raw=true",stream=True).raw)
42inputs = processor(images=raw_image, text=prompt, return_tensors='pt').to(0, torch.float16)
43
44# Manually append the assistant prefix "The quality of this image is "
45prefix_text = "The quality of this image is "
46prefix_ids = tokenizer(prefix_text, return_tensors="pt")["input_ids"].to(0)
47inputs["input_ids"] = torch.cat([inputs["input_ids"], prefix_ids], dim=-1)
48inputs["attention_mask"] = torch.ones_like(inputs["input_ids"]) # Update attention mask
49
50# Generate exactly one token (the rating)
51output = model.generate(
52 **inputs,
53 max_new_tokens=1, # Generate only the rating token
54 output_logits=True,
55 return_dict_in_generate=True,
56)
57
58# Extract logits for the generated rating token
59last_logits = output.logits[-1][0] # Shape: [vocab_size]
60logits_dict = {tok: last_logits[id_].item() for tok, id_ in zip(toks, ids_)}
61weighted_score = wa5(logits_dict)
62print("Weighted average score:", weighted_score)
63# Weighted average score: 0.045549712192942585 range from 0-1
64# if you want range from 0-5, multiply 5