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