U fbK@sddlmZddlmZddZdddZdd Zd d Zd d Z ej j ej j eee ej j ej j ej j ej jej jej jej jej jej jej jej jej jej jej jej jdZdS))backend)kerascCsg}g}|dddgdkr&g}n(|dddgD]}|t|q:|dddgdkrlg}n(|dddgD]}|t|q||dS)zMLoad model training metrics from a config file for a specific framework. trainingmetricsN validation)trainval)getappend metric_dict) frameworkconfigZtraining_metricsZvalidation_metricsmrj/home/ec2-user/SageMaker/vegetation-management-remars2022/remars2022-workshop/libs/solaris/nets/metrics.py get_metricssrHz>cCsxttjt|ddddddf}t|dddf}tj||dd}tj||dd}td |||S) z Dice coefficient for 2 categories. Ignores background pixel label 0 Pass to model as metric during compile statement int32) num_classes.Naxisg@)Kflattenone_hotcastsummean)y_truey_predZsmoothZy_true_fZy_pred_f intersectdenomrrrdice_coef_binarys r$cCsztt|td}tt|td}ttjtjtj||gdddddd}t|t}||}|S)zXPrecision for foreground pixels. Calculates pixelwise precision TP/(TP + FP). rrrfloat64dtyperroundclipepsilonrrallstack)r r!truthpred_postrue_posZ pred_pos_ct precisionrrrr1)s"r1c Cstt|td}tt|td}ttjtjtj||gdddddd}ttt|td}|dkrdS||}|S)zUPrecision for foreground pixels. Calculates pixelwise recall TP/(TP + FN). rrrr%r&rr()r r!r.r/r0Ztruth_ctrecallrrrr2:s"r2cCs,t||}t||}d||||}|S)zF1 score for foreground pixels ONLY. Calculates pixelwise F1 score for the foreground pixels (mask value == 1). Returns NaN if the model does not identify any foreground pixels in the image. r)r1r2)r r!precrecf1_scorerrrr5Ms  r5)accuracybinary_accuracyr1r2r5categorical_accuracycosinecosine_proximityhinge squared_hingekldkullback_leibler_divergencemaemean_absolute_errormsemean_squared_errormslemean_squared_logarithmic_errorsparse_categorical_accuracytop_k_categorical_accuracyN)r)Ztensorflow.kerasrr tensorflowrrr$r1r2r5rr7r8CosineSimilarityr;r<r>r@rBrDrErFr rrrrs6