ValueError:纵坐标(最后一个)维度应该是 2 或 3,得到 4

问题描述 投票:0回答:1

我正在尝试为我的 Faster-RCNN 对象检测模型生成一个混淆矩阵。但在模型上尝试之前,我决定用简单的数组来尝试。但是我收到一个错误,我不知道应该输入什么。

我在尝试生成混淆矩阵时遇到这样的错误:

Traceback (most recent call last):
  File "c:\Users\lemon\Desktop\ap_py_2\py_deneme.py", line 9, in <module>
    print(evaluation(grounds,preds,0.5))
  File "c:\Users\lemon\Desktop\ap_py_2\confusion_matrix.py", line 30, in evaluation
    f1=shapely.geometry.Polygon(f1)
  File "C:\Users\lemon\miniconda3\envs\cnn-env-03\lib\site-packages\shapely\geometry\polygon.py", line 229, in __new__
    shell = LinearRing(shell)
  File "C:\Users\lemon\miniconda3\envs\cnn-env-03\lib\site-packages\shapely\geometry\polygon.py", line 103, in __new__
    geom = shapely.linearrings(coordinates)
  File "C:\Users\lemon\miniconda3\envs\cnn-env-03\lib\site-packages\shapely\decorators.py", line 77, in wrapped
    return func(*args, **kwargs)
  File "C:\Users\lemon\miniconda3\envs\cnn-env-03\lib\site-packages\shapely\creation.py", line 173, in linearrings
    return lib.linearrings(coords, out=out, **kwargs)
ValueError: The ordinate (last) dimension should be 2 or 3, got 4

测试.py:

from confusion_matrix import evaluation
import torch
import numpy as np

pred = [[13,24,25,46], [13,24,25,46], [13,24,25,46]]
ground = [[13,24,25,46],[13,24,25,46],[13,24,25,46]]
preds = np.array(pred)
grounds = np.array(ground)
print(evaluation(grounds,preds,0.5))

confusion_matrix.py: (source)

#!/usr/bin/env python
# coding: utf-8

import numpy as np
from shapely.geometry import Polygon,Point
import matplotlib.pyplot as plt
import shapely
import cv2 as cv
import os
import gc


def evaluation(ground,pred,iou_value):
  """
  ground= array of ground-truth contours.
  preds = array of predicted contours.
  iou_value= iou treshold for TP and otherwise.
  """
  truth=np.squeeze(ground)
  preds=np.squeeze(pred)
  #we will use this function to check iou less than threshold
  def CheckLess(list1,val):
    return(all(x<=val for x in list1))

  # Using predicted output as the reference
  prob1=[]
  for i in range(len(preds)):
      f1=np.expand_dims(preds[i], axis=0)
      # define a Shapely polygone for prediction i
      f1=shapely.geometry.Polygon(f1)
      # determine the radius
      f1_radius=np.sqrt((f1.area)/np.pi)
      #buffer the polygon fromt the centroid
      f1_buffered=shapely.geometry.Point(f1.centroid).buffer(f1_radius*500)
      cont=[]
      for i in range(len(truth)):
        ff=shapely.geometry.Polygon(np.squeeze(truth[i]))
        if f1_buffered.contains(ff)== True:
          iou=(ff.intersection(f1).area)/(ff.union(f1).area)  
       
          cont.append((iou))

      prob1.append(cont)

  fp=0

  for t in prob1:
    if CheckLess(t,iou_value)==True:
      fp=fp+1
    
  prob2=[]
  #loop through each groun truth instance 
  for i in range(len(truth)):
      f1=truth[i]
      f1=shapely.geometry.Polygon(f1)
      #find radius
      f1_radius=np.sqrt((f1.area)/np.pi)
      #buffer the polygon from the centroid
      f1_buffered=shapely.geometry.Point(f1.centroid).buffer(f1_radius*500)
      cont=[]
      # merge up the ground truth instance against prediction
      # to determine the IoU
      for i in range(len(preds)):
        ff=shapely.geometry.Polygon(np.squeeze(preds[i]))
        if f1_buffered.contains(ff)== True:
          #calculate IoU
          iou=(ff.intersection(f1).area)/(ff.union(f1).area)
          cont.append((iou))
      # probability of a given prediction to be contained in a
      # ground truth instance
      prob2.append(cont)
  fn=0
  tp=0
  for t in prob2:
    if np.sum(t)==0:
      fn=fn+1
    elif CheckLess(t,iou_value)==False:
      tp=tp+1
  
  #lets add this section just to print the results
  print("TP:",tp,"\t FP:",fp,"\t FN:",fn,"\t GT:",truth.shape[0])
  precision=round(tp/(tp+fp),3) 
  recall=round(tp/(tp+fn),3)
  f1= round(2*((precision*recall)/(precision+recall)),3)
  print("Precall:",precision,"\t Recall:",recall, "\t F1 score:",f1)
  
  return tp,fp,fn,precision,recall,f1
python shapely dimension
1个回答
0
投票

您在测试中使用了错误的数据结构。由于来源,你的数据大小应该是

n×m×2
,其中
n
是实例的数量,
m
- 掩码的
(x,y)
对的数量。你有
n=3
,
m=4
并且错过了平面上的第二个坐标。

看起来你尝试在一行前面手动添加一个新维度,这意味着你试图在 4 维空间中用一个点制作一个

Polygon
,但是 Polygon 不接受来自 4D 空间的点并引发错误。设置正确的虚拟数据结构(将大小为 2 的第三维添加到数组
grounds
preds
)。

© www.soinside.com 2019 - 2024. All rights reserved.