Exemplo n.º 1
0
    def start_drone(self):
        # Get the arguments for this subcommand.

        # Create the dagger object and train it.
        #pdb.set_trace()
        if (self.iteration > 1):
            self.dag = dagger.DAgger(self.learning)

            if (os.path.isfile('../data/coef.txt')):
                print('Load training')
                self.dag.load_coef()
            else:
                print('Start training')
                self.dag.train()
                self.dag.save_coef()

        # Feature extraction parameters.
        window_size = (10, 5)
        overlap = 0.25
        cmd_history_feats = 7  # the approximate number of cmd history features
        cmd_history_length = 10  # keep a running list of the last 10 cmds
        nav_history_feats = 7  # the approximate number of nav history features
        nav_history_length = 10  # keep a running list of the last 10 nav data

        # Create the drone object.
        self.debug_queue.put({'MSG': ':: Initializing parrot.', 'PRIORITY': 1})
        self.debugger.debug()
        self.drone = parrot.Parrot()

        self.feature_queue = Queue.Queue(maxsize=1)
        init_image = self.drone.get_image()
        self.feature_extractor = feature_extractor.FeatureExtractor(
            self.feature_queue, init_image, window_size, overlap,
            cmd_history_feats, cmd_history_length, nav_history_feats,
            nav_history_length)
Exemplo n.º 2
0
    def train(self, args):
        """ Starts training. Make sure you have annotated the images first.
        """
        self.iterations = args.iterations
        self.learning = args.learning

        # Create the dagger object.
        self.dag = dagger.DAgger(self.learning)
        self.dag.aggregate(self.iterations)
Exemplo n.º 3
0
    def execute(self, args):
        # Get the arguments for this subcommand.
        self.address = args.address
        self.learning = args.learning
        self.iteration = args.iteration
        self.trajectory = args.trajectory

        # Create the dagger object and train it.
        pdb.set_trace()
        self.dag = dagger.DAgger(self.learning)
        self.dag.train()

        # Feature extraction parameters.
        window_size = (10, 5)
        overlap = 0.25
        cmd_history_feats = 7  # the approximate number of cmd history features
        cmd_history_length = 10  # keep a running list of the last 10 cmds
        nav_history_feats = 7  # the approximate number of nav history features
        nav_history_length = 10  # keep a running list of the last 10 nav data

        self.debug_queue.put({
            'MSG': 'Parrot AR 2 Flying Tool :: Execution Mode',
            'PRIORITY': 1
        })
        self.debug_queue.put({
            'MSG': ':: GUI flag set to %s.' % str(self.gui),
            'PRIORITY': 1
        })
        self.debug_queue.put({
            'MSG': ':: Verbosity set to %d.' % self.verbosity,
            'PRIORITY': 1
        })
        self.debug_queue.put({
            'MSG':
            ':: Accessing controller server at: %s.' % self.address[0],
            'PRIORITY':
            1
        })
        self.debug_queue.put({
            'MSG':
            ':: Accessing navigation data server at: %s.' % self.address[1],
            'PRIORITY':
            1
        })
        self.debug_queue.put({
            'MSG':
            ':: Accessing camera stream server at: tcp://192.168.1.1:5555.',
            'PRIORITY': 1
        })

        # Create the drone object.
        self.debug_queue.put({'MSG': ':: Initializing parrot.', 'PRIORITY': 1})
        self.debugger.debug()
        self.drone = parrot.Parrot(self.debug_queue, self.error_queue,
                                   self.address, self.learning, self.iteration,
                                   self.trajectory)

        self.feature_queue = Queue.Queue(maxsize=1)
        init_image = self.drone.get_image()
        self.feature_extractor = feature_extractor.FeatureExtractor(
            self.feature_queue, init_image, window_size, overlap,
            cmd_history_feats, cmd_history_length, nav_history_feats,
            nav_history_length)

        directory = './data/%s/%s/' % (self.iteration, self.trajectory)

        # Start training.
        if self.iteration == 1:
            self.debug_queue.put({
                'MSG':
                'Since this is the first iteration, all input will come from the expert.',
                'PRIORITY': 1
            })

        self.debug_queue.put({'MSG': 'Waiting to take off...', 'PRIORITY': 1})
        self.debugger.debug()

        # Start when the drone takes off.
        while True:
            cmd = self.drone.get_cmd()
            self.drone.send_cmd(cmd)

            if cmd is not None:
                if cmd['T']:
                    break
        self.debug_queue.put({
            'MSG': 'Starting training for iteration %s, trajectory %s.',
            'PRIORITY': 1
        })
        self.debugger.debug()

        features_filename = directory + 'features.data'
        cmd_filename = directory + 'drone_cmds.data'

        # Loop until the drone has landed.
        self.time_step = 1
        feature_flag = False
        while True:
            # Land to avoid a crash.
            emergency_cmd = self.drone.get_cmd()
            if emergency_cmd is not None:
                if emergency_cmd['L']:
                    self.drone.send_cmd(self.drone.remote.land())
                    break

            image_filename = directory + '%s.jpg' % self.time_step

            expert_cmd = self.drone.get_cmd()
            expert_cmd['X'] = expert_cmd['X'] * 0.15
            if self.iteration == 1:
                expert_cmd['Y'] = 0.02
                if expert_cmd is not None and not feature_flag:
                    image = self.drone.get_image()
                    navdata = self.drone.get_navdata()
                    self.feature_extractor.extract(image)
                    self.feature_extractor.update(cmd, navdata)
                    feature_flag = True
                try:
                    features = self.feature_queue.get(block=False)
                    # Save the features and command.
                    self.save_image(image, image_filename)
                    self.save_features(features, features_filename)
                    self.save_cmd(expert_cmd, cmd_filename)
                    self.time_step += 1
                    feature_flag = False
                except Queue.Empty:
                    pass
                self.drone.send_cmd(expert_cmd)
            else:
                if not feature_flag:
                    image = self.drone.get_image()
                    navdata = self.drone.get_navdata()
                    self.feature_extractor.extract(image)
                    feature_flag = True
                try:
                    features = self.feature_queue.get(block=False)

                    # Get the command associated with this state.
                    blah = np.array([0])
                    blah.shape = (1, 1)
                    features = np.hstack((blah, features))
                    x = self.dag.test(features, self.iteration)
                    cmd = self.drone.default_cmd
                    cmd['Y'] = 0.02
                    cmd['X'] = x[0, 0] * 0.15
                    print(x)
                    self.feature_extractor.update(cmd, navdata)

                    # Save the features and command.
                    self.save_image(image, image_filename)
                    self.save_features(features, features_filename)
                    self.save_cmd(cmd, cmd_filename)
                    self.time_step += 1

                    self.drone.send_cmd(cmd)
                    feature_flag = False
                except Queue.Empty:
                    pass