2017-11-14 3 views
0

나는 현재과 같이 TF에 대한 보강 클래스를 만들려고 노력 해요 : 모두 반환과Tensorflow 예기치 않게 캐시 하위 그래프 결과

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) 

내가 해봤 모두 dataset.maptf.map_fn 일관 변환, 즉 :

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을 호출하면이 무작위 작업이 한 번 평가되고 그 결과가 모든 요소에 사용됩니다.

적용에서 tf.random_uniform을 호출하면 여러 가지 임의 연산을 만들고 각기 다른 임의 시퀀스를 생성하므로 차이가 발생합니다. (그것을 가정하는 것은 while 루프 또는 유사한 아닌) tensorflow이 최대 한 번 session.run() 호출 당 연산을 평가하는 매우 근본적인

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 

: 그것은 기본적으로 차이입니다.

관련 문제