def visualize_hidden(self,threshold,bounds):
        print '\nSaving hidden layer filters...\n'

        #Visualizing 1st hidden layer
        f_name = 'my_filter_layer_0.png'
        im_side = sqrt(self.i_size)
        im_count = int(sqrt(self.h_sizes[0]))
        image = Image.fromarray(tile_raster_images(
        X=self.sa_layers[0].W1.get_value(borrow=True).T,
        img_shape=(im_side, im_side), tile_shape=(im_count, im_count),
        tile_spacing=(1, 1)))
        image.save(f_name)

        index = T.lscalar('index')
        max_inputs =[]
        #Higher level hidden layers
        for i in xrange(1,self.n_layers):
            print "Calculating features for higher layers\n"
            inp = 1e-8 + np.random.random_sample((self.i_size,))*0.05
            inp = np.asarray(inp,dtype=config.floatX)
            input = shared(value=inp, name='input',borrow=True)

            max_ins = self.nlopt_optimization(input,threshold,bounds,i)

            f_name = 'my_filter_layer_'+str(i)+'.png'
            im_side = sqrt(self.i_size)
            im_count = int(sqrt(self.h_sizes[i]))
            image = Image.fromarray(tile_raster_images(
                X=max_ins,
                img_shape=(im_side, im_side), tile_shape=(im_count, im_count),
                tile_spacing=(1, 1)))
            image.save(f_name)
def test(Weights, counter, ext, channel=1):
    """this is an utility that takes weights and plot there feature as image"""
    tile_shape = (10, 10)
    image_resize_shape = (2, 2)
    if channel == 1:
        img = tile_raster_images(
            X=Weights.T, img_shape=(window_size, window_size), tile_shape=tile_shape, tile_spacing=(1, 1)
        )
        newimg = np.zeros((img.shape[0] * image_resize_shape[0], img.shape[1] * image_resize_shape[1]))
        for i in xrange(img.shape[0]):
            for j in xrange(img.shape[1]):
                newimg[
                    i * image_resize_shape[0] : (i + 1) * image_resize_shape[0],
                    j * image_resize_shape[1] : (j + 1) * image_resize_shape[1],
                ] = img[i][j] * np.ones(image_resize_shape)
        cv2.imwrite("tmp/" + str(counter) + "_" + ext + ".jpg", newimg)
    else:
        tile = Weights.shape[0] / channel
        i = 0
        temp = (
            Weights.T[:, tile * i : (i + 1) * tile],
            Weights.T[:, (i + 1) * tile : (i + 2) * tile],
            Weights.T[:, (i + 2) * tile : tile * (i + 3)],
        )
        img = tile_raster_images(
            X=temp, img_shape=(window_size, window_size), tile_shape=tile_shape, tile_spacing=(1, 1)
        )
        newimg = cv2.resize(img, (img.shape[0] * image_resize_shape[0], img.shape[1] * image_resize_shape[1]))
        cv2.imwrite("tmp/" + str(counter) + "_" + ext + ".jpg", newimg)
Example #3
0
def test_autos_l2(corruption=0):
#load data
    dataset='mnist.pkl.gz'
    datasets = load_data(dataset)
    train_set_x, train_set_y = datasets[0]
    valid_set_x,valid_set_y = datasets[1]


#test against validation set
    n_hiddens = [10,25,50,100]
    x = T.matrix('x')
    rng = numpy.random.RandomState(123)
    theano_rng = RandomStreams(rng.randint(2 ** 30))

    for n_hidden in n_hiddens:
    #load model
        da = dA(numpy_rng = rng,
                theano_rng = theano_rng,
                input=x,
                n_visible=28*28,
                n_hidden=n_hidden)
        da.load(open('../data/dA_l2/dA_l2_nhid'+str(n_hidden)+'_corr'+str(corruption)+'.p','r'))
        
        reconstructed = da.get_reconstructed_input(input=valid_set_x)
        image = Image.fromarray(tile_raster_images(X=reconstructed.eval(),img_shape=(28, 28), tile_shape=(10, 10),tile_spacing=(1, 1)))
        image.save('../data/dA_l2/pics/dAs_reconstructed_nhid'+str(da.n_hidden)+'_corr'+str(corruption)+'.png')

    image = Image.fromarray(tile_raster_images(X=valid_set_x.get_value(),img_shape=(28, 28), tile_shape=(10, 10),tile_spacing=(1, 1)))
    image.save('../data/dA_l2/pics/original.png')
def test(dataset = 'mnist.pkl.gz', output_folder = 'plots'):
    
    datasets = load_data(dataset)
    train_set_x, train_set_y = datasets[0]
    
    if not os.path.isdir(output_folder):
        os.makedirs(output_folder)
    os.chdir(output_folder)
    
    rng = numpy.random.RandomState(123)
    theano_rng = RandomStreams(rng.randint(2 ** 30))
    
    input = T.matrix('input')
    
    output = get_corrupted_input_gaussian(theano_rng = theano_rng, input = input)
    
    corrupt = theano.function([input], output)
    
    
    mnist_noise = corrupt(train_set_x.get_value(borrow = True))
    mnist_noise = theano.shared(value=mnist_noise, name='mnist_noise', borrow = True)
#     print train_set_x.get_value(borrow=True)[0]
#     print mnist_noise.get_value(borrow=True)[0]
    
    image_clean = Image.fromarray(tile_raster_images(X = train_set_x.get_value(borrow = True),
                                               img_shape=(28, 28), tile_shape=(1, 6),
                                               tile_spacing=(1,1)))
    image_clean.save('clean_6.png')
    
    image_noise = Image.fromarray(tile_raster_images(X = mnist_noise.get_value(borrow = True),
                                               img_shape=(28, 28), tile_shape=(1, 6),
                                               tile_spacing=(1,1)))
    image_noise.save('noise_6.png')
    
    print 'Done!'
def main():
    mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
    trX, trY, teX, teY = mnist.train.images, mnist.train.labels, mnist.test.images,\
        mnist.test.labels
    
    img = Image.fromarray(tile_raster_images(X=trX[1:2], img_shape=(
        28, 28), tile_shape=(1, 1), tile_spacing=(1, 1)))
    plt.rcParams['figure.figsize'] = (2.0, 2.0)
    imgplot = plt.imshow(img)
    imgplot.set_cmap('gray')
    plt.show()
    # trX = [[0, 0, 0], [0, 1, 1], [1, 0, 1], [1, 1, 0]]
    # trX = np.asarray(trX)
    # print(trX.shape[1])
    rbm = RBM(trX.shape[1], 500)
    rbm.train(trX, debug=True)

    # Saving weights and biases
    rbm.save_weights(0)
    rbm.save_biases(0)

    out = rbm.rbm_output(trX[1:2], debug=True)


    v1 = rbm.backward_pass(out, rbm.w, rbm.vb)
    with tf.Session() as sess:
        feed = sess.run(out)
        out = sess.run(v1, feed_dict={out: feed})
    img = Image.fromarray(tile_raster_images(X=out, img_shape=(
        28, 28), tile_shape=(1, 1), tile_spacing=(1, 1)))
    plt.rcParams['figure.figsize'] = (2.0, 2.0)
    imgplot = plt.imshow(img)
    imgplot.set_cmap('gray')
    plt.show()
Example #6
0
def visualize_examples_supervised(dataset):
    patches = dataset[0]
    labels = dataset[1]

    ps = np.sqrt(np.shape(patches)[1])

    posIndices = np.nonzero(labels)
    negIndices = np.nonzero(1 - labels)

    imagePos = PIL.Image.fromarray(
        tile_raster_images(X=patches[posIndices],
                           img_shape=(ps, ps),
                           tile_shape=(10, 10),
                           tile_spacing=(1, 1)))
    imageNeg = PIL.Image.fromarray(
        tile_raster_images(X=patches[negIndices],
                           img_shape=(ps, ps),
                           tile_shape=(10, 10),
                           tile_spacing=(1, 1)))

    plt.subplot(2, 1, 1)
    plt.imshow(imagePos)
    plt.subplot(2, 1, 2)
    plt.imshow(imageNeg)
    plt.show()
Example #7
0
    def visualize_hidden(self,threshold,bounds):
        print '\nSaving hidden layer filters...\n'

        #Visualizing 1st hidden layer
        f_name = 'my_filter_layer_0.png'
        im_count = int(sqrt(self.h_sizes[0]))
        image = Image.fromarray(tile_raster_images(
        X=self.sa_layers[0].W1.get_value(borrow=True).T,
        img_shape=(self.i_width, self.i_height), tile_shape=(im_count, im_count),
        tile_spacing=(1, 1)))
        image.save(f_name)

        index = T.lscalar('index')
        max_inputs =[]
        #Higher level hidden layers
        for i in xrange(1,self.n_layers):
            print "Calculating features for higher layers\n"
            inp = np.random.random_sample((self.i_size,))*0.02
            inp = np.asarray(inp,dtype=config.floatX)
            input = shared(value=inp, name='input',borrow=True)

            max_ins = self.theano_max_activations(input,threshold,i,0.2)

            f_name = 'my_filter_layer_'+str(i)+'.png'
            im_count = int(sqrt(self.h_sizes[i]))
            image = Image.fromarray(tile_raster_images(
                X=max_ins,
                img_shape=(self.i_width, self.i_height), tile_shape=(im_count, im_count),
                tile_spacing=(1, 1)))
            image.save(f_name)
def test(Weights, counter, ext, channel=1):
	"""this is an utility that takes weights and plot there feature as image"""
	tile_shape = (8, 8)
	image_resize_shape = (10, 10)
	img_shape = (window_size, window_size)
	newimg = None
	if channel == 1:
		img = tile_raster_images(X=Weights.T, img_shape=img_shape, tile_shape=tile_shape, tile_spacing=(1, 1))
		newimg = np.zeros((img.shape[0]*image_resize_shape[0], img.shape[1]*image_resize_shape[1]))
		for i in xrange(img.shape[0]):
			for j in xrange(img.shape[1]):
				newimg[i*image_resize_shape[0]:(i+1)*image_resize_shape[0], j*image_resize_shape[1]:(j+1)*image_resize_shape[1]] = img[i][j] * np.ones(image_resize_shape)
		cv2.imwrite('tmp/'+str(counter)+'_'+ext+'.jpg', newimg)
	elif channel == 3:
		tile = Weights.shape[0] / channel
		i = 0
		temp = (Weights.T[:, tile*i:(i+1)*tile], Weights.T[:, (i+1)*tile:(i+2)*tile], Weights.T[:, (i+2)*tile:tile*(i+3)])
		img = tile_raster_images(X=temp, img_shape=img_shape, tile_shape=tile_shape, tile_spacing=(1, 1))
		newimg = cv2.resize(img, (img.shape[0] * image_resize_shape[0],img.shape[1] * image_resize_shape[1]))
		cv2.imwrite('tmp/'+str(counter)+'_'+ext+'.jpg', newimg)
	else:
		temp = []
		Weights = Weights.reshape((window_size*window_size, 64, 64))
		for k in xrange(Weights.shape[1]):
			img = tile_raster_images(X=Weights[:,k, :].T, img_shape=img_shape, tile_shape=tile_shape, tile_spacing=(1, 1))
			newimg = np.zeros((img.shape[0]*image_resize_shape[0], img.shape[1]*image_resize_shape[1]))
			for i in xrange(img.shape[0]):
				for j in xrange(img.shape[1]):
					newimg[i*image_resize_shape[0]:(i+1)*image_resize_shape[0], j*image_resize_shape[1]:(j+1)*image_resize_shape[1]] = img[i][j] * np.ones(image_resize_shape)
			temp.append(newimg)
		result = np.mean(temp, axis=0)
		cv2.imwrite('tmp/'+str(k)+'_'+str(counter)+'_'+ext+'.jpg', result)
def Find_plankton(model_name="plankton_conv_visualize_model.pkl.params"):
    """
    Find plankton that activates the given layers most
    """
    which_layer = 2
    
    import plankton_vis1
    samples = plankton_vis1.loadSamplePlanktons(numSamples=3000)
    print 'Dimension of Samples', np.shape(samples)
    Net = DeConvNet(model_name)
    
    #kernel_list = [ 2,23,60,12,45,9 ]
    kernel_list = range(48,64)
    
    
    num_of_maximum = 9
    Heaps = findmaxactivation( Net, samples, num_of_maximum, kernel_list, which_layer=which_layer)
    bigbigmap = None # what is this?
    for kernel_index in Heaps:        
        print 'dealing with',kernel_index,'th kernel'
        heap = Heaps[kernel_index]
        this_sams = []
        this_Deconv = []
        for pairs in heap:
            this_sam = pairs.sam
            this_sams.append( this_sam.reshape([NUM_C,MAX_PIXEL,MAX_PIXEL]) )
            this_Deconv.append( Net.DeConv( this_sam, kernel_index, which_layer=which_layer ).reshape([NUM_C,MAX_PIXEL,MAX_PIXEL]) )
        
        this_sams = np.array( this_sams )
        this_sams = np.transpose( this_sams, [ 1, 0, 2, 3 ])
        this_sams = this_sams.reshape( [ NUM_C, 9, MAX_PIXEL*MAX_PIXEL ])
        this_sams = tuple( [ this_sams[0] for i in xrange(3)] + [None] )    
        
        this_Deconv = np.array( this_Deconv )
        this_Deconv = np.transpose( this_Deconv, [ 1, 0, 2, 3 ])
        this_Deconv = this_Deconv.reshape( [ NUM_C, num_of_maximum, MAX_PIXEL*MAX_PIXEL ])
        this_Deconv = tuple( [ this_Deconv[0] for i in xrange(3)] + [None] )

        this_map = tile_raster_images( this_sams, img_shape = (MAX_PIXEL,MAX_PIXEL), tile_shape = (3,3), 
                                   tile_spacing=(1, 1), scale_rows_to_unit_interval=True, 
                                    output_pixel_vals=True)
        this_Deconv = tile_raster_images( this_Deconv, img_shape = (MAX_PIXEL,MAX_PIXEL), tile_shape = (3,3), 
                                   tile_spacing=(1, 1), scale_rows_to_unit_interval=True, 
                                    output_pixel_vals=True)
        this_pairmap = np.append( this_map, this_Deconv, axis = 0)

        if bigbigmap == None:
            bigbigmap = this_pairmap
            segment_line = 255*np.ones([bigbigmap.shape[0],1,4],dtype='uint8')
        else:
            bigbigmap = np.append(bigbigmap, segment_line, axis = 1)            
            bigbigmap = np.append(bigbigmap, this_pairmap, axis = 1)
            
            
    plt.imshow(bigbigmap)
    plt.show()
Example #10
0
def Find_cifa_10():
    """
    balabala
    """
    which_layer = 2
    
    ''' -------------- '''
    sam_file = open('SubSet1000.pkl', 'r')
    samples = cPickle.load( sam_file )
    sam_file.close()
    print 'Dimension of Samples', np.shape(samples)
    Net = DeConvNet()
    
    #kernel_list = [ 2,23,60,12,45,9 ]    
    kernel_list = [0,5,10,15]
    
    Heaps = findmaxactivation( Net, samples, 9, kernel_list, which_layer=which_layer)
    bigbigmap = None # what is this?
    for kernel_index in Heaps:        
        print 'dealing with',kernel_index,'th kernel'
        heap = Heaps[kernel_index]
        this_sams = []
        this_Deconv = []
        for pairs in heap:
            this_sam = pairs.sam
            this_sams.append( this_sam.reshape([3,32,32]) )
            this_Deconv.append( Net.DeConv( this_sam, kernel_index, which_layer=which_layer ).reshape([3,32,32]) )
        
        this_sams = np.array( this_sams )
        this_sams = np.transpose( this_sams, [ 1, 0, 2, 3 ])
        this_sams = this_sams.reshape( [ 3, 9, 32*32 ])
        this_sams = tuple( [ this_sams[i] for i in xrange(3)] + [None] )    
        
        this_Deconv = np.array( this_Deconv )
        this_Deconv = np.transpose( this_Deconv, [ 1, 0, 2, 3 ])
        this_Deconv = this_Deconv.reshape( [ 3, 9, 32*32 ])
        this_Deconv = tuple( [ this_Deconv[i] for i in xrange(3)] + [None] )

        this_map = tile_raster_images( this_sams, img_shape = (32,32), tile_shape = (3,3), 
                                   tile_spacing=(1, 1), scale_rows_to_unit_interval=True, 
                                    output_pixel_vals=True)
        this_Deconv = tile_raster_images( this_Deconv, img_shape = (32,32), tile_shape = (3,3), 
                                   tile_spacing=(1, 1), scale_rows_to_unit_interval=True, 
                                    output_pixel_vals=True)
        this_pairmap = np.append( this_map, this_Deconv, axis = 0)

        if bigbigmap == None:
            bigbigmap = this_pairmap
            segment_line = 255*np.ones([bigbigmap.shape[0],1,4],dtype='uint8')
        else:
            bigbigmap = np.append(bigbigmap, segment_line, axis = 1)            
            bigbigmap = np.append(bigbigmap, this_pairmap, axis = 1)
            
            
    plt.imshow(bigbigmap)
    plt.show()
Example #11
0
def Find_cifa_10():
    """
    balabala
    """
    
    sam_file = open('SubSet1000.pkl', 'r')
    samples = cPickle.load( sam_file )
    sam_file.close()
    
    Net = DeConvNet()
    
    kernel_list = [ 2,23,60,12,45,9 ]    
    
    Heaps = findmaxactivation( Net, samples, 9, kernel_list )
    bigbigmap = None
    for kernel_index in Heaps:        
        print 'dealing with',kernel_index,'th kernel'
        heap = Heaps[kernel_index]
        this_sams = []
        this_Deconv = []
        for pairs in heap:
            this_sam = pairs.sam
            this_sams.append( this_sam.reshape([3,32,32]) )
            this_Deconv.append( Net.DeConv( this_sam, kernel_index ).reshape([3,32,32]) )
        
        this_sams = np.array( this_sams )
        this_sams = np.transpose( this_sams, [ 1, 0, 2, 3 ])
        this_sams = this_sams.reshape( [ 3, 9, 32*32 ])
        this_sams = tuple( [ this_sams[i] for i in xrange(3)] + [None] )    
        
        this_Deconv = np.array( this_Deconv )
        this_Deconv = np.transpose( this_Deconv, [ 1, 0, 2, 3 ])
        this_Deconv = this_Deconv.reshape( [ 3, 9, 32*32 ])
        this_Deconv = tuple( [ this_Deconv[i] for i in xrange(3)] + [None] )

        this_map = tile_raster_images( this_sams, img_shape = (32,32), tile_shape = (3,3), 
                                   tile_spacing=(1, 1), scale_rows_to_unit_interval=True, 
                                    output_pixel_vals=True)
        this_Deconv = tile_raster_images( this_Deconv, img_shape = (32,32), tile_shape = (3,3), 
                                   tile_spacing=(1, 1), scale_rows_to_unit_interval=True, 
                                    output_pixel_vals=True)
        this_pairmap = np.append( this_map, this_Deconv, axis = 0)

        if bigbigmap == None:
            bigbigmap = this_pairmap
            segment_line = 255*np.ones([bigbigmap.shape[0],1,4],dtype='uint8')
        else:
            bigbigmap = np.append(bigbigmap, segment_line, axis = 1)            
            bigbigmap = np.append(bigbigmap, this_pairmap, axis = 1)
            
            
    plt.imshow(bigbigmap)
    plt.show()
Example #12
0
def main():
    if not os.path.exists('rbm_vis'):
        os.makedirs('rbm_vis')

    batch_size = 20
    train_set, test_set, val_set = load_data()

    test_x, test_y = test_set
    test_idx = np.where(test_y == 2)[0]
    test_sub_set = test_x[test_idx]

    train_x, train_y = train_set
    train_idx = np.where(train_y == 2)[0]
    train_sub_set = train_x[train_idx]
    rbm = RBM(
          n_visible= 28 * 28,
          n_hidden=100,
          input=train_sub_set,
          lr=0.5,
          b_size=batch_size
          )
    epoches = 20
    for i in range(epoches):

        w, b, a = rbm.compute_updates()
        img = Image.fromarray(utils.tile_raster_images(w.T,
                img_shape=(28, 28),
                tile_shape=(10, 10),
                tile_spacing=(1, 1)))
        img.save('rbm_vis/filter_eapoch_{}.png'.format(i))

        # test phase
        random_idx = numpy.random.choice(range(len(test_sub_set)))
        random_smpl = test_sub_set[random_idx]
        corr_random_smpl = copy.deepcopy(random_smpl)
        corr_random_smpl[:392] = 0

        # reshape
        test_itm = test_sub_set[random_idx].reshape((1,-1))
        visible_p = corr_random_smpl.reshape((1,-1))
        v2_sample = rbm.reconstruct_visible([corr_random_smpl])
        before_after = np.concatenate((test_itm, visible_p, v2_sample), axis=0)
        reconstruct = Image.fromarray(utils.tile_raster_images(before_after,
                img_shape=(28, 28),
                tile_shape=(1, 3),
                tile_spacing=(1, 1)))
        reconstruct.save('rbm_vis/reconst_eapoch_{}.png'.format(i))
        print('finished eapoch {}'.format(i))
Example #13
0
    def Plot_EXP_RF(self, layer=0):
        Exp_RF = self.network.X.T.dot(self.network.Y[layer])

        spike_sum = np.sum(self.network.Y[layer], axis=0, dtype='f')
        Exp_RF = Exp_RF.dot(np.diag(1 / spike_sum))

        im_size, num_dict = Exp_RF.shape

        side = int(np.round(np.sqrt(im_size)))
        im_rows = int(np.sqrt(num_dict))
        if im_rows**2 < num_dict:
            im_cols = im_rows + 1
        else:
            im_cols = im_rows
        OC = num_dict / im_size

        img = tile_raster_images(Exp_RF.T,
                                 img_shape=(side, side),
                                 tile_shape=(im_rows, im_cols),
                                 tile_spacing=(1, 1),
                                 scale_rows_to_unit_interval=True,
                                 output_pixel_vals=True)
        fig = plt.figure()
        plt.title('Experimental Receptive Fields Layer ' + str(layer))
        plt.axis('off')
        plt.imsave(self.directory + '/Images/RFs/Exp_RF_' + str(layer) +
                   '.png',
                   img,
                   cmap=plt.cm.Greys)
        plt.close(fig)
Example #14
0
    def visualise_weight(self, rbm, image_name):
            assert rbm.associative
            if rbm.v_n in [784, 625, 2500, 5000]:
                plotting_start = time.clock()  # Measure plotting time

                w = rbm.W.get_value(borrow=True).T
                u = rbm.U.get_value(borrow=True).T

                weight = np.hstack((w, u))

                tile_shape = (rbm.h_n / 10 + 1, 10)

                image = Image.fromarray(
                    utils.tile_raster_images(
                        X=weight,
                        img_shape=(self.img_shape[0] *2, self.img_shape[1]),
                        tile_shape=tile_shape,
                        tile_spacing=(1, 1)
                    )
                )
                image.save(image_name)

                plotting_end = time.clock()
                return plotting_end - plotting_start
            return 0
Example #15
0
    def visualize_filters(self):
        W = self.W.eval()
        patchSize = np.sqrt(W.shape[0])

        return tile_raster_images(X=W.T, img_shape=(patchSize, patchSize), tile_shape=(20,20), tile_spacing=(0, 0),
                       scale_rows_to_unit_interval=True,
                       output_pixel_vals=True)
Example #16
0
def display(M, side_i, side_t):
    image = Image.fromarray(
        tile_raster_images(M,
                           img_shape=(side_i, side_i),
                           tile_shape=(side_t, side_t),
                           tile_spacing=(2, 2)))
    image.show()
Example #17
0
    def Plot_RF(self, network_Q=None, layer=0, filenum=''):
        if network_Q != None:
            Q = network_Q[layer].get_value()
            filenum = str(filenum)
            function = ''
        else:
            Q = self.network.Q[layer]
            function = self.network.parameters.function
        im_size, num_dict = Q.shape

        side = int(np.round(np.sqrt(im_size)))
        im_rows = int(np.sqrt(num_dict))
        if im_rows**2 < num_dict:
            im_cols = im_rows+1
        else:
            im_cols = im_rows
        OC = num_dict/im_size

        img = tile_raster_images(Q.T, img_shape=(side, side),
                                 tile_shape=(im_rows, im_cols), tile_spacing=(1, 1),
                                 scale_rows_to_unit_interval=True, output_pixel_vals=True)
        fig = plt.figure()
        plt.title('Receptive Fields' + filenum)
        plt.axis('off')
        plt.imsave(self.directory + '/Images/RFs/Receptive_Fields'+function+filenum+'.png', img, cmap=plt.cm.Greys)
        plt.close(fig)
Example #18
0
def train_da(da, index, x, train_set_x, fig = 'filters_corruption_30.png', corruption_level = 0.3, learning_rate = 0.1, training_epochs = 15, batch_size=20):

    cost, updates = da.get_cost_updates(corruption_level = corruption_level, learning_rate = learning_rate)
    train_da = theano.function([index], cost, updates = updates,
                givens = {x : train_set_x[index * batch_size : (index + 1) * batch_size]})

    n_train_batches = train_set_x.get_value(borrow=True).shape[0] / batch_size

    start_time = timeit.default_timer()

    for epoch in xrange(training_epochs):
        c = []
        for batch_index in xrange(n_train_batches):
            c.append(train_da(batch_index))
        print 'train epoch %d cost' %epoch, numpy.mean(c)

    end_time = timeit.default_timer()
    print 'train time %.2f m' %((end_time - start_time) / 60)

    img_size = numpy.ceil( numpy.sqrt( da.W.get_value(borrow = True).shape[0]))

    image = Image.fromarray(tile_raster_images(
        X=da.W.get_value(borrow=True).T,
        img_shape=(img_size, img_size), tile_shape=(10, 10),
        tile_spacing=(1, 1)))
    image.save(location + fig)

    return (end_time - start_time)
Example #19
0
    def sample(self, input=None, n_chains=20, n_samples=10):
        number_of_test_samples = input.get_value(borrow=True).shape[0]
        test_idx = self.numpy_rng.randint(number_of_test_samples - n_chains)
        persistent_vis_chain = theano.shared(
            np.asarray(input.get_value(borrow=True)[test_idx:test_idx +
                                                    n_chains],
                       dtype=theano.config.floatX))

        plot_every = 1000
        ([presig_hids,hid_mfs,hid_samples,presig_vis,vis_mfs,vis_samples],updates) = \
        theano.scan(self.gibbs_vhv,outputs_info=[None, None, None, None, None, persistent_vis_chain],n_steps=plot_every,name="gibbs_vhv")

        updates.update({persistent_vis_chain: vis_samples[-1]})
        sample_fn = theano.function([], [vis_mfs[-1], vis_samples[-1]],
                                    updates=updates,
                                    name='sample_fn')

        image_data = np.zeros((29 * n_samples + 1, 29 * n_chains - 1),
                              dtype='uint8')
        for idx in range(n_samples):
            vis_mf, vis_sample = sample_fn()
            print(' ... plotting sample %d' % idx)
            image_data[29 * idx:29 * idx + 28, :] = tile_raster_images(
                X=vis_mf,
                img_shape=(28, 28),
                tile_shape=(1, n_chains),
                tile_spacing=(1, 1))

        image = Image.fromarray(image_data)
        image.save('samples.png')
        os.chdir('../')
Example #20
0
def test_cA(learning_rate=0.01, training_epochs=20,
            dataset='../datasets/mnist.pkl.gz',
            batch_size=10, output_folder='cA_plots', contraction_level=.1):

    datasets = load_data(dataset)
    
    train_set_x, train_set_y = datasets[0]
    
    n_train_batches = train_set_x.get_value(borrow=True).shape[0] 
    n_train_batches /= batch_size

    index = T.lscalar() 
    x = T.matrix('x')

    if not os.path.isdir(output_folder):
        os.makedirs(output_folder)
    os.chdir(output_folder)

    rng = numpy.random.RandomState(123)

    ca = cA(numpy_rng=rng, input=x,
            n_visible=28 * 28, n_hidden=500, n_batchsize=batch_size)

    cost, updates = ca.get_cost_updates(contraction_level=contraction_level,
                                        learning_rate=learning_rate)

    train_ca = theano.function(
        [index],
        [T.mean(ca.L_rec), ca.L_jacob],
        updates=updates,
        givens={
            x: train_set_x[index * batch_size: (index + 1) * batch_size]
        }
    )

    start_time = timeit.default_timer()

    for epoch in xrange(training_epochs):
        c = []
        for batch_index in xrange(n_train_batches):
            c.append(train_ca(batch_index))

        c_array = numpy.vstack(c)
        print 'Training epoch %d, reconstruction cost ' % epoch, numpy.mean(
            c_array[0]), ' jacobian norm ', numpy.mean(numpy.sqrt(c_array[1]))

    end_time = timeit.default_timer()

    training_time = (end_time - start_time)

    print >> sys.stderr, ('The code for file ' + os.path.split(__file__)[1] +
                          ' ran for %.2fm' % ((training_time) / 60.))
    image = Image.fromarray(tile_raster_images(
        X=ca.W.get_value(borrow=True).T,
        img_shape=(28, 28), tile_shape=(10, 10),
        tile_spacing=(1, 1)))

    image.save('cae_filters.png')

    os.chdir('../')
Example #21
0
def visualize(best_params, best_samples, alg_name, tile_shape_sample, filter_shape=(10,10), tile_shape_J = (10,10), spacing = (1,1)):
    J = best_params
    J_inv = linalg.pinv(J) 
    n_sample = best_samples.shape[0]
    save_name = '-' + 'nsamples' + str(n_sample) + '-'+ alg_name + '.png'
    receptive_field = utils.tile_raster_images(J.T, filter_shape,tile_shape_J, spacing)
    image_rf = Image.fromarray(receptive_field)
    rf_name = 'J' + save_name
    image_rf.save(rf_name)     
    receptive_field_inv = utils.tile_raster_images(J_inv, filter_shape,tile_shape_J, spacing)
    image1 = Image.fromarray(receptive_field_inv)  
    rf_inv_name = 'J_inv' + save_name
    image1.save(rf_inv_name)     
    samples_vis = utils.tile_raster_images(best_samples, filter_shape,tile_shape_sample, spacing)  
    samples_vis_image = Image.fromarray(samples_vis)
    samples_vis_image.save('representative samples.png')
Example #22
0
def train_dA(learning_rate=0.1, training_epochs=250, batch_size=30):
    d = load_data_chunk()
    train_x, train_y = d[0]
    
    n_train_batches = train_x.get_value(borrow=True).shape[0] / batch_size
    index = T.lscalar() # index to a [mini]batch
    x = T.matrix('x')  # the data is presented as rasterized images    

    rng = numpy.random.RandomState(123)
    theano_rng = RandomStreams(rng.randint(2 ** 30))    
    image_size = train_x.get_value(borrow=True).shape[1]
    da = dA(numpy_rng=rng, theano_rng=theano_rng, input=x,
            n_visible=image_size, n_hidden=500)

    cost, updates = da.get_cost_updates(corruption_level=0.3,
                                        learning_rate=learning_rate)

    train_da = theano.function([index], cost, updates=updates,
         givens={x: train_x[index * batch_size:(index + 1) * batch_size]})

    for epoch in xrange(training_epochs):
        c = []
        for batch_index in xrange(n_train_batches):
            c.append(train_da(batch_index))
        print 'Training epoch %d, cost ' % epoch, numpy.mean(c)        
        
    image = PIL.Image.fromarray(tile_raster_images(
        X=da.W.get_value(borrow=True).T,
        img_shape=(IMAGE_WIDTH, IMAGE_HEIGHT), tile_shape=(10, 10),
        tile_spacing=(1, 1)))
    image.save('filters_corruption_30.png')
Example #23
0
def show_image(path, n_w, img_shape, tile_shape):
    image = Image.fromarray(
        tile_raster_images(X=n_w.T,
                           img_shape=img_shape,
                           tile_shape=tile_shape,
                           tile_spacing=(1, 1)))
    image.save(path)
Example #24
0
def sample_DBN():
    persistent_vis_chain = theano.shared(
        numpy.asarray(test_set_x.get_value(borrow=True)[test_idx : test_idx + n_chains], dtype=theano.config.floatX)
    )

    plot_every = 1000
    ([presig_hids, hid_mfs, hid_samples, presig_vis, vis_mfs, vis_samples], updates) = theano.scan(
        rbm.gibbs_vhv, outputs_info=[None, None, None, None, None, persistent_vis_chain], n_steps=plot_every
    )

    updates.update({persistent_vis_chain: vis_samples[-1]})
    sample_fn = theano.function([], [vis_mfs[-1], vis_samples[-1]], updates=updates, name="sample_fn")

    image_data = numpy.zeros((5 * n_samples + 1, 5 * n_chains - 1), dtype="uint8")
    for idx in xrange(n_samples):

        vis_mf, vis_sample = sample_fn()
        print " ... plotting sample ", idx
        image_data[5 * idx : 5 * idx + 4, :] = tile_raster_images(
            X=vis_mf, img_shape=(4, 4), tile_shape=(1, n_chains), tile_spacing=(1, 1)
        )

    image = Image.fromarray(image_data)
    image.save("samples.png")
    os.chdir("../")
Example #25
0
    def Plot_RF(self, network_Q=None, layer=0, filenum=''):
        if network_Q != None:
            Q = network_Q[layer].get_value()
            filenum = str(filenum)
            function = ''
        else:
            Q = self.network.Q[layer]
            function = self.network.parameters.function
        im_size, num_dict = Q.shape

        side = int(np.round(np.sqrt(im_size)))
        im_rows = int(np.sqrt(num_dict))
        if im_rows**2 < num_dict:
            im_cols = im_rows + 1
        else:
            im_cols = im_rows
        OC = num_dict / im_size

        img = tile_raster_images(Q.T,
                                 img_shape=(side, side),
                                 tile_shape=(im_rows, im_cols),
                                 tile_spacing=(1, 1),
                                 scale_rows_to_unit_interval=True,
                                 output_pixel_vals=True)
        fig = plt.figure()
        plt.title('Receptive Fields' + filenum)
        plt.axis('off')
        plt.imsave(self.directory + '/Images/RFs/Receptive_Fields' + function +
                   filenum + '.png',
                   img,
                   cmap=plt.cm.Greys)
        plt.close(fig)
Example #26
0
 def saveImage(self, fname):
     z = self.W.T if type(self.W) == np.ndarray else self.W.eval().T
     a = int(np.sqrt(self.inputShape))
     b = int(np.sqrt(self.outputShape))
     image = PIL.Image.fromarray(tile_raster_images(
             X=z,
             img_shape=(a, a), tile_shape=(b, b),
             tile_spacing=(1, 1)))
     image.save(fname)
Example #27
0
File: rbm.py Project: phquanta/RBM
    def save_pcd_fantasy(self, ind):

        image = Image.fromarray(
            tile_raster_images(X=self.vArr,
                               img_shape=(28, 28),
                               tile_shape=(25, 20),
                               tile_spacing=(1, 1)))

        image.save("pcd_fantasy_step_%d.png" % ind)
Example #28
0
File: rbm.py Project: phquanta/RBM
    def save_weights_visual(self, ind):

        image = Image.fromarray(
            tile_raster_images(X=(self.rbm_w).T,
                               img_shape=(28, 28),
                               tile_shape=(25, 20),
                               tile_spacing=(1, 1)))

        image.save("rbm_%d.png" % ind)
Example #29
0
def test_dA(learning_rate=0.1, training_epochs=15,
			dataset='./data/mnist.pkl.gz',
			batch_size=20, output_folder='dA_plots'):
	
	datasets = load_data(dataset)
	train_set_x, train_set_y = datasets[0]

	n_train_batches = train_set_x.get_value(borrow=True).shape[0] / batch_size

	index = T.lscalar()
	x = T.matrix('x')

	if not os.path.isdir(output_folder):
		os.makedirs(output_folder)
	os.chdir(output_folder)

	rng = numpy.random.RandomState(123)
	theano_rng = RandomStreams(rng.randint(2 ** 30))

	da = dA(numpy_rng=rng, theano_rng=theano_rng, input=x,
			n_visible=28*28, n_hidden=500)

	cost, updates = da.get_cost_updates(corruption_level=0.,
										learning_rate=learning_rate)

	train_da = theano.function([index], cost, updates=updates,
		givens={x: train_set_x[index * batch_size:
							   (index + 1) * batch_size]})

	start_time = time.clock();

	# training
	for epoch in xrange(training_epochs):
		c = []
		for batch_index in xrange(n_train_batches):
			c.append(train_da(batch_index))

		print 'Training epoch %d, cost ' % epoch, numpy.mean(c)

	end_time = time.clock()

	training_time = (end_time - start_time)

	print >> sys.stderr, ('The no corruption code for file ' +
						  os.path.split(__file__)[1] + 
						  ' ran for %.2fm' % ((training_time) / 60.))

	image = PIL.Image.fromarray(
		tile_raster_images(X=da.W.get_value(borrow=True).T,
						   img_shape=(28, 28), tile_shape=(10, 10),
						   tile_spacing=(1, 1)))

	image.save('filters_corruption_0.png')

	# training with corruption_level is 30% ......

	os.chdir('../')
Example #30
0
 def tmp(nn_, train_history):
     epoch = train_history[-1]['epoch']
     
     reco = nn_.predict(data)
     
     Image.fromarray(tile_raster_images(X=reco.reshape(data.shape[0], -1),
                                img_shape=(data.shape[2], data.shape[3]), tile_shape=(tile_size, tile_size),
                                tile_spacing=(1, 1), scale_rows_to_unit_interval=True
                                )).save(os.path.join(outputdir, "reco_%04d.png" % epoch))
def plot(data, Urs, Ua, dwhite):
    '''
    plot the pictures results.
    '''
    image = Image.fromarray(
        tile_raster_images(
            X=data[:100],
            img_shape=(28, 28),
            tile_shape=(10, 10),
            tile_spacing=(1, 1)
        )
    )
    image.save('original.png')

    image = Image.fromarray(
        tile_raster_images(
            X=dwhite[:100],
            img_shape=(28, 28),
            tile_shape=(10, 10),
            tile_spacing=(1, 1)
        )
    )
    image.save('whitened.png')

    for i in range(4):
        zimage = Image.fromarray(
            tile_raster_images(
                X=Urs[i][:100],
                img_shape=(28, 28),
                tile_shape=(10, 10),
                tile_spacing=(1, 1)
            )
        )
        zimage.save('reduced_k%i.png' % i)

        uimage = Image.fromarray(
            tile_raster_images(
                X=Ua[i][:100],
                img_shape=(28, 28),
                tile_shape=(10, 10),
                tile_spacing=(1, 1)
            )
        )
        uimage.save('reconstructed_k%i.png' % i)
Example #32
0
def test_autoencoder():
    learning_rate = 0.1
    training_epochs = 30
    batch_size = 20

    datasets = load_data('data/mnist.pkl.gz')

    train_set_x = datasets[0][0]

    # ミニバッチの数(教師データをbatch数で割るだけ)
    n_train_batches = train_set_x.get_value(borrow=True).shape[0] / batch_size

    # ミニバッチのindexシンボル
    index = T.lscalar()

    # ミニバッチの学習データシンボル
    x = T.matrix('x')

    rng = np.random.RandomState(123)
    theano_rng = RandomStreams(rng.randint(2 ** 30))

    # autoencoder モデル
    da = dA(numpy_rng=rng, theano_rng=theano_rng, input=x, n_visible=28*28, n_hidden=500)

    # コスト関数と更新式のシンボル
    cost, updates = da.get_cost_updates(corruption_level=0.0, learning_rate=learning_rate)

    # trainingの関数
    train_da = theano.function([index], cost, updates=updates, givens={
            x : train_set_x[index*batch_size : (index+1)*batch_size]
        })

    fp = open("log/ae_cost.txt", "w")

    # training
    start_time = time.clock()
    for epoch in xrange(training_epochs):
        c = []
        for batch_index in xrange(n_train_batches):
            c.append(train_da(batch_index))
        print 'Training epoch %d, cost ' % epoch, np.mean(c)
        fp.write('%d\t%f\n' % (epoch, np.mean(c)))

    end_time = time.clock()

    training_time = (end_time - start_time)

    fp.close()

    print "The no corruption code for file " + os.path.split(__file__)[1] + " ran for %.2fm" % ((training_time / 60.0))
    
    image = Image.fromarray(tile_raster_images(
    X=da.W.get_value(borrow=True).T,
    img_shape=(28, 28), tile_shape=(10, 10),
    tile_spacing=(1, 1)))
    image.save('log/dae_filters_corruption_00.png')
Example #33
0
def imageArray2(data,resolution,tileShape=(10,10)):
    try:
        import PIL.Image as Image
    except ImportError:
        import Image
    from utils import tile_raster_images
    image = Image.fromarray(tile_raster_images(X=self.W.get_value(borrow=True).T,
                           img_shape=resolution, tile_shape=tileShape,
                           tile_spacing=(1, 1)))
    return image
Example #34
0
def makeImage(data,resolution,tileShape):
    from utils import tile_raster_images
    try:
        import PIL.Image as Image
    except ImportError:
        import Image
    return Image.fromarray(
        tile_raster_images(X=data,
                           img_shape=resolution, tile_shape=tileShape,
                           tile_spacing=(1, 1)))
Example #35
0
def plot(element, dataset = 'mnist.pkl.gz'):
    autoencoder = pickle.load(open(os.path.join(os.path.split(__file__)[0], 'autoencoder.pkl')))
    if element == 'reconstructions':
        print('... plot reconstructions')

        datasets = load_data(dataset)
        test_set_x, test_set_y   = datasets[2]

        rec = theano.function([autoencoder.x], autoencoder.z)
        image = Image.fromarray(tile_raster_images(X = rec(test_set_x[:100]), img_shape = (28, 28), tile_shape= (5, 20), tile_spacing=(1, 1)))
        image.save(os.path.join(os.path.split(__file__)[0], 'autoencoderrec.png'))
    elif element == 'repflds':
        print('... plot receptive fields')
        image = Image.fromarray(tile_raster_images(X=autoencoder.W.get_value(borrow = True).T, img_shape = (28, 28), tile_shape = (5, 20), tile_spacing=(1, 1)))
        image.save(os.path.join(os.path.split(__file__)[0],'autoencoderfilter.png'))
    else:
        print("dot't know how to plot %" % element) 
        print("either use 'reconstructions' or 'repflds'") 
        return -1
Example #36
0
def main():
    whiten = False
    if len(sys.argv) > 1 and sys.argv[1] == '--whiten':
        whiten = True
        del sys.argv[1]

    if len(sys.argv) <= 3:
        print 'Usage: %s pcaDims n_hidden learningRate' % sys.argv[0]
        sys.exit(1)

    # loads data like datasets = ((train_x, train_y), ([], None), (test_x, None))
    datasets = loadUpsonData('../data/upson_rovio_1/train_15_50000.pkl.gz',
                             '../data/upson_rovio_1/test_15_50000.pkl.gz')
    img_dim = 15  # must match actual size of training data

    print 'done loading.'

    pcaDims = int(sys.argv[1])
    pca = PCA(datasets[0][0])  # train
    datasets[0][0] = pca.toPC(datasets[0][0], pcaDims, whiten=whiten)  # train
    datasets[1][0] = pca.toPC(
        datasets[1][0], pcaDims,
        whiten=whiten) if len(datasets[1][0]) > 0 else array([])  # valid
    datasets[2][0] = pca.toPC(datasets[2][0], pcaDims, whiten=whiten)  # test
    print 'reduced by PCA to'
    print('(%d, %d, %d) %d dimensional examples in (train, valid, test)' %
          (datasets[0][0].shape[0], datasets[1][0].shape[0],
           datasets[2][0].shape[0], datasets[0][0].shape[1]))

    # plot mean and principle components
    image = Image.fromarray(
        tile_raster_images(X=pca.meanAndPc(pcaDims).T,
                           img_shape=(img_dim, img_dim),
                           tile_shape=(10, 10),
                           tile_spacing=(1, 1)))
    image.save(os.path.join(resman.rundir, 'meanAndPc.png'))

    # plot fractional stddev in PCA dimensions
    pyplot.semilogy(pca.fracStd, 'bo-')
    if pcaDims is not None:
        pyplot.axvline(pcaDims)
    pyplot.savefig(os.path.join(resman.rundir, 'fracStd.png'))
    pyplot.clf()

    test_rbm(datasets=datasets,
             training_epochs=45,
             img_dim=img_dim,
             n_input=pcaDims if pcaDims else img_dim * img_dim,
             n_hidden=int(sys.argv[2]),
             learning_rate=float(sys.argv[3]),
             output_dir=resman.rundir,
             quickHack=False,
             visibleModel='real',
             initWfactor=.01,
             imgPlotFunction=lambda xx: pca.fromPC(xx, unwhiten=whiten))
Example #37
0
 def tmp(nn_, train_history):
     epoch = train_history[-1]['epoch']
     
     filter_ = lyr.W.get_value(True).T
     filter_ = filter_.reshape((filter_.shape[0], img_shape, img_shape, -1))
     
     for f in xrange(filter_.shape[-1]):
         Image.fromarray(tile_raster_images(X=filter_[..., f].reshape((filter_.shape[0], -1)),
                                    img_shape=(img_shape, img_shape), tile_shape=(tile_size, tile_size),
                                    tile_spacing=(1, 1), scale_rows_to_unit_interval=True
                                    )).save(os.path.join(outputdir, "weights_n%04d_f%d.png" % (epoch, f)))
Example #38
0
 def tmp(nn_, train_history):
     epoch = train_history[-1]['epoch']
      
      
     resp = get_output(lyr, data, deterministic=True).eval()
     tile_size = int( numpy.sqrt(resp.shape[1]))
     for d in xrange(resp.shape[0]):
         Image.fromarray(tile_raster_images(X=resp[d,...].reshape((resp.shape[1], -1)),
                                    img_shape=(resp.shape[2], resp.shape[3]), tile_shape=(tile_size, tile_size),
                                    tile_spacing=(1, 1), scale_rows_to_unit_interval=True
                                    )).save(os.path.join(outputdir, "resp_%04d_d%d.png" % (epoch,d)))
Example #39
0
def write_patch_examples_unsupervised(datasets):
    patches = datasets[0][0].get_value()
    ps = np.sqrt(np.shape(patches)[1])
    image = PIL.Image.fromarray(
        tile_raster_images(X=patches,
                           img_shape=(ps, ps),
                           tile_shape=(10, 10),
                           tile_spacing=(1, 1)))

    image.save("some_originalExamples.png")

    patches = datasets[0][1].get_value()
    ps = np.sqrt(np.shape(patches)[1])
    image = PIL.Image.fromarray(
        tile_raster_images(X=patches,
                           img_shape=(ps, ps),
                           tile_shape=(10, 10),
                           tile_spacing=(1, 1)))

    image.save("some_noisyExamples.png")
def plotExamples(data, number = 9, title=""):
    title = title + "Example_data"
    print("Plotting " + title)
    image_size = np.floor(np.sqrt(data.shape[1]))
    subplot_size = int(np.floor(np.sqrt(number)))
    raster = tile_raster_images(
        X=data,
        img_shape=(image_size, image_size), tile_shape=(subplot_size, subplot_size),
        tile_spacing=(1, 1))
    image = Image.fromarray(raster)
    image.save("plots/" + datetime.now().strftime("%Y%m%dT%H%M%S") + "_" + title + '.png')
Example #41
0
def my_test():
    datasets = load_data('../data/mnist.pkl.gz')
    train_set_x, train_set_y = datasets[0]
    image_data = numpy.zeros((29 + 1, 29 * 10 - 1), dtype='uint8')
    image_data[:, :, :] = tile_raster_images(
        X=train_set_x.get_value(borrow=True)[0:10],
        img_shape=(28, 28),
        tile_shape=(1, 10),
        tile_spacing=(1, 1))
    image = PIL.Image.fromarray(image_data)
    image.save('samples.png')
Example #42
0
def plot_gabors(gabors):
    n, m = gabors.shape
    side = int(np.round(np.sqrt(m)))
    w = int(np.sqrt(n))
    if w ** 2 < n:
        h = w + 1
    else:
        h = w
    img = tile_raster_images(gabors, (side, side), (w, h), (2, 2))
    plt.imshow(img, interpolation="nearest", cmap="gray")
    plt.show()
Example #43
0
def drawimg(da, set, dataClass, letter):
    y = da.get_hidden_values(set.get_value(borrow=True))
    z = da.get_reconstructed_input(y)

    error = calcost(numpy.array(set.get_value(borrow=True)), numpy.array(z.eval()))

    print(letter + '_' + dataClass + ' loss  = ' + str(error))

    image = Image.fromarray(
        tile_raster_images(X=(set.get_value(borrow=True) * 500),
                           img_shape=(28, 28), tile_shape=(10, 10),
                           tile_spacing=(1, 1)))
    image.save(dataClass + '_' + letter + '.png')


    image = Image.fromarray(
        tile_raster_images(X=(z.eval() * 500),
                           img_shape=(28, 28), tile_shape=(10, 10),
                           tile_spacing=(1, 1)))
    image.save(dataClass + '_' + letter + '_predict.png')
Example #44
0
 def construct_W_image(self):
     # Construct image from the weight matrix
     f, s = largest_factors(self.n_hidden)
     return Image.fromarray(
         tile_raster_images(
             X=self.rbm.W.get_value(borrow=True).T,
             img_shape=(28, 28),
             tile_shape=(f, s),
             tile_spacing=(1, 1)
         )
     )
Example #45
0
def my_test():
    datasets = load_data('../data/mnist.pkl.gz')
    train_set_x, train_set_y = datasets[0]
    image_data = numpy.zeros((29 + 1, 29 * 10 - 1),
                             dtype='uint8')
    image_data[:,:,:] = tile_raster_images(X = train_set_x.get_value(borrow=True)[0:10],
                                           img_shape = (28,28),
                                           tile_shape = (1, 10),
                                           tile_spacing = (1, 1))
    image = PIL.Image.fromarray(image_data)
    image.save('samples.png')
Example #46
0
def plot_gabors(gabors):
    n, m = gabors.shape
    side = int(np.round(np.sqrt(m)))
    w = int(np.sqrt(n))
    if w**2 < n:
        h = w + 1
    else:
        h = w
    img = tile_raster_images(gabors, (side, side), (w, h), (2, 2))
    plt.imshow(img, interpolation='nearest', cmap='gray')
    plt.show()
def plotExamples(data, number=9, title=""):
    title = title + "Example_data"
    print("Plotting " + title)
    image_size = np.floor(np.sqrt(data.shape[1]))
    subplot_size = int(np.floor(np.sqrt(number)))
    raster = tile_raster_images(X=data,
                                img_shape=(image_size, image_size),
                                tile_shape=(subplot_size, subplot_size),
                                tile_spacing=(1, 1))
    image = Image.fromarray(raster)
    image.save("plots/" + datetime.now().strftime("%Y%m%dT%H%M%S") + "_" +
               title + '.png')
def main():
    random.seed(1)
    img_dim = 15     # 10, 15, ...
    datasets = loadUpsonData('../data/upson_rovio_1/train_%d_50000.pkl.gz' % img_dim,
                             '../data/upson_rovio_1/test_%d_50000.pkl.gz'  % img_dim)
    print 'done loading.'
    train_set_x_data, train_set_y = datasets[0]

    pca = PCA(train_set_x_data)
    print 'done PCA.'

    image = Image.fromarray(tile_raster_images(
             X = train_set_x_data,
             img_shape = (img_dim,img_dim),tile_shape = (10,10),
             tile_spacing=(1,1)))
    image.save(os.path.join(resman.rundir, 'samplesData.png'))

    pyplot.figure()
    pyplot.subplot(221); pyplot.semilogy(pca.var);  pyplot.title('pca.var')
    pyplot.subplot(222); pyplot.semilogy(pca.std);  pyplot.title('pca.std')
    pyplot.subplot(223); pyplot.semilogy(pca.fracVar);  pyplot.title('pca.fracVar')
    pyplot.subplot(224); pyplot.semilogy(pca.fracStd);  pyplot.title('pca.fracStd')
    pyplot.savefig(os.path.join(resman.rundir, 'varstd.png'))
    pyplot.close()

    #font = ImageFont.truetype('/usr/share/fonts/truetype/ttf-lyx/cmr10.ttf', 10)
    font = ImageFont.truetype('/usr/share/texmf/fonts/opentype/public/lm/lmmono12-regular.otf', 14)

    def plotImage(xx, filename, str = None):
        arr = tile_raster_images(X = xx,
                                 img_shape = (img_dim,img_dim),tile_shape = (10,10),
                                 tile_spacing=(1,1))
        arrHeight = arr.shape[0]
        if str is not None:
            arr = vstack((arr, zeros((20, arr.shape[1]), dtype = arr.dtype)))
        image = Image.fromarray(arr)
        if str is not None:
            draw = ImageDraw.Draw(image)
            draw.text((2, arrHeight+2), str, 255, font = font)
            draw = ImageDraw.Draw(image)
        image.save(os.path.join(resman.rundir, filename))

    plotImage(pca.pc().T, 'pc.png')

    for dims in [1, 2, 5, 10, 20, 50, 100, 200, 225]:
        plotImage(pca.pcaAndBack(train_set_x_data, dims),
                  'samplesPCA_%03d.png' % dims,
                  'dims=%d' % dims)

        for ee, epsilon in enumerate([0, 1e-4, 1e-3, 1e-2, 1e-1, 1, 2, 5]):
            plotImage(pca.toZca(train_set_x_data, dims, epsilon = epsilon),
                      'samplesZCA_%03d_%02d.png' % (dims, ee),
                      'dims=%d, eps=%s' % (dims, repr(epsilon)))
Example #49
0
def visualize_features(W,W_prev,img_shape=(20,49)):
    # http://deeplearning.stanford.edu/wiki/index.php/Autoencoders_and_Sparsity
    
    W_adjusted = np.dot(W_prev,W)
    neurons = np.reshape(W_adjusted,(img_shape[0],img_shape[1],W_adjusted.shape[1]))
    
    image = Image.fromarray(
                utils.tile_raster_images(X=W_adjusted.T,
                                   img_shape=(img_shape[0],img_shape[1]), tile_shape=(10, 10),
                                   tile_spacing=(1, 1)))
#    plt.imshow(image)
    return image
Example #50
0
def plot_filters(sess, rbm, epoch, dirn='../work'):
    w_T = np.transpose(sess.run(rbm.W))
    image = Image.fromarray(
        tile_raster_images(
            X=w_T,
            # X=rbm.W.get_value(borrow=True).T,
            img_shape=(28, 28),
            tile_shape=(10, 10),
            tile_spacing=(1, 1)))
    fname = 'filters_at_epoch_{}.png'.format(epoch)
    path = os.path.join(dirn, fname)
    image.save(path)
Example #51
0
def filter_visualize_color(params):
    a = open(params,'rb')
    temp = cPickle.load(a)
    w1_c01b = temp[0]
    w1_bc01 = numpy.rollaxis(w1_c01b,3,0)
    w1_shape = w1_bc01.shape
    x = w1_bc01.reshape((w1_shape[0],3,w1_shape[2]*w1_shape[3]))
    x0 = x[:,0,:]
    x1 = x[:,1,:]
    x2 = x[:,2,:]
    image = PIL.Image.fromarray(tile_raster_images(X=(x0,x1,x2,None),img_shape=(w1_shape[2],w1_shape[3]),tile_shape=(w1_shape[0]/8,8),tile_spacing=(1,1)))
    image.save('filters.png')
Example #52
0
def test_autos_l2(corruption=0):
    #load data
    dataset = 'mnist.pkl.gz'
    datasets = load_data(dataset)
    train_set_x, train_set_y = datasets[0]
    valid_set_x, valid_set_y = datasets[1]

    #test against validation set
    n_hiddens = [10, 25, 50, 100]
    x = T.matrix('x')
    rng = numpy.random.RandomState(123)
    theano_rng = RandomStreams(rng.randint(2**30))

    for n_hidden in n_hiddens:
        #load model
        da = dA(numpy_rng=rng,
                theano_rng=theano_rng,
                input=x,
                n_visible=28 * 28,
                n_hidden=n_hidden)
        da.load(
            open(
                '../data/dA_l2/dA_l2_nhid' + str(n_hidden) + '_corr' +
                str(corruption) + '.p', 'r'))

        reconstructed = da.get_reconstructed_input(input=valid_set_x)
        image = Image.fromarray(
            tile_raster_images(X=reconstructed.eval(),
                               img_shape=(28, 28),
                               tile_shape=(10, 10),
                               tile_spacing=(1, 1)))
        image.save('../data/dA_l2/pics/dAs_reconstructed_nhid' +
                   str(da.n_hidden) + '_corr' + str(corruption) + '.png')

    image = Image.fromarray(
        tile_raster_images(X=valid_set_x.get_value(),
                           img_shape=(28, 28),
                           tile_shape=(10, 10),
                           tile_spacing=(1, 1)))
    image.save('../data/dA_l2/pics/original.png')
Example #53
0
def main():
    print '... loading dataset'

    from toolbox import CostMatrixGenerator, MNISTLoader, class_to_example

    loader = MNISTLoader('/home/syang100/datasets')

    train_set, test_set = loader.mnist()
    train_set_x, train_set_y = train_set
    test_set_x, test_set_y = test_set

    cmg = CostMatrixGenerator(train_set_y, 10)
    cost_mat = cmg.general()

    train_set_c = class_to_example(train_set_y, cost_mat)
    test_set_c = class_to_example(test_set_y, cost_mat)

    # model parameters
    # rng_num = np.random.randint(100000)
    rng = np.random.RandomState(123)
    hidden_layer_sizes = [500]
    # pretraining parameters
    pretrain_epochs = 15
    pretrain_learning_rate = 0.1
    pretrain_batch_size = 20
    corruption_levels = [0.25]
    balance_coefs = [100.]
    # finetuneing parameters
    finetune_epochs = 0
    finetune_learning_rate = 0.001
    finetune_batch_size = 1

    reg = CSDNN(numpy_rng=rng,
                n_in=28 * 28,
                hidden_layer_sizes=hidden_layer_sizes,
                n_out=10)

    print '... pre-training model'
    reg.pretrain(train_set=[train_set_x, train_set_y, train_set_c],
                 n_epochs=pretrain_epochs,
                 learning_rate=pretrain_learning_rate,
                 batch_size=pretrain_batch_size,
                 corruption_levels=corruption_levels,
                 balance_coefs=balance_coefs)

    image = Image.fromarray(
        tile_raster_images(X=reg.cA_layers[0].W.get_value(borrow=True).T,
                           img_shape=(28, 28),
                           tile_shape=(10, 10),
                           tile_spacing=(1, 1)))
    image.save('filters_7.png')
    """
Example #54
0
def drawimg(da, set, dataClass, letter):
    y = da.get_hidden_values(set.get_value(borrow=True))
    z = da.get_reconstructed_input(y)

    error = calcost(numpy.array(set.get_value(borrow=True)),
                    numpy.array(z.eval()))

    print(letter + '_' + dataClass + ' loss  = ' + str(error))

    image = Image.fromarray(
        tile_raster_images(X=(set.get_value(borrow=True) * 500),
                           img_shape=(28, 28),
                           tile_shape=(10, 10),
                           tile_spacing=(1, 1)))
    image.save(dataClass + '_' + letter + '.png')

    image = Image.fromarray(
        tile_raster_images(X=(z.eval() * 500),
                           img_shape=(28, 28),
                           tile_shape=(10, 10),
                           tile_spacing=(1, 1)))
    image.save(dataClass + '_' + letter + '_predict.png')
Example #55
0
def visualise_reconstructions(orig, reconstructions, img_shape, plot_n=None, img_name='reconstruction'):
            k = len(reconstructions)
            assert k > 0

            if plot_n:
                data_size = min(plot_n, orig.shape[0])
            else:
                data_size = orig.shape[0]

            if orig.shape[1] in [784, 625, 1250, 784*2, 2500, 5000]:
                nrow, ncol = img_shape

                image_data = np.zeros(
                    ((nrow+1) * (k+1) + 1, (ncol+1) * data_size - 1),
                    dtype='uint8'
                )

                # Original images
                image_data[0:nrow, :] = utils.tile_raster_images(
                    X=orig,
                    img_shape=img_shape,
                    tile_shape=(1, data_size),
                    tile_spacing=(1, 1)
                )

                # Generate image by plotting the sample from the chain
                for i in xrange(1, k+1):
                    # print ' ... plotting sample ', i
                    idx = (nrow+1) * i
                    image_data[idx:(idx+nrow), :] = utils.tile_raster_images(
                        X=(reconstructions[i-1]),
                        img_shape=img_shape,
                        tile_shape=(1, data_size),
                        tile_spacing=(1, 1)
                    )

                # construct image
                image = Image.fromarray(image_data)
                image.save(img_name + '_{}.png'.format(k))
def save_weights(model, epoch):
    if epoch % 5 != 0:
        return
    tile_j = int(numpy.ceil(numpy.sqrt(model.n_hiddens)))
    tile_i = int(numpy.ceil(float(model.n_hiddens) / tile_j))

    W = (model.W.T)

    image = PIL.Image.fromarray(tile_raster_images(
             X = W,
             img_shape = (31, 39), tile_shape = (tile_j, tile_i),
             tile_spacing=(1,1)))
    image.save('weights_%d.png' % epoch)