def kldiv_cs_model(prediction, fm): """ Computes Chao-Shen corrected KL-divergence between prediction and fdm made from fixations in fm. Parameters : prediction : np.ndarray a fixation density map fm : FixMat object """ # compute histogram of fixations needed for ChaoShen corrected kl-div # image category must exist (>-1) and image_size must be non-empty assert(len(fm.image_size) == 2 and (fm.image_size[0] > 0) and (fm.image_size[1] > 0)) assert(-1 not in fm.category) # check whether fixmat contains fixations if len(fm.x) == 0: return np.NaN (scale_factor, _) = calc_resize_factor(prediction, fm.image_size) # this specifies left edges of the histogram bins, i.e. fixations between # ]0 binedge[0]] are included. --> fixations are ceiled e_y = np.arange(0, np.round(scale_factor*fm.image_size[0]+1)) e_x = np.arange(0, np.round(scale_factor*fm.image_size[1]+1)) samples = np.array(zip((scale_factor*fm.y), (scale_factor*fm.x))) (fdm, _) = np.histogramdd(samples, (e_y, e_x)) # compute ChaoShen corrected kl-div q = np.array(prediction, copy = True) q[q == 0] = np.finfo(q.dtype).eps q /= np.sum(q) (H, pa, la) = chao_shen(fdm) q = q[fdm > 0] cross_entropy = -np.sum((pa * np.log2(q)) / la) return (cross_entropy - H)
def roc_model(prediction, fm, ctr_loc = None, ctr_size = None): """ wraps roc functionality for model evaluation Parameters: prediction: 2D array the model salience map fm : fixmat Fixations that define locations of the actuals ctr_loc : tuple of (y.x) coordinates, optional Allows to specify control points for spatial bias correction ctr_size : two element tuple, optional Specifies the assumed image size of the control locations, defaults to fm.image_size """ # check if prediction is a valid numpy array assert type(prediction) == np.ndarray # check whether scaling preserved aspect ratio (r_y, r_x) = calc_resize_factor(prediction, fm.image_size) # read out values in the fdm at actual fixation locations # .astype(int) floors numbers in np.array y_index = (r_y * np.array(fm.y-1)).astype(int) x_index = (r_x * np.array(fm.x-1)).astype(int) actuals = prediction[y_index, x_index] if not ctr_loc: xc = np.random.randint(0, prediction.shape[1], 1000) yc = np.random.randint(0, prediction.shape[0], 1000) ctr_loc = (yc.astype(int), xc.astype(int)) else: if not ctr_size: ctr_size = fm.image_size else: (r_y, r_x) = calc_resize_factor(prediction, ctr_size) ctr_loc = ((r_y * np.array(ctr_loc[0])).astype(int), (r_x * np.array(ctr_loc[1])).astype(int)) controls = prediction[ctr_loc[0], ctr_loc[1]] return fast_roc(actuals, controls)[0]
def correlation_model(prediction, fm): """ wraps numpy.corrcoef functionality for model evaluation input: prediction: 2D Matrix the model salience map fm: fixmat Used to compute a FDM to which the prediction is compared. """ (_, r_x) = calc_resize_factor(prediction, fm.image_size) fdm = compute_fdm(fm, scale_factor = r_x) return np.corrcoef(fdm.flatten(), prediction.flatten())[0,1]
def emd_model(prediction, fm): """ wraps emd functionality for model evaluation requires: OpenCV python bindings input: prediction: the model salience map fm : fixmat filtered for the image corresponding to the prediction """ (_, r_x) = calc_resize_factor(prediction, fm.image_size) gt = fixmat.compute_fdm(fm, scale_factor = r_x) return emd(prediction, gt)
def nss_model(prediction, fm): """ wraps nss functionality for model evaluation input: prediction: 2D matrix the model salience map fm : fixmat Fixations that define the actuals """ (r_y, r_x) = calc_resize_factor(prediction, fm.image_size) fix = ((np.array(fm.y-1)*r_y).astype(int), (np.array(fm.x-1)*r_x).astype(int)) return nss(prediction, fix)
def kldiv_model(prediction, fm): """ wraps kldiv functionality for model evaluation input: prediction: 2D matrix the model salience map fm : fixmat Should be filtered for the image corresponding to the prediction """ (_, r_x) = calc_resize_factor(prediction, fm.image_size) q = np.array(prediction, copy=True) q -= np.min(q.flatten()) q /= np.sum(q.flatten()) return kldiv(None, q, distp = fm, scale_factor = r_x)