Commit efabaaa4 authored by Yuxin Wu's avatar Yuxin Wu

image sample & unittest

parent 6a6d00cd
## Tensorpack High-Level Tutorial
### How to feed data
Define a `DataFlow` instance to feed data.
See [Dataflow documentation](https://github.com/ppwwyyxx/tensorpack/tree/master/tensorpack/dataflow)
### How to define a model
Take a look at the `get_model` function in [mnist example](https://github.com/ppwwyyxx/tensorpack/blob/master/example_mnist.py) first.
To define a model, write a `get_model` function which accepts two arguments:
+ inputs: a list of variables used as input in training. inputs could be batched or not batched (see
[training](#how-to-perform-training))
+ is_training: the graph for training and inference could be different (e.g. dropout, augmentation),
`get_model` function should use this variable to know is it doing training or inference.
The function should define a graph based on input variables.
It could use any pre-defined routines in [tensorpack/models](https://github.com/ppwwyyxx/tensorpack/tree/master/tensorpack/models),
or use tensorflow symbolic functions.
It may also define other helper variables to monitor the training,
(e.g. accuracy), and add tensorboard summaries you need. (See [howto summary](#use-tensorboard-summary))
Also, it's helpful to give names to some important variables used in inference. (See
[inference](#how-to-perform-inference)).
The function should at last return the cost to minimize.
### How to perform training
### How to perform inference
### How to add new models
### Use tensorboard summary
<!--
- what will be automatically summaried
-->
......@@ -8,14 +8,30 @@ import numpy as np
from . import *
import unittest
subs = unittest.TestCase.__subclasses__()
class TestModel(unittest.TestCase):
def run_variable(self, var):
sess = tf.Session()
sess.run(tf.initialize_all_variables())
if isinstance(var, list):
return sess.run(var)
else:
return sess.run([var])[0]
def make_variable(self, *args):
if len(args) > 1:
return [tf.Variable(k) for k in args]
else:
return tf.Variable(args[0])
def run_test_case(case):
suite = unittest.TestLoader().loadTestsFromTestCase(case)
unittest.TextTestRunner(verbosity=2).run(suite)
for cls in subs:
if 'tensorpack.models' in str(cls):
if __name__ == '__main__':
import tensorpack
subs = tensorpack.models._test.TestModel.__subclasses__()
print subs
for cls in subs:
run_test_case(cls)
#!/usr/bin/env python2
# -*- coding: UTF-8 -*-
# File: image_sample.py
# Author: Yuxin Wu <ppwwyyxx@gmail.com>
import tensorflow as tf
from ._common import layer_register
__all__ = ['ImageSample']
# XXX TODO ugly.
# really need to fix this after tensorflow supports multiple indexing
# See github:tensorflow#418,#206
def sample(img, coords):
"""
img: bxhxwxc
coords: bxh2xw2x2 (y, x) integer
"""
shape = img.get_shape().as_list()[1:]
shape2 = coords.get_shape().as_list()[1:3]
max_coor = tf.constant([shape[0] - 1, shape[1] - 1])
coords = tf.minimum(coords, max_coor)
coords = tf.maximum(coords, tf.constant(0))
w = shape[1]
coords = tf.reshape(coords, [-1, 2])
coords = tf.matmul(coords, tf.constant([[w], [1]]))
coords = tf.reshape(coords, [-1] + shape2)
# bxh2xw2
batch_add = tf.range(tf.shape(img)[0]) * (shape[0] * shape[1])
batch_add = tf.reshape(batch_add, [-1, 1, 1]) #bx1x1
flat_coords = coords + batch_add
img = tf.reshape(img, [-1, shape[2]]) #bhw x c
sampled = tf.gather(img, flat_coords)
return sampled
@layer_register()
def ImageSample(template, mapping, interpolate):
"""
Sample an image from template, using the given coordinate
template: bxhxwxc
mapping: bxh2xw2x2 (y, x) real-value coordinates
interpolate: 'nearest'
Return: bxh2xw2xc
"""
if interpolate == 'nearest':
mapping = tf.cast(tf.floor(mapping + 0.5), tf.int32)
return sample(template, mapping)
else:
raise NotImplementedError()
from _test import TestModel
class TestSample(TestModel):
def test_sample(self):
import numpy as np
h, w = 3, 4
def np_sample(img, coords):
# a reference implementation
coords = np.maximum(coords, 0)
coords = np.minimum(coords,
np.array([img.shape[1]-1, img.shape[2]-1]))
xs = coords[:,:,:,1].reshape((img.shape[0], -1))
ys = coords[:,:,:,0].reshape((img.shape[0], -1))
ret = np.zeros((img.shape[0], coords.shape[1], coords.shape[2],
img.shape[3]), dtype='float32')
for k in range(img.shape[0]):
xss, yss = xs[k], ys[k]
ret[k,:,:,:] = img[k,yss,xss,:].reshape((coords.shape[1],
coords.shape[2], 3))
return ret
bimg = np.random.rand(2, h, w, 3).astype('float32')
#mat = np.array([
#[[[1,1], [1.2,1.2]], [[-1, -1], [2.5, 2.5]]],
#[[[1,1], [1.2,1.2]], [[-1, -1], [2.5, 2.5]]]
#], dtype='float32') #2x2x2x2
mat = (np.random.rand(2, 5, 5, 2) - 0.2) * np.array([h + 3, w + 3])
true_res = np_sample(bimg, np.floor(mat + 0.5).astype('int32'))
inp, mapping = self.make_variable(bimg, mat)
output = ImageSample('sample', inp, mapping, 'nearest')
res = self.run_variable(output)
self.assertTrue((res == true_res).all())
......@@ -33,7 +33,7 @@ def FixedUnPooling(x, shape, unpool_mat=None):
Unpool the input with a fixed mat to perform kronecker product with
x: 4D tensor of (b, h, w, c)
shape: int or list/tuple of length 2
unpool_mat: a tf matrix with size=shape. if None, will use a zero-mat with a 1 as first element
unpool_mat: a tf matrix with size=shape. if None, will use a mat with 1 at top-left corner
"""
shape = shape2d(shape)
input_shape = x.get_shape().as_list()
......@@ -58,17 +58,15 @@ def FixedUnPooling(x, shape, unpool_mat=None):
input_shape[3]])
return prod
import unittest
class TestPool(unittest.TestCase):
from _test import TestModel
class TestPool(TestModel):
def test_fixed_unpooling(self):
h, w = 3, 4
mat = np.random.rand(h, w).astype('float32')
inp = tf.Variable(mat)
inp = self.make_variable(mat)
inp = tf.reshape(inp, [1, h, w, 1])
output = FixedUnPooling('unpool', inp, 2)
sess = tf.Session()
sess.run(tf.initialize_all_variables())
res = sess.run([output])[0]
res = self.run_variable(output)
self.assertEqual(res.shape, (1, 2*h, 2*w, 1))
# mat is on cornser
......
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