Upload layer_diff_dataset/tagger_jsh2.py with huggingface_hub
Browse files
layer_diff_dataset/tagger_jsh2.py
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from modelscope import (
|
| 2 |
+
snapshot_download, AutoModelForCausalLM, AutoTokenizer, GenerationConfig
|
| 3 |
+
)
|
| 4 |
+
import torch
|
| 5 |
+
import os
|
| 6 |
+
import json
|
| 7 |
+
# model_id = 'qwen/Qwen-VL-Chat'
|
| 8 |
+
# revision = 'v1.1.0'
|
| 9 |
+
|
| 10 |
+
# model_dir = snapshot_download(model_id, revision=revision)
|
| 11 |
+
seed = 1234
|
| 12 |
+
torch.manual_seed(seed)
|
| 13 |
+
model_dir = "/mnt/workspace/workgroup/sihui.jsh/VITON-HD/diffusers/qwen/Qwen-VL-Chat"
|
| 14 |
+
|
| 15 |
+
# 请注意:分词器默认行为已更改为默认关闭特殊token攻击防护。
|
| 16 |
+
tokenizer = AutoTokenizer.from_pretrained(model_dir, trust_remote_code=True)
|
| 17 |
+
# 打开bf16精度,A100、H100、RTX3060、RTX3070等显卡建议启用以节省显存 balanced
|
| 18 |
+
model = AutoModelForCausalLM.from_pretrained(model_dir, device_map="sequential", trust_remote_code=True, fp16=True).eval()
|
| 19 |
+
# 打开fp16精度,V100、P100、T4等显卡建议启用以节省显存
|
| 20 |
+
# model = AutoModelForCausalLM.from_pretrained(model_dir, trust_remote_code=True, fp16=True).eval()
|
| 21 |
+
# 使用CPU进行推理,需要约32GB内存
|
| 22 |
+
# model = AutoModelForCausalLM.from_pretrained(model_dir, device_map="cpu", trust_remote_code=True).eval()
|
| 23 |
+
# 默认使用自动模式,根据设备自动选择精度
|
| 24 |
+
# model = AutoModelForCausalLM.from_pretrained(model_dir, device_map="auto", trust_remote_code=True).eval()
|
| 25 |
+
|
| 26 |
+
# 可指定不同的生成长度、top_p等相关超参
|
| 27 |
+
model.generation_config = GenerationConfig.from_pretrained(model_dir, trust_remote_code=True)
|
| 28 |
+
|
| 29 |
+
# 第一轮对话 1st dialogue turn
|
| 30 |
+
|
| 31 |
+
# image_dir_path = '/home/nfs/wyy/data/deepfashion/Data/test_lst_512_png'
|
| 32 |
+
# image_dir_path = '/home/nfs/wyy/data/VTO/VITON-HD-512/train/image'
|
| 33 |
+
# image_dir_path = '/mnt/workspace/workgroup/sihui.jsh/VITON-HD/try/image'
|
| 34 |
+
root_dir = '/mnt/workspace/workgroup/sihui.jsh/layer_diff_dataset/train'
|
| 35 |
+
image_dir_path = os.path.join(root_dir,'im')
|
| 36 |
+
|
| 37 |
+
from tqdm import tqdm
|
| 38 |
+
# os.makedirs('./test_cap', exist_ok=True)
|
| 39 |
+
image_dir = os.listdir(image_dir_path)
|
| 40 |
+
image_dir.sort()
|
| 41 |
+
# sort
|
| 42 |
+
# image_dir = sorted(image_dir, key=lambda x: x.split('.')[0])
|
| 43 |
+
import numpy as np
|
| 44 |
+
|
| 45 |
+
# test_dir = image_dir_path.replace('cloth_resize_combined1','caption_new1')
|
| 46 |
+
# os.makedirs(test_dir, exist_ok=True)
|
| 47 |
+
|
| 48 |
+
# prompt = 'Describe the image'
|
| 49 |
+
prompt = 'Only describe the background of the image.'
|
| 50 |
+
with open(os.path.join(root_dir,'im_rgba.json'), 'r') as file:
|
| 51 |
+
data = json.load(file)
|
| 52 |
+
# part_size = len(image_dir) // 2
|
| 53 |
+
pbar = tqdm(enumerate(image_dir),total=len(image_dir))
|
| 54 |
+
for idx, image in pbar:
|
| 55 |
+
# if cnt<1000:
|
| 56 |
+
# continue
|
| 57 |
+
# if cnt==2500:
|
| 58 |
+
# break
|
| 59 |
+
image_path = os.path.join(image_dir_path, image)
|
| 60 |
+
|
| 61 |
+
query = tokenizer.from_list_format([
|
| 62 |
+
{'image': image_path},
|
| 63 |
+
{'text': prompt},
|
| 64 |
+
])
|
| 65 |
+
response, _ = model.chat(tokenizer, query=query, history=None)
|
| 66 |
+
if data[idx]["images"]==image_path:
|
| 67 |
+
data[idx]["bg_prompt"] = response
|
| 68 |
+
else:
|
| 69 |
+
print('error!')
|
| 70 |
+
print(image_path)
|
| 71 |
+
print(data[idx]["images"])
|
| 72 |
+
break
|
| 73 |
+
# print(response)
|
| 74 |
+
# shutil.copy(image_path, os.path.join(test_dir, image))
|
| 75 |
+
# print(os.path.join(test_dir,'image2text.txt'))
|
| 76 |
+
# with open(os.path.join(root_dir,'.txt'), 'a') as f:
|
| 77 |
+
# f.write(image + '\t' + response + '\n')
|
| 78 |
+
with open(os.path.join(root_dir,'im_rgba_new.json'), 'w') as file:
|
| 79 |
+
json.dump(data, file, indent=4)
|