def plot_simclr_images(img1: np.array, img2: np.array, comet_logger: Experiment): fig = plt.figure(figsize=(10, 10)) ax = fig.add_subplot(121) plt.imshow( cv2.cvtColor(np.array(transforms.ToPILImage()(img1.cpu())), cv2.COLOR_BGR2RGB)) ax.set_title("Image 1") ax = fig.add_subplot(122) plt.imshow( cv2.cvtColor(np.array(transforms.ToPILImage()(img2.cpu())), cv2.COLOR_BGR2RGB)) ax.set_title("Image 2") if comet_logger is not None: comet_logger.log_figure(figure=plt) plt.close()
def plot_activity(predictions: np.array, model_name: str, curr_name: str, expert_labels: np.array, returns: np.array, save_fpath: str = None): """Plots trading activity of experts and model""" n_preds = len(predictions) assert n_preds == len(expert_labels) == len(returns[0]), 'Number of predictions has to match number' + \ '# expert labels and size(1) for the return gradient' plt.figure(figsize=(20, 20)) ax = plt.subplot(211) ax.scatter(list(range(n_preds)), expert_labels, alpha=1, color='r', edgecolor='black', s=0.7) im = ax.matshow(returns, alpha=0.6, cmap='RdYlGn', aspect='auto', origin='lower', extent=[0, n_preds, -0.1, 90.1], vmin=-0.08, vmax=0.08) plt.colorbar(im) ax.set_yticks(np.arange(0, 90, 10)) ax.set_yticklabels(['Hold', 'Buy<10d', 'Buy<20d', 'Buy<30d', 'Buy<40d', 'Buy<50d', 'Buy<60d', \ 'Buy<70d', 'Buy<80d', 'Buy<90d']) ax.set_xlabel('Trading Days') plt.title( f'DTCC (oracle) `{curr_name}`, background: {curr_name} return gradient, 1 day tenor (bottom) to 90 days (top)' ) ax = plt.subplot(212) ax.scatter(list(range(n_preds)), predictions.cpu().numpy(), alpha=1, color='y', edgecolor='black', s=0.7) im = ax.matshow(returns, alpha=0.6, cmap='RdYlGn', aspect='auto', origin='lower', extent=[0, n_preds, -0.1, 90.1], vmin=-0.08, vmax=0.08) plt.colorbar(im) ax.set_yticks(np.arange(0, 90, 10)) ax.set_yticklabels(['Hold', 'Buy<10d', 'Buy<20d', 'Buy<30d', 'Buy<40d', 'Buy<50d', 'Buy<60d', \ 'Buy<70d', 'Buy<80d', 'Buy<90d']) ax.set_xlabel('Trading Days') plt.title( f'{model_name} on `{curr_name}`, background: {curr_name} return gradient, 1 day tenor (bottom) to 90 days (top)' ) if save_fpath: plt.savefig(f'{save_fpath}.jpg', dpi=200)
def _apply_impl(self, file_path: PathLike, image: np.array, label: Optional[np.array] = None, **kwargs): assert self._image_info.check_input_image(image) image = self._pre.scale_data(image) image = self._pre.force_dims(image) start_subscript = self._pre.get_random_start_subscript(image) image = self._pre.pad_crop(image, start_subscript) t_image = image.copy() image = self._pre.grayscale(image) image = self._pre.scale_values(image) image = self._pre.torchify(image) image = self._pre.sobelize(image) np_image = image.cpu().detach().numpy().transpose(1, 2, 0) assert self._image_info.check_output_image(np_image) name = PurePath(file_path).stem + "_train" self._save_image(name, np_image) t_image = self._pre.color_jitter(t_image) t_image = self._pre.grayscale(t_image) t_image = self._pre.scale_values(t_image) t_image = self._pre.torchify(t_image) t_image, affine_inverse = self._pre.transform(t_image) t_image = self._pre.sobelize(t_image) np_t_image = t_image.cpu().detach().numpy().transpose(1, 2, 0) assert self._image_info.check_output_image(np_t_image) t_name = name + "_t" self._save_image(t_name, np_t_image) out = { "image": image, "transformed_image": t_image, "affine_inverse": affine_inverse, "file_path": file_path, **kwargs, } if label is not None: assert self._image_info.check_input_label(label) label = self._pre.scale_labels(label) label = self._pre.force_dims(label) label = self._pre.pad_crop(label, start_subscript) label = self._pre.map_labels(label) assert self._image_info.check_output_label(label) self._save_label(name, label) label = self._pre.torchify(label) out["label"] = label return out
def draw_single_image( rasterizer, image: np.array, centroid: np.array, world_to_image: np.array, target_positions: np.array, target_yaws: np.array, predicted_positions: Optional[np.array] = None, predicted_yaws: Optional[np.array] = None, target_color: Optional[tuple] = TARGET_POINTS_COLOR, predicted_color: Optional[tuple] = PREDICTED_POINTS_COLOR, ) -> torch.Tensor: """ Produce a single RGB representation of the rasterized input image and its corresponding position prediction :param rasterizer: :param image: :param centroid: :param world_to_image: :param target_positions: :param target_yaws: :param predicted_positions: :param predicted_yaws: :param target_color: :param predicted_color: :return: """ predicted_yaws = predicted_yaws if predicted_yaws is not None else target_yaws im = _set_image_type(rasterizer.to_rgb(image.cpu().data.numpy().transpose(1, 2, 0))) # Todo enhance draw_trajectory(im, transform_points( target_positions.cpu().data.numpy() + centroid[:2].cpu().data.numpy(), world_to_image.cpu().data.numpy()), target_yaws.cpu().data.numpy(), target_color) if predicted_positions is not None: draw_trajectory(im, transform_points( predicted_positions.cpu().data.numpy() + centroid[:2].cpu().data.numpy(), world_to_image.cpu().data.numpy()), predicted_yaws.cpu().data.numpy(), predicted_color) return np.uint8(im).transpose(2, 0, 1)
def get_felzenszwalb(slices :np.array) -> np.array : slices = slices.cpu().detach().numpy() mid_slice = slices.shape[0] // 2 segments = felzenszwalb(slices[mid_slice,:,:], scale=150, sigma=0.7, min_size=50) selected_pixels = np.array([[5/16, 5/16], [5/16, 11/16], [11/16, 5/16], [11/16, 11/16]]) @ np.array([[256, 0], [0, 256]]) # don't hard code image resolution selected_pixels = selected_pixels.astype('int32') selected_segments = [segments[tuple(sp)] for sp in selected_pixels] pre_mask = [segments == ss for ss in selected_segments] mask = np.logical_or.reduce(pre_mask) return segments, mask
def plot_preds(input_image: np.array, prediction: np.array, target: Tensor, predictions_dir: str, n_images: int = 2, save_fig: bool = True) -> None: """ Takes as input PyTorch tensors, plots the input image, predictions and targets Args: input_image : the input image predictions : the predictions thresholded at 0.5 probability target : Tensor of the targets predictions_dir: directory with oof predictions n_images : number of images to tack on the plot save_fig : if true, saves the figure. Default: True """ # Only select the first n images in a batch prediction = prediction[:n_images] # binarise prediction prediction = torch.sigmoid(prediction) prediction.round() target = target[:n_images] input_image = input_image[:n_images] prediction = prediction.detach().cpu().numpy() preds = np.hstack(prediction) preds_rgb = np.repeat(preds[..., None], 3, axis=2) # repeat for 3 channels to plot #thresholded_pred = np.repeat(preds_rgb[..., None] > 0.5, 3, axis=2) input_image = input_image.cpu().numpy().transpose(0,2,3,1) input_im = np.hstack(input_image[2]) # use Exy channel only for visualisation input_rgb = np.repeat(input_im[..., None], 3, axis=2) # repeat channel 3 times to plot overlayed_im = (input_rgb*0.6 + preds_rgb*0.7).clip(0,1) target = target.detach().cpu().numpy() target = np.hstack(target) target_rgb = np.repeat(target[..., None], 3, axis=2) # repeat for 3 channels to plot fig = plt.figure(figsize=(12,26)) plot_im = np.vstack([input_rgb, overlayed_im, preds_rgb, target_rgb]).clip(0,1).astype(np.float32) plt.imshow(plot_im) plt.axis("off") if save_fig: plt.savefig(os.path.join(predictions_dir, f"preds_{checkpoint_name}.png")) plt.show()
def get_felzenszwalb(slices: np.array) -> np.array: slices = slices.cpu().detach().numpy() mid_slice = slices.shape[0] // 2 segments = felzenszwalb(slices[mid_slice, :, :], scale=150, sigma=0.7, min_size=50) #segments = chan_vese(slices[mid_slice,:,:], mu=0.1) selected_pixels = np.array([ [5 / 16, 5 / 16], [5 / 16, 11 / 16], [11 / 16, 5 / 16], [11 / 16, 11 / 16] ]) @ np.array([[256, 0], [0, 256]]) # don't hard code image resolution selected_pixels = selected_pixels.astype('int32') selected_segments = [segments[tuple(sp)] for sp in selected_pixels] pre_mask = [segments == ss for ss in selected_segments] mask = np.logical_or.reduce(pre_mask) bg_segment = int(np.floor(np.mean(segments[0]))) mid_col = segments.shape[1] // 2 start = timeit.default_timer() max_gap = smart_strip_edges(segments, bg_segment, 1) max_h_gap = smart_strip_edges(segments, bg_segment, 0) stop = timeit.default_timer() print("Step: {} - Runtime: {}".format(3, stop - start)) return segments[max_gap[0]:max_gap[1], max_h_gap[0]:max_h_gap[1]], mask[max_gap[0]:max_gap[1], max_h_gap[0]:max_h_gap[1]]
def _apply_impl(self, file_path: PathLike, image: np.array, label: Optional[np.array] = None, **kwargs): assert self._image_info.check_input_image(image) image = self._pre.scale_data(image) image = self._pre.force_dims(image) start_subscript = self._pre.get_center_start_subscript(image) image = self._pre.pad_crop(image, start_subscript) image = self._pre.grayscale(image) image = self._pre.scale_values(image) image = self._pre.torchify(image) image = self._pre.sobelize(image) np_image = image.cpu().numpy().transpose(1, 2, 0) assert self._image_info.check_output_image(np_image) name = PurePath(file_path).stem + "_test" self._save_image(name, np_image) if label is not None: assert self._image_info.check_input_label(label) label = self._pre.scale_labels(label) label = self._pre.force_dims(label) label = self._pre.pad_crop(label, start_subscript) label = self._pre.map_labels(label) assert self._image_info.check_output_label(label) self._save_label(name, label) label = self._pre.torchify(label) return { "image": image, "label": label, "file_path": file_path, **kwargs }