2017-11-14 2 views
0

をキャッシュ:私は両方すなわち、変換一貫性を返すとdataset.maptf.map_fnの両方を試してみたTensorflowが突然、私は現在、それほどのようなTFのための増強クラスを作成しようとしているサブグラフ結果

class Augmentations: 

     def __init__(self, **kwargs): 
      # Some Global Kwargs Define Here 
      self.transforms = tf.eye(3) 

      self.img_height = kwargs['img_height'] 
      self.img_width = kwargs['img_width'] 

     def _generate_random(self, shape, seed, minval, maxval): 
      return tf.random_uniform(shape=shape, 
            minval=minval, 
            maxval=maxval, 
            seed=seed, 
            dtype=tf.float32) 

     def rotate(self, seed, max_angle): 
      random_rotation_value = self._generate_random([1], seed, 0., max_angle) 
      rotation_matrix = tf.contrib.image.angles_to_projective_transforms(
           random_rotation_value, 
           self.img_height, 
           self.img_width) 

      rotation_matrix = tf.reshape(tf.concat([rotation_matrix, [[1.]]], axis=1), 
             [3, 3]) 

      self.transforms = tf.matmul(self.transforms, rotation_matrix) 
      return self 


     def apply(self, img): 
      t = tf.reshape(tf.matmul(self.transforms[0], 
            self.transforms[1]), 
          [1, 9])[0, :8] 
      return tf.contrib.image.transform(img, t) 

augment = Augment(**kwargs).rotate(None, 30).shear(None, 30) 
dataset = dataset.map(augment.apply, num_parallel_calls=10) 

augment = Augment(**kwargs).rotate(None, 30).shear(None, 30) 
dataset = tf.map_fn(augment.apply) 

番目の両方eseの呼び出しは、同じ変換が適用された異なる画像を返します。

dataset = dataset.map(Augment(**kwargs).rotate(None, 30).shear(None, 30).apply, num_parallel_calls=10) 

をかapply()に乱数のすべてを移動します。

ランダム変換して画像を返すための唯一の方法は、map()に変換呼んでいます。

random_*()のプレースメントがTFで問題となるかどうかは、プレースメントは問題ではないと考えていましたが、についてのみ問題はありますか?

答えて

0

applyメソッドへのすべての呼び出しが同じtf.random_uniform操作を共有するため、これは意図した動作です。 session.runを呼び出すと、このランダム操作は1回評価され、その結果はすべての要素に使用されます。

tf.random_uniformを適用すると、複数のランダム操作を作成し、それぞれ異なるランダムシーケンスを生成するため、違いが生じます。 tensorflowは(それがwhileループまたは類似していないと仮定して)最大で一度session.run()コールあたりOPを評価することはかなり基本的である

sess = tf.InteractiveSession() 
x = tf.random_uniform((1,)) 
y = tf.stack([x * tf.constant(1.0), x * tf.constant(1.0)]) 
y.eval() 
# Prints 
# array([[ 0.67649043], 
#   [ 0.67649043]], dtype=float32) 
# because there is only one random operation in the graph and it is evaluated once. 

sess = tf.InteractiveSession() 
y = tf.stack([tf.random_uniform((1,)) * tf.constant(1.0), tf.random_uniform((1,)) * tf.constant(1.0)]) 
y.eval() 
# Prints 
# array([[ 0.08824277], 
#  [ 0.4801079 ]], dtype=float32) 
# because there are two random operations in the graph and each yields a different value when evaluated 

:これは、基本的に間の差です。