def degree_vaccination(infile, outfile, k): """ Change the state (to VACCINATED) of the top k (%) highly-connected individuals in population contained in infile, and save the altered population into outfile. """ fh = open(infile, "r") params = pickle.load(fh) population = pickle.load(fh) fh.close() net = network.build_network(population, params["network_topology"], params["network_params"]) L = networkx.degree_centrality(net.get_structure()).items() L = sorted(L, key=operator.itemgetter(1), reverse=True) n = int(k / 100.0 * len(population)) for i in range(0, n): person = population[L[i][0]] person.set_state(Player.VACCINATED) sl = person.get_state_list() sl[len(sl) - 1] = Player.VACCINATED person.set_infection(0.0) il = person.get_infection_list() il[len(il) - 1] = 0.0 person.set_c(0.0) cl = person.get_c_list() cl[len(cl) - 1] = 0.0 fh = open(outfile, "w") pickle.dump(params, fh) pickle.dump(population, fh) fh.close()
def degree_vaccination(infile, outfile, k): """ Change the state (to VACCINATED) of the top k (%) highly-connected individuals in population contained in infile, and save the altered population into outfile. """ fh = open(infile, "r") params = pickle.load(fh) population = pickle.load(fh) fh.close() net = network.build_network(population, params["network_topology"], params["network_params"]) L = networkx.degree_centrality(net.get_structure()).items() L = sorted(L, key = operator.itemgetter(1), reverse = True) n = int(k / 100.0 * len(population)) for i in range(0, n): person = population[L[i][0]] person.set_state(Player.VACCINATED) sl = person.get_state_list() sl[len(sl) - 1] = Player.VACCINATED person.set_infection(0.0) il = person.get_infection_list() il[len(il) - 1] = 0.0 person.set_c(0.0) cl = person.get_c_list() cl[len(cl) - 1] = 0.0 fh = open(outfile, "w") pickle.dump(params, fh) pickle.dump(population, fh) fh.close()
def random_vaccination(infile, outfile, k): """ Change the state (to VACCINATED) of k (%) individuals in population contained in infile, and save the altered population into outfile. Each of these individuals is randomly picked. """ fh = open(infile, "r") params = pickle.load(fh) population = pickle.load(fh) fh.close() net = network.build_network(population, params["network_topology"], params["network_params"]) m = int(k / 100.0 * len(population)) i = 0 while i < m: person = net.get_random_vertex() if person == None or person.get_state() == Player.VACCINATED: continue person.set_state(Player.VACCINATED) sl = person.get_state_list() sl[len(sl) - 1] = Player.VACCINATED person.set_infection(0.0) il = person.get_infection_list() il[len(il) - 1] = 0.0 person.set_c(0.0) cl = person.get_c_list() cl[len(cl) - 1] = 0.0 i += 1 fh = open(outfile, "w") pickle.dump(params, fh) pickle.dump(population, fh) fh.close()
def train(args, params): print("Loading training set...") train = load.load_dataset(params['train']) print("Loading dev set...") dev = load.load_dataset(params['dev']) print("Building preprocessor...") preproc = load.Preproc(*train) print("train_set_classes:", preproc.classes) print("Training size: " + str(len(train[0])) + " examples.") print("Dev size: " + str(len(dev[0])) + " examples.") save_dir = make_save_dir(params['save_dir'], args.experiment) util.save(preproc, save_dir) params.update({ "input_shape": [None, 1], "num_categories": len(preproc.classes) }) model = network.build_network(**params) stopping = keras.callbacks.EarlyStopping(patience=30) reduce_lr = keras.callbacks.ReduceLROnPlateau( factor=0.1, patience=2, min_lr=params["learning_rate"] * 0.001) checkpointer = keras.callbacks.ModelCheckpoint( filepath=get_filename_for_saving(save_dir), save_best_only=False) batch_size = params.get("batch_size", 32) # summary = str(model.summary(print_fn=lambda x: fh.write(x + '\n'))) # out = open("/content/ecg/report.txt",'w') # out.write(summary) # out.close if params.get("generator", False): train_gen = load.data_generator(batch_size, preproc, *train) dev_gen = load.data_generator(batch_size, preproc, *dev) model.fit_generator(train_gen, steps_per_epoch=int(len(train[0]) / batch_size), epochs=MAX_EPOCHS, validation_data=dev_gen, validation_steps=int(len(dev[0]) / batch_size), callbacks=[checkpointer, reduce_lr, stopping]) # util.learning_curve(history) else: train_x, train_y = preproc.process(*train) dev_x, dev_y = preproc.process(*dev) model.fit(train_x, train_y, batch_size=batch_size, epochs=MAX_EPOCHS, validation_data=(dev_x, dev_y), callbacks=[checkpointer, reduce_lr, stopping])
def main(restored_model=None, test_model=None, phase='training'): source_train_list = _read_lists(source_train_fid) source_val_list = _read_lists(source_val_fid) target_train_list = _read_lists(target_train_fid) target_val_list = _read_lists(target_val_fid) network = build_network(channels=3, n_class=5, batch_size=1, cost_kwargs=cost_kwargs, network_config=network_config, opt_kwargs=opt_kwargs) logging.info("Network built") trainer = Trainer(net = network,\ source_train_list = source_train_list,\ source_val_list = source_val_list,\ target_train_list = target_train_list,\ target_val_list = target_val_list, \ output_path = output_path, \ opt_kwargs = opt_kwargs,\ num_epochs = 750,\ checkpoint_space = 300) # start tensorboard before getting started command2 = "fuser " + opt_kwargs["port"] + "/tcp -k" os.system(command2) command1 = "tensorboard --logdir=" + output_path + " --port=" + opt_kwargs[ "port"] + " &" os.system(command1) if phase == 'training': trainer.train_segmenter(restored_model=restored_model) if phase == 'testing': # here are for the testing phase test_list_fid = "/home/chenxi/dlummkd/test_mr_list" test_nii_list_fid = "/home/chenxi/dlummkd/test_mr_nii_list" part = "source" logging.info('performance on source ...') source_dice = trainer.test(test_model=test_model, part=part, test_list_fid=test_list_fid, test_nii_list_fid=test_nii_list_fid) test_list_fid = "/home/chenxi/dlummkd/test_ct_list" test_nii_list_fid = "/home/chenxi/dlummkd/test_ct_nii_list" part = "target" logging.info('performance on target ...') target_dice = trainer.test(test_model=test_model, part=part, test_list_fid=test_list_fid, test_nii_list_fid=test_nii_list_fid) return source_dice, target_dice
def static_nem_iterations(input_data, target_data, binary, k): # Get dimensions input_shape = tf.shape(input_data) assert input_shape.get_shape()[0].value == 6, "Requires 6D input (T, B, K, W, H, C) but {}".format(input_shape.get_shape()[0].value) W, H, C = (x.value for x in input_data.get_shape()[-3:]) # set pixel distribution pixel_dist = 'bernoulli' if binary else 'gaussian' # set up inner cells and nem cells inner_cell = build_network(W * H * C, output_dist=pixel_dist) nem_cell = NEMCell(inner_cell, input_shape=(W, H, C), distribution=pixel_dist) # compute prior prior = compute_prior(distribution=pixel_dist) # get state initializer with tf.name_scope('initial_state'): hidden_state = nem_cell.init_state(input_shape[1], k, dtype=tf.float32) # build static iterations outputs = [hidden_state] total_losses, upper_bound_losses, other_losses = [], [], [] loss_step_weights = get_loss_step_weights() with tf.variable_scope('NEM') as varscope: for t, loss_weight in enumerate(loss_step_weights): varscope.reuse_variables() if t > 0 else None # share weights across time with tf.name_scope('step_{}'.format(t)): # run nem cell inputs = (input_data[t], target_data[t+1]) hidden_state, output = nem_cell(inputs, hidden_state) theta, pred, gamma = output # compute nem losses total_loss, intra_loss, inter_loss = compute_outer_loss( pred, gamma, target_data[t+1], prior, pixel_distribution=pixel_dist) # compute estimated loss upper bound (which doesn't use E-step) loss_upper_bound = compute_loss_upper_bound(pred, target_data[t+1], pixel_dist) total_losses.append(loss_weight * total_loss) upper_bound_losses.append(loss_upper_bound) other_losses.append(tf.stack([total_loss, intra_loss, inter_loss])) outputs.append(output) # collect outputs with tf.name_scope('collect_outputs'): thetas, preds, gammas = zip(*outputs) thetas = tf.stack(thetas) # (T, 1, B*K, M) preds = tf.stack(preds) # (T, B, K, W, H, C) gammas = tf.stack(gammas) # (T, B, K, W, H, C) other_losses = tf.stack(other_losses) # (T, 3) upper_bound_losses = tf.stack(upper_bound_losses) # (T,) with tf.name_scope('total_loss'): total_loss = tf.reduce_sum(tf.stack(total_losses)) return total_loss, thetas, preds, gammas, other_losses, upper_bound_losses
def __init__(self, board_state, batch_size=256): ''' Constructor for DQNAgent Input: board_state, (for getting the input_shape of teh network we will train) ''' num_cols = board_state.shape[-1] self.n_actions = num_cols self.net = build_network(input_shape=board_state.shape, num_outputs=self.n_actions) self.target_net = build_network(input_shape=board_state.shape, num_outputs=self.n_actions) self.target_net.set_weights(self.net.get_weights()) self.net_optimizer = tf.train.AdamOptimizer() self.memory = Memory(batch_size) self.curr_obs = None self.curr_action = None
def _build_graph(self): encoder = build_network(inputs=self.datapipe.next_element, model_specs=self.network_specs['encoder'], latent_dim=self.latent_dim, name='encoder') self.mu, self.logvar = tf.split(encoder, [self.latent_dim, self.latent_dim], axis=1) self.z_samples = misc.sampler_normal(self.mu, self.logvar) self._build_decoder(self.z_samples)
def train(args, params): print("Loading training set...") train = load.load_dataset(params['train']) print("Loading dev set...") dev = load.load_dataset(params['dev']) print("Building preprocessor...") preproc = load.Preproc(*train) print("Training size: " + str(len(train[0])) + " examples.") print("Dev size: " + str(len(dev[0])) + " examples.") save_dir = make_save_dir(params['save_dir'], args.experiment) util.save(preproc, save_dir) params.update({ "input_shape": [None, 1], "num_categories": len(preproc.classes) }) print(params) model = network.build_network(**params) stopping = keras.callbacks.EarlyStopping(patience=8) reduce_lr = keras.callbacks.ReduceLROnPlateau( factor=0.1, patience=2, min_lr=params["learning_rate"] * 0.001) checkpointer = keras.callbacks.ModelCheckpoint( filepath=get_filename_for_saving(save_dir), save_best_only=False) ckpt_best = keras.callbacks.ModelCheckpoint(os.path.join( save_dir, 'best.hdf5'), save_best_only=True) batch_size = params.get("batch_size", 32) if params.get("generator", False): train_gen = load.data_generator(batch_size, preproc, *train) dev_gen = load.data_generator(batch_size, preproc, *dev) model.fit_generator( train_gen, steps_per_epoch=int(len(train[0]) / batch_size), epochs=MAX_EPOCHS, validation_data=dev_gen, validation_steps=int(len(dev[0]) / batch_size), callbacks=[checkpointer, ckpt_best, reduce_lr, stopping]) else: train_x, train_y = preproc.process(*train) dev_x, dev_y = preproc.process(*dev) model.fit(train_x, train_y, batch_size=batch_size, epochs=MAX_EPOCHS, validation_data=(dev_x, dev_y), callbacks=[checkpointer, ckpt_best, reduce_lr, stopping])
def train(args, params): print("Loading training set...") train = load.load_dataset(params['train']) print("Loading dev set...") dev = load.load_dataset(params['dev']) print("Building preprocessor...") preproc = load.Preproc(*train) print("Training size: " + str(len(train[0])) + " examples.") print("Dev size: " + str(len(dev[0])) + " examples.") save_dir = make_save_dir(params['save_dir'], args.experiment) util.save(preproc, save_dir) params.update({ "input_shape": [None, 1], "num_categories": len(preproc.classes) }) model = network.build_network(**params) stopping = keras.callbacks.EarlyStopping(patience=8) reduce_lr = keras.callbacks.ReduceLROnPlateau( factor=0.1, patience=2, min_lr=params["learning_rate"] * 0.001) checkpointer = keras.callbacks.ModelCheckpoint( filepath=get_filename_for_saving(save_dir), save_best_only=False) batch_size = params.get("batch_size", 32) if params.get("generator", False): train_gen = load.data_generator(batch_size, preproc, *train) dev_gen = load.data_generator(batch_size, preproc, *dev) model.fit_generator( train_gen, steps_per_epoch=int(len(train[0]) / batch_size), epochs=MAX_EPOCHS, validation_data=dev_gen, validation_steps=int(len(dev[0]) / batch_size), callbacks=[checkpointer, reduce_lr, stopping]) else: train_x, train_y = preproc.process(*train) dev_x, dev_y = preproc.process(*dev) model.fit( train_x, train_y, batch_size=batch_size, epochs=MAX_EPOCHS, validation_data=(dev_x, dev_y), callbacks=[checkpointer, reduce_lr, stopping])
def _build_encoder(self, inputs): encoder = build_network(inputs=inputs, model_specs=self.network_specs['encoder'], latent_dim=self.network_specs['latent_dim'], name='encoder') z_mean, z_logvar = tf.split(encoder, [ self.network_specs['latent_dim'], self.network_specs['latent_dim'] ], axis=1) z_samples = sampler_normal(z_mean, z_logvar) return z_mean, z_logvar, z_samples
def _build_decoder(self, inputs): logits = build_network(inputs=inputs, model_specs=self.network_specs['decoder'], num_channel=self.network_specs['num_channel'], name='decoder') re_mask, re_image_mean = tf.split(logits, [1, 3], axis=-1) # i think this should be trained with tf.nn.sigmoid # because the means should be between 0 and 1 if self.sigmoid_output: re_image_mean = tf.nn.sigmoid(re_image_mean) return re_mask, re_image_mean
def main(): communities_t0, number_nodes_t0, nodes_t0 = build_communities() build_network(communities_t0, number_nodes_t0, nodes_t0) if write_gephi: write_links_csv(nodes_t0) for Ts.timestamp in range(1, TIMESTEPS+1): communities_t1, number_nodes_t1, nodes_t1 = build_communities() # initialize confusion matrix with deaths and births confusion_matrix = events_communities(communities_t0, nodes_t0, communities_t1, nodes_t1, number_nodes_t1) # find most similar transition score, global_solution = transition_t0_t1(communities_t0, communities_t1) # finalize confusion matrix flow_nodes(communities_t0, communities_t1, nodes_t1, global_solution, confusion_matrix) match_communities(communities_t0, communities_t1, confusion_matrix, sigma, jaccard_null_model) build_network(communities_t1, number_nodes_t1, nodes_t1) if write_gephi: write_links_csv(nodes_t1) printouts(confusion_matrix, score, communities_t0, communities_t1) communities_t0, nodes_t0 = turnover_communities(communities_t1, nodes_t1) # [node.reset_node_links() for node in nodes_t1] if write_gephi: write_nodes_csv() if write_netgram: write_nodes_netgram()
def mypredict(args): # ecg = load.load_ecg(record +".mat") if args.data_fname[-3:] == "npy": x_test = np.load(args.data_fname) elif args.data_fname[-3:] == "csv": x_test = pd.read_csv(args.data_fname) x_test.drop(['id'], axis=1) x_test = x_test.fillna(0) x_test = x_test.values np.save(args.data_fname[:-4], x_test) print("data loading done") x_test_list = [] for i in range(len(x_test)): x_i_end = x_test[i].nonzero()[0][-1] + 1 x_i_end = STEP * int((x_i_end) / STEP) # print(x_i_end) x_test_list.append(x_test[i][:x_i_end]) preproc = util.load(args.prep_fname) x_test = preproc.process_x(x_test_list) params = json.load(open(args.config_fname)) params.update({ "compile": False, "input_shape": [None, 1], "num_categories": len(preproc.classes) }) model = network.build_network(**params) model.load_weights(args.model_fname) # print(x_test.shape) probs = model.predict(x_test) preds = np.argmax(probs, axis=2) results = -np.ones((len(x_test), ), dtype=np.int64) for i in range(len(x_test)): # print(len(x_test_list[i])//STEP) # print(preds[i, :len(x_test_list[i])//STEP]) # print(sst.mode(preds[i, :len(x_test_list[i])//STEP])[0][0]) results[i] = sst.mode(preds[i, :len(x_test_list[i]) // STEP])[0][0] # print(probs.shape) # prediction = sst.mode(np.argmax(probs, axis=2).squeeze())[0][0] print(Counter(results)) sample = pd.read_csv(args.sample_fname) sample['y'] = results sample.to_csv(args.submit_fname, index=False) return
def __init__(self, inputSimulator, preprocessor, pretrained): self.iS = inputSimulator self.pP = preprocessor self.input_var = T.tensor4('input') self.network = build_network(6, self.input_var, (20,20)) self.prediction = lasagne.layers.get_output(self.network, deterministic=True) self.x = np.zeros((1,4,20,20),np.int8) if pretrained: self.loadParams() print("Compiling network") self.pred_fn = theano.function([self.input_var], self.prediction, allow_input_downcast=True) print("Compiling done")
def __init__(self, W, H, n_input, lr, n_h1=25, n_h2=20, l2=10, name='deep_irl_fc'): self.n_input = n_input self.lr = lr self.n_h1 = n_h1 self.n_h2 = n_h2 self.name = name self.networks = build_network(W, H) # self.network = self.network.cuda() self.finetune(allow=True) # self.sess = tf.Session() # self.input_s, self.reward, self.theta = self.build_network(self.name) self.optimizer = optim.SGD(self.networks.parameters(), lr=0.001) # self.optimizer = tf.train.GradientDescentOptimizer(lr) self.l2_loss = nn.MSELoss()
def predict(record): ecg = load.load_ecg(record +".mat") preproc = util.load(".") x = preproc.process_x([ecg]) params = json.load(open("config.json")) params.update({ "compile" : False, "input_shape": [None, 1], "num_categories": len(preproc.classes) }) model = network.build_network(**params) model.load_weights('model.hdf5') probs = model.predict(x) prediction = sst.mode(np.argmax(probs, axis=2).squeeze())[0][0] return preproc.int_to_class[prediction]
def train(session, dataset): network = network_mod.build_network(dataset.image_shape, dataset.num_classes) session.run(tf.global_variables_initializer()) file_writer = tf.summary.FileWriter("logs/", graph=session.graph) saver = tf.train.Saver() ts = TrainingSession( session=session, network=network, dataset=dataset, file_writer=file_writer, saver=saver, learning_rate=config.INITIAL_LEARNING_RATE, ) for epoch_idx in range(1, config.NUM_EPOCHS + 1): ts = train_epoch(ts, epoch_idx)
def mylimepredict(ecg): preproc = util.load(".") x = preproc.process_x(ecg) params = json.load(open("config.json")) params.update({ "compile": False, "input_shape": [None, 1], "num_categories": len(preproc.classes) }) model = network.build_network(**params) model.load_weights('model.hdf5') probs = model.predict(x) classprobs = np.amax(probs, axis=1) prediction = sst.mode(np.argmax(probs, axis=2).squeeze())[0][0] # predictionclass = preproc.int_to_class[prediction] # print prediction, predictionclass return classprobs
train_word_y = np.expand_dims(data["train_word"][1], -1) train_orth_char_x = data["orth_char"][0] train_orth_x = data["train_orth"] n_train_examples = len(train_word_x) dev_char_x = data["char"][1] dev_word_x = data["dev_word"][0] dev_word_y = data["dev_word"][1] dev_mask = data["dev_word"][2] dev_orth_char_x = data["orth_char"][1] dev_orth_x = data["dev_orth"] n_dev_examples = len(dev_word_x) # Build network logger.info("Building network") model = network.build_network(args, data) num_labels = data["label_collection"].size() - 1 # Compiling model logger.info("Compiling model") model.compile(optimizer="adam", loss=model.layers[-1].loss, metrics=["accuracy"]) # Callbacks loss_train = [] acc_train = [] loss_history = keras.callbacks.LambdaCallback( on_epoch_end=lambda epoch, logs: loss_train.append(logs['loss'])) acc_history = keras.callbacks.LambdaCallback( on_epoch_end=lambda epoch, logs: acc_train.append(logs['acc'] * 100.0))
def test(): tf.reset_default_graph() infer_graph = tf.Graph() with infer_graph.as_default(): encoder_outputs_t, inputs_t = build_cnn(False, flags.batch_size, flags.height, flags.width, flags.channels) _, _, pred_ids, logits_t, decoder_inputs_t, \ _, _ ,keep_prob_t= build_network(encoder_outputs_t, True, flags.batch_size, flags.decoder_length, flags.tgt_vocab_size, flags.attn_num_hidden, flags.encoder_length, flags.max_gradient_norm ) infer_saver = tf.train.Saver() infer_sess = tf.Session(graph=infer_graph) model_file = tf.train.latest_checkpoint(flags.load_dir) infer_saver.restore(infer_sess, model_file) with open(flags.test_txt) as f: test = [line.rstrip() for line in f] test_len = len(test) test = np.array(test) data_test = Dataset(test) if flags.lex_txt != None: with open(flags.lex_txt) as f: lex = [line.rstrip().lower() for line in f] ti = int(test_len / flags.batch_size) rest = test_len % flags.batch_size gt = [] predict = [] for t in range(ti): batch_test = data_test.next_batch(flags.batch_size) path = [] texts = [] for line in batch_test: path.append(line.split(' ', 1)[0]) texts.append(line.split(' ', 1)[1]) images = load_img(path, flags.height, flags.width) testing_decoder_inputs = np.zeros( (flags.decoder_length, flags.batch_size), dtype=float) feed_dict_t = { inputs_t: images[:, :, :, np.newaxis], decoder_inputs_t: testing_decoder_inputs, keep_prob_t: 1 } q = infer_sess.run(pred_ids, feed_dict=feed_dict_t) for j in range(flags.batch_size): gt.append(texts[j]) ans = np.array(q).T[j] pd = [] for c in ans: if c != -1: character = tools.idx_to_word[c] if character != '<EOS>': pd.append(character) predict.append(''.join(pd)) batch_test = data_test.next_batch(flags.batch_size) path = [] texts = [] for line in batch_test: path.append(line.split(' ', 1)[0]) texts.append(line.split(' ', 1)[1]) images = load_img(path, flags.height, flags.width) feed_dict_t = { inputs_t: images[:, :, :, np.newaxis], decoder_inputs_t: testing_decoder_inputs, keep_prob_t: 1 } q = infer_sess.run(pred_ids, feed_dict=feed_dict_t) for k in range(rest): gt.append(texts[k]) ans = np.array(q).T[k] pd = [] for c in ans: if c != -1: character = tools.idx_to_word[c] if character != '<EOS>': pd.append(character) predict.append(''.join(pd)) correct = float(0) cnt = 0 acc_s = 0 for l in range(len(gt)): cnt = cnt + 1 if gt[l] == predict[l]: correct = correct + 1 acc_s = correct / cnt if flags.lex_txt != None: correct_l = float(0) cnt = 0 for l in range(len(gt)): cnt = cnt + 1 lexicon = lex[l].split(',') dt = editdistance.eval(predict[l], lexicon[0]) pl = lexicon[0] for ll in lexicon[1:]: dt_temp = editdistance.eval(predict[l], ll) if dt_temp < dt: dt = dt_temp pl = ll if pl == gt[l]: correct_l = correct_l + 1 acc_l = correct_l / cnt print('accuracy: ', acc_s) if flags.lex_txt != None: print('accuracy with lexicon: ', acc_l)
def train(): f_size = int(flags.img_size / 8) encoder_length = f_size * f_size with open(flags.train_txt) as f: sample = [line.rstrip() for line in f] sample = np.array(sample) iteration = len(sample) // flags.batch_size data = Dataset(sample) tf.reset_default_graph() train_graph = tf.Graph() infer_graph = tf.Graph() start = time.time() with train_graph.as_default(): c, inputs = build_cnn(is_training=True, batch_size=flags.batch_size, img_size=flags.img_size, channels=flags.channels) deconv_outputs = build_deconv(True, c, flags.batch_size) x = np.linspace(-0.5, 0.5, f_size) x = np.tile(x, (f_size, 1)) y = np.transpose(x) x = np.expand_dims(x, axis=2) y = np.expand_dims(y, axis=2) m = np.concatenate((x, y), axis=2) m = np.expand_dims(m, axis=0) m = np.repeat(m, flags.batch_size, axis=0) m = tf.convert_to_tensor(m, np.float32) encoder_outputs = tf.concat([c, m], -1) encoder_outputs = tf.reshape(encoder_outputs, shape=(-1, f_size * f_size, 258)) encoder_outputs = tf.transpose(encoder_outputs, [1, 0, 2]) train_op, loss , sample_ids,logits, decoder_inputs, \ target_labels, learning_rate,attention_weights_history,att_label,lamda,att_mask,input_seg= build_network(encoder_outputs, False, flags.batch_size, flags.decoder_length, flags.tgt_vocab_size, flags.attn_num_hidden, encoder_length, flags.max_gradient_norm, f_size, flags.att_loss, flags.img_size, deconv_outputs ) initializer = tf.global_variables_initializer() train_saver = tf.train.Saver() train_sess = tf.Session(graph=train_graph) train_sess.run(initializer) with infer_graph.as_default(): c_t, inputs_t = build_cnn(is_training=False, batch_size=flags.batch_size, img_size=flags.img_size, channels=flags.channels) deconv_outputs_t = build_deconv(False, c_t, flags.batch_size) x_t = np.linspace(-0.5, 0.5, f_size) x_t = np.tile(x_t, (f_size, 1)) y_t = np.transpose(x_t) x_t = np.expand_dims(x_t, axis=2) y_t = np.expand_dims(y_t, axis=2) m_t = np.concatenate((x_t, y_t), axis=2) m_t = np.expand_dims(m_t, axis=0) m_t = np.repeat(m_t, flags.batch_size, axis=0) m_t = tf.convert_to_tensor(m_t, np.float32) encoder_outputs_t = tf.concat([c_t, m_t], -1) encoder_outputs_t = tf.reshape(encoder_outputs_t, shape=(-1, f_size * f_size, 258)) encoder_outputs_t = tf.transpose(encoder_outputs_t, [1, 0, 2]) _, _ , pred_ids,logits_t, decoder_inputs_t, \ _, _,_,_,_,_,_= build_network(encoder_outputs_t, True, flags.batch_size, flags.decoder_length, flags.tgt_vocab_size, flags.attn_num_hidden, encoder_length, flags.max_gradient_norm, f_size, flags.att_loss, flags.img_size, deconv_outputs_t ) infer_saver = tf.train.Saver() infer_sess = tf.Session(graph=infer_graph) # Training la = 10 acc_log = 0 count = 0 lr = flags.learning_rate for h in range(flags.epoch): for i in range(iteration): batch_train = data.next_batch(flags.batch_size) path = [] texts = [] for line in batch_train: path.append(line.split(' ')[0]) texts.append(line.split(' ')[1]) if flags.att_loss: images, npy, mask, seg = load_img_label( path, flags.img_size, flags.decoder_length) else: images = load_img(path, flags.img_size) training_target_labels = get_label(texts, flags.decoder_length) training_decoder_inputs = np.delete(training_target_labels, -1, axis=1) training_decoder_inputs = np.c_[ np.zeros(training_decoder_inputs.shape[0]), training_decoder_inputs].T feed_dict = { inputs: images, decoder_inputs: training_decoder_inputs, target_labels: training_target_labels, learning_rate: lr } if flags.att_loss: feed_dict[att_label] = npy feed_dict[att_mask] = mask feed_dict[input_seg] = seg[:, :, :, np.newaxis] feed_dict[lamda] = la _, loss_value, att = train_sess.run( [train_op, loss, attention_weights_history], feed_dict=feed_dict) step = float(i + 1) if step % flags.display_step == 0: now = time.time() print(step, now - start, loss_value) start = now if step % flags.eval_step == 0: train_saver.save(train_sess, flags.save_dir) model_file = tf.train.latest_checkpoint( flags.save_dir.rsplit('/', 1)[0]) infer_saver.restore(infer_sess, model_file) gt = [] predict = [] images = load_img(path, flags.img_size) testing_decoder_inputs = np.zeros( (flags.decoder_length, flags.batch_size), dtype=float) feed_dict_t = { inputs_t: images, decoder_inputs_t: testing_decoder_inputs } q = infer_sess.run(pred_ids, feed_dict=feed_dict_t) for j in range(flags.batch_size): gt.append(texts[j]) ans = np.array(q).T[j] pd = [] for c in ans: if c != -1: character = tools.idx_to_word[c] if character != '<EOS>': pd.append(character) predict.append(''.join(pd)) correct = float(0) cnt = 0 acc_s = 0 for l in range(len(gt)): cnt = cnt + 1 if gt[l] == predict[l]: correct = correct + 1 count = count + 1 acc_s = correct / cnt if acc_s > acc_log: acc_log = acc_s count = 0 if count == (iteration // flags.eval_step): lr = lr / 5
def train(args, params): print("Loading training set...") train = load.load_dataset(params['train']) print("Loading dev set...") dev = load.load_dataset(params['dev']) print("Building preprocessor...") preproc = load.Preproc(*train) print("Training size: " + str(len(train[0])) + " examples.") print("Dev size: " + str(len(dev[0])) + " examples.") preproc.set_window_size(params['window_size']) save_dir = make_save_dir(params['save_dir'], args.experiment) util.save(preproc, save_dir) print('classes', preproc.classes) print("Skip Init initial Alpha : {}, L2 : {}, Lr : {}, batch_size : {}". format(params["skip_init_a"], params["conv_l2"], params["learning_rate"], params["batch_size"])) params.update({ "input_shape": [None, 1], "num_categories": len(preproc.classes) }) model = network.build_network(**params) print(model.summary()) if params.get('is_regular_conv', False): plot_model(model, to_file='model_regular_conv.png', show_shapes=True) else: if params.get('conv_batch_norm'): plot_model(model, to_file='model_residual_conv_bn.png', show_shapes=True) else: plot_model(model, to_file='model_residual_conv_nobn.png', show_shapes=True) stopping = keras.callbacks.EarlyStopping(patience=8) reduce_lr = keras.callbacks.ReduceLROnPlateau( factor=0.1, patience=2, min_lr=params["learning_rate"] * 0.001) # checkpointer = keras.callbacks.ModelCheckpoint( # filepath=get_filename_for_saving(save_dir), # save_best_only=False, save_weights_only=False) checkpointer = keras.callbacks.ModelCheckpoint( filepath=get_filename_for_saving(save_dir), save_weights_only=False, save_best_only=True, monitor='val_accuracy', mode='max', ) batch_size = params.get("batch_size", 32) if params.get("generator", False): train_gen = load.data_generator(batch_size, preproc, *train) dev_gen = load.data_generator(batch_size, preproc, *dev) train_gen = np.array(train_gen) dev_gen = np.array(dev_gen) # print('train_gen m, s: ', train_gen.mean(), train_gen.std()) # print('train_gen min, max: ', train_gen.min(), train_gen.max()) print('train_gen shape : ', train_gen.shape) print('dev_gen shape : ', train_gen.shape) model.fit_generator(train_gen, steps_per_epoch=int(len(train[0]) / batch_size), epochs=params.get("epoch", 10), validation_data=dev_gen, validation_steps=int(len(dev[0]) / batch_size), callbacks=[checkpointer, reduce_lr, stopping]) else: train_x, train_y = preproc.process(*train) dev_x, dev_y = preproc.process(*dev) print('train_x original shape : ', train_x.shape) print('train_y original shape : ', train_y.shape) print('test_x original shape : ', dev_x.shape) print('test_y original shape : ', dev_y.shape) # train_x = np.array(train_x) # train_y = np.array(train_y) # test_x = np.array(dev_x) # test_y = np.array(dev_y) window_size = 256 n_sample = 40 r_i = 0 r_length = 1 # print('random number : ', np.random.choice(test_x.shape[1] // window_size, 1)[0]) # train_x = train_x[:, 256 * r_i:256 * (r_i+1)* r_length, :] # train_y = train_y[:, r_i, :] # # test_x = test_x[:, 256 * r_i:256 * (r_i+1) * r_length, :] # test_y = test_y[:, r_i, :] # train_x = np.array(all_train_x) # train_y = np.array(all_train_y) # FOR PLAINTEXT conversion start # get 8 windows # train_x = train_x[:, :window_size * n_sample, :] # train_y = train_y[:, r_i, :] # test_x = test_x[:, :window_size * n_sample, :] # test_y = test_y[:, r_i, :] # test_x = np.array(random_test_x) # train_x = np.array(train_x) # # train_x = np.squeeze(train_x, axis=(2,)) # test_x = np.squeeze(test_x, axis=(2,)) # # print('train_x m, s: ', train_x.mean(), train_x.std()) # print('train_x min, max: ', train_x.min(), train_x.max()) # # print('train_x shape : ', train_x.shape) # print('train_y shape : ', train_y.shape) # print('test_x shape : ', test_x.shape) # print('test_y shape : ', test_y.shape) # gt = np.argmax(test_y, axis=1) # print('0:', len(gt[gt == 0])) # print('1:', len(gt[gt == 1])) # print('2:', len(gt[gt == 2])) # print('3:', len(gt[gt == 3])) # print('gt shape:', gt.shape) # data_dir = 'processed_data_2560' # data_dir = 'processed_data_5120' # # if not os.path.exists(data_dir): # os.makedirs(data_dir) # train_file_suffix = 'train' # test_file_suffix = 'test' # # file_name_train_x = 'X{}'.format(train_file_suffix) # file_name_train_y = 'y{}'.format(train_file_suffix) # file_name_test_x = 'X{}'.format(test_file_suffix) # file_name_test_y = 'y{}'.format(test_file_suffix) # # np.savetxt('{}/{}'.format(data_dir, file_name_train_x), train_x, delimiter=',', fmt='%1.8f') # np.savetxt('{}/{}'.format(data_dir, file_name_train_y), train_y, delimiter=',', fmt='%1.8f') # np.savetxt('{}/{}'.format(data_dir, file_name_test_x), test_x, delimiter=',', fmt='%1.8f') # np.savetxt('{}/{}'.format(data_dir, file_name_test_y), test_y, delimiter=',', fmt='%1.8f') # print('train_x shape : ', train_x.shape) # print('train_y shape : ', train_y.shape) # print('test_x shape : ', test_x.shape) # print('test_y shape : ', test_y.shape) model.fit(train_x, train_y, batch_size=batch_size, epochs=params.get("epoch", 10), validation_data=(dev_x, dev_y), callbacks=[checkpointer, reduce_lr, stopping])
def build_model(self, reuse, device): with tf.variable_scope(self.name) and tf.device(device): if reuse: tf.get_variable_scope().reuse_variables() # placeholder for inputs of network self.screen_ph = tf.placeholder(tf.float32, [ None, U.screen_channel(), self.screen_dimensions, self.screen_dimensions ], name='screen') self.minimap_ph = tf.placeholder(tf.float32, [ None, U.minimap_channel(), self.minimap_dimensions, self.minimap_dimensions ], name='minimap') self.structured_ph = tf.placeholder( tf.float32, [None, self.structured_dimensions], name='structured') # build network network = build_network(self.structured_ph, self.screen_ph, self.minimap_ph, len(actions.FUNCTIONS)) self.non_spatial_action, self.spatial_action, self.value = network # placeholder for targets and masks self.valid_non_spatial_action_ph = tf.placeholder( tf.float32, [None, len(actions.FUNCTIONS)], name='valid_non_spatial_action') self.sample_non_spatial_action_ph = tf.placeholder( tf.float32, [None, len(actions.FUNCTIONS)], name='sample_non_spatial_action') self.valid_spatial_action_ph = tf.placeholder( tf.float32, [None], name='valid_spatial_action') self.sample_spatial_action_ph = tf.placeholder( tf.float32, [None, self.minimap_dimensions**2], name='sample_spatial_action') self.target_value_ph = tf.placeholder(tf.float32, [None], name='target_value') # compute log probability valid_non_spatial_action_prob = tf.reduce_sum( self.non_spatial_action * self.valid_non_spatial_action_ph, axis=1) valid_non_spatial_action_prob = tf.clip_by_value( valid_non_spatial_action_prob, 1e-10, 1.) non_spatial_action_prob = tf.reduce_sum( self.non_spatial_action * self.sample_non_spatial_action_ph, axis=1) non_spatial_action_prob /= valid_non_spatial_action_prob non_spatial_action_log_prob = tf.log( tf.clip_by_value(non_spatial_action_prob, 1e-10, 1.)) spatial_action_prob = tf.reduce_sum(self.spatial_action * self.sample_spatial_action_ph, axis=1) spatial_action_log_prob = tf.log( tf.clip_by_value(spatial_action_prob, 1e-10, 1.)) self.summary.append( tf.summary.histogram('non_spatial_action_prob', non_spatial_action_prob)) self.summary.append( tf.summary.histogram('spatial_action_prob', spatial_action_prob)) # compute loss action_log_prob = self.valid_spatial_action_ph * spatial_action_log_prob + non_spatial_action_log_prob advantage = tf.stop_gradient(self.target_value_ph - self.value) policy_loss = -tf.reduce_mean(action_log_prob * advantage) value_loss = -tf.reduce_mean(self.value * advantage) loss = policy_loss + value_loss self.summary.append(tf.summary.scalar('policy_loss', policy_loss)) self.summary.append(tf.summary.scalar('value_loss', value_loss)) # optimizer self.learning_rate_ph = tf.placeholder(tf.float32, None, name='learning_rate') optimizer = tf.train.RMSPropOptimizer(self.learning_rate_ph, decay=0.99, epsilon=1e-10) grads = optimizer.compute_gradients(loss) clipped_grads = [] for grad, var in grads: self.summary.append(tf.summary.histogram(var.op.name, var)) self.summary.append( tf.summary.histogram(var.op.name + '/grad', grad)) grad = tf.clip_by_norm(grad, 10.0) clipped_grads.append([grad, var]) self.train_op = optimizer.apply_gradients(clipped_grads) self.summary_op = tf.summary.merge(self.summary) self.saver = tf.train.Saver(max_to_keep=None)
def build_model(self, reuse, device): with tf.variable_scope(self.name) and tf.device(device): # tf.device:指定用於新創建的操作的默認設備。 if reuse: tf.get_variable_scope().reuse_variables() """ 達到重複利用變量的效果, 要使用tf.variable_scope(), 並搭配tf.get_variable() 這種方式產生和提取變量. 不像tf.Variable() 每次都會產生新的變量, tf.get_variable () 如果遇到了同樣名字的變量時, 它會單純的提取這個同樣名字的變量(避免產生新變量). 而在重複使用的時候, 一定要在代碼中強調scope.reuse_variables(), 否則係統將會報錯, 以為你只是單純的不小心重複使用到了一個變量 """ self.screen = tf.placeholder(tf.float32, [None, utils.screen(), self.resolution, self.resolution], name="screen") self.minimap = tf.placeholder(tf.float32, [None, utils.minimap(), self.resolution, self.resolution], name="minimap") self.structured = tf.placeholder(tf.float32, [None, self.structured_dimensions], name="structured") # build network network = build_network(self.structured, self.screen, self.minimap, len(actions.FUNCTIONS)) self.non_spatial_action, self.spatial_action, self.value = network self.valid_non_spatial_action = tf.placeholder(tf.float32, [None, len(actions.FUNCTIONS)], name="valid_non_spatial_action") self.non_spatial_action_selected = tf.placeholder(tf.float32, [None, len(actions.FUNCTIONS)], name="non_spatial_action_selected") self.valid_spatial_action = tf.placeholder(tf.float32, [None], name="valid_spatial_action") self.spatial_action_selected = tf.placeholder(tf.float32, [None, self.resolution ** 2], name="spatial_action_selected") self.target_value = tf.placeholder(tf.float32, [None], name="target_value") # compute log probability valid_non_spatial_action_prob = tf.reduce_sum(self.non_spatial_action * self.valid_non_spatial_action, axis=1) # tf.reduce_sum:計算張量維度的元素總和。 valid_non_spatial_action_prob = tf.clip_by_value(valid_non_spatial_action_prob, 1e-5, 1.) # tf.clip_by_value: 輸入一個張量A,把A中的每一個元素的值都壓縮在min和max之間。小於min的讓它等於min,大於max的元素的值等於max。 non_spatial_action_prob = tf.reduce_sum(self.non_spatial_action * self.non_spatial_action_selected, axis=1) non_spatial_action_prob /= valid_non_spatial_action_prob non_spatial_action_log_prob = tf.log(tf.clip_by_value(non_spatial_action_prob, 1e-5, 1.)) spatial_action_prob = tf.reduce_sum(self.spatial_action * self.spatial_action_selected, axis=1) spatial_action_log_prob = tf.log(tf.clip_by_value(spatial_action_prob, 1e-5, 1.)) self.summary.append(tf.summary.histogram('non_spatial_action_prob', non_spatial_action_prob)) self.summary.append(tf.summary.histogram('spatial_action_prob', spatial_action_prob)) # compute loss !! 尚未加入entropy以及loss_weight action_log_prob = self.valid_spatial_action * spatial_action_log_prob + non_spatial_action_log_prob advantage = tf.stop_gradient(self.target_value - self.value) policy_loss = -tf.reduce_mean(action_log_prob * advantage) # value_loss = -tf.reduce_mean(self.value * advantage) value_loss = tf.reduce_mean(tf.square(self.target_value - self.value) / 2.) entropy = 0 if self.mode == 'original_ac3': entropy = -tf.reduce_sum(self.non_spatial_action * tf.log(self.non_spatial_action + 1e-10)) loss = policy_loss + value_loss * self.value_loss_weight - entropy * self.entropy_weight loss = policy_loss + value_loss * self.value_loss_weight - entropy * self.entropy_weight self.summary.append(tf.summary.scalar('policy_loss', policy_loss)) self.summary.append(tf.summary.scalar('value_loss', value_loss)) self.summary.append(tf.summary.scalar('value', tf.reduce_mean(self.value))) self.summary.append(tf.summary.scalar('advantage', tf.reduce_mean(advantage))) self.summary.append(tf.summary.scalar('returns', tf.reduce_mean(self.target_value))) # optimizer self.learning_rate = tf.placeholder(tf.float32, None, name="learning_rate") optimizer = tf.train.RMSPropOptimizer(self.learning_rate, decay=0.99, epsilon=1e-10) grads = optimizer.compute_gradients(loss) clipped_grads = [] for grad, var in grads: self.summary.append(tf.summary.histogram(var.op.name, var)) self.summary.append(tf.summary.histogram(var.op.name + '/grad', grad)) grad = tf.clip_by_norm(grad, 10.0) clipped_grads.append([grad, var]) self.train_op = optimizer.apply_gradients(clipped_grads) self.summary_op = tf.summary.merge(self.summary) self.saver = tf.train.Saver()
masses = np.random.uniform(0.1, 10.0, (N_PARTICLES)) # get a target scaling targets = np.random.uniform(10.0, 100.0, (1, N_PARTICLES, 1)) def random_four_vectors(n_data=256): momenta = np.random.normal(0.0, 1.0, (n_data, N_PARTICLES, 3)) energies = np.sqrt(np.sum(momenta**2, axis=-1) + np.sum(masses**2)) vectors = np.concatenate((np.expand_dims(energies, axis=-1), momenta), axis=-1) return np.reshape(vectors, (-1, N_PARTICLES, 4)) network = build_network(n_particles=N_PARTICLES, batch_size=BATCH_SIZE, built_in_noise=BUILT_IN_NOISE) # Some information about the network print(network.summary()) print("-" * 20) print("Trainable Variables:") for layer in network.layers: print("-" * 20) print("{}:".format(layer.name)) print("Trainable Variables:") print(layer.trainable_variables) data = random_four_vectors(n_data=N_DATA) noise = tf.random.normal((N_DATA, N_PARTICLES, 4), 0.0, 1.0) print("SHAPE:")
print("Loading training set...") train = load.load_dataset(params['train']) print("Loading dev set...") dev = load.load_dataset(params['dev']) print("Building preprocessor...") preproc = load.Preproc(*train) print("Training size: " + str(len(train[0])) + " examples.") print("Dev size: " + str(len(dev[0])) + " examples.") params.update({ "input_shape": [8000, 1], "num_categories": len(preproc.classes) }) #create the cl-pcg-net model = network.build_network(**params) #learning rate reduce strategy def scheduler(epoch): if epoch % 80 == 0 and epoch != 0: lr = K.get_value(model.optimizer.lr) model.load_weights(save_dir + 'best.hdf5') K.set_value(model.optimizer.lr, lr * 0.1) print("lr changed to {}".format(lr * 0.1)) return K.get_value(model.optimizer.lr) reduce_lr = LearningRateScheduler(scheduler) #choose best model to save checkpointer = keras.callbacks.ModelCheckpoint(mode='max', monitor='val_acc',
def test(): tf.reset_default_graph() infer_graph = tf.Graph() with infer_graph.as_default(): _, _, pred_ids,pred_logits , inputs_t, decoder_inputs_t, decoder_lengths_t, \ _ = build_network(is_training=False, batch_size=FLAGS.batch_size, height=FLAGS.height, width=FLAGS.width, channels=FLAGS.channels, decoder_length=FLAGS.decoder_length, tgt_vocab_size=FLAGS.tgt_vocab_size, num_units=FLAGS.num_units, beam_width=FLAGS.beam_width, encoder_length=FLAGS.encoder_length, max_gradient_norm=FLAGS.max_gradient_norm, embedding_size=FLAGS.embedding_size) infer_saver = tf.train.Saver() infer_sess = tf.Session(graph=infer_graph) with open(FLAGS.test_txt) as f: test = [line.rstrip() for line in f] test_len = len(test) test = np.array(test) data_test = Dataset_test(test) if FLAGS.lex_txt != None: with open(FLAGS.lex_txt) as f: lex = [line.rstrip().lower() for line in f] ti = int(test_len / FLAGS.batch_size) rest = test_len % FLAGS.batch_size gt = [] predict = [] model_file=tf.train.latest_checkpoint(FLAGS.load_dir) infer_saver.restore(infer_sess, model_file) for t in range(ti): batch_test = data_test.next_batch(FLAGS.batch_size) path = [] texts = [] for line in batch_test: path.append(line.split(' ',1)[0]) texts.append(line.split(' ',1)[1]) images = load_img(path,FLAGS.height,FLAGS.width) feed_dict_t = {inputs_t:images[:, :, :, np.newaxis], decoder_lengths_t:np.ones((FLAGS.batch_size), \ dtype=int) * FLAGS.decoder_length } q= infer_sess.run( pred_ids,feed_dict=feed_dict_t) for j in range(len(texts)): gt.append(texts[j]) ans = q[j].T[0] pd = [] for c in ans: if c != -1: character = tools.idx_to_word[c] if character != '<EOS>': pd.append(character) predict.append(''.join(pd)) batch_test = data_test.next_batch(FLAGS.batch_size) path = [] texts = [] for line in batch_test: path.append(line.split(' ',1)[0]) texts.append(line.split(' ',1)[1]) images = load_img(path,FLAGS.height,FLAGS.width) feed_dict_t = {inputs_t:images[:, :, :, np.newaxis], decoder_lengths_t:np.ones((FLAGS.batch_size), \ dtype=int) * FLAGS.decoder_length } q = infer_sess.run( pred_ids,feed_dict=feed_dict_t) for k in range(rest): gt.append(texts[k]) ans = q[k].T[0] pd = [] for c in ans: if c != -1: character = tools.idx_to_word[c] if character != '<EOS>': pd.append(character) predict.append(''.join(pd)) correct = float(0) cnt = 0 acc_s = 0 for l in range(len(gt)): cnt =cnt + 1 if gt[l] == predict[l]: correct = correct + 1 acc_s = correct / cnt if FLAGS.lex_txt != None: correct_l = float(0) cnt = 0 for l in range(len(gt)): cnt =cnt + 1 lexicon = lex[l].split(',') dt = distance.levenshtein(predict[l], lexicon[0]) pl = lexicon[0] for ll in lexicon[1:]: dt_temp = distance.levenshtein(predict[l], ll) if dt_temp < dt: dt = dt_temp pl = ll if pl == gt[l]: correct_l = correct_l + 1 acc_l = correct_l / cnt print('accuracy: ', acc_s) if FLAGS.lex_txt != None: print('accuracy with lexicon: ', acc_l)
def dynamic_nem_iteration(input_data, target_data, h_old, preds_old, gamma_old, k, collisions=None, actions=None): # Get dimensions input_shape = tf.shape(input_data) assert input_shape.get_shape( )[0].value == 5, "Requires 5D input (B, K, W, H, C) but {}".format( input_shape.get_shape()[0].value) W, H, C = (x.value for x in input_data.get_shape()[-3:]) # set pixel distribution pixel_dist = 'bernoulli' # set up inner cells and nem cells inner_cell = build_network(K=k) nem_cell = NEMCell(inner_cell, input_shape=(W, H, C), distribution=pixel_dist) # compute prior prior = compute_prior(distribution=pixel_dist) # build dynamic iteration with tf.variable_scope('R-RNNEM') as varscope: with tf.name_scope('step_{}'.format(0)): # compute inputs inputs = (input_data, target_data) # feed action through hidden state if actions is not None: h_old = {'state': h_old, 'action': actions} hidden_state = (h_old, preds_old, gamma_old) # run hidden cell hidden_state, output = nem_cell(inputs, hidden_state) theta, pred, gamma = output # set collision collision = tf.zeros( (1, 1, 1, 1, 1)) if collisions is None else collisions # compute nem losses total_loss, intra_loss, inter_loss, r_total_loss, r_intra_loss, r_inter_loss = compute_outer_loss( pred, gamma, target_data, prior, pixel_distribution=pixel_dist, collision=collision) # compute estimated loss upper bound (which doesn't use E-step) total_ub_loss, intra_ub_loss, inter_ub_loss, r_total_ub_loss, r_intra_ub_loss, r_inter_ub_loss = \ compute_outer_ub_loss(pred, target_data, prior, pixel_distribution=pixel_dist, collision=collision) other_losses = tf.stack([total_loss, intra_loss, inter_loss]) other_ub_losses = tf.stack( [total_ub_loss, intra_ub_loss, inter_ub_loss]) r_other_losses = tf.stack([r_total_loss, r_intra_loss, r_inter_loss]) r_other_ub_losses = tf.stack( [r_total_ub_loss, r_intra_ub_loss, r_inter_ub_loss]) return total_loss, total_ub_loss, r_total_loss, r_total_ub_loss, theta, pred, gamma, other_losses, \ other_ub_losses, r_other_losses, r_other_ub_losses
def static_nem_iterations(input_data, target_data, k, collisions=None, actions=None): # Get dimensions input_shape = tf.shape(input_data) assert input_shape.get_shape( )[0].value == 6, "Requires 6D input (T, B, K, W, H, C) but {}".format( input_shape.get_shape()[0].value) W, H, C = (x.value for x in input_data.get_shape()[-3:]) # set pixel distribution pixel_dist = 'bernoulli' # set up inner cells and nem cells inner_cell = build_network(K=k) nem_cell = NEMCell(inner_cell, input_shape=(W, H, C), distribution=pixel_dist) # compute prior prior = compute_prior(distribution=pixel_dist) # get state initializer with tf.name_scope('initial_state'): hidden_state = nem_cell.init_state(input_shape[1], k, dtype=tf.float32) # build static iterations outputs = [hidden_state] total_losses, total_ub_losses, r_total_losses, r_total_ub_losses, other_losses, other_ub_losses, r_other_losses, r_other_ub_losses = [], [], [], [], [], [], [], [] loss_step_weights = get_loss_step_weights() with tf.variable_scope('R-RNNEM') as varscope: for t, loss_weight in enumerate(loss_step_weights): varscope.reuse_variables() if t > 0 else None with tf.name_scope('step_{}'.format(t)): # compute inputs inputs = (input_data[t], target_data[t + 1]) # feed action through hidden state if actions is not None: h_old, preds_old, gamma_old = hidden_state h_old = {'state': h_old, 'action': actions[t]} hidden_state = (h_old, preds_old, gamma_old) # run hidden cell hidden_state, output = nem_cell(inputs, hidden_state) theta, pred, gamma = output # set collision collision = tf.zeros( (1, 1, 1, 1, 1)) if collisions is None else collisions[t] # compute nem losses total_loss, intra_loss, inter_loss, r_total_loss, r_intra_loss, r_inter_loss = compute_outer_loss( pred, gamma, target_data[t + 1], prior, pixel_distribution=pixel_dist, collision=collision) # compute estimated loss upper bound (which doesn't use E-step) total_ub_loss, intra_ub_loss, inter_ub_loss, r_total_ub_loss, r_intra_ub_loss, r_inter_ub_loss = \ compute_outer_ub_loss(pred, target_data[t+1], prior, pixel_distribution=pixel_dist, collision=collision) total_losses.append(loss_weight * total_loss) total_ub_losses.append(loss_weight * total_ub_loss) r_total_losses.append(loss_weight * r_total_loss) r_total_ub_losses.append(loss_weight * r_total_ub_loss) other_losses.append(tf.stack([total_loss, intra_loss, inter_loss])) other_ub_losses.append( tf.stack([total_ub_loss, intra_ub_loss, inter_ub_loss])) r_other_losses.append( tf.stack([r_total_loss, r_intra_loss, r_inter_loss])) r_other_ub_losses.append( tf.stack([r_total_ub_loss, r_intra_ub_loss, r_inter_ub_loss])) outputs.append(output) # collect outputs with tf.name_scope('collect_outputs'): thetas, preds, gammas = zip(*outputs) thetas = tf.stack(thetas) # (T, 1, B*K, M) preds = tf.stack(preds) # (T, B, K, W, H, C) gammas = tf.stack(gammas) # (T, B, K, W, H, C) other_losses = tf.stack(other_losses) # (T, 3) other_ub_losses = tf.stack(other_ub_losses) # (T, 3) r_other_losses = tf.stack(r_other_losses) r_other_ub_losses = tf.stack(r_other_ub_losses) with tf.name_scope('total_loss'): total_loss = tf.reduce_sum( tf.stack(total_losses)) / np.sum(loss_step_weights) with tf.name_scope('total_ub_loss'): total_ub_loss = tf.reduce_sum( tf.stack(total_ub_losses)) / np.sum(loss_step_weights) with tf.name_scope('r_total_loss'): r_total_loss = tf.reduce_sum( tf.stack(r_total_losses)) / np.sum(loss_step_weights) with tf.name_scope('r_total_ub_loss'): r_total_ub_loss = tf.reduce_sum( tf.stack(r_total_ub_losses)) / np.sum(loss_step_weights) return total_loss, total_ub_loss, r_total_loss, r_total_ub_loss, thetas, preds, gammas, other_losses, \ other_ub_losses, r_other_losses, r_other_ub_losses
def train(): with open(flags.train_txt) as f: sample = [line.rstrip() for line in f] sample = np.array(sample) iteration = len(sample) // flags.batch_size data = Dataset(sample) tf.reset_default_graph() train_graph = tf.Graph() infer_graph = tf.Graph() with train_graph.as_default(): encoder_outputs, inputs = build_cnn(True, flags.batch_size, flags.height, flags.width, flags.channels) train_op, loss, sample_ids, logits, decoder_inputs, \ target_labels, learning_rate,keep_prob = build_network(encoder_outputs, False, flags.batch_size, flags.decoder_length, flags.tgt_vocab_size, flags.attn_num_hidden, flags.encoder_length, flags.max_gradient_norm ) initializer = tf.global_variables_initializer() train_saver = tf.train.Saver() train_sess = tf.Session(graph=train_graph) train_sess.run(initializer) with infer_graph.as_default(): encoder_outputs_t, inputs_t = build_cnn(False, flags.batch_size, flags.height, flags.width, flags.channels) _, _, pred_ids, logits_t, decoder_inputs_t, \ _, _ ,keep_prob_t= build_network(encoder_outputs_t, True, flags.batch_size, flags.decoder_length, flags.tgt_vocab_size, flags.attn_num_hidden, flags.encoder_length, flags.max_gradient_norm ) infer_saver = tf.train.Saver() infer_sess = tf.Session(graph=infer_graph) # Training start = time.time() acc_log = 0 count = 0 lr = flags.learning_rate for h in range(flags.epoch): for i in range(iteration): batch_train = data.next_batch(flags.batch_size) path = [] texts = [] for line in batch_train: path.append(line.split(' ')[0]) texts.append(line.split(' ')[1]) images = load_img(path, flags.height, flags.width) training_target_labels = get_label(texts, flags.decoder_length) training_decoder_inputs = np.delete(training_target_labels, -1, axis=1) training_decoder_inputs = np.c_[ np.zeros(training_decoder_inputs.shape[0]), training_decoder_inputs].T feed_dict = { inputs: images[:, :, :, np.newaxis], decoder_inputs: training_decoder_inputs, target_labels: training_target_labels, learning_rate: lr, keep_prob: 0.5 } _, loss_value = train_sess.run([train_op, loss], feed_dict=feed_dict) step = float(i + 1) if step % flags.display_step == 0: now = time.time() print(step, now - start, loss_value) start = now if step % flags.eval_step == 0: train_saver.save(train_sess, flags.save_dir) model_file = tf.train.latest_checkpoint( flags.save_dir.rsplit('/', 1)[0]) infer_saver.restore(infer_sess, model_file) gt = [] predict = [] images = load_img(path, flags.height, flags.width) testing_decoder_inputs = np.zeros( (flags.decoder_length, flags.batch_size), dtype=float) feed_dict_t = { inputs_t: images[:, :, :, np.newaxis], decoder_inputs_t: testing_decoder_inputs, keep_prob_t: 1 } q = infer_sess.run(pred_ids, feed_dict=feed_dict_t) for j in range(flags.batch_size): gt.append(texts[j]) ans = np.array(q).T[j] pd = [] for c in ans: if c != -1: character = tools.idx_to_word[c] if character != '<EOS>': pd.append(character) predict.append(''.join(pd)) correct = float(0) cnt = 0 acc_s = 0 for l in range(len(gt)): cnt = cnt + 1 if gt[l] == predict[l]: correct = correct + 1 count = count + 1 acc_s = correct / cnt if acc_s > acc_log: acc_log = acc_s count = 0 if count == (iteration // flags.eval_step): lr = lr / 5
def PolarOffsetMain(args, cfg): if args.launcher == None: dist_train = False else: args.batch_size, cfg.LOCAL_RANK = getattr( common_utils, 'init_dist_%s' % args.launcher)(args.batch_size, args.tcp_port, args.local_rank, backend='nccl') dist_train = True cfg['DIST_TRAIN'] = dist_train output_dir = os.path.join('./output', args.tag) ckpt_dir = os.path.join(output_dir, 'ckpt') tmp_dir = os.path.join(output_dir, 'tmp') summary_dir = os.path.join(output_dir, 'summary') if not os.path.exists(output_dir): os.makedirs(output_dir, exist_ok=True) if not os.path.exists(ckpt_dir): os.makedirs(ckpt_dir, exist_ok=True) if not os.path.exists(tmp_dir): os.makedirs(tmp_dir, exist_ok=True) if not os.path.exists(summary_dir): os.makedirs(summary_dir, exist_ok=True) if args.onlyval and args.saveval: results_dir = os.path.join(output_dir, 'test', 'sequences') if not os.path.exists(results_dir): os.makedirs(results_dir, exist_ok=True) for i in range(8, 9): sub_dir = os.path.join(results_dir, str(i).zfill(2), 'predictions') if not os.path.exists(sub_dir): os.makedirs(sub_dir, exist_ok=True) if args.onlytest: results_dir = os.path.join(output_dir, 'test', 'sequences') if not os.path.exists(results_dir): os.makedirs(results_dir, exist_ok=True) for i in range(11, 22): sub_dir = os.path.join(results_dir, str(i).zfill(2), 'predictions') if not os.path.exists(sub_dir): os.makedirs(sub_dir, exist_ok=True) log_file = os.path.join( output_dir, ('log_train_%s.txt' % datetime.datetime.now().strftime('%Y%m%d-%H%M%S'))) logger = common_utils.create_logger(log_file, rank=cfg.LOCAL_RANK) logger.info('**********************Start logging**********************') gpu_list = os.environ[ 'CUDA_VISIBLE_DEVICES'] if 'CUDA_VISIBLE_DEVICES' in os.environ.keys( ) else 'ALL' logger.info('CUDA_VISIBLE_DEVICES=%s' % gpu_list) if dist_train: total_gpus = dist.get_world_size() logger.info('total_batch_size: %d' % (total_gpus * args.batch_size)) for key, val in vars(args).items(): logger.info('{:16} {}'.format(key, val)) log_config_to_file(cfg, logger=logger) if cfg.LOCAL_RANK == 0: os.system('cp %s %s' % (args.config, output_dir)) ### create dataloader if (not args.onlytest) and (not args.onlyval): train_dataset_loader = build_dataloader(args, cfg, split='train', logger=logger) val_dataset_loader = build_dataloader(args, cfg, split='val', logger=logger, no_shuffle=True, no_aug=True) elif args.onlyval: val_dataset_loader = build_dataloader(args, cfg, split='val', logger=logger, no_shuffle=True, no_aug=True) else: test_dataset_loader = build_dataloader(args, cfg, split='test', logger=logger, no_shuffle=True, no_aug=True) ### create model model = build_network(cfg) model.cuda() ### create optimizer optimizer = train_utils.build_optimizer(model, cfg) ### load ckpt ckpt_fname = os.path.join(ckpt_dir, args.ckpt_name) epoch = -1 other_state = {} if args.pretrained_ckpt is not None and os.path.exists(ckpt_fname): logger.info( "Now in pretrain mode and loading ckpt: {}".format(ckpt_fname)) if not args.nofix: if args.fix_semantic_instance: logger.info( "Freezing backbone, semantic and instance part of the model." ) model.fix_semantic_instance_parameters() else: logger.info( "Freezing semantic and backbone part of the model.") model.fix_semantic_parameters() optimizer = train_utils.build_optimizer(model, cfg) epoch, other_state = train_utils.load_params_with_optimizer_otherstate( model, ckpt_fname, to_cpu=dist_train, optimizer=optimizer, logger=logger) # new feature logger.info("Loaded Epoch: {}".format(epoch)) elif args.pretrained_ckpt is not None: train_utils.load_pretrained_model(model, args.pretrained_ckpt, to_cpu=dist_train, logger=logger) if not args.nofix: if args.fix_semantic_instance: logger.info( "Freezing backbone, semantic and instance part of the model." ) model.fix_semantic_instance_parameters() else: logger.info( "Freezing semantic and backbone part of the model.") model.fix_semantic_parameters() else: logger.info("No Freeze.") optimizer = train_utils.build_optimizer(model, cfg) elif os.path.exists(ckpt_fname): epoch, other_state = train_utils.load_params_with_optimizer_otherstate( model, ckpt_fname, to_cpu=dist_train, optimizer=optimizer, logger=logger) # new feature logger.info("Loaded Epoch: {}".format(epoch)) if other_state is None: other_state = {} ### create optimizer and scheduler lr_scheduler = None if lr_scheduler == None: logger.info('Not using lr scheduler') model.train( ) # before wrap to DistributedDataParallel to support fixed some parameters if dist_train: model = nn.parallel.DistributedDataParallel( model, device_ids=[cfg.LOCAL_RANK % torch.cuda.device_count()], find_unused_parameters=True) logger.info(model) if cfg.LOCAL_RANK == 0: writer = SummaryWriter(log_dir=summary_dir) logger.info('**********************Start Training**********************') rank = cfg.LOCAL_RANK best_before_iou = -1 if 'best_before_iou' not in other_state else other_state[ 'best_before_iou'] best_pq = -1 if 'best_pq' not in other_state else other_state['best_pq'] best_after_iou = -1 if 'best_after_iou' not in other_state else other_state[ 'best_after_iou'] global_iter = 0 if 'global_iter' not in other_state else other_state[ 'global_iter'] val_global_iter = 0 if 'val_global_iter' not in other_state else other_state[ 'val_global_iter'] best_tracking_loss = 10086 if 'best_tracking_loss' not in other_state else other_state[ 'best_tracking_loss'] ### test if args.onlytest: logger.info('----EPOCH {} Testing----'.format(epoch)) model.eval() if rank == 0: vbar = tqdm(total=len(test_dataset_loader), dynamic_ncols=True) for i_iter, inputs in enumerate(test_dataset_loader): with torch.no_grad(): if cfg.MODEL.NAME.startswith( 'PolarOffsetSpconvPytorchMeanshiftTracking' ) or cfg.MODEL.NAME.startswith('PolarOffsetSpconvTracking'): ret_dict = model(inputs, is_test=True, merge_evaluator_list=None, merge_evaluator_window_k_list=None, require_cluster=True) else: ret_dict = model(inputs, is_test=True, require_cluster=True, require_merge=True) common_utils.save_test_results(ret_dict, results_dir, inputs) if rank == 0: vbar.set_postfix({ 'fname': '/'.join(inputs['pcd_fname'][0].split('/')[-3:]) }) vbar.update(1) if rank == 0: vbar.close() logger.info("----Testing Finished----") return ### evaluate if args.onlyval: logger.info('----EPOCH {} Evaluating----'.format(epoch)) model.eval() min_points = 50 # according to SemanticKITTI official rule if cfg.MODEL.NAME.startswith( 'PolarOffsetSpconvPytorchMeanshiftTracking' ) or cfg.MODEL.NAME.startswith('PolarOffsetSpconvTracking'): merge_evaluator_list = [] merge_evaluator_window_k_list = [] for k in [1, 5, 10, 15]: merge_evaluator_list.append(init_eval(min_points)) merge_evaluator_window_k_list.append(k) else: before_merge_evaluator = init_eval(min_points=min_points) after_merge_evaluator = init_eval(min_points=min_points) if rank == 0: vbar = tqdm(total=len(val_dataset_loader), dynamic_ncols=True) for i_iter, inputs in enumerate(val_dataset_loader): inputs['i_iter'] = i_iter # torch.cuda.empty_cache() with torch.no_grad(): if cfg.MODEL.NAME.startswith( 'PolarOffsetSpconvPytorchMeanshiftTracking' ) or cfg.MODEL.NAME.startswith('PolarOffsetSpconvTracking'): ret_dict = model(inputs, is_test=True, merge_evaluator_list=merge_evaluator_list, merge_evaluator_window_k_list= merge_evaluator_window_k_list, require_cluster=True) else: ret_dict = model( inputs, is_test=True, before_merge_evaluator=before_merge_evaluator, after_merge_evaluator=after_merge_evaluator, require_cluster=True) ######################### # with open('./ipnb/{}_matching_list.pkl'.format(i_iter), 'wb') as fd: # pickle.dump(ret_dict['matching_list'], fd) ######################### if args.saveval: common_utils.save_test_results(ret_dict, results_dir, inputs) if rank == 0: vbar.set_postfix({ 'loss': ret_dict['loss'].item(), 'fname': '/'.join(inputs['pcd_fname'][0].split('/')[-3:]), 'ins_num': -1 if 'ins_num' not in ret_dict else ret_dict['ins_num'] }) vbar.update(1) if dist_train: if cfg.MODEL.NAME.startswith( 'PolarOffsetSpconvPytorchMeanshiftTracking' ) or cfg.MODEL.NAME.startswith('PolarOffsetSpconvTracking'): pass else: before_merge_evaluator = common_utils.merge_evaluator( before_merge_evaluator, tmp_dir) dist.barrier() after_merge_evaluator = common_utils.merge_evaluator( after_merge_evaluator, tmp_dir) if rank == 0: vbar.close() if rank == 0: ## print results if cfg.MODEL.NAME.startswith( 'PolarOffsetSpconvPytorchMeanshiftTracking' ) or cfg.MODEL.NAME.startswith('PolarOffsetSpconvTracking'): for evaluate, window_k in zip(merge_evaluator_list, merge_evaluator_window_k_list): logger.info("Current Window K: {}".format(window_k)) printResults(evaluate, logger=logger) else: logger.info("Before Merge Semantic Scores") before_merge_results = printResults(before_merge_evaluator, logger=logger, sem_only=True) logger.info("After Merge Panoptic Scores") after_merge_results = printResults(after_merge_evaluator, logger=logger) logger.info("----Evaluating Finished----") return ### train while True: epoch += 1 if 'MAX_EPOCH' in cfg.OPTIMIZE.keys(): if epoch > cfg.OPTIMIZE.MAX_EPOCH: break ### train one epoch logger.info('----EPOCH {} Training----'.format(epoch)) loss_acc = 0 if rank == 0: pbar = tqdm(total=len(train_dataset_loader), dynamic_ncols=True) for i_iter, inputs in enumerate(train_dataset_loader): # torch.cuda.empty_cache() torch.autograd.set_detect_anomaly(True) model.train() optimizer.zero_grad() inputs['i_iter'] = i_iter inputs['rank'] = rank ret_dict = model(inputs) if args.pretrained_ckpt is not None and not args.fix_semantic_instance: # training offset if args.nofix: loss = ret_dict['loss'] elif len(ret_dict['offset_loss_list']) > 0: loss = sum(ret_dict['offset_loss_list']) else: loss = torch.tensor(0.0, requires_grad=True) #mock pbar ret_dict['offset_loss_list'] = [loss] #mock writer elif args.pretrained_ckpt is not None and args.fix_semantic_instance and cfg.MODEL.NAME == 'PolarOffsetSpconvPytorchMeanshift': # training dynamic shifting loss = sum(ret_dict['meanshift_loss']) elif cfg.MODEL.NAME.startswith( 'PolarOffsetSpconvPytorchMeanshiftTracking' ) or cfg.MODEL.NAME.startswith('PolarOffsetSpconvTracking'): loss = sum(ret_dict['tracking_loss']) ######################### # with open('./ipnb/{}_matching_list.pkl'.format(i_iter), 'wb') as fd: # pickle.dump(ret_dict['matching_list'], fd) ######################### else: loss = ret_dict['loss'] loss.backward() optimizer.step() if rank == 0: try: cur_lr = float(optimizer.lr) except: cur_lr = optimizer.param_groups[0]['lr'] loss_acc += loss.item() pbar.set_postfix({ 'loss': loss.item(), 'lr': cur_lr, 'mean_loss': loss_acc / float(i_iter + 1) }) pbar.update(1) writer.add_scalar('Train/01_Loss', ret_dict['loss'].item(), global_iter) writer.add_scalar('Train/02_SemLoss', ret_dict['sem_loss'].item(), global_iter) if 'offset_loss_list' in ret_dict and sum( ret_dict['offset_loss_list']).item() > 0: writer.add_scalar('Train/03_InsLoss', sum(ret_dict['offset_loss_list']).item(), global_iter) writer.add_scalar('Train/04_LR', cur_lr, global_iter) writer_acc = 5 if 'meanshift_loss' in ret_dict: writer.add_scalar('Train/05_DSLoss', sum(ret_dict['meanshift_loss']).item(), global_iter) writer_acc += 1 if 'tracking_loss' in ret_dict: writer.add_scalar('Train/06_TRLoss', sum(ret_dict['tracking_loss']).item(), global_iter) writer_acc += 1 more_keys = [] for k, _ in ret_dict.items(): if k.find('summary') != -1: more_keys.append(k) for ki, k in enumerate(more_keys): if k == 'bandwidth_weight_summary': continue ki += writer_acc writer.add_scalar( 'Train/{}_{}'.format(str(ki).zfill(2), k), ret_dict[k], global_iter) global_iter += 1 if rank == 0: pbar.close() ### evaluate after each epoch logger.info('----EPOCH {} Evaluating----'.format(epoch)) model.eval() min_points = 50 before_merge_evaluator = init_eval(min_points=min_points) after_merge_evaluator = init_eval(min_points=min_points) tracking_loss = 0 if rank == 0: vbar = tqdm(total=len(val_dataset_loader), dynamic_ncols=True) for i_iter, inputs in enumerate(val_dataset_loader): # torch.cuda.empty_cache() inputs['i_iter'] = i_iter inputs['rank'] = rank with torch.no_grad(): if cfg.MODEL.NAME.startswith( 'PolarOffsetSpconvPytorchMeanshiftTracking' ) or cfg.MODEL.NAME.startswith('PolarOffsetSpconvTracking'): ret_dict = model(inputs, is_test=True, merge_evaluator_list=None, merge_evaluator_window_k_list=None, require_cluster=True) else: ret_dict = model( inputs, is_test=True, before_merge_evaluator=before_merge_evaluator, after_merge_evaluator=after_merge_evaluator, require_cluster=True) if rank == 0: vbar.set_postfix({'loss': ret_dict['loss'].item()}) vbar.update(1) writer.add_scalar('Val/01_Loss', ret_dict['loss'].item(), val_global_iter) writer.add_scalar('Val/02_SemLoss', ret_dict['sem_loss'].item(), val_global_iter) if 'offset_loss_list' in ret_dict and sum( ret_dict['offset_loss_list']).item() > 0: writer.add_scalar('Val/03_InsLoss', sum(ret_dict['offset_loss_list']).item(), val_global_iter) if 'tracking_loss' in ret_dict: writer.add_scalar('Val/06_TRLoss', sum(ret_dict['tracking_loss']).item(), global_iter) tracking_loss += sum(ret_dict['tracking_loss']).item() more_keys = [] for k, _ in ret_dict.items(): if k.find('summary') != -1: more_keys.append(k) for ki, k in enumerate(more_keys): if k == 'bandwidth_weight_summary': continue ki += 4 writer.add_scalar('Val/{}_{}'.format(str(ki).zfill(2), k), ret_dict[k], val_global_iter) val_global_iter += 1 tracking_loss /= len(val_dataset_loader) if dist_train: try: before_merge_evaluator = common_utils.merge_evaluator( before_merge_evaluator, tmp_dir, prefix='before_') dist.barrier() after_merge_evaluator = common_utils.merge_evaluator( after_merge_evaluator, tmp_dir, prefix='after_') except: print("Someting went wrong when merging evaluator in rank {}". format(rank)) if rank == 0: vbar.close() if rank == 0: ## print results logger.info("Before Merge Semantic Scores") before_merge_results = printResults(before_merge_evaluator, logger=logger, sem_only=True) logger.info("After Merge Panoptic Scores") after_merge_results = printResults(after_merge_evaluator, logger=logger) ## save ckpt other_state = { 'best_before_iou': best_before_iou, 'best_pq': best_pq, 'best_after_iou': best_after_iou, 'global_iter': global_iter, 'val_global_iter': val_global_iter, 'best_tracking_loss': best_tracking_loss, } saved_flag = False if best_tracking_loss > tracking_loss and cfg.MODEL.NAME.startswith( 'PolarOffsetSpconvPytorchMeanshiftTracking' ) or cfg.MODEL.NAME.startswith('PolarOffsetSpconvTracking'): best_tracking_loss = tracking_loss if not saved_flag: states = train_utils.checkpoint_state( model, optimizer, epoch, other_state) train_utils.save_checkpoint( states, os.path.join( ckpt_dir, 'checkpoint_epoch_{}_{}.pth'.format( epoch, str(tracking_loss)[:5]))) saved_flag = True if best_before_iou < before_merge_results['iou_mean']: best_before_iou = before_merge_results['iou_mean'] if not saved_flag: states = train_utils.checkpoint_state( model, optimizer, epoch, other_state) train_utils.save_checkpoint( states, os.path.join( ckpt_dir, 'checkpoint_epoch_{}_{}_{}_{}.pth'.format( epoch, str(best_before_iou)[:5], str(best_pq)[:5], str(best_after_iou)[:5]))) saved_flag = True if best_pq < after_merge_results['pq_mean']: best_pq = after_merge_results['pq_mean'] if not saved_flag: states = train_utils.checkpoint_state( model, optimizer, epoch, other_state) train_utils.save_checkpoint( states, os.path.join( ckpt_dir, 'checkpoint_epoch_{}_{}_{}_{}.pth'.format( epoch, str(best_before_iou)[:5], str(best_pq)[:5], str(best_after_iou)[:5]))) saved_flag = True if best_after_iou < after_merge_results['iou_mean']: best_after_iou = after_merge_results['iou_mean'] if not saved_flag: states = train_utils.checkpoint_state( model, optimizer, epoch, other_state) train_utils.save_checkpoint( states, os.path.join( ckpt_dir, 'checkpoint_epoch_{}_{}_{}_{}.pth'.format( epoch, str(best_before_iou)[:5], str(best_pq)[:5], str(best_after_iou)[:5]))) saved_flag = True logger.info("Current best before IoU: {}".format(best_before_iou)) logger.info("Current best after IoU: {}".format(best_after_iou)) logger.info("Current best after PQ: {}".format(best_pq)) logger.info( "Current best tracking loss: {}".format(best_tracking_loss)) if lr_scheduler != None: lr_scheduler.step(epoch) # new feature