Tensorboardx: How to draw a picture about attention ?

Created on 11 Jun 2018  Â·  10Comments  Â·  Source: lanpa/tensorboardX

Hello, I want to draw a picture about attention which size is batchsrc_lenthtgt_lenth,like a Tensor (20,30,25),so I want to draw one picture which size is 30*25,but I find the function of add_image's image_tensor should be given by a Tensor like(3,H,W).
I am confused that how can I draw this picture with only H-W.
I can copy this dimension three times,Is it right?

All 10 comments

Passing a Tensor of size (20,30,25) should be handled automatically by tensorboardX. The only thing you need to do is normalize your image to [0, 1]. The doc is too old.

Thank you for your reply.Do you mean to directly send tensor (20, 30, 25) to the add_image?When I do this,I meet a problem.

 12 Traceback (most recent call last):$
 13   File "/home/hfyu/anaconda/lib/python3.6/site-packages/PIL/Image.py", line 2423, in fromarray$
 14     mode, rawmode = _fromarray_typemap[typekey]$
 15 KeyError: ((1, 1, 200), '|u1')$
 16 $
 17 During handling of the above exception, another exception occurred:$
 18 $
 19 Traceback (most recent call last):$
 20   File "train.py", line 29, in <module>$
 21     main(args)$
 22   File "train.py", line 23, in main$
 23     singleprocess_main(args)$
 24   File "/home/hfyu/fairseq-py-0.4.0/singleprocess_train.py", line 86, in main$
 25     train(args, trainer, dataset, epoch, batch_offset)$
 26   File "/home/hfyu/fairseq-py-0.4.0/singleprocess_train.py", line 157, in train$
 27     log_output = trainer.train_step(sample)$
 28   File "/home/hfyu/fairseq-py-0.4.0/fairseq/trainer.py", line 97, in train_step$
 29     loss, sample_sizes, logging_outputs, ooms_fwd = self._forward(sample)$
 30   File "/home/hfyu/fairseq-py-0.4.0/fairseq/trainer.py", line 145, in _forward$
 31     loss, sample_size, logging_output_ = self.criterion(self.model, sample)$
 32   File "/home/hfyu/anaconda/lib/python3.6/site-packages/torch/nn/modules/module.py", line 491, in __call__$
 33     result = self.forward(*input, **kwargs)$
 34   File "/home/hfyu/fairseq-py-0.4.0/fairseq/criterions/cross_entropy.py", line 30, in forward$
 35     net_output = model(**sample['net_input'])$
 36   File "/home/hfyu/anaconda/lib/python3.6/site-packages/torch/nn/modules/module.py", line 491, in __call__$
 37     result = self.forward(*input, **kwargs)$
 38   File "/home/hfyu/fairseq-py-0.4.0/fairseq/models/fairseq_model.py", line 44, in forward$
 39     decoder_out = self.decoder(prev_output_tokens, encoder_out)$
 40   File "/home/hfyu/anaconda/lib/python3.6/site-packages/torch/nn/modules/module.py", line 491, in __call__$
 41     result = self.forward(*input, **kwargs)$
 42   File "/home/hfyu/fairseq-py-0.4.0/fairseq/models/fconv.py", line 323, in forward$
 43     self.writer.add_image('atten_Image', avg_attn_scores)$
 44   File "/home/hfyu/anaconda/lib/python3.6/site-packages/tensorboardX/writer.py", line 342, in add_image$
 45     self.file_writer.add_summary(image(tag, img_tensor), global_step)$
 46   File "/home/hfyu/anaconda/lib/python3.6/site-packages/tensorboardX/summary.py", line 158, in image$
 47     image = make_image(tensor)$
 48   File "/home/hfyu/anaconda/lib/python3.6/site-packages/tensorboardX/summary.py", line 166, in make_image$
 49     image = Image.fromarray(tensor)$
 50   File "/home/hfyu/anaconda/lib/python3.6/site-packages/PIL/Image.py", line 2426, in fromarray$
 51     raise TypeError("Cannot handle this data type")$
 52 TypeError: Cannot handle this data type$

Sorry. It will not convert automatically in this case. You can reshape to (20, 1, 30, 25). or stacking it to (20, 3, 30, 25)

Thank you for your reply,I get it.

Trouble you,I have another problem,the attention picture I draw is too dark,I think maybe the number of my attention's Tensor is between 0 and 1(max is 1)so ,I hope it can be showed with percentage.
Or what is the cause of this picture too dark?

@lanpa

maybe your feature map is just empty. you can check the distribution with add_histogram().

OK, I'll try it

oh,I change the Tensor(20,30,25)to (1,1,30,25)like this.because I just want to one picture.
a= torch.randn(20,30,25) a = a[:1,:,:].unsqueeze(0)
Does it have effects?

Thank you ,I solve it

Was this page helpful?
0 / 5 - 0 ratings

Related issues

chinmay5 picture chinmay5  Â·  5Comments

hrtang picture hrtang  Â·  4Comments

mbenami picture mbenami  Â·  6Comments

kmyfoer picture kmyfoer  Â·  4Comments

mlagunas picture mlagunas  Â·  5Comments