def save_network_graph(self, network, filename): """ Creates and saves the network graph image. :param network: the last lasagne layer(s) of a network :param filename: name of the image file to be saved in data_dir """ import os import lasagne import matplotlib matplotlib.use('Agg') from nolearn.lasagne.visualize import draw_to_file file_path = os.path.join(self.data_dir, filename) layers = lasagne.layers.get_all_layers(network) draw_to_file(layers, file_path)
def train(self, it_train, it_val, batch_size, num_epochs, out_dir, model_dir=None, save_every=10, resume=False, quick_run=False): """ Training loop. it_train: training set iterator it_val: validation set iterator batch_size: batch size num_epochs: number of epochs to train out_dir: output directory to log results model_dir: output directory to log saved models save_every: how many epochs should we save the model? resume: if `True`, append to the results file, not overwrite it quick_run: only perform one minibatch per train/valid loop. This is good for fast debugging. """ def _loop(fn, itr): rec = [ [] for i in range(len(self.train_keys)) ] for b in range(itr.N // batch_size): X_batch, Y_batch = it_train.next() #print X_batch.shape, Y_batch.shape Z_batch = floatX(self.sampler(X_batch.shape[0], self.latent_dim)) results = fn(Z_batch,X_batch,Y_batch) for i in range(len(results)): rec[i].append(results[i]) if quick_run: break return tuple( [ np.mean(elem) for elem in rec ] ) header = ["epoch"] for key in self.train_keys: header.append("train_%s" % key) for key in self.train_keys: header.append("valid_%s" % key) header.append("lr") header.append("time") header.append("mode") if not os.path.exists(out_dir): os.makedirs(out_dir) if model_dir != None and not os.path.exists(model_dir): os.makedirs(model_dir) if self.verbose: try: from nolearn.lasagne.visualize import draw_to_file draw_to_file(get_all_layers(self.dcgan['gen']), "%s/gen_dcgan.png" % out_dir, verbose=True) draw_to_file(get_all_layers(self.dcgan['disc']), "%s/disc_dcgan.png" % out_dir, verbose=True) draw_to_file(get_all_layers(self.p2p['gen']), "%s/gen_p2p.png" % out_dir, verbose=True) draw_to_file(get_all_layers(self.p2p['disc']), "%s/disc_p2p.png" % out_dir, verbose=True) except: pass f = open("%s/results.txt" % out_dir, "wb" if not resume else "a") if not resume: f.write(",".join(header)+"\n"); f.flush() print ",".join(header) else: if self.verbose: print "loading weights from: %s" % resume self.load_model(resume) #cb = ReduceLROnPlateau(self.lr,verbose=self.verbose) for e in range(num_epochs): out_str = [] out_str.append(str(e+1)) t0 = time() # training results = _loop(self.train_fn, it_train) for i in range(len(results)): #train_losses[i].append(results[i]) out_str.append(str(results[i])) #if reduce_on_plateau: # cb.on_epoch_end(np.mean(recon_losses), e+1) # validation results = _loop(self.loss_fn, it_val) for i in range(len(results)): #valid_losses[i].append(results[i]) out_str.append(str(results[i])) out_str.append(str(self.lr.get_value())) out_str.append(str(time()-t0)) out_str.append(self.train_mode) out_str = ",".join(out_str) print out_str f.write("%s\n" % out_str); f.flush() if self.train_mode in ['both', 'p2p']: # plot an NxN grid of [A, predict(A)] plot_grid("%s/out_%i.png" % (out_dir,e+1), it_val, self.gen_fn, is_a_grayscale=self.is_a_grayscale, is_b_grayscale=self.is_b_grayscale) # plot big pictures of predict(A) in the valid set self.generate_atob(it_train, 1, "%s/dump_train" % out_dir, deterministic=False) self.generate_atob(it_val, 1, "%s/dump_valid" % out_dir, deterministic=False) if self.train_mode in ['both', 'dcgan']: # plot A generated from G(z) self.generate_gz(num_examples=20, batch_size=batch_size, out_dir="%s/dump_a" % out_dir, deterministic=False) if model_dir != None and (e+1) % save_every == 0: self.save_model("%s/%i.model" % (model_dir, e+1))
def test_draw_to_file_layers(self, net_fitted, tmpdir): from nolearn.lasagne.visualize import draw_to_file fn = str(tmpdir.join('network.pdf')) draw_to_file(net_fitted.get_all_layers(), fn, output_shape=False)
def draw(): global net net.initialize() draw_to_file(net, "layout.png", verbose=True) draw_to_notebook(net)
def test_draw_to_file_layers(self, net_fitted, tmpdir): from nolearn.lasagne.visualize import draw_to_file fn = str(tmpdir.join('network.pdf')) draw_to_file( net_fitted.get_all_layers(), fn, output_shape=False)
def test_draw_to_file_net(self, net_fitted, tmpdir): from nolearn.lasagne.visualize import draw_to_file fn = str(tmpdir.join("network.pdf")) draw_to_file(net_fitted, fn, output_shape=False)