def testCalculateReceptiveBoxes(self): boxes = feature_extractor.CalculateReceptiveBoxes( height=1, width=2, rf=291, stride=32, padding=145) exp_boxes = [[-145., -145., 145., 145.], [-145., -113., 145., 177.]] with self.test_session() as sess: boxes_out = sess.run(boxes) self.assertAllEqual(exp_boxes, boxes_out)
def testCalculateReceptiveBoxes(self): boxes = feature_extractor.CalculateReceptiveBoxes(height=1, width=2, rf=291, stride=32, padding=145) exp_boxes = [[-145., -145., 145., 145.], [-145., -113., 145., 177.]] self.assertAllEqual(exp_boxes, boxes)
def _ProcessSingleScale(scale_index, boxes, features, scales, scores): """Resizes the image and run feature extraction and keypoint selection. This function will be passed into tf.while_loop() and be called repeatedly. The input boxes are collected from the previous iteration [0: scale_index -1]. We get the current scale by image_scales[scale_index], and run resize image, feature extraction and keypoint selection. Then we will get a new set of selected_boxes for current scale. In the end, we concat the previous boxes with current selected_boxes as the output. Args: scale_index: A valid index in the image_scales. boxes: Box tensor with the shape of [N, 4]. features: Feature tensor with the shape of [N, depth]. scales: Scale tensor with the shape of [N]. scores: Attention score tensor with the shape of [N]. Returns: scale_index: The next scale index for processing. boxes: Concatenated box tensor with the shape of [K, 4]. K >= N. features: Concatenated feature tensor with the shape of [K, depth]. scales: Concatenated scale tensor with the shape of [K]. scores: Concatenated score tensor with the shape of [K]. """ scale = tf.gather(image_scales, scale_index) new_image_size = tf.dtypes.cast( tf.round(original_image_shape_float * scale), tf.int32) resized_image = tf.image.resize(image_tensor, new_image_size) attention_prob, feature_map = attention_model_fn(resized_image) attention_prob = tf.squeeze(attention_prob, axis=[0]) feature_map = tf.squeeze(feature_map, axis=[0]) rf_boxes = feature_extractor.CalculateReceptiveBoxes( tf.shape(feature_map)[0], tf.shape(feature_map)[1], rf, stride, padding) # Re-project back to the original image space. rf_boxes = tf.divide(rf_boxes, scale) attention_prob = tf.reshape(attention_prob, [-1]) feature_map = tf.reshape(feature_map, [-1, feature_depth]) # Use attention score to select feature vectors. indices = tf.reshape(tf.where(attention_prob >= abs_thres), [-1]) selected_boxes = tf.gather(rf_boxes, indices) selected_features = tf.gather(feature_map, indices) selected_scores = tf.gather(attention_prob, indices) selected_scales = tf.ones_like(selected_scores, tf.float32) / scale # Concat with the previous result from different scales. boxes = tf.concat([boxes, selected_boxes], 0) features = tf.concat([features, selected_features], 0) scales = tf.concat([scales, selected_scales], 0) scores = tf.concat([scores, selected_scores], 0) return scale_index + 1, boxes, features, scales, scores
def _ResizeAndExtract(scale_index): """Helper function to resize image then extract features. Args: scale_index: A valid index in image_scales. Returns: global_descriptor: [1,D] tensor denoting the extracted global descriptor. boxes: Box tensor with the shape of [K, 4]. local_descriptors: Local descriptor tensor with the shape of [K, depth]. scales: Scale tensor with the shape of [K]. scores: Score tensor with the shape of [K]. """ scale = tf.gather(image_scales, scale_index) new_image_size = tf.dtypes.cast( tf.round(original_image_shape_float * scale), tf.int32) resized_image = tf.image.resize(image_tensor, new_image_size) global_descriptor, attention_prob, feature_map = model_fn( resized_image) attention_prob = tf.squeeze(attention_prob, axis=[0]) feature_map = tf.squeeze(feature_map, axis=[0]) # Compute RF boxes and re-project them to the original image space. rf_boxes = feature_extractor.CalculateReceptiveBoxes( tf.shape(feature_map)[0], tf.shape(feature_map)[1], rf, stride, padding) rf_boxes = tf.divide(rf_boxes, scale) attention_prob = tf.reshape(attention_prob, [-1]) feature_map = tf.reshape(feature_map, [-1, tf.shape(feature_map)[2]]) # Use attention score to select local features. indices = tf.reshape(tf.where(attention_prob >= abs_thres), [-1]) boxes = tf.gather(rf_boxes, indices) local_descriptors = tf.gather(feature_map, indices) scores = tf.gather(attention_prob, indices) scales = tf.ones_like(scores, tf.float32) / scale return global_descriptor, boxes, local_descriptors, scales, scores