comparison data_generation/mnist_resized/rescale_mnist.py @ 633:13baba8a4522

merge
author Yoshua Bengio <bengioy@iro.umontreal.ca>
date Sat, 19 Mar 2011 22:51:40 -0400
parents 128bc92897f2
children
comparison
equal deleted inserted replaced
632:5541056d3fb0 633:13baba8a4522
1 import numpy,cPickle,gzip,Image,pdb,sys
2
3
4 def zeropad(vect,img_size=(28,28),out_size=(32,32)):
5 delta = (numpy.abs(img_size[0]-out_size[0])/2,numpy.abs(img_size[1]-out_size[1])/2)
6 newvect = numpy.zeros(out_size)
7 newvect[delta[0]:-delta[0],delta[1]:-delta[1]] = vect.reshape(img_size)
8 return newvect.flatten()
9
10 def rescale(vect,img_size=(28,28),out_size=(32,32), filter=Image.NEAREST):
11 im = Image.fromarray(numpy.asarray(vect.reshape(img_size)*255.,dtype='uint8'))
12 return (numpy.asarray(im.resize(out_size,filter),dtype='float32')/255.).flatten()
13
14
15 #pdb.set_trace()
16 def rescale_mnist(newsize=(32,32),output_file='mnist_rescaled_32_32.pkl',mnist=cPickle.load(gzip.open('mnist.pkl.gz'))):
17 newmnist = []
18 for set in mnist:
19 newset=numpy.zeros((len(set[0]),newsize[0]*newsize[1]))
20 for i in xrange(len(set[0])):
21 print i,
22 sys.stdout.flush()
23 newset[i] = rescale(set[0][i])
24 newmnist.append((newset,set[1]))
25 cPickle.dump(newmnist,open(output_file,'w'),protocol=-1)
26 print 'Done rescaling'
27
28
29 def zeropad_mnist(newsize=(32,32),output_file='mnist_zeropadded_32_32.pkl',mnist=cPickle.load(gzip.open('mnist.pkl.gz'))):
30 newmnist = []
31 for set in mnist:
32 newset=numpy.zeros((len(set[0]),newsize[0]*newsize[1]))
33 for i in xrange(len(set[0])):
34 print i,
35 sys.stdout.flush()
36 newset[i] = zeropad(set[0][i])
37 newmnist.append((newset,set[1]))
38 cPickle.dump(newmnist,open(output_file,'w'),protocol=-1)
39 print 'Done padding'
40
41 if __name__ =='__main__':
42 print 'Creating resized datasets'
43 mnist_ds = cPickle.load(gzip.open('mnist.pkl.gz'))
44 #zeropad_mnist(mnist=mnist_ds)
45 rescale_mnist(mnist=mnist_ds)
46 print 'Finished.'