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

        Daniel: not testing with validation, argument copied over from ravens. Naively,
        we can train with MSE, but better to use a mixture model (MDN) since the output
        should be multi-modal; could be several pick points, and several placing points
        wrt those pick points.

        Also, notice how one iteration involves taking a batch of data, whereas for
        Transporters, we only used one image per 'iteration'. This means that each gt-state
        model consumes 128x more data points during training.

        NOTE: there is a special case for num_iter=0 which I use for the "zero iteration" baseline.
        Without this, it is impossible to save the model because it hasn't been built yet. However,
        even with this I get the warning:

        WARNING:tensorflow:Skipping full serialization of Keras layer <tensorflow.python.keras.layers.core.Dense object at 0x7fc88c170890>, because it is not built.

        but the loading seems to be OK, so I'm not sure what could be the issue, and given that
        it's for the 0 iteration case, it's unlikely to be worth fully investigating for now.
        """
        if self.model is None:
            self.init_model(dataset)

        if self.USE_MDN:
            loss_criterion = mdn_utils.mdn_loss
        else:
            loss_criterion = tf.keras.losses.MeanSquaredError()

        @tf.function
        def train_step(model, batch_obs, batch_act, loss_criterion):
            with tf.GradientTape() as tape:
                prediction = model(batch_obs)
                loss = loss_criterion(batch_act, prediction)
                grad = tape.gradient(loss, model.trainable_variables)
                self.optim.apply_gradients(zip(grad, model.trainable_variables))
            return loss

        print_rate = 50
        VALIDATION_RATE = 1000

        # Need this case due to some quirks with saving this type of model. No gradient tupdates.
        if num_iter == 0:
            batch_obs, batch_act, obs, act, info = self.get_data_batch(dataset)
            print('Doing a single forward pass to enable us to save a snapshot for the num_iter == 0 case.')
            prediction = self.model(batch_obs)

        for i in range(num_iter):
            start = time.time()
            batch_obs, batch_act, obs, act, info = self.get_data_batch(dataset)

            # Forward through model, compute training loss, update weights.
            self.metric.reset_states()
            loss = train_step(self.model, batch_obs, batch_act, loss_criterion)
            self.metric(loss)
            with writer.as_default():
                tf.summary.scalar('gt_state_loss', self.metric.result(), step=self.total_iter+i)
            if i % print_rate == 0:
                loss = np.float32(loss)
                print(f'Train Iter: {self.total_iter + i} Loss: {loss:.4f} Iter time:', time.time() - start)

            # Compute valid loss only if we have a validation dataset.
            if ((self.total_iter + i) % VALIDATION_RATE == 0) and (validation_dataset is not None):
                print("Validating!")
                tf.keras.backend.set_learning_phase(0)
                self.val_metric.reset_states()
                batch_obs, batch_act, _, _, _ = self.get_data_batch(validation_dataset)
                prediction = self.model(batch_obs)
                loss = loss_criterion(batch_act, prediction)
                self.val_metric(loss)
                with writer.as_default():
                    tf.summary.scalar('validation_gt_state_loss', self.val_metric.result(),
                        step=self.total_iter+i)
                tf.keras.backend.set_learning_phase(1)

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

        Daniel: not testing with validation, argument copied over from ravens. Naively,
        we can train with MSE, but better to use a mixture model (MDN) since the output
        should be multi-modal; could be several pick points, and several placing points
        wrt those pick points.

        Also, notice how one iteration involves taking a batch of data, whereas for
        Transporters, we only used one image per 'iteration'. This means that each gt-state
        model consumes 128x more data points during training.

        NOTE: there is a special case for num_iter=0 which I use for the "zero iteration" baseline.
        Without this, it is impossible to save the model because it hasn't been built yet. However,
        even with this I get the warning:

        WARNING:tensorflow:Skipping full serialization of Keras layer <tensorflow.python.keras.layers.core.Dense object at 0x7fc88c170890>, because it is not built.

        but the loading seems to be OK, so I'm not sure what could be the issue, and given that
        it's for the 0 iteration case, it's unlikely to be worth fully investigating for now.
        
Train on dataset for a specific number of iterations.
Daniel: not testing with validation, argument copied over from ravens. Naively,
we can train with MSE, but better to use a mixture model (MDN) since the output
should be multi-modal; could be several pick points, and several placing points
wrt those pick points.

Also, notice how one iteration involves taking a batch of data, whereas for
Transporters, we only used one image per 'iteration'. This means that each gt-state
model consumes 128x more data points during training.

there is a special case for num_iter=0 which I use for the "zero iteration" baseline.
Without this, it is impossible to save the model because it hasn't been built yet. However,
even with this I get the warning.

Skipping full serialization of Keras layer , because it is not built.

but the loading seems to be OK, so I'm not sure what could be the issue, and given that
it's for the 0 iteration case, it's unlikely to be worth fully investigating for now.
Train on dataset for a specific number of iterations . Daniel : not testing with validation argument copied over from ravens . Naively we can train with MSE but better to use a mixture model ( MDN ) since the output should be multi - modal ; could be several pick points and several placing points wrt those pick points . Also notice how one iteration involves taking a batch of data whereas for Transporters we only used one image per ' iteration ' . This means that each gt - state model consumes 128x more data points during training . there is a special case for num_iter = 0 which I use for the " zero iteration " baseline . Without this it is impossible to save the model because it hasn ' t been built yet . However even with this I get the warning . Skipping full serialization of Keras layer because it is not built . but the loading seems to be OK so I ' m not sure what could be the issue and given that it ' s for the 0 iteration case it ' s unlikely to be worth fully investigating for now .
def train(self, dataset, num_iter, writer, validation_dataset=None):
        if self.model is None:
            self.init_model(dataset)
        if self.USE_MDN:
            loss_criterion = mdn_utils.mdn_loss
        else:
            loss_criterion = tf.keras.losses.MeanSquaredError()
        @tf.function
        def train_step(model, batch_obs, batch_act, loss_criterion):
            with tf.GradientTape() as tape:
                prediction = model(batch_obs)
                loss = loss_criterion(batch_act, prediction)
                grad = tape.gradient(loss, model.trainable_variables)
                self.optim.apply_gradients(zip(grad, model.trainable_variables))
            return loss
        print_rate = 50
        VALIDATION_RATE = 1000
        if num_iter == 0:
            batch_obs, batch_act, obs, act, info = self.get_data_batch(dataset)
            print('Doing a single forward pass to enable us to save a snapshot for the num_iter == 0 case.')
            prediction = self.model(batch_obs)
        for i in range(num_iter):
            start = time.time()
            batch_obs, batch_act, obs, act, info = self.get_data_batch(dataset)
            self.metric.reset_states()
            loss = train_step(self.model, batch_obs, batch_act, loss_criterion)
            self.metric(loss)
            with writer.as_default():
                tf.summary.scalar('gt_state_loss', self.metric.result(), step=self.total_iter+i)
            if i % print_rate == 0:
                loss = np.float32(loss)
                print(f'Train Iter: {self.total_iter + i} Loss: {loss:.4f} Iter time:', time.time() - start)
            if ((self.total_iter + i) % VALIDATION_RATE == 0) and (validation_dataset is not None):
                print("Validating!")
                tf.keras.backend.set_learning_phase(0)
                self.val_metric.reset_states()
                batch_obs, batch_act, _, _, _ = self.get_data_batch(validation_dataset)
                prediction = self.model(batch_obs)
                loss = loss_criterion(batch_act, prediction)
                self.val_metric(loss)
                with writer.as_default():
                    tf.summary.scalar('validation_gt_state_loss', self.val_metric.result(),
                        step=self.total_iter+i)
                tf.keras.backend.set_learning_phase(1)
        self.total_iter += num_iter
        self.save()
def train ( self , dataset , num_iter , writer , validation_dataset = None ) : if self . model is None : self . init_model ( dataset ) if self . USE_MDN : loss_criterion = mdn_utils . mdn_loss else : loss_criterion = tf . keras . losses . MeanSquaredError ( ) @ tf . function def train_step ( model , batch_obs , batch_act , loss_criterion ) : with tf . GradientTape ( ) as tape : prediction = model ( batch_obs ) loss = loss_criterion ( batch_act , prediction ) grad = tape . gradient ( loss , model . trainable_variables ) self . optim . apply_gradients ( zip ( grad , model . trainable_variables ) ) return loss print_rate = 50 VALIDATION_RATE = 1000 if num_iter == 0 : batch_obs , batch_act , obs , act , info = self . get_data_batch ( dataset ) print ( 'Doing a single forward pass to enable us to save a snapshot for the num_iter == 0 case.' ) prediction = self . model ( batch_obs ) for i in range ( num_iter ) : start = time . time ( ) batch_obs , batch_act , obs , act , info = self . get_data_batch ( dataset ) self . metric . reset_states ( ) loss = train_step ( self . model , batch_obs , batch_act , loss_criterion ) self . metric ( loss ) with writer . as_default ( ) : tf . summary . scalar ( 'gt_state_loss' , self . metric . result ( ) , step = self . total_iter + i ) if i % print_rate == 0 : loss = np . float32 ( loss ) print ( f'Train Iter: {self.total_iter + i} Loss: {loss:.4f} Iter time:' , time . time ( ) - start ) if ( ( self . total_iter + i ) % VALIDATION_RATE == 0 ) and ( validation_dataset is not None ) : print ( "Validating!" ) tf . keras . backend . set_learning_phase ( 0 ) self . val_metric . reset_states ( ) batch_obs , batch_act , _ , _ , _ = self . get_data_batch ( validation_dataset ) prediction = self . model ( batch_obs ) loss = loss_criterion ( batch_act , prediction ) self . val_metric ( loss ) with writer . as_default ( ) : tf . summary . scalar ( 'validation_gt_state_loss' , self . val_metric . result ( ) , step = self . total_iter + i ) tf . keras . backend . set_learning_phase ( 1 ) 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: not testing with validation, argument copied over from ravens. Naively,
        we can train with MSE, but better to use a mixture model (MDN) since the output
        should be multi-modal; could be several pick points, and several placing points
        wrt those pick points.

        Also, notice how one iteration involves taking a batch of data, whereas for
        Transporters, we only used one image per 'iteration'. This means that each gt-state
        model consumes 128x more data points during training.

        NOTE: there is a special case for num_iter=0 which I use for the "zero iteration" baseline.
        Without this, it is impossible to save the model because it hasn't been built yet. However,
        even with this I get the warning:

        WARNING:tensorflow:Skipping full serialization of Keras layer <tensorflow.python.keras.layers.core.Dense object at 0x7fc88c170890>, because it is not built.

        but the loading seems to be OK, so I'm not sure what could be the issue, and given that
        it's for the 0 iteration case, it's unlikely to be worth fully investigating for now.
        """ # Need this case due to some quirks with saving this type of model. No gradient tupdates. # Forward through model, compute training loss, update weights. # Compute valid loss only if we have a validation dataset.
self
dataset
num_iter
writer
validation_dataset