예제 #1
0
    def __init__(self, data_function=load_sim_data, data_path='../data/train_2d_controller.npz',
                       device='cpu',
                       number_mixtures=20, hidden_size=1024,
                       batch_size=1, lead_in=5,
                       model_load_path='saved_models/model_000000006500000.pkl',
                       random_state_number=44):

        # time index (axis=0)
        self.rdn = np.random.RandomState(random_state_number)
        # data path can be testing or training data
        self.data_path = data_path
        self.finished = True
        # return data as numpy array
        self.x, self.y, self.subgoals, self.keys, self.pts = load_function(self.data_path, cut=True)
        # ughhh TODO one hot the action data in the simulation model
        self.steering_actions = [-1, -0.5, 0, 0.5, 1]
        self.throttle_actions = [-.5, .2, .4, .6, .8]
        actions = []
        for s in self.steering_actions:
            for t in self.throttle_actions:
                actions.append((s,t))

        self.actions = np.array(actions)
        self.action_inds = np.arange(len(self.actions))
        self.max_step = self.y.shape[0]
        self.data_indexes = np.arange(self.x.shape[1], dtype=np.int)
        self.trace_length = self.x.shape[0]
        self.input_size = self.x.shape[2]
        self.output_size = self.y.shape[2]
        self.lead_in = lead_in
        # build model
        self.model_load_path = model_load_path
        if not os.path.exists(self.model_load_path):
            print("Provided model path does not exist!")
            print(self.model_load_path)
            sys.exit()
        else:
            lstm_dict = torch.load(self.model_load_path)
        try:
            self.batch_size = batch_size
            self.number_mixtures = number_mixtures
            self.hidden_size = hidden_size
            self.device = device
            self.lstm = mdnLSTM(input_size=self.input_size, hidden_size=self.hidden_size,
                                number_mixtures=self.number_mixtures).to(self.device)
            self.lstm.load_state_dict(lstm_dict['state_dict'])
            print("sucessfully loaded model: %s" %self.model_load_path)
        except Exception:
            print('unable to load model')
            print(e)
예제 #2
0
        test_load_path=
        '../data/icra2019_vehicle_data_2018-09-13_1670_files_test.npz',
        batch_size=batch_size)
    if savedir == '':
        print('please provide savedir -s')
        sys.exit()
    if not os.path.exists(savedir):
        os.makedirs(savedir)

    v_xnp, v_ynp = data_loader.validation_data()
    v_x = Variable(torch.FloatTensor(v_xnp))
    v_y = Variable(torch.FloatTensor(v_ynp))
    input_size = v_x.shape[2]
    output_shape = v_y.shape[2]
    lstm = mdnLSTM(input_size=input_size,
                   hidden_size=hidden_size,
                   number_mixtures=number_mixtures).to(DEVICE)
    optim = torch.optim.Adam(lstm.parameters(), lr=args.learning_rate)

    model_save_name = 'model'
    if args.load:
        if not os.path.exists(args.model_loadname):
            print("load model: %s does not exist" % args.model_loadname)
            sys.exit()
        else:
            print("loading %s" % args.model_loadname)
            lstm_dict = torch.load(args.model_loadname)
            lstm.load_state_dict(lstm_dict['state_dict'])
            optim.load_state_dict(lstm_dict['optimizer'])
            train_cnts = lstm_dict['train_cnts']
            train_losses = lstm_dict['train_losses']

class mdnAgent():
    def __init__(self, model_savedir, model_base_save_name='pose_model', DEVICE='cpu', batch_size=32, seq_length=15,
                 data_input_size=10, hidden_size=1024, number_mixtures=20,
                 grad_clip=5, save_every=1000, learning_rate=1e-4)

        super(mdnAgent, self).__init__()
        self.train_losses, self.test_losses, self.train_cnts, self.test_cnts = [], [], [], []
        if not os.path.exists(self.model_savedir):
            os.makedirs(self.model_savedir)

        self.last_save = 0
        self.cnt = 0
        self.lstm = mdnLSTM(input_size=self.data_input_size,
                            hidden_size=self.hidden_size,
                            number_mixtures=self.number_mixtures).to(self.DEVICE)
        self.optim = torch.optim.Adam(self.lstm.parameters(), lr=self.learning_rate)

    def load_data_from_file(self):
        data_loader = DataLoader(load_sim_data, train_load_path='../data/train_2d_controller.npz',
                                 test_load_path='../data/train_2d_controller.npz',
                                 batch_size=batch_size)

        v_xnp, v_ynp = data_loader.validation_data()
        v_x = Variable(torch.FloatTensor(v_xnp))
        v_y = Variable(torch.FloatTensor(v_ynp))
        input_size = v_x.shape[2]
        output_shape = v_y.shape[2]

    def load_model(self, model_loadname):