[英]Image ordering tensor-flow
我正在使用張量流運行滑動窗口的代碼。
這是針對Theano的,這就是為什么我收到圖像尺寸排序錯誤的原因。
誰能告訴我如何針對張量流進行修改?
錯誤:
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)
好吧,錯誤消息非常清楚,TF中的順序應該是(None, dim_x, dim,_y, n_channels)
。 我認為按如下所示放置圖像數組應該可以解決問題。
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.