我正在使用张量流运行滑动窗口的代码。
这是为了Theano,这就是我得到图像尺寸排序错误的原因。
任何人都可以告诉我如何修改张量流量?
错误:
ValueError:检查时出错:预期input_2具有形状(无,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)