U ˆófb/ã@sXddlZddlZddlZddlZddlmZddd„Zddd „Z dd d „Z dd d„Z dS)éNçà?Fc Cs¦|j|jkrtdƒ‚t |dd¡ t¡}t |dd¡|k t¡}d||}||}t |dk¡}t |dk¡} |} d| | } |r¢td| ƒtd| ƒtd| ƒ| S) aCompute pixel-wise intersection over union. Multiplies truth_mask by 2, and subtract. Make sure arrays are clipped so that overlapping regions don't cause problems Arguments --------- truth_mask : :class:`numpy.ndarray` 2-D binary array of ground truth pixels. prop_mask : :class:`numpy.ndarray` 2-D array of proposals. prop_threshold : float, optional The threshold for proposal values to be defined as positive (``1``) or negative (``0``) predictions. Values >= `prop_threshold` will be set to ``1``, values < `prop_threshold` will be set to ``0``. verbose : bool, optional Switch to print relevant values. Returns ------- iou : float Intersection over union of ground truth and proposal z;The shape of `truth_mask` and `prop_mask` must be the same.rééçð?z intersection:zunion:ziou:)ÚshapeÚ ValueErrorÚnpÚclipÚastypeÚfloatÚsumÚprint) Ú truth_maskÚ prop_maskÚprop_thresholdÚverboseÚtruth_mask_clipÚprop_mask_clipÚsub_maskZadd_maskÚtp_countÚunionÚ intersectionÚiou©rúh/home/ec2-user/SageMaker/vegetation-management-remars2022/remars2022-workshop/libs/solaris/eval/pixel.pyrs      rÚéÈc  Csjt |dd¡ t¡} t |dd¡|k t¡} d| | } tt | dk¡dƒ} tt | dk¡dƒ} tt | dk¡dƒ}tt | dk¡dƒ}tt | dk¡dƒ}| dkrô| dkrôt| ƒt| |ƒ}t| ƒt| |ƒ}d||||}n d\}}}|rbtd|jƒtd|jƒtd|ƒtd |ƒtd |ƒtd | ƒtd |ƒtd |ƒtd|ƒ|r`d}t   ¡}dt t  |d¡ƒdt t  |d¡ƒdt t  |d¡ƒ}|rÜt j dddddd\}\\}}\}}nt j dddddd\}\}}}t j||dt|ƒdkr| t |d¡¡t jj}| dd¡| dd¡| t¡}tj||dk<|j||dtjjddddd|jd |dn| | ¡|jd!|d| d"¡| | ¡| d"¡|jd#|d|rú| | ¡}| |¡| d"¡n | | ¡| d"¡|jd$|dt jd%d&t|ƒdkrDt j||d'td(t   ¡|d)ƒt  ¡|||fS)*aCompute pixel-wise precision, recall, and f1 score. Find true pos, false pos, true neg, false neg as well as f1 score. Multiply truth_mask by 2, and subtract. Make sure arrays are clipped so that overlapping regions don't cause problems. Arguments --------- truth_mask : :class:`numpy.ndarray` 2-D binary array of ground truth pixels. prop_mask : :class:`numpy.ndarray` 2-D array of proposals. prop_threshold : float, optional The threshold for proposal values to be defined as positive (``1``) or negative (``0``) predictions. Values >= `prop_threshold` will be set to ``1``, values < `prop_threshold` will be set to ``0``. show_plot : bool, optional Switch to plot the outputs. Defaults to ``False``. im_file : str, optional Image file corresponding to the masks. Ignored if ``show_plot == False``. Defaults to ``''``. show_colorbar : bool, optional Switch to show colorbar. Ignored if ``show_plot == False``. Defaults to ``False``. plot_file : str, optional Output file if plotting. Ignored if ``show_plot == False``. Defaults to ``''``. dpi : int, optional Dots per inch for plotting. Ignored if ``show_plot == False``. Defaults to ``200``. verbose : bool, optional Switch to print relevant values. Returns ------- f1 : float Pixel-wise F1 score. precision : float Pixel-wise precision. recall : float Pixel-wise recall. rrréÿÿÿÿ)rrrú mask.shape: ú num pixels: z false_neg: z false_pos: z true_neg: z true_pos: ú precision: ú recall: z F1 score: éz Precision: éz Recall: z F1: T)r"r")ÚsharexÚshareyÚfigsize)g#@r#)ÚfontsizeÚorangerrgÍÌÌÌÌÌì?F)ÚvminÚvmaxr )ÚcmapÚalphaÚnormztruth_mask_clip + imagerÚoffrZ subtract_maskgš™™™™™é?)Útop)Údpiz!Time to create and save F1 plots:Úseconds) rr r r ÚlenÚwherer rÚsizeÚtimeÚstrÚroundÚpltÚsubplotsÚsuptitleÚimshowÚcv2ÚimreadÚcmÚgrayÚset_overÚnanÚ matplotlibÚcolorsÚ NormalizeÚ set_titleÚaxisÚpcolorÚcolorbarÚsubplots_adjustÚsavefigÚshow)rrrZ show_plotZim_fileZ show_colorbarZ plot_filer0rrrrZn_posrZfp_countZtn_countZfn_countÚ precisionÚrecallÚf1r'Út0ÚtitleÚfigÚax1Úax2Zax3Zax4ÚpaletteÚzrrrrN8sž-           ÿÿþþ þ þ    ÿÿ          rNr#c CsDtd||ƒ}t|||ƒ}td||ƒ}t|||ƒ}||||fS)z3Get neighbors of point p with pixel coords row, colr)ÚmaxÚmin) ÚrowÚcolÚhÚwÚrhoÚrowminÚrowmaxÚcolminÚcolmaxrrrÚ_get_neighborhood_limitsÌs rac CsÈt |dd¡ t¡}t |dd¡ t¡}tt |dk¡dƒ}tt |dk¡dƒ}d}d} |j\} } t| ƒD]¨} t| ƒD]š} || | }|| | }t| | | | |d\}}}}|||…||…f}|||…||…f}|dkröt  |¡dkrö|d7}|dkrzt  |¡dkrz| d7} qzqn|dkr(d}n d| |}|dkrDd}n d||}|dkrz|dkrzd||||}nd}|rºt d|jƒt d|j ƒt d|ƒt d |ƒt d t ƒ|||f}|S) uV Compute relaxed f1 score Notes ----- Also find relaxed precision, recall, f1. http://www.cs.toronto.edu/~fritz/absps/road_detection.pdf "completenetess represents the fraction of true road pixels that are within Ï pixels of a predicted road pixel, while correctness measures the fraction of predicted road pixels that are within Ï pixels of a true road pixel." https://arxiv.org/pdf/1711.10684.pdf The relaxed precision is defined as the fraction of number of pixels predicted as road within a range of Ï pixels from pixels labeled as road. The relaxed recall is the fraction of number of pixels labeled as road that are within a range of Ï pixels from pixels predicted as road. http://ceur-ws.org/Vol-156/paper5.pdf Arguments --------- truth_mask : np array 2-D array of ground truth. prop_mask : np array 2-D array of proposals. radius : int Radius in pixels to use for relaxed f1. verbose : bool Switch to print relevant values Returns ------- output : tuple Tuple containing [relaxed_f1, relaxed_precision, relaxed_recall] Examples -------- >>> truth_mask = np.zeros(shape=(10, 10)) >>> prop_mask = np.zeros(shape=(10, 10)) >>> truth_mask[5, :] = 1 >>> prop_mask[5, :] = 1 >>> prop_mask[:, 2] = 0 >>> prop_mask[:, 3] = 1 >>> prop_mask[6:8, :] = 0 >>> prop_mask array([[0., 0., 0., 1., 0., 0., 0., 0., 0., 0.], [0., 0., 0., 1., 0., 0., 0., 0., 0., 0.], [0., 0., 0., 1., 0., 0., 0., 0., 0., 0.], [0., 0., 0., 1., 0., 0., 0., 0., 0., 0.], [0., 0., 0., 1., 0., 0., 0., 0., 0., 0.], [1., 1., 0., 1., 1., 1., 1., 1., 1., 1.], [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], [0., 0., 0., 1., 0., 0., 0., 0., 0., 0.], [0., 0., 0., 1., 0., 0., 0., 0., 0., 0.]]) >>>truth_mask array([[0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.], [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]]) >>> relaxed_f1(truth_mask, prop_mask, radius=3) (0.8571428571428571, 0.75, 1.0) rr)r\rrrrr r!z rF1 score: ) rr r r r2r3rÚrangerarVr r4rN)rrÚradiusrrrZn_truthZn_propZprecision_countZ recall_countrZr[rXrYZ truth_valZprop_valr]r^r_r`Z truth_winZprop_winZrelaxed_recallZrelaxed_precisionÚ relaxed_f1Úoutputrrrrd×sZJ     ÿ      ÿ      rd)rF)rFrFrrF)r#)r#F) r5r<ÚnumpyrrBÚmatplotlib.pyplotÚpyplotr8rrNrardrrrrÚs  0ÿ