PhiCookBook
PhiCookBook copied to clipboard
unrelated output during phi 3.5 vision inference
when using phi 3.5 vision for text generation, it can output the corresponding answer first, but followed by some unrelated texts(shown in red box)...
This is my code:
import os
import torch
import transformers
from peft import LoraConfig, get_peft_model
import ast
from transformers import AutoProcessor, BitsAndBytesConfig, AutoModelForCausalLM
from trainer import Phi3VTrainer
from data import make_supervised_data_module
from params import DataArguments, ModelArguments, TrainingArguments
from train_utils import get_peft_state_maybe_zero_3, get_peft_state_non_lora_maybe_zero_3, safe_save_model_for_hf_trainer
import pathlib
from models.processing_phi3_v import Phi3VProcessor
from models.modeling_phi3_v import Phi3VForCausalLM
from PIL import Image
import copy
from data import llava_to_openai
import numpy as np
model_path='/root/autodl-tmp/ckpt/'
model = Phi3VForCausalLM.from_pretrained(model_path, torch_dtype="auto", device_map="auto")
processor = Phi3VProcessor.from_pretrained(model_path)
images = []
images.append(Image.open('/root/autodl-tmp/img/00002.jpg').convert("RGB"))
sources={
"id": "c89d63d1-d314-4245-afd3-d6adcc773513",
"image": "00002.jpg",
"conversations": [
{
"from": "human",
"value": "<image>\nTake a detailed look at the image's quality and give a disrcription. Do not output other unrelated words."
},
{
"from": "gpt",
"value": "The overall clarity of this image is good, and the people and shops in the picture retain rich texture details. However, there are some composition issues, and the content of the image is somewhat cluttered. There are also some focusing problems, resulting in some blurriness in the crowd. Therefore, the quality of this image is mediocre."
}
]
}
sources = copy.deepcopy(llava_to_openai(sources['conversations'], is_video=False, num_frames=None))
user_input=sources[0]
user_input = processor.tokenizer.apply_chat_template([user_input], tokenize=False, add_generation_prompt=True)
inputs = processor(text=user_input, images=images, return_tensors='pt')
inputs = inputs.to("cuda")
generate_ids = model.generate(**inputs, max_new_tokens=1024)
generate_ids_trimmed = [out_ids[len(in_ids) :] for in_ids, out_ids in zip(inputs.input_ids, generate_ids)]
output = processor.batch_decode(generate_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0]
print('sources: \n', sources)
print('output: \n', output)
Maybe it looks like the eot_token isn't generated. Or just by passing it.
You could set the stop_token with <end>. That's gonna stop generating for now.
Maybe it looks like the eot_token isn't generated. Or just by passing it. You could set the stop_token with
<end>. That's gonna stop generating for now.
very useful suggestion, and it works! Thank you!!