Beispiel #1
0
    def save_features_to_h5(
        self,
        layer_i_features_fname: str,
        indices: tuple,
        subset: tuple,
        label: tuple,
        hidden_states: torch.tensor,
        transformer_model_config,
    ):
        """
        A function that saves the indices of the processed examples,
        the subset labels (tr/va/te), and the layer features of the transformer model
        :param layer_i_features_fname: The name of .h5 file to store the layer features
        :param indices: A list of the batch indices
        :param subset: A list of the batch subset labels
        :param label: A list of the batch target labels
        :param hidden_states: A tensor of the batch hidden states
        :return:
        """
        batch = {
            "index": indices.numpy().astype(int),
            "subset": np.asarray(list(subset)).astype("S20"),
            "label": np.asarray(list(label)).astype("S20"),
            "hidden_states": hidden_states.astype(float),
        }

        if not os.path.isfile(layer_i_features_fname):
            with h5py.File(layer_i_features_fname, "w") as h5f:
                for field, feats in batch.items():
                    h5f.create_dataset(
                        field,
                        data=feats,
                        maxshape=(None, transformer_model_config.hidden_size)
                        if field == "hidden_states" else (None, ),
                        chunks=True,
                    )
        else:
            with h5py.File(layer_i_features_fname, "a") as h5f:
                for field, feats in batch.items():
                    feats_shape = feats.shape[0]
                    resize_shape = h5f[field].shape[0] + feats_shape

                    h5f[field].resize(
                        (resize_shape, transformer_model_config.hidden_size
                         )) if field == "hidden_states" else h5f[field].resize(
                             (resize_shape, ))
                    h5f[field][-feats_shape:] = feats
Beispiel #2
0
def run_opencv(bayer: torch.tensor):
    bayer = bayer.squeeze().cpu().to(torch.float32).numpy() * 255.0
    bayer = bayer.astype(np.uint8)
    rgb = cv2.cvtColor(bayer, cv2.COLOR_BAYER_BG2RGB)
    return torch.tensor(rgb).permute(2, 0, 1).to(torch.float32) / 255.0