-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathproj_utils.py
123 lines (97 loc) · 4.2 KB
/
proj_utils.py
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
from tensorflow.contrib.tensorboard.plugins import projector
import matplotlib.pyplot as plt
import tensorflow as tf
import numpy as np
import os
LOG_DIR = 'minimal_sample'
NAME_TO_VISUALISE_VARIABLE = "fonts_embedding"
TO_EMBED_COUNT = 500
def create_sprite_image(images):
"""Returns a sprite image consisting of images passed as argument. Images should be count x width x height"""
if isinstance(images, list):
images = np.array(images)
img_h = images.shape[1]
img_w = images.shape[2]
n_plots = int(np.ceil(np.sqrt(images.shape[0])))
spriteimage = np.ones((img_h * n_plots ,img_w * n_plots ))
for i in range(n_plots):
for j in range(n_plots):
this_filter = i * n_plots + j
if this_filter < images.shape[0]:
this_img = images[this_filter]
spriteimage[i * img_h:(i + 1) * img_h,
j * img_w:(j + 1) * img_w] = this_img
return spriteimage
def vector_to_matrix(images,hig=56,wid=56):
return np.reshape(images,(-1,hig,wid))
def invert_grayscale(images):
""" Makes black white, and white black """
return 1-images
def gen_sprite(images,sprite_path,invert=False):
ims = vector_to_matrix(images)
if invert:
ims = invert_grayscale(ims)
sprite_image = create_sprite_image(ims)
plt.imsave(sprite_path,sprite_image,cmap='gray')
print ("SAVED SPRITE AT",sprite_path)
def gen_metadata(fnames,metadata_path):
# with open(metadata_path,'w') as f:
# f.write("Index\tLabel\n")
# for index,label in enumerate(fnames):
# f.write("%d\t%s \n" % (index,label))
all_names = '\n'.join(fnames)
with open(metadata_path,'w') as vecfile:
vecfile.write(all_names)
print ("SAVED METADATA AT",metadata_path)
def load_names(metadata_path):
with open(metadata_path,'r') as vecfile:
all_names = vecfile.read()
return all_names.splitlines()
def project(embeddings,font_names,images,logdir=None):
logdir = LOG_DIR if logdir is None else logdir
logdir = os.getcwd()+'/tf_logs/'+logdir
if not os.path.exists(logdir):
os.mkdir(logdir)
sprite_path = os.path.join(logdir,'font_sprite.png')
metadata_path = os.path.join(logdir,'metadata.tsv')
gen_sprite(images,sprite_path)
gen_metadata(font_names,metadata_path)
embedding_var = tf.Variable(embeddings, name=NAME_TO_VISUALISE_VARIABLE)
summary_writer = tf.summary.FileWriter(logdir)
config = projector.ProjectorConfig()
embedding = config.embeddings.add()
embedding.tensor_name = embedding_var.name
embedding.metadata_path = metadata_path
embedding.sprite.image_path = sprite_path
embedding.sprite.single_image_dim.extend([images.shape[1],images.shape[2]])
# Say that you want to visualise the embeddings
projector.visualize_embeddings(summary_writer, config)
sess = tf.InteractiveSession()
sess.run(tf.global_variables_initializer())
saver = tf.train.Saver()
saver.save(sess, os.path.join(logdir, "model.ckpt"), 1)
print ("RUN :\n"+"tensorboard --logdir "+logdir)
#
# sprite_image = create_sprite_image(vector_to_matrix_mnist(images))
# plt.imsave(LOG_DIR+'/sprite.png',sprite_image,cmap='gray')
# plt.imsave(LOG_DIR+'/sprite.png',sprite_image,cmap='gray')
# plt.imsave(LOG_DIR+'/sprite.png',sprite_image,cmap='gray')
# from tensorflow.contrib.tensorboard.plugins import projector
# from tensorflow.examples.tutorials.mnist import input_data
# import tensorflow as tf
# all_names = '\n'.join(names)
# with open(LOG_DIR+'/metadata.tsv','w') as vecfile:
# vecfile.write(all_names)
# config = projector.ProjectorConfig()
# summary_writer = tf.summary.FileWriter(LOG_DIR)
# embedding_var = tf.Variable(codes, name="font_embedding")
# embedding = config.embeddings.add()
# embedding.tensor_name = embedding_var.name
# embedding.metadata_path = path_for_mnist_metadata
# embedding.sprite.image_path = path_for_mnist_sprites
# embedding.sprite.single_image_dim.extend([56,56])
# projector.visualize_embeddings(summary_writer, config)
# sess = tf.InteractiveSession()
# sess.run(tf.global_variables_initializer())
# saver = tf.train.Saver()
# saver.save(sess, os.path.join(LOG_DIR, "model.ckpt"), 1)