view transformations/pipeline.py @ 11:dbc806d025a2

Added a thick.py script defining a Thick class transforming randomly the thickness of the characters
author Xavier Glorot <glorotxa@iro.umontreal.ca>
date Wed, 27 Jan 2010 19:14:37 -0500
parents faacc76d21c2
children f6b6c74bb82f
line wrap: on
line source

from __future__ import with_statement

import sys, os
import numpy
import filetensor as ft
import random

BATCH_SIZE = 100

#import <modules> and stuff them in mods below

mods = []

# DANGER: HIGH VOLTAGE -- DO NOT EDIT BELOW THIS LINE
# -----------------------------------------------------------

train_data = open('/data/lisa/data/nist/by_class/all/all_train_data.ft', 'rb')

dim = tuple(ft._read_header(train_data)[3])

res_data = numpy.empty(dim)

all_settings = ['complexity']

for mod in mods:
    all_settings += mod.get_settings_names()

params = numpy.empty(((dim[0]/BATCH_SIZE)+1, len(all_settings)))

for i in xrange(0, dim[0], BATCH_SIZE):
    train_data.seek(0)
    imgs = ft.read(train_data, slice(i, i+BATCH_SIZE))
    
    complexity = random.random()
    p = i/BATCH_SIZE
    j = 1
    for mod in mods:
        par = mod.regenerate_parameters(complexity)
        params[p, j:j+len(par)] = par
        j += len(par)
    
    for k in range(imgs.shape[0]):
        c = imgs[k]
        for mod in mods:
            c = mod.transform_image(c)
        res_data[i+k] = c

with open(sys.argv[1], 'wb') as f:
    ft.write(f, res_data)

numpy.save(sys.argv[2], params)