WenhaoWang commited on
Commit
0c0900e
·
verified ·
1 Parent(s): 03d4d5b

Upload wizmap_diffusiondb_vidprom_final.py

Browse files
Files changed (1) hide show
  1. wizmap_diffusiondb_vidprom_final.py +283 -0
wizmap_diffusiondb_vidprom_final.py ADDED
@@ -0,0 +1,283 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python
2
+ # coding: utf-8
3
+
4
+ # In[1]:
5
+
6
+
7
+ from VidProM.isc.io import read_descriptors
8
+
9
+
10
+ # In[2]:
11
+
12
+
13
+ vid_name, vid_feature = read_descriptors(['./VidProM/vidprom_embed.hdf5'])
14
+
15
+
16
+ # In[3]:
17
+
18
+
19
+ vid_feature.shape
20
+
21
+
22
+ # In[4]:
23
+
24
+
25
+ import re
26
+
27
+ def remove_numbers_and_words(s):
28
+ # 删除所有数字
29
+ s = re.sub(r'\d+', '', s)
30
+ # 删除指定的单词
31
+ s = re.sub(r'(image|message|attachment|quot|make)', '', s, flags=re.IGNORECASE)
32
+ return s
33
+
34
+
35
+ # In[5]:
36
+
37
+
38
+ import pandas as pd
39
+ df = pd.read_csv('./prompts4video_unique.csv')
40
+ imdb_reviews = list(df['prompt'])
41
+ imdb_reviews_clean = [i.split('-')[0] for i in imdb_reviews]
42
+ vidprom_prompts = [remove_numbers_and_words(i) for i in imdb_reviews_clean]
43
+
44
+
45
+ # In[6]:
46
+
47
+
48
+ len(vidprom_prompts)
49
+
50
+
51
+ # In[7]:
52
+
53
+
54
+ diffdb_name, diffdb_feature = read_descriptors(['./DiffusionDB/diffusiondb_embed.hdf5'])
55
+
56
+
57
+ # In[8]:
58
+
59
+
60
+ diffdb_feature.shape
61
+
62
+
63
+ # In[1]:
64
+
65
+
66
+ import pandas as pd
67
+ path_to_prompt_parquet = "DiffusionDB/metadata-large.parquet"
68
+ prompts = pd.read_parquet(
69
+ path_to_prompt_parquet,
70
+ columns=['prompt']
71
+ )
72
+ diffdb_prompts = sorted(list(set(prompts['prompt'])))
73
+ print("Length of prompts: ", len(diffdb_prompts))
74
+
75
+
76
+ # In[2]:
77
+
78
+
79
+ diffdb_prompts_1 = list(set(prompts['prompt']))
80
+
81
+
82
+ # In[3]:
83
+
84
+
85
+ with open("diffusiondb_prompts.txt", 'w', encoding='utf-8') as file:
86
+ for fruit in diffdb_prompts_1:
87
+ file.write(fruit + '\n')
88
+
89
+
90
+ # In[5]:
91
+
92
+
93
+ len(diffdb_prompts_1)
94
+
95
+
96
+ # In[ ]:
97
+
98
+
99
+ hf.upload_file(path_or_fileobj="./wizmap_vidprom_diffusiondb_final/data_vidprom_diffusiondb.ndjson", \
100
+ path_in_repo="data_vidprom_diffusiondb.ndjson", repo_id="WenhaoWang/VidProM", \
101
+ repo_type="dataset")
102
+
103
+
104
+ # In[ ]:
105
+
106
+
107
+
108
+
109
+
110
+ # In[ ]:
111
+
112
+
113
+
114
+
115
+
116
+ # In[ ]:
117
+
118
+
119
+ import umap
120
+ import numpy as np
121
+ embedding_0 = umap.UMAP(n_neighbors=60,
122
+ min_dist=0.1,
123
+ metric='correlation').fit_transform(np.concatenate([vid_feature,diffdb_feature]))
124
+
125
+
126
+ # In[ ]:
127
+
128
+
129
+ np.save('umap_diffusiondb_vidprom.npy', embedding_0)
130
+
131
+
132
+ # In[10]:
133
+
134
+
135
+ import numpy as np
136
+ embedding_0 = np.load('umap_diffusiondb_vidprom.npy')
137
+
138
+
139
+ # In[11]:
140
+
141
+
142
+ texts = vidprom_prompts + diffdb_prompts
143
+ xs = embedding_0[:, 0].astype(float).tolist()
144
+ ys = embedding_0[:, 1].astype(float).tolist()
145
+
146
+
147
+ # In[12]:
148
+
149
+
150
+ from glob import glob
151
+ from os.path import exists, join, basename
152
+ from tqdm import tqdm
153
+ from json import load, dump
154
+ from matplotlib import pyplot as plt
155
+ from collections import Counter
156
+
157
+ from sklearn.feature_extraction.text import CountVectorizer, TfidfTransformer
158
+ from quadtreed3 import Quadtree, Node
159
+ from scipy.sparse import csr_matrix
160
+ from sklearn.neighbors import KernelDensity
161
+ from scipy.stats import norm
162
+ from typing import Tuple
163
+ from io import BytesIO
164
+ from umap import UMAP
165
+
166
+ import pandas as pd
167
+ import numpy as np
168
+ import ndjson
169
+ import requests
170
+ import urllib
171
+ import wizmap
172
+
173
+ SEED = 20230501
174
+
175
+ plt.rcParams['figure.dpi'] = 300
176
+
177
+
178
+ # In[13]:
179
+
180
+
181
+ labels = [0]*len(vidprom_prompts) + [1] *len(diffdb_prompts)
182
+
183
+
184
+ # In[14]:
185
+
186
+
187
+ len(labels)
188
+
189
+
190
+ # In[15]:
191
+
192
+
193
+ group_names = ["VidProM", "DiffusionDB"]
194
+
195
+
196
+ # In[16]:
197
+
198
+
199
+ grid_dict = wizmap.generate_grid_dict(embedding_0[:, 0].astype(float).tolist(), \
200
+ embedding_0[:, 1].astype(float).tolist(), \
201
+ texts, \
202
+ embedding_name = 'VidProM_DiffusionDB', \
203
+ labels = labels, \
204
+ group_names = group_names)
205
+
206
+
207
+ # In[17]:
208
+
209
+
210
+ print(grid_dict.keys())
211
+
212
+
213
+ # In[18]:
214
+
215
+
216
+ data_list = wizmap.generate_data_list(xs, ys, texts, labels = labels)
217
+
218
+
219
+ # In[19]:
220
+
221
+
222
+ get_ipython().system('mkdir wizmap_vidprom_diffusiondb_final')
223
+
224
+
225
+ # In[20]:
226
+
227
+
228
+ wizmap.save_json_files(data_list, grid_dict, output_dir='./wizmap_vidprom_diffusiondb_final')
229
+
230
+
231
+ # In[21]:
232
+
233
+
234
+ get_ipython().system('mv ./wizmap_vidprom_diffusiondb_final/data.ndjson ./wizmap_vidprom_diffusiondb_final/data_vidprom_diffusiondb.ndjson')
235
+
236
+
237
+ # In[22]:
238
+
239
+
240
+ get_ipython().system('mv ./wizmap_vidprom_diffusiondb_final/grid.json ./wizmap_vidprom_diffusiondb_final/grid_vidprom_diffusiondb.json')
241
+
242
+
243
+ # In[6]:
244
+
245
+
246
+ import os
247
+
248
+ # os.environ["HF_ENDPOINT"] = "http://localhost:5564"
249
+ os.environ["HF_HUB_ENABLE_HF_TRANSFER"] = "1"
250
+
251
+ from huggingface_hub import HfApi, logging
252
+
253
+ logging.set_verbosity_debug()
254
+ hf = HfApi(
255
+ endpoint="https://huggingface.co", # Can be a Private Hub endpoint.
256
+ token="xxxx", # Token is not persisted on the machine.
257
+ )
258
+
259
+
260
+ # In[ ]:
261
+
262
+
263
+ hf.upload_file(path_or_fileobj="./wizmap_vidprom_diffusiondb_final/grid_vidprom_diffusiondb.json", \
264
+ path_in_repo="grid_vidprom_diffusiondb.json", repo_id="WenhaoWang/VidProM", \
265
+ repo_type="dataset")
266
+
267
+
268
+ # In[24]:
269
+
270
+
271
+ hf.upload_file(path_or_fileobj="./wizmap_vidprom_diffusiondb_final/grid_vidprom_diffusiondb.json", \
272
+ path_in_repo="grid_vidprom_diffusiondb.json", repo_id="WenhaoWang/VidProM", \
273
+ repo_type="dataset")
274
+
275
+
276
+ # In[25]:
277
+
278
+
279
+ hf.upload_file(path_or_fileobj="./wizmap_vidprom_diffusiondb_final/data_vidprom_diffusiondb.ndjson", \
280
+ path_in_repo="data_vidprom_diffusiondb.ndjson", repo_id="WenhaoWang/VidProM", \
281
+ repo_type="dataset")
282
+
283
+