installTensorFlowTX2 icon indicating copy to clipboard operation
installTensorFlowTX2 copied to clipboard

tf.image.crop_and_resize() return 0 values when assigned to GPU on the Jetson TX2

Open ghost opened this issue 6 years ago • 0 comments

System information

  • Have I written custom code (as opposed to using a stock example script provided in TensorFlow): Yes
  • OS Platform and Distribution (e.g., Linux Ubuntu 16.04): 16.04.LTS
  • TensorFlow installed from (source or binary): Source
  • TensorFlow version (use command below): 1.3.0
  • Python version: 2.7.12
  • Bazel version (if compiling from source): 0.5.2
  • CUDA/cuDNN version: 8.0/6.0.21
  • GPU model and memory: Nvidia Tegra X2
  • Exact command to reproduce:tf.image.crop_and_resize(raw_sample, boxes, box_ind)

Describe the problem

I'm getting completly different results from tensorflow's function tf.image.crop_and_resize(...) when assigned it to gpu and cpu. In other words: -when I run this ops on CPU, I get correct results( I mean, the right crops) -when I put it on the GPU device I get crops fulled with 0 values.

Source code / logs

Here, you can see a simple use case:

import tensorflow as tf 
import numpy as np
import cv2 #Just importing cv2 to read  image, you use PIL or anything else to load it

device='gpu' 

def img2batch_crops(input_image):
    raw_sample_tensor_4d=tf.expand_dims(input_image, 0)
    
    #Setting the size to crop and the final size of cropped images
    patches_top=[0,0.5]
    patches_bottom =[0.5,0.5]
    crop_size = [100,100]
    boxes=tf.stack([patches_top, patches_top, patches_bottom, patches_bottom], axis=1)
    
    ##Here is the bug:
        #When device == 'cpu', I got  results 
        #When device == 'gpu', I got  black cropped images( 0 values)
    with tf.device('/'+device+':0'):  
        crops=tf.image.crop_and_resize(raw_sample_tensor_4d, boxes, box_ind=tf.zeros_like(patches_top, dtype=tf.int32), crop_size=crop_size, name="croper")

    return crops


def main():

	img_data = cv2.imread('image.jpg') #Just loading the image,

	print("Shape and type of image input ",img_data.shape, img_data.dtype) #Print the shape and the type of the image, supposed to be a numpy array

	raw_image = tf.placeholder(dtype=tf.float32, shape=img_data.shape, name='input_image')
     
       crops = img2batch_crops(raw_image) # Adding ops to the graph

	with tf.Session() as sess:
	    myBatchedImages = sess.run(crops, feed_dict={raw_image:img_data})
	    cv2.imwrite('result_'+device+'.jpg',myBatchedImages[0])   ## Savej just one cropped image to see how it looks like

main()

ghost avatar Oct 21 '17 21:10 ghost