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

        @tf.function
        def pick_train_step(model, optim, in_tensor, yxtheta, loss_criterion):
            with tf.GradientTape() as tape:
                output = model(in_tensor)
                loss = loss_criterion(yxtheta, output)
            grad = tape.gradient(loss, model.trainable_variables)
            optim.apply_gradients(
                zip(grad, model.trainable_variables))
            return loss

        @tf.function
        def place_train_step(model, optim, in_tensor, yxtheta, loss_criterion):
            with tf.GradientTape() as tape:
                output = model(in_tensor)
                loss = loss_criterion(yxtheta, output)
            grad = tape.gradient(loss, model.trainable_variables)
            optim.apply_gradients(
                zip(grad, model.trainable_variables))
            return loss

        for i in range(num_iter):
            start = time.time()


            input_images, p0s, p0_thetas = [], [], []
            p1s, p1_thetas = [], []
            for _ in range(self.batch_size):
                obs, act, info = dataset.random_sample()

                # Get heightmap from RGB-D images.
                configs = act['camera_config']
                colormap, heightmap = self.get_heightmap(obs, configs)
                #self.show_images(colormap, heightmap)

                # Get training labels from data sample.

                # (spatially distributed on object) get actions from oracle distribution
                #pose0, pose1 = act['params']['pose0'], act['params']['pose1']

                # (identical object location) get actions from object poses
                l_object = info[4]
                pose0 = l_object[0], l_object[1]
                l_target = info[5]
                pose1 = l_target[0], l_target[1]

                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])

                # to make it relative
                # p1_theta = p1_theta - p0_theta
                # p0_theta = 0

                p1_xytheta = np.array([p1_position[0], p1_position[1], p1_theta])

                # Concatenate color with depth images.
                # input_image = np.concatenate((colormap,
                #                               heightmap[..., None],
                #                               heightmap[..., None],
                #                               heightmap[..., None]), axis=2)

                input_image = colormap

                input_images.append(input_image)
                p0s.append(p0)
                p0_thetas.append(p0_theta)
                p1s.append(p1)
                p1_thetas.append(p1_theta)

            input_image = np.array(input_images)
            p0 = np.array(p0s)
            p0_theta = np.array(p0_thetas)
            p1 = np.array(p1s)
            p1_theta = np.array(p1_thetas)

            # Compute train loss - regression place
            loss0 = self.pick_regression_model.train_pick(input_image, p0, p0_theta, pick_train_step)
            with writer.as_default():
                tf.summary.scalar('pick_loss', self.pick_regression_model.metric.result(),
                    step=self.total_iter+i)

            # Compute train loss - regression place
            loss1 = self.place_regression_model.train_pick(input_image, p1, p1_theta, place_train_step)
            with writer.as_default():
                tf.summary.scalar('place_loss', self.place_regression_model.metric.result(),
                    step=self.total_iter+i)

            #loss1 = 0.0
            print(f'Train Iter: {self.total_iter + i} Loss: {loss0:.4f} {loss1:.4f} Iter time:', time.time() - start)

        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 .
def train(self, dataset, num_iter, writer):
        @tf.function
        def pick_train_step(model, optim, in_tensor, yxtheta, loss_criterion):
            with tf.GradientTape() as tape:
                output = model(in_tensor)
                loss = loss_criterion(yxtheta, output)
            grad = tape.gradient(loss, model.trainable_variables)
            optim.apply_gradients(
                zip(grad, model.trainable_variables))
            return loss
        @tf.function
        def place_train_step(model, optim, in_tensor, yxtheta, loss_criterion):
            with tf.GradientTape() as tape:
                output = model(in_tensor)
                loss = loss_criterion(yxtheta, output)
            grad = tape.gradient(loss, model.trainable_variables)
            optim.apply_gradients(
                zip(grad, model.trainable_variables))
            return loss
        for i in range(num_iter):
            start = time.time()
            input_images, p0s, p0_thetas = [], [], []
            p1s, p1_thetas = [], []
            for _ in range(self.batch_size):
                obs, act, info = dataset.random_sample()
                configs = act['camera_config']
                colormap, heightmap = self.get_heightmap(obs, configs)
                l_object = info[4]
                pose0 = l_object[0], l_object[1]
                l_target = info[5]
                pose1 = l_target[0], l_target[1]
                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_xytheta = np.array([p1_position[0], p1_position[1], p1_theta])
                input_image = colormap
                input_images.append(input_image)
                p0s.append(p0)
                p0_thetas.append(p0_theta)
                p1s.append(p1)
                p1_thetas.append(p1_theta)
            input_image = np.array(input_images)
            p0 = np.array(p0s)
            p0_theta = np.array(p0_thetas)
            p1 = np.array(p1s)
            p1_theta = np.array(p1_thetas)
            loss0 = self.pick_regression_model.train_pick(input_image, p0, p0_theta, pick_train_step)
            with writer.as_default():
                tf.summary.scalar('pick_loss', self.pick_regression_model.metric.result(),
                    step=self.total_iter+i)
            loss1 = self.place_regression_model.train_pick(input_image, p1, p1_theta, place_train_step)
            with writer.as_default():
                tf.summary.scalar('place_loss', self.place_regression_model.metric.result(),
                    step=self.total_iter+i)
            print(f'Train Iter: {self.total_iter + i} Loss: {loss0:.4f} {loss1:.4f} Iter time:', time.time() - start)
        self.total_iter += num_iter
        self.save()
def train ( self , dataset , num_iter , writer ) : @ tf . function def pick_train_step ( model , optim , in_tensor , yxtheta , loss_criterion ) : with tf . GradientTape ( ) as tape : output = model ( in_tensor ) loss = loss_criterion ( yxtheta , output ) grad = tape . gradient ( loss , model . trainable_variables ) optim . apply_gradients ( zip ( grad , model . trainable_variables ) ) return loss @ tf . function def place_train_step ( model , optim , in_tensor , yxtheta , loss_criterion ) : with tf . GradientTape ( ) as tape : output = model ( in_tensor ) loss = loss_criterion ( yxtheta , output ) grad = tape . gradient ( loss , model . trainable_variables ) optim . apply_gradients ( zip ( grad , model . trainable_variables ) ) return loss for i in range ( num_iter ) : start = time . time ( ) input_images , p0s , p0_thetas = [ ] , [ ] , [ ] p1s , p1_thetas = [ ] , [ ] for _ in range ( self . batch_size ) : obs , act , info = dataset . random_sample ( ) configs = act [ 'camera_config' ] colormap , heightmap = self . get_heightmap ( obs , configs ) l_object = info [ 4 ] pose0 = l_object [ 0 ] , l_object [ 1 ] l_target = info [ 5 ] pose1 = l_target [ 0 ] , l_target [ 1 ] 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_xytheta = np . array ( [ p1_position [ 0 ] , p1_position [ 1 ] , p1_theta ] ) input_image = colormap input_images . append ( input_image ) p0s . append ( p0 ) p0_thetas . append ( p0_theta ) p1s . append ( p1 ) p1_thetas . append ( p1_theta ) input_image = np . array ( input_images ) p0 = np . array ( p0s ) p0_theta = np . array ( p0_thetas ) p1 = np . array ( p1s ) p1_theta = np . array ( p1_thetas ) loss0 = self . pick_regression_model . train_pick ( input_image , p0 , p0_theta , pick_train_step ) with writer . as_default ( ) : tf . summary . scalar ( 'pick_loss' , self . pick_regression_model . metric . result ( ) , step = self . total_iter + i ) loss1 = self . place_regression_model . train_pick ( input_image , p1 , p1_theta , place_train_step ) with writer . as_default ( ) : tf . summary . scalar ( 'place_loss' , self . place_regression_model . metric . result ( ) , step = self . total_iter + i ) print ( f'Train Iter: {self.total_iter + i} Loss: {loss0:.4f} {loss1:.4f} Iter time:' , time . time ( ) - start ) 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.""" # Get heightmap from RGB-D images. #self.show_images(colormap, heightmap) # Get training labels from data sample. # (spatially distributed on object) get actions from oracle distribution #pose0, pose1 = act['params']['pose0'], act['params']['pose1'] # (identical object location) get actions from object poses # to make it relative # p1_theta = p1_theta - p0_theta # p0_theta = 0 # Concatenate color with depth images. # input_image = np.concatenate((colormap, #                               heightmap[..., None], #                               heightmap[..., None], #                               heightmap[..., None]), axis=2) # Compute train loss - regression place # Compute train loss - regression place #loss1 = 0.0
self
dataset
num_iter
writer