Commit dc59ad5f authored by Yuxin Wu's avatar Yuxin Wu

fix image_sampling, and email

parent 6e080435
#!/usr/bin/env python2 #!/usr/bin/env python2
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# File: DQN.py # File: DQN.py
# Author: Yuxin Wu <ppwwyyxx@gmail.com> # Author: Yuxin Wu <ppwwyyxxc@gmail.com>
import numpy as np import numpy as np
import tensorflow as tf import tensorflow as tf
......
#!/usr/bin/env python #!/usr/bin/env python
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# File: mnist-disturb.py # File: mnist-disturb.py
# Author: Yuxin Wu <ppwwyyxx@gmail.com> # Author: Yuxin Wu <ppwwyyxxc@gmail.com>
import numpy as np import numpy as np
import tensorflow as tf import tensorflow as tf
......
#!/usr/bin/env python #!/usr/bin/env python
# -*- coding: UTF-8 -*- # -*- coding: UTF-8 -*-
# File: svhn-disturb.py # File: svhn-disturb.py
# Author: Yuxin Wu <ppwwyyxx@gmail.com> # Author: Yuxin Wu <ppwwyyxxc@gmail.com>
import tensorflow as tf import tensorflow as tf
import argparse import argparse
......
#!/usr/bin/env python #!/usr/bin/env python
# -*- coding: UTF-8 -*- # -*- coding: UTF-8 -*-
# File: svhn-digit-dorefa.py # File: svhn-digit-dorefa.py
# Author: Yuxin Wu <ppwwyyxx@gmail.com> # Author: Yuxin Wu <ppwwyyxxc@gmail.com>
import tensorflow as tf import tensorflow as tf
import argparse import argparse
......
#!/usr/bin/env python #!/usr/bin/env python
# -*- coding: UTF-8 -*- # -*- coding: UTF-8 -*-
# File: hed.py # File: hed.py
# Author: Yuxin Wu <ppwwyyxx@gmail.com> # Author: Yuxin Wu <ppwwyyxxc@gmail.com>
import cv2 import cv2
import tensorflow as tf import tensorflow as tf
......
#!/usr/bin/env python #!/usr/bin/env python
# -*- coding: UTF-8 -*- # -*- coding: UTF-8 -*-
# File: inception-bn.py # File: inception-bn.py
# Author: Yuxin Wu <ppwwyyxx@gmail.com> # Author: Yuxin Wu <ppwwyyxxc@gmail.com>
import cv2 import cv2
import argparse import argparse
......
#!/usr/bin/env python #!/usr/bin/env python
# -*- coding: UTF-8 -*- # -*- coding: UTF-8 -*-
# File: inceptionv3.py # File: inceptionv3.py
# Author: Yuxin Wu <ppwwyyxx@gmail.com> # Author: Yuxin Wu <ppwwyyxxc@gmail.com>
import cv2 import cv2
import argparse import argparse
......
#!/usr/bin/env python #!/usr/bin/env python
# -*- coding: UTF-8 -*- # -*- coding: UTF-8 -*-
# File: cifar10-resnet.py # File: cifar10-resnet.py
# Author: Yuxin Wu <ppwwyyxx@gmail.com> # Author: Yuxin Wu <ppwwyyxxc@gmail.com>
import numpy as np import numpy as np
import tensorflow as tf import tensorflow as tf
......
#!/usr/bin/env python #!/usr/bin/env python
# -*- coding: UTF-8 -*- # -*- coding: UTF-8 -*-
# File: svhn-resnet.py # File: svhn-resnet.py
# Author: Yuxin Wu <ppwwyyxx@gmail.com> # Author: Yuxin Wu <ppwwyyxxc@gmail.com>
import tensorflow as tf import tensorflow as tf
import argparse import argparse
......
#!/usr/bin/env python #!/usr/bin/env python
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# File: char-rnn.py # File: char-rnn.py
# Author: Yuxin Wu <ppwwyyxx@gmail.com> # Author: Yuxin Wu <ppwwyyxxc@gmail.com>
import tensorflow as tf import tensorflow as tf
import numpy as np import numpy as np
......
#!/usr/bin/env python #!/usr/bin/env python
# -*- coding: UTF-8 -*- # -*- coding: UTF-8 -*-
# File: cifar-convnet.py # File: cifar-convnet.py
# Author: Yuxin Wu <ppwwyyxx@gmail.com> # Author: Yuxin Wu <ppwwyyxxc@gmail.com>
import tensorflow as tf import tensorflow as tf
import argparse import argparse
import numpy as np import numpy as np
......
#!/usr/bin/env python2 #!/usr/bin/env python2
# -*- coding: UTF-8 -*- # -*- coding: UTF-8 -*-
# File: load-alexnet.py # File: load-alexnet.py
# Author: Yuxin Wu <ppwwyyxx@gmail.com> # Author: Yuxin Wu <ppwwyyxxc@gmail.com>
import tensorflow as tf import tensorflow as tf
import numpy as np import numpy as np
......
#!/usr/bin/env python2 #!/usr/bin/env python2
# -*- coding: UTF-8 -*- # -*- coding: UTF-8 -*-
# File: load-vgg16.py # File: load-vgg16.py
# Author: Yuxin Wu <ppwwyyxx@gmail.com> # Author: Yuxin Wu <ppwwyyxxc@gmail.com>
import cv2 import cv2
import tensorflow as tf import tensorflow as tf
...@@ -72,7 +72,6 @@ class Model(ModelDesc): ...@@ -72,7 +72,6 @@ class Model(ModelDesc):
def run_test(path, input): def run_test(path, input):
param_dict = np.load(path).item() param_dict = np.load(path).item()
pred_config = PredictConfig( pred_config = PredictConfig(
model=Model(), model=Model(),
input_var_names=['input'], input_var_names=['input'],
......
#!/usr/bin/env python #!/usr/bin/env python
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# File: mnist-convnet.py # File: mnist-convnet.py
# Author: Yuxin Wu <ppwwyyxx@gmail.com> # Author: Yuxin Wu <ppwwyyxxc@gmail.com>
import numpy as np import numpy as np
import tensorflow as tf import tensorflow as tf
......
#!/usr/bin/env python #!/usr/bin/env python
# -*- coding: UTF-8 -*- # -*- coding: UTF-8 -*-
# File: svhn-digit-convnet.py # File: svhn-digit-convnet.py
# Author: Yuxin Wu <ppwwyyxx@gmail.com> # Author: Yuxin Wu <ppwwyyxxc@gmail.com>
import tensorflow as tf import tensorflow as tf
import argparse import argparse
......
...@@ -12,11 +12,12 @@ from .imgaug import AugmentorList ...@@ -12,11 +12,12 @@ from .imgaug import AugmentorList
__all__ = ['ImageFromFile', 'AugmentImageComponent', 'AugmentImageComponents'] __all__ = ['ImageFromFile', 'AugmentImageComponent', 'AugmentImageComponents']
class ImageFromFile(DataFlow): class ImageFromFile(DataFlow):
""" Generate rgb images from list of files """
def __init__(self, files, channel=3, resize=None): def __init__(self, files, channel=3, resize=None):
""" :param files: list of file paths """
:param channel: 1 or 3 channel Generate rgb images from list of files
:param resize: a (h, w) tuple. If given, will force a resize :param files: list of file paths
:param channel: 1 or 3 channel
:param resize: a (h, w) tuple. If given, will force a resize
""" """
assert len(files) assert len(files)
self.files = files self.files = files
...@@ -38,11 +39,9 @@ class ImageFromFile(DataFlow): ...@@ -38,11 +39,9 @@ class ImageFromFile(DataFlow):
class AugmentImageComponent(MapDataComponent): class AugmentImageComponent(MapDataComponent):
"""
Augment the image component of datapoints
"""
def __init__(self, ds, augmentors, index=0): def __init__(self, ds, augmentors, index=0):
""" """
Augment the image component of datapoints
:param ds: a `DataFlow` instance. :param ds: a `DataFlow` instance.
:param augmentors: a list of `ImageAugmentor` instance to be applied in order. :param augmentors: a list of `ImageAugmentor` instance to be applied in order.
:param index: the index (or list of indices) of the image component in the produced datapoints by `ds`. default to be 0 :param index: the index (or list of indices) of the image component in the produced datapoints by `ds`. default to be 0
...@@ -57,9 +56,8 @@ class AugmentImageComponent(MapDataComponent): ...@@ -57,9 +56,8 @@ class AugmentImageComponent(MapDataComponent):
class AugmentImageComponents(MapData): class AugmentImageComponents(MapData):
""" Augment a list of images of the same shape, with the same parameters"""
def __init__(self, ds, augmentors, index=(0,1)): def __init__(self, ds, augmentors, index=(0,1)):
""" """ Augment a list of images of the same shape, with the same parameters
:param ds: a `DataFlow` instance. :param ds: a `DataFlow` instance.
:param augmentors: a list of `ImageAugmentor` instance to be applied in order. :param augmentors: a list of `ImageAugmentor` instance to be applied in order.
:param index: tuple of indices of the image components :param index: tuple of indices of the image components
......
...@@ -12,7 +12,7 @@ __all__ = ['ImageSample'] ...@@ -12,7 +12,7 @@ __all__ = ['ImageSample']
# XXX TODO ugly. # XXX TODO ugly.
# really need to fix this after tensorflow supports advanced indexing # really need to fix this after tensorflow supports advanced indexing
# See github:tensorflow#418,#206 # See github:tensorflow#418,#206
def sample(img, coords, borderMode): def sample(img, coords):
""" """
:param img: bxhxwxc :param img: bxhxwxc
:param coords: bxh2xw2x2 (y, x) floating point (but is actually holding integer) :param coords: bxh2xw2x2 (y, x) floating point (but is actually holding integer)
...@@ -39,14 +39,6 @@ def sample(img, coords, borderMode): ...@@ -39,14 +39,6 @@ def sample(img, coords, borderMode):
img = tf.reshape(img, [-1, shape[2]]) #bhw x c img = tf.reshape(img, [-1, shape[2]]) #bhw x c
sampled = tf.gather(img, flat_coords) sampled = tf.gather(img, flat_coords)
if borderMode == 'constant':
mask = tf.less_equal(orig_coords, max_coor)
mask2 = tf.greater_equal(orig_coords, 0)
mask = tf.logical_and(mask, mask2) #bxh2xw2x2
mask = tf.reduce_all(mask, [3]) # bxh2xw2 boolean
mask = tf.expand_dims(mask, 3)
sampled = sampled * tf.cast(mask, tf.float32)
return sampled return sampled
@layer_register() @layer_register()
...@@ -64,8 +56,12 @@ def ImageSample(inputs, borderMode='repeat'): ...@@ -64,8 +56,12 @@ def ImageSample(inputs, borderMode='repeat'):
""" """
template, mapping = inputs template, mapping = inputs
assert template.get_shape().ndims == 4 and mapping.get_shape().ndims == 4 assert template.get_shape().ndims == 4 and mapping.get_shape().ndims == 4
input_shape = template.get_shape().as_list()[1:]
assert None not in input_shape, \
"Images in ImageSample layer must have fully-defined shape"
assert borderMode in ['repeat', 'constant'] assert borderMode in ['repeat', 'constant']
orig_mapping = mapping
mapping = tf.maximum(mapping, 0.0) mapping = tf.maximum(mapping, 0.0)
lcoor = tf.floor(mapping) lcoor = tf.floor(mapping)
ucoor = lcoor + 1 ucoor = lcoor + 1
...@@ -86,10 +82,19 @@ def ImageSample(inputs, borderMode='repeat'): ...@@ -86,10 +82,19 @@ def ImageSample(inputs, borderMode='repeat'):
#diff = tf.Print(diff, [tf.is_finite(tf.reduce_sum(diff)), tf.shape(prod), #diff = tf.Print(diff, [tf.is_finite(tf.reduce_sum(diff)), tf.shape(prod),
#tf.reduce_max(diff), diff], summarize=50) #tf.reduce_max(diff), diff], summarize=50)
return tf.add_n([sample(template, lcoor, borderMode) * neg_diffx * neg_diffy, ret = tf.add_n([sample(template, lcoor) * neg_diffx * neg_diffy,
sample(template, ucoor, borderMode) * diffx * diffy, sample(template, ucoor) * diffx * diffy,
sample(template, lyux, borderMode) * neg_diffy * diffx, sample(template, lyux) * neg_diffy * diffx,
sample(template, uylx, borderMode) * diffy * neg_diffx], name='sampled') sample(template, uylx) * diffy * neg_diffx], name='sampled')
if borderMode == 'constant':
max_coor = tf.constant([input_shape[0] - 1, input_shape[1] - 1], dtype=tf.float32)
mask = tf.greater_equal(orig_mapping, 0.0)
mask2 = tf.less_equal(orig_mapping, max_coor)
mask = tf.logical_and(mask, mask2) #bxh2xw2x2
mask = tf.reduce_all(mask, [3]) # bxh2xw2 boolean
mask = tf.expand_dims(mask, 3)
ret = ret * tf.cast(mask, tf.float32)
return ret
from ._test import TestModel from ._test import TestModel
class TestSample(TestModel): class TestSample(TestModel):
...@@ -137,21 +142,21 @@ if __name__ == '__main__': ...@@ -137,21 +142,21 @@ if __name__ == '__main__':
h, w = 300, 400 h, w = 300, 400
mapping = np.zeros((1, h, w, 2), dtype='float32') mapping = np.zeros((1, h, w, 2), dtype='float32')
diff = 2000 diff = 200
for x in range(w): for x in range(w):
for y in range(h): for y in range(h):
mapping[0,y,x,:] = np.array([y-diff+0.4, x-diff+0.5]) mapping[0,y,x,:] = np.array([y-diff+0.4, x-diff+0.5])
mapv = tf.Variable(mapping) mapv = tf.Variable(mapping)
output = ImageSample('sample', [imv, mapv]) output = ImageSample('sample', [imv, mapv], borderMode='constant')
sess = tf.Session() sess = tf.Session()
sess.run(tf.initialize_all_variables()) sess.run(tf.initialize_all_variables())
out = sess.run(tf.gradients(tf.reduce_sum(output), mapv)) #out = sess.run(tf.gradients(tf.reduce_sum(output), mapv))
#out = sess.run(output) #out = sess.run(output)
print(out[0].min()) #print(out[0].min())
print(out[0].max()) #print(out[0].max())
print(out[0].sum()) #print(out[0].sum())
out = sess.run([output])[0] out = sess.run([output])[0]
im = out[0] im = out[0]
......
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