From: Francois Fleuret Date: Wed, 1 Mar 2017 11:00:50 +0000 (+0100) Subject: Update. X-Git-Url: https://fleuret.org/cgi-bin/gitweb/gitweb.cgi?a=commitdiff_plain;h=c2a7c7d6dfec8bd1eca29406d160cce5b4a35209;p=flatland.git Update. --- diff --git a/test.py b/test.py index de408aa..bf51360 100755 --- a/test.py +++ b/test.py @@ -4,17 +4,46 @@ import torch import torchvision from torchvision import datasets -from _ext import mylib +###################################################################### + +def sequences_to_image(x): + from PIL import Image + + nb_sequences = x.size(0) + nb_images_per_sequences = x.size(1) + nb_channels = 3 + + if x.size(2) != nb_channels: + print('Can only handle 3 channel tensors.') + exit(1) + + height = x.size(3) + width = x.size(4) + gap = 1 + gap_color = (0, 128, 255) -x = torch.ByteTensor(4, 5).fill_(0) + result = torch.ByteTensor(nb_channels, + gap + nb_sequences * (height + gap), + gap + nb_images_per_sequences * (width + gap)) -print(x.size()) + result[0].fill_(gap_color[0]) + result[1].fill_(gap_color[1]) + result[2].fill_(gap_color[2]) -mylib.generate_sequence(8, x) + for s in range(0, nb_sequences): + for i in range(0, nb_images_per_sequences): + result.narrow(1, gap + s * (height + gap), height).narrow(2, gap + i * (width + gap), width).copy_(x[s][i]) + + result_numpy = result.cpu().byte().transpose(0, 2).transpose(0, 1).numpy() + + return Image.fromarray(result_numpy, 'RGB') + +###################################################################### + +from _ext import mylib -print(x.size()) +x = torch.ByteTensor() -x = x.float().sub_(128).div_(128) +mylib.generate_sequence(10, x) -for s in range(0, x.size(0)): - torchvision.utils.save_image(x[s], 'example_' + str(s) + '.png') +sequences_to_image(x).save('sequences.png')