@@ -28,16 +28,17 @@ def make_sprite(label_img, save_path):
2828 else :
2929 torchvision .utils .save_image (label_img , os .path .join (save_path , 'sprite.png' ), nrow = nrow , padding = 0 )
3030
31- def make_pbtxt (save_path , metadata , label_img ):
32- with open (os .path .join (save_path , 'projector_config.pbtxt' ), 'w' ) as f :
31+ def append_pbtxt (metadata , label_img , save_path , global_step ):
32+ with open (os .path .join (save_path , 'projector_config.pbtxt' ), 'a' ) as f :
33+ #step = os.path.split(save_path)[-1]
3334 f .write ('embeddings {\n ' )
34- f .write ('tensor_name: "embedding:0 "\n ' )
35- f .write ('tensor_path: "tensors.tsv "\n ' )
35+ f .write ('tensor_name: "embedding:{} "\n ' . format ( global_step ) )
36+ f .write ('tensor_path: "{} "\n ' . format ( os . path . join ( global_step , "tensors.tsv" )) )
3637 if metadata is not None :
37- f .write ('metadata_path: "metadata.tsv "\n ' )
38+ f .write ('metadata_path: "{} "\n ' . format ( os . path . join ( global_step , "metadata.tsv" )) )
3839 if label_img is not None :
3940 f .write ('sprite {\n ' )
40- f .write ('image_path: "sprite.png "\n ' )
41+ f .write ('image_path: "{} "\n ' . format ( os . path . join ( global_step , "sprite.png" )) )
4142 f .write ('single_image_dim: {}\n ' .format (label_img .size (3 )))
4243 f .write ('single_image_dim: {}\n ' .format (label_img .size (2 )))
4344 f .write ('}\n ' )
@@ -47,129 +48,4 @@ def make_mat(matlist, save_path):
4748 with open (os .path .join (save_path , 'tensors.tsv' ), 'w' ) as f :
4849 for x in matlist :
4950 x = [str (i ) for i in x ]
50- f .write ('\t ' .join (x ) + '\n ' )
51-
52- def add_embedding (mat , save_path , metadata = None , label_img = None ):
53- """add embedding
54-
55- Args:
56- mat (torch.Tensor): A matrix which each row is the feature vector of the data point
57- save_path (string): Save path (use ``writer.file_writer.get_logdir()`` to show embedding along with other summaries)
58- metadata (list): A list of labels, each element will be convert to string
59- label_img (torch.Tensor): Images correspond to each data point
60- Shape:
61- mat: :math:`(N, D)`, where N is number of data and D is feature dimension
62-
63- label_img: :math:`(N, C, H, W)`
64-
65- .. note::
66- ~~This function needs tensorflow installed. It invokes tensorflow to dump data. ~~
67- Therefore I separate it from the SummaryWriter class. Please pass ``writer.file_writer.get_logdir()`` to ``save_path`` to prevent glitches.
68-
69- If ``save_path`` is different than SummaryWritter's save path, you need to pass the leave directory to tensorboard's logdir argument,
70- otherwise it cannot display anything. e.g. if ``save_path`` equals 'path/to/embedding',
71- you need to call 'tensorboard --logdir=path/to/embedding', instead of 'tensorboard --logdir=path'.
72-
73-
74- Examples::
75-
76- from tensorboard.embedding import add_embedding
77- import keyword
78- import torch
79- meta = []
80- while len(meta)<100:
81- meta = meta+keyword.kwlist # get some strings
82- meta = meta[:100]
83-
84- for i, v in enumerate(meta):
85- meta[i] = v+str(i)
86-
87- label_img = torch.rand(100, 3, 10, 32)
88- for i in range(100):
89- label_img[i]*=i/100.0
90-
91- add_embedding(torch.randn(100, 5), 'embedding1', metadata=meta, label_img=label_img)
92- add_embedding(torch.randn(100, 5), 'embedding2', label_img=label_img)
93- add_embedding(torch.randn(100, 5), 'embedding3', metadata=meta)
94- """
95- try :
96- os .makedirs (save_path )
97- except OSError :
98- print ('warning: dir exists' )
99- if metadata is not None :
100- assert mat .size (0 )== len (metadata ), '#labels should equal with #data points'
101- make_tsv (metadata , save_path )
102- if label_img is not None :
103- assert mat .size (0 )== label_img .size (0 ), '#images should equal with #data points'
104- make_sprite (label_img , save_path )
105- assert mat .dim ()== 2 , 'mat should be 2D, where mat.size(0) is the number of data points'
106- make_mat (mat .tolist (), save_path )
107- make_pbtxt (save_path , metadata , label_img )
108-
109- def append_pbtxt (f , metadata , label_img ,path ):
110-
111- f .write ('embeddings {\n ' )
112- f .write ('tensor_name: "{}"\n ' .format (os .path .join (path ,"embedding" )))
113- f .write ('tensor_path: "{}"\n ' .format (os .path .join (path ,"tensors.tsv" )))
114- if metadata is not None :
115- f .write ('metadata_path: "{}"\n ' .format (os .path .join (path ,"metadata.tsv" )))
116- if label_img is not None :
117- f .write ('sprite {\n ' )
118- f .write ('image_path: "{}"\n ' .format (os .path .join (path ,"sprite.png" )))
119- f .write ('single_image_dim: {}\n ' .format (label_img .size (3 )))
120- f .write ('single_image_dim: {}\n ' .format (label_img .size (2 )))
121- f .write ('}\n ' )
122- f .write ('}\n ' )
123-
124-
125- class EmbeddingWriter (object ):
126- """
127- Class to allow writing embeddings ad defined timestep
128-
129- """
130- def __init__ (self ,save_path ):
131- """
132-
133- :param save_path: should be the same path of you SummaryWriter
134- """
135- self .save_path = save_path
136- #make dir if needed, it should not
137- try :
138- os .makedirs (save_path )
139- except OSError :
140- print ('warning: dir exists' )
141- #create config file to store all embeddings conf
142- self .f = open (os .path .join (save_path , 'projector_config.pbtxt' ), 'w' )
143-
144- def add_embedding (self ,mat , metadata = None , label_img = None ,timestep = 0 ):
145- """
146- add an embedding at the defined timestep
147-
148- :param mat:
149- :param metadata:
150- :param label_img:
151- :param timestep:
152- :return:
153- """
154- # TODO make doc
155- #path to the new subdir
156- timestep_path = "{}" .format (timestep )
157- # TODO should this be handled?
158- os .makedirs (os .path .join (self .save_path ,timestep_path ))
159- #check other info
160- #save all this metadata in the new subfolder
161- if metadata is not None :
162- assert mat .size (0 ) == len (metadata ), '#labels should equal with #data points'
163- make_tsv (metadata , os .path .join (self .save_path ,timestep_path ))
164- if label_img is not None :
165- assert mat .size (0 ) == label_img .size (0 ), '#images should equal with #data points'
166- make_sprite (label_img , os .path .join (self .save_path ,timestep_path ))
167- assert mat .dim () == 2 , 'mat should be 2D, where mat.size(0) is the number of data points'
168- make_mat (mat .tolist (), os .path .join (self .save_path ,timestep_path ))
169- #new funcion to append to the config file a new embedding
170- append_pbtxt (self .f , metadata , label_img ,timestep_path )
171-
172-
173- def __del__ (self ):
174- #close the file at the end of the script
175- self .f .close ()
51+ f .write ('\t ' .join (x ) + '\n ' )
0 commit comments