# HG changeset patch # User SylvainPL # Date 1265297527 18000 # Node ID 3bc75139654a5fe4c40c932d28975b42aeaaee1b # Parent 48a21d19b8eb9197a7456b0b58652c191f474f71 Ajout d'un petit bruit gaussien sur chaque pixel de l'image. diff -r 48a21d19b8eb -r 3bc75139654a transformations/DistorsionGauss.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/transformations/DistorsionGauss.py Thu Feb 04 10:32:07 2010 -0500 @@ -0,0 +1,77 @@ +#!/usr/bin/python +# coding: utf-8 + +''' +Ajout d'une composante aleatoire dans chaque pixel de l'image. +C'est une distorsion gaussienne de moyenne 0 et d'écart type complexity/10 + +Sylvain Pannetier Lebeuf dans le cadre de IFT6266, hiver 2010 + +''' + +import numpy +import random + +class DistorsionGauss(): + + def __init__(self): + self.ecart_type=0.1 #L'ecart type de la gaussienne + + def get_settings_names(self): + return ['ecart_type'] + + def regenerate_parameters(self, complexity): + self.ecart_type=float(complexity)/10 + return self._get_current_parameters() + + def _get_current_parameters(self): + return [] + + def get_parameters_determined_by_complexity(self, complexity): + return [float(complexity)/10] + + def transform_image(self, image): + image=image.reshape(1024,1) + aleatoire=numpy.zeros((1024,1)).astype('float32') + for i in xrange(0,1024): + aleatoire[i]=float(random.gauss(0,self.ecart_type)) + image=image+aleatoire + + #Ramener tout entre 0 et 1 + if numpy.min(image) < 0: + image-=numpy.min(image) + if numpy.max(image) > 1: + image/=numpy.max(image) + + 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 = DistorsionGauss() + 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(1) + +