无论是在cpu还是gpu上运行,tensorflow.contrib.percentile都比numpy.percentile慢得多

时间:2019-02-15 22:49:40

标签: python numpy tensorflow nvidia

我正在尝试在GPU(GeForce GTX 1050)上进行一些图像处理。

我正在使用tensorflow并与CPU中的相同实现进行比较:

这是两个功能:

CPU

def contrast_stretch(img, out_min=0.0, out_max=255.0):
"""
Performs a simple contrast stretch of the given image, in order to remove
extreme outliers.
"""

 import time
 t1 = time.time()
 in_min = np.percentile(img, 0.05)
 in_max = np.percentile(img, 99.95)
 t2 = time.time()
 print ("time taken cpu", t2-t1)

 out = img - in_min
 out *= (out_max-out_min) / (in_max - in_min)
 out += out_min

 out[out < out_min] = out_min
 out[out > out_max] = out_max

 return out

Tensorflow版本:

def contrast_stretch(img, out_min=0.0, out_max=255.0):
  """
  Performs a simple contrast stretch of the given image, in order to      remove
extreme outliers.
"""
  t1 = time.time()

  in_min = np.percentile(img.numpy(), 0.05, name="percentile")
  in_max = np.contrib.distributions.percentile(img.numpy(), 99.95, name="percentile")

  t2 = time.time()
  print ("time_taken gpu ", t2-t1)

  #JAD should i use tf to subtract scalars???

  out = tf.add(tf.multiply(tf.subtract(img, in_min), (out_max-out_min) / (in_max - in_min)), out_min)
  out = tf.clip_by_value(out, out_min, out_max)
  return out

这是程序输出:

花费的时间cpu 0.009064197540283203

耗时cpu 0.014983415603637695

花费的时间cpu 0.011657476425170898

time_taken gpu 2.475447654724121

time_taken gpu 0.7083957195281982

time_taken gpu 0.7085175514221191

观察:cpu花费的时间要少得多(例如少70倍)。另外,第一次调用contrib.percentile的时间要比其他时间长。

注意: 我还在比较这两个函数,它们的张量流实现更快(预期)。

CPU:

 def normalize(im):
    t1 = time.time()
    b,g,r = cv2.split(im)

    mean_b = np.mean(b)
    mean_g = np.mean(g)
    mean_r = np.mean(r)

    var_b = np.std(b)
    var_g = np.std(g)
    var_r = np.std(r)

    b = np.divide(np.subtract(b, mean_b), var_b)
    g = np.divide(np.subtract(g, mean_g), var_g)
    r = np.divide(np.subtract(r, mean_r), var_r)
    t2 = time.time()
    print ("cpu numpy time ", t2-t1)
    img = cv2.merge((b,g,r))
    return img

Tensorflow:

def normalize(tf_image):
  float_caster = tf.cast(tf_image, tf.float64, name='caster')
  t1 = time.time()
  b, g, r = tf.split(float_caster, 3, axis=2, name="splitter")

  mean_b, var_b = tf.nn.moments(b, axes=[0, 1], name="moment")
  mean_g, var_g = tf.nn.moments(g, axes=[0, 1])
  mean_r, var_r = tf.nn.moments(r, axes=[0, 1])

  b_norm = tf.math.divide(tf.math.subtract(b, mean_b), tf.math.sqrt(var_b))
  g_norm = tf.math.divide(tf.math.subtract(g, mean_g), tf.math.sqrt(var_g))
  r_norm = tf.math.divide(tf.math.subtract(r, mean_r), tf.math.sqrt(var_r))
  t2 = time.time()
  print ("tensorflow time ", t2-t1)
  return b_norm, g_norm, r_norm

这是它的输出:

cpu numpy时间0.029398679733276367

张量流动时间0.014415740966796875

这显示了一些使用GPU的速度游戏,但是我不知道为什么tf.distributions.percentile永远需要...

0 个答案:

没有答案