ravens/agents/transporter.py
def train(self, dataset, num_iter, writer):
        """Train on dataset for a specific number of iterations.

        Daniel: notice how little training data we use! One 'iteration' is
        simply one image and an associated action, drawn by (a) sampling
        demo, then (b) sampling time within it. We do heavy data
        augmentation, but it's still just one real image.

        If using a goal image, we use a different random_sample method that
        also picks the LAST image of that episode, which is assigned as the
        goal image. This would likely not work for super long-horizon tasks,
        but maybe; (Agarwal et al., NeurIPS 2016) in the PokeBot paper
        actually got something like this 'greedy-style' planning to work.
        Otherwise we might have to do something like (Nair et al., ICRA 2017)
        in the follow-up work where we feed in a target image for each time
        step, which would be the *next* image saved.

        For data augmentation with this goal image, I believe we should stack
        the current and goal image together, and THEN do augmentation. The
        perturb method will make sure placing pixels are preserved -- which
        for short-horizon environments usually means the goal image will
        contain most of the relevant information. When data augmenting, for
        both normal and goal-conditioned Transporters, the p1_theta
        (rotation) is the same, but pick points are correctly 'converted' to
        those appropriate for the augmented images.
        """
        for i in range(num_iter):
            if self.use_goal_image:
                obs, act, info, goal = dataset.random_sample(goal_images=True)
            else:
                obs, act, info = dataset.random_sample()

            # Get heightmap from RGB-D images.
            configs = act['camera_config']
            colormap, heightmap = self.get_heightmap(obs, configs)
            if self.use_goal_image:
                colormap_g, heightmap_g = self.get_heightmap(goal, configs)

            # Get training labels from data sample.
            pose0, pose1 = act['params']['pose0'], act['params']['pose1']
            p0_position, p0_rotation = pose0[0], pose0[1]
            p0 = utils.position_to_pixel(p0_position, self.bounds, self.pixel_size)
            p0_theta = -np.float32(p.getEulerFromQuaternion(p0_rotation)[2])
            p1_position, p1_rotation = pose1[0], pose1[1]
            p1 = utils.position_to_pixel(p1_position, self.bounds, self.pixel_size)
            p1_theta = -np.float32(p.getEulerFromQuaternion(p1_rotation)[2])
            p1_theta = p1_theta - p0_theta
            p0_theta = 0

            # Concatenate color with depth images.
            input_image = self.concatenate_c_h(colormap, heightmap)

            # If using goal image, stack _with_ input_image for data augmentation.
            if self.use_goal_image:
                goal_image = self.concatenate_c_h(colormap_g, heightmap_g)
                input_image = np.concatenate((input_image, goal_image), axis=2)
                assert input_image.shape[2] == 12, input_image.shape

            # Do data augmentation (perturb rotation and translation).
            original_pixels = (p0, p1)
            input_image, pixels = utils.perturb(input_image, [p0, p1])
            p0, p1 = pixels

            # Optionally visualize images _after_ data agumentation.
            if False:
                self.visualize_images(p0, p0_theta, p1, p1_theta, original_pixels,
                        colormap=colormap, heightmap=heightmap,
                        colormap_g=colormap_g, heightmap_g=heightmap_g,
                        input_image=input_image, before_aug=False)

            # Compute Attention training loss.
            if self.attn_no_targ and self.use_goal_image:
                maxdim = int(input_image.shape[2] / 2)
                input_only = input_image[:, :, :maxdim]
                loss0 = self.attention_model.train(input_only, p0, p0_theta)
            else:
                loss0 = self.attention_model.train(input_image, p0, p0_theta)
            with writer.as_default():
                tf.summary.scalar('attention_loss', self.attention_model.metric.result(),
                    step=self.total_iter+i)

            # Compute Transport training loss.
            if isinstance(self.transport_model, Attention):
                loss1 = self.transport_model.train(input_image, p1, p1_theta)
            elif isinstance(self.transport_model, TransportGoal):
                half = int(input_image.shape[2] / 2)
                img_curr = input_image[:, :, :half]
                img_goal = input_image[:, :, half:]
                loss1 = self.transport_model.train(img_curr, img_goal, p0, p1, p1_theta)
            else:
                loss1 = self.transport_model.train(input_image, p0, p1, p1_theta)
            with writer.as_default():
                tf.summary.scalar('transport_loss', self.transport_model.metric.result(),
                    step=self.total_iter+i)

            print(f'Train Iter: {self.total_iter + i} Loss: {loss0:.4f} {loss1:.4f}')

        self.total_iter += num_iter
        self.save()
Train on dataset for a specific number of iterations.

        Daniel: notice how little training data we use! One 'iteration' is
        simply one image and an associated action, drawn by (a) sampling
        demo, then (b) sampling time within it. We do heavy data
        augmentation, but it's still just one real image.

        If using a goal image, we use a different random_sample method that
        also picks the LAST image of that episode, which is assigned as the
        goal image. This would likely not work for super long-horizon tasks,
        but maybe; (Agarwal et al., NeurIPS 2016) in the PokeBot paper
        actually got something like this 'greedy-style' planning to work.
        Otherwise we might have to do something like (Nair et al., ICRA 2017)
        in the follow-up work where we feed in a target image for each time
        step, which would be the *next* image saved.

        For data augmentation with this goal image, I believe we should stack
        the current and goal image together, and THEN do augmentation. The
        perturb method will make sure placing pixels are preserved -- which
        for short-horizon environments usually means the goal image will
        contain most of the relevant information. When data augmenting, for
        both normal and goal-conditioned Transporters, the p1_theta
        (rotation) is the same, but pick points are correctly 'converted' to
        those appropriate for the augmented images.
        
Train on dataset for a specific number of iterations.
Daniel: notice how little training data we use. One 'iteration' is
simply one image and an associated action, drawn by (a) sampling
demo, then (b) sampling time within it. We do heavy data
augmentation, but it's still just one real image.

If using a goal image, we use a different random_sample method that
also picks the LAST image of that episode, which is assigned as the
goal image. This would likely not work for super long-horizon tasks,
but maybe; (Agarwal et al., NeurIPS 2016) in the PokeBot paper
actually got something like this 'greedy-style' planning to work.
Otherwise we might have to do something like (Nair et al., ICRA 2017)
in the follow-up work where we feed in a target image for each time
step, which would be the *next* image saved.

For data augmentation with this goal image, I believe we should stack
the current and goal image together, and THEN do augmentation. The
perturb method will make sure placing pixels are preserved -- which
for short-horizon environments usually means the goal image will
contain most of the relevant information. When data augmenting, for
both normal and goal-conditioned Transporters, the p1_theta
(rotation) is the same, but pick points are correctly 'converted' to
those appropriate for the augmented images.
Train on dataset for a specific number of iterations . Daniel : notice how little training data we use . One ' iteration ' is simply one image and an associated action drawn by ( a ) sampling demo then ( b ) sampling time within it . We do heavy data augmentation but it ' s still just one real image . If using a goal image we use a different random_sample method that also picks the LAST image of that episode which is assigned as the goal image . This would likely not work for super long - horizon tasks but maybe ; ( Agarwal et al . NeurIPS 2016 ) in the PokeBot paper actually got something like this ' greedy - style ' planning to work . Otherwise we might have to do something like ( Nair et al . ICRA 2017 ) in the follow - up work where we feed in a target image for each time step which would be the * next * image saved . For data augmentation with this goal image I believe we should stack the current and goal image together and THEN do augmentation . The perturb method will make sure placing pixels are preserved -- which for short - horizon environments usually means the goal image will contain most of the relevant information . When data augmenting for both normal and goal - conditioned Transporters the p1_theta ( rotation ) is the same but pick points are correctly ' converted ' to those appropriate for the augmented images .
def train(self, dataset, num_iter, writer):
        for i in range(num_iter):
            if self.use_goal_image:
                obs, act, info, goal = dataset.random_sample(goal_images=True)
            else:
                obs, act, info = dataset.random_sample()
            configs = act['camera_config']
            colormap, heightmap = self.get_heightmap(obs, configs)
            if self.use_goal_image:
                colormap_g, heightmap_g = self.get_heightmap(goal, configs)
            pose0, pose1 = act['params']['pose0'], act['params']['pose1']
            p0_position, p0_rotation = pose0[0], pose0[1]
            p0 = utils.position_to_pixel(p0_position, self.bounds, self.pixel_size)
            p0_theta = -np.float32(p.getEulerFromQuaternion(p0_rotation)[2])
            p1_position, p1_rotation = pose1[0], pose1[1]
            p1 = utils.position_to_pixel(p1_position, self.bounds, self.pixel_size)
            p1_theta = -np.float32(p.getEulerFromQuaternion(p1_rotation)[2])
            p1_theta = p1_theta - p0_theta
            p0_theta = 0
            input_image = self.concatenate_c_h(colormap, heightmap)
            if self.use_goal_image:
                goal_image = self.concatenate_c_h(colormap_g, heightmap_g)
                input_image = np.concatenate((input_image, goal_image), axis=2)
                assert input_image.shape[2] == 12, input_image.shape
            original_pixels = (p0, p1)
            input_image, pixels = utils.perturb(input_image, [p0, p1])
            p0, p1 = pixels
            if False:
                self.visualize_images(p0, p0_theta, p1, p1_theta, original_pixels,
                        colormap=colormap, heightmap=heightmap,
                        colormap_g=colormap_g, heightmap_g=heightmap_g,
                        input_image=input_image, before_aug=False)
            if self.attn_no_targ and self.use_goal_image:
                maxdim = int(input_image.shape[2] / 2)
                input_only = input_image[:, :, :maxdim]
                loss0 = self.attention_model.train(input_only, p0, p0_theta)
            else:
                loss0 = self.attention_model.train(input_image, p0, p0_theta)
            with writer.as_default():
                tf.summary.scalar('attention_loss', self.attention_model.metric.result(),
                    step=self.total_iter+i)
            if isinstance(self.transport_model, Attention):
                loss1 = self.transport_model.train(input_image, p1, p1_theta)
            elif isinstance(self.transport_model, TransportGoal):
                half = int(input_image.shape[2] / 2)
                img_curr = input_image[:, :, :half]
                img_goal = input_image[:, :, half:]
                loss1 = self.transport_model.train(img_curr, img_goal, p0, p1, p1_theta)
            else:
                loss1 = self.transport_model.train(input_image, p0, p1, p1_theta)
            with writer.as_default():
                tf.summary.scalar('transport_loss', self.transport_model.metric.result(),
                    step=self.total_iter+i)
            print(f'Train Iter: {self.total_iter + i} Loss: {loss0:.4f} {loss1:.4f}')
        self.total_iter += num_iter
        self.save()
def train ( self , dataset , num_iter , writer ) : for i in range ( num_iter ) : if self . use_goal_image : obs , act , info , goal = dataset . random_sample ( goal_images = True ) else : obs , act , info = dataset . random_sample ( ) configs = act [ 'camera_config' ] colormap , heightmap = self . get_heightmap ( obs , configs ) if self . use_goal_image : colormap_g , heightmap_g = self . get_heightmap ( goal , configs ) pose0 , pose1 = act [ 'params' ] [ 'pose0' ] , act [ 'params' ] [ 'pose1' ] p0_position , p0_rotation = pose0 [ 0 ] , pose0 [ 1 ] p0 = utils . position_to_pixel ( p0_position , self . bounds , self . pixel_size ) p0_theta = - np . float32 ( p . getEulerFromQuaternion ( p0_rotation ) [ 2 ] ) p1_position , p1_rotation = pose1 [ 0 ] , pose1 [ 1 ] p1 = utils . position_to_pixel ( p1_position , self . bounds , self . pixel_size ) p1_theta = - np . float32 ( p . getEulerFromQuaternion ( p1_rotation ) [ 2 ] ) p1_theta = p1_theta - p0_theta p0_theta = 0 input_image = self . concatenate_c_h ( colormap , heightmap ) if self . use_goal_image : goal_image = self . concatenate_c_h ( colormap_g , heightmap_g ) input_image = np . concatenate ( ( input_image , goal_image ) , axis = 2 ) assert input_image . shape [ 2 ] == 12 , input_image . shape original_pixels = ( p0 , p1 ) input_image , pixels = utils . perturb ( input_image , [ p0 , p1 ] ) p0 , p1 = pixels if False : self . visualize_images ( p0 , p0_theta , p1 , p1_theta , original_pixels , colormap = colormap , heightmap = heightmap , colormap_g = colormap_g , heightmap_g = heightmap_g , input_image = input_image , before_aug = False ) if self . attn_no_targ and self . use_goal_image : maxdim = int ( input_image . shape [ 2 ] / 2 ) input_only = input_image [ : , : , : maxdim ] loss0 = self . attention_model . train ( input_only , p0 , p0_theta ) else : loss0 = self . attention_model . train ( input_image , p0 , p0_theta ) with writer . as_default ( ) : tf . summary . scalar ( 'attention_loss' , self . attention_model . metric . result ( ) , step = self . total_iter + i ) if isinstance ( self . transport_model , Attention ) : loss1 = self . transport_model . train ( input_image , p1 , p1_theta ) elif isinstance ( self . transport_model , TransportGoal ) : half = int ( input_image . shape [ 2 ] / 2 ) img_curr = input_image [ : , : , : half ] img_goal = input_image [ : , : , half : ] loss1 = self . transport_model . train ( img_curr , img_goal , p0 , p1 , p1_theta ) else : loss1 = self . transport_model . train ( input_image , p0 , p1 , p1_theta ) with writer . as_default ( ) : tf . summary . scalar ( 'transport_loss' , self . transport_model . metric . result ( ) , step = self . total_iter + i ) print ( f'Train Iter: {self.total_iter + i} Loss: {loss0:.4f} {loss1:.4f}' ) self . total_iter += num_iter self . save ( )
Train on dataset for a specific number of iterations.
Train on dataset for a specific number of iterations .
"""Train on dataset for a specific number of iterations.

        Daniel: notice how little training data we use! One 'iteration' is
        simply one image and an associated action, drawn by (a) sampling
        demo, then (b) sampling time within it. We do heavy data
        augmentation, but it's still just one real image.

        If using a goal image, we use a different random_sample method that
        also picks the LAST image of that episode, which is assigned as the
        goal image. This would likely not work for super long-horizon tasks,
        but maybe; (Agarwal et al., NeurIPS 2016) in the PokeBot paper
        actually got something like this 'greedy-style' planning to work.
        Otherwise we might have to do something like (Nair et al., ICRA 2017)
        in the follow-up work where we feed in a target image for each time
        step, which would be the *next* image saved.

        For data augmentation with this goal image, I believe we should stack
        the current and goal image together, and THEN do augmentation. The
        perturb method will make sure placing pixels are preserved -- which
        for short-horizon environments usually means the goal image will
        contain most of the relevant information. When data augmenting, for
        both normal and goal-conditioned Transporters, the p1_theta
        (rotation) is the same, but pick points are correctly 'converted' to
        those appropriate for the augmented images.
        """ # Get heightmap from RGB-D images. # Get training labels from data sample. # Concatenate color with depth images. # If using goal image, stack _with_ input_image for data augmentation. # Do data augmentation (perturb rotation and translation). # Optionally visualize images _after_ data agumentation. # Compute Attention training loss. # Compute Transport training loss.
self
dataset
num_iter
writer