def mean_ap(distmat, query_ids=None, gallery_ids=None, query_cams=None, gallery_cams=None): distmat = to_numpy(distmat) m, n = distmat.shape # Fill up default values if query_ids is None: query_ids = np.arange(m) if gallery_ids is None: gallery_ids = np.arange(n) if query_cams is None: query_cams = np.zeros(m).astype(np.int32) if gallery_cams is None: gallery_cams = np.ones(n).astype(np.int32) # Ensure numpy array query_ids = np.asarray(query_ids) gallery_ids = np.asarray(gallery_ids) query_cams = np.asarray(query_cams) gallery_cams = np.asarray(gallery_cams) # Sort and find correct matches indices = np.argsort(distmat, axis=1) matches = (gallery_ids[indices] == query_ids[:, np.newaxis]) # Compute AP for each query aps = [] for i in range(m): # Filter out the same id and same camera valid = ((gallery_ids[indices[i]] != query_ids[i]) | (gallery_cams[indices[i]] != query_cams[i])) y_true = matches[i, valid] y_score = -distmat[i][indices[i]][valid] if not np.any(y_true): continue aps.append(average_precision_score(y_true, y_score)) if len(aps) == 0: raise RuntimeError("No valid query") return np.mean(aps)
def __init__(self, data, style=None, extra=None, debug=False, order=None): """Storage for raw data order is a list into the base array's data; each item in the list is an index of the base array. E.g. if the base array is the 20 element list containing the data [100, 101, ... 119] and the order is [10, 0, 5, 2], the segment data used is [110, 100, 105, 102] """ self.order = order self.is_indexed = order is not None if self.is_indexed: self.data = OrderWrapper(data, order) else: self.data = to_numpy(data) if style is None: if debug: self.style = np.arange(len(self), dtype=np.uint8) else: self.style = np.zeros(len(self), dtype=np.uint8) else: if self.is_indexed: self.style = OrderWrapper(style, order) else: self.style = style if extra is None: extra = UserExtraData() self.extra = extra self.reverse_index_mapping = None
def main(): parser = argparse.ArgumentParser() parser.add_argument('-n', '--top_n', type=int, required=False, default=TOP_N, help='Number of documents to use.') FLAGS, _ = parser.parse_known_args() top_n = FLAGS.top_n train_dataset = utils.read_dataset(TRAIN_DATA_PATH, top_n) train_dataset = utils.augment_with_permutations(train_dataset) train_data, train_labels = utils.to_numpy(train_dataset, top_n) del train_dataset val_dataset = utils.read_dataset(VAL_DATA_PATH, top_n) val_data, val_labels = utils.to_numpy(val_dataset, top_n) del val_dataset model = arch.get_model(top_n) model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['acc']) model.summary() filepath = os.path.join( MODELS_DIR, "model-" + str(uuid.uuid4()) + "-{epoch:04d}-{val_loss:.4f}-{val_acc:.4f}.hdf5") save_model = ModelCheckpoint(filepath, monitor='val_acc', verbose=0, save_best_only=False, mode='max') model.fit(train_data, train_labels, validation_data=(val_data, val_labels), batch_size=128, epochs=75, verbose=1, callbacks=[save_model])
def NMI(X, ground_truth, n_cluster=3): X = [to_numpy(x) for x in X] # list to numpy X = np.array(X) ground_truth = np.array(ground_truth) # print('x_type:', type(X)) # print('label_type:', type(ground_truth)) kmeans = KMeans(n_clusters=n_cluster, n_jobs=-1, random_state=0).fit(X) print('K-means done') nmi = normalized_mutual_info_score(ground_truth, kmeans.labels_) return nmi
def test(): sim_mat = torch.rand(int(7e2), int(14e2)) sim_mat = to_numpy(sim_mat) query_ids = int(1e2) * list(range(7)) gallery_ids = int(2e2) * list(range(7)) gallery_ids = np.asarray(gallery_ids) query_ids = np.asarray(query_ids) print( Recall_at_ks(sim_mat, query_ids=query_ids, gallery_ids=gallery_ids, data='shop'))
def svm_topk_smooth_py_2(x, y, tau, k): x, y = to_numpy(x), to_numpy(y) n_samples, n_classes = x.shape exp = np.exp(x * 1. / (k * tau)) term_1 = np.zeros(n_samples) for i in range(n_samples): all_but_y = [j for j in range(n_classes) if j != y[i]] for indices in itertools.combinations(all_but_y, k - 1): term_1[i] += np.product(exp[i, indices]) term_2 = np.zeros(n_samples) for i in range(n_samples): all_but_y = [j for j in range(n_classes) if j != y[i]] for indices in itertools.combinations(all_but_y, k): term_2[i] += np.product(exp[i, indices]) all_ = np.arange(n_samples) loss = tau * (np.log(term_1 * exp[all_, y] + np.exp(1. / tau) * term_2) - np.log(term_1 * exp[all_, y])) return loss
def svm_topk_smooth_py_1(x, y, tau, k): x, y = to_numpy(x), to_numpy(y) x = x.astype(np.float128) tau = float(tau) n_samples, n_classes = x.shape exp = np.exp(x * 1. / (k * tau)) term_1 = np.zeros(n_samples) for indices in itertools.combinations(range(n_classes), k): delta = 1. - np.sum(indices == y[:, None], axis=1) term_1 += np.product(exp[:, indices], axis=1) * np.exp(delta / tau) term_2 = np.zeros(n_samples) for i in range(n_samples): all_but_y = [j for j in range(n_classes) if j != y[i]] for indices in itertools.combinations(all_but_y, k - 1): term_2[i] += np.product(exp[i, indices]) * exp[i, y[i]] loss = tau * (np.log(term_1) - np.log(term_2)) return loss
def changer(): input_dir = "../../assets/mnist_png/mnist_png/testing/All_resized8x8/" output_dir = "../../assets/mnist_png/mnist_png/testing/All_resized8x8/" for _ in range(100): idx = np.random.randint(10000) inp_img_path = input_dir + "{:}.png".format(idx) img = Image.open(inp_img_path) img_np = utils.to_numpy(img) img_np[:, :] = 0 img_new = utils.to_img(img_np) out_img_path = output_dir + "{:}.png".format(idx) img_new.save(out_img_path, "PNG")
def test_trained_model(args, model, testLoader, target_names, log_file_heading_msg): model.to(args.device) model.eval() all_preds, all_labels = ( [], []) # To store the all the predictions and true lables as a list with torch.no_grad(): for images, labels in testLoader: images, labels = images.to(args.device), labels.to(args.device) predictions = model(images) # Calculating predictions and true labels of a batch # Appending them into a list after converting into numpy arrays # so that we can have `a list of predicitons` and `a list of true labels` for all the test samples # we will calvulate accuracy, F-1, classificaiton reoprt, Confusion matrix by using `a list of predicitons` and `a list of true labels` _, pred = predictions.topk(1, 1, True, True) pred = to_numpy(pred.t()) pred = np.reshape(pred, -1) labels = to_numpy(labels) all_preds.extend(pred) all_labels.extend(labels) # Accuracy, F-1, Confusion Matrix, class-wise classification report on test dataset test_accuracy = accuracy_score(all_labels, all_preds) test_f1 = f1_score(all_labels, all_preds, average='macro', zero_division=0) test_classificaiton_report = classification_report( all_labels, all_preds, target_names=target_names, zero_division=0) test_confusion_matrix = confusion_matrix(all_labels, all_preds) # Logging all the test results in a txt file logger_for_test.info(log_file_heading_msg) logger_for_test.info(f'Accuracy: {test_accuracy*100:.2f}%') logger_for_test.info(f'\nF-1 Score: {test_f1*100:.2f}%') logger_for_test.info( f'\nClasswise report:\n {test_classificaiton_report}') logger_for_test.info(f'\nConfusion Matrix: \n {test_confusion_matrix}')
def train(train_loader, model, proxies, criterion, optimizer, epoch, scheduler): """Training loop for one epoch""" batch_time = AverageMeter() data_time = AverageMeter() val_loss = AverageMeter() val_acc = AverageMeter() # switch to train mode model.train() end = time.time() for i, (x, y) in enumerate(train_loader): # measure data loading time data_time.update(time.time() - end) if len(x.shape) == 5: batch_size, nviews = x.shape[0], x.shape[1] x = x.view(batch_size * nviews, 3, 224, 224) if len(y) == args.batch_size: if args.cuda: x = x.cuda() y = y.cuda() x = Variable(x) # embed x_emb = model(x) loss, acc = criterion(x_emb, y, proxies) val_loss.update(to_numpy(loss), x.size(0)) val_acc.update(acc, x.size(0)) # compute gradient and do SGD step optimizer.zero_grad() loss.backward() optimizer.step() scheduler.step() # measure elapsed time batch_time.update(time.time() - end) end = time.time() txt = ('Epoch [%d] (Time %.2f Data %.2f):\t' 'Loss %.4f\t Acc %.4f' % (epoch, batch_time.avg * i, data_time.avg * i, val_loss.avg, val_acc.avg * 100.)) print(txt) write_logs(txt)
def forward(self, input_labels): if self.variable_lengths: input_lengths = (input_labels != 0).sum(1) # make ixs input_lengths_list = to_numpy(input_lengths).tolist() sorted_input_lengths_list = np.sort( input_lengths_list)[::-1].tolist() max_length = sorted_input_lengths_list[0] sort_ixs = np.argsort(input_lengths_list)[::-1].tolist() s2r = {s: r for r, s in enumerate(sort_ixs)} recover_ixs = [s2r[s] for s in range(len(input_lengths_list))] # move to long tensor sort_ixs = input_labels.data.new(sort_ixs).long().cuda() recover_ixs = input_labels.data.new(recover_ixs).long().cuda() # sort input_labels by descending order input_labels = input_labels[sort_ixs, 0:max_length].long().cuda() assert max(input_lengths_list) == input_labels.size(1) # embed embedded = self.embedding(input_labels) embedded = self.input_dropout(embedded) if self.variable_lengths: embedded = nn.utils.rnn.pack_padded_sequence( embedded, sorted_input_lengths_list, batch_first=True) # forward rnn output, hidden = self.rnn(embedded) # recover if self.variable_lengths: # embedded (batch, seq_len, word_embedding_size) embedded, _ = nn.utils.rnn.pad_packed_sequence(embedded, batch_first=True) embedded = embedded[recover_ixs] # recover rnn output, _ = nn.utils.rnn.pad_packed_sequence( output, batch_first=True) # (batch, max_len, hidden) output = output[recover_ixs] # recover hidden if self.rnn_type == 'lstm': hidden = hidden[0] hidden = hidden[:, recover_ixs, :] hidden = hidden.transpose(0, 1).contiguous() hidden = hidden.view(hidden.size(0), -1) return output, hidden, embedded, max_length
def visualize_rgb(images): """Visualize RGB modality.""" images = utils.to_numpy(images) mean = np.array([0.485, 0.456, 0.406]) std = np.array([0.229, 0.224, 0.225]) images = np.moveaxis(images, -3, -1) images = images * std + mean images = np.clip(images * 255, 0, 255) images = images[..., ::-1].astype(np.uint8) images = images[0, 0] # subsample imgproc.save_avi('/home/luoa/research/rgb.avi', images)
def __init__(self, src, tgt, map_src=None, map_tgt=None, k=10, gpu_device=-1): """ inputs: :param src (np.ndarray) : the source np.ndarray object :param tgt (np.ndarray) : the target np.ndarray object :param map_src (linear layer) : the Linear Layer for mapping the source (if applicable) :param map_tgt (linear layer) : the Linear Layer for mapping the target (if applicable) :param k (int) : the number of nearest neighbours to use (default: 10, as in paper) """ # assert type(src) is np.ndarray # assert type(tgt) is np.ndarray if map_src is None: self.src = to_numpy(normalize(Variable(torch.Tensor(src))), gpu_device >= 0) else: # self.src = to_numpy(normalize(src.mm(W_src)), gpu_device >= 0) self.src = to_numpy(normalize(map_src(Variable(src))), gpu_device >= 0) if map_tgt is None: self.tgt = to_numpy(normalize(Variable(torch.Tensor(tgt))), gpu_device >= 0) else: self.tgt = to_numpy(normalize(self.map_tgt(Variable(tgt))), gpu_device >= 0) self.k = k self.gpu_device = gpu_device self.r_src = get_mean_similarity(self.src, self.tgt, self.k, self.gpu_device) self.r_tgt = get_mean_similarity(self.tgt, self.src, self.k, self.gpu_device)
def predict_ensemble(loader, models, device, batch_size=1): preds = {} metricss = [] for i, sample in tqdm(enumerate(loader, 1)): # Take variable and put them to GPU (irm, mask, patient) = sample irm = to_var(irm.float(), device) mask = to_numpy(mask) pred_masks = np.zeros_like(mask) for p in range(len(models)): model = models[p] irm_ = irm[:, p, :, :, :].view(1, 1, 80, 128, 128) pred_mask = to_numpy(model(irm_)) pred_masks += pred_mask ref = np.argmax(mask[0], axis=0) mask_out = np.argmax(pred_masks[0], axis=0) met = evalAllmetric(ref, mask_out) metricss.append(met) return preds, metricss
def _get_view(self, tf): allow = ["wall", "key", "ball", "box", ">", "<", "^", "V"] allow = {k: v + 1 for v, k in enumerate(allow)} view, vis = self.gen_obs_grid() view = to_numpy(view, allow, None, vis) view = np.expand_dims(view, 0) if tf: agent = np.zeros_like(view[0]) agent[self.agent_view_size - 1][int(self.agent_view_size / 2)] = allow[agent_dir[3]] agent = np.expand_dims(agent, 0) view = np.concatenate([view, agent], axis=0) return view
def select_action(self, s_t, episode): # assert episode >= self.warmup, 'Episode: {} warmup: {}'.format(episode, self.warmup) action = to_numpy(self.actor(to_tensor(np.array(s_t).reshape( 1, -1)))).squeeze(0) delta = self.init_delta * (self.delta_decay**(episode - self.warmup)) # action += self.is_training * max(self.epsilon, 0) * self.random_process.sample() action = self.sample_from_truncated_normal_distribution( lower=self.lbound, upper=self.rbound, mu=action, sigma=delta) action = np.clip(action, self.lbound, self.rbound) # self.a_t = action return action
def Recall_at_ks(sim_mat, k_s=None, query_ids=None, gallery_ids=None): # start_time = time.time() # print(start_time) """ :param sim_mat: :param query_ids :param gallery_ids Compute [R@1, R@2, R@4, R@8] """ if k_s is None: k_s = [1, 2, 4, 8] sim_mat = to_numpy(sim_mat) m, n = sim_mat.shape gallery_ids = np.asarray(gallery_ids) if query_ids is None: query_ids = gallery_ids else: query_ids = np.asarray(query_ids) num_max = int(1e6) if m > num_max: samples = list(range(m)) random.shuffle(samples) samples = samples[:num_max] sim_mat = sim_mat[samples, :] query_ids = [query_ids[k] for k in samples] m = num_max # Hope to be much faster yes!! num_valid = np.zeros(len(k_s)) neg_nums = np.zeros(m) for i in range(m): x = sim_mat[i] pos_max = np.max(x[gallery_ids == query_ids[i]]) neg_num = np.sum(x > pos_max) neg_nums[i] = neg_num for i, k in enumerate(k_s): if i == 0: temp = np.sum(neg_nums < k) num_valid[i:] += temp else: temp = np.sum(neg_nums < k) num_valid[i:] += temp - num_valid[i - 1] # t = time.time() - start_time # print(t) return num_valid / float(m)
def get_closest_csls_matches(self, source_indices, n, mode="csls"): """ Gets the n closest matches of the elements located at the source indices in the target embedding. Returns: indices of closest matches and the mean CSLS of all these matches. This function maps the indices internally. inputs: :param source_indices (np.ndarray) : the source indices (in the source domain) :param n (int) : the number of closest matches to obtain """ logger.info("Using Mode: {0}".format(mode)) tgt_tensor = to_cuda(torch.Tensor(self.tgt), int(self.gpu_device[-1])).t() src_tensor = torch.Tensor(self.map_to_tgt(source_indices)) r_src_tensor = to_cuda(torch.Tensor(self.r_src[source_indices, np.newaxis]), int(self.gpu_device[-1])) r_tgt_tensor = to_cuda(torch.Tensor(self.r_tgt[np.newaxis, ...]), int(self.gpu_device[-1])) batched_list = [] batched_list_idx = [] batch_size = 512 for i in range(0, src_tensor.shape[0], batch_size): src_tensor_indexed = to_cuda(src_tensor[i: i + batch_size], int(self.gpu_device[-1])) r_src_tensor_indexed = r_src_tensor[i: i + batch_size] if mode == "nn": batch_scores = src_tensor_indexed.mm(tgt_tensor) elif mode == "csls": batch_scores = (2 * src_tensor_indexed.mm(tgt_tensor)) - r_src_tensor_indexed - r_tgt_tensor elif mode == "cdm": mu_x = torch.sqrt(1. - r_src_tensor_indexed) mu_y = torch.sqrt(1. - r_tgt_tensor) dxy = 1. - src_tensor_indexed.mm(tgt_tensor) eps = 1e-3 batch_scores = -dxy / (mu_x + mu_y + eps) else: raise NotImplementedError("{0} not implemented yet".format(mode)) best_scores, best_ix = batch_scores.topk(n) batched_list.append(best_scores) batched_list_idx.append(best_ix) return to_numpy(torch.cat(batched_list_idx, 0), self.gpu_device), to_numpy(torch.cat(batched_list, 0), self.gpu_device)
def __init__(self, devices_to_use, model_dir, data_dir, results_dir, model_config, schedule_config, training_config, continue_training, use_mixed_precision, use_xla, show_mode, show_steps=None): super().__init__(devices_to_use, model_dir, data_dir, results_dir, model_config, schedule_config, use_mixed_precision, use_xla, show_mode, show_steps) if continue_training: self.continue_num = self._get_last_ckpt_num() else: self.continue_num = None self._get_training_objects(**training_config) restored = self.try_restore_state() if not restored: self.moving_avg_weights = self.model.get_weights() else: self.moving_avg_weights = self.ema_model.get_weights() self.i = int(to_numpy(self.optimizer.iterations)) self.starttime = time()
def rcsls(X_src, Y_tgt, Z_src, Z_tgt, R, knn=10): X_trans = torch.mm(X_src, R.t()) f = 2 * torch.sum(X_trans * Y_tgt) df = 2 * torch.mm(Y_tgt.t(), X_src) fk0, dfk0 = getknn(torch.mm(X_trans, Z_tgt.t()), X_src, Z_tgt, knn) obj1 = torch.mm(Z_src, R.t()) obj2 = Y_tgt.t() # CUDA memory error when multiplying these 200000x300 and 300x200000 # so converting to np # obj_fin = torch.mm(obj1, obj2).t() obj1 = to_numpy(obj1, gpuid >= 0) obj2 = to_numpy(obj2, gpuid >= 0) obj_fin = np.dot(obj1, obj2).T obj_fin = to_cuda(torch.Tensor(obj_fin), gpuid) fk1, dfk1 = getknn(obj_fin, Y_tgt, Z_src) f = f - fk0 - fk1 df = df - dfk0 - dfk1.t() return -f / X_src.shape[0], -df / X_src.shape[0]
def RecPostProcess(output, target, score, dataset=None): pred_list, targ_list = get_str_list(output, target, dataset) max_len_labels = output.size(1) score_list = [] score = to_numpy(score) for i, pred in enumerate(pred_list): len_pred = len(pred) + 1 # eos should be included len_pred = min( max_len_labels, len_pred) # maybe the predicted string don't include a eos. score_i = score[i, :len_pred] score_i = math.exp(sum(map(math.log, score_i))) score_list.append(score_i) return pred_list, targ_list, score_list
def mean_average_precision(sim_mat, img_name, data='cub', query_ids=None, gallery_ids=None): """Compute the Mean Average Precision. --------------------------------------------------- Inputs: distmat : numpy.ndarray The distance matrix. ``distmat[i, j]`` is the distance between i-th probe sample and j-th gallery sample. glabels : numpy.ndarray or None, optional plabels : numpy.ndarray or None, optional --------------------------------------------------- Outputs: out : numpy.ndarray, The MAP result --------------------------------------------------- """ rank = 1 - sim_mat # because we will sort its distance according to similarity in sim_mat show_retrieval_results = True rank = np.argsort(rank, axis=1) rank = to_numpy(rank) gallery_ids = np.asarray(gallery_ids) query_ids = np.asarray(query_ids) n_probe, n_gallery = rank.shape average_precision = 1.0 * np.zeros_like(query_ids) for i in range(n_probe): relevant_size = np.sum(gallery_ids == query_ids[i]) hit_index = np.where(gallery_ids[rank[i, :]] == query_ids[i]) # if show_retrieval_results: # print('Query image is', img_name[query_ids[i]]) # relevant_size = hit_index[0].shape[0] # retrieved_img = [img_name[hit_index[0][k]] for k in range(relevant_size)] # print('retrieved image are: ', retrieved_img) # exit() precision = 1.0 * np.zeros_like(hit_index[0]) assert relevant_size == hit_index[0].shape[0] for j in range(relevant_size): hitid = np.max(hit_index[0][j]) precision[j] = np.sum(gallery_ids[rank[i, :hitid]] == query_ids[i]) * 1.0 / (hit_index[0][j] + 1) average_precision[i] = np.sum(precision) * 1.0 / relevant_size score = np.mean(average_precision) return score
def dpp(self, critic, actor, state): all_q = [critic((state), actor((state)))] state = utils.to_numpy(state) mid_index = 180 / self.args.angle_res coupling = self.args.coupling # dpp_sweep = [mid_index + i for i in range(int(-coupling/2), int(-coupling/2) + coupling, 1)] dpp_sweep = random.sample(range(360 / self.args.angle_res), coupling + 1) for i in dpp_sweep: states[i, :] += 2.0 vals = torch.cat((vals, net.critic_forward(to_tensor(states))), 0) return torch.max(vals, 0)[0].unsqueeze(0)
def Recall_at_ks_products(sim_mat, query_ids=None, gallery_ids=None): """ :param sim_mat: :param query_ids :param gallery_ids for the Deep Metric problem, following the evaluation table of Proxy NCA loss only compute the [R@1, R@10, R@100] fast computation via heapq """ sim_mat = to_numpy(sim_mat) m, n = sim_mat.shape num_max = int(1e4) # Fill up default values gallery_ids = np.asarray(gallery_ids) if query_ids is None: query_ids = np.arange(m) if gallery_ids is None: gallery_ids = np.arange(n) # Ensure numpy array if m > num_max: samples = list(range(m)) random.shuffle(samples) samples = samples[:num_max] sim_mat = sim_mat[samples, :] query_ids = [query_ids[k] for k in samples] m = num_max else: query_ids = np.asarray(query_ids) # Sort and find correct matches # indice = np.argsort(sim_mat, axis=1) num_valid = np.zeros(4) for i in range(m): x = sim_mat[i] indice = heapq.nlargest(1000, range(len(x)), x.take) if query_ids[i] == gallery_ids[indice[0]]: num_valid += 1 elif query_ids[i] in gallery_ids[indice[1:10]]: num_valid[1:] += 1 elif query_ids[i] in gallery_ids[indice[10:100]]: num_valid[2:] += 1 elif query_ids[i] in gallery_ids[indice[100:]]: num_valid[3] += 1 return num_valid / float(m)
def dpp(self, critic, actor, state): all_q = [critic((state), actor((state)))] state = utils.to_numpy(state) mid_index = 180 / self.args.angle_resolution coupling = self.args.coupling # dpp_sweep = [mid_index + i for i in range(int(-coupling/2), int(-coupling/2) + coupling, 1)] dpp_sweep = random.sample(range(360 / self.args.angle_resolution), coupling + 1) for i, add_index in enumerate(dpp_sweep): state[:, add_index] += 2.0 shaped_state = utils.to_tensor(state) all_q.append( critic((shaped_state), actor((shaped_state))) / (i + 2)) all_q = torch.cat(all_q, 1) return torch.max(all_q, 1)[0].unsqueeze(1)
def __init__( self, input_img, small_imgs_assets_path="../../assets/mnist_png/mnist_png/testing/All_resized8x8/", small_imgs_num=10000, format_str=None): self.input_img = utils.to_numpy(input_img) self.progress_imgs = [] # Parameters to be tuned self.num_iterations = 100 self.population_size = 50 self.selection_percentage = 0.2 self.crossover_percentage = (0.5, 0.5 ) # ((row, col) in case of two parents self.crossover_num_parents = 2 # Not implemented for now for crossover with multiple parent self.mutation_probability = 0.1 self.hybrid_crossover_ratio = { "uniform": 0, "parts": 0.5, "greedy": 0.5 } self.termination_threshold = 800 # To be chosen, the threshold that the fitness score is acceptable self.img_size = (512, 512) self.imgs = utils.read_small_imgs(assets_dir=small_imgs_assets_path, num=small_imgs_num, format_str=format_str) self.imgs = np.array(self.imgs) self.small_img_shape = self.imgs.shape[1:] # Configurations for the small images self.max_index_imgs = small_imgs_num self.small_imgs_num_rc = tuple([ self.img_size[0] // self.small_img_shape[0], self.img_size[1] // self.small_img_shape[1] ]) self.current_population = self._generate_population()
def main(args): device = "cpu" if args.no_cuda else "cuda" start_t = time.time() generator = Generator().to(device) end_t = time.time() print("init time : {}".format(end_t - start_t)) start_t = time.time() pretrain_model = flow.load(args.model_path) generator.load_state_dict(pretrain_model) end_t = time.time() print("load params time : {}".format(end_t - start_t)) generator.eval() start_t = time.time() z = to_tensor(np.random.normal(0, 1, size=(args.batch_size, 100)), False).to(device) predictions = to_numpy(generator(z), False) end_t = time.time() print("infer time : {}".format(end_t - start_t)) save_images(predictions, args.batch_size, args.save_path)
def mean_ap(distmat, query_ids=None, gallery_ids=None, query_cams=None, gallery_cams=None): distmat = to_numpy(distmat) # <class 'tuple'>: (100, 100) m, n = distmat.shape # Fill up default values if query_ids is None: query_ids = np.arange(m) if gallery_ids is None: gallery_ids = np.arange(n) if query_cams is None: query_cams = np.zeros(m).astype(np.int32) if gallery_cams is None: gallery_cams = np.ones(n).astype(np.int32) # Ensure numpy array query_ids = np.asarray(query_ids) gallery_ids = np.asarray(gallery_ids) query_cams = np.asarray(query_cams) gallery_cams = np.asarray(gallery_cams) # Sort and find correct matches indices = np.argsort(distmat, axis=1) matches = (gallery_ids[indices] == query_ids[:, np.newaxis]) # Compute AP for each query aps = [] for i in range(m): # Filter out the same id and same camera valid = ((gallery_ids[indices[i]] != query_ids[i]) | (gallery_cams[indices[i]] != query_cams[i])) y_true = matches[i, valid] y_score = -distmat[i][indices[i]][valid] if not np.any(y_true): continue aps.append(average_precision_score(y_true, y_score)) if len(aps) == 0: raise RuntimeError("No valid query") return np.mean(aps)
def compute_csls_accuracy(x_src, x_tgt, lexicon, gpuid, lexicon_size=-1, k=10, bsz=1024): print("Computing csls cuda accuraccy") if lexicon_size < 0: lexicon_size = len(lexicon) idx_src = list(lexicon.keys()) assert gpuid >= 0 x_src /= torch.norm(x_src, p=2, dim=1).unsqueeze(-1) + 1e-8 x_tgt /= torch.norm(x_tgt, p=2, dim=1).unsqueeze(-1) + 1e-8 sr = x_src[list(idx_src)] sc = torch.mm(sr, x_tgt.t()) similarities = 2 * sc sc2 = to_cuda(torch.zeros(x_tgt.shape[0]), gpuid) for i in range(0, x_tgt.shape[0], bsz): j = min(i + bsz, x_tgt.shape[0]) sc_batch = torch.mm(x_tgt[i:j, :], x_src.t()) dotprod = torch.topk(sc_batch, k, dim=1)[0] sc2[i:j] = torch.mean(dotprod, dim=1) sc2 = sc2.unsqueeze(0) similarities -= sc2 _, nn = torch.max(similarities, dim=1) nn = to_numpy(nn, gpuid>=0).tolist() correct = 0.0 for k in range(0, len(lexicon)): if nn[k] in lexicon[idx_src[k]]: correct += 1.0 return correct / lexicon_size
is_larger_than = neighbor_val > pixel bitstring = ''.join([str(int(bit)) for bit in is_larger_than]) dec_value = int(bitstring, 2) # populate the histogram # index function should return the index of the bin # if the decimal is uniform # otherwise it returns TypeError try: bins = uniform_label.index(dec_value) hist[bins] += 1 except ValueError: # populate the 59 bin for non uniform hist[58] += 1 norm_hist = hist / sum(hist) hists[block, :] = norm_hist block += 1 return hists if __name__ == '__main__': im = Image.open('../data/YALE/centered/subject01.centerlight.pgm') im = utils.to_numpy(im) face_area = (15, 215, 15, 175) block_size = (40, 40) radius = 2 im = normalization.tan_triggs_norm(im, 0.2, 1, 4, 0.1, 10) lbp_uni = uniform_lbp2(im, radius, face_area, block_size)
# ------------------------------------------------------------------------ img = utils.read_img(inp_img_path) imgs = utils.read_small_imgs(assets_dir=assets_dir, num=small_imgs_num, format_str="{:05d}.png") # indexes = [] # for i in range(64): # indexes.append([np.random.randint(10000) for i in range(64)]) # imgt = utils.Image(imgs=imgs, index=indexes) # img = imgt.construct_img() # img = utils.to_img(img) # print(img.size) # utils.preview_img(img) # ------------------------------------------------------------------------ img_np = utils.to_numpy(img) time1 = time() out_img = generate_baseline_img(imgs, img_np) time2 = time() print("Process time: {:}".format(time2 - time1)) utils.preview_img(out_img, title="Baseline_img") utils.write_img(out_img_path, out_img)
def weighted_ind_neutral_bins_return_rate(factor_score, industry, weights, returns, mindex, n_bins=5, mname=''): """ Compute weighted value for stocks with top-k & bottom-k factor scores, all parameters should have corresponding rank of axis datetime & stock. Params: - factor_score: pd.DataFrame, dir: +, sample: STOCK_CODE DATE1 DATE2 DATE3 xxxxxx xxx xxx xxx xxxxxx xxx xxx xxx - weights: pd.DataFrame, sample: STOCK_CODE DATE1 DATE2 DATE3 xxxxxx xxx xxx xxx xxxxxx xxx xxx xxx - industry: pd.DataFrame, sample: STOCK_CODE INDUSTRY xxxxxx xxx xxxxxx xxx - returns: pd.DataFrame, sample: STOCK_CODE DATE1 DATE2 DATE3 xxxxxx xxx xxx xxx xxxxxx xxx xxx xxx - mindex: pd.DataFrame, sample: DATETIME INDEX xxxxx xxx xxxxx xxx """ dates = pd.to_datetime(mindex.DATETIME) if not mname: mname = mindex.columns[-1] factor_score = to_numpy(factor_score) weights = to_numpy(weights) returns = to_numpy(returns) n_returns = [[ 1., ] for _ in range(n_bins)] for date in range(factor_score.shape[1] - 2): mask = (weights[:, date + 1] > 0) factor_rank = sorted(np.arange(np.sum(mask)), key=lambda i: factor_score[mask, date + 1][i], reverse=True) industry_factor_rank = [ np.array(factor_rank)[i] for i in industry.loc[mask, :].groupby( by=['INDUSTRY']).indices.values() ] industry_k = [len(i) / n_bins for i in industry_factor_rank] for i in range(n_bins): cur = list() weight = list() for r, j in zip(industry_factor_rank, industry_k): cur.extend(r[range(int(np.floor(j * i)), int(np.ceil(j * (i + 1))))]) if j * (i + 1) > np.ceil(j * i + 1e-10): weight.append(np.ceil(j * i + 1e-10) - j * i) weight.extend([1] * (len( range(int(np.floor(j * i)), int(np.ceil(j * (i + 1))))) - 2)) weight.append(j * (i + 1) - np.floor(j * (i + 1) - 1e-10)) else: weight.append(j) n_returns[i].append( np.sum(returns[mask, date + 2][cur] * weights[mask, date + 1][cur] * np.array(weight)) / np.sum(returns[mask, date + 1][cur] * weights[mask, date + 1][cur] * np.array(weight)) * n_returns[i][-1]) for i in range(n_bins): plt.plot(dates, 100. * (np.array(n_returns[i]) - 1), label=f"{mname}第{i+1}高评分组合") plt.plot(dates, 100. * (mindex.values[:, 1] / mindex.values[0, 1] - 1.), label=f"{mname}") plt.xlabel('日期') plt.ylabel("区间累计回报(%)") plt.legend()
def weighted_top_factor_return_rate(factor_score, weights, returns, mindex, n_top=20, mname=''): """ Compute weighted value for stocks with top-k & bottom-k factor scores, all parameters should have corresponding rank of axis datetime & stock. Params: - factor_score: pd.DataFrame, dir: +, sample: STOCK_CODE DATE1 DATE2 DATE3 xxxxxx xxx xxx xxx xxxxxx xxx xxx xxx - weights: pd.DataFrame, sample: STOCK_CODE DATE1 DATE2 DATE3 xxxxxx xxx xxx xxx xxxxxx xxx xxx xxx - returns: pd.DataFrame, sample: STOCK_CODE DATE1 DATE2 DATE3 xxxxxx xxx xxx xxx xxxxxx xxx xxx xxx - mindex: pd.DataFrame, sample: DATETIME INDEX xxxxx xxx xxxxx xxx """ dates = pd.to_datetime(mindex.DATETIME) if not mname: mname = mindex.columns[-1] factor_score = to_numpy(factor_score) weights = to_numpy(weights) returns = to_numpy(returns) top_k_returns = [ 1., ] bot_k_returns = [ 1., ] for date in range(factor_score.shape[1] - 2): mask = (weights[:, date + 1] > 0) factor_rank = sorted(np.arange(np.sum(mask)), key=lambda i: factor_score[mask, date + 1][i], reverse=True) top_k = factor_rank[:n_top] bot_k = factor_rank[-n_top:] top_k_returns.append( np.sum(returns[mask, date + 2][top_k] * weights[mask, date + 1][top_k]) / np.sum(returns[mask, date + 1][top_k] * weights[mask, date + 1][top_k]) * top_k_returns[-1]) bot_k_returns.append( np.sum(returns[mask, date + 2][bot_k] * weights[mask, date + 1][bot_k]) / np.sum(returns[mask, date + 1][bot_k] * weights[mask, date + 1][bot_k]) * bot_k_returns[-1]) plt.plot(dates, 100. * (np.array(top_k_returns) - 1), label=f"{mname}高评分{n_top}组合") plt.plot(dates, 100. * (mindex.values[:, 1] / mindex.values[0, 1] - 1.), label=f"{mname}") plt.plot(dates, 100. * (np.array(bot_k_returns) - 1), label=f"{mname}低评分{n_top}组合") plt.xlabel('日期') plt.ylabel("区间累计回报(%)") plt.legend()
def cmc(distmat, query_ids=None, gallery_ids=None, query_cams=None, gallery_cams=None, topk=100, separate_camera_set=False, single_gallery_shot=False, first_match_break=False): distmat = to_numpy(distmat) m, n = distmat.shape # Fill up default values if query_ids is None: query_ids = np.arange(m) if gallery_ids is None: gallery_ids = np.arange(n) if query_cams is None: query_cams = np.zeros(m).astype(np.int32) if gallery_cams is None: gallery_cams = np.ones(n).astype(np.int32) # Ensure numpy array query_ids = np.asarray(query_ids) gallery_ids = np.asarray(gallery_ids) query_cams = np.asarray(query_cams) gallery_cams = np.asarray(gallery_cams) # Sort and find correct matches indices = np.argsort(distmat, axis=1) matches = (gallery_ids[indices] == query_ids[:, np.newaxis]) # Compute CMC for each query ret = np.zeros(topk) num_valid_queries = 0 for i in range(m): # Filter out the same id and same camera valid = ((gallery_ids[indices[i]] != query_ids[i]) | (gallery_cams[indices[i]] != query_cams[i])) if separate_camera_set: # Filter out samples from same camera valid &= (gallery_cams[indices[i]] != query_cams[i]) if not np.any(matches[i, valid]): continue if single_gallery_shot: repeat = 10 gids = gallery_ids[indices[i][valid]] inds = np.where(valid)[0] ids_dict = defaultdict(list) for j, x in zip(inds, gids): ids_dict[x].append(j) else: repeat = 1 for _ in range(repeat): if single_gallery_shot: # Randomly choose one instance for each id sampled = (valid & _unique_sample(ids_dict, len(valid))) index = np.nonzero(matches[i, sampled])[0] else: index = np.nonzero(matches[i, valid])[0] delta = 1. / (len(index) * repeat) for j, k in enumerate(index): if k - j >= topk: break if first_match_break: ret[k - j] += 1 break ret[k - j] += delta num_valid_queries += 1 if num_valid_queries == 0: raise RuntimeError("No valid query") return ret.cumsum() / num_valid_queries