def train(args): data_loader = DataLoader(args.batch_size, args.seq_length, args.data_scale) with open(os.path.join('save', 'config.pkl'), 'w') as f: cPickle.dump(args, f) model = Model(args) with tf.Session() as sess: tf.initialize_all_variables().run() saver = tf.train.Saver(tf.all_variables()) for e in xrange(args.num_epochs): sess.run(tf.assign(model.lr, args.learning_rate * (args.decay_rate ** e))) data_loader.reset_batch_pointer() state = model.initial_state.eval() for b in xrange(data_loader.num_batches): start = time.time() x, y = data_loader.next_batch() feed = {model.input_data: x, model.target_data: y, model.initial_state: state} train_loss, state, _ = sess.run([model.cost, model.final_state, model.train_op], feed) end = time.time() print "{}/{} (epoch {}), train_loss = {:.3f}, time/batch = {:.3f}" \ .format(e * data_loader.num_batches + b, args.num_epochs * data_loader.num_batches, e, train_loss, end - start) if (e * data_loader.num_batches + b) % args.save_every == 0 and ((e * data_loader.num_batches + b) > 0): checkpoint_path = os.path.join('save', 'model.ckpt') saver.save(sess, checkpoint_path, global_step = e * data_loader.num_batches + b) print "model saved to {}".format(checkpoint_path)
def train(args): dim = 6 data_loader = DataLoader(dim, args.batch_size, args.seq_length, reprocess=args.reprocess) x, y = data_loader.next_batch() if not os.path.exists(args.save_dir): os.mkdir(args.save_dir) with open(os.path.join(args.save_dir, 'config.pkl'), 'w') as f: cPickle.dump(args, f) model = Model(dim, args) with tf.Session() as sess: tf.initialize_all_variables().run() ts = TrainingStatus(sess, args.num_epochs, data_loader.num_batches, save_interval = args.save_every, graph_def = sess.graph_def, save_dir = args.save_dir) for e in xrange(ts.startEpoch, args.num_epochs): sess.run(tf.assign(model.lr, args.learning_rate * (args.decay_rate ** e))) data_loader.reset_batch_pointer() state = model.initial_state.eval() for b in xrange(data_loader.num_batches): ts.tic() x, y = data_loader.next_batch() feed = {model.input_data: x, model.target_data: y, model.initial_state: state} summary, train_loss, state, _ = sess.run([model.summary, model.cost, model.final_state, model.train_op], feed) print ts.tocBatch(summary, e, b, train_loss) ts.tocEpoch(sess, e)
def evals(net, adata ,alabel, batch_size): hidden = net.begin_state(func=mx.nd.zeros,batch_size = batch_size,ctx=mx.cpu()) dataLoader = DataLoader(adata, alabel) tl = 0 for data, label in dataLoader.dataIter(batch_size): label = nd.array(label) #label = nd.ones(shape=(5,batch_size)) * label #label = label.reshape((-1,)) dd = nd.array(data.reshape((batch_size,5,11)).swapaxes(0,1)) #hidden = detach(hidden) output,hidden = net(dd, hidden) output = output.reshape((5,batch_size,1)) output = nd.sum(output,axis=0)/5 lv = loss(output, label) tl += nd.sum(lv).asscalar() return tl / len(adata)
related_path, antonyms_path, _embedding_size=EMBEDDING_SIZE) lang.load_gensim_word2vec(embeddings_path) # load pre-trained embeddings # this creates TF matrix of embeddings embeddings_matrix = lang.create_embeddings() #embeddings_matrix = lang.create_temp_embeddings() # this loads a matrix of on_sense -> related words mappings related = load_related(related_path) # load tf_records tf_records_files = [ path_to_tf_records + f for f in os.listdir(path_to_tf_records) if f[0] != "." ] data_loader = DataLoader() dataset = data_loader.make_dataset(tf_records_files, batch_size=BATCH_SIZE) eval_data_loader = EvalDataLoader() validation_dataset = eval_data_loader.make_dataset([path_to_valid_records], batch_size=BATCH_SIZE) test_dataset = eval_data_loader.make_dataset([path_to_test_records], batch_size=BATCH_SIZE) train(embedding_matrix=embeddings_matrix, related_matrix=related, dataset=dataset, validation_dataset=validation_dataset, test_dataset=test_dataset, encoder_hidden_size=ENCODER_HIDDEN_SIZE, encoder_embedding_size=EMBEDDING_SIZE, _learning_rate=LEARNING_RATE,
def train(args): # Construct the DataLoader object dataloader = DataLoader(args.batch_size, args.seq_length + 1, args.train_dataset, forcePreProcess=True) # Construct the ST-graph object stgraph = ST_GRAPH(1, args.seq_length + 1) # Log directory log_directory = 'log/trainedOn_'+ str(args.train_dataset) if not os.path.exists(log_directory): os.makedirs(log_directory) # Logging file log_file_curve = open(os.path.join(log_directory, 'log_curve.txt'), 'w') log_file = open(os.path.join(log_directory, 'val.txt'), 'w') # Save directory for saving the model save_directory = 'save/trainedOn_'+str(args.train_dataset) if not os.path.exists(save_directory): os.makedirs(save_directory) # Open the configuration file with open(os.path.join(save_directory, 'config.pkl'), 'wb') as f: pickle.dump(args, f) #store arguments from parser # Path to store the checkpoint file def checkpoint_path(x): return os.path.join(save_directory, 'srnn_model_'+str(x)+'.tar') # Initialize net net = SRNN(args) if args.use_cuda: net = net.cuda() # optimizer = torch.optim.Adam(net.parameters(), lr=args.learning_rate) # optimizer = torch.optim.RMSprop(net.parameters(), lr=args.learning_rate, momentum=0.0001, centered=True) optimizer = torch.optim.Adagrad(net.parameters()) learning_rate = args.learning_rate print('Training begin') best_val_loss = 100 best_epoch = 0 # Training for epoch in range(args.num_epochs): dataloader.reset_batch_pointer(valid=False) loss_epoch = 0 # For each batch for batch in range(dataloader.num_batches): start = time.time() # Get batch data x, _, _, d = dataloader.next_batch(randomUpdate=True) # Loss for this batch loss_batch = 0 # For each sequence in the batch for sequence in range(dataloader.batch_size): # Construct the graph for the current sequence stgraph.readGraph([x[sequence]]) nodes, edges, nodesPresent, edgesPresent = stgraph.getSequence() # Convert to cuda variables nodes = Variable(torch.from_numpy(nodes).float()) if args.use_cuda: nodes = nodes.cuda() edges = Variable(torch.from_numpy(edges).float()) if args.use_cuda: edges = edges.cuda() # Define hidden states numNodes = nodes.size()[1] hidden_states_node_RNNs = Variable(torch.zeros(numNodes, args.human_node_rnn_size)) if args.use_cuda: hidden_states_node_RNNs = hidden_states_node_RNNs.cuda() hidden_states_edge_RNNs = Variable(torch.zeros(numNodes*numNodes, args.human_human_edge_rnn_size)) if args.use_cuda: hidden_states_edge_RNNs = hidden_states_edge_RNNs.cuda() cell_states_node_RNNs = Variable(torch.zeros(numNodes, args.human_node_rnn_size)) if args.use_cuda: cell_states_node_RNNs = cell_states_node_RNNs.cuda() cell_states_edge_RNNs = Variable(torch.zeros(numNodes*numNodes, args.human_human_edge_rnn_size)) if args.use_cuda: cell_states_edge_RNNs = cell_states_edge_RNNs.cuda() # Zero out the gradients net.zero_grad() optimizer.zero_grad() # Forward prop outputs, _, _, _, _, _ = net(nodes[:args.seq_length], edges[:args.seq_length], nodesPresent[:-1], edgesPresent[:-1], hidden_states_node_RNNs, hidden_states_edge_RNNs, cell_states_node_RNNs, cell_states_edge_RNNs) # Compute loss loss = Gaussian2DLikelihood(outputs, nodes[1:], nodesPresent[1:], args.pred_length) loss_batch += loss.data[0] # Compute gradients loss.backward() # Clip gradients torch.nn.utils.clip_grad_norm(net.parameters(), args.grad_clip) # Update parameters optimizer.step() # Reset the stgraph stgraph.reset() end = time.time() loss_batch = loss_batch / dataloader.batch_size loss_epoch += loss_batch print( '{}/{} (epoch {}), train_loss = {:.3f}, time/batch = {:.3f}'.format(epoch * dataloader.num_batches + batch, args.num_epochs * dataloader.num_batches, epoch, loss_batch, end - start)) # Compute loss for the entire epoch loss_epoch /= dataloader.num_batches # Log it log_file_curve.write(str(epoch)+','+str(loss_epoch)+',') # Validation dataloader.reset_batch_pointer(valid=True) loss_epoch = 0 for batch in range(dataloader.valid_num_batches): # Get batch data x, _, d = dataloader.next_valid_batch(randomUpdate=False) # Loss for this batch loss_batch = 0 for sequence in range(dataloader.batch_size): stgraph.readGraph([x[sequence]]) nodes, edges, nodesPresent, edgesPresent = stgraph.getSequence() # Convert to cuda variables nodes = Variable(torch.from_numpy(nodes).float()) if args.use_cuda: nodes = nodes.cuda() edges = Variable(torch.from_numpy(edges).float()) if args.use_cuda: edges = edges.cuda() # Define hidden states numNodes = nodes.size()[1] hidden_states_node_RNNs = Variable(torch.zeros(numNodes, args.human_node_rnn_size)) if args.use_cuda: hidden_states_node_RNNs = hidden_states_node_RNNs.cuda() hidden_states_edge_RNNs = Variable(torch.zeros(numNodes*numNodes, args.human_human_edge_rnn_size)) if args.use_cuda: hidden_states_edge_RNNs = hidden_states_edge_RNNs.cuda() cell_states_node_RNNs = Variable(torch.zeros(numNodes, args.human_node_rnn_size)) if args.use_cuda: cell_states_node_RNNs = cell_states_node_RNNs.cuda() cell_states_edge_RNNs = Variable(torch.zeros(numNodes*numNodes, args.human_human_edge_rnn_size)) if args.use_cuda: cell_states_edge_RNNs = cell_states_edge_RNNs.cuda() outputs, _, _, _, _, _ = net(nodes[:args.seq_length], edges[:args.seq_length], nodesPresent[:-1], edgesPresent[:-1], hidden_states_node_RNNs, hidden_states_edge_RNNs, cell_states_node_RNNs, cell_states_edge_RNNs) # Compute loss loss = Gaussian2DLikelihood(outputs, nodes[1:], nodesPresent[1:], args.pred_length) loss_batch += loss.data[0] # Reset the stgraph stgraph.reset() loss_batch = loss_batch / dataloader.batch_size loss_epoch += loss_batch loss_epoch = loss_epoch / dataloader.valid_num_batches #Saving the model\ if loss_epoch < best_val_loss or args.save_every: print('Saving model') torch.save({ 'epoch': epoch, 'state_dict': net.state_dict(), 'optimizer_state_dict': optimizer.state_dict() }, checkpoint_path(epoch)) #save_best_model overwriting the earlier file #if loss_epoch < best_val_loss: # Update best validation loss until now if loss_epoch < best_val_loss: best_val_loss = loss_epoch best_epoch = epoch # Record best epoch and best validation loss print('(epoch {}), valid_loss = {:.3f}'.format(epoch, loss_epoch)) print('Best epoch {}, Best validation loss {}'.format(best_epoch, best_val_loss)) # Log it log_file_curve.write(str(loss_epoch)+'\n') # Record the best epoch and best validation loss overall print('Best epoch {}, Best validation loss {}'.format(best_epoch, best_val_loss)) # Log it log_file.write(str(best_epoch)+','+str(best_val_loss)) # Close logging files log_file.close() log_file_curve.close()
def train(args): prefix = '' f_prefix = '.' if not os.path.isdir("log/"): print("Directory creation script is running...") subprocess.call([f_prefix+'/make_directories.sh']) args.freq_validation = np.clip(args.freq_validation, 0, args.num_epochs) validation_epoch_list = list(range(args.freq_validation, args.num_epochs+1, args.freq_validation)) validation_epoch_list[-1]-=1 # Create the data loader object. This object would preprocess the data in terms of # batches each of size args.batch_size, of length args.seq_length dataloader = DataLoader(f_prefix, args.batch_size, args.seq_length, args.num_validation, forcePreProcess=True) method_name = "VANILLALSTM" model_name = "LSTM" save_tar_name = method_name+"_lstm_model_" if args.gru: model_name = "GRU" save_tar_name = method_name+"_gru_model_" # Log directory log_directory = os.path.join(prefix, 'log/') plot_directory = os.path.join(prefix, 'plot/', method_name, model_name) plot_train_file_directory = 'validation' # Logging files log_file_curve = open(os.path.join(log_directory, method_name, model_name,'log_curve.txt'), 'w+') log_file = open(os.path.join(log_directory, method_name, model_name, 'val.txt'), 'w+') # model directory save_directory = os.path.join(f_prefix, 'model') # Save the arguments int the config file with open(os.path.join(save_directory, method_name, model_name,'config.pkl'), 'wb') as f: pickle.dump(args, f) # Path to store the checkpoint file def checkpoint_path(x): return os.path.join(save_directory, method_name, model_name, save_tar_name+str(x)+'.tar') # model creation net = LSTM(args) if args.use_cuda: net = net.cuda() # optimizer = torch.optim.Adagrad(net.parameters(), weight_decay=args.lambda_param) optimizer = torch.optim.RMSprop(net.parameters(), lr=args.learning_rate) loss_f = torch.nn.MSELoss() learning_rate = args.learning_rate best_val_loss = 100 best_val_data_loss = 100 smallest_err_val = 100000 smallest_err_val_data = 100000 best_epoch_val = 0 best_epoch_val_data = 0 best_err_epoch_val = 0 best_err_epoch_val_data = 0 all_epoch_results = [] grids = [] num_batch = 0 # Training for epoch in range(args.num_epochs): print('****************Training epoch beginning******************') if dataloader.additional_validation and (epoch-1) in validation_epoch_list: dataloader.switch_to_dataset_type(True) dataloader.reset_batch_pointer(valid=False) loss_epoch = 0 # For each batch # num_batches 資料可以被分多少批 要跑幾個iter for batch in range(dataloader.num_batches): start = time.time() # print(dataloader.num_batches, dataloader.batch_size) # Get batch data x, y, d = dataloader.next_batch(randomUpdate=False) loss_batch = 0 # x_cat = Variable(torch.from_numpy(np.array(x[0])).float()) x_seq = np.array(x) y_seq = np.array(y) x_seq = Variable(torch.from_numpy(x_seq).float()) y_seq = Variable(torch.from_numpy(y_seq).float()) temp = x_seq[:,:,-2:] x_seq = x_seq[:,:,:-2] y_seq = y_seq[:,:,:3] hidden_states = Variable(torch.zeros(x_seq.size()[0], args.rnn_size)) cell_states = Variable(torch.zeros(x_seq.size()[0], args.rnn_size)) if args.use_cuda: x_seq = x_seq.cuda() y_seq = y_seq.cuda() temp = temp.cuda() hidden_states = hidden_states.cuda() cell_states = cell_states.cuda() # Zero out gradients net.zero_grad() optimizer.zero_grad() outputs, _, _ = net(x_seq, temp, hidden_states, cell_states) loss = loss_f(outputs, y_seq) loss_batch = loss.detach().item() # Compute gradients loss.backward() # Clip gradients torch.nn.utils.clip_grad_norm_(net.parameters(), args.grad_clip) # Update parameters optimizer.step() end = time.time() loss_epoch += loss_batch print('{}/{} (epoch {}), train_loss = {:.3f}, time/batch = {:.3f}'.format((batch+1) * dataloader.batch_size, dataloader.num_batches * dataloader.batch_size, epoch, loss_batch, end - start)) loss_epoch /= dataloader.num_batches print("Training epoch: "+str(epoch)+" loss: "+str(loss_epoch)) #Log loss values log_file_curve.write("Training epoch: "+str(epoch)+" loss: "+str(loss_epoch)+'\n') # Validation dataset if dataloader.additional_validation and (epoch) in validation_epoch_list: dataloader.switch_to_dataset_type() print('****************Validation with dataset epoch beginning******************') dataloader.reset_batch_pointer(valid=False) dataset_pointer_ins = dataloader.dataset_pointer validation_dataset_executed = True loss_epoch = 0 err_epoch = 0 num_of_batch = 0 smallest_err = 100000 #results of one epoch for all validation datasets epoch_result = [] #results of one validation dataset results = [] # For each batch for batch in range(dataloader.num_batches): # Get batch data x, y, d = dataloader.next_batch(randomUpdate=False) # Loss for this batch loss_batch = 0 err_batch = 0 # For each sequence for sequence in range(len(x)): # Get the sequence x_seq = x[sequence] y_seq = y[sequence] x_seq= np.array(x_seq) y_seq= np.array(y_seq)[:,:3] x_seq = Variable(torch.from_numpy(x_seq).float()) y_seq = Variable(torch.from_numpy(y_seq).float()) temp = x_seq[:,-2:] x_seq = x_seq[:,:-2] y_seq = y_seq[:,:3] if args.use_cuda: x_seq = x_seq.cuda() y_seq = y_seq.cuda() temp = temp.cuda() #will be used for error calculation orig_x_seq = y_seq.clone() # print(x_seq.size(), args.seq_length) with torch.no_grad(): hidden_states = Variable(torch.zeros(1, args.rnn_size)) cell_states = Variable(torch.zeros(1, args.rnn_size)) ret_x_seq = Variable(torch.zeros(args.seq_length, net.input_size)) # all_outputs = Variable(torch.zeros(1, args.seq_length, net.input_size)) # Initialize the return data structure if args.use_cuda: ret_x_seq = ret_x_seq.cuda() hidden_states = hidden_states.cuda() cell_states = cell_states.cuda() total_loss = 0 # For the observed part of the trajectory for tstep in range(args.seq_length): outputs, hidden_states, cell_states = net(x_seq[tstep].view(1, 1, net.input_size), temp[tstep].view(1, 1, temp.size()[-1]), hidden_states, cell_states) ret_x_seq[tstep, 0] = outputs[0,0,0] ret_x_seq[tstep, 1] = outputs[0,0,1] ret_x_seq[tstep, 2] = outputs[0,0,2] print(outputs.size(), ) loss = loss_f(outputs, y_seq[tstep].view(1, 1, y_seq.size()[1])) total_loss += loss total_loss = total_loss / args.seq_length #get mean and final error # print(ret_x_seq.size(), y_seq.size()) err = get_mean_error(ret_x_seq.data, y_seq.data, args.use_cuda) loss_batch += total_loss.item() err_batch += err print('Current file : ',' Batch : ', batch+1, ' Sequence: ', sequence+1, ' Sequence mean error: ', err, 'valid_loss: ',total_loss.item()) results.append((y_seq.data.cpu().numpy(), ret_x_seq.data.cpu().numpy())) loss_batch = loss_batch / dataloader.batch_size err_batch = err_batch / dataloader.batch_size num_of_batch += 1 loss_epoch += loss_batch err_epoch += err_batch epoch_result.append(results) all_epoch_results.append(epoch_result) if dataloader.num_batches != 0: loss_epoch = loss_epoch / dataloader.num_batches err_epoch = err_epoch / dataloader.num_batches # avarage_err = (err_epoch + f_err_epoch)/2 # Update best validation loss until now if loss_epoch < best_val_data_loss: best_val_data_loss = loss_epoch best_epoch_val_data = epoch if err_epoch<smallest_err_val_data: # Save the model after each epoch print('Saving model') torch.save({ 'epoch': epoch, 'state_dict': net.state_dict(), 'optimizer_state_dict': optimizer.state_dict() }, checkpoint_path(epoch)) smallest_err_val_data = err_epoch best_err_epoch_val_data = epoch print('(epoch {}), valid_loss = {:.3f}, valid_mean_err = {:.3f}'.format(epoch, loss_epoch, err_epoch)) print('Best epoch', best_epoch_val_data, 'Best validation loss', best_val_data_loss, 'Best error epoch',best_err_epoch_val_data, 'Best error', smallest_err_val_data) log_file_curve.write("Validation dataset epoch: "+str(epoch)+" loss: "+str(loss_epoch)+" mean_err: "+str(err_epoch.data.cpu().numpy())+'\n') optimizer = time_lr_scheduler(optimizer, epoch, lr_decay_epoch = args.freq_optimizer) if dataloader.valid_num_batches != 0: print('Best epoch', best_epoch_val, 'Best validation Loss', best_val_loss, 'Best error epoch',best_err_epoch_val, 'Best error', smallest_err_val) # Log the best epoch and best validation loss log_file.write('Validation Best epoch:'+str(best_epoch_val_data)+','+' Best validation Loss: '+str(best_val_data_loss)) if dataloader.additional_validation: print('Best epoch acording to validation dataset', best_epoch_val_data, 'Best validation Loss', best_val_data_loss, 'Best error epoch',best_err_epoch_val_data, 'Best error', smallest_err_val_data) log_file.write("Validation dataset Best epoch: "+str(best_epoch_val_data)+','+' Best validation Loss: '+str(best_val_data_loss)+'Best error epoch: ',str(best_err_epoch_val_data),'\n') #dataloader.write_to_plot_file(all_epoch_results[best_epoch_val_data], plot_directory) #elif dataloader.valid_num_batches != 0: # dataloader.write_to_plot_file(all_epoch_results[best_epoch_val], plot_directory) #else: if validation_dataset_executed: dataloader.switch_to_dataset_type(load_data=False) create_directories(plot_directory, [plot_train_file_directory]) dataloader.write_to_plot_file(all_epoch_results[len(all_epoch_results)-1], os.path.join(plot_directory, plot_train_file_directory)) # Close logging files log_file.close() log_file_curve.close()
class data_augmentator(): # class for data augmentation def __init__(self,f_prefix, num_of_data, seq_length, val_percent): self.base_train_path = 'data/train/' self.base_validation_path = 'data/validation/' # list of angles will be use for rotation self.angles = list(range(0,360,30)) self.num_of_data = np.clip(num_of_data, 0, len(self.angles) -1) self.num_validation_data = math.ceil(self.num_of_data * val_percent) # number of validation dataset self.num_train_data = self.num_of_data - self.num_validation_data # number of train dataset print("For each dataset -----> Number of additional training dataset: ", self.num_train_data, " Number of validation dataset: ", self.num_validation_data) self.num_validation_data =+1 self.seq_length = seq_length self.val_percent = val_percent self.f_prefix = f_prefix self.dataloader = DataLoader(f_prefix, 1, seq_length , 0 ,forcePreProcess = True, infer = False, generate=True) # noise parameter definition self.noise_std_min = 0.05 self.noise_std_max = 0.15 self.noise_std = random.uniform(self.noise_std_min, self.noise_std_max) self.noise_mean = 0.0 # remove datasets from directories for new creation self.clear_directories(self.base_train_path) self.clear_directories(self.base_validation_path, True) self.random_dataset_creation() def random_dataset_creation(self): self.dataloader.reset_batch_pointer(valid=False) dataset_pointer_ins = self.dataloader.dataset_pointer dataset_instances = {} whole_dataset = [] random_angles = random.sample(self.angles, self.num_of_data) file_name = self.dataloader.get_file_name() print("Dataset creation for: ", file_name, " angles: ", random_angles) for batch in range(self.dataloader.num_batches): start = time.time() # Get data x, y, d , numPedsList, PedsList, _= self.dataloader.next_batch() dir_name = self.dataloader.get_directory_name() file_name = self.dataloader.get_file_name() # Get the sequence x_seq,d_seq ,numPedsList_seq, PedsList_seq = x[0], d[0], numPedsList[0], PedsList[0] # convert dense vector x_seq , lookup_seq = self.dataloader.convert_proper_array(x_seq, numPedsList_seq, PedsList_seq) if dataset_pointer_ins is not self.dataloader.dataset_pointer: if self.dataloader.dataset_pointer is not 0: whole_dataset.append(dataset_instances) dataset_instances = {} random_angles = random.sample(self.angles, self.num_of_data) # sample new angle self.noise_std = random.uniform(self.noise_std_min, self.noise_std_max) #sample new noise print("Dataset creation for: ", file_name, " angles: ", random_angles) dataset_pointer_ins = self.dataloader.dataset_pointer for index, angle in enumerate(random_angles): self.noise_std = random.uniform(self.noise_std_min, self.noise_std_max) # modify and preprocess dataset modified_x_seq = self.submision_seq_preprocess(self.handle_seq(x_seq, lookup_seq, PedsList_seq, angle), self.seq_length, lookup_seq) # store modified data points to dict self.dataloader.add_element_to_dict(dataset_instances, (dir_name, file_name, index), modified_x_seq) end = time.time() print('Current file : ', file_name,' Processed trajectory number : ', batch+1, 'out of', self.dataloader.num_batches, 'trajectories in time', end - start) # write modified datapoints to txt files whole_dataset.append(dataset_instances) createDirectories(os.path.join(self.f_prefix, self.base_validation_path), self.dataloader.get_all_directory_namelist()) self.write_modified_datasets(whole_dataset) def handle_seq(self, x_seq, lookup_seq, PedsList_seq, angle): # add noise and rotate a trajectory vectorized_x_seq, first_values_dict = vectorizeSeq(x_seq, PedsList_seq, lookup_seq) modified_x_seq = vectorized_x_seq.clone() mean = torch.FloatTensor([self.noise_mean, self.noise_mean]) stddev =torch.FloatTensor([self.noise_std, self.noise_std]) origin = (0, 0) for ind, frame in enumerate(vectorized_x_seq): for ped in PedsList_seq[ind]: selected_point = frame[lookup_seq[ped], :] # rotate a frame point rotated_point = rotate(origin, selected_point, math.radians(angle)) noise = torch.normal(mean, stddev).clone() # add random noise modified_x_seq[ind, lookup_seq[ped], 0] = rotated_point[0] + noise[0] modified_x_seq[ind, lookup_seq[ped], 1] = rotated_point[1] + noise[1] #modified_x_seq[ind, lookup_seq[ped], :] = torch.cat(rotate(origin, first_values_dict[ped], math.radians(angle))) + modified_x_seq[ind, lookup_seq[ped], :] #roatate first frame value as well and add it back to get absoute coordinates modified_x_seq[ind, lookup_seq[ped], 0] = (rotate(origin, first_values_dict[ped], math.radians(angle)))[0] + modified_x_seq[ind, lookup_seq[ped], 0] modified_x_seq[ind, lookup_seq[ped], 1] = (rotate(origin, first_values_dict[ped], math.radians(angle)))[1] + modified_x_seq[ind, lookup_seq[ped], 1] return modified_x_seq def submision_seq_preprocess(self, x_seq, seq_lenght, lookup_seq): # create original txt structure for modified datapoints ret_x_seq_c = x_seq.data.numpy() ped_ids = self.dataloader.get_id_sequence(seq_lenght) positions_of_peds = [lookup_seq[ped] for ped in ped_ids] ret_x_seq_c = ret_x_seq_c[:, positions_of_peds, :] ret_x_seq_c_selected = ret_x_seq_c[:,0,:] ret_x_seq_c_selected[:,[0,1]] = ret_x_seq_c_selected[:,[1,0]] frame_numbers = self.dataloader.get_frame_sequence(seq_lenght) id_integrated_seq = np.append(np.array(ped_ids)[:,None], ret_x_seq_c_selected, axis=1) frame_integrated_seq = np.append(frame_numbers[:, None], id_integrated_seq, axis=1) return frame_integrated_seq def write_modified_datasets(self, dataset_instances_store): # write constructed txt structure to txt file self.dataloader.reset_batch_pointer() for dataset_index in range(self.dataloader.numDatasets): dataset_instances = dataset_instances_store[dataset_index] train_sub_instances = dict(random.sample(dataset_instances.items(), self.num_train_data)) validation_sub_instances = {k: v for k, v in dataset_instances.items() if k not in train_sub_instances} print("*********************************************************************************") print("Training datasets are writing for: ", self.dataloader.get_file_name(dataset_index)) self.write_dict(train_sub_instances, self.base_train_path) print("*********************************************************************************") print("Validation datasets are writing for: ", self.dataloader.get_file_name(dataset_index)) self.write_dict(validation_sub_instances, self.base_validation_path) def write_dict(self, dict, base_path): cleared_direcories = [] for key, value in dict.items(): path = os.path.join(self.f_prefix, base_path, key[0]) ext_removed_file_name = removeFileExtention(key[1]) file_name = ext_removed_file_name + "_" + str(key[2]) file_name = addFileExtention(file_name, 'txt') self.dataloader.write_dataset(value, file_name, path) def clear_directories(self, base_path, delete_all = False): # delete all files from a directory print("Clearing directories...") dir_names = self.dataloader.get_all_directory_namelist() base_path = os.path.join(self.f_prefix, base_path) for dir_ in dir_names: dir_path = os.path.join(base_path, dir_) file_names = getAllFileNames(dir_path) if delete_all: base_file_names = [] else: base_file_names = self.dataloader.get_base_file_name(dir_) [deleteFile(dir_path, [file_name]) for file_name in file_names if file_name not in base_file_names]
# %% Imports from utils import DataLoader from sklearn.ensemble import RandomForestClassifier from sklearn.metrics import f1_score, accuracy_score from interpret.blackbox import LimeTabular from interpret import show # %% Load and preprocess data data_loader = DataLoader() data_loader.load_dataset() data_loader.preprocess_data() # Split the data for evaluation X_train, X_test, y_train, y_test = data_loader.get_data_split() # Oversample the train data X_train, y_train = data_loader.oversample(X_train, y_train) print(X_train.shape) print(X_test.shape) # %% Fit blackbox model rf = RandomForestClassifier() rf.fit(X_train, y_train) y_pred = rf.predict(X_test) print(f"F1 Score {f1_score(y_test, y_pred, average='macro')}") print(f"Accuracy {accuracy_score(y_test, y_pred)}") # %% Apply lime # Initilize Lime for Tabular data lime = LimeTabular(predict_fn=rf.predict_proba, data=X_train, random_state=1) # Get local explanations lime_local = lime.explain_local(X_test[-20:], y_test[-20:], name='LIME')
save_every = args.save_every num_epochs = args.num_epochs workdir = args.workdir is_verbose = args.verbose use_pretrained_model = args.pretrained_embedding learning_rate = args.lr logpath = os.path.abspath(os.path.join(workdir, args.logfile)) log_reset(logpath) # logpath = os.path.abspath(os.path.join(workdir, "runs")) # with subprocess.Popen(['tensorboard', '--logdir', logpath]): """Loading datasets""" dataloader_train = DataLoader('dataset/sentences.train', vocab_size, max_size, workdir=workdir) dataloader_eval = DataLoader('dataset/sentences.eval', vocab_size, max_size, workdir=workdir) """Get the vocab and save it to vocab.dat. If method not called, the model tries to open vocab.dat Otherwise if no savefile is given, just loads the vocab directly. Uncomment to generate the vocab.dat file""" # dataloader_train.compute_vocab(savefile='vocab.dat') """Let's do some test on the dataloader...""" # Get the word to index correspondance for the embedding.
if __name__ == '__main__': from utils import loadDataLabel3, DataLoader a,b,c,d = loadDataLabel3('three_pt',0.7) print(d.shape) rnn = RNN("rnn_tanh",11,100,2) rnn.collect_params().initialize(mx.init.Xavier()) batch_size = 256 clipping_norm = 0.1 num_steps = 5 loss = mx.gluon.loss.L2Loss() trainer = mx.gluon.Trainer(rnn.collect_params(),'sgd',{ 'learning_rate':0.5, "wd":0.00 }) dataLoader = DataLoader(a,b) trl = [] tel = [] for epoch in range(500): total_L = 0.0 hidden = rnn.begin_state(func=mx.nd.zeros,batch_size = batch_size,ctx=mx.cpu()) for data,label in dataLoader.dataIter(batch_size): label = nd.array(label) # print("label shape" ,label.shape) #label = nd.ones(shape=(5,32)) * label #label = label.reshape((-1,)) dd = nd.array(data.reshape((batch_size,5,11)).swapaxes(0,1)) hidden = detach(hidden) with mx.autograd.record(): output, hidden = rnn(dd,hidden)
print('test prediction gt = {}'.format(test_gt)) # define testing generator function and # in testing, only generator networks, there is no discriminator networks and flownet. with tf.variable_scope('generator', reuse=None): print('testing = {}'.format(tf.get_variable_scope().name)) test_outputs = generator(test_inputs, layers=const.LAYERS, output_channel=3) test_psnr_error = psnr_error(gen_frames=test_outputs, gt_frames=test_gt) diff_mask_tensor = diff_mask(test_outputs, test_gt) config = tf.ConfigProto() config.gpu_options.allow_growth = True with tf.Session(config=config) as sess: # dataset data_loader = DataLoader(test_folder, height, width) # initialize weights sess.run(tf.global_variables_initializer()) print('Init global successfully!') # tf saver saver = tf.train.Saver(var_list=tf.global_variables(), max_to_keep=None) restore_var = [v for v in tf.global_variables()] loader = tf.train.Saver(var_list=restore_var) load(loader, sess, snapshot_dir) videos_info = data_loader.videos num_videos = len(videos_info.keys()) print('Num videos: ', num_videos) total = 0
def dataload(self, train_data, train_label, test_data, test_label): self.train_data = train_data self.train_label = train_label self.test_data = test_data self.test_label = test_label self.dataLoader = DataLoader(self.train_data, self.train_label)
class MLPTrainer(object): paramsFloder = './params/MLP' def __init__(self, labelName, loss, net='mlp', bn=False, dropout=None, all=False): self.all = all self.labelName = labelName self.labelIndex = OutputData.colName().index(labelName) self.bn = bn self.dropout = dropout if net == 'mlp': self.net = MLP(bn, dropout) elif net == 'smlp': self.net = SampleMLP(bn, dropout) #self.net = MLP(bn,dropout) self.loss = loss def initnet(self, lr, wd, opt='adam', init='x'): self.lr = lr self.wd = wd if init == 'x': self.net.collect_params().initialize(mx.init.Xavier(), force_reinit=True) else: self.net.initialize() self.trainer = gluon.Trainer(self.net.collect_params(), opt, { 'learning_rate': self.lr, 'wd': self.wd }) def dataload(self, train_data, train_label, test_data, test_label): self.train_data = train_data self.train_label = train_label self.test_data = test_data self.test_label = test_label self.dataLoader = DataLoader(self.train_data, self.train_label) def train(self, epochs, batch_size, con=False, ctx=None): self.train_loss = [] self.test_loss = [] if con: self.load(ctx=ctx) t1 = time.time() for epoch in range(epochs): train_loss = 0.0 if is_sigint_up: break for data, label in self.dataLoader.dataIter(batch_size): with autograd.record(): output = self.net(data) lossv = self.loss(output, label) lossv.backward() self.trainer.step(batch_size) train_loss += nd.sum(lossv).asscalar() ptrain_loss = self.eval(self.train_data, self.train_label) ptest_loss = self.eval(self.test_data, self.test_label) self.test_loss.append(ptest_loss / len(self.test_data)) self.train_loss.append(ptrain_loss / len(self.train_data)) #train_acc = self.accuracy(self.train_data,self.train_label) #test_acc = self.accuracy(self.test_data, self.test_label) # train_acc = 0.0 # test_acc = 0.0 #print('Epoch %d : Train loss -> %f Test loss -> %f Train acc -> %f Test acc -> %f' % (epoch, self.train_loss[-1], self.test_loss[-1], # train_acc, test_acc)) print("'Epoch %d : Train loss -> %f Test loss -> %f" % (epoch, self.train_loss[-1], self.test_loss[-1])) t2 = time.time() print("Time : ", (t2 - t1) / epochs) p = input('plot ? (y/n)') if p.lower() == 'y': self.plot() r = input('save params ? (y/n)') if r.lower() == 'y': self.save() def plot(self): plt.figure(figsize=(8, 6)) plt.plot(self.train_loss) plt.plot(self.test_loss) plt.legend(['train', 'test']) plt.xlabel('Epoch') plt.ylabel('Loss Value') plt.show() def eval(self, data, label): data = nd.array(data) label = nd.array(label) #print(output[:,0]-test_label) #print(data.shape,label.shape) output = self.net(data) return nd.sum(self.loss(output, label)).asscalar() #loss = nd.sqrt(2*nd.sum(nd.power(output.reshape(label.shape) - label,2))).asscalar() #return loss def predict(self, x): x = nd.array(x) l = self.net(x) #print('x : ',x.shape) return l.argmax(axis=1) def save(self): loss_folder = "./loss" if not os.path.exists(loss_folder): os.makedirs(loss_folder) with open(os.path.join(loss_folder, self.labelName + str(self.lr) + 'txt'), 'w', newline="") as f: writer = csv.writer(f) #print(self.train_loss) writer.writerows([self.train_loss, self.test_loss]) name = self.labelName + str(self.lr) + '.txt' paramsFile = os.path.join(self.paramsFloder, name) self.net.save_params(paramsFile) def load(self, ctx=None): name = self.labelName + str(self.lr) + '.txt' paramsFile = os.path.join(self.paramsFloder, name) self.net.load_params(paramsFile, ctx) def accuracy(self, data, label): output = self.net(data) #print(output.argmax(axis=1)) return nd.mean(output.argmax(axis=1) == label).asscalar()
from __future__ import division from __future__ import print_function from utils import DataLoader import numpy as np from sklearn import metrics import pandas import tensorflow as tf from tensorflow.contrib import learn ### Training data print("LOAD") loader = DataLoader('data/tweets', 5, 144) x, y = loader.get_xy() split = int(len(x) * 0.8) X_train, X_test = x[:split], x[split:] y_train, y_test = pandas.Series(y[:split]), pandas.Series(y[split:]) y_train = y_train.convert_objects(convert_numeric=True) y_test = y_test.convert_objects(convert_numeric=True) ### Process vocabulary MAX_DOCUMENT_LENGTH = 144 print("PREPROCESS") char_processor = learn.preprocessing.ByteProcessor(MAX_DOCUMENT_LENGTH) X_train = np.array(list(char_processor.fit_transform(X_train))) X_test = np.array(list(char_processor.transform(X_test)))
def train(args): data_loader = DataLoader(args.batch_size, args.seq_length, args.data_scale) if args.model_dir != '' and not os.path.exists(args.model_dir): os.makedirs(args.model_dir) with open(os.path.join(args.model_dir, 'config.pkl'), 'wb') as f: pickle.dump(args, f) model = Model(args) with tf.Session() as sess: summary_writer = tf.summary.FileWriter( os.path.join(args.model_dir, 'log'), sess.graph) tf.global_variables_initializer().run() saver = tf.train.Saver(tf.global_variables()) for e in range(args.num_epochs): sess.run(tf.assign(model.lr, args.learning_rate * (args.decay_rate ** e))) data_loader.reset_batch_pointer() v_x, v_y = data_loader.validation_data() valid_feed = { model.input_data: v_x, model.target_data: v_y, model.state_in: model.state_in.eval()} state = model.state_in.eval() for b in range(data_loader.num_batches): ith_train_step = e * data_loader.num_batches + b start = time.time() x, y = data_loader.next_batch() feed = { model.input_data: x, model.target_data: y, model.state_in: state} train_loss_summary, train_loss, state, _ = sess.run( [model.train_loss_summary, model.cost, model.state_out, model.train_op], feed) summary_writer.add_summary(train_loss_summary, ith_train_step) valid_loss_summary, valid_loss, = sess.run( [model.valid_loss_summary, model.cost], valid_feed) summary_writer.add_summary(valid_loss_summary, ith_train_step) end = time.time() print( "{}/{} (epoch {}), train_loss = {:.3f}, valid_loss = {:.3f}, time/batch = {:.3f}" .format( ith_train_step, args.num_epochs * data_loader.num_batches, e, train_loss, valid_loss, end - start)) if (ith_train_step % args.save_every == 0) and (ith_train_step > 0): checkpoint_path = os.path.join( args.model_dir, 'model.ckpt') saver.save( sess, checkpoint_path, global_step=ith_train_step) print("model saved to {}".format(checkpoint_path))
else: gpus = "" for i in range(len(args.gpus)): gpus = gpus + args.gpus[i] + "," os.environ["CUDA_VISIBLE_DEVICES"] = gpus[:-1] #os.environ['CUDA_VISIBLE_DEVICES'] = '0,1,2' torch.backends.cudnn.enabled = True # make sure to use cudnn for computational performance 确保使用cudnn train_folder = args.dataset_path + args.dataset_type + "/training/frames/" #train_folder = args.dataset_path + args.dataset_type + "/videos/training_frames/" test_folder = args.dataset_path + args.dataset_type + "/testing/frames/" train_dataset = DataLoader(train_folder, transforms.Compose([ transforms.ToTensor(), ]), resize_height=args.h, resize_width=args.w, time_step=args.t_length - 1) # transforms.ToTensor() 将numpy的ndarray或PIL.Image读的图片转换成形状为(C,H, W)的Tensor格式,且/255归一化到[0,1.0]之间 train_size = len(train_dataset) print(train_size) split = int(np.floor(args.vali * train_size)) # 在训练模型时使用到此函数,用来把训练数据分成多个小组,此函数每次抛出一组数据。直至把所有的数据都抛出。就是做一个数据的初始化 val_size = int(args.vali * len(train_dataset)) #验证用20%的训练集 train_size = int(len(train_dataset) - val_size) #训练集就是剩下80% train_dataset, valid_dataset = torch.utils.data.random_split( train_dataset, [train_size, val_size]) train_loader = data.DataLoader(train_dataset, batch_size=args.batch_size,
def train(args): origin = (0, 0) reference_point = (0, 1) validation_dataset_executed = False prefix = '' f_prefix = '.' if args.drive is True: prefix = 'drive/semester_project/social_lstm_final/' f_prefix = 'drive/semester_project/social_lstm_final' if not os.path.isdir("log/"): print("Directory creation script is running...") subprocess.call([f_prefix + '/make_directories.sh']) args.freq_validation = np.clip(args.freq_validation, 0, args.num_epochs) validation_epoch_list = list( range(args.freq_validation, args.num_epochs + 1, args.freq_validation)) validation_epoch_list[-1] -= 1 # Create the data loader object. This object would preprocess the data in terms of # batches each of size args.batch_size, of length args.seq_length dataloader = DataLoader(f_prefix, args.batch_size, args.seq_length, args.num_validation, forcePreProcess=True) method_name = "OBSTACLELSTM" model_name = "LSTM" save_tar_name = method_name + "_lstm_model_" if args.gru: model_name = "GRU" save_tar_name = method_name + "_gru_model_" # Log directory log_directory = os.path.join(prefix, 'log/') plot_directory = os.path.join(prefix, 'plot/', method_name, model_name) plot_train_file_directory = 'validation' # Logging files log_file_curve = open( os.path.join(log_directory, method_name, model_name, 'log_curve.txt'), 'w+') log_file = open( os.path.join(log_directory, method_name, model_name, 'val.txt'), 'w+') # model directory save_directory = os.path.join(prefix, 'model/') # save_directory += str(args.leaveDataset) + '/' # Save the arguments in the config file with open( os.path.join(save_directory, method_name, model_name, 'config.pkl'), 'wb') as f: pickle.dump(args, f) # Path to store the checkpoint file def checkpoint_path(x): return os.path.join(save_directory, method_name, model_name, save_tar_name + str(x) + '.tar') # model creation net = OLSTMModel(args) if args.use_cuda: net = net.cuda() # optimizer = torch.optim.RMSprop(net.parameters(), lr=args.learning_rate) optimizer = torch.optim.Adagrad(net.parameters(), weight_decay=args.lambda_param) # optimizer = torch.optim.Adam(net.parameters(), weight_decay=args.lambda_param) learning_rate = args.learning_rate best_val_loss = 100 best_val_data_loss = 100 smallest_err_val = 100000 smallest_err_val_data = 100000 best_epoch_val = 0 best_epoch_val_data = 0 best_err_epoch_val = 0 best_err_epoch_val_data = 0 all_epoch_results = [] grids = [] num_batch = 0 dataset_pointer_ins_grid = -1 [grids.append([]) for dataset in range(dataloader.get_len_of_dataset())] # Training for epoch in range(args.num_epochs): print('****************Training epoch beginning******************') if dataloader.additional_validation and (epoch - 1) in validation_epoch_list: dataloader.switch_to_dataset_type(True) dataloader.reset_batch_pointer(valid=False) loss_epoch = 0 # For each batch for batch in range(dataloader.num_batches): start = time.time() # Get batch data x, y, d, numPedsList, PedsList, target_ids = dataloader.next_batch( ) loss_batch = 0 # if we are in a new dataset, zero the counter of batch if dataset_pointer_ins_grid is not dataloader.dataset_pointer and epoch is not 0: num_batch = 0 dataset_pointer_ins_grid = dataloader.dataset_pointer # For each sequence for sequence in range(dataloader.batch_size): # Get the data corresponding to the current sequence x_seq, _, d_seq, numPedsList_seq, PedsList_seq = x[ sequence], y[sequence], d[sequence], numPedsList[ sequence], PedsList[sequence] target_id = target_ids[sequence] # get processing file name and then get dimensions of file folder_name = dataloader.get_directory_name_with_pointer(d_seq) dataset_data = dataloader.get_dataset_dimension( folder_name ) #dataloader.get_dataset_dimension(folder_name) # dense vector creation x_seq, lookup_seq = dataloader.convert_proper_array( x_seq, numPedsList_seq, PedsList_seq) # print("LOOKUP SEQ = " ,lookup_seq) #print("TARGET_ID = " , target_id) target_id_values = x_seq[0][lookup_seq[target_id], 0:2] #:2 # Compute grid masks if (args.grid): if (epoch is 0): grid_seq = getSequenceGridMask(x_seq, dataset_data, PedsList_seq, args.neighborhood_size, args.grid_size, args.use_cuda, True) grids[dataloader.dataset_pointer].append(grid_seq) else: grid_seq = grids[dataloader.dataset_pointer][ (num_batch * dataloader.batch_size) + sequence] else: grid_seq = getSequenceGridMask(x_seq, dataset_data, PedsList_seq, args.neighborhood_size, args.grid_size, args.use_cuda, True) # vectorize trajectories in sequence x_seq, _ = vectorize_seq(x_seq, PedsList_seq, lookup_seq) if args.use_cuda: x_seq = x_seq.cuda() # number of peds in this sequence per frame numNodes = len(lookup_seq) hidden_states = Variable(torch.zeros(numNodes, args.rnn_size)) if args.use_cuda: hidden_states = hidden_states.cuda() cell_states = Variable(torch.zeros(numNodes, args.rnn_size)) if args.use_cuda: cell_states = cell_states.cuda() # Zero out gradients net.zero_grad() optimizer.zero_grad() # Forward prop outputs, _, _ = net(x_seq, grid_seq, hidden_states, cell_states, PedsList_seq, numPedsList_seq, dataloader, lookup_seq) # Compute loss loss = Gaussian2DLikelihood(outputs, x_seq, PedsList_seq, lookup_seq) loss_batch += loss.item() # Compute gradients loss.backward() # Clip gradients torch.nn.utils.clip_grad_norm_(net.parameters(), args.grad_clip) # Update parameters optimizer.step() end = time.time() loss_batch = loss_batch / dataloader.batch_size loss_epoch += loss_batch num_batch += 1 print('{}/{} (epoch {}), train_loss = {:.3f}, time/batch = {:.3f}'. format(epoch * dataloader.num_batches + batch, args.num_epochs * dataloader.num_batches, epoch, loss_batch, end - start)) loss_epoch /= dataloader.num_batches # Log loss values log_file_curve.write("Training epoch: " + str(epoch) + " loss: " + str(loss_epoch) + '\n') if dataloader.valid_num_batches > 0: print( '****************Validation epoch beginning******************') # Validation dataloader.reset_batch_pointer(valid=True) loss_epoch = 0 err_epoch = 0 # For each batch for batch in range(dataloader.valid_num_batches): # Get batch data x, y, d, numPedsList, PedsList, target_ids = dataloader.next_valid_batch( ) # Loss for this batch loss_batch = 0 err_batch = 0 # For each sequence for sequence in range(dataloader.batch_size): # Get data corresponding to the current sequence x_seq, _, d_seq, numPedsList_seq, PedsList_seq = x[ sequence], y[sequence], d[sequence], numPedsList[ sequence], PedsList[sequence] target_id = target_ids[sequence] # get processing file name and then get dimensions of file folder_name = dataloader.get_directory_name_with_pointer( d_seq) dataset_data = dataloader.get_dataset_dimension( folder_name) # dense vector creation x_seq, lookup_seq = dataloader.convert_proper_array( x_seq, numPedsList_seq, PedsList_seq) target_id_values = x_seq[0][lookup_seq[target_id], 0:2] # Compute grid masks if (args.grid): if (epoch is 0): grid_seq = getSequenceGridMask( x_seq, dataset_data, PedsList_seq, args.neighborhood_size, args.grid_size, args.use_cuda, True) grids[dataloader.dataset_pointer].append(grid_seq) else: grid_seq = grids[dataloader.dataset_pointer][ (num_batch * dataloader.batch_size) + sequence] else: grid_seq = getSequenceGridMask(x_seq, dataset_data, PedsList_seq, args.neighborhood_size, args.grid_size, args.use_cuda, True) # vectorize trajectories in sequence x_seq, _ = vectorize_seq(x_seq, PedsList_seq, lookup_seq) if args.use_cuda: x_seq = x_seq.cuda() # number of peds in this sequence per frame numNodes = len(lookup_seq) hidden_states = Variable( torch.zeros(numNodes, args.rnn_size)) if args.use_cuda: hidden_states = hidden_states.cuda() cell_states = Variable(torch.zeros(numNodes, args.rnn_size)) if args.use_cuda: cell_states = cell_states.cuda() # Forward prop outputs, _, _ = net(x_seq[:-1], grid_seq[:-1], hidden_states, cell_states, PedsList_seq[:-1], numPedsList_seq, dataloader, lookup_seq) # Compute loss loss = Gaussian2DLikelihood(outputs, x_seq[1:], PedsList_seq[1:], lookup_seq) # Extract the mean, std and corr of the bivariate Gaussian mux, muy, sx, sy, corr = getCoef(outputs) # Sample from the bivariate Gaussian next_x, next_y = sample_gaussian_2d( mux.data, muy.data, sx.data, sy.data, corr.data, PedsList_seq[-1], lookup_seq) next_vals = torch.FloatTensor(1, numNodes, 2) next_vals[:, :, 0] = next_x next_vals[:, :, 1] = next_y err = get_mean_error(next_vals, x_seq[-1].data[None, :, :], [PedsList_seq[-1]], [PedsList_seq[-1]], args.use_cuda, lookup_seq) loss_batch += loss.item() err_batch += err loss_batch = loss_batch / dataloader.batch_size err_batch = err_batch / dataloader.batch_size loss_epoch += loss_batch err_epoch += err_batch if dataloader.valid_num_batches != 0: loss_epoch = loss_epoch / dataloader.valid_num_batches err_epoch = err_epoch / dataloader.num_batches # Update best validation loss until now if loss_epoch < best_val_loss: best_val_loss = loss_epoch best_epoch_val = epoch if err_epoch < smallest_err_val: smallest_err_val = err_epoch best_err_epoch_val = epoch print('(epoch {}), valid_loss = {:.3f}, valid_err = {:.3f}'. format(epoch, loss_epoch, err_epoch)) print('Best epoch', best_epoch_val, 'Best validation loss', best_val_loss, 'Best error epoch', best_err_epoch_val, 'Best error', smallest_err_val) log_file_curve.write("Validation epoch: " + str(epoch) + " loss: " + str(loss_epoch) + " err: " + str(err_epoch) + '\n') # Validation dataset if dataloader.additional_validation and ( epoch) in validation_epoch_list: dataloader.switch_to_dataset_type() print( '****************Validation with dataset epoch beginning******************' ) dataloader.reset_batch_pointer(valid=False) dataset_pointer_ins = dataloader.dataset_pointer validation_dataset_executed = True loss_epoch = 0 err_epoch = 0 f_err_epoch = 0 num_of_batch = 0 smallest_err = 100000 # results of one epoch for all validation datasets epoch_result = [] # results of one validation dataset results = [] # For each batch for batch in range(dataloader.num_batches): # Get batch data x, y, d, numPedsList, PedsList, target_ids = dataloader.next_batch( ) if dataset_pointer_ins is not dataloader.dataset_pointer: if dataloader.dataset_pointer is not 0: print('Finished prosessed file : ', dataloader.get_file_name(-1), ' Avarage error : ', err_epoch / num_of_batch) num_of_batch = 0 epoch_result.append(results) dataset_pointer_ins = dataloader.dataset_pointer results = [] # Loss for this batch loss_batch = 0 err_batch = 0 f_err_batch = 0 # For each sequence for sequence in range(dataloader.batch_size): # Get data corresponding to the current sequence x_seq, _, d_seq, numPedsList_seq, PedsList_seq = x[ sequence], y[sequence], d[sequence], numPedsList[ sequence], PedsList[sequence] target_id = target_ids[sequence] # get processing file name and then get dimensions of file folder_name = dataloader.get_directory_name_with_pointer( d_seq) dataset_data = dataloader.get_dataset_dimension( folder_name) # dense vector creation x_seq, lookup_seq = dataloader.convert_proper_array( x_seq, numPedsList_seq, PedsList_seq) # will be used for error calculation orig_x_seq = x_seq.clone() target_id_values = orig_x_seq[0][lookup_seq[target_id], 0:2] # grid mask calculation grid_seq = getSequenceGridMask(x_seq, dataset_data, PedsList_seq, args.neighborhood_size, args.grid_size, args.use_cuda, True) # vectorize datapoints x_seq, first_values_dict = vectorize_seq( x_seq, PedsList_seq, lookup_seq) if args.use_cuda: x_seq = x_seq.cuda() # sample predicted points from model ret_x_seq, loss = sample_validation_data( x_seq, PedsList_seq, grid_seq, args, net, lookup_seq, numPedsList_seq, dataloader) # revert the points back to original space ret_x_seq = revert_seq(ret_x_seq, PedsList_seq, lookup_seq, first_values_dict) # get mean and final error err = get_mean_error(ret_x_seq.data, orig_x_seq.data, PedsList_seq, PedsList_seq, args.use_cuda, lookup_seq) f_err = get_final_error(ret_x_seq.data, orig_x_seq.data, PedsList_seq, PedsList_seq, lookup_seq) loss_batch += loss.item() err_batch += err f_err_batch += f_err print('Current file : ', dataloader.get_file_name(0), ' Batch : ', batch + 1, ' Sequence: ', sequence + 1, ' Sequence mean error: ', err, ' Sequence final error: ', f_err, ' time: ', end - start) results.append( (orig_x_seq.data.cpu().numpy(), ret_x_seq.data.cpu().numpy(), PedsList_seq, lookup_seq, dataloader.get_frame_sequence(args.seq_length), target_id)) loss_batch = loss_batch / dataloader.batch_size err_batch = err_batch / dataloader.batch_size f_err_batch = f_err_batch / dataloader.batch_size num_of_batch += 1 loss_epoch += loss_batch err_epoch += err_batch f_err_epoch += f_err_batch epoch_result.append(results) all_epoch_results.append(epoch_result) if dataloader.num_batches != 0: loss_epoch = loss_epoch / dataloader.num_batches err_epoch = err_epoch / dataloader.num_batches f_err_epoch = f_err_epoch / dataloader.num_batches # Update best validation loss until now if loss_epoch < best_val_data_loss: best_val_data_loss = loss_epoch best_epoch_val_data = epoch if err_epoch < smallest_err_val_data: smallest_err_val_data = err_epoch best_err_epoch_val_data = epoch print( '(epoch {}), valid_loss = {:.3f}, valid_mean_err = {:.3f}, valid_final_err = {:.3f}' .format(epoch, loss_epoch, err_epoch, f_err_epoch)) print('Best epoch', best_epoch_val_data, 'Best validation loss', best_val_data_loss, 'Best error epoch', best_err_epoch_val_data, 'Best error', smallest_err_val_data) log_file_curve.write("Validation dataset epoch: " + str(epoch) + " loss: " + str(loss_epoch) + " mean_err: " + str(err_epoch) + 'final_err: ' + str(f_err_epoch) + '\n') optimizer = time_lr_scheduler(optimizer, epoch, lr_decay_epoch=args.freq_optimizer) # Save the model after each epoch print('Saving model') torch.save( { 'epoch': epoch, 'state_dict': net.state_dict(), 'optimizer_state_dict': optimizer.state_dict() }, checkpoint_path(epoch)) if dataloader.valid_num_batches != 0: print('Best epoch', best_epoch_val, 'Best validation Loss', best_val_loss, 'Best error epoch', best_err_epoch_val, 'Best error', smallest_err_val) # Log the best epoch and best validation loss log_file.write('Validation Best epoch:' + str(best_epoch_val) + ',' + ' Best validation Loss: ' + str(best_val_loss)) if dataloader.additional_validation: print('Best epoch acording to validation dataset', best_epoch_val_data, 'Best validation Loss', best_val_data_loss, 'Best error epoch', best_err_epoch_val_data, 'Best error', smallest_err_val_data) log_file.write("Validation dataset Best epoch: " + str(best_epoch_val_data) + ',' + ' Best validation Loss: ' + str(best_val_data_loss) + '\n') # dataloader.write_to_plot_file(all_epoch_results[best_epoch_val_data], plot_directory) # elif dataloader.valid_num_batches != 0: # dataloader.write_to_plot_file(all_epoch_results[best_epoch_val], plot_directory) # else: if validation_dataset_executed: # if we executed a validation epoch, write last epoch file dataloader.switch_to_dataset_type(load_data=False) create_directories(plot_directory, [plot_train_file_directory]) dataloader.write_to_plot_file( all_epoch_results[len(all_epoch_results) - 1], os.path.join(plot_directory, plot_train_file_directory)) # Close logging files log_file.close() log_file_curve.close()
def main(unused_argv): # Initialise learning rate eff_rate = FLAGS.learning_rate # extract the vocabulary from training sentendes with open("vocabulary.pickle", "rb") as f: vocabulary = pickle.load(f) # load training data if FLAGS.do_train: train_loader = DataLoader(FLAGS.train_file_path, vocabulary, do_shuffle=True) batches_train = train_loader.batch_iterator(FLAGS.num_epochs, FLAGS.batch_size) # load validation data if FLAGS.do_eval: eval_loader = DataLoader(FLAGS.eval_file_path, vocabulary, do_shuffle=False) batches_eval = eval_loader.batch_iterator(num_epochs=1, batch_size=1000) # Load continuation data if FLAGS.do_generate: gen_loader = DataLoader(FLAGS.generate_file_path, vocabulary, do_shuffle=False, is_partial=True) batches_gen = gen_loader.batch_iterator(num_epochs=1, batch_size=1000) # Create the graph global_counter = tf.Variable(0, trainable=False) if FLAGS.task == "A": # For a) the embedding is a matrix to be learned from scratch embedding_matrix = tf.get_variable( name="embedding_matrix", shape=[FLAGS.vocab_size, FLAGS.embedding_size], dtype=tf.float32, initializer=tf.contrib.layers.xavier_initializer()) else: # For simplicity we do not use the code given but use the given # embeddings directly. # TODO confirm that this is okay for the tutors. keys = [] emb = [] ext_emb = csv.reader(open(FLAGS.embedding), delimiter=' ') for line in ext_emb: keys += [line[0]] emb += [list(map(float, line[1:]))] given_emb = dict(zip(keys, emb)) external_embedding = np.zeros(shape=(FLAGS.vocab_size, FLAGS.embedding_size)) for k, v in vocabulary.get_vocabulary_as_dict().items(): try: external_embedding[v, :] = given_emb[k] except KeyError: print("Unmatched: %s" % k) external_embedding[v, :] = \ np.random.uniform(low=-0.25, high=0.25, size=FLAGS.embedding_size) embedding_matrix = tf.Variable(external_embedding, dtype=tf.float32) input_words = tf.placeholder(tf.int32, [None, FLAGS.sentence_length]) # add to collection for usage from restored model tf.add_to_collection("input_words", input_words) embedded_words = tf.nn.embedding_lookup(embedding_matrix, input_words) # RNN graph lstm = tf.contrib.rnn.BasicLSTMCell(FLAGS.lstm_size) # Somehow lstm has a touple of states instead of just one. # We learn sensible initial states as well. # The states seem to correspond to the LSTM cell state and the hidden layer output # see http://stackoverflow.com/questions/41789133/c-state-and-m-state-in-tensorflow-lstm # https://www.tensorflow.org/api_docs/python/tf/contrib/rnn/LSTMStateTuple lstm_zero_c = \ tf.get_variable("zero_state_c", shape=[1, FLAGS.lstm_size], dtype=tf.float32, initializer=tf.contrib.layers.xavier_initializer()) lstm_zero_h = \ tf.get_variable("zero_state_h", shape=[1, FLAGS.lstm_size], dtype=tf.float32, initializer=tf.contrib.layers.xavier_initializer()) lstm_state = (tf.tile(lstm_zero_c, [tf.shape(input_words)[0], 1]), tf.tile(lstm_zero_h, [tf.shape(input_words)[0], 1])) lstm_state_g = (tf.tile(lstm_zero_c, [tf.shape(input_words)[0], 1]), tf.tile(lstm_zero_h, [tf.shape(input_words)[0], 1])) if not FLAGS.task == "C": out_to_logit_w = tf.get_variable( name="output_weights", shape=[FLAGS.lstm_size, FLAGS.vocab_size], dtype=tf.float32, initializer=tf.contrib.layers.xavier_initializer()) out_to_logit_b = tf.get_variable("output_bias", shape=[FLAGS.vocab_size]) else: inter_w = tf.get_variable("intermediate_weights", shape=[FLAGS.lstm_size, FLAGS.intermediate_size], dtype=tf.float32, initializer=tf.contrib.layers.xavier_initializer()) inter_b = tf.get_variable("intermediate_biases", shape=[FLAGS.intermediate_size]) out_to_logit_w = tf.get_variable( name="output_weights", shape=[FLAGS.intermediate_size, FLAGS.vocab_size], dtype=tf.float32, initializer=tf.contrib.layers.xavier_initializer()) out_to_logit_b = tf.get_variable("output_bias", shape=[FLAGS.vocab_size]) # initialize lstm_outputs = [] # add summaries for tensorboard with tf.variable_scope("RNN"): for time_step in range(FLAGS.sentence_length): if time_step > 0: tf.get_variable_scope().reuse_variables() lstm_out, lstm_state = lstm(embedded_words[:, time_step, :], lstm_state) lstm_outputs.append(lstm_out) output = tf.concat(axis=0, values=lstm_outputs) if not FLAGS.task == "C": logits = tf.matmul(output, out_to_logit_w) + out_to_logit_b l2_loss = tf.nn.l2_loss(out_to_logit_w) * FLAGS.lambda_l2 else: logits = tf.matmul(tf.matmul(output, inter_w) + inter_b, out_to_logit_w) + out_to_logit_b l2_loss = (tf.nn.l2_loss(out_to_logit_w) + tf.nn.l2_loss( inter_w)) * FLAGS.lambda_l2 logits_reshaped = tf.transpose(tf.reshape(logits, [FLAGS.sentence_length, -1, FLAGS.vocab_size]), [1, 0, 2]) best_pred = tf.arg_max(logits_reshaped, 2) loss = tf.nn.sparse_softmax_cross_entropy_with_logits( labels=input_words[:, 1:], logits=logits_reshaped[:, :-1, :]) / np.log(2) * \ tf.to_float(tf.not_equal(input_words[:, 1:], vocabulary.dict[vocabulary.PADDING])) # Sanity check any_word = input_words[10, 5] any_word_pre = input_words[10, 4] any_word_probs = tf.nn.softmax(logits_reshaped[10, 5, :]) any_word_max_prob = tf.reduce_max(any_word_probs) any_word_prediction = tf.argmax(any_word_probs, dimension=0) any_word_real_perp = 1 / any_word_probs[any_word] any_word_probs2 = tf.nn.softmax(logits_reshaped[11, 6, :]) any_word_prediction2 = tf.argmax(any_word_probs2, dimension=0) # output of the last layer in the unrolled LSTM last_output = lstm_outputs[-1] last_prob = tf.nn.softmax(logits_reshaped[:, -1, :]) # add to collection for re-use in task 1.2 tf.add_to_collection("last_output", last_output) tf.add_to_collection("last_prob", last_prob) tf.summary.histogram("last_prob", last_prob) # define perplexity and add to collection to provide access when reloading the model elsewhere # add a summary scalar for tensorboard # TODO Confirm that the elementwise crossentropy is -p(w_t|w_1,...,w_{t-1}) mean_loss = tf.reduce_sum(loss) / tf.reduce_sum( tf.to_float(tf.not_equal(input_words[:, 1:], vocabulary.dict[vocabulary.PADDING]))) + l2_loss tf.summary.scalar('loss', mean_loss) perplexity = tf.pow(2.0, mean_loss) tf.add_to_collection("perplexity", perplexity) tf.summary.scalar('perplexity', perplexity) nominator = tf.reduce_sum(loss, axis=1) denominator = tf.reduce_sum(tf.to_float( tf.not_equal(input_words[:, 1:], vocabulary.dict[vocabulary.PADDING])), axis=1) sentence_perplexity = \ tf.pow(2.0, tf.reduce_sum(loss, axis=1) / tf.reduce_sum(tf.to_float( tf.not_equal(input_words[:, 1:], vocabulary.dict[vocabulary.PADDING])), axis=1)) tf.summary.histogram('sPerplexity', sentence_perplexity) # TODO add learning_rate to summaries optimiser = tf.train.AdamOptimizer(FLAGS.learning_rate) gradients, v = zip(*optimiser.compute_gradients(mean_loss)) # TODO from the doc it looks as if we actually wanted to set use_norm to 10 instead. Confirm! # https://www.tensorflow.org/api_docs/python/tf/clip_by_global_norm clipped_gradients, _ = tf.clip_by_global_norm(gradients, 10) train_op = optimiser.apply_gradients(zip(clipped_gradients, v), global_step=global_counter) # initialize the Variables init_op = tf.global_variables_initializer() # Saver to save model checkpoints saver = tf.train.Saver(max_to_keep=FLAGS.num_checkpoints, keep_checkpoint_every_n_hours=2) # Get an idea of the overall size of the model total_parameters = 0 for variable in tf.trainable_variables(): shape = variable.get_shape() print(shape) variable_parametes = 1 for dim in shape: variable_parametes *= dim.value total_parameters += variable_parametes print("Built a graph with a total of %d trainable parameters" % ( total_parameters)) merged_summaries = tf.summary.merge_all() # loop over training batches if FLAGS.do_train: with tf.Session() as sess: # Summary Filewriter train_summary_dir = os.path.join(FLAGS.model_dir, "summary", "train") train_summary_writer = tf.summary.FileWriter(train_summary_dir, sess.graph) # Restoring or initialising session if not FLAGS.force_init: try: saver.restore(sess, tf.train.latest_checkpoint(FLAGS.model_dir)) print("Recovered Session") except: # TODO find name for this exception (it does not accept the NotFoundError displayed if it does not find the save) sess.run(init_op) print("Unexpectedly initialised session") else: sess.run(init_op) print("Initialised session") print("Start training") for data_train in batches_train: gc_ = 0 if (gc_ % FLAGS.evaluate_every) == 0 or gc_ == 1: ms_, gc_, pp_, last_out_, last_prob_, _, \ word, max_p, pred, perp_of_true, word2, word_pre = \ sess.run([merged_summaries, global_counter, perplexity, last_output, last_prob, train_op, any_word, any_word_max_prob, any_word_prediction, any_word_real_perp, any_word_prediction2, any_word_pre], feed_dict={input_words: data_train}) else: _, gc_ = sess.run([train_op, global_counter], feed_dict={input_words: data_train}) if gc_ > FLAGS.no_output_before_n: train_summary_writer.add_summary(ms_, gc_) if (gc_ % FLAGS.evaluate_every) == 0 or gc_ == 1: print("Iteration %s: Perplexity is %s" % (gc_, pp_)) if (gc_ % FLAGS.checkpoint_every == 0) and gc_ > 0: ckpt_path = saver.save(sess, os.path.join(FLAGS.model_dir, 'model'), gc_) print("Model saved in file: %s" % ckpt_path) if gc_ % FLAGS.hlave_lr_every == 0 & gc_ > 0: eff_rate /= 2 print("Adjusted learning rate") print(eff_rate) if gc_ % 250 == 0: print( "Target: %s, Perplexity of target: %s, " "max prob: %s, predicted: %s, second_word: %s," "Previous word: %s" % (word, perp_of_true, max_p, pred, word2, word_pre)) if FLAGS.do_eval: with tf.Session() as sess: # Restoring or initialising session saver.restore(sess, tf.train.latest_checkpoint(FLAGS.model_dir)) print("Recovered Session") out_pp = np.empty(0) for data_eval in batches_eval: out_pp = np.concatenate((out_pp, sess.run(sentence_perplexity, feed_dict={ input_words: data_eval}))) np.savetxt( FLAGS.output_dir + "/group25.perplexity" + FLAGS.task, np.array(out_pp), fmt="%4.8f", delimiter=',') if FLAGS.do_generate: with tf.Session() as sess: # Restoring or initialising session saver.restore(sess, tf.train.latest_checkpoint(FLAGS.model_dir)) print("Recovered Session") sentences = [] for data_gen in batches_gen: input = data_gen for t in range(FLAGS.sentence_length - 1): best = sess.run(best_pred, feed_dict={input_words: input}) if t < (FLAGS.sentence_length - 1): next_available = data_gen[:, t+1] != vocabulary.dict[vocabulary.PADDING] input[:, t + 1] = next_available * data_gen[:, t+1] + \ (1-next_available) * best[:, t] sentences.append(input) translator = vocabulary.get_inverse_voc_dict() sentence_together = np.vstack(sentences) out_sentences = np.array([translator[x] for x in sentence_together.reshape([-1])]).reshape([-1, FLAGS.sentence_length]) tt = clean_and_cut_sentences(out_sentences) np.savetxt( FLAGS.output_dir + "/group25.continuation" + FLAGS.task, np.array(tt), fmt="%s", delimiter='\n')
def main(): parser = argparse.ArgumentParser() # Model to be loaded parser.add_argument('--epoch', type=int, default=15, help='Epoch of model to be loaded') parser.add_argument('--seq_length', type=int, default=20, help='RNN sequence length') parser.add_argument('--use_cuda', action="store_true", default=False, help='Use GPU or not') parser.add_argument('--drive', action="store_true", default=False, help='Use Google drive or not') # Size of neighborhood to be considered parameter parser.add_argument( '--neighborhood_size', type=int, default=32, help='Neighborhood size to be considered for social grid') # Size of the social grid parameter parser.add_argument('--grid_size', type=int, default=4, help='Grid size of the social grid') # number of validation will be used parser.add_argument( '--num_validation', type=int, default=5, help='Total number of validation dataset will be visualized') # gru support parser.add_argument('--gru', action="store_true", default=False, help='True : GRU cell, False: LSTM cell') # method selection parser.add_argument( '--method', type=int, default=1, help= 'Method of lstm will be used (1 = social lstm, 2 = obstacle lstm, 3 = vanilla lstm)' ) # Parse the parameters sample_args = parser.parse_args() # for drive run prefix = '' f_prefix = '.' if sample_args.drive is True: prefix = 'drive/semester_project/social_lstm_final/' f_prefix = 'drive/semester_project/social_lstm_final' method_name = get_method_name(sample_args.method) model_name = "LSTM" save_tar_name = method_name + "_lstm_model_" if sample_args.gru: model_name = "GRU" save_tar_name = method_name + "_gru_model_" # Save directory save_directory = os.path.join(f_prefix, 'model/', method_name, model_name) # plot directory for plotting in the future plot_directory = os.path.join(f_prefix, 'plot/', method_name, model_name) plot_validation_file_directory = 'validation' # Define the path for the config file for saved args with open(os.path.join(save_directory, 'config.pkl'), 'rb') as f: saved_args = pickle.load(f) origin = (0, 0) reference_point = (0, 1) net = get_model(sample_args.method, saved_args, True) if sample_args.use_cuda: net = net.cuda() # Get the checkpoint path checkpoint_path = os.path.join( save_directory, save_tar_name + str(sample_args.epoch) + '.tar') if os.path.isfile(checkpoint_path): print('Loading checkpoint') checkpoint = torch.load(checkpoint_path) model_epoch = checkpoint['epoch'] net.load_state_dict(checkpoint['state_dict']) print('Loaded checkpoint at epoch', model_epoch) # Create the DataLoader object dataloader = DataLoader(f_prefix, 1, sample_args.seq_length, num_of_validation=sample_args.num_validation, forcePreProcess=True, infer=True) create_directories(plot_directory, [plot_validation_file_directory]) dataloader.reset_batch_pointer() print( '****************Validation dataset batch processing******************' ) dataloader.reset_batch_pointer(valid=False) dataset_pointer_ins = dataloader.dataset_pointer loss_epoch = 0 err_epoch = 0 f_err_epoch = 0 num_of_batch = 0 smallest_err = 100000 # results of one epoch for all validation datasets epoch_result = [] # results of one validation dataset results = [] # For each batch for batch in range(dataloader.num_batches): start = time.time() # Get batch data x, y, d, numPedsList, PedsList, target_ids = dataloader.next_batch() if dataset_pointer_ins is not dataloader.dataset_pointer: if dataloader.dataset_pointer is not 0: print('Finished prosessed file : ', dataloader.get_file_name(-1), ' Avarage error : ', err_epoch / num_of_batch) num_of_batch = 0 epoch_result.append(results) dataset_pointer_ins = dataloader.dataset_pointer results = [] # Loss for this batch loss_batch = 0 err_batch = 0 f_err_batch = 0 # For each sequence for sequence in range(dataloader.batch_size): # Get data corresponding to the current sequence x_seq, _, d_seq, numPedsList_seq, PedsList_seq = x[sequence], y[ sequence], d[sequence], numPedsList[sequence], PedsList[ sequence] target_id = target_ids[sequence] folder_name = dataloader.get_directory_name_with_pointer(d_seq) dataset_data = dataloader.get_dataset_dimension(folder_name) # dense vector creation x_seq, lookup_seq = dataloader.convert_proper_array( x_seq, numPedsList_seq, PedsList_seq) # will be used for error calculation orig_x_seq = x_seq.clone() target_id_values = x_seq[0][lookup_seq[target_id], 0:2] # grid mask calculation if sample_args.method == 2: # obstacle lstm grid_seq = getSequenceGridMask(x_seq, dataset_data, PedsList_seq, saved_args.neighborhood_size, saved_args.grid_size, saved_args.use_cuda, True) elif sample_args.method == 1: # social lstm grid_seq = getSequenceGridMask(x_seq, dataset_data, PedsList_seq, saved_args.neighborhood_size, saved_args.grid_size, saved_args.use_cuda) # vectorize datapoints x_seq, first_values_dict = vectorize_seq(x_seq, PedsList_seq, lookup_seq) # <---------------- Experimental block (may need update in methods)-----------------------> # x_seq = translate(x_seq, PedsList_seq, lookup_seq ,target_id_values) # angle = angle_between(reference_point, (x_seq[1][lookup_seq[target_id], 0].data.numpy(), x_seq[1][lookup_seq[target_id], 1].data.numpy())) # x_seq = rotate_traj_with_target_ped(x_seq, angle, PedsList_seq, lookup_seq) # # Compute grid masks # grid_seq = getSequenceGridMask(x_seq, dataset_data, PedsList_seq, sample_args.neighborhood_size, sample_args.grid_size, sample_args.use_cuda) # x_seq, first_values_dict = vectorize_seq(x_seq, PedsList_seq, lookup_seq) if sample_args.use_cuda: x_seq = x_seq.cuda() if sample_args.method == 3: # vanilla lstm ret_x_seq, loss = sample_validation_data_vanilla( x_seq, PedsList_seq, sample_args, net, lookup_seq, numPedsList_seq, dataloader) else: ret_x_seq, loss = sample_validation_data( x_seq, PedsList_seq, grid_seq, sample_args, net, lookup_seq, numPedsList_seq, dataloader) # <---------------------Experimental inverse block --------------> # ret_x_seq = revert_seq(ret_x_seq, PedsList_seq, lookup_seq, target_id_values, first_values_dict) # ret_x_seq = rotate_traj_with_target_ped(ret_x_seq, -angle, PedsList_seq, lookup_seq) # ret_x_seq = translate(ret_x_seq, PedsList_seq, lookup_seq ,-target_id_values) # revert the points back to original space ret_x_seq = revert_seq(ret_x_seq, PedsList_seq, lookup_seq, first_values_dict) err = get_mean_error(ret_x_seq.data, orig_x_seq.data, PedsList_seq, PedsList_seq, sample_args.use_cuda, lookup_seq) f_err = get_final_error(ret_x_seq.data, orig_x_seq.data, PedsList_seq, PedsList_seq, lookup_seq) loss_batch += loss err_batch += err f_err_batch += f_err results.append( (orig_x_seq.data.cpu().numpy(), ret_x_seq.data.cpu().numpy(), PedsList_seq, lookup_seq, dataloader.get_frame_sequence(sample_args.seq_length), target_id)) end = time.time() print('Current file : ', dataloader.get_file_name(0), ' Batch : ', batch + 1, ' Sequence: ', sequence + 1, ' Sequence mean error: ', err, ' Sequence final error: ', f_err, ' time: ', end - start) loss_batch = loss_batch / dataloader.batch_size err_batch = err_batch / dataloader.batch_size f_err_batch = f_err_batch / dataloader.batch_size num_of_batch += 1 loss_epoch += loss_batch.item() err_epoch += err_batch f_err_epoch += f_err_batch epoch_result.append(results) if dataloader.num_batches != 0: loss_epoch = loss_epoch / dataloader.num_batches err_epoch = err_epoch / dataloader.num_batches f_err_epoch = f_err_epoch / dataloader.num_batches print( 'valid_loss = {:.3f}, valid_mean_err = {:.3f}, valid_final_err = {:.3f}' .format(loss_epoch, err_epoch, f_err_epoch)) dataloader.write_to_plot_file( epoch_result, os.path.join(plot_directory, plot_validation_file_directory))
sample_args = parser.parse_args() '''KITTI Training Setting''' save_directory = '/home/serene/Documents/KITTIData/GT/save/pixel_data/1' with open(os.path.join(save_directory, 'config.pkl'), 'rb') as f: saved_args = pickle.load(f) f = open('/home/serene/Documents/KITTIData/GT/save/pixel_data/1/results.pkl', 'rb') results = pickle.load(f) dataset = [sample_args.visual_dataset] data_loader = DataLoader(1, sample_args.pred_length + sample_args.obs_length, dataset, True, infer=True) #print(data_loader.data[0][0].shape) # for j in range(len(data_loader.data[0])): # # sourceFileName = "/home/hesl/PycharmProjects/social-lstm-tf-HW/data/KITTI-17/img1/" + str(j + 1).zfill(6) + ".jpg" # # avatar= cv2.imread(sourceFileName) # #drawAvatar= ImageDraw.Draw(avatar) # #print(avatar.shape) # xSize = avatar.shape[1] # ySize = avatar.shape[0] # #print(data_loader.data[0][0][0]) # for i in range(20):
optimizer = RMSprop(lr=0.003) model.compile(loss=negative_log_likelihood_sequences, optimizer=optimizer) yPadded = np.pad(y, ((0, 0), (0, 0), (0, 3)), 'constant', constant_values=(0., 0.)) # the values is not important print('yPadded', yPadded.shape) history = model.fit(X, yPadded, batch_size=50, epochs=1, verbose=2) # model.save('many_to_many.h5') return history seqLength = 20 obsLength = 8 predLength = 12 data_loader = DataLoader(seq_length=seqLength) # , forcePreProcess=True) trajectoriesTrain, trajectoriesTest = data_loader.get_data(test_dataset=4) xA = np.array(trajectoriesTrain, dtype='float32') print('training set shape', xA.shape) X = xA[:, 0:obsLength, :] y = xA[:, obsLength:seqLength, :] xOneFrame = xA[:, 0, :] yOneFrame = xA[:, obsLength, :] hist = train_many2one(X, yOneFrame) # hist = train_many2many(X, y) import matplotlib.pyplot as plt #print(hist.history['loss'])
def train(args): print("INPUT SEQUENCE LENGTH: {}".format(args.seq_length)) print("OUTPUT SEQUENCE LENGTH: {}".format(args.pred_length)) # Construct the DataLoader object dataloader = DataLoader(args.batch_size, args.seq_length + 1, forcePreProcess=False) # Construct the ST-graph object stgraph = ST_GRAPH(1, args.seq_length + 1) # Log directory log_directory = "../log/" # Logging file log_file_curve = open(os.path.join(log_directory, "log_curve.txt"), "w") log_file = open(os.path.join(log_directory, "val.txt"), "w") # Save directory save_directory = "../../save_weight/" # Open the configuration file # 현재 argument 세팅 저장. with open(os.path.join(save_directory, "config.pkl"), "wb") as f: pickle.dump(args, f) # Path to store the checkpoint file def checkpoint_path(x): return os.path.join(save_directory, "4Dgraph.S-{}.P-{}.srnn_model_"\ .format(args.seq_length, args.pred_length)\ + str(x) + ".tar") # Initialize net net = SRNN(args) if args.use_cuda: net = net.cuda() optimizer = torch.optim.Adam(net.parameters(), weight_decay=1e-5) # learning_rate = args.learning_rate logging.info("Training begin") best_val_loss = 100 best_epoch = 0 # Training for epoch in range(args.num_epochs): dataloader.reset_batch_pointer(valid=False) # Initialization loss_epoch = 0 # For each batch # dataloader.num_batches = 10. 1 epoch have 10 batches for batch in range(dataloader.num_batches): start = time.time() # Get batch data, mini-batch x, _, _, d = dataloader.next_batch(randomUpdate=True) # Loss for this batch loss_batch = 0 # For each sequence in the batch for sequence in range( dataloader.batch_size): #미니 배치에 있는 각 sequence 데이터에 대한 처리. # Construct the graph for the current sequence in {nodes, edges} stgraph.readGraph([x[sequence]]) nodes, edges, nodesPresent, edgesPresent = stgraph.getSequence( ) #미니 배치에 있는 각 sequence의 graph 정보. ##### Convert to cuda variables ##### nodes = Variable(torch.from_numpy(nodes).float()) # nodes[0] represent all the person(object)'s corrdinate show up in frame 0. if args.use_cuda: nodes = nodes.cuda() edges = Variable(torch.from_numpy(edges).float()) if args.use_cuda: edges = edges.cuda() # Define hidden states numNodes = nodes.size()[1] #numNodes hidden_states_node_RNNs = Variable( torch.zeros(numNodes, args.node_rnn_size)) if args.use_cuda: hidden_states_node_RNNs = hidden_states_node_RNNs.cuda() hidden_states_edge_RNNs = Variable( torch.zeros(numNodes * numNodes, args.edge_rnn_size)) if args.use_cuda: hidden_states_edge_RNNs = hidden_states_edge_RNNs.cuda() cell_states_node_RNNs = Variable( torch.zeros(numNodes, args.node_rnn_size)) if args.use_cuda: cell_states_node_RNNs = cell_states_node_RNNs.cuda() cell_states_edge_RNNs = Variable( torch.zeros(numNodes * numNodes, args.edge_rnn_size)) if args.use_cuda: cell_states_edge_RNNs = cell_states_edge_RNNs.cuda() hidden_states_super_node_RNNs = Variable( #NOTE: 0 for peds., 1 for Bic., 2 for Veh. torch.zeros(3, args.node_rnn_size)) if args.use_cuda: hidden_states_super_node_RNNs = hidden_states_super_node_RNNs.cuda( ) cell_states_super_node_RNNs = Variable( torch.zeros(3, args.node_rnn_size)) if args.use_cuda: cell_states_super_node_RNNs = cell_states_super_node_RNNs.cuda( ) hidden_states_super_node_Edge_RNNs = Variable( torch.zeros(3, args.edge_rnn_size)) if args.use_cuda: hidden_states_super_node_Edge_RNNs = ( hidden_states_super_node_Edge_RNNs.cuda()) cell_states_super_node_Edge_RNNs = Variable( torch.zeros(3, args.edge_rnn_size)) if args.use_cuda: cell_states_super_node_Edge_RNNs = ( cell_states_super_node_Edge_RNNs.cuda()) # Zero out the gradients // Initialization Step net.zero_grad() optimizer.zero_grad() # Forward prop outputs, _, _, _, _, _, _, _, _, _ = net( nodes[:args.seq_length], edges[:args.seq_length], nodesPresent[:-1], edgesPresent[:-1], hidden_states_node_RNNs, hidden_states_edge_RNNs, cell_states_node_RNNs, cell_states_edge_RNNs, hidden_states_super_node_RNNs, hidden_states_super_node_Edge_RNNs, cell_states_super_node_RNNs, cell_states_super_node_Edge_RNNs, ) # Compute loss loss = Gaussian2DLikelihood(outputs, nodes[1:], nodesPresent[1:], args.pred_length) loss_batch += loss.item() # embed() # Compute gradients loss.backward() # Clip gradients torch.nn.utils.clip_grad_norm(net.parameters(), args.grad_clip) # Update parameters optimizer.step() # Reset the stgraph stgraph.reset() end = time.time() loss_batch = loss_batch / dataloader.batch_size ##### NOTE: Expected Loss; E[L] loss_epoch += loss_batch logging.info( "{}/{} (epoch {}), train_loss = {:.12f}, time/batch = {:.12f}". format( epoch * dataloader.num_batches + batch, args.num_epochs * dataloader.num_batches, epoch, loss_batch, end - start, )) # Compute loss for the entire epoch loss_epoch /= dataloader.num_batches # Log it log_file_curve.write(str(epoch) + "," + str(loss_epoch) + ",") ##################### Validation Part ##################### dataloader.reset_batch_pointer(valid=True) loss_epoch = 0 for batch in range(dataloader.valid_num_batches): # Get batch data x, _, d = dataloader.next_valid_batch(randomUpdate=False) # Loss for this batch loss_batch = 0 for sequence in range(dataloader.batch_size): stgraph.readGraph([x[sequence]]) nodes, edges, nodesPresent, edgesPresent = stgraph.getSequence( ) # Convert to cuda variables nodes = Variable(torch.from_numpy(nodes).float()) if args.use_cuda: nodes = nodes.cuda() edges = Variable(torch.from_numpy(edges).float()) if args.use_cuda: edges = edges.cuda() # Define hidden states numNodes = nodes.size()[1] hidden_states_node_RNNs = Variable( torch.zeros(numNodes, args.node_rnn_size)) if args.use_cuda: hidden_states_node_RNNs = hidden_states_node_RNNs.cuda() hidden_states_edge_RNNs = Variable( torch.zeros(numNodes * numNodes, args.edge_rnn_size)) if args.use_cuda: hidden_states_edge_RNNs = hidden_states_edge_RNNs.cuda() cell_states_node_RNNs = Variable( torch.zeros(numNodes, args.node_rnn_size)) if args.use_cuda: cell_states_node_RNNs = cell_states_node_RNNs.cuda() cell_states_edge_RNNs = Variable( torch.zeros(numNodes * numNodes, args.edge_rnn_size)) if args.use_cuda: cell_states_edge_RNNs = cell_states_edge_RNNs.cuda() hidden_states_super_node_RNNs = Variable( torch.zeros(3, args.node_rnn_size)) if args.use_cuda: hidden_states_super_node_RNNs = hidden_states_super_node_RNNs.cuda( ) cell_states_super_node_RNNs = Variable( torch.zeros(3, args.node_rnn_size)) if args.use_cuda: cell_states_super_node_RNNs = cell_states_super_node_RNNs.cuda( ) hidden_states_super_node_Edge_RNNs = Variable( torch.zeros(3, args.edge_rnn_size)) if args.use_cuda: hidden_states_super_node_Edge_RNNs = ( hidden_states_super_node_Edge_RNNs.cuda()) cell_states_super_node_Edge_RNNs = Variable( torch.zeros(3, args.edge_rnn_size)) if args.use_cuda: cell_states_super_node_Edge_RNNs = ( cell_states_super_node_Edge_RNNs.cuda()) outputs, _, _, _, _, _, _, _, _, _ = net( nodes[:args.seq_length], edges[:args.seq_length], nodesPresent[:-1], edgesPresent[:-1], hidden_states_node_RNNs, hidden_states_edge_RNNs, cell_states_node_RNNs, cell_states_edge_RNNs, hidden_states_super_node_RNNs, hidden_states_super_node_Edge_RNNs, cell_states_super_node_RNNs, cell_states_super_node_Edge_RNNs, ) # Compute loss loss = Gaussian2DLikelihood(outputs, nodes[1:], nodesPresent[1:], args.pred_length) loss_batch += loss.item() # Reset the stgraph stgraph.reset() loss_batch = loss_batch / dataloader.batch_size #### NOTE: Expected Loss; E[L] loss_epoch += loss_batch loss_epoch = loss_epoch / dataloader.valid_num_batches # Update best validation loss until now if loss_epoch < best_val_loss: best_val_loss = loss_epoch best_epoch = epoch # Record best epoch and best validation loss logging.info("(epoch {}), valid_loss = {:.3f}".format( epoch, loss_epoch)) logging.info("Best epoch {}, Best validation loss {}".format( best_epoch, best_val_loss)) # Log it log_file_curve.write(str(loss_epoch) + "\n") # Save the model after each epoch logging.info("Saving model") torch.save( { "epoch": epoch, "state_dict": net.state_dict(), "optimizer_state_dict": optimizer.state_dict(), }, checkpoint_path(epoch), ) # Record the best epoch and best validation loss overall logging.info("Best epoch {}, Best validation loss {}".format( best_epoch, best_val_loss)) # Log it log_file.write(str(best_epoch) + "," + str(best_val_loss)) # Close logging files log_file.close() log_file_curve.close()
def train(args): data_loader = DataLoader(args.batch_size, args.seq_length, args.data_scale) if args.model_dir != '' and not os.path.exists(args.model_dir): os.makedirs(args.model_dir) with open(os.path.join(args.model_dir, 'config.pkl'), 'wb') as f: pickle.dump(args, f) model = Model(args) with tf.Session() as sess: summary_writer = tf.summary.FileWriter( os.path.join(args.model_dir, 'log'), sess.graph) tf.global_variables_initializer().run() saver = tf.train.Saver(tf.global_variables(), max_to_keep=0) for e in range(args.num_epochs): sess.run( tf.assign(model.lr, args.learning_rate * (args.decay_rate**e))) data_loader.reset_batch_pointer() v_x, v_y = data_loader.validation_data() valid_feed = { model.input_data: v_x, model.target_data: v_y, model.state_in: model.state_in.eval() } state = model.state_in.eval() for b in range(data_loader.num_batches): ith_train_step = e * data_loader.num_batches + b start = time.time() x, y = data_loader.next_batch() feed = { model.input_data: x, model.target_data: y, model.state_in: state } train_loss_summary, train_loss, state, _ = sess.run([ model.train_loss_summary, model.cost, model.state_out, model.train_op ], feed) summary_writer.add_summary(train_loss_summary, ith_train_step) valid_loss_summary, valid_loss, = sess.run( [model.valid_loss_summary, model.cost], valid_feed) summary_writer.add_summary(valid_loss_summary, ith_train_step) end = time.time() print( "{}/{} (epoch {}), train_loss = {:.3f}, valid_loss = {:.3f}, time/batch = {:.3f}" .format(ith_train_step, args.num_epochs * data_loader.num_batches, e, train_loss, valid_loss, end - start)) if (ith_train_step % args.save_every == 0) and (ith_train_step > 0): checkpoint_path = os.path.join(args.model_dir, 'model.ckpt') saver.save(sess, checkpoint_path, global_step=ith_train_step) print("model saved to {}".format(checkpoint_path))
def main(): # Define the parser parser = argparse.ArgumentParser() # Observed length of the trajectory parameter parser.add_argument('--obs_length', type=int, default=5, help='Observed length of the trajectory') # Predicted length of the trajectory parameter parser.add_argument('--pred_length', type=int, default=5, help='Predicted length of the trajectory') # Test dataset parser.add_argument('--test_dataset', type=int, default=3, help='Dataset to be tested on') # Read the arguments sample_args = parser.parse_args() sample_args.train_logs = os.path.join('..', 'train_logs', 'lstm') # Load the saved arguments to the model from the config file with open(os.path.join(sample_args.train_logs, 'save', 'config.pkl'), 'rb') as f: saved_args = pickle.load(f) # Initialize with the saved args model = Model(saved_args, True) # Initialize TensorFlow session sess = tf.InteractiveSession() # Initialize TensorFlow saver saver = tf.train.Saver() # Get the checkpoint state to load the model from ckpt = tf.train.get_checkpoint_state( os.path.join(sample_args.train_logs, 'model')) print('loading model: ', ckpt.model_checkpoint_path) # Restore the model at the checpoint saver.restore(sess, ckpt.model_checkpoint_path) # Dataset to get data from dataset = [sample_args.test_dataset] # Initialize the dataloader object to # Get sequences of length obs_length+pred_length data_loader = DataLoader(1, sample_args.pred_length + sample_args.obs_length, dataset, True) # Reset the data pointers of the data loader object data_loader.reset_batch_pointer() # Maintain the total_error until now total_error = 0. counter = 0. for b in range(data_loader.num_batches): # Get the source, target data for the next batch x, y = data_loader.next_batch() # The observed part of the trajectory obs_traj = x[0][:sample_args.obs_length] # Get the complete trajectory with both the observed and the predicted part from the model complete_traj = model.sample(sess, obs_traj, x[0], num=sample_args.pred_length) # Compute the mean error between the predicted part and the true trajectory total_error += get_mean_error(complete_traj, x[0], sample_args.obs_length) print("Processed trajectory number : ", b, "out of ", data_loader.num_batches, " trajectories") # Print the mean error across all the batches print("Total mean error of the model is ", total_error / data_loader.num_batches)
# -*- coding: utf-8 -*- from __future__ import absolute_import from __future__ import division from __future__ import print_function import tensorflow as tf import experiments.Hybird_tVAE_test as T import utils.DataLoader as D import utils.Utils as U from models.Hybird_tVAE import Hybird_tVAE flags = tf.app.flags # data_loader data_loader = D.DataLoader(D.Vocab('europarl_hybird_tvae', D.Level.CHAR)) # ptb_loader = D.DataLoader(D.Vocab('ptb_hybird_tvae', D.Level.CHAR)) # hybird_tvae config flags.DEFINE_string('model_name', 'Hybird_tVAE', '') flags.DEFINE_string('ckpt_path', './results/Hybird_tVAE/ckpt/', '') flags.DEFINE_string('logs_path', './results/Hybird_tVAE/logs/', '') flags.DEFINE_integer('batch_size', 32, '') flags.DEFINE_integer('steps', U.epoch_to_step(10, data_loader.train_size, batch_size=32), '') flags.DEFINE_integer('lr', 0.001, 'learning rate') flags.DEFINE_integer('z_size', 32, '') flags.DEFINE_integer('seq_len', 60, '') flags.DEFINE_integer('rnn_num', 1, '') flags.DEFINE_integer('embed_size', 80, '') flags.DEFINE_integer('vocab_size', data_loader.vocab_size, '')
def main(): ''' 主要目的為 計算測試資料的 error rate ''' parser = argparse.ArgumentParser() # Observed length of the trajectory parameter parser.add_argument('--obs_length', type=int, default=180, help='Observed length of the trajectory') # Predicted length of the trajectory parameter # parser.add_argument('--pred_length', type=int, default=378-60-1, # help='Predicted length of the trajectory') parser.add_argument('--pred_length', type=int, default=240, help='Predicted length of the trajectory') # Model to be loaded parser.add_argument('--epoch', type=int, default=199, help='Epoch of model to be loaded') # cuda support parser.add_argument('--use_cuda', action="store_true", default=False, help='Use GPU or not') # gru model parser.add_argument('--gru', action="store_true", default=False, help='True : GRU cell, False: LSTM cell') # method selection parser.add_argument( '--method', type=int, default=1, help= 'Method of lstm will be used (1 = social lstm, 2 = obstacle lstm, 3 = vanilla lstm)' ) # Parse the parameters sample_args = parser.parse_args() # for drive run prefix = '' f_prefix = '.' method_name = "OLSTM" model_name = "LSTM" save_tar_name = method_name + "_lstm_model_" if sample_args.gru: model_name = "GRU" save_tar_name = method_name + "_gru_model_" print("Selected method name: ", method_name, " model name: ", model_name) # Save directory save_directory = os.path.join(f_prefix, 'model/', method_name, model_name) # plot directory for plotting in the future plot_directory = os.path.join(f_prefix, 'plot/', method_name, model_name) result_directory = os.path.join(f_prefix, 'result/', method_name) plot_test_file_directory = 'test' # Define the path for the config file for saved args with open(os.path.join(save_directory, 'config.pkl'), 'rb') as f: saved_args = pickle.load(f) seq_lenght = sample_args.pred_length + sample_args.obs_length # Create the DataLoader object dataloader = DataLoader(f_prefix, 1, sample_args.pred_length + sample_args.obs_length, forcePreProcess=True, infer=True) create_directories(os.path.join(result_directory, model_name), dataloader.get_all_directory_namelist()) create_directories(plot_directory, [plot_test_file_directory]) dataloader.reset_batch_pointer(valid=False) dataset_pointer_ins = dataloader.dataset_pointer smallest_err = 100000 smallest_err_iter_num = -1 origin = (0, 0) reference_point = (0, 1) submission_store = [] # store submission data points (txt) result_store = [] # store points for plotting # Initialize net net = OLSTM(saved_args, True) if sample_args.use_cuda: net = net.cuda() # Get the checkpoint path checkpoint_path = os.path.join( save_directory, save_tar_name + str(sample_args.epoch) + '.tar') if os.path.isfile(checkpoint_path): print('Loading checkpoint') checkpoint = torch.load(checkpoint_path) model_epoch = checkpoint['epoch'] net.load_state_dict(checkpoint['state_dict']) print('Loaded checkpoint at epoch', model_epoch) results_it = [] for iterator in range(50): x_seq_arr = [] ret_x_seq_arr = [] error_arr = [] expected_day_arr = [] predicted_day_arr = [] total_error = 0 for batch in range(dataloader.num_batches): # Get data x, y, d = dataloader.next_batch(randomUpdate=False) # Get the sequence x_seq, y_seq, d_seq = x[0], y[0], d[0] x_seq = np.array(x_seq) ''' x_seq = dataloader.inverse_transform_MinMaxScaler(x_seq) print('{}/{}'.format(batch, dataloader.num_batches)) x_seq[sample_args.obs_length:,-2]= 17 x_seq[sample_args.obs_length:,-1]= 28 x_seq = dataloader.fit_transform_MinMaxScaler(x_seq) ''' x_seq = Variable(torch.from_numpy(x_seq).float()) if sample_args.use_cuda: x_seq = x_seq.cuda() obs_data = x_seq[:sample_args.obs_length] ret_x_seq = sample(sample_args, x_seq, net) error = get_mean_error(x_seq[sample_args.obs_length:], ret_x_seq[sample_args.obs_length:], False) total_error += error # 顯示預測 # x_seq = result[0] x_seq = x_seq.data.cpu().numpy() x_seq = np.reshape(x_seq, (x_seq.shape[0], saved_args.input_size)) x_seq = dataloader.inverse_transform_MinMaxScaler(x_seq) # ret_x_seq = result[1] ret_x_seq = ret_x_seq.data.cpu().numpy() ret_x_seq = np.reshape(ret_x_seq, (ret_x_seq.shape[0], saved_args.input_size)) ret_x_seq = dataloader.inverse_transform_MinMaxScaler(ret_x_seq) gt = (x_seq[:, 0] - x_seq[:, 2]) / (x_seq[:, 1] - x_seq[:, 0]) pred = (ret_x_seq[:, 0] - ret_x_seq[:, 2]) / (ret_x_seq[:, 1] - ret_x_seq[:, 0]) gt2 = gt[sample_args.obs_length:] pred2 = pred[sample_args.obs_length:] expected_day = np.mean(gt2) predicted_day = np.mean(pred2) # print(expected_day, predicted_day, expected_day-predicted_day) # print('Error: ',error) expected_day = np.mean(gt2) predicted_day = np.mean(pred2) x_seq_arr.append(x_seq) ret_x_seq_arr.append(ret_x_seq) error_arr.append(error.data.cpu().numpy()) expected_day_arr.append(expected_day) predicted_day_arr.append(predicted_day) # fig, axs = plt.subplots(6, 1) # axs[0].plot(ret_x_seq[:,0], color = 'blue' , label = 'Predict h1', linestyle='--', marker='^') # axs[0].plot(x_seq[:,0], color = 'red', label = 'Real h1', linestyle='-', marker='.') # axs[1].plot(ret_x_seq[:,1], color = 'blue' , label = 'Predict h2', linestyle='--', marker='^') # axs[1].plot(x_seq[:,1], color = 'red', label = 'Real h2', linestyle='-', marker='.') # axs[2].plot(ret_x_seq[:,2], color = 'blue' , label = 'Predict h3', linestyle='--', marker='^') # axs[2].plot(x_seq[:,2], color = 'red', label = 'Real h3', linestyle='-', marker='.') # axs[3].plot(pred, color = 'blue' , label = 'Predict h3', linestyle='--', marker='^') # axs[3].plot(gt, color = 'red', label = 'Real h3', linestyle='-', marker='.') # axs[4].plot(ret_x_seq[:,-2], color = 'blue' , label = 'Predict Tevwi', linestyle='--', marker='^') # axs[4].plot(x_seq[:,-2], color = 'red', label = 'Real Tevwi', linestyle='-', marker='.') # axs[5].plot(ret_x_seq[:,-1], color = 'blue' , label = 'Predict Tcdwi', linestyle='--', marker='^') # axs[5].plot(x_seq[:,-1], color = 'red', label = 'Real Tcdwi', linestyle='-', marker='.') # for ax in axs: # ax.legend() # ax.grid() # plt.show() total_error = total_error / dataloader.num_batches if total_error < smallest_err: print("**********************************************************") print('Best iteration has been changed. Previous best iteration: ', smallest_err_iter_num, 'Error: ', smallest_err) print('New best iteration : ', iterator, 'Error: ', total_error) smallest_err_iter_num = iterator smallest_err = total_error results_it.append((sample_args.pred_length, sample_args.obs_length, x_seq_arr, ret_x_seq_arr, error_arr)) dataloader.write_to_plot_file([results_it[smallest_err_iter_num]], os.path.join(plot_directory, plot_test_file_directory))
alpha_num = const.ALPHA_NUM lam_lp = const.LAM_LP lam_gdl = const.LAM_GDL lam_adv = const.LAM_ADV lam_flow = const.LAM_FLOW adversarial = (lam_adv != 0) summary_dir = const.SUMMARY_DIR snapshot_dir = const.SNAPSHOT_DIR print(const) # define dataset with tf.name_scope('dataset'): train_loader = DataLoader(train_folder, resize_height=height, resize_width=width) train_dataset = train_loader(batch_size=batch_size, time_steps=num_his, num_pred=1) train_it = train_dataset.make_one_shot_iterator() train_videos_clips_tensor = train_it.get_next() train_videos_clips_tensor.set_shape( [batch_size, height, width, 3 * (num_his + 1)]) train_inputs = train_videos_clips_tensor[..., 0:num_his * 3] train_gt = train_videos_clips_tensor[..., -3:] print('train inputs = {}'.format(train_inputs)) print('train prediction gt = {}'.format(train_gt))
model.add(Reshape((-1, 3))) model.summary() return model if __name__ == '__main__': train_size = 1054 * 5 valid_size = 1186 * 5 batch_size = 72 epochs = 100 train_loader = DataLoader( file_glob_pattern='no_compensate_feature/*.train.*.npy', batch_size=batch_size) valid_loader = DataLoader( file_glob_pattern='no_compensate_feature/*.valid.*.npy', batch_size=batch_size) model_ckpt = ModelCheckpoint('model/no_compensate.deconv.model.keras.h5', verbose=1, save_best_only=True) tensorboard = TensorBoard(log_dir='./logs/deconv_no_compensate', histogram_freq=0, write_graph=True, write_images=False) model = build_model() # model.compile(loss = 'categorical_crossentropy', optimizer = Adam(lr = 1e-4), metrics = ['accuracy']) # history = model.fit_generator(train_loader, steps_per_epoch = train_size // batch_size, epochs = epochs, validation_data = valid_loader, validation_steps = valid_size // batch_size
""" In these experiments we will try logistic regression, svms, ridge, decision trees, naive bayes and random forests classifiers across a wide variety of parameters for each algorithm and test them via nested cross validation method. """ from utils import DataLoader, nested_cross_val from sklearn.ensemble import RandomForestClassifier from sklearn.feature_extraction.text import TfidfVectorizer from sklearn.linear_model import LogisticRegression, RidgeClassifier from sklearn.naive_bayes import MultinomialNB, BernoulliNB from sklearn.model_selection import KFold, GridSearchCV from sklearn.pipeline import Pipeline from sklearn.svm import SVC from sklearn.tree import DecisionTreeClassifier from sklearn.metrics import make_scorer, confusion_matrix, f1_score, accuracy_score, precision_score, recall_score dl = DataLoader('data/Ethos_Dataset_Binary.csv') X, y = dl.get_data() f = open("res/setA.txt", "w+") f.write("{: <7} | {: <7} {: <7} {: <7} {: <7} {: <7} {: <7} {: <7} {: <7} \n". format('Method', 'Duration', 'scoreTi', 'F1', 'Prec.', 'Recall', 'Acc.', 'Spec.', 'Sens.')) f.write( "=========================================================================\n" ) f.close() """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """ Run Naive Bayes """ """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """""" mNB = MultinomialNB() vec = TfidfVectorizer(analyzer='word') pipe = Pipeline(steps=[('vec', vec), ('mNB', mNB)])
def train(args): ## 19th Feb : move training files for 3 && 4 to fine_obstacle under copy_2 # os.chdir('/home/serene/PycharmProjects/srnn-pytorch-master/') # context_factor is an experiment for including potential destinations of pedestrians in the graph. # did not yield good improvement os.chdir('/home/serene/Documents/copy_srnn_pytorch/srnn-pytorch-master') # os.chdir('/home/siri0005/srnn-pytorch-master') # base_dir = '../fine_obstacle/prelu/p_02/' base_dir = '../fine_obstacle/prelu/p_02/' # '../MultiNodeAttn_HH/' # os.chdir('/home/serene/Documents/KITTIData/GT') datasets = [i for i in [0,1,2,3,4]] # Remove the leave out dataset from the datasets datasets.remove(args.leaveDataset) # datasets = [0] # args.leaveDataset = 0 # Construct the DataLoader object dataloader = DataLoader(args.batch_size, args.seq_length + 1, datasets, forcePreProcess=True) # Construct the ST-graph object stgraph = ST_GRAPH(1, args.seq_length + 1) # Log directory # log_directory = './log/world_data/normalized_01/' log_directory = base_dir+'log/' log_directory += str(args.leaveDataset) + '/' log_directory += 'log_attention/' # Logging file log_file_curve = open(os.path.join(log_directory, 'log_curve.txt'), 'w') log_file = open(os.path.join(log_directory, 'val.txt'), 'w') # Save directory # save_directory = './save/world_data/normalized_01/' save_directory = base_dir+'save/' save_directory += str(args.leaveDataset)+'/' save_directory += 'save_attention/' # log RELU parameter param_log_dir = save_directory + 'param_log.txt' param_log = open(param_log_dir , 'w') # Open the configuration file with open(os.path.join(save_directory, 'config.pkl'), 'wb') as f: pickle.dump(args, f) # Path to store the checkpoint file def checkpoint_path(x): return os.path.join(save_directory, 'srnn_model_'+str(x)+'.tar') # Initialize net net = SRNN(args) # srnn_model = SRNN(args) # net = torch.nn.DataParallel(srnn_model) # CUDA_VISIBLE_DEVICES = 1 net.cuda() optimizer = torch.optim.Adam(net.parameters(), lr=args.learning_rate) # optimizer = torch.optim.RMSprop(net.parameters(), lr=args.learning_rate, momentum=0.0001, centered=True) # learning_rate = args.learning_rate print('Training begin') best_val_loss = 100 best_epoch = 0 # start_epoch = 0 # if args.leaveDataset == 3: # start_epoch =checkpoint_path(0) # ckp = torch.load(checkpoint_path(3)) # net.load_state_dict(ckp['state_dict']) # optimizer.load_state_dict(ckp['optimizer_state_dict']) # ckp = torch.load(checkpoint_path(4)) # net.load_state_dict(ckp['state_dict']) # optimizer.load_state_dict(ckp['optimizer_state_dict']) # last_epoch = ckp['epoch'] # rang = range(last_epoch, args.num_epochs, 1) rang = range(args.num_epochs) # Training for epoch in rang: dataloader.reset_batch_pointer(valid=False) loss_epoch = 0 # if args.leaveDataset == 2 and epoch == 0: # dataloader.num_batches = dataloader.num_batches + start_epoch # epoch += start_epoch # For each batch stateful = True # flag that controls transmission of previous hidden states to current hidden states vectors. # Part of statefulness for batch in range( dataloader.num_batches): start = time.time() # Get batch data x, _, _, d = dataloader.next_batch(randomUpdate=True) ## shuffling input + stateless lstm # Loss for this batch loss_batch = 0 # For each sequence in the batch for sequence in range(dataloader.batch_size): # Construct the graph for the current sequence stgraph.readGraph([x[sequence]],d, args.distance_thresh) nodes, edges, nodesPresent, edgesPresent,obsNodes, obsEdges, obsNodesPresent, obsEdgesPresent = stgraph.getSequence() # # Convert to cuda variables nodes = Variable(torch.from_numpy(nodes).float()).cuda() edges = Variable(torch.from_numpy(edges).float()).cuda() obsNodes = Variable(torch.from_numpy(obsNodes).float()).cuda() obsEdges = Variable(torch.from_numpy(obsEdges).float()).cuda() ## Modification : reset hidden and cell states only once after every batch ; keeping states updated during sequences 31st JAN # Define hidden states numNodes = nodes.size()[1] numObsNodes = obsNodes.size()[1] # if not stateful: hidden_states_node_RNNs = Variable(torch.zeros(numNodes, args.human_node_rnn_size)).cuda() hidden_states_edge_RNNs = Variable(torch.zeros(numNodes * numNodes, args.human_human_edge_rnn_size)).cuda() cell_states_node_RNNs = Variable(torch.zeros(numNodes, args.human_node_rnn_size)).cuda() cell_states_edge_RNNs = Variable(torch.zeros(numNodes * numNodes, args.human_human_edge_rnn_size)).cuda() ## new update : 25th JAN , let the hidden state transition begin with negative ones ## such initialization did not lead to any new learning results, network converged quickly and loss did not decrease below # -6 hidden_states_obs_node_RNNs = Variable(torch.zeros(numObsNodes, args.obs_node_rnn_size)).cuda() hidden_states_obs_edge_RNNs = Variable(torch.zeros(numNodes * numNodes, args.human_obstacle_edge_rnn_size)).cuda() cell_states_obs_node_RNNs = Variable(torch.zeros(numObsNodes, args.obs_node_rnn_size)).cuda() cell_states_obs_edge_RNNs = Variable(torch.zeros(numNodes * numNodes, args.human_obstacle_edge_rnn_size)).cuda() net.zero_grad() optimizer.zero_grad() # Forward prop # _ = \ outputs, h_node_rnn, h_edge_rnn, cell_node_rnn, cell_edge_rnn,o_h_node_rnn, o_h_edge_rnn, o_cell_node_rnn, o_cell_edge_rnn, _ = \ net(nodes[:args.seq_length], edges[:args.seq_length], nodesPresent[:-1], edgesPresent[:-1], hidden_states_node_RNNs, hidden_states_edge_RNNs, cell_states_node_RNNs, cell_states_edge_RNNs, obsNodes[:args.seq_length], obsEdges[:args.seq_length], obsNodesPresent[:-1], obsEdgesPresent[:-1] ,hidden_states_obs_node_RNNs, hidden_states_obs_edge_RNNs, cell_states_obs_node_RNNs, cell_states_obs_edge_RNNs) # # else: # # if len(nodes) == len(hidden_states_node_RNNs): # no additional nodes introduced in graph # # hidden_states_node_RNNs = Variable(h_node_rnn).cuda() # # cell_states_node_RNNs = Variable(cell_node_rnn).cuda() # # else: # for now number of nodes is only increasing in time as new pedestrians are detected in the scene # # pad_size = len(nodes) - len(hidden_states_node_RNNs) # cell_states_node_RNNs = Variable(np.pad(cell_node_rnn, pad_size)).cuda() # if sequence > 0: # hidden_states_node_RNNs = Variable(h_node_rnn).resize(hidden_states_node_RNNs.cpu().size()) # hidden_states_edge_RNNs = h_edge_rnn # cell_states_node_RNNs = cell_node_rnn # cell_states_edge_RNNs = cell_edge_rnn # new_num_nodes = h_node_rnn.size()[0] - hidden_states_node_RNNs.size()[0] # if h_node_rnn.size()[0] - hidden_states_node_RNNs.size()[0] >=1: # np.pad(hidden_states_node_RNNs.cpu() , new_num_nodes, mode='constant').cuda() # hidden_states_obs_node_RNNs = o_h_node_rnn # hidden_states_obs_edge_RNNs = o_h_edge_rnn # cell_states_obs_node_RNNs = o_cell_node_rnn # cell_states_obs_edge_RNNs = o_cell_edge_rnn # Zero out the gradients # Compute loss loss = Gaussian2DLikelihood(outputs, nodes[1:], nodesPresent[1:], args.pred_length) loss_batch += loss.data[0] # Compute gradients loss.backward(retain_variables=True) param_log.write(str(net.alpha.data[0]) + '\n') # Clip gradients torch.nn.utils.clip_grad_norm(net.parameters(), args.grad_clip) # Update parameters optimizer.step() # Reset the stgraph stgraph.reset() end = time.time() loss_batch = loss_batch / dataloader.batch_size loss_epoch += loss_batch print( '{}/{} (epoch {}), train_loss = {:.3f}, time/batch = {:.3f}'.format(epoch * dataloader.num_batches + batch, args.num_epochs * dataloader.num_batches, epoch, loss_batch, end - start)) # Compute loss for the entire epoch loss_epoch /= dataloader.num_batches # Log it log_file_curve.write(str(epoch)+','+str(loss_epoch)+',') # Validation dataloader.reset_batch_pointer(valid=True) loss_epoch = 0 # dataloader.valid_num_batches = dataloader.valid_num_batches + start_epoch for batch in range(dataloader.valid_num_batches): # Get batch data x, _, d = dataloader.next_valid_batch(randomUpdate=False)## stateless lstm without shuffling # Loss for this batch loss_batch = 0 for sequence in range(dataloader.batch_size): stgraph.readGraph([x[sequence]], d, args.distance_thresh) nodes, edges, nodesPresent, edgesPresent,obsNodes, obsEdges, obsNodesPresent, obsEdgesPresent = stgraph.getSequence() # # Convert to cuda variables nodes = Variable(torch.from_numpy(nodes).float()).cuda() edges = Variable(torch.from_numpy(edges).float()).cuda() obsNodes = Variable(torch.from_numpy(obsNodes).float()).cuda() obsEdges = Variable(torch.from_numpy(obsEdges).float()).cuda() # Define hidden states numNodes = nodes.size()[1] hidden_states_node_RNNs = Variable(torch.zeros(numNodes, args.human_node_rnn_size)).cuda() hidden_states_edge_RNNs = Variable(torch.zeros(numNodes * numNodes, args.human_human_edge_rnn_size)).cuda() cell_states_node_RNNs = Variable(torch.zeros(numNodes, args.human_node_rnn_size)).cuda() cell_states_edge_RNNs = Variable(torch.zeros(numNodes * numNodes, args.human_human_edge_rnn_size)).cuda() numObsNodes = obsNodes.size()[1] hidden_states_obs_node_RNNs = Variable(torch.zeros(numObsNodes, args.obs_node_rnn_size)).cuda() hidden_states_obs_edge_RNNs = Variable(torch.zeros(numNodes * numNodes, args.human_obstacle_edge_rnn_size)).cuda() cell_states_obs_node_RNNs = Variable(torch.zeros(numObsNodes, args.obs_node_rnn_size)).cuda() cell_states_obs_edge_RNNs = Variable(torch.zeros(numNodes * numNodes, args.human_obstacle_edge_rnn_size)).cuda() # outputs, h_node_rnn, h_edge_rnn, cell_node_rnn, cell_edge_rnn,o_h_node_rnn ,o_h_edge_rnn, o_cell_node_rnn, o_cell_edge_rnn, _= net(nodes[:args.seq_length], edges[:args.seq_length], nodesPresent[:-1], edgesPresent[:-1], hidden_states_node_RNNs, hidden_states_edge_RNNs, cell_states_node_RNNs, cell_states_edge_RNNs , obsNodes[:args.seq_length], obsEdges[:args.seq_length], obsNodesPresent[:-1], obsEdgesPresent[:-1] ,hidden_states_obs_node_RNNs, hidden_states_obs_edge_RNNs, cell_states_obs_node_RNNs, cell_states_obs_edge_RNNs) # Compute loss loss = Gaussian2DLikelihood(outputs, nodes[1:], nodesPresent[1:], args.pred_length) loss_batch += loss.data[0] # Reset the stgraph stgraph.reset() loss_batch = loss_batch / dataloader.batch_size loss_epoch += loss_batch loss_epoch = loss_epoch / dataloader.valid_num_batches # Update best validation loss until now if loss_epoch < best_val_loss: best_val_loss = loss_epoch best_epoch = epoch # Record best epoch and best validation loss print('(epoch {}), valid_loss = {:.3f}'.format(epoch, loss_epoch)) print('Best epoch {}, Best validation loss {}'.format(best_epoch, best_val_loss)) # Log it log_file_curve.write(str(loss_epoch) + '\n') # Save the model after each epoch print('Saving model') torch.save({ 'epoch': epoch, 'state_dict': net.state_dict(), 'optimizer_state_dict': optimizer.state_dict() }, checkpoint_path(epoch)) # Record the best epoch and best validation loss overall print('Best epoch {}, Best validation loss {}'.format(best_epoch, best_val_loss)) # Log it log_file.write(str(best_epoch) + ',' + str(best_val_loss)) # Close logging files log_file.close() log_file_curve.close() param_log.close()
def main(): # os.chdir('/home/serene/Documents/KITTIData/GT/') # os.chdir('/home/siri0005/copy_srnn_pytorch/srnn-pytorch-master/')#/srnn-pytorch-master os.chdir('/home/serene/Documents/copy_srnn_pytorch/srnn-pytorch-master') H_path = ['./pedestrians/ewap_dataset/seq_eth/H.txt', './pedestrians/ewap_dataset/seq_hotel/H.txt', './pedestrians/ucy_crowd/data_zara01/H.txt', './pedestrians/ucy_crowd/data_zara02/H.txt', './pedestrians/ucy_crowd/data_students03/H.txt'] # ,1,2,3, base_dir = '../fine_obstacle/prelu/p_02/' for i in [4]:#,1,2,3,4 avg = [] ade = [] with open(base_dir+'log/{0}/log_attention/val.txt'.format(i)) as val_f: if i == 1: e = 99 else: best_val = val_f.readline() [e, val] = best_val.split(',') parser = argparse.ArgumentParser() # Observed length of the trajectory parameter parser.add_argument('--obs_length', type=int, default=8, help='Observed length of the trajectory') # Predicted length of the trajectory parameter parser.add_argument('--pred_length', type=int, default=12, help='Predicted length of the trajectory') # Test dataset parser.add_argument('--test_dataset', type=int, default=i, help='Dataset to be tested on') # Model to be loaded parser.add_argument('--epoch', type=int, default=e, help='Epoch of model to be loaded') # Parse the parameters sample_args = parser.parse_args() # Save directory save_directory = base_dir+'save/{0}/save_attention'.format(i) plot_directory = base_dir + '/selected_plots/' #'plot_1/' # Define the path for the config file for saved args with open(os.path.join(save_directory, 'config.pkl'), 'rb') as f: saved_args = pickle.load(f) # Initialize net net = SRNN(saved_args, sample_args.test_dataset, True) net.cuda() ## TODO: visualize trained weights # plt.imshow(net.humanNodeRNN.edge_embed.weight) # plt.colorbar() # plt.show() checkpoint_path = os.path.join(save_directory, 'srnn_model_'+str(sample_args.epoch)+'.tar') if os.path.isfile(checkpoint_path): print('Loading checkpoint') checkpoint = torch.load(checkpoint_path) # model_iteration = checkpoint['iteration'] model_epoch = checkpoint['epoch'] net.load_state_dict(checkpoint['state_dict']) print('Loaded checkpoint at {}'.format(model_epoch)) H_mat = np.loadtxt(H_path[i]) avg = [] ade = [] # Dataset to get data from dataset = [sample_args.test_dataset] sample_ade_error_arr = [] sample_fde_error_arr = [] num_nodes = 0 inner_num_nodes_1= 0 inner_num_nodes_2= 0 ade_sum = 0 fde_sum = 0 dataloader = DataLoader(1, sample_args.pred_length + sample_args.obs_length, dataset, True, infer=True) dataloader.reset_batch_pointer() # Construct the ST-graph object stgraph = ST_GRAPH(1, sample_args.pred_length + sample_args.obs_length) results = [] # Variable to maintain total error total_error = 0 final_error = 0 # TRY this code version for batch in range(dataloader.num_batches): sample_fde_error = [] sample_ade_error = [] running_time_sample = [] c = 0 x, _, frameIDs, d = dataloader.next_batch(randomUpdate=False) # Construct ST graph stgraph.readGraph(x, ds_ptr=d, threshold=1.0) nodes, edges, nodesPresent, edgesPresent, obsNodes, obsEdges, obsNodesPresent, obsEdgesPresent = stgraph.getSequence() nodes = Variable(torch.from_numpy(nodes).float(), volatile=True).cuda() edges = Variable(torch.from_numpy(edges).float(), volatile=True).cuda() obsNodes = Variable(torch.from_numpy(obsNodes).float()).cuda() obsEdges = Variable(torch.from_numpy(obsEdges).float()).cuda() # Separate out the observed part of the trajectory obs_nodes, obs_edges, obs_nodesPresent, obs_edgesPresent = nodes[:sample_args.obs_length], edges[:sample_args.obs_length], nodesPresent[:sample_args.obs_length], edgesPresent[:sample_args.obs_length] # Separate out the observed obstacles in a given sequence obsnodes_v, obsEdges_v, obsNodesPresent_v, obsEdgesPresent_v = obsNodes[:sample_args.obs_length], obsEdges[:sample_args.obs_length], obsNodesPresent[:sample_args.obs_length], obsEdgesPresent[:sample_args.obs_length] # if c == 0: # num_nodes += np.shape(nodes)[1] for c in range(10): num_nodes += np.shape(nodes)[1] start = time.time() # Sample function ret_nodes, ret_attn = sample(obs_nodes, obs_edges, obs_nodesPresent, obs_edgesPresent, obsnodes_v, obsEdges_v, obsNodesPresent_v,obsEdgesPresent_v, sample_args, net, nodes, edges, nodesPresent) end = time.time() running_time_sample.append((end-start)) # print('One-time Sampling took = ', (end - start), ' seconds.') # Compute mean and final displacement error total_error , _ = get_mean_error(ret_nodes[sample_args.obs_length:].data, nodes[sample_args.obs_length:].data, nodesPresent[sample_args.obs_length - 1], nodesPresent[sample_args.obs_length:], H_mat, i) # print("ADE errors:", total_error) inner_num_nodes_1 += _ sample_ade_error.append(total_error) final_error , _ = get_final_error(ret_nodes[sample_args.obs_length:].data, nodes[sample_args.obs_length:].data, nodesPresent[sample_args.obs_length - 1], nodesPresent[sample_args.obs_length:], H_mat, i) # print("final errors:", final_error) sample_fde_error.append(final_error) results.append((nodes.data.cpu().numpy(), ret_nodes.data.cpu().numpy(), nodesPresent, sample_args.obs_length, ret_attn, frameIDs)) stgraph.reset() sample_ade_error_arr.append(np.sum(sample_ade_error)) sample_fde_error_arr.append(np.sum(sample_fde_error)) sample_ade_error = np.sum(sample_ade_error, 0) if len(sample_ade_error): # sample_ade_error /= 10 sample_ade_error = torch.min(sample_ade_error) ade_sum += sample_ade_error ade.append(ade_sum) # for non-rectangular tensors for (e, idx) in zip(sample_fde_error , range(len(sample_fde_error))): if int(len(e)) > 0 : l = int(len(e)) sample_fde_error[idx] = np.sum(e) #/l else: del sample_fde_error[idx] print(sample_fde_error) if (np.ndim(sample_fde_error) == 1 and len(sample_fde_error)) or \ (np.ndim(sample_fde_error) > 1 and np.all([True for x in sample_fde_error if len(x) > 0] == True)): sample_fde_error = np.min(sample_fde_error) fde_sum += sample_fde_error avg.append(fde_sum) with open(os.path.join(save_directory, 'results.pkl'), 'wb') as f: pickle.dump(results, f) print('SUMMARY **************************//') print('One-time Sampling took = ', np.average(running_time_sample), ' seconds.') print(np.sum(ade) , ' ' , np.sum(avg)) print('average ADE', np.sum(ade) / (sample_args.pred_length * num_nodes))# print('average FDE', np.sum(avg) / (num_nodes*10))# with open(os.path.join(save_directory, 'sampling_results.txt'), 'wb') as o: np.savetxt(os.path.join(save_directory, 'sampling_results.txt'), (np.sum(ade) / (sample_args.pred_length * num_nodes), np.sum(avg) / inner_num_nodes_1))
class DCGAN(): def __init__(self): # Input shape self.channels = 3 self.img_size = 64 self.latent_dim = 100 self.time = time() self.dataset_name = 'vdsr' self.learning_rate = 1e-4 optimizer = Adam(self.learning_rate, beta_1=0.5, decay=0.00005) self.gf = 64 # filter size of generator's last layer self.df = 64 # filter size of discriminator's first layer # Configure data loader self.data_loader = DataLoader(dataset_name=self.dataset_name, img_res=(self.img_size, self.img_size), mem_load=True) self.n_data = self.data_loader.get_n_data() self.generator = self.build_generator() print( "---------------------generator summary----------------------------" ) self.generator.summary() self.generator.compile(loss='mse', optimizer=optimizer, metrics=['mse']) # Build and compile the discriminator self.discriminator = self.build_discriminator() print( "\n---------------------discriminator summary----------------------------" ) self.discriminator.summary() self.discriminator.compile(loss='mse', optimizer=optimizer, metrics=['accuracy']) make_trainable(self.discriminator, False) z = Input(shape=(self.latent_dim, )) fake_img = self.generator(z) # for the combined model, we only train ganerator self.discriminator.trainable = False validity = self.discriminator(fake_img) self.combined = Model([z], [validity]) print( "\n---------------------combined summary----------------------------" ) self.combined.summary() self.combined.compile(loss=['binary_crossentropy'], optimizer=optimizer) def build_generator(self): noise = Input(shape=(self.latent_dim, )) def deconv2d(layer_input, filters=256, kernel_size=(5, 5), strides=(2, 2), bn_relu=True): """Layers used during upsampling""" # u = UpSampling2D(size=2)(layer_input) u = Conv2DTranspose(filters, kernel_size=kernel_size, strides=strides, padding='same')(layer_input) if bn_relu: u = BatchNormalization(momentum=0.9)(u) u = Activation('relu')(u) return u generator = Dense(16 * self.gf * self.img_size // 16 * self.img_size // 16, activation="relu")(noise) generator = Reshape((self.img_size // 16, self.img_size // 16, self.gf * 16))(generator) generator = BatchNormalization()(generator) generator = Activation('relu')(generator) generator = deconv2d(generator, filters=self.gf * 8) generator = deconv2d(generator, filters=self.gf * 4) generator = deconv2d(generator, filters=self.gf * 2) generator = deconv2d(generator, filters=self.gf) generator = deconv2d(generator, filters=self.channels, kernel_size=(3, 3), strides=(1, 1), bn_relu=False) gen_img = Activation('tanh')(generator) return Model(noise, gen_img) def build_discriminator(self): def d_block(layer_input, filters, strides=1, bn=True): """Discriminator layer""" d = Conv2D(filters, kernel_size=3, strides=strides, padding='same')(layer_input) if bn: d = BatchNormalization(momentum=0.9)(d) d = LeakyReLU(alpha=0.2)(d) return d # Input img = generated image d0 = Input(shape=(self.img_size, self.img_size, self.channels)) d = d_block(d0, self.df, strides=2, bn=False) d = d_block(d, self.df * 2, strides=2) d = d_block(d, self.df * 4, strides=2) d = d_block(d, self.df * 8, strides=2) d = Flatten()(d) validity = Dense(1, activation='sigmoid')(d) return Model(d0, validity) def train(self, epochs, batch_size, sample_interval): def named_logs(model, logs): result = {} for l in zip(model.metrics_names, logs): result[l[0]] = l[1] return result start_time = datetime.datetime.now() valid = np.ones((batch_size, 1)) fake = np.zeros((batch_size, 1)) max_iter = int(self.n_data / batch_size) os.makedirs('./logs/%s' % self.time, exist_ok=True) tensorboard = TensorBoard('./logs/%s' % self.time) tensorboard.set_model(self.generator) os.makedirs('models/%s' % self.time, exist_ok=True) with open('models/%s/%s_architecture.json' % (self.time, 'generator'), 'w') as f: f.write(self.generator.to_json()) print( "\nbatch size : %d | num_data : %d | max iteration : %d | time : %s \n" % (batch_size, self.n_data, max_iter, self.time)) for epoch in range(1, epochs + 1): for iter in range(max_iter): # ------------------ # Train Generator # ------------------ ref_imgs = self.data_loader.load_data(batch_size) noise = np.random.normal(0, 1, (batch_size, self.latent_dim)) gen_imgs = self.generator.predict(noise) make_trainable(self.discriminator, True) d_loss_real = self.discriminator.train_on_batch( ref_imgs, valid * 0.9) # label smoothing d_loss_fake = self.discriminator.train_on_batch(gen_imgs, fake) d_loss = 0.5 * np.add(d_loss_real, d_loss_fake) make_trainable(self.discriminator, False) logs = self.combined.train_on_batch([noise], [valid]) tensorboard.on_epoch_end(iter, named_logs(self.combined, [logs])) if iter % (sample_interval // 10) == 0: elapsed_time = datetime.datetime.now() - start_time print( "epoch:%d | iter : %d / %d | time : %10s | g_loss : %15s | d_loss : %s " % (epoch, iter, max_iter, elapsed_time, logs, d_loss)) if (iter + 1) % sample_interval == 0: self.sample_images(epoch, iter + 1) # save weights after every epoch self.generator.save_weights('models/%s/%s_epoch%d_weights.h5' % (self.time, 'generator', epoch)) def sample_images(self, epoch, iter): os.makedirs('samples/%s' % self.time, exist_ok=True) r, c = 5, 5 noise = np.random.normal(0, 1, (r * c, self.latent_dim)) gen_img = self.generator.predict(noise) # Rescale images 0 - 1 gen_img = 0.5 * gen_img + 0.5 # Save generated images and the high resolution originals fig, axs = plt.subplots(r, c) for row in range(r): for col in range(c): axs[row, col].imshow(gen_img[5 * row + col]) axs[row, col].axis('off') fig.savefig("samples/%s/e%d-i%d.png" % (self.time, epoch, iter), bbox_inches='tight', dpi=100) plt.close()
# -*- coding: utf-8 -*- from __future__ import absolute_import from __future__ import division from __future__ import print_function import tensorflow as tf from utils import Utils as U, DataLoader as D from Hybird_tVAE import Hybird_tVAE flags = tf.app.flags # data_loader # data_loader = D.DataLoader(D.Vocab('europarl_hybird_tvae', D.Level.CHAR)) data_loader = D.DataLoader(D.Vocab('train128', D.Level.CHAR)) # hybird_tvae config flags.DEFINE_string('model_name', 'Hybird_tVAE', '') flags.DEFINE_string('ckpt_path', './results/Hybird_tVAE/ckpt_f5_1/', '') flags.DEFINE_string('logs_path', './results/Hybird_tVAE/logs_f5_1/', '') flags.DEFINE_integer('batch_size', 32, '') flags.DEFINE_integer('steps', U.epoch_to_step(5, data_loader.train_size, batch_size=64), '') flags.DEFINE_integer('lr', 0.001, 'learning rate') flags.DEFINE_integer('z_size', 32, '') flags.DEFINE_integer('seq_len', 129, '') flags.DEFINE_integer('rnn_num', 2, '') flags.DEFINE_integer('embed_size', 80, '') flags.DEFINE_integer('vocab_size', data_loader.vocab_size, '')
def sample(self, sess): def get_pi_idx(x, pdf): N = pdf.size accumulate = 0 for i in range(0, N): accumulate += pdf[i] if (accumulate >= x): return i print('error with sampling ensemble') return -1 # def sample_gaussian_2d(mu1, mu2, s1, s2, rho): # mean = [mu1, mu2] # cov = [[s1*s1, rho*s1*s2], [rho*s1*s2, s2*s2]] # x = np.random.multivariate_normal(mean, cov, 1) # return x[0][0], x[0][1] def sample_gaussian_2d(mu, s): mean = mu sigma = s x = np.random.normal(mean, sigma) print(x.shape) return x prev_x = np.random.rand(1, 90, 3 * 25) prev_state = sess.run(self.cell.zero_state(1, tf.float32)) dance = np.zeros((self.args.seq_length * 4, 3 * 25), dtype=np.float32) mixture_params = [] data_loader = DataLoader('data', 'data_files.list', 'output_body.h5') mini, maxi = data_loader.load_preprocessed('true') for i in range(self.args.seq_length * 4): feed = {self.input_data: prev_x, self.state_in: prev_state} [o_pi, o_mu, o_sigma, next_state ] = sess.run([self.pi, self.mu, self.sigma, self.state_out], feed) idx = get_pi_idx(random.random(), o_pi[0]) next_x1 = sample_gaussian_2d(o_mu[0, :, idx], o_sigma[0, :, idx]) dance[i, :] = next_x1 params = [o_pi[0], o_mu[0], o_sigma[0]] mixture_params.append(params) prev_x = np.zeros((1, 90, 3 * 25), dtype=np.float32) prev_x[0, 0:] = np.array([next_x1], dtype=np.float32) prev_state = next_state if i == 355: # archivo = open("archivo.txt","w") # archivo.write(str((dance))) # archivo.close() for j, sequence in enumerate(dance): print('Maxi,mini: ') print(maxi) print(mini) dance[j] = sequence * (maxi - mini) + mini np.savetxt('fooTODO.csv', dance, delimiter=",") return dance, mixture_params
from __future__ import division from __future__ import print_function from utils import DataLoader import numpy as np from sklearn import metrics import pandas import tensorflow as tf from tensorflow.contrib import learn ### Training data print("LOAD") loader = DataLoader('data/tweets',5,144) x,y = loader.get_xy() split = int(len(x)*0.8) X_train,X_test = x[:split],x[split:] y_train,y_test = pandas.Series(y[:split]),pandas.Series(y[split:]) y_train = y_train.convert_objects(convert_numeric=True) y_test = y_test.convert_objects(convert_numeric=True) ### Process vocabulary MAX_DOCUMENT_LENGTH = 144 print("PREPROCESS") char_processor = learn.preprocessing.ByteProcessor(MAX_DOCUMENT_LENGTH) X_train = np.array(list(char_processor.fit_transform(X_train))) X_test = np.array(list(char_processor.transform(X_test)))
def train(args): data_loader = DataLoader(args.batch_size, args.data_scale, args.bptt_length) data_loader.reset_batch_pointer() # model needs to know the dim of one-hot vectors args.char_vec_dim = data_loader.char_vec_dim # also the max length of char vector args.max_char_len = data_loader.max_char_len if args.model_dir != '' and not os.path.exists(args.model_dir): os.makedirs(args.model_dir) with open(os.path.join(args.model_dir, 'config.pkl'), 'wb') as f: pickle.dump(args, f) print("hyperparam. saved.") model = Model(args) # training with tf.Session() as sess: tf.global_variables_initializer().run() saver = tf.train.Saver() if args.load_model is not None: saver.restore(sess, args.load_model) _, ep_start = args.load_model.rsplit("-", 1) ep_start = int(ep_start) model_steps = int(ep_start * data_loader.num_batches) else: ep_start = 0 model_steps = last_model_steps = 0 last_time = time.time() for ep in range(ep_start, args.num_epochs): ep_loss = [] sess.run( tf.assign(model.lr, args.learning_rate * (args.decay_rate**ep))) for i in range(int(data_loader.num_sequences / args.batch_size)): idx = ep * data_loader.num_sequences + i * args.batch_size start = time.time() x, y, w, c = data_loader.next_batch() loss_list, model_steps = model.train( sess=sess, sequence=x, targets=y, weights=w, texts=c, subseq_length=args.bptt_length, step_count=model_steps) ep_loss += loss_list if model_steps - last_model_steps >= 100: last_model_steps = model_steps new_time = time.time() print( "Sequence %d/%d (epoch %d), batch %d, train_loss = %.3f, time/(100*batch) = %.3f" % (idx, args.num_epochs * data_loader.num_sequences, ep, model_steps, np.mean(loss_list), new_time - last_time), flush=True) last_time = new_time print("Epoch %d completed, average train loss %.6f" % (ep, np.mean(ep_loss))) if not os.path.isdir(args.model_dir): os.makedirs(args.model_dir) if (ep + 1) % args.save_every == 0: checkpoint_path = os.path.join(args.model_dir, 'model.ckpt') saver.save(sess, save_path=checkpoint_path, global_step=(ep + 1)) print("model saved.")