def __call__(self, img: np.ndarray): """ 对图片进行处理,先按照高度进行resize,resize之后如果宽度不足指定宽度,就补黑色像素,否则就强行缩放到指定宽度 :param img_path: 图片地址 :return: """ data_augment = False if self.phase == 'train' and np.random.rand() > 0.5: data_augment = True if data_augment: img_h = 40 img_w = 340 else: img_h = self.img_h img_w = self.img_w h, w = img.shape[:2] ratio_h = float(img_h) / h new_w = int(w * ratio_h) if new_w < img_w and self.pad: img = cv2.resize(img, (new_w, img_h)) if len(img.shape) == 2: img = np.expand_dims(img, 3) step = np.zeros((img_h, img_w - new_w, img.shape[-1]), dtype=img.dtype) img = np.column_stack((img, step)) else: img = cv2.resize(img, (img_w, img_h)) if data_augment: img = nd.array(img) img, _ = image.random_crop(img, (self.img_w, self.img_h)) img = img.asnumpy() return img
def _empirical_cdf_inverse_transform( batch_target_sorted: np.ndarray, batch_predictions: np.ndarray, slopes: np.ndarray, intercepts: np.ndarray, ) -> np.ndarray: """ Apply forward transformation of the empirical CDF. Parameters ---------- batch_target_sorted Sorted targets of the input batch. batch_predictions Predictions of the underlying probability distribution slopes Slopes of the piece-wise linear function. intercepts Intercepts of the piece-wise linear function. Returns ------- outputs Forward transformed outputs. """ slopes = slopes.asnumpy() intercepts = intercepts.asnumpy() batch_target_sorted = batch_target_sorted.asnumpy() batch_size, num_timesteps, target_dim = batch_target_sorted.shape indices = np.floor(batch_predictions * num_timesteps) # indices = indices - 1 # for now project into [0, 1] indices = np.clip(indices, 0, num_timesteps - 1) indices = indices.astype(np.int) transformed = np.where( np.take_along_axis(slopes, indices, axis=1) != 0.0, (batch_predictions - np.take_along_axis(intercepts, indices, axis=1)) / np.take_along_axis(slopes, indices, axis=1), np.take_along_axis(batch_target_sorted, indices, axis=1), ) return transformed