tf.image.crop_and_resize(raw_sample, boxes, box_ind)
当我将其分配给gpu和cpu时,我得到了与tensorflow函数tf.image.crop_and_resize(...)
完全不同的结果。
换句话说:
当我在CPU上运行此操作时,我得到了正确的结果(我的意思是,正确的作物)
当我把它放在GPU设备上时,我的作物被充满了0值。
在这里,您可以看到一个简单的用例:
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()