comparison AMT/gene_images.py @ 395:f61a04074723

code for amazon MT
author goldfinger
date Tue, 27 Apr 2010 13:45:32 -0400
parents
children
comparison
equal deleted inserted replaced
394:ae8102aad586 395:f61a04074723
1 import PIL
2 import numpy, cPickle, gzip
3 from numpy import *
4 from utils import *
5 #from logistic_sgd import load_data
6
7 def generate_img(source, n=1):
8
9
10 #param : source = the path to the pickled data
11 #param : n = the number of tiled image to create
12
13 #loading the dataset contaning images and labels (we gonna sample from the test set)
14 f = gzip.open(source,'rb')
15 train_set, valid_set, test_set = cPickle.load(f)
16 test_set_x, test_set_y = test_set
17 f.close()
18
19 max = test_set_x.shape[0]
20
21 # creating n images containing each 10 randomly pickes caraters from the test set
22
23 for i in range(n):
24
25 #picking randomly 10 images in the test set with their labels
26 rng = numpy.random.RandomState(None)
27 sample_idx = rng.randint(max)
28 samples = numpy.array(test_set_x[sample_idx: sample_idx + 10 ])
29 samples_labels = numpy.array(test_set_y[sample_idx: sample_idx + 10 ])
30
31
32 #tiling images into a PIL images and saving it
33 image = PIL.Image.fromarray(tile_raster_images( samples,
34 img_shape = (28,28), tile_shape = (1,10),
35 tile_spacing=(1,1)))
36
37 print ' ... is saving images'
38 img_name = source + str(i)
39 image.save(img_name+'.png')
40
41 #saving the corresponding labels : todo after testing if the saving works
42 print '... is saving labels'
43 numpy.savetxt(img_name+'.txt', samples_labels)
44
45 print n, 'images saved'
46
47 if __name__ == '__main__':
48
49 print '... is generating samples'
50 generate_img('mnist.pkl.gz', n=5)
51 print 'done'
52