zjuJish commited on
Commit
73d263c
·
verified ·
1 Parent(s): f109b14

Upload layer_diff_dataset/tagger_jsh2 copy 2.py with huggingface_hub

Browse files
layer_diff_dataset/tagger_jsh2 copy 2.py ADDED
@@ -0,0 +1,80 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 = '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 cnt, image in pbar:
55
+
56
+ if cnt<=500:
57
+ continue
58
+ if cnt>1000:
59
+ break
60
+ image_path = os.path.join(image_dir_path, image)
61
+
62
+ query = tokenizer.from_list_format([
63
+ {'image': image_path},
64
+ {'text': prompt},
65
+ ])
66
+ response, _ = model.chat(tokenizer, query=query, history=None)
67
+ # if data[idx]["images"]==image_path:
68
+ # data[idx]["bg_prompt"] = response
69
+ # else:
70
+ # print('error!')
71
+ # print(image_path)
72
+ # print(data[idx]["images"])
73
+ # break
74
+ # print(response)
75
+ # shutil.copy(image_path, os.path.join(test_dir, image))
76
+ # print(os.path.join(test_dir,'image2text.txt'))
77
+ with open(os.path.join(root_dir,'im_rgba2.txt'), 'a') as f:
78
+ f.write(image + '\t' + response + '\n')
79
+ # with open(os.path.join(root_dir,'im_rgba_new.json'), 'w') as file:
80
+ # json.dump(data, file, indent=4)