annotate AMT/gene_images.py @ 395:f61a04074723

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