Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tf compatibility problems #66

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions libs/box_utils/boxes_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import tensorflow as tf
from libs.box_utils.coordinate_convert import forward_convert

tf_major_ver = int(tf.__version__.split(".")[0])
tf_minor_ver = int(tf.__version__.split(".")[1])

def clip_boxes_to_img_boundaries(decode_boxes, img_shape):
'''
Expand Down Expand Up @@ -88,10 +90,12 @@ def padd_boxes_with_zeros(boxes, scores, max_num_of_boxes):

zero_boxes = tf.zeros(shape=[pad_num, 4], dtype=boxes.dtype)
zero_scores = tf.zeros(shape=[pad_num], dtype=scores.dtype)

final_boxes = tf.concat([boxes, zero_boxes], axis=0)

final_scores = tf.concat([scores, zero_scores], axis=0)
if(tf_major_ver<1):
final_boxes = tf.concat([boxes, zero_boxes], concat_dim=0)
final_scores = tf.concat([scores, zero_scores], concat_dim=0)
else:
final_boxes = tf.concat([boxes, zero_boxes], axis=0)
final_scores = tf.concat([scores, zero_scores], axis=0)

return final_boxes, final_scores

Expand Down
30 changes: 23 additions & 7 deletions libs/box_utils/nms_rotate.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
if cfgs.ROTATE_NMS_USE_GPU:
from libs.box_utils.rotate_polygon_nms import rotate_gpu_nms

tf_major_ver = int(tf.__version__.split(".")[0])
tf_minor_ver = int(tf.__version__.split(".")[1])

def nms_rotate(decode_boxes, scores, iou_threshold, max_output_size,
use_angle_condition=False, angle_threshold=0, use_gpu=True, gpu_id=0):
Expand All @@ -29,11 +31,16 @@ def nms_rotate(decode_boxes, scores, iou_threshold, max_output_size,
angle_gap_threshold=angle_threshold,
use_angle_condition=use_angle_condition,
device_id=gpu_id)

keep = tf.cond(
tf.greater(tf.shape(keep)[0], max_output_size),
true_fn=lambda: tf.slice(keep, [0], [max_output_size]),
false_fn=lambda: keep)
if(tf_major_ver==1 and tf_minor_ver<2) or (tf_major_ver==0):
keep = tf.cond(
tf.greater(tf.shape(keep)[0], max_output_size),
fn1=lambda: tf.slice(keep, [0], [max_output_size]),
fn2=lambda: keep)
else:
keep = tf.cond(
tf.greater(tf.shape(keep)[0], max_output_size),
true_fn=lambda: tf.slice(keep, [0], [max_output_size]),
false_fn=lambda: keep)

else:
keep = tf.py_func(nms_rotate_cpu,
Expand Down Expand Up @@ -87,15 +94,24 @@ def nms_rotate_gpu(boxes_list, scores, iou_threshold, use_angle_condition=False,
if use_angle_condition:
y_c, x_c, h, w, theta = tf.unstack(boxes_list, axis=1)
boxes_list = tf.transpose(tf.stack([x_c, y_c, w, h, theta]))
det_tensor = tf.concat([boxes_list, tf.expand_dims(scores, axis=1)], axis=1)
if(tf_major_ver<1):
det_tensor = tf.concat([boxes_list, tf.expand_dims(scores, axis=1)], concat_dim=1)
else:
det_tensor = tf.concat([boxes_list, tf.expand_dims(scores, axis=1)], axis=1)


keep = tf.py_func(rotate_gpu_nms,
inp=[det_tensor, iou_threshold, device_id],
Tout=tf.int64)
return keep
else:
y_c, x_c, h, w, theta = tf.unstack(boxes_list, axis=1)
boxes_list = tf.transpose(tf.stack([x_c, y_c, w, h, theta]))
det_tensor = tf.concat([boxes_list, tf.expand_dims(scores, axis=1)], axis=1)
if(tf_major_ver<1):
det_tensor = tf.concat([boxes_list, tf.expand_dims(scores, axis=1)], concat_dim=1)
else:
det_tensor = tf.concat([boxes_list, tf.expand_dims(scores, axis=1)], axis=1)

keep = tf.py_func(rotate_gpu_nms,
inp=[det_tensor, iou_threshold, device_id],
Tout=tf.int64)
Expand Down
35 changes: 25 additions & 10 deletions libs/fast_rcnn/build_fast_rcnn.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
import numpy as np

DEBUG = False

tf_major_ver = int(tf.__version__.split(".")[0])
tf_minor_ver = int(tf.__version__.split(".")[1])

class FastRCNN(object):
def __init__(self,
Expand Down Expand Up @@ -133,9 +134,12 @@ def get_rois(self):
[self.roi_pool_kernel_size, self.roi_pool_kernel_size],
stride=self.roi_pool_kernel_size)
all_level_roi_list.append(level_i_rois)

all_level_rois = tf.concat(all_level_roi_list, axis=0)
all_level_proposals = tf.concat(all_level_proposal_list, axis=0)
if(tf_major_ver<1):
all_level_rois = tf.concat(all_level_roi_list, concat_dim=0)
all_level_proposals = tf.concat(all_level_proposal_list, concat_dim=0)
else:
all_level_rois = tf.concat(all_level_roi_list, axis=0)
all_level_proposals = tf.concat(all_level_proposal_list, axis=0)

return all_level_rois, all_level_proposals

Expand Down Expand Up @@ -219,8 +223,11 @@ def fast_rcnn_minibatch(self, reference_boxes):

negative_indices = tf.random_shuffle(negative_indices)
negative_indices = tf.slice(negative_indices, begin=[0], size=[num_of_negatives])

minibatch_indices = tf.concat([positive_indices, negative_indices], axis=0)

if(tf_major_ver<1):
minibatch_indices = tf.concat([positive_indices, negative_indices], concat_dim=0)
else:
minibatch_indices = tf.concat([positive_indices, negative_indices], axis=0)
minibatch_indices = tf.random_shuffle(minibatch_indices)

minibatch_reference_boxes_mattached_gtboxes = tf.gather(reference_boxes_mattached_gtboxes,
Expand Down Expand Up @@ -259,7 +266,10 @@ def fast_rcnn_loss(self):
tmp_class_weights = tf.ones(shape=[tf.shape(minibatch_encode_boxes)[0], 4], dtype=tf.float32)
tmp_class_weights = tmp_class_weights * tf.expand_dims(category_list[i], axis=1)
class_weights_list.append(tmp_class_weights)
class_weights = tf.concat(class_weights_list, axis=1) # [minibatch_size, num_classes*4]
if(tf_major_ver<1):
class_weights = tf.concat(class_weights_list, concat_dim=1) # [minibatch_size, num_classes*4]
else:
class_weights = tf.concat(class_weights_list, axis=1) # [minibatch_size, num_classes*4]

# loss
with tf.variable_scope('fast_rcnn_classification_loss'):
Expand Down Expand Up @@ -312,10 +322,15 @@ def fast_rcnn_proposals(self, decode_boxes, scores):
tmp_category = tf.gather(category, valid_indices)

category_list.append(tmp_category)
if(tf_major_ver<1):
all_nms_boxes = tf.concat(after_nms_boxes, concat_dim=0)
all_nms_scores = tf.concat(after_nms_scores, concat_dim=0)
all_category = tf.concat(category_list, concat_dim=0)
else:
all_nms_boxes = tf.concat(after_nms_boxes, axis=0)
all_nms_scores = tf.concat(after_nms_scores, axis=0)
all_category = tf.concat(category_list, axis=0)

all_nms_boxes = tf.concat(after_nms_boxes, axis=0)
all_nms_scores = tf.concat(after_nms_scores, axis=0)
all_category = tf.concat(category_list, axis=0)

all_nms_boxes = boxes_utils.clip_boxes_to_img_boundaries(all_nms_boxes,
img_shape=self.img_shape)
Expand Down
51 changes: 36 additions & 15 deletions libs/fast_rcnn/build_fast_rcnn1.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
from libs.configs import cfgs

DEBUG = False

tf_major_ver = int(tf.__version__.split(".")[0])
tf_minor_ver = int(tf.__version__.split(".")[1])

class FastRCNN(object):
def __init__(self,
Expand Down Expand Up @@ -136,9 +137,12 @@ def get_rois(self):
[self.roi_pool_kernel_size, self.roi_pool_kernel_size],
stride=self.roi_pool_kernel_size)
all_level_roi_list.append(level_i_rois)

all_level_rois = tf.concat(all_level_roi_list, axis=0)
all_level_proposals = tf.concat(all_level_proposal_list, axis=0)
if(tf_major_ver<1):
all_level_rois = tf.concat(all_level_roi_list, concat_dim=0)
all_level_proposals = tf.concat(all_level_proposal_list, concat_dim=0)
else:
all_level_rois = tf.concat(all_level_roi_list, axis=0)
all_level_proposals = tf.concat(all_level_proposal_list, axis=0)
return all_level_rois, all_level_proposals

def fast_rcnn_net(self):
Expand Down Expand Up @@ -240,7 +244,10 @@ def fast_rcnn_minibatch(self, reference_boxes):
negative_indices = tf.random_shuffle(negative_indices)
negative_indices = tf.slice(negative_indices, begin=[0], size=[num_of_negatives])

minibatch_indices = tf.concat([positive_indices, negative_indices], axis=0)
if(tf_major_ver<1):
minibatch_indices = tf.concat([positive_indices, negative_indices], concat_dim=0)
else:
minibatch_indices = tf.concat([positive_indices, negative_indices], axis=0)
minibatch_indices = tf.random_shuffle(minibatch_indices)

minibatch_reference_boxes_mattached_gtboxes = tf.gather(reference_boxes_mattached_gtboxes,
Expand Down Expand Up @@ -298,15 +305,21 @@ def fast_rcnn_loss(self):
tmp_class_weights = tf.ones(shape=[tf.shape(minibatch_encode_boxes)[0], 4], dtype=tf.float32)
tmp_class_weights = tmp_class_weights * tf.expand_dims(category_list[i], axis=1)
class_weights_list.append(tmp_class_weights)
class_weights = tf.concat(class_weights_list, axis=1) # [minibatch_size, num_classes*4]
if(tf_major_ver<1):
class_weights = tf.concat(class_weights_list, concat_dim=1) # [minibatch_size, num_classes*4]
else:
class_weights = tf.concat(class_weights_list, axis=1) # [minibatch_size, num_classes*4]

class_weights_list_rotate = []
category_list_rotate = tf.unstack(minibatch_label_one_hot, axis=1)
for i in range(1, self.num_classes + 1):
tmp_class_weights_rotate = tf.ones(shape=[tf.shape(minibatch_encode_boxes_rotate)[0], 5], dtype=tf.float32)
tmp_class_weights_rotate = tmp_class_weights_rotate * tf.expand_dims(category_list_rotate[i], axis=1)
class_weights_list_rotate.append(tmp_class_weights_rotate)
class_weights_rotate = tf.concat(class_weights_list_rotate, axis=1) # [minibatch_size, num_classes*5]
if(tf_major_ver<1):
class_weights_rotate = tf.concat(class_weights_list_rotate, concat_dim=1) # [minibatch_size, num_classes*5]
else:
class_weights_rotate = tf.concat(class_weights_list_rotate, axis=1) # [minibatch_size, num_classes*5]

# loss
with tf.variable_scope('fast_rcnn_classification_loss'):
Expand Down Expand Up @@ -371,10 +384,14 @@ def fast_rcnn_proposals(self, decode_boxes, scores):
tmp_category = tf.gather(category, valid_indices)

category_list.append(tmp_category)

all_nms_boxes = tf.concat(after_nms_boxes, axis=0)
all_nms_scores = tf.concat(after_nms_scores, axis=0)
all_category = tf.concat(category_list, axis=0)
if(tf_major_ver<1):
all_nms_boxes = tf.concat(after_nms_boxes, concat_dim=0)
all_nms_scores = tf.concat(after_nms_scores, concat_dim=0)
all_category = tf.concat(category_list, concat_dim=0)
else:
all_nms_boxes = tf.concat(after_nms_boxes, axis=0)
all_nms_scores = tf.concat(after_nms_scores, axis=0)
all_category = tf.concat(category_list, axis=0)

all_nms_boxes = boxes_utils.clip_boxes_to_img_boundaries(all_nms_boxes,
img_shape=self.img_shape)
Expand Down Expand Up @@ -429,10 +446,14 @@ def fast_rcnn_proposals_rotate(self, decode_boxes, scores):
tmp_category = tf.gather(category, valid_indices)

category_list.append(tmp_category)

all_nms_boxes = tf.concat(after_nms_boxes, axis=0)
all_nms_scores = tf.concat(after_nms_scores, axis=0)
all_category = tf.concat(category_list, axis=0)
if(tf_major_ver<1):
all_nms_boxes = tf.concat(after_nms_boxes, concat_dim=0)
all_nms_scores = tf.concat(after_nms_scores, concat_dim=0)
all_category = tf.concat(category_list, concat_dim=0)
else:
all_nms_boxes = tf.concat(after_nms_boxes, axis=0)
all_nms_scores = tf.concat(after_nms_scores, axis=0)
all_category = tf.concat(category_list, axis=0)

# all_nms_boxes = boxes_utils.clip_boxes_to_img_boundaries(all_nms_boxes,
# img_shape=self.img_shape)
Expand Down
26 changes: 19 additions & 7 deletions libs/networks/nets/vggnet16.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
from libs.configs import cfgs

VGG_MEAN = [103.939, 116.779, 123.68]

tf_major_ver = int(tf.__version__.split(".")[0])
tf_minor_ver = int(tf.__version__.split(".")[1])

class Vgg16:
def __init__(self, vgg16_npy_path=cfgs.VGG16_WEIGHT_PATH):
Expand Down Expand Up @@ -72,18 +73,29 @@ def build(self, rgb, rgb2gbr=False):
if rgb2gbr:
# Convert RGB to BGR
red, green, blue = tf.split(self.color, num_or_size_splits=3, axis=3)
self.color = tf.concat([blue - VGG_MEAN[0],
green - VGG_MEAN[1],
red - VGG_MEAN[2]], axis=3)
if(tf_major_ver<1):
self.color = tf.concat([blue - VGG_MEAN[0],
green - VGG_MEAN[1],
red - VGG_MEAN[2]], concat_dim=3)
else:
self.color = tf.concat([blue - VGG_MEAN[0],
green - VGG_MEAN[1],
red - VGG_MEAN[2]], axis=3)

self.conv1_1 = self.conv_op(input_op=self.color, name="conv1_1", kh=3, kw=3,
n_out=64, dh=1, dw=1)

else:

blue, green, red = tf.split(self.color, num_or_size_splits=3, axis=3)
self.color = tf.concat([blue - VGG_MEAN[0],
green - VGG_MEAN[1],
red - VGG_MEAN[2]], axis=3)
if(tf_major_ver<1):
self.color = tf.concat([blue - VGG_MEAN[0],
green - VGG_MEAN[1],
red - VGG_MEAN[2]], concat_dim=3)
else:
self.color = tf.concat([blue - VGG_MEAN[0],
green - VGG_MEAN[1],
red - VGG_MEAN[2]], axis=3)
self.conv1_1 = self.conv_op(input_op=self.color, name="conv1_1", kh=3, kw=3,
n_out=64, dh=1, dw=1)

Expand Down
Loading