ravens/models/transport.py
def train(self, in_img, p, q, theta):
        """Transport pixel p to pixel q.

          Args:
            input:
            depth_image:
            p: pixel (y, x)
            q: pixel (y, x)
          Returns:
            A `Tensor`. Has the same type as `input`.

        Daniel: the `in_img` will include the color and depth. Much is
        similar to the attention model if we're not using the per-pixel loss:
        (a) forward pass, (b) get angle discretizations [though we set only 1
        rotation for the picking model], (c) make the label consider
        rotations in the last axis, but only provide the label to one single
        (pixel,rotation) combination, (d) follow same exact steps for the
        non-per pixel loss otherwise. The output reshaping to (1, ...) is
        done in the attention model forward pass, but not in the transport
        forward pass. Note the `1` meaning a batch size of 1.
        """
        self.metric.reset_states()
        with tf.GradientTape() as tape:
            output = self.forward(in_img, p, apply_softmax=False)

            itheta = theta / (2 * np.pi / self.num_rotations)
            itheta = np.int32(np.round(itheta)) % self.num_rotations

            label_size = in_img.shape[:2] + (self.num_rotations,)
            label = np.zeros(label_size)
            label[q[0], q[1], itheta] = 1

            if self.per_pixel_loss:
                sampling = True  # sampling negatives seems to converge faster
                if sampling:
                    num_samples = 100
                    inegative = utils.sample_distribution(1 - label, num_samples)
                    inegative = [np.ravel_multi_index(i, label.shape) for i in inegative]
                    ipositive = np.ravel_multi_index([q[0], q[1], itheta], label.shape)
                    output = tf.reshape(output, (-1, 2))
                    output_samples = ()
                    for i in inegative:
                        output_samples += (tf.reshape(output[i, :], (1, 2)),)
                    output_samples += (tf.reshape(output[ipositive, :], (1, 2)),)
                    output = tf.concat(output_samples, axis=0)
                    label = np.int32([0] * num_samples + [1])[..., None]
                    label = np.hstack((1 - label, label))
                    weights = np.ones(label.shape[0])
                    weights[:num_samples] = 1./num_samples
                    weights = weights / np.sum(weights)

                else:
                    ipositive = np.ravel_multi_index([q[0], q[1], itheta], label.shape)
                    output = tf.reshape(output, (-1, 2))
                    label = np.int32(np.reshape(label, (int(np.prod(label.shape)), 1)))
                    label = np.hstack((1 - label, label))
                    weights = np.ones(label.shape[0]) * 0.0025  # magic constant
                    weights[ipositive] = 1

                label = tf.convert_to_tensor(label, dtype=tf.int32)
                weights = tf.convert_to_tensor(weights, dtype=tf.float32)
                loss = tf.nn.softmax_cross_entropy_with_logits(label, output)
                loss = tf.reduce_mean(loss * weights)

            else:
                label = label.reshape(1, np.prod(label.shape))
                label = tf.convert_to_tensor(label, dtype=tf.float32)
                output = tf.reshape(output, (1, np.prod(output.shape)))
                loss = tf.nn.softmax_cross_entropy_with_logits(label, output)
                loss = tf.reduce_mean(loss)

        grad = tape.gradient(loss, self.model.trainable_variables)
        self.optim.apply_gradients(zip(grad, self.model.trainable_variables))

        self.metric(loss)

        return np.float32(loss)
Transport pixel p to pixel q.

          Args:
            input:
            depth_image:
            p: pixel (y, x)
            q: pixel (y, x)
          Returns:
            A `Tensor`. Has the same type as `input`.

        Daniel: the `in_img` will include the color and depth. Much is
        similar to the attention model if we're not using the per-pixel loss:
        (a) forward pass, (b) get angle discretizations [though we set only 1
        rotation for the picking model], (c) make the label consider
        rotations in the last axis, but only provide the label to one single
        (pixel,rotation) combination, (d) follow same exact steps for the
        non-per pixel loss otherwise. The output reshaping to (1, ...) is
        done in the attention model forward pass, but not in the transport
        forward pass. Note the `1` meaning a batch size of 1.
        
Transport pixel p to pixel q.

the `in_img` will include the color and depth. Much is
similar to the attention model if we're not using the per-pixel loss:
(a) forward pass, (b) get angle discretizations [though we set only 1
rotation for the picking model], (c) make the label consider
rotations in the last axis, but only provide the label to one single
(pixel,rotation) combination, (d) follow same exact steps for the
non-per pixel loss otherwise. The output reshaping to (1, ...) is
done in the attention model forward pass, but not in the transport
forward pass. Note the `1` meaning a batch size of 1.
Transport pixel p to pixel q . the ` in_img ` will include the color and depth . Much is similar to the attention model if we ' re not using the per - pixel loss : ( a ) forward pass ( b ) get angle discretizations [ though we set only 1 rotation for the picking model ] ( c ) make the label consider rotations in the last axis but only provide the label to one single ( pixel rotation ) combination ( d ) follow same exact steps for the non - per pixel loss otherwise . The output reshaping to ( 1 ... ) is done in the attention model forward pass but not in the transport forward pass . Note the ` 1 ` meaning a batch size of 1 .
def train(self, in_img, p, q, theta):
        self.metric.reset_states()
        with tf.GradientTape() as tape:
            output = self.forward(in_img, p, apply_softmax=False)
            itheta = theta / (2 * np.pi / self.num_rotations)
            itheta = np.int32(np.round(itheta)) % self.num_rotations
            label_size = in_img.shape[:2] + (self.num_rotations,)
            label = np.zeros(label_size)
            label[q[0], q[1], itheta] = 1
            if self.per_pixel_loss:
                sampling = True  
                if sampling:
                    num_samples = 100
                    inegative = utils.sample_distribution(1 - label, num_samples)
                    inegative = [np.ravel_multi_index(i, label.shape) for i in inegative]
                    ipositive = np.ravel_multi_index([q[0], q[1], itheta], label.shape)
                    output = tf.reshape(output, (-1, 2))
                    output_samples = ()
                    for i in inegative:
                        output_samples += (tf.reshape(output[i, :], (1, 2)),)
                    output_samples += (tf.reshape(output[ipositive, :], (1, 2)),)
                    output = tf.concat(output_samples, axis=0)
                    label = np.int32([0] * num_samples + [1])[..., None]
                    label = np.hstack((1 - label, label))
                    weights = np.ones(label.shape[0])
                    weights[:num_samples] = 1./num_samples
                    weights = weights / np.sum(weights)
                else:
                    ipositive = np.ravel_multi_index([q[0], q[1], itheta], label.shape)
                    output = tf.reshape(output, (-1, 2))
                    label = np.int32(np.reshape(label, (int(np.prod(label.shape)), 1)))
                    label = np.hstack((1 - label, label))
                    weights = np.ones(label.shape[0]) * 0.0025  
                    weights[ipositive] = 1
                label = tf.convert_to_tensor(label, dtype=tf.int32)
                weights = tf.convert_to_tensor(weights, dtype=tf.float32)
                loss = tf.nn.softmax_cross_entropy_with_logits(label, output)
                loss = tf.reduce_mean(loss * weights)
            else:
                label = label.reshape(1, np.prod(label.shape))
                label = tf.convert_to_tensor(label, dtype=tf.float32)
                output = tf.reshape(output, (1, np.prod(output.shape)))
                loss = tf.nn.softmax_cross_entropy_with_logits(label, output)
                loss = tf.reduce_mean(loss)
        grad = tape.gradient(loss, self.model.trainable_variables)
        self.optim.apply_gradients(zip(grad, self.model.trainable_variables))
        self.metric(loss)
        return np.float32(loss)
def train ( self , in_img , p , q , theta ) : self . metric . reset_states ( ) with tf . GradientTape ( ) as tape : output = self . forward ( in_img , p , apply_softmax = False ) itheta = theta / ( 2 * np . pi / self . num_rotations ) itheta = np . int32 ( np . round ( itheta ) ) % self . num_rotations label_size = in_img . shape [ : 2 ] + ( self . num_rotations , ) label = np . zeros ( label_size ) label [ q [ 0 ] , q [ 1 ] , itheta ] = 1 if self . per_pixel_loss : sampling = True if sampling : num_samples = 100 inegative = utils . sample_distribution ( 1 - label , num_samples ) inegative = [ np . ravel_multi_index ( i , label . shape ) for i in inegative ] ipositive = np . ravel_multi_index ( [ q [ 0 ] , q [ 1 ] , itheta ] , label . shape ) output = tf . reshape ( output , ( - 1 , 2 ) ) output_samples = ( ) for i in inegative : output_samples += ( tf . reshape ( output [ i , : ] , ( 1 , 2 ) ) , ) output_samples += ( tf . reshape ( output [ ipositive , : ] , ( 1 , 2 ) ) , ) output = tf . concat ( output_samples , axis = 0 ) label = np . int32 ( [ 0 ] * num_samples + [ 1 ] ) [ ... , None ] label = np . hstack ( ( 1 - label , label ) ) weights = np . ones ( label . shape [ 0 ] ) weights [ : num_samples ] = 1. / num_samples weights = weights / np . sum ( weights ) else : ipositive = np . ravel_multi_index ( [ q [ 0 ] , q [ 1 ] , itheta ] , label . shape ) output = tf . reshape ( output , ( - 1 , 2 ) ) label = np . int32 ( np . reshape ( label , ( int ( np . prod ( label . shape ) ) , 1 ) ) ) label = np . hstack ( ( 1 - label , label ) ) weights = np . ones ( label . shape [ 0 ] ) * 0.0025 weights [ ipositive ] = 1 label = tf . convert_to_tensor ( label , dtype = tf . int32 ) weights = tf . convert_to_tensor ( weights , dtype = tf . float32 ) loss = tf . nn . softmax_cross_entropy_with_logits ( label , output ) loss = tf . reduce_mean ( loss * weights ) else : label = label . reshape ( 1 , np . prod ( label . shape ) ) label = tf . convert_to_tensor ( label , dtype = tf . float32 ) output = tf . reshape ( output , ( 1 , np . prod ( output . shape ) ) ) loss = tf . nn . softmax_cross_entropy_with_logits ( label , output ) loss = tf . reduce_mean ( loss ) grad = tape . gradient ( loss , self . model . trainable_variables ) self . optim . apply_gradients ( zip ( grad , self . model . trainable_variables ) ) self . metric ( loss ) return np . float32 ( loss )
Transport pixel p to pixel q.
Args:
input:
depth_image:
p: pixel (y, x)
q: pixel (y, x)
Returns:
A `Tensor`.
Transport pixel p to pixel q . Args : input : depth_image : p : pixel ( y x ) q : pixel ( y x ) Returns : A ` Tensor ` .
"""Transport pixel p to pixel q.

          Args:
            input:
            depth_image:
            p: pixel (y, x)
            q: pixel (y, x)
          Returns:
            A `Tensor`. Has the same type as `input`.

        Daniel: the `in_img` will include the color and depth. Much is
        similar to the attention model if we're not using the per-pixel loss:
        (a) forward pass, (b) get angle discretizations [though we set only 1
        rotation for the picking model], (c) make the label consider
        rotations in the last axis, but only provide the label to one single
        (pixel,rotation) combination, (d) follow same exact steps for the
        non-per pixel loss otherwise. The output reshaping to (1, ...) is
        done in the attention model forward pass, but not in the transport
        forward pass. Note the `1` meaning a batch size of 1.
        """ # sampling negatives seems to converge faster # magic constant
self
in_img
p
q
theta