view transformations/PermutPixel.py @ 73:859ebd7f8754

fixed float32 return on affine_transform
author Xavier Glorot <glorotxa@iro.umontreal.ca>
date Wed, 10 Feb 2010 13:02:05 -0500
parents 48a21d19b8eb
children 9936c4886299
line wrap: on
line source

#!/usr/bin/python
# coding: utf-8

'''
Un echange de pixels est effectue entre certain pixels choisit aleatoirement
et un de ses 4 voisins, tout aussi choisi aleatoirement.

Le nombre de pixels permutes est definit pas complexity*1024

Sylvain Pannetier Lebeuf dans le cadre de IFT6266, hiver 2010

'''

import numpy
import random

class PermutPixel():
    
    def __init__(self):
        self.nombre=10 #Le nombre de pixels a permuter
        self.proportion=0.3
        
    def get_settings_names(self):
        return ['nombre']

    def regenerate_parameters(self, complexity):
        self.proportion=float(complexity)
        self.nombre=int(256*self.proportion)*4   #Par multiple de 4 (256=1024/4)
        return self._get_current_parameters()

    def _get_current_parameters(self):
        return []  
    
    def get_parameters_determined_by_complexity(self, complexity):
        return [int(complexity*256)*4]
    
    def transform_image(self, image):
        image=image.reshape(1024,1)
        temp=0  #variable temporaire
        #constitution de l'echantillon
        echantillon=random.sample(xrange(0,1024),self.nombre)
        for i in xrange(0,self.nombre,4):
            #gauche
            if echantillon[i] > 0:
                temp=image[echantillon[i]-1]
                image[echantillon[i]-1]=image[echantillon[i]]
                image[echantillon[i]]=temp
            #droite
            if echantillon[i+1] < 1023:
                temp=image[echantillon[i+1]+1]
                image[echantillon[i+1]+1]=image[echantillon[i+1]]
                image[echantillon[i+1]]=temp
            #haut
            if echantillon[i+2] > 31:
                temp=image[echantillon[i+2]-32]
                image[echantillon[i+2]-32]=image[echantillon[i+2]]
                image[echantillon[i+2]]=temp
            #bas
            if echantillon[i+3] < 992:
                temp=image[echantillon[i+3]+32]
                image[echantillon[i+3]+32]=image[echantillon[i+3]]
                image[echantillon[i+3]]=temp
            
            
        return image.reshape((32,32))


#---TESTS---

def _load_image():
    f = open('/home/sylvain/Dropbox/Msc/IFT6266/donnees/lower_test_data.ft')  #Le jeu de donnees est en local. 
    d = ft.read(f)
    w=numpy.asarray(d[random.randint(0,100)])
    return (w/255.0).astype('float')

def _test(complexite):
    img=_load_image()
    transfo = PermutPixel()
    pylab.imshow(img.reshape((32,32)))
    pylab.show()
    print transfo.get_settings_names()
    print transfo.regenerate_parameters(complexite)
    
    img_trans=transfo.transform_image(img)
    
    pylab.imshow(img_trans.reshape((32,32)))
    pylab.show()
    

if __name__ == '__main__':
    from pylearn.io import filetensor as ft
    import pylab
    for i in xrange(0,5):
        _test(0.5)