File size: 6,930 Bytes
936459a |
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 |
import os
import collections
import json
import logging
import argparse
import numpy as np
import pandas as pd
import torch
from time import time
from torch import optim
from tqdm import tqdm
import torch.utils.data as data
from torch.utils.data import DataLoader
from index.models.rqvae import RQVAE
# from rq_llama import *
# from index.datasets import EmbDataset
import random
class NpyDataset(data.Dataset):
def __init__(self, data_path):
self.data_path = data_path
self.embeddings = np.load(data_path)
self.dim = self.embeddings.shape[-1]
def __getitem__(self, index):
emb = self.embeddings[index]
tensor_emb = torch.FloatTensor(emb)
return tensor_emb
def __len__(self):
return len(self.embeddings)
def if_collided(all_indices_str):
tot_item = len(all_indices_str)
tot_indice = len(set(all_indices_str.tolist()))
return tot_item == tot_indice
def get_indices_count(all_indices_str):
indices_count = collections.defaultdict(int)
for index in all_indices_str:
indices_count[index] += 1
return indices_count
def get_collision_item(all_indices_str):
index2id = {}
for i, index in enumerate(all_indices_str):
if index not in index2id:
index2id[index] = []
index2id[index].append(i)
collision_item_groups = []
for index in index2id:
if len(index2id[index]) > 1:
collision_item_groups.append(index2id[index])
return collision_item_groups
def parse_args():
parser = argparse.ArgumentParser(description = "Index")
parser.add_argument("--item_model_path", type = str, default = "", help = "")
parser.add_argument("--item_data_path", type = str, default = "", help = "")
parser.add_argument("--user_model_path", type = str, default = "", help = "")
parser.add_argument("--user_data_path", type = str, default = "", help = "")
# parser.add_argument("--save_path", type = str, default = "", help = "")
parser.add_argument("--device", type = str, default = "cuda:0", help = "gpu or cpu")
return parser.parse_args()
generate_args = parse_args()
print(generate_args)
device = torch.device(generate_args.device)
# generate item index
ckpt = torch.load(os.path.join(generate_args.item_model_path, 'best_collision_model.pth'), map_location = torch.device('cpu'))
args = ckpt['args']
state_dict = ckpt['state_dict']
data = NpyDataset(generate_args.item_data_path)
data_loader = DataLoader(data, num_workers = args.num_workers, batch_size = 64, shuffle = False, pin_memory = True)
# model = RQVAE(
# in_dim = data.dim,
# num_emb_list = args.num_emb_list,
# e_dim = args.e_dim,
# layers = args.layers,
# dropout_prob = args.dropout_prob,
# bn = args.bn,
# loss_type = args.loss_type,
# quant_loss_weight = args.quant_loss_weight,
# kmeans_init = args.kmeans_init,
# kmeans_iters = args.kmeans_iters,
# sk_epsilons = args.sk_epsilons,
# sk_iters = args.sk_iters,
# )
# model.load_state_dict(state_dict)
# model = model.to(device)
# model.eval()
# print(model)
prefix = ["<a_{}>","<b_{}>","<c_{}>","<d_{}>","<e_{}>"]
postfix = "<p_{}>"
index_table = {}
all_indices = []
all_indices_str = []
with torch.no_grad():
for x in tqdm(data_loader):
# indices = model.get_indices(x.to(device), False)
# indices = indices.view(-1, indices.shape[-1]).cpu().numpy()
indices = np.random.randint(0, 256, size = (64, 4), dtype = int)
for index in indices:
code = []
for i, ind in enumerate(index):
code.append(prefix[i].format(int(ind)))
if str(code) in index_table:
index_table[str(code)] += 1
else:
index_table[str(code)] = 0
code.append(postfix.format(index_table[str(code)]))
all_indices.append(code)
all_indices_str.append(str(code))
all_indices = np.array(all_indices)
all_indices_str = np.array(all_indices_str)
print("All indices number: ", len(all_indices))
print("Max number of conflicts: ", max(get_indices_count(all_indices_str).values()))
print('Re-index number:', max(index_table.values()))
all_indices_dict = {}
for item, indices in enumerate(all_indices.tolist()):
all_indices_dict[item] = list(indices)
reindex_dict = {'reindex': max(index_table.values())}
item_index_path = os.path.join(generate_args.item_model_path, 'indices.random.item.json')
with open(item_index_path, 'w', encoding = 'utf-8') as f:
json.dump(all_indices_dict, f)
item_reindex_path = os.path.join(generate_args.item_model_path, 'reindex.random.item.json')
with open(item_reindex_path, 'w', encoding = 'utf-8') as f:
json.dump(reindex_dict, f)
# generate user index
ckpt = torch.load(os.path.join(generate_args.user_model_path, 'best_collision_model.pth'), map_location = torch.device('cpu'))
args = ckpt['args']
state_dict = ckpt['state_dict']
data = NpyDataset(generate_args.user_data_path)
data_loader = DataLoader(data, num_workers = args.num_workers, batch_size = 64, shuffle = False, pin_memory = True)
# model = RQVAE(
# in_dim = data.dim,
# num_emb_list = args.num_emb_list,
# e_dim = args.e_dim,
# layers = args.layers,
# dropout_prob = args.dropout_prob,
# bn = args.bn,
# loss_type = args.loss_type,
# quant_loss_weight = args.quant_loss_weight,
# kmeans_init = args.kmeans_init,
# kmeans_iters = args.kmeans_iters,
# sk_epsilons = args.sk_epsilons,
# sk_iters = args.sk_iters,
# )
# model.load_state_dict(state_dict)
# model = model.to(device)
# model.eval()
# print(model)
prefix = ['<z-{}>','<y-{}>','<x-{}>','<w-{}>','<v-{}>']
all_indices = []
all_indices_str = []
with torch.no_grad():
for x in tqdm(data_loader):
# indices = rqvae.get_indices(x.to(device), False)
# indices = indices.view(-1, indices.shape[-1]).cpu().numpy()
indices = np.random.randint(0, 256, size = (64, 4), dtype = int)
for index in indices:
code = []
for i, ind in enumerate(index):
code.append(prefix[i].format(int(ind)))
all_indices.append(code)
all_indices_str.append(str(code))
all_indices = np.array(all_indices)
all_indices_str = np.array(all_indices_str)
print("All indices number: ", len(all_indices))
print("Max number of conflicts: ", max(get_indices_count(all_indices_str).values()))
all_indices_dict = {}
for item, indices in enumerate(all_indices.tolist()):
all_indices_dict[item] = list(indices)
json_path = os.path.join(generate_args.user_model_path, 'indices.random.user.json')
with open(json_path, 'w', encoding = 'utf-8') as f:
json.dump(all_indices_dict, f) |