def main(num_epochs=20): print("Building model and compiling functions...") input_var = T.tensor4('inputs') fcae = build_fcae(input_var) output = nn.layers.get_output(fcae['output']) output_det = nn.layers.get_output(fcae['output'], deterministic=True) loss = nn.objectives.binary_crossentropy(output, input_var).mean() test_loss = nn.objectives.binary_crossentropy(output_det, input_var).mean() # ADAM updates params = nn.layers.get_all_params(fcae['output'], trainable=True) updates = nn.updates.adam(loss, params, learning_rate=1e-3) train_fn = theano.function([input_var], loss, updates=updates) val_fn = theano.function([input_var], test_loss) ae_fn = theano.function([input_var], nn.layers.get_output(fcae['output'])) data = u.DataH5PyStreamer(os.path.join(c.external_data, 'mnist.hdf5'), batch_size=128) hist = u.train_with_hdf5( data, num_epochs=num_epochs, train_fn=train_fn, test_fn=val_fn, max_per_epoch=40, tr_transform=lambda x: u.raw_to_floatX(x[0], pixel_shift=0.), te_transform=lambda x: u.raw_to_floatX(x[0], pixel_shift=0.)) u.save_params(fcae['output'], 'fcae_params_{}.npz'.format(np.asarray(hist)[-1, -1])) from PIL import Image from matplotlib import pyplot as plt streamer = data.streamer() imb = next(streamer.get_epoch_iterator()) batch = u.raw_to_floatX(imb[0], pixel_shift=0.).transpose((0, 1, 3, 2)) orig_dim = 28 im = Image.new("RGB", (orig_dim * 20, orig_dim * 20)) for j in xrange(10): dim = orig_dim orig_im = Image.fromarray( u.get_picture_array(batch, np.random.randint(batch.shape[0]), shift=0.0)) im.paste(orig_im.resize((2 * orig_dim, 2 * orig_dim), Image.ANTIALIAS), box=(0, j * orig_dim * 2)) new_im = {} for i in xrange(9): new_im = orig_im.resize((dim, dim), Image.ANTIALIAS) new_im = ae_fn( u.arr_from_img(new_im, shift=0.).reshape(1, -1, dim, dim)) new_im = Image.fromarray(u.get_picture_array(new_im, 0, shift=0.))\ .resize((orig_dim*2, orig_dim*2), Image.ANTIALIAS) im.paste(new_im, box=((i + 1) * orig_dim * 2, j * orig_dim * 2)) dim = int(dim * 1.2) im.save('increasing_size_autoencoded.jpg')
def main(num_epochs = 20): print("Building model and compiling functions...") input_var = T.tensor4('inputs') fcae = build_fcae(input_var) output = nn.layers.get_output(fcae['output']) output_det = nn.layers.get_output(fcae['output'], deterministic=True) loss = nn.objectives.binary_crossentropy(output, input_var).mean() test_loss = nn.objectives.binary_crossentropy(output_det, input_var).mean() # ADAM updates params = nn.layers.get_all_params(fcae['output'], trainable=True) updates = nn.updates.adam(loss, params, learning_rate=1e-3) train_fn = theano.function([input_var], loss, updates=updates) val_fn = theano.function([input_var], test_loss) ae_fn = theano.function([input_var], nn.layers.get_output(fcae['output'])) data = u.DataH5PyStreamer(os.path.join(c.external_data, 'mnist.hdf5'), batch_size=128) hist = u.train_with_hdf5(data, num_epochs=num_epochs, train_fn = train_fn, test_fn = val_fn, max_per_epoch=40, tr_transform = lambda x: u.raw_to_floatX(x[0], pixel_shift=0.), te_transform = lambda x: u.raw_to_floatX(x[0], pixel_shift=0.)) u.save_params(fcae['output'], 'fcae_params_{}.npz'.format(np.asarray(hist)[-1,-1])) from PIL import Image from matplotlib import pyplot as plt streamer = data.streamer() imb = next(streamer.get_epoch_iterator()) batch = u.raw_to_floatX(imb[0], pixel_shift=0.).transpose((0,1,3,2)) orig_dim = 28 im = Image.new("RGB", (orig_dim*20, orig_dim*20)) for j in xrange(10): dim = orig_dim orig_im = Image.fromarray(u.get_picture_array(batch, np.random.randint(batch.shape[0]), shift=0.0)) im.paste(orig_im.resize((2*orig_dim, 2*orig_dim), Image.ANTIALIAS), box=(0,j*orig_dim*2)) new_im = {} for i in xrange(9): new_im = orig_im.resize((dim, dim), Image.ANTIALIAS) new_im = ae_fn(u.arr_from_img(new_im, shift=0.).reshape(1,-1,dim,dim)) new_im = Image.fromarray(u.get_picture_array(new_im, 0, shift=0.))\ .resize((orig_dim*2, orig_dim*2), Image.ANTIALIAS) im.paste(new_im, box=((i+1)*orig_dim*2, j*orig_dim*2)) dim = int(dim * 1.2) im.save('increasing_size_autoencoded.jpg')
def main(specstr=default_specstr, z_dim=256, num_epochs=10, ch=3, init_from='', img_size=64, pxsh=0.5, data_file='', batch_size=8, save_to='params'): # build expressions for the output, loss, gradient input_var = T.tensor4('inputs') print('building specstr {} - zdim {}'.format(specstr, z_dim)) cae = m.build_cae_nopoolinv(input_var, shape=(img_size,img_size), channels=ch, specstr=specstr.format(z_dim)) l_list = nn.layers.get_all_layers(cae) pred = nn.layers.get_output(cae) loss = nn.objectives.squared_error(pred, input_var.flatten(2)).mean() params = nn.layers.get_all_params(cae, trainable=True) grads = nn.updates.total_norm_constraint(T.grad(loss, params), 10) updates = nn.updates.adam(grads, params, learning_rate=1e-3) te_pred = nn.layers.get_output(cae, deterministic=True) te_loss = nn.objectives.squared_error(te_pred, input_var.flatten(2)).mean() # training functions print('compiling functions') train_fn = theano.function([input_var], loss, updates=updates) val_fn = theano.function([input_var], te_loss) # compile functions for encode/decode to test later enc_layer = l_list[next(i for i in xrange(len(l_list)) if l_list[i].name=='encode')] enc_fn = theano.function([input_var], nn.layers.get_output(enc_layer, deterministic=True)) dec_fn = lambda z: nn.layers.get_output(cae, deterministic=True, inputs={l_list[0]:np.zeros((z.shape[0],ch,img_size,img_size),dtype=theano.config.floatX), enc_layer:z}).eval().reshape(-1,ch,img_size,img_size) # load params if requested, run training if len(init_from) > 0: print('loading params from {}'.format(init_from)) load_params(cae, init_from) data = u.DataH5PyStreamer(data_file, batch_size=batch_size) print('training for {} epochs'.format(num_epochs)) hist = u.train_with_hdf5(data, num_epochs=num_epochs, train_fn = train_fn, test_fn = val_fn, tr_transform=lambda x: u.raw_to_floatX(x[0], pixel_shift=pxsh, center=False), te_transform=lambda x: u.raw_to_floatX(x[0], pixel_shift=pxsh, center=True)) # generate examples, save training history te_stream = data.streamer(shuffled=True) imb, = next(te_stream.get_epoch_iterator()) tg = u.raw_to_floatX(imb, pixel_shift=pxsh, square=True, center=True) pr = dec_fn(enc_fn(tg)) for i in range(pr.shape[0]): u.get_image_pair(tg, pr,index=i,shift=pxsh).save('output_{}.jpg'.format(i)) hist = np.asarray(hist) np.savetxt('cae_train_hist.csv', np.asarray(hist), delimiter=',', fmt='%.5f') u.save_params(cae, os.path.join(save_to, 'cae_{}.npz'.format(hist[-1,-1])))
def transform_data(ims_batch, center=False): imb = u.raw_to_floatX( ims_batch, pixel_shift=pixel_shift, center=center)[np.random.randint(frame_skip)::frame_skip] zbatch = np.zeros((tr_batch_size, seq_length + 1, z_dim), dtype=theano.config.floatX) zsigbatch = np.zeros((tr_batch_size, seq_length + 1, z_dim), dtype=theano.config.floatX) for i in xrange(samples_per_image): chunk = tr_batch_size / samples_per_image if diffs: zf = z_from_img(imb).reshape((chunk, seq_length + 1, -1)) zbatch[i * chunk:(i + 1) * chunk, 1:] = zf[:, 1:] - zf[:, :-1] if kl_loss: zls = z_ls_from_img(imb).reshape( (chunk, seq_length + 1, -1)) zsigbatch[i * chunk:(i + 1) * chunk, 1:] = zls[:, 1:] - zls[:, :-1] else: zbatch[i * chunk:(i + 1) * chunk] = z_from_img(imb).reshape( (chunk, seq_length + 1, -1)) if kl_loss: zsigbatch[i * chunk:(i + 1) * chunk] = z_ls_from_img(imb).reshape( (chunk, seq_length + 1, -1)) if kl_loss: return zbatch[:, : -1, :], zsigbatch[:, : -1, :], zbatch[:, 1:, :], zsigbatch[:, 1:, :] return zbatch[:, :-1, :], zbatch[:, 1:, :]
def transform_data(imb): y, x = imb # data augmentation: flip = -1 if we do flip over y-axis, 1 if not flip = -2 * np.random.binomial(1, p=0.5) + 1 # this vgg-net expects image values that are normalized by mean but not magnitude x = (u.raw_to_floatX(x[:,:,::flip], pixel_shift=0.)\ .transpose(0,1,3,2)[:,::-1] * 255. - mean_values) return conv_feats(x), np.concatenate([zeros, one_hot(y)], axis=1)
def transform_data(imb): y,x = imb # data augmentation: flip = -1 if we do flip over y-axis, 1 if not flip = -2*np.random.binomial(1, p=0.5) + 1 # this vgg-net expects image values that are normalized by mean but not magnitude x = (u.raw_to_floatX(x[:,:,::flip], pixel_shift=0.)\ .transpose(0,1,3,2)[:,::-1] * 255. - mean_values) return conv_feats(x), np.concatenate([zeros, one_hot(y)], axis=1)
def main(L=2, img_size=64, pxsh=0., z_dim=32, n_hid=1024, num_epochs=12, binary='True', init_from='', data_file='', batch_size=128, save_to='params', max_per_epoch=-1): binary = binary.lower()=='true' # Create VAE model input_var = T.tensor4('inputs') print("Building model and compiling functions...") print("L = {}, z_dim = {}, n_hid = {}, binary={}".format(L, z_dim, n_hid, binary)) l_tup = l_z_mu, l_z_ls, l_x_mu_list, l_x_ls_list, l_x_list, l_x = \ m.build_vcae(input_var, L=L, binary=binary, z_dim=z_dim, n_hid=n_hid) if len(init_from) > 0: print('loading from {}'.format(init_from)) load_params(l_x, init_from) # compile functions loss, _ = u.build_vae_loss(input_var, *l_tup, deterministic=False, binary=binary, L=L) test_loss, test_prediction = u.build_vae_loss(input_var, *l_tup, deterministic=True, binary=binary, L=L) params = nn.layers.get_all_params(l_x, trainable=True) updates = nn.updates.adam(loss, params, learning_rate=3e-5) train_fn = theano.function([input_var], loss, updates=updates) val_fn = theano.function([input_var], test_loss) ae_fn = theano.function([input_var], test_prediction) # run training loop print('training for {} epochs'.format(num_epochs)) data = u.DataH5PyStreamer(data_file, batch_size=batch_size) hist = u.train_with_hdf5(data, num_epochs=num_epochs, train_fn=train_fn, test_fn=val_fn, max_per_epoch=max_per_epoch, tr_transform=lambda x: u.raw_to_floatX(x[0], pixel_shift=pxsh, center=False), te_transform=lambda x: u.raw_to_floatX(x[0], pixel_shift=pxsh, center=True)) # generate examples, save training history te_stream = data.streamer(shuffled=True) imb, = next(te_stream.get_epoch_iterator()) orig_images = u.raw_to_floatX(imb, pixel_shift=pxsh) autoencoded_images = ae_fn(orig_images) for i in range(autoencoded_images.shape[0]): u.get_image_pair(orig_images, autoencoded_images, index=i, shift=pxsh) \ .save('output_{}.jpg'.format(i)) hist = np.asarray(hist) np.savetxt('vcae_train_hist.csv', np.asarray(hist), delimiter=',', fmt='%.5f') u.save_params(l_x, os.path.join(save_to, 'vcae_{}.npz'.format(hist[-1,-1])))
def main(batch_size = 128, num_epochs = 30, learning_rate = 1e-4, d_ratio=4): print("Building model and compiling functions...") X = T.tensor4('inputs') Z = T.matrix('Z') ldict = build_nets(input_var = X) def build_loss(deterministic): # this currently has the problem that these 3 expressions come from 3 different # get_output calls, so they won't return the same mask if dropout or other # noise is used. Currently not using dropout so not a problem. ae = nn.layers.get_output(ldict['ae_out'], deterministic=deterministic) disc_real = nn.layers.get_output(ldict['disc_out'], deterministic=deterministic) disc_fake = nn.layers.get_output(ldict['disc_out'], { ldict['disc_in']:ae }, deterministic=deterministic) d_cost_real=nn.objectives.binary_crossentropy(disc_real, T.ones_like(disc_real)).mean() d_cost_fake=nn.objectives.binary_crossentropy(disc_fake, T.zeros_like(disc_fake)).mean() g_cost=nn.objectives.binary_crossentropy(disc_fake, T.ones_like(disc_fake)).mean() d_cost = d_cost_real + d_cost_fake mse = nn.objectives.squared_error(ae, X).mean() return g_cost, d_cost, mse g_cost, d_cost, _ = build_loss(deterministic=False) g_cost_det, d_cost_det, mse = build_loss(deterministic=True) d_lr = theano.shared(nn.utils.floatX(learning_rate)) d_params = nn.layers.get_all_params(ldict['disc_out'], trainable=True) d_updates = nn.updates.adam(d_cost, d_params, learning_rate=d_lr) g_lr = theano.shared(nn.utils.floatX(learning_rate)) g_params = nn.layers.get_all_params(ldict['ae_out'], trainable=True) g_updates = nn.updates.adam(g_cost, g_params, learning_rate=g_lr) _train_g = theano.function([X], g_cost, updates=g_updates) _train_d = theano.function([X], d_cost, updates=d_updates) _test_g = theano.function([X], g_cost_det) _test_d = theano.function([X], d_cost_det) mse = theano.function([X], mse) print("Starting training...") data = u.DataH5PyStreamer(os.path.join(c.external_data, 'mnist.hdf5'), batch_size=batch_size) def train_fn(x, d_ratio=d_ratio): cost = 0 for i in xrange(d_ratio): cost += _train_d(x) cost += _train_g(x) return cost transform_data = lambda x: u.raw_to_floatX(x[0], pixel_shift=0.) hist = u.train_with_hdf5(data, num_epochs=num_epochs, train_fn = train_fn, test_fn = mse, tr_transform=transform_data, te_transform=transform_data)
def transform_data(ims_batch, center=False): imb = u.raw_to_floatX(ims_batch, pixel_shift=pixel_shift, center=center)[np.random.randint(frame_skip)::frame_skip] zbatch = np.zeros((tr_batch_size, seq_length+1, z_dim), dtype=theano.config.floatX) zsigbatch = np.zeros((tr_batch_size, seq_length+1, z_dim), dtype=theano.config.floatX) for i in xrange(samples_per_image): chunk = tr_batch_size/samples_per_image if diffs: zf = z_from_img(imb).reshape((chunk, seq_length+1, -1)) zbatch[i*chunk:(i+1)*chunk, 1:] = zf[:,1:] - zf[:,:-1] if kl_loss: zls = z_ls_from_img(imb).reshape((chunk, seq_length+1, -1)) zsigbatch[i*chunk:(i+1)*chunk, 1:] = zls[:,1:] - zls[:,:-1] else: zbatch[i*chunk:(i+1)*chunk] = z_from_img(imb).reshape((chunk, seq_length+1, -1)) if kl_loss: zsigbatch[i*chunk:(i+1)*chunk] = z_ls_from_img(imb).reshape((chunk, seq_length+1, -1)) if kl_loss: return zbatch[:,:-1,:], zsigbatch[:,:-1,:], zbatch[:,1:,:], zsigbatch[:,1:,:] return zbatch[:,:-1,:], zbatch[:,1:,:]
def data_transform(x, do_center): floatx_ims = u.raw_to_floatX(x, pixel_shift=pxsh, square=True, center=do_center) return (conv_feats(floatx_ims), floatx_ims)
def main(specstr=default_specstr, z_dim=256, num_epochs=10, ch=3, init_from='', img_size=64, pxsh=0.5, data_file='', batch_size=8, save_to='params'): # build expressions for the output, loss, gradient input_var = T.tensor4('inputs') print('building specstr {} - zdim {}'.format(specstr, z_dim)) cae = m.build_cae_nopoolinv(input_var, shape=(img_size, img_size), channels=ch, specstr=specstr.format(z_dim)) l_list = nn.layers.get_all_layers(cae) pred = nn.layers.get_output(cae) loss = nn.objectives.squared_error(pred, input_var.flatten(2)).mean() params = nn.layers.get_all_params(cae, trainable=True) grads = nn.updates.total_norm_constraint(T.grad(loss, params), 10) updates = nn.updates.adam(grads, params, learning_rate=1e-3) te_pred = nn.layers.get_output(cae, deterministic=True) te_loss = nn.objectives.squared_error(te_pred, input_var.flatten(2)).mean() # training functions print('compiling functions') train_fn = theano.function([input_var], loss, updates=updates) val_fn = theano.function([input_var], te_loss) # compile functions for encode/decode to test later enc_layer = l_list[next(i for i in xrange(len(l_list)) if l_list[i].name == 'encode')] enc_fn = theano.function([input_var], nn.layers.get_output(enc_layer, deterministic=True)) dec_fn = lambda z: nn.layers.get_output( cae, deterministic=True, inputs={ l_list[0]: np.zeros((z.shape[0], ch, img_size, img_size), dtype=theano.config.floatX), enc_layer: z }).eval().reshape(-1, ch, img_size, img_size) # load params if requested, run training if len(init_from) > 0: print('loading params from {}'.format(init_from)) load_params(cae, init_from) data = u.DataH5PyStreamer(data_file, batch_size=batch_size) print('training for {} epochs'.format(num_epochs)) hist = u.train_with_hdf5(data, num_epochs=num_epochs, train_fn=train_fn, test_fn=val_fn, tr_transform=lambda x: u.raw_to_floatX( x[0], pixel_shift=pxsh, center=False), te_transform=lambda x: u.raw_to_floatX( x[0], pixel_shift=pxsh, center=True)) # generate examples, save training history te_stream = data.streamer(shuffled=True) imb, = next(te_stream.get_epoch_iterator()) tg = u.raw_to_floatX(imb, pixel_shift=pxsh, square=True, center=True) pr = dec_fn(enc_fn(tg)) for i in range(pr.shape[0]): u.get_image_pair(tg, pr, index=i, shift=pxsh).save('output_{}.jpg'.format(i)) hist = np.asarray(hist) np.savetxt('cae_train_hist.csv', np.asarray(hist), delimiter=',', fmt='%.5f') u.save_params(cae, os.path.join(save_to, 'cae_{}.npz'.format(hist[-1, -1])))
def transform_data(imb): # data augmentation: flip = -1 if we do flip over y-axis, 1 if not flip = -2*np.random.binomial(1, p=0.5) + 1 return u.raw_to_floatX(imb[0], pixel_shift=0)[:,:,::flip], imb[1].flatten()
def main(L=2, img_size=64, pxsh=0., z_dim=32, n_hid=1024, num_epochs=12, binary='True', init_from='', data_file='', batch_size=128, save_to='params', max_per_epoch=-1): binary = binary.lower() == 'true' # Create VAE model input_var = T.tensor4('inputs') print("Building model and compiling functions...") print("L = {}, z_dim = {}, n_hid = {}, binary={}".format( L, z_dim, n_hid, binary)) l_tup = l_z_mu, l_z_ls, l_x_mu_list, l_x_ls_list, l_x_list, l_x = \ m.build_vcae(input_var, L=L, binary=binary, z_dim=z_dim, n_hid=n_hid) if len(init_from) > 0: print('loading from {}'.format(init_from)) load_params(l_x, init_from) # compile functions loss, _ = u.build_vae_loss(input_var, *l_tup, deterministic=False, binary=binary, L=L) test_loss, test_prediction = u.build_vae_loss(input_var, *l_tup, deterministic=True, binary=binary, L=L) params = nn.layers.get_all_params(l_x, trainable=True) updates = nn.updates.adam(loss, params, learning_rate=3e-5) train_fn = theano.function([input_var], loss, updates=updates) val_fn = theano.function([input_var], test_loss) ae_fn = theano.function([input_var], test_prediction) # run training loop print('training for {} epochs'.format(num_epochs)) data = u.DataH5PyStreamer(data_file, batch_size=batch_size) hist = u.train_with_hdf5(data, num_epochs=num_epochs, train_fn=train_fn, test_fn=val_fn, max_per_epoch=max_per_epoch, tr_transform=lambda x: u.raw_to_floatX( x[0], pixel_shift=pxsh, center=False), te_transform=lambda x: u.raw_to_floatX( x[0], pixel_shift=pxsh, center=True)) # generate examples, save training history te_stream = data.streamer(shuffled=True) imb, = next(te_stream.get_epoch_iterator()) orig_images = u.raw_to_floatX(imb, pixel_shift=pxsh) autoencoded_images = ae_fn(orig_images) for i in range(autoencoded_images.shape[0]): u.get_image_pair(orig_images, autoencoded_images, index=i, shift=pxsh) \ .save('output_{}.jpg'.format(i)) hist = np.asarray(hist) np.savetxt('vcae_train_hist.csv', np.asarray(hist), delimiter=',', fmt='%.5f') u.save_params(l_x, os.path.join(save_to, 'vcae_{}.npz'.format(hist[-1, -1])))
def data_transform(x, do_center): floatx_ims = u.raw_to_floatX(x, pixel_shift=pxsh, square=True, center=do_center) return convs_from_img(floatx_ims)