def tsne_feature(X_features, y_label, use_cuda=True, save_fig=SAVE_NAME): print("Feature Shape: " + str(X_features.shape)) print("Label len: " + str(len(y_label))) # reshape feature and label # reshaped_feature = np.asarray(reshaped_feature, dtype=np.uint8) y_label = np.asarray(y_label, dtype=np.uint8) if USE_PCA: pca = PCA(n_components=NCOMPONENTS) X_features = pca.fit_transform(X_features) print("PCA DONE") if USE_CUDA: from tsnecuda import TSNE domain_shift_tsne = TSNE(n_components=2, perplexity=15, learning_rate=10).fit_transform(X_features) else: from sklearn.manifold import TSNE domain_shift_tsne = TSNE(random_state=RS).fit_transform(X_features) fashion_scatter(domain_shift_tsne, y_label) #fashion_scatter_3D(domain_shift_tsne, y_label) #angle = 3 #ani = animation.FuncAnimation(f, rotate, frames=np.arange(0, 360, angle), interval=50) #ani.save('inhadr_tsne1.gif', writer=animation.PillowWriter(fps=20)) plt.savefig('./images/' + save_fig + '.png', dpi=120) return
def apply_tsne(act, perplexity, n_iter): time_start = time.time() tsne = TSNE(n_components=2, verbose=1, perplexity=perplexity, n_iter=n_iter) tsne_results = tsne.fit_transform(act) # del act print(f"Time elapsed: {time.time() - time_start} seconds") return tsne_results
def compute_tsne_scenarios(): if PRECOMPUTED: x_subset, y_subset = load_dataset_precomputed() else: x_subset, y_subset = load_idda() if USE_PCA: #compute first pca pca = PCA(n_components=50) pca_result = pca.fit_transform(x_subset) print("PCA DONE") #compute than tsne domain_shift_tsne = TSNE(random_state=RS).fit_transform(pca_result) print("TSN DONE") else: # compute the tSNE if USE_CUDA: domain_shift_tsne = TSNE(n_components=2, perplexity=15, learning_rate=10).fit_transform(x_subset) else: domain_shift_tsne = TSNE(random_state=RS).fit_transform(x_subset) print("TSN DONE") # plot the tSNE computed fashion_scatter(domain_shift_tsne, y_subset) plt.savefig('./images/' + SAVE_NAME + '.png', dpi=120)
def display_latent_code( latent_code: np.ndarray, labels: np.ndarray, title: str, seed: int ) -> None: """ Plots the computed latent code representation for the features. Parameters ---------- latent_code: np.ndarray The latent code representation for features. labels: np.ndarray The labels for the dataset features. title: str The plot title to use. seed: int The pseudorandom seed to use for reproducible t-SNE visualization. """ tsne_encoder = TSNE(random_seed=seed, perplexity=50, learning_rate=10, n_iter=5000) latent_code = tsne_encoder.fit_transform(latent_code) sns.set_style("darkgrid") plt.scatter(latent_code[:, 0], latent_code[:, 1], c=labels, marker="o") plt.title(title) plt.grid() plt.savefig(fname=f"data/{title}.png", dpi=150) plt.show()
def compute_tsne(X, plot=True): tsne_model = TSNE(n_components=2, perplexity=40, learning_rate=100, verbose=10) tsne_Y = tsne_model.fit_transform(tsne_vectors) if plot: fig = plt.figure(figsize=(10, 10)) ax = fig.gca() ax.scatter(tsne_Y[:, 1], tsne_Y[:, 0], c=tsne_labels, s=1, cmap='hsv')
def tsne(lemniscate, perp, labels): if isinstance(perp, int): perp = [perp] else: perp = perp.split(',') for p in perp: x = lemniscate.memory x = x.data.cpu() x_embedded = TSNE(n_components=2, perplexity=p, learning_rate=10).fit_transform(x) x, y = [[] for i in range(2)] for c in x_embedded: x.append(c[0]) y.append(c[1]) plt.scatter(x, y) for idx in labels.keys(): label = labels[idx] lx = x[idx] ly = y[idx] plt.annotate(label, xy=(lx, ly), xytext=(-20, 20), arrowprops=dict(arrowstyle='->', connectionstyle='arc3,rad=0'), textcoords='offset points') plt.savefig('tsne-{}.png'.format(p)) plt.clf()
def do_tsne(options): options.shuffle = False options.batch_size = 2048 options.cuda_device = f"cuda:{get_gpu_ids()[0]}" # Load dataset into memory dataset = getattr(datasets, options.dataset)(options).test_set num = len(dataset) # Get the model model = get_model(options) proj_dim = options.projection_dim X = np.empty((num, proj_dim)) y = np.empty(num) # Get the dataloader loader = get_loader(dataset, options) idx = 0 for (batch, labels) in loader: num_batch = batch.shape[0] X[idx:idx + num_batch] = model(batch).cpu().numpy() y[idx:idx + num_batch] = labels.cpu().numpy() idx += num_batch X_new = TSNE().fit_transform(X) return (X_new, y)
def visualize(self, src_data_loader, tgt_data_loader, epoch): fig = plt.figure() features = [] labels = [] for data_loader in [src_data_loader, tgt_data_loader]: tmp_features = [] tmp_labels = [] for i, inputs in enumerate(data_loader): with torch.no_grad(): inputs, targets = self._parse_data(inputs) feature = self.model.extract_feature(inputs) tmp_features.append(feature) tmp_labels.append(targets) tmp_features = torch.cat(tmp_features, dim=0) tmp_labels = torch.cat(tmp_labels, dim=0) tmp_features = tmp_features.detach().cpu().numpy() tmp_labels = tmp_labels.detach().cpu().numpy() features.append(tmp_features) labels.append(tmp_labels) mid = features[0].shape[0] features = np.concatenate((features[0], features[1]), axis=0) feat_tsne = TSNE().fit_transform(features) plt.scatter(feat_tsne[:mid,0], feat_tsne[:mid,1], c=labels[0], alpha=0.4) #plt.scatter(feat_tsne[mid:,0], feat_tsne[mid:,1], c=labels[1]/2, alpha=0.5) save_path = osp.join(self.backup_path, str(epoch)+'.png') plt.savefig(save_path) self.writer.add_figure('visualization', fig,global_step=epoch) plt.clf()
def proj(X): Y = TSNE(n_components=2).fit_transform(X) # scaler = MinMaxScaler() # Y = scaler.fit_transform(Y) # Y = Y.tolist() # Y = np.round(Y, decimals=3).tolist() return Y
def write_tsne(label_dict, embedding, extension="png", tsne_model=None): """ base:https://medium.com/analytics-vidhya/super-fast-tsne-cuda-on-kaggle-b66dcdc4a5a4 """ if tsne_model is None: tsne_model = TSNE() x_embedding = tsne_model.fit_transform(embedding) for key in label_dict: label = label_dict[key] embedding_and_label = pd.concat([pd.DataFrame(x_embedding), pd.DataFrame(data=label,columns=["label"])], axis=1) sns.FacetGrid(embedding_and_label, hue="label", height=6).map(plt.scatter, 0, 1).add_legend() plt.savefig("{}.{}".format(key,extension)) plt.clf() plt.close('all')
def tsne(X, n_components, perplexity, learning_rate, N, gt_classes, dir_out, suffix, has_label): X_embedded = TSNE( n_components=n_components, perplexity=perplexity, learning_rate=learning_rate, ).fit_transform(X) if has_label: fig = plt.figure(figsize=(20, 15)) else: fig = plt.figure(figsize=(15, 15)) # cmap = plt.cm.jet # cmap = plt.cm.rainbow cmap = discrete_cmap(N, base_cmap='tab20') scat = plt.scatter( X_embedded[:, 0], X_embedded[:, 1], c=gt_classes[:], cmap=cmap, ) # bg_inds = np.where(gt_classes == 0)[0] # fg_inds = np.where(gt_classes > 0)[0] # plt.scatter( # X_embedded[bg_inds, 0], # X_embedded[bg_inds, 1], # c=gt_classes[bg_inds], # cmap=cmap, # ) if has_label: plt.colorbar(ticks=range(N)) plt.clim(-0.5, N - 0.5) plt.axis('off') plt.tick_params(axis='both', left='off', top='off', right='off', bottom='off', labelleft='off', labeltop='off', labelright='off', labelbottom='off') plt.tight_layout(pad=0) # now = strftime("%Y%m%d_%H%M%S") save_path = os.path.join( dir_out, '{}_{}_{}.png'.format( perplexity, learning_rate, suffix, )) plt.savefig(save_path, bbox_inches='tight', pad_inches=0) plt.close('all')
def write_tsne(file_name, embedding, ins_label, sem_label=None): """ base:https://medium.com/analytics-vidhya/super-fast-tsne-cuda-on-kaggle-b66dcdc4a5a4 """ x_embedding = TSNE().fit_transform(embedding) embedding_and_ins_label = pd.concat([pd.DataFrame(x_embedding), pd.DataFrame(data=ins_label,columns=["label"])], axis=1) sns.FacetGrid(embedding_and_ins_label, hue="label", height=6).map(plt.scatter, 0, 1).add_legend() plt.savefig(file_name) plt.close()
def tsne_vis(feature, feature_labels, vis_path): feature_np = feature.detach().cpu().numpy() feature_embeddings = TSNE().fit_transform(feature_np) vis_x = feature_embeddings[:, 0] vis_y = feature_embeddings[:, 1] plt.figure(figsize=(4, 3), dpi=160) plt.scatter(vis_x, vis_y, c=feature_labels, cmap=plt.cm.get_cmap("jet", 10), marker='.') plt.colorbar(ticks=range(10)) plt.clim(-0.5, 9.5) plt.savefig(vis_path)
def to_tsne(conv, labels, resultpath): plt.figure(figsize=(8, 8)) X_embedded = TSNE(n_components=2, perplexity=15, learning_rate=10, random_seed=0).fit_transform(conv) plt.scatter(X_embedded[:, 0], X_embedded[:, 1], c=labels, alpha=0.8) plt.colorbar() plt.title('TSNE Features and labels') plt.savefig(os.path.join(resultpath, 'tsne.png')) plt.close()
def do_tsne_on_episode(options): options.cuda_device = f"cuda:{get_gpu_ids()[0]}" episode_loader = SimpleShotEpisodes(options).episode_loader(options) model = get_model(options) idx = 0 for (batch, labels) in episode_loader: X = model(batch).cpu().numpy() y = labels.cpu().numpy() X_new = TSNE(perplexity=10).fit_transform(X) visualize(X_new, y, f"figs/fig{idx:03d}.png") idx += 1
def compute_tsne_2d_components(embedding_dict): embeddings = [] for name, path in embedding_dict.items(): if os.path.exists(path): embedding_pd = pd.read_parquet(path) vectors = np.array([np.array(v) for v in embedding_pd['vector'].to_numpy()]) vectors_tsne_results = TSNE(n_components=2, perplexity=40, learning_rate=10).fit_transform(vectors) embedding_pd = embedding_pd.join(pd.DataFrame(data=vectors_tsne_results, columns=['tsne-2d-one', 'tsne-2d-two'])) embeddings.append((name, embedding_pd)) else: LOGGER.error('embedding {name} does not exisit at {path}'.format(name=name, path=path)) return embeddings
def reduce_dimensions(model, perplexity): num_dimensions = 2 # final num dimensions (2D, 3D, etc) vectors = [] # positions in vector space labels = [] # keep track of words to label our data again later for word in model.wv.vocab: vectors.append(model.wv[word]) labels.append(word) # convert both lists into numpy vectors for reduction vectors = np.asarray(vectors) labels = np.asarray(labels) # reduce using t-SNE vectors = np.asarray(vectors) tsne = TSNE(n_components=num_dimensions, perplexity=perplexity) vectors = tsne.fit_transform(vectors) x_vals = [v[0] for v in vectors] y_vals = [v[1] for v in vectors] print(x_vals[:5]) return x_vals, y_vals, labels
def get_tsne(model, tsneloader, device): model.eval() feature_space = [] sample_weights = [] with torch.no_grad(): for image, target, file_labelweight in tsneloader: image, target = image.to(device), target.to(device) features = model.encoder(image) if features.size()[3] == 72: continue flattened_features = np.squeeze(features.cpu().numpy()).transpose( (1, 2, 0)) flattened_features = np.reshape( flattened_features, (np.prod(flattened_features.shape), )) feature_space.append(flattened_features) sample_weights.append(file_labelweight[1][0].item()) return TSNE(perplexity=100, learning_rate=300).fit_transform( np.array(feature_space)), sample_weights
def main(training_method, dataset_name, dataset_dir, gpu_id, seed): torch.manual_seed(seed) device = get_device(gpu_id) dataset = get_data(dataset_name, dataset_dir) features_dimension, classes_dimensions = dataset.num_features, dataset.num_classes data = dataset[0] data = data.to(device) model, optimizer = get_model_and_optimizer(training_method, features_dimension, classes_dimensions, device) best_validation_accuracy = best_test_accuracy = 0 for e in range(1, 251): train(model, optimizer, data) training_accuracy, validation_accuracy, test_accuracy = test( model, data) if validation_accuracy > best_validation_accuracy: best_validation_accuracy = validation_accuracy best_test_accuracy = test_accuracy print( f"Epoch: {e}, Train: {training_accuracy:.4f}, Val.: {validation_accuracy:.4f} " f"({best_validation_accuracy:.4f}), Test: {test_accuracy:.4f} ({best_test_accuracy:.4f})" ) embeddings = model.get_hidden_embeddings(data).detach().cpu().numpy() tsne_embeddings = TSNE(n_components=2, perplexity=20, learning_rate=100, n_iter=5000).fit_transform(embeddings) sb.set_style('white', {}) tsne_plot = sb.scatterplot(x=tsne_embeddings[:, 0], y=tsne_embeddings[:, 1], hue=data.y.cpu(), legend=False, edgecolor=None, palette=sb.color_palette("colorblind", n_colors=7)) tsne_plot.set(xticks=[], yticks=[]) tsne_plot = tsne_plot.get_figure() sb.despine(fig=tsne_plot, left=True, bottom=True) tsne_plot.savefig('/home/julienlaunay/tsne.png')
def plot_tsne(ax, curnpz, name, cluster_path='./data_clean/results/'): labels = np.load("{}{}/features_conv_v27.npz".format(cluster_path, name))['labels'][:, 1] conv = np.load("{}{}/features_conv_v27.npz".format(cluster_path, name))['feature_vector'] cal = calinski_harabasz_score(conv, labels) X_embedded = TSNE(n_components=2, perplexity=15, learning_rate=10, random_seed=0).fit_transform(conv) classes = np.unique(labels) cmap = plt.cm.get_cmap('brg', len(classes)) im = ax.scatter(X_embedded[:, 0], X_embedded[:, 1], c=labels, cmap=cmap) ax.figure.colorbar(im, ax=ax, ticks=classes) title = "Cluster audio, T-SNE representation" ax.set(title=title) space = int(len(conv) * 0.1) add_direction(ax, X_embedded[:, 0], X_embedded[:, 1], space, 5) return ax
def save_data(embeddings, labels, iteration=None, detected_labels=None): N_embedded = embeddings.shape[1] # num_dims = N_embedded # num_display_dims = 2 # tsne_lr = 20.0 # Get only the non-zero indexes idx_array = labels.nonzero() idx_tuple = idx_array.split(1, dim=1) labeled_pixels = labels[idx_tuple].squeeze(1) object_pixels = torch.gather(embeddings, 0, idx_array.repeat(1, N_embedded)) from tsnecuda import TSNE # from tsne import tsne X = object_pixels.cpu().detach().numpy() Y = TSNE(n_components=2, perplexity=40, learning_rate=50).fit_transform(X) fig = plt.figure() ax1 = fig.add_subplot(111) ax1.scatter(Y[:, 0], Y[:, 1], 5, labeled_pixels, cmap='tab20b') if iteration is None: iteration = 0 plt.savefig("low_dim_embeeding/embedded_%d.png" % iteration) plt.close(fig) if detected_labels is not None: detected_labels_pixels = detected_labels[idx_tuple].squeeze(1) fig = plt.figure() ax1 = fig.add_subplot(111) pylab.scatter(Y[:, 0], Y[:, 1], 5, detected_labels_pixels, cmap='tab20b') pylab.savefig("low_dim_embeeding/embedded_%d.png" % (iteration + 1)) pylab.close(fig)
embed_set = "embedding" embed_dir = "stored_data/embeddings" embed_dir = os.path.join(embed_dir, embed_name, "embeddings") create_folder(embed_dir) fig_dir = os.path.join(embed_dir, "figures") create_folder(fig_dir) df_emb, embeddings = calculate_embedding(test_dataset, emb_model, savedir=os.path.join(embed_dir, embed_set), concatenate="append") print(embeddings.mean()) print(embeddings.var()) embeddings = sklearn.preprocessing.StandardScaler().fit_transform(embeddings.reshape(embeddings.shape[0], -1)) print("normalized") print(embeddings.mean()) print(embeddings.var()) df_emb = df_emb.fillna("") tsne = TSNE() tsne_emb = tsne.fit_transform(X=embeddings.reshape(embeddings.shape[0], -1)) tsne_plots(tsne_emb, df_emb, savefig=os.path.join(fig_dir, embed_set)) scater_valid_rat = scatter_ratio(embeddings.reshape(embeddings.shape[0], -1), df_emb.reset_index()) silhouette_valid_score = sklearn.metrics.silhouette_score( embeddings.reshape(embeddings.shape[0], -1), df_emb.event_labels, metric='euclidean') LOG.info("Valid silhouette for all classes in 2D (tsne) : {}".format( sklearn.metrics.silhouette_score(df_emb[["X", "Y"]], df_emb.event_labels, metric='euclidean'))) embed_dir = "stored_data/embeddings" embed_dir = os.path.join(embed_dir, embed_name) create_folder(embed_dir) np.save(os.path.join(embed_dir, "embed" + str(epoch_model)), embeddings) test_fr.to_csv(os.path.join(embed_dir, "df" + str(epoch_model)), sep="\t", index=False)
embedding_pd = pd.read_parquet( '/data/research_ops/omops/omop_2019q4_embeddings/mce_visit/embeddings') embedding_pd.set_index('standard_concept_id', inplace=True) patient_event = spark.read.parquet( '/data/research_ops/omops/omop_2019q4_embeddings/visit/patient_event/') patient_event = patient_event \ .withColumn("lower_bound", F.unix_timestamp(F.date_add(F.from_unixtime(F.col('date'), 'yyyy-MM-dd'), -30), 'yyyy-MM-dd')) \ .withColumn("upper_bound", F.unix_timestamp(F.date_add(F.from_unixtime(F.col('date'), 'yyyy-MM-dd'), 30), 'yyyy-MM-dd')) patient_event.cache() data = visualize_time_lines(patient_event, 312327, 5000) data['sequence'] = [list(map(int, i.split())) for i in data['sequence']] sequences = data['sequence'].to_list() max_pooled_vector = np.asarray([max_pool(embedding_pd, s) for s in sequences]) vectors_tsne_results = TSNE(n_components=2, perplexity=40, learning_rate=10).fit_transform(max_pooled_vector) data = data.join( pd.DataFrame(data=vectors_tsne_results, columns=['tsne-2d-one', 'tsne-2d-two'])) alt.data_transformers.disable_max_rows() alt.Chart(data, title='max pooled vectors').mark_point().encode( x='tsne-2d-one:Q', y='tsne-2d-two:Q', tooltip=['person_id']).interactive() sequences = data[(data['tsne-2d-one'] > -15) & (data['tsne-2d-one'] < -5) & (data['tsne-2d-two'] > -25) & (data['tsne-2d-two'] < -15)]['sequence'] sequences = data['sequence'].to_list()[0:100] # +
torch.load("../data/model/resnet18x3_fft_ela_adaption_epoch30.pth")) model = model.to(device) feature_list = [] label_list = [] # Test model.eval() with torch.no_grad(): for images, labels, fourier, ela in tqdm(test_loader): images = images.to(device) fourier = fourier.to(device) labels = labels.to(device) ela = ela.to(device) _, feature = model(images, fourier, ela) feature_list.append(feature) label_list.append(labels) features = torch.Tensor.cpu(torch.cat(feature_list, 0)) X = TSNE(n_components=2).fit_transform(features) labels = torch.Tensor.cpu(torch.cat(label_list, 0)) plt.figure() for i in tqdm(range(len(X))): if labels[i].item() == 0: plt.plot(X[i, 0], X[i, 1], 'r.') else: plt.plot(X[i, 0], X[i, 1], 'g.') plt.savefig("../data/visualize/{}.jpg".format( time.strftime("%y-%m-%d-%H-%M-%S", time.localtime())))
def apply_tsne(j): idx, md5, x = j tsne = TSNE(**kwargs) return (idx, md5, tsne.fit_transform(x))
# Notice the view 2 predictions. Had our view2 images been randomly rotated, # the predictions would have a hazy circle, since the best guess would be the # mean of all the rotated digits. Since we don't rotate our view2 images, we # instead get something that's only a bit hazy around the edges -- corresonding # to the mean of all the non-rotated digits. # Next let's visualize our 20d test embeddings with T-SNE and see if they # represent our original underlying representation -- the digits from 0-9 -- of # which we made two views of. In the perfect scenario, each of the 10,000 # vectors of our test embedding would be one of ten vectors, representing the # digits from 0-9. (Our network wouldn't do this, as it tries to reconstruct # each unique view1 image exactly). In lieu of this we can hope for embedding # vectors corresponding to the same digits to be closer together. tsne = TSNE() tsneEmbeddings = tsne.fit_transform(testEmbed) def plot2DEmbeddings(embeddings, labels): pointColors = [] origColors = [ [55, 55, 55], [255, 34, 34], [38, 255, 38], [10, 10, 255], [255, 12, 255], [250, 200, 160], [120, 210, 180], [150, 180, 205], [210, 160, 210], [190, 190, 110] ] origColors = (np.array(origColors)) / 255 for l in labels.cpu().numpy(): pointColors.append(tuple(origColors[l].tolist()))
data = [] target = [] subdirectories = { dir_: idx for (idx, dir_) in enumerate(os.listdir(bboxForTsne_path)) if os.path.isdir(os.path.join(bboxForTsne_path, dir_)) } for sub_ in subdirectories.keys(): for img_ in imread_collection( os.path.join(bboxForTsne_path, sub_) + '/*.jpg'): target.append(sub_) data.append(resize(img_, (300, 300)).ravel()) return np.array(data), target, subdirectories data, target, target_num = load_images_from_subdirectories() tsne = TSNE() data_tsne = tsne.fit_transform(data) #np.save('tsne.npy', tsne) for x, y, tg in zip(data_tsne[:, 0], data_tsne[:, 1], target): plt.scatter(x, y, c=color[target_num[tg]]) plt.xlim(data_tsne[:, 0].min(), data_tsne[:, 0].max()) # 최소, 최대 plt.ylim(data_tsne[:, 1].min(), data_tsne[:, 1].max()) # 최소, 최대 plt.xlabel('t-SNE 특성0') # x축 이름 plt.ylabel('t-SNE 특성1') # y축 이름 plt.savefig('./result.png')
x_s = x_s.cuda() y_s = y_s.cuda() x_t = x_t.cuda() optimizer.zero_grad() # optimizer_ad.zero_grad() ########### Networks Forward Propagation f_s, p_s = model(x_s) f_t, p_t = model(x_t) features = torch.cat((f_s, f_t), dim=0) outputs = torch.cat((p_s, p_t), dim=0) loss = nn.CrossEntropyLoss()(outputs.narrow(0, 0, x_s.size(0)), y_s) ### TSNE tsne_model = TSNE(learning_rate=100) transformed = tsne_model.fit_transform(f_s.detach()) # transformed = tsne_model.fit_transform(f_s.detach().cpu()) X_embedded = TSNE(n_components=2, perplexity=15, learning_rate=10).fit_transform(f_s.detach()) xs = transformed[:, 0] ys = transformed[:, 1] fig = plt.figure() plt.scatter(xs, ys, c=y_s.cpu()) fig.savefig('f_s_tsne.png') pdb.set_trace()
def apply_tsne(j): idx, md5, x = j tsne = TSNE(**kwargs) return (idx, md5, tsne.fit_transform(x))
def save_data(embeddings, labels, iteration=None, detected_labels=None): N_embedded = embeddings.shape[1] # num_dims = N_embedded # num_display_dims = 2 # tsne_lr = 20.0 # Get only the non-zero indexes idx_array = labels.nonzero() idx_tuple = idx_array.split(1, dim=1) labeled_pixels = labels[idx_tuple].squeeze(1) object_pixels = torch.gather(embeddings, 0, idx_array.repeat(1, N_embedded)) unique_labels = torch.unique(labeled_pixels) N_objects = len(unique_labels) mu_vector = torch.zeros(N_objects, N_embedded) for c in range(N_objects): fiber_id = unique_labels[c] idx_c = (labeled_pixels == fiber_id).nonzero() # xi vector x_i = torch.gather(embeddings, 0, idx_c.repeat(1, N_embedded)) # get mu mu = x_i.mean(0) mu_vector[c, :] = mu resta = mu_vector[c, :] - x_i.cpu() lv_term = torch.norm(resta, 2) - 0 lv_temp2 = torch.norm(resta, 2, dim=1) ff = 0 for k in range(x_i.shape[0]): ff += torch.norm(resta[k, :], 2) mu_vector = mu_vector.detach().cpu().numpy() # Y = tsne(object_pixels[::4, :].detach().numpy(), num_display_dims, num_dims, tsne_lr) # Y = pca(object_pixels.detach().numpy(), no_dims=2).real from tsnecuda import TSNE # from tsne import tsne X = object_pixels.cpu().detach().numpy() Y = TSNE(n_components=2, perplexity=20, learning_rate=50).fit_transform(X) fig = plt.figure() ax1 = fig.add_subplot(111) ax1.scatter(Y[:, 0], Y[:, 1], 5, labeled_pixels, cmap='tab20b') if iteration is None: iteration = 0 plt.savefig("low_dim_embeeding/embedded_%d.png" % iteration) plt.close(fig) # print(X[:, 0].shape) # Y = tsne(X, 2, 32, 20.0) # fig = pylab.figure() # pylab.scatter(Y[:, 0], Y[:, 1], 5, labeled_pixels, cmap='tab20b') # if iteration is None:labels # iteration = 0 # pylab.savefig("low_dim_embeeding/embedded_%d.png" % iteration) # pylab.close(fig) ''' ax1 = fig.add_subplot(222) ax1.scatter(Y[:, 0], Y[:, 2], 5, labeled_pixels, cmap='tab20b') ax1 = fig.add_subplot(223) ax1.scatter(Y[:, 0], Y[:, 3], 5, labeled_pixels, cmap='tab20b') ax1 = fig.add_subplot(224) ax1.scatter(Y[:, 0], Y[:, 4], 5, labeled_pixels, cmap='tab20b') ''' fig = pylab.figure() ax1 = fig.add_subplot(221) ax1.scatter(X[:, 0], X[:, 1], 5, labeled_pixels, cmap='tab20b') ax1.scatter(mu_vector[:, 0], mu_vector[:, 1], 40, unique_labels, cmap='tab20b', marker="x") ax1 = fig.add_subplot(222) ax1.scatter(X[:, 0], X[:, 2], 5, labeled_pixels, cmap='tab20b') ax1.scatter(mu_vector[:, 0], mu_vector[:, 2], 40, unique_labels, cmap='tab20b', marker="x") ax1 = fig.add_subplot(223) ax1.scatter(X[:, 0], X[:, 3], 5, labeled_pixels, cmap='tab20b') ax1.scatter(mu_vector[:, 0], mu_vector[:, 3], 40, unique_labels, cmap='tab20b', marker="x") ax1 = fig.add_subplot(224) ax1.scatter(X[:, 0], X[:, 4], 5, labeled_pixels, cmap='tab20b') ax1.scatter(mu_vector[:, 0], mu_vector[:, 4], 40, unique_labels, cmap='tab20b', marker="x") plt.savefig("2_embedding/embedded_%d.png" % iteration) plt.close(fig) if detected_labels is not None: pylab.scatter(Y[:, 0], Y[:, 1], 5, detected_labels, cmap='tab20b') pylab.savefig("low_dim_embeeding/embedded_%d.png" % (iteration + 1000)) pylab.close(fig)
y_dim = test_set.num_classes() num_bits = args.nbits num_features = test_set[0][0].size(0) # load model model = VDSH(None, num_features, num_bits, dropoutProb=0.1, device=device) model.to(device) model.load_state_dict(torch.load('trained_models_32/' + args.model, map_location=torch.device(device))) model.eval() codes = [] # encode all of test for step, (xb, yb) in enumerate(test_loader): with torch.no_grad(): code = model.get_code(xb).numpy()[0] #print("Sample " + str(step) + ": " + str(code)) codes.append(code) codes = np.array(codes) embedded = TSNE(n_components = 2).fit_transform(codes) df_embedded = pd.DataFrame() df_embedded['tsne_x'] = embedded[:,0] df_embedded['tsne_y'] = embedded[:,1] df_embedded.to_pickle('df_embedded_result.pkl') sns.scatterplot(data=df_embedded, alpha=0.3, x='tsne_x', y='tsne_y') plt.show()