Skip to content

Commit

Permalink
support for deterministic testing
Browse files Browse the repository at this point in the history
  • Loading branch information
kmyi committed Sep 29, 2016
1 parent 69f58c0 commit e9fe138
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 28 deletions.
8 changes: 3 additions & 5 deletions python-code/Utils/evaluate.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
# Created: Fri Mar 6 11:52:42 2015 (+0100)
# Version:
# Package-Requires: ()
# Last-Updated: Thu Jul 7 10:54:17 2016 (+0200)
# By: Kwang Moo Yi
# Update #: 234
# URL:
# Doc URL:
# Keywords:
Expand Down Expand Up @@ -42,7 +39,8 @@
from Utils.networks.buildLayers import instantiateLayers


def testModelNew(img_file_name, kp_file_name, pathconf, param, model_epoch=""):
def testModelNew(img_file_name, kp_file_name, pathconf, param, model_epoch="",
deterministic=False):

if param.runType != "CVPR16":
raise NotImplementedError(
Expand Down Expand Up @@ -104,7 +102,7 @@ def testModelNew(img_file_name, kp_file_name, pathconf, param, model_epoch=""):
# Actual instantiation and setup
myNet = SiameseOrientationLearner(myNetConfig)

instantiateLayers(myNet)
instantiateLayers(myNet, deterministic)
# myNet.setupSGD()
myNet.setupDataAndCompile4Test()

Expand Down
21 changes: 9 additions & 12 deletions python-code/Utils/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
# Created: Tue Jan 27 11:17:55 2015 (+0100)
# Version:
# Package-Requires: ()
# Last-Updated: Mon Jul 4 05:51:24 2016 (-0700)
# By: Kwang Moo Yi
# Update #: 858
# URL:
# Doc URL:
# Keywords:
Expand Down Expand Up @@ -117,12 +114,12 @@ def __init__(self, rng, input, filter_shape, image_shape,
self.b = b_in

# convolve input feature maps with filters
conv_out = conv.conv2d(input=input, filters=self.W,
filter_shape=filter_shape,
image_shape=image_shape)
self.conv_out = conv.conv2d(input=input, filters=self.W,
filter_shape=filter_shape,
image_shape=image_shape)

# downsample each feature map individually, using maxpooling
pooled_out = downsample.max_pool_2d(input=conv_out,
pooled_out = downsample.max_pool_2d(input=self.conv_out,
ds=poolsize, ignore_border=True)

# add the bias term. Since the bias is a vector (1D array), we first
Expand Down Expand Up @@ -480,15 +477,15 @@ def __init__(self, rng, input, n_in, n_out, n_sum, n_max,

W_vec = self.W.reshape([n_in, n_out * n_sum * n_max])
raw_out = T.dot(input, W_vec) + self.b.flatten().dimshuffle('x', 0)
reshaped_raw_out = raw_out.reshape([batch_size, n_out, n_sum, n_max])
self.reshaped_raw_out = raw_out.reshape([batch_size, n_out, n_sum, n_max])

max_out = T.max(reshaped_raw_out, axis=3)
sum_out = T.sum(
max_out * self.delta.dimshuffle('x', 'x', 0),
self.max_out = T.max(self.reshaped_raw_out, axis=3)
self.sum_out = T.sum(
self.max_out * self.delta.dimshuffle('x', 'x', 0),
axis=2, dtype=floatX
)

lin_output = sum_out
lin_output = self.sum_out
output = (lin_output if activation is None else activation(lin_output))

# If nAtanPool is 2 then it means in this layer, we atan pool
Expand Down
21 changes: 16 additions & 5 deletions python-code/Utils/networks/buildLayers.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
# Created: Fri Aug 14 13:44:40 2015 (+0200)
# Version:
# Package-Requires: ()
# Last-Updated: Tue Jul 5 16:19:41 2016 (+0200)
# By: Kwang Moo Yi
# Update #: 129
# URL:
# Doc URL:
# Keywords:
Expand Down Expand Up @@ -52,7 +49,7 @@
floatX = theano.config.floatX


def instantiateLayers(myNet):
def instantiateLayers(myNet, deterministic):
'''
Build layers here
Expand Down Expand Up @@ -100,7 +97,7 @@ def instantiateLayers(myNet):
# ---------------------------------------------------------------------
# If necessary, generate the dropout test layer and the corresponding
# output
if bDropout:
if bDropout and deterministic:
myNet.test_layers[idxSiam] = buildDropoutTestLayers(
myNet.layers[idxSiam], dropout_rates)
else:
Expand Down Expand Up @@ -280,13 +277,24 @@ def buildDropoutTestLayers(orig_layer_list, dropout_rates):
test_layer_list = []

# For each layer in the network
prev_layer_output = None
for cur_layer, dropout_rate in zip(orig_layer_list, dropout_rates):

# Note that these will carry the normal name and args_in excluding the
# dropout init
layer_name = cur_layer.name
layer_args = cur_layer.args_in

# Use input from the previous test layer
if prev_layer_output is not None:
# flatten the input layer into two dims if hidden layer
if cur_layer.name[-11:] == "HiddenLayer":
cur_layer_input = prev_layer_output.flatten(2)
else:
cur_layer_input = prev_layer_output
layer_args = (layer_args[0],) + \
(cur_layer_input,) + layer_args[2:]

# Change the last arguments so that it simply uses the weights
# of the other layer! note that the original implementation
# did not multiply the output for some reason
Expand All @@ -297,6 +305,9 @@ def buildDropoutTestLayers(orig_layer_list, dropout_rates):
# Call the appropriate layer with it's name
test_layer = globals()[layer_name](*new_layer_args)

# Store the output
prev_layer_output = test_layer.output

test_layer_list += [test_layer]

return test_layer_list
Expand Down
2 changes: 0 additions & 2 deletions python-code/Utils/networks/cvpr16.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,6 @@ def runTest(self, x_in, model_epoch=""):
self.test_x[idxSiam].set_value(
numpy.asarray(test_x_in, dtype=floatX))

# self.test_x_id.set_value(test_x_id_in)
# self.test_y.set_value(test_y_in)
test_result = self.get_output()
test_raw_output = self.get_raw_output()
# ---------------------------------------------------------------------
Expand Down
15 changes: 11 additions & 4 deletions python-code/runSingleTestWithFiles.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/bin/env python2
# runSingleTestWithFiles.py ---
#
# Filename: runSingleTestWithFiles.py
Expand Down Expand Up @@ -43,16 +44,22 @@
if __name__ == '__main__':
""" Main routine """

if len(sys.argv) != 5:
if len(sys.argv) != 5 and len(sys.argv) != 6:
raise RuntimeError(
"USAGE: python runSingleTestWithFiles.py "
"<image_file> <kp_file> <config_file> <output_file>")
"<image_file> <kp_file> <config_file> <output_file> "
"<isdeterministic(0/1)>")

image_file_name = sys.argv[1]
kp_file_name = sys.argv[2]
config_file = sys.argv[3]
output_file = sys.argv[4]

# Default test mode is stochastic
deterministic = False
if len(sys.argv) >= 6:
deterministic = bool(int(sys.argv[5]))

# ------------------------------------------
# Setup and load parameters
param = paramStruct()
Expand All @@ -72,8 +79,8 @@
# ------------------------------------------
# Run Evaluate
start_time = time.clock()
eval_res = testModelNew(
image_file_name, kp_file_name, pathconf, param, model_epoch)
eval_res = testModelNew(image_file_name, kp_file_name, pathconf, param,
model_epoch, deterministic)
end_time = time.clock()
print("Time taken to compute for image {} (including compile time)"
" is {} seconds".format(
Expand Down

0 comments on commit e9fe138

Please sign in to comment.