jetsonhacks / installTensorFlowTX2

Install TensorFlow on the NVIDIA Jetson TX2 Development Kit
MIT License
165 stars 82 forks source link

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

Open ghost opened 6 years ago

ghost commented 6 years ago

System information

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()