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 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(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 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(data_file = '', num_epochs=10, batch_size = 128, L=2, z_dim=256, n_hid=1500, binary='false', img_size = 64, init_from = '', save_to='params', split_layer='conv7', pxsh = 0.5, specstr = c.pf_cae_specstr, cae_weights=c.pf_cae_params, deconv_weights = c.pf_deconv_params): binary = binary.lower() == 'true' # pre-trained function for extracting convolutional features from images cae = m.build_cae(input_var=None, specstr=specstr, shape=(img_size,img_size)) laydict = dict((l.name, l) for l in nn.layers.get_all_layers(cae)) convshape = nn.layers.get_output_shape(laydict[split_layer]) convs_from_img, _ = m.encoder_decoder(cae_weights, specstr=specstr, layersplit=split_layer, shape=(img_size, img_size)) # pre-trained function for returning to images from convolutional features img_from_convs = m.deconvoluter(deconv_weights, specstr=specstr, shape=convshape) # Create VAE model print("Building model and compiling functions...") print("L = {}, z_dim = {}, n_hid = {}, binary={}".format(L, z_dim, n_hid, binary)) input_var = T.tensor4('inputs') c,w,h = convshape[1], convshape[2], convshape[3] l_tup = l_z_mu, l_z_ls, l_x_mu_list, l_x_ls_list, l_x_list, l_x = \ m.build_vae(input_var, L=L, binary=binary, z_dim=z_dim, n_hid=n_hid, shape=(w,h), channels=c) if len(init_from) > 0: print("loading from {}".format(init_from)) u.load_params(l_x, init_from) # build loss, updates, training, prediction 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) lr = theano.shared(nn.utils.floatX(1e-5)) params = nn.layers.get_all_params(l_x, trainable=True) updates = nn.updates.adam(loss, params, learning_rate=lr) 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 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) 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, tr_transform=lambda x: data_transform(x[0], do_center=False), te_transform=lambda x: data_transform(x[0], do_center=True)) # generate examples, save training history te_stream = data.streamer(shuffled=True) imb, = next(te_stream.get_epoch_iterator()) orig_feats = data_transform(imb, do_center=True) reconstructed_feats = ae_fn(orig_feats).reshape(orig_feats.shape) orig_feats_deconv = img_from_convs(orig_feats) reconstructed_feats_deconv = img_from_convs(reconstructed_feats) for i in range(reconstructed_feats_deconv.shape[0]): u.get_image_pair(orig_feats_deconv, reconstructed_feats_deconv, index=i, shift=pxsh)\ .save('output_{}.jpg'.format(i)) hist = np.asarray(hist) np.savetxt('vae_convs_train_hist.csv', np.asarray(hist), delimiter=',', fmt='%.5f') u.save_params(l_x, os.path.join(save_to, 'vae_convs_{}.npz'.format(hist[-1,-1])))
def main(data_file='', num_epochs=10, batch_size=128, L=2, z_dim=256, n_hid=1500, binary='false', img_size=64, init_from='', save_to='params', split_layer='conv7', pxsh=0.5, specstr=c.pf_cae_specstr, cae_weights=c.pf_cae_params, deconv_weights=c.pf_deconv_params): binary = binary.lower() == 'true' # pre-trained function for extracting convolutional features from images cae = m.build_cae(input_var=None, specstr=specstr, shape=(img_size, img_size)) laydict = dict((l.name, l) for l in nn.layers.get_all_layers(cae)) convshape = nn.layers.get_output_shape(laydict[split_layer]) convs_from_img, _ = m.encoder_decoder(cae_weights, specstr=specstr, layersplit=split_layer, shape=(img_size, img_size)) # pre-trained function for returning to images from convolutional features img_from_convs = m.deconvoluter(deconv_weights, specstr=specstr, shape=convshape) # Create VAE model print("Building model and compiling functions...") print("L = {}, z_dim = {}, n_hid = {}, binary={}".format( L, z_dim, n_hid, binary)) input_var = T.tensor4('inputs') c, w, h = convshape[1], convshape[2], convshape[3] l_tup = l_z_mu, l_z_ls, l_x_mu_list, l_x_ls_list, l_x_list, l_x = \ m.build_vae(input_var, L=L, binary=binary, z_dim=z_dim, n_hid=n_hid, shape=(w,h), channels=c) if len(init_from) > 0: print("loading from {}".format(init_from)) u.load_params(l_x, init_from) # build loss, updates, training, prediction 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) lr = theano.shared(nn.utils.floatX(1e-5)) params = nn.layers.get_all_params(l_x, trainable=True) updates = nn.updates.adam(loss, params, learning_rate=lr) 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 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) 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, tr_transform=lambda x: data_transform(x[0], do_center=False), te_transform=lambda x: data_transform(x[0], do_center=True)) # generate examples, save training history te_stream = data.streamer(shuffled=True) imb, = next(te_stream.get_epoch_iterator()) orig_feats = data_transform(imb, do_center=True) reconstructed_feats = ae_fn(orig_feats).reshape(orig_feats.shape) orig_feats_deconv = img_from_convs(orig_feats) reconstructed_feats_deconv = img_from_convs(reconstructed_feats) for i in range(reconstructed_feats_deconv.shape[0]): u.get_image_pair(orig_feats_deconv, reconstructed_feats_deconv, index=i, shift=pxsh)\ .save('output_{}.jpg'.format(i)) hist = np.asarray(hist) np.savetxt('vae_convs_train_hist.csv', np.asarray(hist), delimiter=',', fmt='%.5f') u.save_params( l_x, os.path.join(save_to, 'vae_convs_{}.npz'.format(hist[-1, -1])))