Exemplo n.º 1
0
    def load_model(self, path):
        # torch.load最后返回的是一个dict,里面包含了保存模型时的一些参数和模型
        checkpoint = torch.load(path, map_location='cpu')
        self.generator = self.get_generator(checkpoint)
        # AttrDict是根据参数中的dict内容生成一个更加方便访问的dict实例
        self.args = AttrDict(checkpoint['args'])
        train_path = get_dset_path(self.args.dataset_name, "train")
        test_path = get_dset_path(self.args.dataset_name, "test")
        self.args.batch_size = 1
        _, self.loader = data_loader(self.args, train_path)
        _, self.test_loader = data_loader(self.args, test_path)

        self.metrics_val = checkpoint['metrics_val']
        self.metrics_train = checkpoint['metrics_train']
Exemplo n.º 2
0
def main(args):
    test_path = get_dset_path(args.dataset_name, 'test')

    logger.info("Initializing test dataset")
    test_dset, test_loader = data_loader(args, test_path)

    net = LSTM_model(args)
    net = net.cuda()

    checkpoint_path = ".\model\lstm767.tar"
    checkpoint = torch.load(checkpoint_path)
    net.load_state_dict(checkpoint['state_dict'])
    net.eval()

    batch_error = 0
    batch_fde = 0
    for idx, batch in enumerate(test_loader):

        (obs_traj, pred_traj_gt, obs_traj_rel, pred_traj_gt_rel, non_linear_ped,
         loss_mask, seq_start_end) = batch
        num_ped = obs_traj.size(1)   # (8 n 2)
        pred_traj_gt = pred_traj_gt.cuda()
        pred_traj = net(obs_traj.cuda(), num_ped, pred_traj_gt)

        ade_1 = get_mean_error(pred_traj, pred_traj_gt)
        ade_2 = displacement_error(pred_traj, pred_traj_gt) / (pred_traj.size(1) * 12)
        fde = final_displacement_error(pred_traj, pred_traj_gt) / pred_traj.size(1)

        batch_error += ade_2
        batch_fde += fde
    ade = batch_error / (idx+1)
    fin_fde = batch_fde / (idx+1)
    logger.info("ade is {:.2f}".format(ade))
    logger.info("ade is {:.2f}".format(fin_fde))
def polyfit_visualize(args,data_path):
	degree=2 	#degree of polynomial to be fitted
	coeff=np.ones(3)
	obs_len=args.obs_len
	pred_len=args.pred_len
	dataset,dataloader=loader.data_loader(args,data_path)
	for i,batch in enumerate(dataloader):
		observed_batch=batch[0].numpy()		#observed trajectory batch
		target_batch=batch[1].numpy() 	#Target trajectory batch
		trajec=observed_batch+target_batch
		seq,peds,coords=observed_batch.shape
		for j in range(peds):
			z=np.polyfit(observed_batch[:,j,0],observed_batch[:,j,1],degree)
			coeff=np.column_stack((coeff,z))
		if(i>15):
			break
	plt.figure()
	for i in range(degree+1):
		plt.plot(coeff[i,1:],label='{} order coefficient'.format(i))
	plt.xlabel('Pedestrian Trajectories')
	plt.ylabel('Coefficients of Plynomial')
	plt.title("Fitted polynomial to dataset:{}".format(cur_dataset))	
	plt.legend()
	# plt.show(block=True)
	plt.savefig("./saved_figs/" + 'lstm_polyfit_'+cur_dataset+'predlen_' + str(pred_len) +'_obs'+str(obs_len)+'.png')
Exemplo n.º 4
0
def main(args):
    
    '''define parameters for training and testing loops!'''

    # num_epoch = 20
    # pred_len = 12
    # learning_rate = 0.001

    num_epoch = 2 #args.num_epochs
    pred_len =8 #args.pred_len
    learning_rate = 0.0017 #args.learning_rate
    
    # retrieve dataloader
    dataset, dataloader = loader.data_loader(args, data_dir)

    ''' define the network, optimizer and criterion '''
    # lstm_net = lstmNet()

    # criterion = nn.MSELoss() # MSE works best for difference between predicted and actual coordinate paths
    # # optimizer = optim.Adam(lstm_net.parameters(), lr=learning_rate)

    # # initialize lists for capturing losses/errors
    # test_loss = []
    # avg_test_loss = []
    # test_finalD_error=[]
    # test_avgD_error=[]
    # std_test_loss = []

    #calling the test function and calculating the test losses
    test_observed_batch,test_target_batch,out=test(lstm_net,args,pred_len,data_dir)
    pass
def test(args):
    test_data_dir = "/home/roongtaaahsih/ped_traj/sgan_ab/scripts/datasets/zara1/test"

    # retrieve dataloader
    dataset, dataloader = loader.data_loader(args, test_data_dir)

    # define parameters for training and testing loops
    pred_len = 12
    criterion = nn.MSELoss(
    )  # MSE works best for difference between predicted and actual coordinate paths

    # initialize lists for capturing losses
    test_loss = []

    # now, test the model
    for i, batch in enumerate(dataloader):
        test_observed_batch = batch[0]
        test_target_batch = batch[1]
        out = gru_net(
            test_observed_batch,
            pred_len=pred_len)  # forward pass of gru network for training
        print("out's shape:", out.shape)
        cur_test_loss = criterion(out, test_target_batch)  # calculate MSE loss
        print('Current test loss: {}'.format(
            cur_test_loss.item()))  # print current test loss
        test_loss.append(cur_test_loss.item())
    avg_testloss = sum(test_loss) / len(test_loss)
    print("========== Average test loss:", avg_testloss, "==========")

    pass
Exemplo n.º 6
0
def main(args):
    if os.path.isdir(args.model_path):
        filenames = os.listdir(args.model_path)
        filenames.sort()
        paths = [os.path.join(args.model_path, file_) for file_ in filenames]
    else:
        paths = [args.model_path]

    for path in paths:
        checkpoint = torch.load(path)
        generator = get_generator(checkpoint)
        _args = AttrDict(checkpoint['args'])
        path = get_dset_path(_args.dataset_name, args.dset_type)
        _, loader = data_loader(_args, path)
        ade, fde, trajs = evaluate(_args, loader, generator, args.num_samples)
        print('Dataset: {}, Pred Len: {}, ADE: {:.2f}, FDE: {:.2f}'.format(
            _args.dataset_name, _args.pred_len, ade, fde))

        path = "trajs_dumped/" + "/".join(_args.dataset_name.split("/")[:-1])
        pathlib.Path(path).mkdir(parents=True, exist_ok=True)
        with open(
                "trajs_dumped/" +
                args.model_path.split("/")[-1].split(".")[0] + "_" +
                args.dset_type + "_trajs.pkl", 'wb+') as f:
            pickle.dump(trajs, f)
        print(
            "trajs dumped at ",
            args.model_path.split("/")[-1].split(".")[0] + "_" +
            args.dset_type + "_trajs.pkl")
Exemplo n.º 7
0
def polyfit_visualize(args, data_path):
    coeff = np.ones(3)
    ped_c = 0
    dataset, dataloader = loader.data_loader(args, data_path)
    for i, batch in enumerate(dataloader):
        observed_batch = batch[0].numpy()  #observed trajectory batch
        target_batch = batch[1].numpy()  #Target trajectory batch
        trajec = observed_batch + target_batch
        seq, peds, coords = observed_batch.shape
        ped_c += peds
        for j in range(peds):
            z = np.polyfit(observed_batch[:, j, 0], observed_batch[:, j, 1],
                           degree)
            coeff = np.column_stack(
                (coeff, z)
            )  #adding the coefficients of each polynomial fitted to columns of coeff array
        if (ped_c > 60
            ):  #Just a random cap on the number of pedestrians in each dataset
            break
    # plt.figure()
    # for i in range(degree+1):
    # 	plt.plot(coeff[i,1:],label='{} order coefficient'.format(i))
    # plt.xlabel('Pedestrian Trajectories')
    # plt.ylabel('Coefficients of Plynomial')
    # plt.title("Fitted polynomial to dataset:{}".format(cur_dataset))
    # plt.legend()
    # # plt.show(block=True)
    # plt.savefig("./saved_figs/" + 'lstm_polyfit_'+cur_dataset+'predlen_' + str(pred_len) +'_obs'+str(obs_len)+'.png')
    return coeff
Exemplo n.º 8
0
def json_dump(args):
    # Some preparation
    torch.cuda.manual_seed(1000)
    print ('Loading data')
    if args.imgf_path ==None:
        #default bottom-up top-down
        loader = data_loader(b_size=512, train=False)
    else:
        loader = data_loader(b_size=512,image_path=args.imgf_path, train=False)
    model = Model(v_size=loader.v_size,
                  K=loader.K,
                  f_dim=loader.f_dim,
                  h_dim=512,
                  o_dim=loader.o_dim,
                  pretrained_we=loader.we_matrix)

    model = model.cuda()
    if args.savedmodel and os.path.isfile(args.savedmodel):
        print('Reading Saved model {}'.format(args.savedmodel))
        ckpt = torch.load(args.savedmodel)
        model.load_state_dict(ckpt['state_dict'])
    else:
        print('Wrong Modelpath')

    result = []
    for step in range(loader.n_batch+1):
        # Batch preparation
        q_batch, a_batch, i_batch = loader.next_batch()
        q_batch = Variable(torch.from_numpy(q_batch))
        i_batch = Variable(torch.from_numpy(i_batch))
        if step == loader.n_batch+1:
            q_batch = Variable(torch.from_numpy(q_batch))[:loader.q_num-loader.n_batch*loader.b_size]
            i_batch = Variable(torch.from_numpy(i_batch))[:loader.q_num-loader.n_batch*loader.b_size]
            a_batch = Variable(torch.from_numpy(a_batch))[:loader.q_num-loader.n_batch*loader.b_size]

        q_batch, i_batch = q_batch.cuda(), i_batch.cuda()
        output = model(q_batch, i_batch)
        _, ix = output.data.max(1)
        for i, qid in enumerate(a_batch):
            result.append({
                'question_id': (int)(qid),
                'answer': loader.a_itow[ix[i]]
            })

    outfile = open(args.savedmodel+'result.json','w')
    json.dump(result,outfile)
    print ('Validation done')
def test(gru_net, args, pred_len=0):

    test_data_dir = os.path.join(
        '/home/ashishpc/Desktop/sgan_ab/scripts/datasets/',
        cur_dataset + '/train')

    # retrieve dataloader
    dataset, dataloader = loader.data_loader(args, test_data_dir)

    # define parameters for training and testing loops
    criterion = nn.MSELoss(
    )  # MSE works best for difference between predicted and actual coordinate paths

    # initialize lists for capturing losses
    test_loss = []
    test_avgD_error = []
    test_finalD_error = []

    # now, test the model
    for i, batch in enumerate(dataloader):
        test_observed_batch = batch[0]
        test_target_batch = batch[1]
        out = gru_net(
            test_observed_batch,
            pred_len=pred_len)  # forward pass of lstm network for training
        cur_test_loss = criterion(out, test_target_batch)  # calculate MSE loss
        test_loss.append(cur_test_loss.item())
        out1 = out
        target_batch1 = test_target_batch  #making a copy of the tensors to convert them to array
        seq, peds, coords = test_target_batch.shape

        avgD_error = (np.sum(
            np.sqrt(
                np.square(out1[:, :, 0].detach().numpy() -
                          target_batch1[:, :, 0].detach().numpy()) +
                np.square(out1[:, :, 1].detach().numpy() -
                          target_batch1[:, :, 1].detach().numpy())))) / (
                              pred_len * peds)
        test_avgD_error.append(avgD_error)

        # final displacement error
        finalD_error = (np.sum(
            np.sqrt(
                np.square(out1[pred_len - 1, :, 0].detach().numpy() -
                          target_batch1[pred_len - 1, :, 0].detach().numpy()) +
                np.square(out1[pred_len - 1, :, 1].detach().numpy() -
                          target_batch1[pred_len - 1, :, 1].detach().numpy())))
                        ) / peds
        test_finalD_error.append(finalD_error)

    avg_testloss = sum(test_loss) / len(test_loss)
    avg_testD_error = sum(test_avgD_error) / len(test_avgD_error)
    avg_testfinalD_error = sum(test_finalD_error) / len(test_finalD_error)
    print("============= Average test loss:", avg_testloss,
          "====================")

    return avg_testloss, avg_testD_error, avg_testfinalD_error
Exemplo n.º 10
0
def test(vanilla_lstm_net, args, pred_len=0):
    test_data_dir = "/home/roongtaaahsih/ped_traj/sgan_ab/scripts/datasets/eth/test"

    # retrieve dataloader
    dataset, dataloader = loader.data_loader(args, test_data_dir)

    # define parameters for training and testing loops
    criterion = nn.MSELoss(
    )  # MSE works best for difference between predicted and actual coordinate paths

    # initialize lists for capturing losses
    test_loss = []
    test_avgD_error = []
    test_finalD_error = []

    # now, test the model
    for i, batch in enumerate(dataloader):
        test_observed_batch = batch[0]
        test_target_batch = batch[1]
        out = vanilla_lstm_net(
            test_observed_batch,
            pred_len=pred_len)  # forward pass of lstm network for training
        # print("vnet out's shape",out.shape)
        cur_test_loss = criterion(out, test_target_batch)  # calculate MSE loss
        test_loss.append(cur_test_loss.item())
        out1 = out
        target_batch1 = test_target_batch  #making a copy of the tensors to convert them to array
        seq, peds, coords = test_target_batch.shape  # q is number of pedestrians

        avgD_error = (np.sum(
            np.sqrt(
                np.square(out1[:, :, 0].detach().numpy() -
                          target_batch1[:, :, 0].detach().numpy()) +
                np.square(out1[:, :, 1].detach().numpy() -
                          target_batch1[:, :, 1].detach().numpy())))) / (
                              pred_len * peds)
        test_avgD_error.append(avgD_error)
        # print("current avg Disp error:",avgD_error)
        #calculating final displacement error
        finalD_error = (np.sum(
            np.sqrt(
                np.square(out1[pred_len - 1, :, 0].detach().numpy() -
                          target_batch1[pred_len - 1, :, 0].detach().numpy()) +
                np.square(out1[pred_len - 1, :, 1].detach().numpy() -
                          target_batch1[pred_len - 1, :, 1].detach().numpy())))
                        ) / peds
        test_finalD_error.append(finalD_error)

    avg_testloss = sum(test_loss) / len(test_loss)
    avg_testD_error = sum(test_avgD_error) / len(test_avgD_error)
    avg_testfinalD_error = sum(test_finalD_error) / len(test_finalD_error)
    print("============= Average test loss:", avg_testloss,
          "====================")

    return avg_testloss, avg_testD_error, avg_testfinalD_error
Exemplo n.º 11
0
def main(args):
    '''define parameters for training and testing loops!'''

    # num_epoch = 20
    # pred_len = 12
    # learning_rate = 0.001

    num_epoch = args.num_epochs
    pred_len = args.pred_len
    learning_rate = args.learning_rate

    # retrieve dataloader
    dataset, dataloader = loader.data_loader(args, data_dir)
    ''' define the network, optimizer and criterion '''
    # gru_net = GRUNet()

    criterion = nn.MSELoss(
    )  # MSE works best for difference between predicted and actual coordinate paths
    # optimizer = optim.Adam(gru_net.parameters(), lr=learning_rate)

    # # initialize lists for capturing losses/errors
    # test_loss = []
    # avg_test_loss = []
    # test_finalD_error=[]
    # test_avgD_error=[]
    # std_test_loss = []

    #calling the test function and calculating the test losses
    avg_test_loss, test_avgD_error, test_finalD_error = test(
        gru_net, args, pred_len, data_dir)

    # save results to text file
    txtfilename = os.path.join(
        "./txtfiles/",
        r"Trained_all_Results_table_lr_" + str(learning_rate) + ".txt")
    os.makedirs(os.path.dirname("./txtfiles/"),
                exist_ok=True)  # make directory if it doesn't exist
    with open(
            txtfilename, "a+"
    ) as f:  #will append to a file, create a new one if it doesn't exist
        if (pred_len == 2):  #To print the heading in the txt file
            f.write("Pred_Len" + "\t" + "Avg_Test_Loss" + "\t" +
                    "Test_AvgD_Error" + "\t" + "Test_FinalDisp_Error" + "\n")
        #f.write("\n==============Average train loss vs. epoch:===============")
        f.write(str(pred_len) + "\t")
        #f.write("\n==============avg test loss vs. epoch:===================")
        f.write(str(avg_test_loss) + "\t")
        #f.write("\n==============Avg test displacement error:===================")
        f.write(str(test_avgD_error) + "\t")
        #f.write("\n==============final test displacement error:===================")
        f.write(str(test_finalD_error) + "\n")
        f.close()
    print(
        "saved average and std of training losses to text file in: ./txtfiles")
Exemplo n.º 12
0
def main():
    """run the pipeline """
    test, train = data_loader(data_path)
    X_train, tfidf = preprocess(train, True)
    X_test, _ = preprocess(test, tfidf)
    y_train = train.author

    model = train_model(X_train, y_train)

    sub = pd.Series(model.predict(X_test))

    sub.to_csv(os.path.join(output_path, "simple_submission.csv"), index=False)
def test(vanilla_lstm_net,args,pred_len):


	test_data_dir = os.path.join('/home/roongtaaahsih/ped_trajectory_prediction/sgan_ab/scripts/datasets/', cur_dataset + '/test')

	# retrieve dataloader
	dataset, dataloader = loader.data_loader(args, test_data_dir)

	# define parameters for training and testing loops
	criterion = nn.MSELoss() # MSE works best for difference between predicted and actual coordinate paths

	# initialize lists for capturing losses
	test_loss = []
	test_avgD_error=[]
	test_finalD_error=[]

	# now, test the model
	for i, batch in enumerate(dataloader):
		test_observed_batch = batch[0]
		test_target_batch = batch[1]
		# out = vanilla_lstm_net(test_observed_batch, pred_len=pred_len) # forward pass of vanilla_lstm network for training
		seq, peds, coords = test_observed_batch.shape # q is number of pedestrians 
		output_seq=[]
		ht = torch.zeros(test_observed_batch.size(1), vanilla_lstm_net.rnn_size, dtype=torch.float)
		ct = torch.zeros(test_observed_batch.size(1), vanilla_lstm_net.rnn_size, dtype=torch.float)
		for step in range(seq):
			out,ht,ct = vanilla_lstm_net(test_observed_batch[step,:,:],ht,ct) # forward pass of vanilla_lstm network for training
		# print("out's shape:", out.shape)
		for step in range(pred_len):
			out,ht,ct=vanilla_lstm_net(out,ht,ct)
			output_seq+=[out]
		output_seq = torch.stack(output_seq).squeeze() # convert list to tensor
		cur_test_loss = criterion(output_seq, test_target_batch) # calculate MSE loss
		test_loss.append(cur_test_loss.item())
		out1=output_seq
		target_batch1=test_target_batch  #making a copy of the tensors to convert them to array
		seq, peds, coords = test_target_batch.shape
		avgD_error=(np.sum(np.sqrt(np.square(out1[:,:,0].detach().numpy()-target_batch1[:,:,0].detach().numpy())+
            np.square(out1[:,:,1].detach().numpy()-target_batch1[:,:,1].detach().numpy()))))/(pred_len*peds)
		test_avgD_error.append(avgD_error)

		# final displacement error
		finalD_error=(np.sum(np.sqrt(np.square(out1[pred_len-1,:,0].detach().numpy()-target_batch1[pred_len-1,:,0].detach().numpy())+
            np.square(out1[pred_len-1,:,1].detach().numpy()-target_batch1[pred_len-1,:,1].detach().numpy()))))/peds
		test_finalD_error.append(finalD_error)
	avg_testloss = sum(test_loss)/len(test_loss)
	avg_testD_error=sum(test_avgD_error)/len(test_avgD_error)
	avg_testfinalD_error=sum(test_finalD_error)/len(test_finalD_error)
	print("============= Average test loss:", avg_testloss, "====================")

	return avg_testloss, avg_testD_error,avg_testfinalD_error
Exemplo n.º 14
0
def test(lstm_net,args,pred_len,data_dir):

    test_data_dir = data_dir #os.path.join('/home/ashishpc/Desktop/sgan_ab/scripts/datasets/', cur_dataset + '/train')

    # retrieve dataloader
    dataset, dataloader = loader.data_loader(args, test_data_dir)

    # define parameters for training and testing loops
    criterion = nn.MSELoss() # MSE works best for difference between predicted and actual coordinate paths

    # initialize lists for capturing losses
    test_loss = []
    test_avgD_error=[]
    test_finalD_error=[]
    # obs=[]
    # predict=[]
    # ground_truth=[]
    plt.figure(figsize=(32,20))
    plt.xlabel("X coordinates of pedestrians")
    plt.ylabel("Y coordinates of pedestrians")
    # now, test the model
    for i, batch in enumerate(dataloader):
        test_observed_batch = batch[0]
        test_target_batch = batch[1]
        out = lstm_net(test_observed_batch, pred_len=pred_len) # forward pass of lstm network for training
        # cur_test_loss = criterion(out, test_target_batch) # calculate MSE loss
        # test_loss.append(cur_test_loss.item())
        s,peds,c=out.shape
        out1=out.detach().numpy()
        target1=test_target_batch.detach().numpy()
        observed1=test_observed_batch.detach().numpy()
        print("observed 1 shape:",observed1.shape)
        print("target1 shape:", target1.shape)
        print("out 1 shape", out1.shape)
        out2=np.vstack((observed1,out1))
        target2=np.vstack((observed1,target1))
        print("out2 shape",out2.shape)
        for t in range(6):
            plt.plot(observed1[:,t,0],observed1[:,t,1],color='b',marker='o',linewidth=5,markersize=12)
            plt.plot(target2[s-1:s+pred_len,t,0],target2[s-1:s+pred_len,t,1],color='red',marker='o',linewidth=5,markersize=12)
            plt.plot(out2[s-1:s+pred_len,t,0],out2[s-1:s+pred_len,t,1],color='g',marker='o',linewidth=5,markersize=12)
        plt.legend(["Observed","Ground Truth","Predicted"])
        plt.show(block=True)
    

        # out1=out
        # target_batch1=test_target_batch  #making a copy of the tensors to convert them to array
        # seq, peds, coords = test_target_batch.shape

    return test_observed_batch,test_target_batch,out
Exemplo n.º 15
0
def main(args):
    if os.path.isdir(args.model_path):
        filenames = os.listdir(args.model_path)
        filenames.sort()
        paths = [os.path.join(args.model_path, file_) for file_ in filenames]
    else:
        paths = [args.model_path]

    for path in paths:
        checkpoint = torch.load(path)
        generator = get_generator(checkpoint)
        _args = AttrDict(checkpoint['args'])
        path = get_dset_path(_args.dataset_name, args.dset_type)
        _, loader = data_loader(_args, path)
        ade, fde = evaluate(_args, loader, generator, args.num_samples)
        print('Dataset: {}, Pred Len: {}, ADE: {:.2f}, FDE: {:.2f}'.format(
            _args.dataset_name, _args.pred_len, ade, fde))
Exemplo n.º 16
0
def main(args):
    if os.path.isdir(args.model_path):
        filenames = os.listdir(args.model_path)
        filenames.sort()
        paths = [os.path.join(args.model_path, file_) for file_ in filenames]
    else:
        paths = [args.model_path]

    accuracy = []
    confusionmatrix = []

    for ind, path in enumerate(paths):
        checkpoint = torch.load(path)
        model = get_model(checkpoint)
        _args = AttrDict(checkpoint['args'])
        _args.batch_size = 40
        data_dset, loader = data_loader(_args, args.sample_path)
        with torch.no_grad():
            for batch in loader:
                (groupInput, target) = batch
                groupInput = groupInput.to(device)
                target = target.to(device)
                TAtt, BAtt, groupAttention, groupOutput = model(
                    groupInput)  # TAtt: 40X4X37X60, BAtt:40X4X37
                # frame=2
                # agent=3
                # timelist=[90,180,360]

                # bodyweights=BAtt[frame,agent,:]
                # bodyweights=bodyweights.cpu().numpy()
                # vissingle(ind,agent, bodyweights, timelist)

                data = TAtt[frame, agent, :, :]
                data = data.cpu().numpy()
                ax = sb.heatmap(data,
                                vmin=0,
                                vmax=0.022,
                                cmap=sb.cubehelix_palette(n_colors=100,
                                                          light=.95,
                                                          dark=.08))
                # vissingle(ind,agent)
                plt.savefig('temporal/model_' + str(ind) + 'frame_' +
                            str(frame) + '__agentID__' + str(agent) + '.png')
                plt.close()
Exemplo n.º 17
0
    def run_episode(self, train_mode):
        if train_mode:
            way = self.cfg['way']
            shot = self.cfg['shot']
            query_way = self.cfg['query_way']
            query_shot = self.cfg['query_shot']
            image_size = self.cfg['images_size']
            image_path = self.cfg['images_path']

        else:
            way = self.cfg['val_way']
            shot = self.cfg['val_shot']
            query_way = self.cfg['val_query_way']
            query_shot = self.cfg['val_query_shot']
            image_size = self.cfg['images_size']
            image_path = self.cfg['val_images_path']

        loader = data_loader(way, shot, query_way, query_shot, image_size,
                             image_path)
        for data_batch in loader:
            self.episode_processor(data_batch, way, shot, query_way,
                                   query_shot)
Exemplo n.º 18
0
def main(args):
    os.environ["CUDA_VISIBLE_DEVICES"] = "0,1"
    device_ids = [0, 1]
    test_path = get_dset_path(args.dataset_name, 'test')

    logger.info("Initializing test dataset")
    test_dset, test_loader = data_loader(args, test_path)

    net = LSTM_model(args)
    #net = net.cuda(device_ids[1])

    checkpoint_path = "./model/lstm348.tar"
    checkpoint = torch.load(checkpoint_path)
    net.load_state_dict(checkpoint['state_dict'])
    net.eval()

    count = 0
    total_ade = 0
    total_fde = 0
    for batch in test_loader:
        (obs_traj, pred_traj_gt, obs_traj_rel, pred_traj_gt_rel, non_linear_ped,
         loss_mask, seq_start_end) = batch
        num_ped = obs_traj.size(1)   # (8 n 2)
        #pred_traj_gt = pred_traj_gt.cuda(device_ids[1])
        pred_traj = net(obs_traj, num_ped, pred_traj_gt, seq_start_end)
        ade = get_mean_error(pred_traj, pred_traj_gt)
        total_ade += ade
        fde = final_displacement_error(pred_traj[-1], pred_traj_gt[-1])
        total_fde += (fde / num_ped)
        #logger.info("ade is {:.2f}".format(ade))
        count += 1

    ade_fin = total_ade / count
    fde_fin = total_fde / count
    logger.info("ade is {:.2f}".format(ade_fin))
    logger.info("fde is {:.2f}".format(fde_fin))
Exemplo n.º 19
0
def main(args):
    os.environ["CUDA_VISIBLE_DEVICES"] = args.gpu_num
    train_path = get_dset_path(args.dataset_name, 'train')
    val_path = get_dset_path(args.dataset_name, 'val')

    long_dtype, float_dtype = get_dtypes(args)

    logger.info("Initializing train dataset")
    train_dset, train_loader = data_loader(args, train_path)
    logger.info("Initializing val dataset")
    _, val_loader = data_loader(args, val_path)

    iterations_per_epoch = len(train_dset) / args.batch_size / args.d_steps
    if args.num_epochs:
        args.num_iterations = int(iterations_per_epoch * args.num_epochs)

    logger.info(
        'There are {} iterations per epoch'.format(iterations_per_epoch))

    generator = TrajectoryGenerator(
        obs_len=args.obs_len,
        pred_len=args.pred_len,
        embedding_dim=args.embedding_dim,
        encoder_h_dim=args.encoder_h_dim_g,
        decoder_h_dim=args.decoder_h_dim_g,
        mlp_dim=args.mlp_dim,
        num_layers=args.num_layers,
        noise_dim=args.noise_dim,
        noise_type=args.noise_type,
        noise_mix_type=args.noise_mix_type,
        pooling_type=args.pooling_type,
        pool_every_timestep=args.pool_every_timestep,
        dropout=args.dropout,
        bottleneck_dim=args.bottleneck_dim,
        neighborhood_size=args.neighborhood_size,
        grid_size=args.grid_size,
        batch_norm=args.batch_norm)

    generator.apply(init_weights)
    generator.type(float_dtype).train()
    logger.info('Here is the generator:')
    logger.info(generator)

    # discriminator = TrajectoryDiscriminator(
    #     obs_len=args.obs_len,
    #     pred_len=args.pred_len,
    #     embedding_dim=args.embedding_dim,
    #     h_dim=args.encoder_h_dim_d,
    #     mlp_dim=args.mlp_dim,
    #     num_layers=args.num_layers,
    #     dropout=args.dropout,
    #     batch_norm=args.batch_norm,
    #     d_type=args.d_type)

    # discriminator.apply(init_weights)
    # discriminator.type(float_dtype).train()
    logger.info('Here is the discriminator:')
    # logger.info(discriminator)

    g_loss_fn = gan_g_loss
    d_loss_fn = gan_d_loss

    optimizer_g = optim.Adam(generator.parameters(), lr=args.g_learning_rate)
    # optimizer_d = optim.Adam(
    # discriminator.parameters(), lr=args.d_learning_rate
    # )

    # Maybe restore from checkpoint
    restore_path = None
    if args.checkpoint_start_from is not None:
        restore_path = args.checkpoint_start_from
    elif args.restore_from_checkpoint == 1:
        restore_path = os.path.join(args.output_dir,
                                    '%s_with_model.pt' % args.checkpoint_name)

    if restore_path is not None and os.path.isfile(restore_path):
        logger.info('Restoring from checkpoint {}'.format(restore_path))
        checkpoint = torch.load(restore_path)
        generator.load_state_dict(checkpoint['g_state'])
        # discriminator.load_state_dict(checkpoint['d_state'])
        optimizer_g.load_state_dict(checkpoint['g_optim_state'])
        # optimizer_d.load_state_dict(checkpoint['d_optim_state'])
        t = checkpoint['counters']['t']
        epoch = checkpoint['counters']['epoch']
        checkpoint['restore_ts'].append(t)
    else:
        # Starting from scratch, so initialize checkpoint data structure
        t, epoch = 0, 0
        checkpoint = {
            'args': args.__dict__,
            'G_losses': defaultdict(list),
            'D_losses': defaultdict(list),
            'losses_ts': [],
            'metrics_val': defaultdict(list),
            'metrics_train': defaultdict(list),
            'sample_ts': [],
            'restore_ts': [],
            'norm_g': [],
            'norm_d': [],
            'counters': {
                't': None,
                'epoch': None,
            },
            'g_state': None,
            'g_optim_state': None,
            'd_state': None,
            'd_optim_state': None,
            'g_best_state': None,
            'd_best_state': None,
            'best_t': None,
            'g_best_nl_state': None,
            'd_best_state_nl': None,
            'best_t_nl': None,
        }
    t0 = None
    while t < args.num_iterations:
        gc.collect()
        d_steps_left = args.d_steps
        g_steps_left = args.g_steps
        epoch += 1
        logger.info('Starting epoch {}'.format(epoch))
        for batch in train_loader:
            # if args.timing == 1:
            #     torch.cuda.synchronize()
            #     t1 = time.time()

            # Decide whether to use the batch for stepping on discriminator or
            # generator; an iteration consists of args.d_steps steps on the
            # discriminator followed by args.g_steps steps on the generator.
            # if d_steps_left > 0:
            #     step_type = 'd'
            # losses_d = discriminator_step(args, batch, generator,
            #                               discriminator, d_loss_fn,
            #                               optimizer_d)
            # checkpoint['norm_d'].append(
            #     get_total_norm(discriminator.parameters()))
            # d_steps_left -= 1
            # elif g_steps_left > 0:
            step_type = 'g'
            losses_g = generator_step(args, batch, generator, optimizer_g)
            checkpoint['norm_g'].append(get_total_norm(generator.parameters()))
            g_steps_left -= 1

            # if args.timing == 1:
            #     torch.cuda.synchronize()
            #     t2 = time.time()
            #     logger.info('{} step took {}'.format(step_type, t2 - t1))

            # Skip the rest if we are not at the end of an iteration
            # if d_steps_left > 0 or g_steps_left > 0:
            #     continue

            # if args.timing == 1:
            #     if t0 is not None:
            #         logger.info('Interation {} took {}'.format(
            #             t - 1, time.time() - t0
            #         ))
            #     t0 = time.time()

            # Maybe save loss
            if t % args.print_every == 0:
                print(
                    "ARSAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
                )
                logger.info('t = {} / {}'.format(t + 1, args.num_iterations))
                # for k, v in sorted(losses_d.items()):
                #     logger.info('  [D] {}: {:.3f}'.format(k, v))
                #     checkpoint['D_losses'][k].append(v)
                for k, v in sorted(losses_g.items()):
                    print(k)
                    print(v)
                    logger.info('  [G] {}: {:.3f}'.format(k, v))
                    checkpoint['G_losses'][k].append(v)
                checkpoint['losses_ts'].append(t)

            # Maybe save a checkpoint
            # if t > 0 and t % args.checkpoint_every == 0:
            if t > 0:

                checkpoint['counters']['t'] = t
                checkpoint['counters']['epoch'] = epoch
                checkpoint['sample_ts'].append(t)

                # Check stats on the validation set
                logger.info('Checking stats on val ...')
                metrics_val = check_accuracy(args, val_loader, generator)
                logger.info('Checking stats on train ...')
                metrics_train = check_accuracy(
                    args,
                    train_loader,
                    generator,
                    # d_loss_fn,
                    limit=True)

                for k, v in sorted(metrics_val.items()):
                    logger.info('  [val] {}: {:.3f}'.format(k, v))
                    checkpoint['metrics_val'][k].append(v)
                for k, v in sorted(metrics_train.items()):
                    logger.info('  [train] {}: {:.3f}'.format(k, v))
                    checkpoint['metrics_train'][k].append(v)

                min_ade = min(checkpoint['metrics_val']['ade'])
                min_ade_nl = min(checkpoint['metrics_val']['ade_nl'])

                if metrics_val['ade'] == min_ade:
                    logger.info('New low for avg_disp_error')
                    checkpoint['best_t'] = t
                    checkpoint['g_best_state'] = generator.state_dict()
                    # checkpoint['d_best_state'] = discriminator.state_dict()

                if metrics_val['ade_nl'] == min_ade_nl:
                    logger.info('New low for avg_disp_error_nl')
                    checkpoint['best_t_nl'] = t
                    checkpoint['g_best_nl_state'] = generator.state_dict()
                    # checkpoint['d_best_nl_state'] = discriminator.state_dict()

                # Save another checkpoint with model weights and
                # optimizer state
                checkpoint['g_state'] = generator.state_dict()
                checkpoint['g_optim_state'] = optimizer_g.state_dict()
                # checkpoint['d_state'] = discriminator.state_dict()
                # checkpoint['d_optim_state'] = optimizer_d.state_dict()
                checkpoint_path = os.path.join(
                    args.output_dir, '%s_with_model.pt' % args.checkpoint_name)
                logger.info('Saving checkpoint to {}'.format(checkpoint_path))
                torch.save(checkpoint, checkpoint_path)
                logger.info('Done.')

                # Save a checkpoint with no model weights by making a shallow
                # copy of the checkpoint excluding some items
                checkpoint_path = os.path.join(
                    args.output_dir, '%s_no_model.pt' % args.checkpoint_name)
                logger.info('Saving checkpoint to {}'.format(checkpoint_path))
                key_blacklist = [
                    'g_state', 'd_state', 'g_best_state', 'g_best_nl_state',
                    'g_optim_state', 'd_optim_state', 'd_best_state',
                    'd_best_nl_state'
                ]
                small_checkpoint = {}
                for k, v in checkpoint.items():
                    if k not in key_blacklist:
                        small_checkpoint[k] = v
                torch.save(small_checkpoint, checkpoint_path)
                logger.info('Done.')

            t += 1
            # d_steps_left = args.d_steps
            g_steps_left = args.g_steps
            if t >= args.num_iterations:
                break
Exemplo n.º 20
0
def main(args):
    os.environ["CUDA_VISIBLE_DEVICES"] = args.gpu_num

    # train_path = get_dset_path(args.dataset_name, 'train')
    # val_path = get_dset_path(args.dataset_name, 'val')
    data_path = get_dset_path(args.dataset_name)

    long_dtype, float_dtype = get_dtypes(args)

    logger.info("Initializing dataset")
    data_dset, data_load = data_loader(args, data_path)
    # logger.info("Initializing val dataset")
    # _, val_loader = data_loader(args, val_path)

    logger.info('{} data trials are loaded'.format(len(data_dset)))

    iterations_per_epoch = len(data_dset) / args.batch_size
    if args.num_epochs:
        args.num_iterations = int(iterations_per_epoch * args.num_epochs)

    logger.info(
        'There are {} iterations per epoch'.format(iterations_per_epoch))

    model = GroupEncoder(embedding_dim=args.embedding_dim,
                         seq_len=args.seq_len,
                         n_head=args.n_head,
                         d_k=args.d_k,
                         d_v=args.d_v,
                         d_inner=args.d_inner,
                         h_dim=args.h_dim,
                         num_layers=args.num_layers,
                         dropout=args.dropout,
                         num_markers=37)

    cv = KFold(n_splits=5, random_state=42, shuffle=False)
    foldcounter = 0
    accuracy10fold = []
    for ifold, (train_index, valid_index) in enumerate(cv.split(data_dset)):
        train = torch.utils.data.Subset(data_dset, train_index)
        valid = torch.utils.data.Subset(data_dset, valid_index)
        train_loader = torch.utils.data.DataLoader(
            train,
            batch_size=args.batch_size,
            shuffle=True,
            num_workers=args.loader_num_workers)
        vaild_loader = torch.utils.data.DataLoader(
            valid,
            batch_size=args.batch_size,
            shuffle=True,
            num_workers=args.loader_num_workers)

        model.apply(init_weights)
        model.type(float_dtype).train()
        logger.info('The {}th fold test'.format(foldcounter))
        foldcounter += 1
        logger.info('Here is the model:')
        logger.info(model)

        model_loss_fn = model_loss
        optimizer = optim.Adam(model.parameters(),
                               lr=args.learning_rate,
                               weight_decay=args.weight_decay)
        scheduler = optim.lr_scheduler.MultiStepLR(optimizer,
                                                   milestones=args.steps,
                                                   gamma=0.1)
        model = model.to(args.device_ids[0])

        if len(args.device_ids) > 1:
            if torch.cuda.device_count() >= len(args.device_ids):
                model = nn.DataParallel(model, device_ids=args.device_ids)
            else:
                raise ValueError("the machine don't have {} gpus".format(
                    str(len(args.device_ids))))

        # Maybe restore from checkpoint
        restore_path = None
        if args.checkpoint_start_from is not None:
            restore_path = args.checkpoint_start_from
        elif args.restore_from_checkpoint == 1:
            restore_path = os.path.join(
                args.output_dir,
                '%s_%s_with_model.pt' % (args.checkpoint_name, str(ifold)))

        if restore_path is not None and os.path.isfile(restore_path):
            logger.info('Restoring from checkpoint {}'.format(restore_path))
            checkpoint = torch.load(restore_path)
            model.load_state_dict(checkpoint['state'])
            optimizer.load_state_dict(checkpoint['optim_state'])
            t = checkpoint['counters']['t']
            epoch = checkpoint['counters']['epoch']
            checkpoint['restore_ts'].append(t)

        else:
            # Starting from scratch, so initialize checkpoint data structure
            t, epoch = 0, 0
            checkpoint = {
                'args': args.__dict__,
                'losses': [],
                'losses_ts': [],
                'metrics_val': defaultdict(list),
                'metrics_train': defaultdict(list),
                'sample_ts': [],
                'restore_ts': [],
                'norm': [],
                'accuracy': [],
                'confusionmatrix': [],
                'counters': {
                    't': None,
                    'epoch': None,
                },
                'state': None,
                'optim_state': None,
                'best_state': None,
                'best_accuracy': None,
                'd_best_state': None,
                'best_t': None,
            }

        t0 = None
        best_accuracy = 0
        while t < args.num_iterations:
            gc.collect()
            epoch += 1
            logger.info('Starting {}th fold epoch {}'.format(
                foldcounter, epoch))
            for batch in train_loader:
                if args.timing == 1:
                    torch.cuda.synchronize()
                    t1 = time.time()

                losses = {}
                # print(batch)
                # batch = [tensor.cuda() for tensor in batch]
                (groupInput, target) = batch
                groupInput = groupInput.to(device)
                target = target.to(device)
                _, _, _, groupOutput = model(groupInput)
                loss = model_loss_fn(groupOutput, target)
                # loss=nn.BCELoss(groupOutput,target)
                logger.info('loss is {}'.format(loss))
                losses['loss'] = loss
                optimizer.zero_grad()
                loss.backward()
                # if args.clipping_threshold > 0:
                #     nn.utils.clip_grad_norm_(
                #         model.parameters(), args.clipping_threshold
                #     )
                optimizer.step()
                checkpoint['norm'].append(get_total_norm(model.parameters()))

                if args.timing == 1:
                    torch.cuda.synchronize()
                    t2 = time.time()
                    logger.info('it took {}'.format(t2 - t1))

                if args.timing == 1:
                    if t0 is not None:
                        logger.info('Interation {} took {}'.format(
                            t - 1,
                            time.time() - t0))
                    t0 = time.time()

                # Maybe save loss
                if t % args.print_every == 0:
                    logger.info('t = {} / {}'.format(t + 1,
                                                     args.num_iterations))
                    # for k, v in sorted(losses.items()):
                    #     logger.info('  [Loss] {}: {:.3f}'.format(k, v))
                    checkpoint['losses'].append(loss)
                    checkpoint['losses_ts'].append(t)

                # Maybe save a checkpoint
                if t > 0 and t % args.checkpoint_every == 0:
                    checkpoint['counters']['t'] = t
                    checkpoint['counters']['epoch'] = epoch
                    checkpoint['sample_ts'].append(t)

                    # Check stats on the validation set
                    logger.info('Checking stats on val ...')
                    metrics_val, accuracy, confumatrix = check_accuracy(
                        args, vaild_loader, model, model_loss_fn)
                    checkpoint['accuracy'].append(accuracy)
                    logger.info('  [val] accuracy: {:.3f}'.format(accuracy))
                    logger.info(
                        '  [val] confusion matrix: {}'.format(confumatrix))
                    for k, v in sorted(metrics_val.items()):
                        logger.info('  [val] {}: {:.3f}'.format(k, v))
                        checkpoint['metrics_val'][k].append(v)

                    min_loss = min(checkpoint['metrics_val']['loss'])

                    if metrics_val['loss'] == min_loss:
                        logger.info('New low for avg_error')
                        checkpoint['best_t'] = t
                        checkpoint['best_state'] = model.state_dict()
                        checkpoint['best_accuracy'] = accuracy
                        checkpoint['confusionmatrix'] = confumatrix
                        best_accuracy = accuracy

                    # Save another checkpoint with model weights and
                    # optimizer state
                    checkpoint['state'] = model.state_dict()
                    checkpoint['optim_state'] = optimizer.state_dict()
                    checkpoint_path = os.path.join(
                        args.output_dir, '%s_%s_with_model.pt' %
                        (args.checkpoint_name, str(ifold)))
                    logger.info(
                        'Saving checkpoint to {}'.format(checkpoint_path))
                    torch.save(checkpoint, checkpoint_path)
                    logger.info('Done.')

                t += 1
                if t >= args.num_iterations:
                    break
            scheduler.step()
        accuracy10fold.append(best_accuracy)
    # scores = cross_val_score(model, X_data, y_data, cv=10, scoring="accuracy")
    logger.info('accuracy10fold'.format(accuracy10fold))
Exemplo n.º 21
0
datapath = '/media/einhard/Seagate Expansion Drive/3380_data/data/'

print('Loading USE...')
embed = hub.load(
    '/media/einhard/Seagate Expansion Drive/3380_data/tensorflow_hub/universal-sentence-encoder_4'
)
print('Success!')

print('Loading word embeddings')
sentence_array = joblib.load(
    '/media/einhard/Seagate Expansion Drive/3380_data/3380-Book-Recommendations/Models/Summarizer/reviewEmbeddings.pkl'
)

print('Loading reviews and books')
books, reviews = data_loader(datapath, 'filtered_books.csv',
                             'filtered_reviews.csv')


def find_reviews(query, reviews=reviews, n_results=10):
    # Create vector from query and compare with global embedding
    sentence = [query]
    sentence_vector = np.array(embed(sentence))
    inner_product = np.inner(sentence_vector, sentence_array)[0]

    # Find sentences with highest inner products
    top_n_sentences = pd.Series(inner_product).nlargest(n_results + 1)
    top_n_indices = top_n_sentences.index.tolist()
    top_n_list = list(reviews.review_text.iloc[top_n_indices][1:])

    print(f'Input sentence: "{query}"\n')
    print(f'{n_results} most semantically similar reviews: \n\n')
Exemplo n.º 22
0
def main(args):
    train_path = get_dset_path(args.dataset_name, 'train')
    val_path = get_dset_path(args.dataset_name, 'val')

    # 随机种子
    # torch.manual_seed(2)
    # np.random.seed(2)
    # if args.use_gpu:
    #     torch.cuda.manual_seed_all(2)

    logger.info("Initializing train dataset")
    train_dset, train_loader = data_loader(args, train_path)
    logger.info("Initializing val dataset")
    _, val_loader = data_loader(args, val_path)

    log_path = './log/'
    log_file_curve = open(os.path.join(log_path, 'log_loss.txt'), 'w+')
    log_file_curve_val = open(os.path.join(log_path, 'log_loss_val.txt'), 'w+')
    log_file_curve_val_ade = open(
        os.path.join(log_path, 'log_loss_val_ade.txt'), 'w+')

    net = LSTM_model(args)
    if args.use_gpu:
        net = net.cuda()

    optimizer = torch.optim.Adam(net.parameters(), lr=args.learning_rate)
    #scheduler = torch.optim.lr_scheduler.ExponentialLR(optimizer, gamma=0.98)

    #接着上次训练的地方继续训练
    # restore_path = '.\model\lstm294.tar'
    # logger.info('Restoring from checkpoint {}'.format(restore_path))
    # checkpoint = torch.load(restore_path)
    # net.load_state_dict(checkpoint['state_dict'])
    # optimizer.load_state_dict(checkpoint['optimizer_state_dict'])
    #
    # for i_epoch in range(checkpoint['epoch']+1):
    #     if (i_epoch + 1) % 100 == 0:
    #         args.learning_rate *= 0.98

    epoch_loss_min = 160
    epoch_smallest = 0
    #for epoch in range(checkpoint['epoch']+1, args.num_epochs):
    for epoch in range(args.num_epochs):
        count = 0
        batch_loss = 0

        for batch in train_loader:
            # Zero out gradients
            net.zero_grad()
            optimizer.zero_grad()

            (obs_traj, pred_traj_gt, obs_traj_rel, pred_traj_gt_rel,
             non_linear_ped, loss_mask, seq_start_end) = batch
            num_ped = obs_traj.size(1)
            pred_traj_gt = pred_traj_gt

            #model_teacher.py
            pred_traj = net(obs_traj, num_ped, pred_traj_gt, seq_start_end)
            loss = displacement_error(pred_traj, pred_traj_gt)
            #loss = get_mean_error(pred_traj, pred_traj_gt)

            # Compute gradients
            loss.backward()
            # Clip gradients
            torch.nn.utils.clip_grad_norm_(net.parameters(), args.grad_clip)
            # Update parameters
            optimizer.step()

            batch_loss += loss
            count += 1

            #print(loss / num_ped)
        if (epoch + 1) % 6 == 0:
            pass
            #scheduler.step()
        logger.info('epoch {} train loss is {}'.format(epoch,
                                                       batch_loss / count))
        log_file_curve.write(str(batch_loss.item() / count) + "\n")

        batch_loss = 0
        val_ade = 0
        total_ade = 0
        for idx, batch in enumerate(val_loader):
            (obs_traj, pred_traj_gt, obs_traj_rel, pred_traj_gt_rel,
             non_linear_ped, loss_mask, seq_start_end) = batch
            num_ped = obs_traj.size(1)
            pred_traj_gt = pred_traj_gt

            # model_teacher.py
            pred_traj = net(obs_traj, num_ped, pred_traj_gt, seq_start_end)
            loss = displacement_error(pred_traj, pred_traj_gt)

            batch_loss += loss
            val_ade += loss / (num_ped * 12)
            total_ade += val_ade

            count += 1

        fin_ade = total_ade / (idx + 1)
        log_file_curve_val_ade.write(str(fin_ade.item()) + "\n")

        epoch_loss = batch_loss / count
        if epoch_loss_min > epoch_loss:
            epoch_loss_min = epoch_loss
            epoch_smallest = epoch

            logger.info('Saving model')
            torch.save(
                {
                    'epoch': epoch,
                    'state_dict': net.state_dict(),
                    'optimizer_state_dict': optimizer.state_dict()
                }, checkpoint_path(epoch))
        logger.info('epoch {} val loss is {}'.format(epoch, epoch_loss))
        log_file_curve_val.write(str(epoch_loss.item()) + "\n")
        logger.info('epoch {} is smallest loss is {}'.format(
            epoch_smallest, epoch_loss_min))
        logger.info('the smallest ade is {}'.format(total_ade / (idx + 1)))
        logger.info("-" * 50)
import pandas as pd
import numpy as np
import os
from loader import data_read, data_loader
from models import xgb_model

if __name__ == '__main__':
    print('yo! here we go')
    data = data_read()
    print('data columns', data.df.columns)
    print('data head', data.df.head(2))
    loader = data_loader(data.train, data.df)
    model = xgb_model(loader.xg_train_s, loader.xg_val, loader.xg_test,
                      loader.xg_train, loader.target_train_s,
                      loader.target_val, loader.target_train, loader.Unique_ID)
    submit = model.submit.append(data.test_manual, ignore_index=True)
    submit.to_csv("submit.csv", index=False)
def train(vanilla_lstm_net,args):

	num_epoch = args.num_epochs
	pred_len = args.pred_len
	learning_rate = args.learning_rate
    
    # retrieve dataloader
	dataset, dataloader = loader.data_loader(args, data_dir)

	''' define the network, optimizer and criterion '''
	name="estep_1Opt" # to add to the name of files
	# if (cur_dataset == "eth"):
	# vanilla_lstm_net = VanillaLSTMNet()

	criterion = nn.MSELoss() # MSE works best for difference between predicted and actual coordinate paths
	optimizer = optim.Adam(vanilla_lstm_net.parameters(), lr=learning_rate)

	# initialize lists for capturing losses/errors
	train_loss = []
	test_loss = []
	avg_train_loss = []
	avg_test_loss = []
	train_avgD_error=[]
	train_finalD_error=[]
	avg_train_avgD_error=[]
	avg_train_finalD_error=[]
	test_finalD_error=[]
	test_avgD_error=[]
	std_train_loss = []
	std_test_loss = []

	for i in range(num_epoch):
		print('======================= Epoch: {cur_epoch} / {total_epochs} =======================\n'.format(cur_epoch=i, total_epochs=num_epoch))
		for i, batch in enumerate(dataloader):
			train_batch = batch[0]
			target_batch = batch[1]
			# print("train_batch's shape", train_batch.shape)
			# print("target_batch's shape", target_batch.shape)
			seq, peds, coords = train_batch.shape # q is number of pedestrians 
			output_seq=[]
			ht = torch.zeros(train_batch.size(1), vanilla_lstm_net.rnn_size, dtype=torch.float)
			ct = torch.zeros(train_batch.size(1), vanilla_lstm_net.rnn_size, dtype=torch.float)
			for step in range(seq):
				out,ht,ct = vanilla_lstm_net(train_batch[step,:,:],ht,ct) # forward pass of vanilla_lstm network for training
			# print("out's shape:", out.shape)
			for step in range(pred_len):
				out,ht,ct=vanilla_lstm_net(out,ht,ct)
				output_seq+=[out]
				optimizer.zero_grad()
				step_loss=criterion(out,target_batch[step,:,:])
				step_loss.backward(retain_graph=True) #backward prop
				optimizer.step() #updating weights after each step prediction

        
			optimizer.zero_grad() # zero out gradients
			# print("output: {}".format(output_seq))
			output_seq = torch.stack(output_seq).squeeze() # convert list to tensor
			cur_train_loss = criterion(output_seq, target_batch) # calculate MSE loss
			# print('Current training loss: {}'.format(cur_train_loss.item())) # print current training loss
			# print('Current training loss: {}'.format(cur_train_loss.item())) # print current training loss

			#calculating average deisplacement error
			out1=output_seq
			target_batch1=target_batch  #making a copy of the tensors to convert them to array
			avgD_error=(np.sum(np.sqrt(np.square(out1[:,:,0].detach().numpy()-target_batch1[:,:,0].detach().numpy())+
			    np.square(out1[:,:,1].detach().numpy()-target_batch1[:,:,1].detach().numpy()))))/(pred_len*peds)
			train_avgD_error.append(avgD_error)

			#calculate final displacement error
			finalD_error=(np.sum(np.sqrt(np.square(out1[pred_len-1,:,0].detach().numpy()-target_batch1[pred_len-1,:,0].detach().numpy())+
			    np.square(out1[pred_len-1,:,1].detach().numpy()-target_batch1[pred_len-1,:,1].detach().numpy()))))/peds
			train_finalD_error.append(finalD_error)

			train_loss.append(cur_train_loss.item())
			cur_train_loss.backward() # backward prop
			# optimizer.step() # step like a mini-batch (after all pedestrians)
		# optimizer.step() # update weights

        # save model at every epoch (uncomment) 
        # torch.save(vanilla_lstm_net, './saved_models/vanilla_lstm_model_v3.pt')
        # print("Saved vanilla_lstm_net!")
		avg_train_loss.append(np.sum(train_loss)/len(train_loss))
		avg_train_avgD_error.append(np.sum(train_avgD_error)/len(train_avgD_error))
		avg_train_finalD_error.append(np.sum(train_finalD_error)/len(train_finalD_error))   
		std_train_loss.append(np.std(np.asarray(train_loss)))
		train_loss = [] # empty train loss

		print("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$")
		print("average train loss: {}".format(avg_train_loss[-1]))
		print("average std loss: {}".format(std_train_loss[-1]))
		avgTestLoss,avgD_test,finalD_test=test(vanilla_lstm_net,args,pred_len)
		avg_test_loss.append(avgTestLoss)
		test_finalD_error.append(finalD_test)
		test_avgD_error.append(avgD_test)
		print("test finalD error: ",finalD_test)
		print("test avgD error: ",avgD_test)
		#avg_test_loss.append(test(vanilla_lstm_net,args,pred_len)) ##calliing test function to return avg test loss at each epoch


    # '''after running through epochs, save your model and visualize.
    #    then, write your average losses and standard deviations of 
    #    losses to a text file for record keeping.'''

	save_path = os.path.join('./saved_models/', 'vanilla_lstm_model_'+name+'_lr_' + str(learning_rate) + '_epoch_' + str(num_epoch) + '_predlen_' + str(pred_len) + '.pt')
	# torch.save(vanilla_lstm_net, './saved_models/vanilla_lstm_model_lr001_ep20.pt')
	torch.save(vanilla_lstm_net, save_path)
	print("saved vanilla_lstm_net! location: " + save_path)

	''' visualize losses vs. epoch'''
	plt.figure() # new figure
	plt.title("Average train loss vs {} epochs".format(num_epoch))
	plt.plot(avg_train_loss,label='avg train_loss') 
	plt.plot(avg_test_loss,color='red',label='avg test_loss')
	plt.legend()
	plt.savefig("./saved_figs/" + "vanilla_lstm_"+name+"_avgtrainloss_lr_"+ str(learning_rate) + '_epochs_' + str(num_epoch) + '_predlen_' + str(pred_len) + '.jpeg')
	# plt.show()
	# plt.show(block=True)

	plt.figure() # new figure
	plt.title("Average and final displacement error {} epochs".format(num_epoch))
	plt.plot(avg_train_finalD_error,label='train:final disp. error') 
	plt.plot(avg_train_avgD_error,color='red',label='train:avg disp. error')
	plt.plot(test_finalD_error,color='green',label='test:final disp. error')
	plt.plot(test_avgD_error,color='black',label='test:avg disp. error')
	plt.ylim((0,10))
	plt.legend()
	# plt.show()
	plt.savefig("./saved_figs/" + "vanilla_lstm_"+name+"_avg_final_displacement_lr_"+ str(learning_rate) + '_epochs_' + str(num_epoch) + '_predlen_' + str(pred_len) + '.jpeg')

	plt.figure()
	plt.title("Std of train loss vs epoch{} epochs".format(num_epoch))
	plt.plot(std_train_loss)
	plt.savefig("./saved_figs/" + "vanilla_lstm_"+name+"_stdtrainloss_lr_"+ str(learning_rate) + '_epochs_' + str(num_epoch) + '_predlen_' + str(pred_len) + '.jpeg')
	# plt.show(block=True)
	print("saved images for avg training losses! location: " + "./saved_figs")

	# save results to text file
	txtfilename = os.path.join("./txtfiles/", "vanilla_lstm_"+name+"_avgtrainlosses_lr_"+ str(learning_rate) + '_epochs_' + str(num_epoch) + '_predlen_' + str(pred_len) + ".txt")
	os.makedirs(os.path.dirname("./txtfiles/"), exist_ok=True) # make directory if it doesn't exist
	with open(txtfilename, "w") as f:
		f.write("\n==============Average train loss vs. epoch:===============\n")
		f.write(str(avg_train_loss))
		f.write("\nepochs: " + str(num_epoch))
		f.write("\n==============Std train loss vs. epoch:===================\n")
		f.write(str(std_train_loss))
		f.write("\n==============Avg test loss vs. epoch:===================\n")
		f.write(str(avg_test_loss))
		f.write("\n==============Avg train displacement error:===================\n")
		f.write(str(avg_train_avgD_error))
		f.write("\n==============Final train displacement error:===================\n")
		f.write(str(avg_train_finalD_error))
		f.write("\n==============Avg test displacement error:===================\n")
		f.write(str(test_avgD_error))
		f.write("\n==============Final test displacement error:===================\n")
		f.write(str(test_finalD_error))
	print("saved average and std of training losses to text file in: ./txtfiles")
Exemplo n.º 25
0
def main(args):

    # define parameters for training and testing loops
    num_epoch = 5
    # pred_freq = 1
    pred_len = 12
    learning_rate = 0.0005

    # get data
    # train_input, train_target, test_input, test_target = getData()
    dataset, dataloader = loader.data_loader(args, data_dir)

    # define the network and criterion
    vanilla_lstm_net = VanillaLSTMNet()
    # vanilla_lstm_net.double() # casts tensor to double
    criterion = nn.MSELoss(
    )  # MSE works best for difference between predicted and actual coordinate paths
    # define the optimizer
    optimizer = optim.Adam(vanilla_lstm_net.parameters(), lr=learning_rate)

    # initialize lists for capturing losses
    train_loss = []
    test_loss = []
    avg_train_loss = []
    avg_test_loss = []
    train_avgD_error = []
    train_finalD_error = []
    avg_train_avgD_error = []
    avg_train_finalD_error = []
    test_finalD_error = []
    tets_avgD_error = []
    std_train_loss = []
    std_test_loss = []
    '''train for 'num_epoch' epochs and test every 'pred_freq' epochs & when predicting use pred_len=6'''

    ### TRAINING FUNCTION ###
    for i in range(num_epoch):
        print(
            '======================= Epoch: {cur_epoch} / {total_epochs} ======================='
            .format(cur_epoch=i, total_epochs=num_epoch))

        def closure():
            for i, batch in enumerate(dataloader):
                # print("batch length:", len(batch)) # DEBUG
                train_batch = batch[0]
                target_batch = batch[1]
                print("train_batch's shape", train_batch.shape)
                print("target_batch's shape", target_batch.shape)

                seq, peds, coords = train_batch.shape  # q is number of pedestrians

                #forward pass
                out = vanilla_lstm_net(
                    train_batch, pred_len=pred_len
                )  # forward pass of lstm network for training
                print("out's shape:", out.shape)
                optimizer.zero_grad()  # zero out gradients
                cur_train_loss = criterion(out,
                                           target_batch)  # calculate MSE loss
                print('Current training loss: {}'.format(
                    cur_train_loss.item()))  # print current training loss
                #calculating average deisplacement error
                out1 = out
                target_batch1 = target_batch  #making a copy of the tensors to convert them to array
                avgD_error = (np.sum(
                    np.sqrt(
                        np.square(out1[:, :, 0].detach().numpy() -
                                  target_batch1[:, :, 0].detach().numpy()) +
                        np.square(out1[:, :, 1].detach().numpy() -
                                  target_batch1[:, :, 1].detach().numpy())))
                              ) / (pred_len * peds)
                train_avgD_error.append(avgD_error)
                print("current avg Disp error:", avgD_error)
                #calculating final displacement error
                finalD_error = (np.sum(
                    np.sqrt(
                        np.square(out1[pred_len - 1, :, 0].detach().numpy() -
                                  target_batch1[pred_len - 1, :,
                                                0].detach().numpy()) +
                        np.square(out1[pred_len - 1, :, 1].detach().numpy() -
                                  target_batch1[pred_len - 1, :,
                                                1].detach().numpy())))) / peds
                train_finalD_error.append(finalD_error)
                print("current final displacement error:", finalD_error)

                train_loss.append(cur_train_loss.item())
                cur_train_loss.backward()  # backward prop
                optimizer.step(
                )  # step like a mini-batch (after all pedestrians)
                ### end prototyping ###

            return cur_train_loss

        optimizer.step(closure)  # update weights

        # save model at every epoch (uncomment)
        # torch.save(vanilla_lstm_net, './saved_models/vanilla_lstm_model_lr0005.pt')
        # print("Saved vanilla_lstm_net!")
        avg_train_loss.append(np.sum(train_loss) / len(train_loss))
        avg_train_avgD_error.append(
            np.sum(train_avgD_error) / len(train_avgD_error))
        avg_train_finalD_error.append(
            np.sum(train_finalD_error) / len(train_finalD_error))
        std_train_loss.append(np.std(np.asarray(train_loss)))
        train_loss = []  # empty train loss

        print("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$")
        print("average train loss: {}".format(avg_train_loss))
        print("average std loss: {}".format(std_train_loss))
        avg_test_loss.append(
            test(vanilla_lstm_net, args)
        )  ##calliing test function to return avg test loss at each epoch

    # all epochs have ended, so now save your model
    torch.save(vanilla_lstm_net, './saved_models/Vlstm_model_lr0005_ep5.pt')
    print("Saved vanilla_lstm_net!" +
          './saved_models/Vlstm_model_lr0005_ep5.pt')
    ''' visualize losses vs. epoch'''

    plt.figure()  # new figure
    plt.title(
        "Average train & test loss v/s epoch {} epochs".format(num_epoch))
    plt.plot(avg_train_loss, label='avg train_loss')
    plt.plot(avg_test_loss, color='red', label='avg test_loss')
    plt.legend()
    plt.show(block=True)
    # plt.show()

    plt.figure()  # new figure
    plt.title(
        "Average and final displacement error {} epochs".format(num_epoch))
    plt.plot(avg_train_finalD_error, label='final displacement error')
    plt.plot(avg_train_avgD_error, color='red', label='avg displacement error')
    plt.legend()
    plt.show(block=True)

    #visualizing std deviation v/s epoch
    plt.figure()
    plt.title("Std of train loss vs epoch")
    plt.plot(std_train_loss)
    plt.show(block=True)
Exemplo n.º 26
0
def train(args):
    torch.cuda.manual_seed(1000)
    print('Loading data')

    if args.imgf_path == None:
        #default bottom-up top-down
        loader = data_loader(b_size=args.b_size)
    else:
        loader = data_loader(b_size=args.b_size, image_path=args.imgf_path)

    model = Model(v_size=loader.v_size,
                  K=loader.K,
                  f_dim=loader.f_dim,
                  h_dim=args.hidden,
                  o_dim=loader.o_dim,
                  pretrained_we=loader.we_matrix)

    criterion = nn.BCEWithLogitsLoss()

    # Move it to GPU
    model = model.cuda()
    criterion = criterion.cuda()

    if args.optim == 'adam':
        optim = torch.optim.Adam(model.parameters(), lr=args.lr)
    elif args.optim == 'adadelta':
        optim = torch.optim.Adadelta(model.parameters(), lr=args.lr)
    elif args.optim == 'adagrad':
        optim = torch.optim.Adagrad(model.parameters(), lr=args.lr)
    elif args.optim == 'SGD':
        optim = torch.optim.SGD(model.parameters(), lr=args.lr)
    else:
        sys.exit('Invalid optimizer.(adam, adadelta, adagrad, SGD)')

    # Continue training from saved model
    if args.savedmodel and os.path.isfile(args.savedmodel):
        print('Reading Saved model {}'.format(args.savedmodel))
        ckpt = torch.load(args.savedmodel)
        model.load_state_dict(ckpt['state_dict'])
        optim.load_state_dict(ckpt['optimizer'])

    # Training script
    print('Start training.')

    # for model save
    path_num = 1
    while os.path.exists('save/model_{}'.format(path_num)):
        path_num += 1

    for ep in range(args.epoch):
        ep_loss = 0
        ep_correct = 0

        for step in range(loader.n_batch):
            # Batch preparation
            q_batch, a_batch, i_batch = loader.next_batch()
            q_batch = Variable(torch.from_numpy(q_batch))
            a_batch = Variable(torch.from_numpy(a_batch))
            i_batch = Variable(torch.from_numpy(i_batch))
            q_batch, a_batch, i_batch = q_batch.cuda(), a_batch.cuda(
            ), i_batch.cuda()

            # Do model forward
            output = model(q_batch, i_batch)
            # print(output.shape)
            loss = criterion(output, a_batch)

            if step % 400 == 0:
                _, oix = output.data.max(1)
                _, aix = a_batch.data.max(1)

                correct = torch.eq(oix, aix).sum()
                ep_correct += correct
                ep_loss += loss.data[0]
                print(
                    'Epoch %02d(%03d/%03d), loss: %.3f, correct: %3d / %d (%.2f%%)'
                    % (ep + 1, step, loader.n_batch, loss.data[0], correct,
                       args.b_size, correct * 100 / args.b_size))

            # compute gradient and do optim step
            optim.zero_grad()
            loss.backward()
            optim.step()

        # Save model after every epoch
        tbs = {
            'epoch': ep + 1,
            'loss': ep_loss / loader.n_batch,
            'accuracy': ep_correct * 100 / (loader.n_batch * args.b_size),
            'state_dict': model.state_dict(),
            'optimizer': optim.state_dict()
        }
        save_model(tbs, path_num, ep, args)
Exemplo n.º 27
0
def main(args):

    # define parameters for training and testing loops
    num_epoch = 10
    # pred_freq = 1
    pred_len = 12
    learning_rate = 0.0005

    # get data
    # train_input, train_target, test_input, test_target = getData()
    dataset, dataloader = loader.data_loader(args, data_dir)

    # define the network and criterion
    gru_net = GRUNet()
    criterion = nn.MSELoss(
    )  # MSE works best for difference between predicted and actual coordinate paths
    # define the optimizer
    optimizer = optim.Adam(gru_net.parameters(), lr=learning_rate)

    # initialize lists for capturing losses
    train_loss = []
    test_loss = []
    avg_train_loss = []
    avg_test_loss = []
    std_train_loss = []
    std_test_loss = []
    '''train for 'num_epoch' epochs and test every 'pred_freq' epochs & when predicting use pred_len=6'''

    ### TRAINING FUNCTION ###
    for i in range(num_epoch):
        print(
            '======================= Epoch: {cur_epoch} / {total_epochs} ======================='
            .format(cur_epoch=i, total_epochs=num_epoch))

        def closure():
            for i, batch in enumerate(dataloader):
                # print("batch length:", len(batch)) # DEBUG
                train_batch = batch[0]
                target_batch = batch[1]
                print("train_batch's shape", train_batch.shape)
                print("target_batch's shape", target_batch.shape)

                seq, peds, coords = train_batch.shape  # q is number of pedestrians

                #forward pass
                out = gru_net(train_batch, pred_len=pred_len
                              )  # forward pass of GRU network for training
                print("out's shape:", out.shape)
                optimizer.zero_grad()  # zero out gradients
                cur_train_loss = criterion(out,
                                           target_batch)  # calculate MSE loss
                print('Current training loss: {}'.format(
                    cur_train_loss.item()))  # print current training loss
                train_loss.append(cur_train_loss.item())
                cur_train_loss.backward()  # backward prop
                optimizer.step(
                )  # step like a mini-batch (after all pedestrians)
                ### end prototyping ###

            return cur_train_loss

        optimizer.step(closure)  # update weights

        # save model at every epoch (uncomment)
        # torch.save(gru_net, './saved_models/name_of_model.pt')
        # print("Saved gru_net!")
        avg_train_loss.append(sum(train_loss) / len(train_loss))
        std_train_loss.append(np.std(np.asarray(train_loss)))
        train_loss = []  # empty train loss

        print("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$")
        print("average train loss: {}".format(avg_train_loss))
        print("average std loss: {}".format(std_train_loss))

    # all epochs have ended, so now save your model
    torch.save(gru_net, './saved_models/GRU_lr0005_ep10.pt')
    print("Saved GRU_net!" + './saved_models/GRU_lr0005_ep10.pt')
    ''' visualize losses vs. epoch'''

    plt.figure()  # new figure
    plt.title("Average train loss vs epoch")
    plt.plot(avg_train_loss)
    plt.show(block=True)
    # plt.show()
    #visualizing std deviation v/s epoch
    plt.figure()
    plt.title("Std of train loss vs epoch")
    plt.plot(std_train_loss)
    plt.show(block=True)
    default=8,
    help='Frequency number(epoch) of learning decay for optimizer')
# store grids in epoch 0 and use further.2 times faster -> Intensive memory use around 12 GB
parser.add_argument('--grid',
                    action="store_true",
                    default=True,
                    help='Whether store grids and use further epoch')

# Dataset options
parser.add_argument('--dataset_name', default='zara1', type=str)
parser.add_argument('--delim', default='\t')
parser.add_argument('--loader_num_workers', default=4, type=int)
parser.add_argument('--obs_len', default=8, type=int)
parser.add_argument('--pred_len', default=8, type=int)
parser.add_argument('--skip', default=1, type=int)

args = parser.parse_args()

# In[67]:

dataset, dataloader = loader.data_loader(args, data_dir)

# for i, f****r in enumerate(dataloader):
#      print("this is f****r's shape:\n", len(f****r)) # returns list of 7
#      for j, subfucker in enumerate(f****r):
#           # print("this is subfucker\n", subfucker)
#           print("subfucker's shape\n", subfucker.shape)

print(dataset)
print(dataloader)
Exemplo n.º 29
0
def main(args):
    if args.use_gpu == 1:
        os.environ["CUDA_VISIBLE_DEVICES"] = args.gpu_num
        print("Using GPU", args.gpu_num)
    else:
        print("Not using GPU")

    train_dir = "train"
    val_dir = "val"
    long_dtype, float_dtype = get_dtypes(args)

    print("Initializing train dataset")
    train_dset, train_loader = data_loader(args.dataset_folder, train_dir,
                                           args.batch_size)
    print("Initializing val dataset")
    val_dset, val_loader = data_loader(args.dataset_folder, val_dir,
                                       args.batch_size)
    print("Training for %d" % args.num_epochs)
    print("Arguments", args.__dict__)

    model = PredictFromNightBaseline()
    # model = PredictFromDayBaseline()
    # model = PredictBaseline()
    model.type(float_dtype)
    print(model)

    optimizer = optim.Adam(filter(lambda p: p.requires_grad,
                                  model.parameters()),
                           lr=args.learning_rate)
    # criterion = nn.BCELoss()
    criterion = nn.CrossEntropyLoss()

    max_val_acc = 0.0

    for epoch in range(args.num_epochs):
        gc.collect()

        # Train epoch
        model.train()
        loss_avg = RunningAverage()
        acc_avg = RunningAverage()
        with tqdm(total=len(train_loader)) as t:
            for i, train_batch in enumerate(train_loader):
                train_batch = [
                    tensor.cuda() if args.use_gpu else tensor
                    for tensor in train_batch
                ]
                X_day, X_night, Y = train_batch

                out = model(X_day, X_night)
                loss = criterion(out, Y)

                optimizer.zero_grad()
                loss.backward()
                optimizer.step()

                acc_avg.update_step(calc_accuracy(out, Y), Y.shape[0])
                loss_avg.update_step(loss.item(), Y.shape[0])
                t.set_postfix(loss='{:05.3f}'.format(loss_avg()),
                              acc='{:05.3f}'.format(acc_avg()))
                t.update()


#why doesnt the code go here
# Val metrics
        model.eval()
        val_loss = RunningAverage()
        val_acc = RunningAverage()
        for i, val_batch in enumerate(val_loader):
            val_batch = [
                tensor.cuda() if args.use_gpu else tensor
                for tensor in val_batch
            ]
            X_day, X_night, Y = val_batch

            out = model(X_day, X_night)
            loss = criterion(out, Y)

            val_loss.update_step(loss.item(), Y.shape[0])
            val_acc.update_step(calc_accuracy(out, Y), Y.shape[0])

        metrics_string = "Loss: {:05.3f} ; Acc: {:05.3f}".format(
            loss_avg(), acc_avg())
        val_metrics = "Loss: {:05.3f} ; Acc: {:05.3f}".format(
            val_loss(), val_acc())
        print("Epoch [%d/%d] - Train -" % (epoch + 1, args.num_epochs),
              metrics_string, "- Val -", val_metrics)

        if val_acc() > max_val_acc and args.save_model_weights:
            torch.save(model.state_dict(),
                       os.path.join(model_path, str(epoch)))
Exemplo n.º 30
0
    print('The journey begins! ML automation with Pytorch')

    #Call the class data_read()
    data = data_read(config["directory"], config['dataframe_list'] ,config['date_list'] , config['target_list'] ,
    config['idx'], config['multiclass_discontinuous'],config['text'] , config['remove_list'])

    # #call the class data_prep()
    dataframe_list = data.df_list
    base_join = data.df_list[0]
    join_list = [[data.df_list[1] , ['campaign_id'] , ['left']]]

    prep = data_prep(dataframe_list, base_join , join_list)
    print('lets check the data prepared' , prep.dataframe.head(2) , prep.dataframe.shape)

    # #call the classes from loader data_loader() , data_loader_pytorch()
    load = data_loader(prep.dataframe , data.dtype_list)
    load_text = data_loader_text(load.dataframe , config['text'] , load.in_emb_list , load.out_emb_list , load.mlp_features , load.emb_features)
    load_torch = data_loader_pytorch(load_text.dataframe , load_text.mlp_features , load_text.emb_features,load_text.emb_text_data , load.target)

    # #call the class Neural_Network()
    model = Neural_Network(load.in_emb_list , load.out_emb_list ,load_text.in_text_emb_list,load_text.out_text_emb_list ,inp_mlp_dim = 38, out_mlp_dim = 1)
    criterion = torch.nn.BCELoss(size_average = True)
    optimizer = torch.optim.SGD(model.parameters() , lr = 0.1)


    for epoch in range(10):
        target , mlp , embed , emb_text_data = load_torch.target ,load_torch.mlp , load_torch.emb , load_torch.emb_text_data
        pred = model(embed , mlp , emb_text_data)
        print('pred & target' , pred)
        loss = criterion(pred.float() , target.float()) ; print(epoch , loss)
        optimizer.zero_grad()