annotate data_generation/mnist_resized/rescale_mnist.py @ 629:75458692efba

make a makefile rule for the aistats2011_cameraready.pdf target. It use new generic rule behind.
author Frederic Bastien <nouiz@nouiz.org>
date Thu, 17 Mar 2011 14:34:52 -0400
parents 128bc92897f2
children
rev   line source
625
128bc92897f2 Script to generate resized version of mnist in two different ways: rescaling and zeropadding
Salah Rifai <salahmeister@gmail.com>
parents:
diff changeset
1 import numpy,cPickle,gzip,Image,pdb,sys
128bc92897f2 Script to generate resized version of mnist in two different ways: rescaling and zeropadding
Salah Rifai <salahmeister@gmail.com>
parents:
diff changeset
2
128bc92897f2 Script to generate resized version of mnist in two different ways: rescaling and zeropadding
Salah Rifai <salahmeister@gmail.com>
parents:
diff changeset
3
128bc92897f2 Script to generate resized version of mnist in two different ways: rescaling and zeropadding
Salah Rifai <salahmeister@gmail.com>
parents:
diff changeset
4 def zeropad(vect,img_size=(28,28),out_size=(32,32)):
128bc92897f2 Script to generate resized version of mnist in two different ways: rescaling and zeropadding
Salah Rifai <salahmeister@gmail.com>
parents:
diff changeset
5 delta = (numpy.abs(img_size[0]-out_size[0])/2,numpy.abs(img_size[1]-out_size[1])/2)
128bc92897f2 Script to generate resized version of mnist in two different ways: rescaling and zeropadding
Salah Rifai <salahmeister@gmail.com>
parents:
diff changeset
6 newvect = numpy.zeros(out_size)
128bc92897f2 Script to generate resized version of mnist in two different ways: rescaling and zeropadding
Salah Rifai <salahmeister@gmail.com>
parents:
diff changeset
7 newvect[delta[0]:-delta[0],delta[1]:-delta[1]] = vect.reshape(img_size)
128bc92897f2 Script to generate resized version of mnist in two different ways: rescaling and zeropadding
Salah Rifai <salahmeister@gmail.com>
parents:
diff changeset
8 return newvect.flatten()
128bc92897f2 Script to generate resized version of mnist in two different ways: rescaling and zeropadding
Salah Rifai <salahmeister@gmail.com>
parents:
diff changeset
9
128bc92897f2 Script to generate resized version of mnist in two different ways: rescaling and zeropadding
Salah Rifai <salahmeister@gmail.com>
parents:
diff changeset
10 def rescale(vect,img_size=(28,28),out_size=(32,32), filter=Image.NEAREST):
128bc92897f2 Script to generate resized version of mnist in two different ways: rescaling and zeropadding
Salah Rifai <salahmeister@gmail.com>
parents:
diff changeset
11 im = Image.fromarray(numpy.asarray(vect.reshape(img_size)*255.,dtype='uint8'))
128bc92897f2 Script to generate resized version of mnist in two different ways: rescaling and zeropadding
Salah Rifai <salahmeister@gmail.com>
parents:
diff changeset
12 return (numpy.asarray(im.resize(out_size,filter),dtype='float32')/255.).flatten()
128bc92897f2 Script to generate resized version of mnist in two different ways: rescaling and zeropadding
Salah Rifai <salahmeister@gmail.com>
parents:
diff changeset
13
128bc92897f2 Script to generate resized version of mnist in two different ways: rescaling and zeropadding
Salah Rifai <salahmeister@gmail.com>
parents:
diff changeset
14
128bc92897f2 Script to generate resized version of mnist in two different ways: rescaling and zeropadding
Salah Rifai <salahmeister@gmail.com>
parents:
diff changeset
15 #pdb.set_trace()
128bc92897f2 Script to generate resized version of mnist in two different ways: rescaling and zeropadding
Salah Rifai <salahmeister@gmail.com>
parents:
diff changeset
16 def rescale_mnist(newsize=(32,32),output_file='mnist_rescaled_32_32.pkl',mnist=cPickle.load(gzip.open('mnist.pkl.gz'))):
128bc92897f2 Script to generate resized version of mnist in two different ways: rescaling and zeropadding
Salah Rifai <salahmeister@gmail.com>
parents:
diff changeset
17 newmnist = []
128bc92897f2 Script to generate resized version of mnist in two different ways: rescaling and zeropadding
Salah Rifai <salahmeister@gmail.com>
parents:
diff changeset
18 for set in mnist:
128bc92897f2 Script to generate resized version of mnist in two different ways: rescaling and zeropadding
Salah Rifai <salahmeister@gmail.com>
parents:
diff changeset
19 newset=numpy.zeros((len(set[0]),newsize[0]*newsize[1]))
128bc92897f2 Script to generate resized version of mnist in two different ways: rescaling and zeropadding
Salah Rifai <salahmeister@gmail.com>
parents:
diff changeset
20 for i in xrange(len(set[0])):
128bc92897f2 Script to generate resized version of mnist in two different ways: rescaling and zeropadding
Salah Rifai <salahmeister@gmail.com>
parents:
diff changeset
21 print i,
128bc92897f2 Script to generate resized version of mnist in two different ways: rescaling and zeropadding
Salah Rifai <salahmeister@gmail.com>
parents:
diff changeset
22 sys.stdout.flush()
128bc92897f2 Script to generate resized version of mnist in two different ways: rescaling and zeropadding
Salah Rifai <salahmeister@gmail.com>
parents:
diff changeset
23 newset[i] = rescale(set[0][i])
128bc92897f2 Script to generate resized version of mnist in two different ways: rescaling and zeropadding
Salah Rifai <salahmeister@gmail.com>
parents:
diff changeset
24 newmnist.append((newset,set[1]))
128bc92897f2 Script to generate resized version of mnist in two different ways: rescaling and zeropadding
Salah Rifai <salahmeister@gmail.com>
parents:
diff changeset
25 cPickle.dump(newmnist,open(output_file,'w'),protocol=-1)
128bc92897f2 Script to generate resized version of mnist in two different ways: rescaling and zeropadding
Salah Rifai <salahmeister@gmail.com>
parents:
diff changeset
26 print 'Done rescaling'
128bc92897f2 Script to generate resized version of mnist in two different ways: rescaling and zeropadding
Salah Rifai <salahmeister@gmail.com>
parents:
diff changeset
27
128bc92897f2 Script to generate resized version of mnist in two different ways: rescaling and zeropadding
Salah Rifai <salahmeister@gmail.com>
parents:
diff changeset
28
128bc92897f2 Script to generate resized version of mnist in two different ways: rescaling and zeropadding
Salah Rifai <salahmeister@gmail.com>
parents:
diff changeset
29 def zeropad_mnist(newsize=(32,32),output_file='mnist_zeropadded_32_32.pkl',mnist=cPickle.load(gzip.open('mnist.pkl.gz'))):
128bc92897f2 Script to generate resized version of mnist in two different ways: rescaling and zeropadding
Salah Rifai <salahmeister@gmail.com>
parents:
diff changeset
30 newmnist = []
128bc92897f2 Script to generate resized version of mnist in two different ways: rescaling and zeropadding
Salah Rifai <salahmeister@gmail.com>
parents:
diff changeset
31 for set in mnist:
128bc92897f2 Script to generate resized version of mnist in two different ways: rescaling and zeropadding
Salah Rifai <salahmeister@gmail.com>
parents:
diff changeset
32 newset=numpy.zeros((len(set[0]),newsize[0]*newsize[1]))
128bc92897f2 Script to generate resized version of mnist in two different ways: rescaling and zeropadding
Salah Rifai <salahmeister@gmail.com>
parents:
diff changeset
33 for i in xrange(len(set[0])):
128bc92897f2 Script to generate resized version of mnist in two different ways: rescaling and zeropadding
Salah Rifai <salahmeister@gmail.com>
parents:
diff changeset
34 print i,
128bc92897f2 Script to generate resized version of mnist in two different ways: rescaling and zeropadding
Salah Rifai <salahmeister@gmail.com>
parents:
diff changeset
35 sys.stdout.flush()
128bc92897f2 Script to generate resized version of mnist in two different ways: rescaling and zeropadding
Salah Rifai <salahmeister@gmail.com>
parents:
diff changeset
36 newset[i] = zeropad(set[0][i])
128bc92897f2 Script to generate resized version of mnist in two different ways: rescaling and zeropadding
Salah Rifai <salahmeister@gmail.com>
parents:
diff changeset
37 newmnist.append((newset,set[1]))
128bc92897f2 Script to generate resized version of mnist in two different ways: rescaling and zeropadding
Salah Rifai <salahmeister@gmail.com>
parents:
diff changeset
38 cPickle.dump(newmnist,open(output_file,'w'),protocol=-1)
128bc92897f2 Script to generate resized version of mnist in two different ways: rescaling and zeropadding
Salah Rifai <salahmeister@gmail.com>
parents:
diff changeset
39 print 'Done padding'
128bc92897f2 Script to generate resized version of mnist in two different ways: rescaling and zeropadding
Salah Rifai <salahmeister@gmail.com>
parents:
diff changeset
40
128bc92897f2 Script to generate resized version of mnist in two different ways: rescaling and zeropadding
Salah Rifai <salahmeister@gmail.com>
parents:
diff changeset
41 if __name__ =='__main__':
128bc92897f2 Script to generate resized version of mnist in two different ways: rescaling and zeropadding
Salah Rifai <salahmeister@gmail.com>
parents:
diff changeset
42 print 'Creating resized datasets'
128bc92897f2 Script to generate resized version of mnist in two different ways: rescaling and zeropadding
Salah Rifai <salahmeister@gmail.com>
parents:
diff changeset
43 mnist_ds = cPickle.load(gzip.open('mnist.pkl.gz'))
128bc92897f2 Script to generate resized version of mnist in two different ways: rescaling and zeropadding
Salah Rifai <salahmeister@gmail.com>
parents:
diff changeset
44 #zeropad_mnist(mnist=mnist_ds)
128bc92897f2 Script to generate resized version of mnist in two different ways: rescaling and zeropadding
Salah Rifai <salahmeister@gmail.com>
parents:
diff changeset
45 rescale_mnist(mnist=mnist_ds)
128bc92897f2 Script to generate resized version of mnist in two different ways: rescaling and zeropadding
Salah Rifai <salahmeister@gmail.com>
parents:
diff changeset
46 print 'Finished.'