# HG changeset patch # User SylvainPL # Date 1265297427 18000 # Node ID 48a21d19b8eb9197a7456b0b58652c191f474f71 # Parent f8a92292b2991be84c28ae05fd856c3a158102ed Effectue une permutation aleatoire de certains pixels choisit avec un voisin diff -r f8a92292b299 -r 48a21d19b8eb transformations/PermutPixel.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/transformations/PermutPixel.py Thu Feb 04 10:30:27 2010 -0500 @@ -0,0 +1,96 @@ +#!/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) + +