Skip to content
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
7 changes: 6 additions & 1 deletion help_utils/help_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,4 +167,9 @@ def np_print(ary):
result = tf.reshape(result, tf.shape(tensor))
result = tf.cast(result, tf.float32)
sum_ = tf.reduce_sum(result)
tf.summary.scalar('print_s/{}'.format(tensor_name), sum_)
tf_major_ver = int(tf.__version__.split(".")[0])
tf_minor_ver = int(tf.__version__.split(".")[1])
if(tf_major_ver==0 and tf_minor_ver<11):
tf.scalar_summary('print_s/{}'.format(tensor_name), sum_)
else:
tf.summary.scalar('print_s/{}'.format(tensor_name), sum_)
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
8 changes: 6 additions & 2 deletions libs/networks/slim_nets/alexnet_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
from nets import alexnet

slim = tf.contrib.slim

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

class AlexnetV2Test(tf.test.TestCase):

Expand Down Expand Up @@ -137,7 +138,10 @@ def testForward(self):
with self.test_session() as sess:
inputs = tf.random_uniform((batch_size, height, width, 3))
logits, _ = alexnet.alexnet_v2(inputs)
sess.run(tf.global_variables_initializer())
if(tf_major_ver==0 and tf_minor_ver<12):
sess.run(tf.initialize_all_variables())
else:
sess.run(tf.global_variables_initializer())
output = sess.run(logits)
self.assertTrue(output.any())

Expand Down
18 changes: 14 additions & 4 deletions libs/networks/slim_nets/inception_resnet_v2_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
import tensorflow as tf

from nets import inception

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

class InceptionTest(tf.test.TestCase):

Expand Down Expand Up @@ -224,7 +225,10 @@ def testUnknownBatchSize(self):
self.assertListEqual(logits.get_shape().as_list(),
[None, num_classes])
images = tf.random_uniform((batch_size, height, width, 3))
sess.run(tf.global_variables_initializer())
if(tf_major_ver==0 and tf_minor_ver<12):
sess.run(tf.initialize_all_variables())
else:
sess.run(tf.global_variables_initializer())
output = sess.run(logits, {inputs: images.eval()})
self.assertEquals(output.shape, (batch_size, num_classes))

Expand All @@ -238,7 +242,10 @@ def testEvaluation(self):
num_classes,
is_training=False)
predictions = tf.argmax(logits, 1)
sess.run(tf.global_variables_initializer())
if(tf_major_ver==0 and tf_minor_ver<12):
sess.run(tf.initialize_all_variables())
else:
sess.run(tf.global_variables_initializer())
output = sess.run(predictions)
self.assertEquals(output.shape, (batch_size,))

Expand All @@ -256,7 +263,10 @@ def testTrainEvalWithReuse(self):
is_training=False,
reuse=True)
predictions = tf.argmax(logits, 1)
sess.run(tf.global_variables_initializer())
if(tf_major_ver==0 and tf_minor_ver<12):
sess.run(tf.initialize_all_variables())
else:
sess.run(tf.global_variables_initializer())
output = sess.run(predictions)
self.assertEquals(output.shape, (eval_batch_size,))

Expand Down
Loading