Hello, anyone know whats wrong with my code?
I think i already add a img_tensor.cpu().data but its doesnt work
i have a code like this…
#Define plot function
def plot_image(img_tensor, annotation, block=True):
fig,ax = plt.subplots(1)
img = img_tensor.cpu().data
# Display the image
ax.imshow( np.array( img.permute(1, 2, 0) ) )
for box, label in zip( annotation["boxes"], annotation["labels"] ):
xmin, ymin, xmax, ymax = box
# Create a Rectangle patch
if label==1:
rect = patches.Rectangle((xmin,ymin),(xmax-xmin),(ymax-ymin),linewidth=1,edgecolor='g',facecolor='none')
elif label==2:
rect = patches.Rectangle((xmin,ymin),(xmax-xmin),(ymax-ymin),linewidth=1,edgecolor='r',facecolor='none')
elif label==3:
rect = patches.Rectangle((xmin,ymin),(xmax-xmin),(ymax-ymin),linewidth=1,edgecolor='y',facecolor='none')
# Add the patch to the Axes
ax.add_patch(rect)
ax.axis("off")
plt.show(block=block)
count=0
model.eval()
for imgs, annotations in test_loader:
imgs = list(img.to(device) for img in imgs)
annotations = [{k: v.to(device) for k, v in t.items()} for t in annotations]
preds = model(imgs)
for i in range(len(imgs)):
print("Prediction")
plot_image(imgs[i], preds[i])
print("Target")
plot_image(imgs[i], annotations[i])
count += 1
if count == 20:#We will check 20 images in test set.
break