Пример #1
0
def trainLSTM():
	#path_to_checkpoint = poseDataset.base_dir + '/checkpoints_LSTM_no_Trans_no_rot_lr_{0}_mu_{1}_gclip_{2}_batch_size_{3}_truncate_gradient_{4}/'.format(args.initial_lr,args.momentum,args.g_clip,args.batch_size,args.truncate_gradient)

	path_to_checkpoint = poseDataset.base_dir + '/{0}/'.format(args.checkpoint_path)

	if not os.path.exists(path_to_checkpoint):
		os.mkdir(path_to_checkpoint)
	saveNormalizationStats(path_to_checkpoint)

	trX,trY = poseDataset.getMalikFeatures()
	trX_validation,trY_validation = poseDataset.getMalikValidationFeatures()
	trX_forecasting,trY_forecasting = poseDataset.getMalikTrajectoryForecasting()
	
	saveForecastedMotion(trY_forecasting,path_to_checkpoint)
	saveForecastedMotion(trX_forecasting,path_to_checkpoint,'motionprefix_N_')
	print 'X forecasting ',trX_forecasting.shape
	print 'Y forecasting ',trY_forecasting.shape
	
	inputDim = trX.shape[2]
	print inputDim
	rnn = []
	if args.use_pretrained == 1:
		rnn = load(path_to_checkpoint+'checkpoint.'+str(args.iter_to_load))
	else:
		args.iter_to_load = 0
		rnn = LSTMRegression(inputDim)
	rnn.fitModel(trX, trY, snapshot_rate=args.snapshot_rate, path=path_to_checkpoint, epochs=args.epochs, batch_size=args.batch_size,
		decay_after=args.decay_after, learning_rate=args.initial_lr, learning_rate_decay=args.learning_rate_decay, trX_validation=trX_validation,
		trY_validation=trY_validation, trX_forecasting=trX_forecasting, trY_forecasting=trY_forecasting, iter_start=args.iter_to_load,
		decay_type=args.decay_type, decay_schedule=args.decay_schedule, decay_rate_schedule=args.decay_rate_schedule,
		use_noise=args.use_noise, noise_schedule=args.noise_schedule, noise_rate_schedule=args.noise_rate_schedule,maxiter=args.maxiter,poseDataset=poseDataset,unNormalizeData=unNormalizeData)
Пример #2
0
def trainLSTM():
    #path_to_checkpoint = poseDataset.base_dir + '/checkpoints_LSTM_no_Trans_no_rot_lr_{0}_mu_{1}_gclip_{2}_batch_size_{3}_truncate_gradient_{4}/'.format(args.initial_lr,args.momentum,args.g_clip,args.batch_size,args.truncate_gradient)

    path_to_checkpoint = poseDataset.base_dir + '/{0}/'.format(
        args.checkpoint_path)

    if not os.path.exists(path_to_checkpoint):
        os.mkdir(path_to_checkpoint)
    saveNormalizationStats(path_to_checkpoint)

    trX, trY = poseDataset.getMalikFeatures()
    trX_validation, trY_validation = poseDataset.getMalikValidationFeatures()
    trX_forecasting, trY_forecasting = poseDataset.getMalikTrajectoryForecasting(
    )

    saveForecastedMotion(trY_forecasting, path_to_checkpoint)
    saveForecastedMotion(trX_forecasting, path_to_checkpoint,
                         'motionprefix_N_')
    print 'X forecasting ', trX_forecasting.shape
    print 'Y forecasting ', trY_forecasting.shape

    inputDim = trX.shape[2]
    print inputDim
    rnn = []
    if args.use_pretrained == 1:
        rnn = load(path_to_checkpoint + 'checkpoint.' + str(args.iter_to_load))
    else:
        args.iter_to_load = 0
        rnn = LSTMRegression(inputDim)
    rnn.fitModel(trX,
                 trY,
                 snapshot_rate=args.snapshot_rate,
                 path=path_to_checkpoint,
                 epochs=args.epochs,
                 batch_size=args.batch_size,
                 decay_after=args.decay_after,
                 learning_rate=args.initial_lr,
                 learning_rate_decay=args.learning_rate_decay,
                 trX_validation=trX_validation,
                 trY_validation=trY_validation,
                 trX_forecasting=trX_forecasting,
                 trY_forecasting=trY_forecasting,
                 iter_start=args.iter_to_load,
                 decay_type=args.decay_type,
                 decay_schedule=args.decay_schedule,
                 decay_rate_schedule=args.decay_rate_schedule,
                 use_noise=args.use_noise,
                 noise_schedule=args.noise_schedule,
                 noise_rate_schedule=args.noise_rate_schedule,
                 maxiter=args.maxiter,
                 poseDataset=poseDataset,
                 unNormalizeData=unNormalizeData)
Пример #3
0
		fname = 'motionprefixlong'
		model.saveForecastedMotion(trX_forecast_nodeFeatures_,path,fname)

		cellstate = model.predict_cell(trX_forecasting,trX_forecast_nodeFeatures,sequence_length=trY_forecasting.shape[0],poseDataset=poseDataset,graph=graph)
		fname = 'forecast_celllong_{0}'.format(iteration)
		model.saveCellState(cellstate,path,fname)
		t1 = time.time()
		del model

elif args.forecast == 'lstm' or args.forecast == 'malik':
	path_to_checkpoint = '{0}checkpoint.{1}'.format(path,iteration)
	if os.path.exists(path_to_checkpoint):
		model = load(path_to_checkpoint)
		print 'Loaded LSTM/Malik: ',path_to_checkpoint

		trX_forecasting,trY_forecasting = poseDataset.getMalikTrajectoryForecasting()

		fname = 'ground_truth_forecast'
		model.saveForecastedMotion(trY_forecasting,path,fname)

		fname = 'motionprefix'
		model.saveForecastedMotion(trX_forecasting,path,fname)

		forecasted_motion = model.predict_sequence(trX_forecasting,sequence_length=trY_forecasting.shape[0])
		fname = 'forecast_iteration_{0}'.format(iteration)
		model.saveForecastedMotion(forecasted_motion,path,fname)

		'''
		skel_err = np.mean(np.sqrt(np.sum(np.square((forecasted_motion - trY_forecasting)),axis=2)),axis=1)
		err_per_dof = skel_err / trY_forecasting.shape[2]
		fname = 'forecast_error_iteration_{0}'.format(iteration)
Пример #4
0
            trX_forecast_nodeFeatures,
            sequence_length=trY_forecasting.shape[0],
            poseDataset=poseDataset,
            graph=graph)
        fname = 'forecast_celllong_{0}'.format(iteration)
        model.saveCellState(cellstate, path, fname)
        t1 = time.time()
        del model

elif args.forecast == 'lstm' or args.forecast == 'malik':
    path_to_checkpoint = '{0}checkpoint.{1}'.format(path, iteration)
    if os.path.exists(path_to_checkpoint):
        model = load(path_to_checkpoint)
        print 'Loaded LSTM/Malik: ', path_to_checkpoint

        trX_forecasting, trY_forecasting = poseDataset.getMalikTrajectoryForecasting(
        )

        fname = 'ground_truth_forecast'
        model.saveForecastedMotion(trY_forecasting, path, fname)

        fname = 'motionprefix'
        model.saveForecastedMotion(trX_forecasting, path, fname)

        forecasted_motion = model.predict_sequence(
            trX_forecasting, sequence_length=trY_forecasting.shape[0])
        fname = 'forecast_iteration_{0}'.format(iteration)
        model.saveForecastedMotion(forecasted_motion, path, fname)
        '''
		skel_err = np.mean(np.sqrt(np.sum(np.square((forecasted_motion - trY_forecasting)),axis=2)),axis=1)
		err_per_dof = skel_err / trY_forecasting.shape[2]
		fname = 'forecast_error_iteration_{0}'.format(iteration)
def model():
    if args.forecast == 'srnn':
        path_to_checkpoint = '{0}checkpoint.{1}'.format(path, args.iteration)
        print "Using checkpoint at: ",path_to_checkpoint
        if os.path.exists(path_to_checkpoint):
            [nodeNames,
            nodeList,
            nodeFeatureLength,
            nodeConnections,
            edgeList,
            edgeListComplete,
            edgeFeatures,
            nodeToEdgeConnections,
            trX,trY,
            trX_validation,
            trY_validation,
            trX_forecasting,
            trY_forecasting,
            trX_forecast_nodeFeatures] = graph.readCRFgraph(poseDataset)

            print trX_forecast_nodeFeatures.keys()
            print 'Loading the model (this takes long, can take upto 25 minutes)'
            model = loadDRA(path_to_checkpoint)
            print model
            print 'Loaded S-RNN from ', path_to_checkpoint
            t0 = time.time()
            save_full = True

            trY_forecasting = model.convertToSingleVec(trY_forecasting,new_idx,featureRange)  # 8x100x54
            print trY_forecasting.shape
            trX_forecast_nodeFeatures_ = model.convertToSingleVec(trX_forecast_nodeFeatures, new_idx, featureRange)
            # normalize_data_for_srnn()

            trY_forecasting_tem = trY_forecasting
            trX_forecasting_tem = trX_forecast_nodeFeatures_

            filterList = [10, 11,
                        16, 17, 18, 19, 20,
                        25, 26,
                        31, 32, 33, 34, 35,
                        48, 49, 50,
                        58, 59,
                        63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74,
                        82, 83, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98]

            for i in filterList:
                trY_forecasting_tem = np.insert(trY_forecasting_tem, i, 0, axis=2)
                trX_forecasting_tem = np.insert(trX_forecasting_tem, i, 0, axis=2)
            trY_forecasting_full = trY_forecasting_tem
            trX_forecasting_full = trX_forecasting_tem
            print(trY_forecasting_full.shape)
            print(trX_forecasting_full.shape)

            fname = 'ground_truth_forecast'
            if(save_full):
                # unnormalize_data()
                trY_forecasting_full_orig = unnormalize_data(trY_forecasting_full, poseDataset.data_mean, poseDataset.data_std, [])
                model.saveForecastedMotion(trY_forecasting_full_orig, path, fname)
            else:
                model.saveForecastedMotion(trY_forecasting, path, fname)

            fname = 'motionprefix'
            if(save_full):
                trX_forecast_nodeFeatures_full_orig = unnormalize_data(trX_forecasting_full, poseDataset.data_mean, poseDataset.data_std, [])
                model.saveForecastedMotion(trX_forecast_nodeFeatures_full_orig, path, fname)
            else:
                model.saveForecastedMotion(trX_forecasting, path, fname)

            forecasted_motion = model.predict_sequence(trX_forecasting, trX_forecast_nodeFeatures,sequence_length=trY_forecasting.shape[0],poseDataset=poseDataset,graph=graph)
            forecasted_motion = model.convertToSingleVec(forecasted_motion, new_idx, featureRange)
            fname = 'forecast'
            if(save_full):
                filter_list = []
                for x in range(trY_forecasting_full.shape[2]):
                    if x in  filterList:
                        continue
                    filter_list.append(x)
                forecasted_motion_full = trY_forecasting_full
                forecasted_motion_full[:, :, filter_list] = forecasted_motion
                # unnormalize_data()
                forecasted_motion_full_orig = unnormalize_data(forecasted_motion_full, poseDataset.data_mean, poseDataset.data_std, [])
                model.saveForecastedMotion(forecasted_motion_full_orig, path, fname)
            else:
                model.saveForecastedMotion(forecasted_motion, path, fname)

            skel_err = np.mean(np.sqrt(np.sum(np.square((forecasted_motion - trY_forecasting)),axis=2)),axis=1)
            err_per_dof = skel_err / trY_forecasting.shape[2]
            fname = 'forecast_error'
            model.saveForecastError(skel_err,err_per_dof,path,fname)
            t1 = time.time()
            print t1-t0
            del model
        else:
            print "the path is not found!!!"


    elif args.forecast == 'lstm3lr' or args.forecast == 'erd':
        path_to_checkpoint = '{0}checkpoint.{1}'.format(path, args.iteration)
        if os.path.exists(path_to_checkpoint):
            print "Loading the model {0} (this may take sometime)".format(args.forecast)
            model = load(path_to_checkpoint)
            print 'Loaded the model from ',path_to_checkpoint

            save_full = True
            # more colum than srnn1 8x100x99
            trX_forecasting_full, trY_forecasting_full = poseDataset.getMalikTrajectoryForecastingFull()

            # same mat as srnn1 8x100x54
            trX_forecasting, trY_forecasting = poseDataset.getMalikTrajectoryForecasting()

            fname = 'ground_truth_forecast'
            if(save_full):
                # unnormalize_data()
                # print(poseDataset.dimensions_to_ignore)
                trY_forecasting_full_orig = unnormalize_data(trY_forecasting_full, poseDataset.data_mean,
                                                            poseDataset.data_std, [])
                model.saveForecastedMotion(trY_forecasting_full_orig, path, fname)
            else:
                model.saveForecastedMotion(trY_forecasting, path, fname)

            fname = 'motionprefix'
            if(save_full):
                # unnormalize_data()
                trX_forecasting_full_orig = unnormalize_data(trX_forecasting_full, poseDataset.data_mean,
                                                            poseDataset.data_std, [])
                model.saveForecastedMotion(trX_forecasting_full_orig, path, fname)
            else:
                model.saveForecastedMotion(trX_forecasting, path, fname)

            forecasted_motion = model.predict_sequence(trX_forecasting,sequence_length=trY_forecasting.shape[0])
            fname = 'forecast'
            if(save_full):
                filter_list = []
                for x in range(trY_forecasting_full.shape[2]):
                    if x in poseDataset.dimensions_to_ignore:
                        continue
                    filter_list.append(x)
                forecasted_motion_full = trY_forecasting_full
                forecasted_motion_full[:, :, filter_list] = forecasted_motion
                # unnormalize_data()
                forecasted_motion_full_orig = unnormalize_data(forecasted_motion_full, poseDataset.data_mean,
                                                            poseDataset.data_std, [])
                model.saveForecastedMotion(forecasted_motion_full_orig, path, fname)
            else:
                model.saveForecastedMotion(forecasted_motion,path,fname)

            skel_err = np.mean(np.sqrt(np.sum(np.square((forecasted_motion - trY_forecasting)),axis=2)),axis=1)
            err_per_dof = skel_err / trY_forecasting.shape[2]
            fname = 'forecast_error'
            model.saveForecastError(skel_err,err_per_dof,path,fname)

            # print(trX_forecasting_full.shape)
            # print(trX_forecasting.shape)

            del model
        else:
            print "the path is not found!!!"


    elif args.forecast == 'dracell':
        path_to_checkpoint = '{0}checkpoint.{1}'.format(path, args.iteration)
        if os.path.exists(path_to_checkpoint):
            [nodeNames,nodeList,nodeFeatureLength,nodeConnections,edgeList,edgeListComplete,edgeFeatures,nodeToEdgeConnections,trX,trY,trX_validation,trY_validation,trX_forecasting,trY_forecasting,trX_forecast_nodeFeatures] = graph.readCRFgraph(poseDataset,noise=0.7,forecast_on_noisy_features=True)
            print trX_forecast_nodeFeatures.keys()
            print 'Loading the model'
            model = loadDRA(path_to_checkpoint)
            print 'Loaded DRA: ',path_to_checkpoint
            t0 = time.time()
            trY_forecasting = model.convertToSingleVec(trY_forecasting,new_idx,featureRange)

            trX_forecast_nodeFeatures_ = model.convertToSingleVec(trX_forecast_nodeFeatures,new_idx,featureRange)
            fname = 'motionprefixlong'
            model.saveForecastedMotion(trX_forecast_nodeFeatures_,path,fname)

            cellstate = model.predict_cell(trX_forecasting,trX_forecast_nodeFeatures,sequence_length=trY_forecasting.shape[0],poseDataset=poseDataset,graph=graph)
            fname = 'forecast_celllong_{0}'.format(args.iteration)
            model.saveCellState(cellstate,path,fname)
            t1 = time.time()
            del model

    print 'ending program'