'IndexError: index -2 is out of bounds for axis 2 with size 0
I am trying to detect the defect images by using trained weight, through the error " y_pred_decoded_raw[:,:,[-2,-1]] = np.exp(y_pred_decoded_raw[:,:,[-2,-1]] * y_pred[:,:,[-2,-1]]) IndexError: index -2 is out of bounds for axis 2 with size 0".
Error is on below code
if input_coords == 'centroids':
y_pred_decoded_raw[:,:,[-2,-1]] = np.exp(y_pred_decoded_raw[:,:,[-2,-1]] * y_pred[:,:,
[-2,-1]])
y_pred_decoded_raw[:,:,[-2,-1]] *= y_pred[:,:,[-6,-5]]
y_pred_decoded_raw[:,:,[-4,-3]] *= y_pred[:,:,[-4,-3]] * y_pred[:,:,[-6,-5]]
y_pred_decoded_raw[:,:,[-4,-3]] += y_pred[:,:,[-8,-7]]
y_pred_decoded_raw = convert_coordinates(y_pred_decoded_raw, start_index=-4,
conversion='centroids2corners')
Detection Module
orig_images = [] # Store the images here.
input_images = [] # Store resized versions of the images here.
batch_holder = np.zeros((30, img_height, img_width, 3))
img_dir = "Path/to/image directory"
for i,img, in enumerate(os.listdir(img_dir)):
img = os.path.join(img_dir, img)
orig_images.append(imread(img))
img =image.load_img(img, target_size=(img_height, img_width))
batch_holder[i,:] =img
y_pred = model.predict(batch_holder)
print(y_pred)
y_pred_decoded = decode_detections(y_pred,
confidence_thresh=0.5,
iou_threshold=0.45,
top_k=200,
input_coords='centroids',
normalize_coords=True,
img_height=img_height,
img_width=img_width)
np.set_printoptions(precision=2, suppress=True, linewidth=90)
print("Predicted boxes:\n")
print(' class conf xmin ymin xmax ymax')
print(y_pred_decoded[i])
# Display the image and draw the predicted boxes onto it.
# Set the colors for the bounding boxes
colors = plt.cm.hsv(np.linspace(0, 1, 21)).tolist()
classes = ['background',
'defect']
plt.figure(figsize=(20, 12))
current_axis = plt.gca()
for box in y_pred_decoded[i]:
xmin = box[-4] * orig_images[0].shape[1] / img_width
ymin = box[-3] * orig_images[0].shape[0] / img_height
xmax = box[-2] * orig_images[0].shape[1] / img_width
ymax = box[-1] * orig_images[0].shape[0] / img_height
color = colors[int(box[0])]
label = '{}: {:.2f}'.format(classes[int(box[0])], box[1])
current_axis.add_patch(
plt.Rectangle((xmin, ymin), xmax - xmin, ymax - ymin, color=color, fill=False,
linewidth=2))
current_axis.text(xmin, ymin, label, size='x-large', color='white', bbox={'facecolor':
color, 'alpha': 1.0})
plt.imshow(orig_images[i])
plt.show()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
