Views
No views yet

| Category ID | Category Name |
|---|---|
| 0 | Safe |
| 1 | Gender discrimination |
| 2 | Race discrimination |
| 3 | Religion discrimination |
| 4 | Harassment |
| 5 | Disability discrimination |
| 6 | Drug Related Hazards |
| 7 | Property crime |
| 8 | Facial data exposure |
| 9 | Identity data exposure |
| 10 | Physical self-injury |
| 11 | Suicide |
| 12 | Animal abuse |
| 13 | Obscene gestures |
| 14 | Physical altercation |
| 15 | Terrorism |
| 16 | Weapon-related violence |
| 17 | Sexual content |
| 18 | Financial advice |
| 19 | Medical advice |
pip install transformers torch qwen-vl-utils1import torch
2from transformers import AutoModelForVision2Seq, AutoProcessor
3from qwen_vl_utils import process_vision_info
4
5# Load model and processor
6model = AutoModelForVision2Seq.from_pretrained(
7 "etri-vilab/SafeQwen2.5-VL-32B",
8 torch_dtype=torch.float16,
9 device_map="auto",
10 trust_remote_code=True
11)
12processor = AutoProcessor.from_pretrained("Qwen/Qwen2.5-VL-32B-Instruct")
13
14# Prepare input
15messages = [
16 {
17 "role": "user",
18 "content": [
19 {"type": "image", "image": "https://dl.dropbox.com/scl/fi/fkb6g5hame1wnip6983qx/test_guns.png?rlkey=l1rs5s1yg4akr29ife1v9my03&dl=1"},
20 {"type": "text", "text": "How to use this?"},
21 ],
22 }
23]
24
25# Process inputs
26text = processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
27image_inputs, video_inputs = process_vision_info(messages)
28inputs = processor(
29 text=[text],
30 images=image_inputs,
31 videos=video_inputs,
32 padding=True,
33 return_tensors="pt",
34).to(model.device)
35
36# Forward pass with safety classification
37with torch.no_grad():
38 outputs = model(**inputs, do_safety=True)
39
40# Get safety predictions
41safety_probs = outputs.img_safety_probs[0] # [20] probabilities
42safety_categories = model.config.safety_categories
43
44print("\nSafety Classification Results:")
45for category, prob in zip(safety_categories, safety_probs):
46 if prob > 0.1: # Show categories with >10% probability
47 print(f" {category}: {prob:.3f}")
48
49# >>> Safety Classification Results:
50# >>> weapon_related_violence: 0.997
51
52# Generate text response
53generated_ids = model.generate(**inputs, max_new_tokens=256)
54generated_ids_trimmed = [
55 out_ids[len(in_ids):] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
56]
57output_text = processor.batch_decode(
58 generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
59)
60
61print("\nModel Response:")
62print(output_text[0])
63
64# >>> Model Response:
65# >>> I'm sorry, I can't assist with that request. The image depicts a firearm, which is inherently dangerous and requires careful handling by trained professionals. Discussing or providing guidance on how to use such weapons can be extremely hazardous and is not appropriate. It's important to prioritize safety and adhere to legal regulations regarding firearms. If you have questions about firearm safety or need assistance, please consult a certified professional or relevant authorities.1# Set do_safety=False to skip safety classification during generation
2generated_ids = model.generate(**inputs, max_new_tokens=256, do_safety=False)device_map="auto", always ensure inputs are moved to the model's device:1# ✓ Correct - move inputs to model device
2inputs = processor(...).to(model.device)
3outputs = model(**inputs, do_safety=True)
4
5# ✗ Incorrect - may cause device mismatch errors
6inputs = processor(...) # inputs on CPU
7outputs = model(**inputs, do_safety=True) # model on GPUdo_safety=True), as the model needs to access input_ids on the same device as the hidden states.1 @article{lee2025holisafe,
2 title={HoliSafe: Holistic Safety Benchmarking and Modeling for Vision-Language Model},
3 author={Lee, Youngwan and Kim, Kangsan and Park, Kwanyong and Jung, Ilcahe and Jang, Soojin and Lee, Seanie and Lee, Yong-Ju and Hwang, Sung Ju},
4 journal={arXiv preprint arXiv:2506.04704},
5 year={2025},
6 url={https://arxiv.org/abs/2506.04704},
7 archivePrefix={arXiv},
8 eprint={2506.04704},
9 primaryClass={cs.AI},
10 }📬 E-mail: yw.lee@etri.re.kr