[英]Image ordering tensor-flow
I am running the code for sliding window using tensor flow. 我正在使用张量流运行滑动窗口的代码。
It is for Theano, that's why I am getting an error of image dimensions ordering. 这是针对Theano的,这就是为什么我收到图像尺寸排序错误的原因。
Can anyone tell me how to amend it for tensor flow? 谁能告诉我如何针对张量流进行修改?
Error: 错误:
ValueError: Error when checking : expected input_2 to have shape (None, 224, 224, 3) but got array with shape (1, 3, 224, 224) ValueError:检查时出错:预期input_2具有形状(None,224、224、3)但具有形状(1、3、224、224)的数组
image=load_img('\e.jpg')
image= np.array(image).reshape((3264,5896,3))
image = image.astype('float32')
image /= 255
plt.imshow(image)
print(image.shape)
#%%SLIDING WINDOW
''
def find_a_object(image, step=224, window_sizes=[224]):
boxCrack = 0
locations = []
for win_size in window_sizes:
#top = Y, left = X
for Y in range(0, image.shape[0] - win_size + 1, step):
for X in range(0, image.shape[1] - win_size + 1, step):
# compute the (top, left, bottom, right) of the bounding box
box = (Y, X, Y + win_size, X + win_size)
#crop original image
cropped_img = image[box[0]:box[2], box[1]:box[3]]
#reshape cropped image by window
cropped_img = np.array(cropped_img).reshape((1,3,224,224))
boxCrack = predict_function(cropped_img)
if boxCrack ==0:
#print('box classified as crack')
#save location of it
locations.append(box)
print("found")
return locations
#%%FUNCTIONS
def predict_function(x):
result = model.predict_classes(x)
if result==0:
return 0
else:
return 1
##SHOW CROPPED IMAGE
#def show_image(im):
# plt.imshow(im.reshape((100,100,3)))
)
#DRAW BOXES FROM LOCATIONS when classified
def draw_boxes(image, locations):
fix,ax = plt.subplots(1)
ax.imshow(image)
for l in locations:
print (l)
rectR = patches.Rectangle((l[1],l[0]),224,224,linewidth=1,edgecolor='R',facecolor='none'
ax.add_patch(rectR)
#%%get locations from image
locations = find_a_object(image)
draw_boxes(image,locations)
Well, the error message is pretty clear, the ordering in TF should be (None, dim_x, dim,_y, n_channels)
. 好吧,错误消息非常清楚,TF中的顺序应该是
(None, dim_x, dim,_y, n_channels)
。 I think transposing the image array as shown below should do the trick. 我认为按如下所示放置图像数组应该可以解决问题。
cropped_img = image[box[0]:box[2], box[1]:box[3]]
#reshape cropped image by window
cropped_img = np.array(cropped_img).reshape((1,3,224,224))
cropped_img = cropped_img.transpose(0,2,3,1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.