Commit eedec322 authored by Yuxin Wu's avatar Yuxin Wu

sampling

parent efabaaa4
...@@ -17,10 +17,10 @@ class DataFlow(object): ...@@ -17,10 +17,10 @@ class DataFlow(object):
A generator to generate data as tuple. A generator to generate data as tuple.
""" """
@abstractmethod
def size(self): def size(self):
""" """
Size of this data flow. Size of this data flow.
""" """
raise NotImplementedError()
...@@ -30,7 +30,6 @@ def run_test_case(case): ...@@ -30,7 +30,6 @@ def run_test_case(case):
if __name__ == '__main__': if __name__ == '__main__':
import tensorpack import tensorpack
subs = tensorpack.models._test.TestModel.__subclasses__() subs = tensorpack.models._test.TestModel.__subclasses__()
print subs
for cls in subs: for cls in subs:
run_test_case(cls) run_test_case(cls)
......
...@@ -39,20 +39,41 @@ def sample(img, coords): ...@@ -39,20 +39,41 @@ def sample(img, coords):
return sampled return sampled
@layer_register() @layer_register()
def ImageSample(template, mapping, interpolate): def ImageSample(template, mapping):
""" """
Sample an image from template, using the given coordinate Sample the template image, using the given coordinate, by bilinear interpolation.
template: bxhxwxc template: bxhxwxc
mapping: bxh2xw2x2 (y, x) real-value coordinates mapping: bxh2xw2x2 (y, x) real-value coordinates
interpolate: 'nearest'
Return: bxh2xw2xc Return: bxh2xw2xc
""" """
mapping = tf.maximum(mapping, 0.0)
tf.check_numerics(mapping, "mapping")
lcoor = tf.cast(mapping, tf.int32) # floor
ucoor = lcoor + 1
if interpolate == 'nearest': # has to cast to int32 and then cast back
mapping = tf.cast(tf.floor(mapping + 0.5), tf.int32) # XXX tf.floor have gradient 1 w.r.t input, bug or feature?
return sample(template, mapping) diff = mapping - tf.cast(lcoor, tf.float32)
else: neg_diff = 1.0 - diff #bxh2xw2x2
raise NotImplementedError()
lcoory, lcoorx = tf.split(3, 2, lcoor)
ucoory, ucoorx = tf.split(3, 2, ucoor)
lyux = tf.concat(3, [lcoory, ucoorx])
uylx = tf.concat(3, [ucoory, lcoorx])
diffy, diffx = tf.split(3, 2, diff)
neg_diffy, neg_diffx = tf.split(3, 2, neg_diff)
prod = tf.reduce_prod(diff, 3, keep_dims=True)
diff = tf.Print(diff, [tf.is_finite(tf.reduce_sum(diff)), tf.shape(prod),
tf.reduce_max(diff), diff],
summarize=50)
return sample(template, lcoor) * neg_diffx * neg_diffy + \
sample(template, ucoor) * diffx * diffy + \
sample(template, lyux) * neg_diffy * diffx + \
sample(template, uylx) * diffy * neg_diffx
from _test import TestModel from _test import TestModel
class TestSample(TestModel): class TestSample(TestModel):
...@@ -85,7 +106,39 @@ class TestSample(TestModel): ...@@ -85,7 +106,39 @@ class TestSample(TestModel):
true_res = np_sample(bimg, np.floor(mat + 0.5).astype('int32')) true_res = np_sample(bimg, np.floor(mat + 0.5).astype('int32'))
inp, mapping = self.make_variable(bimg, mat) inp, mapping = self.make_variable(bimg, mat)
output = ImageSample('sample', inp, mapping, 'nearest') output = sample(inp, tf.cast(tf.floor(mapping+0.5), tf.int32))
res = self.run_variable(output) res = self.run_variable(output)
self.assertTrue((res == true_res).all()) self.assertTrue((res == true_res).all())
if __name__ == '__main__':
import cv2
import numpy as np
import sys
im = cv2.imread('cat.jpg')
im = im.reshape((1,) + im.shape).astype('float32')
imv = tf.Variable(im)
h, w = 300, 400
mapping = np.zeros((1, h, w, 2), dtype='float32')
diff = 2000
for x in range(w):
for y in range(h):
mapping[0,y,x,:] = np.array([y-diff+0.4, x-diff+0.5])
mapv = tf.Variable(mapping)
output = ImageSample('sample', imv, mapv)
sess = tf.Session()
sess.run(tf.initialize_all_variables())
out = sess.run(tf.gradients(tf.reduce_sum(output), mapv))
#out = sess.run(output)
print out[0].min()
print out[0].max()
print out[0].sum()
out = sess.run([output])[0]
im = out[0]
cv2.imwrite('sampled.jpg', im)
...@@ -87,6 +87,10 @@ def summary_grads(grads): ...@@ -87,6 +87,10 @@ def summary_grads(grads):
if grad: if grad:
tf.histogram_summary(var.op.name + '/gradients', grad) tf.histogram_summary(var.op.name + '/gradients', grad)
def check_grads(grads):
for grad, var in grads:
tf.Assert(tf.reduce_all(tf.is_finite(var)), [var])
def start_train(config): def start_train(config):
""" """
Start training with the given config Start training with the given config
...@@ -144,6 +148,7 @@ def start_train(config): ...@@ -144,6 +148,7 @@ def start_train(config):
cost_var = config.get_model_func(model_inputs, is_training=True) cost_var = config.get_model_func(model_inputs, is_training=True)
grads = config.optimizer.compute_gradients(cost_var) grads = config.optimizer.compute_gradients(cost_var)
summary_grads(grads) summary_grads(grads)
check_grads(grads)
avg_maintain_op = summary_moving_average(cost_var) avg_maintain_op = summary_moving_average(cost_var)
train_op = tf.group( train_op = tf.group(
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment