Ejemplo n.º 1
0
    def wrap_obs(self, obs: np.array, dummy_value: float = 0.0) -> np.array:
        vardiff = self.to_vars - self.from_vars
        obs = list(obs)
        # insert observation value
        [
            obs.insert(i, dummy_value)
            for i in range(self.from_vars, self.to_vars)
        ]
        # insert intervention value
        [
            obs.insert(i, dummy_value)
            for i in range(self.to_vars + self.from_vars, 2 * self.to_vars)
        ]

        # insert graph state values
        acc_index = self.to_vars * 2
        for i in range(self.from_vars - 1, 1, -1):
            acc_index += i
            [obs.insert(acc_index, dummy_value) for i in range(vardiff)]
            acc_index += vardiff
        # fill up until end
        while acc_index < (int((self.to_vars * 2) + self.to_vars *
                               (self.to_vars - 1) / 2)) - 1:
            obs.append(dummy_value)
            acc_index += 1

        return np.array(obs)
Ejemplo n.º 2
0
def run_saliency_map(model: keras.Model, data: np.array) -> np.array:
    """[summary]

  Args:
      model (keras.Model): [description]
      data (np.array): [description]

  Returns:
      np.array: [description]
  """
    saliency = get_saliency(data, model)

    ts = str(calendar.timegm(time.gmtime()))
    saliency = np.median(saliency, axis=-1)

    filename_path = "./saliency.png"
    save_images_plot(saliency, filename_path)

    # samples_idx = sorted(range(len(saliency)), key=lambda i: saliency[i])[-20:]
    saliency_list = saliency.tolist()
    samples_idx = range(len(saliency_list))

    data = []
    for idx in samples_idx:
        data.append([idx, saliency_list[idx]])

    return data
Ejemplo n.º 3
0
def real_data_loading(data: np.array, seq_len):
    """Load and preprocess real-world datasets.
    Args:
      - data_name: Numpy array with the values from a a Dataset
      - seq_len: sequence length

    Returns:
      - data: preprocessed data.
    """
    # Flip the data to make chronological data
    ori_data = data[::-1]
    # Normalize the data
    scaler = MinMaxScaler().fit(ori_data)
    ori_data = scaler.transform(ori_data)

    # Preprocess the dataset
    temp_data = []
    # Cut data by sequence length
    for i in range(0, len(ori_data) - seq_len):
        _x = ori_data[i:i + seq_len]
        temp_data.append(_x)

    # Mix the datasets (to make it similar to i.i.d)
    idx = np.random.permutation(len(temp_data))
    data = []
    for i in range(len(temp_data)):
        data.append(temp_data[idx[i]])
    return data
Ejemplo n.º 4
0
def Rest(A: np.array, k: int, centers: np.array) -> np.array:
    """
    Determines rest of centers to be used in Gonzalez.
    """
    while k > 0:
        A[centers, :] = np.zeros((np.size(centers), np.shape(A)[1]))
        relevant = A[:, centers]
        min_each_row = np.amin(relevant, axis=1)
        maxmin = np.argmax(min_each_row)
        centers.append(maxmin)
        k -= 1
    return np.array(centers)
def _generate_joints(
    events: np.array,
    joints: np.array,
    subject: str,
    action: str,
    output_joint_path: str,
) -> np.array:

    gt_generator = joint_generator(events, joints, num_events)
    joints = []
    for joint_frame in gt_generator:
        joints.append(joint_frame)
    return np.stack(joints)
Ejemplo n.º 6
0
    def generate(self, starting_sequence: np.array) -> np.array:
        for _ in range(self.sequence_length):
            predicted_character = np.argmax(
                self.model.predict(
                    np.reshape(starting_sequence, (1, len(starting_sequence), 1))
                    / float(len(self.characters))
                )
            )

            starting_sequence.append(predicted_character)
            starting_sequence = starting_sequence[1 : len(starting_sequence)]

        return starting_sequence
Ejemplo n.º 7
0
        def func(x: np.array, inc: float, mu_f: np.array, sigma_f: np.array, acq_f: np.array, x_f: np.array) -> float:
            """function that is internally used as it is passed to the minimize
            takes input and calculates the respective acquisition value and negates it as minimize is used
            but the original problem is maximization

            :param x: input value
            :param inc: incumbent
            :param mu_f: list of mu values to append mu-prediction to for persistence
            :param sigma_f: list of sigma values to append sigma-prediction to for persistence
            :param acq_f: list of acquisition function values to append for to persistence
            :param x_f: list of acquisition function sample inputs to append to for persistence
            :return:
            """
            within_bounds = True
            for i, x_d in enumerate(x):
                if x_d < self.lower_search_bounds[i] or x_d > self.upper_search_bounds[i]:
                    within_bounds = False
            if not within_bounds:
                return 0
            m, s = self.context.estimator.regress([x])
            mu_f.append(m)
            sigma_f.append(s)
            a = self.context.acq.evaluate(m, s, inc)
            acq_f.append(a)
            x_f.append(x)
            return -a
Ejemplo n.º 8
0
        def func(x: np.array, inc: float, mu_f: np.array, sigma_f: np.array,
                 acq_f: np.array, x_f: np.array) -> float:
            """function that is internally used as it is passed to the minimize
            takes input and calculates the respective acquisition value and negates it as minimize is used
            but the original problem is maximization

            :param x: input value
            :param inc: incumbent
            :param mu_f: list of mu values to append mu-prediction to for persistence
            :param sigma_f: list of sigma values to append sigma-prediction to for persistence
            :param acq_f: list of acquisition function values to append for to persistence
            :param x_f: list of acquisition function sample inputs to append to for persistence
            :return:
            """
            m, s = self.context.estimator.regress(np.array([x]))
            mu_f.append(m)
            sigma_f.append(s)
            a = self.context.acq.evaluate(m, s, inc)
            acq_f.append(a)
            x_f.append(x)
            if len(x_f) % 200 == 0:
                gc.collect()

            return -a
Ejemplo n.º 9
0
 def _create_plane_mesh(
     self,
     vertices: np.array,
     vertices_floor: np.array,
     textures: typing.List[np.array],
     texture_floor: np.array,
     texture_ceiling: np.array,
     delta_height: np.array,
     ignore_ceiling: bool = False,
 ) -> open3d.geometry.TriangleMesh:  # create mesh for 3D floorplan visualization
     triangles, triangle_uvs = [], []
     num_walls = len(vertices)  # the number of vertical walls
     # 1. vertical wall (always rectangle)
     num_vertices = 0
     for i in range(len(vertices)):
         triangle = np.array(
             [[0, 2, 1], [2, 0,
                          3]])  # hardcode triangles for each vertical wall
         triangles.append(triangle + num_vertices)
         num_vertices += 4
         triangle_uv = np.array(
             [[i / (num_walls + 2), 0], [i / (num_walls + 2), 1],
              [(i + 1) / (num_walls + 2), 1], [(i + 1) /
                                               (num_walls + 2), 0]],
             dtype=np.float32)
         triangle_uvs.append(triangle_uv)
     # 2. floor and ceiling
     tri = Triangulator(
     )  # Since the floor and ceiling may not be a rectangle, triangulate the polygon first.
     for i in range(len(vertices_floor)):
         tri.add_vertex(vertices_floor[i, 0], vertices_floor[i, 1])
     for i in range(len(vertices_floor)):
         tri.add_polygon_vertex(i)
     tri.triangulate()  # polygon triangulation
     triangle = []
     for i in range(tri.getNumTriangles()):
         triangle.append([
             tri.get_triangle_v0(i),
             tri.get_triangle_v1(i),
             tri.get_triangle_v2(i)
         ])
     triangle = np.array(triangle)
     # add triangles for floor and ceiling
     triangles.append(triangle + num_vertices)
     num_vertices += len(np.unique(triangle))
     if not ignore_ceiling:
         triangles.append(triangle + num_vertices)
     # texture for floor and ceiling
     vertices_floor_min = np.min(vertices_floor[:, :2], axis=0)
     vertices_floor_max = np.max(vertices_floor[:, :2], axis=0)
     # normalize to [0, 1]
     triangle_uv = (vertices_floor[:, :2] - vertices_floor_min) / (
         vertices_floor_max - vertices_floor_min)
     triangle_uv[:, 0] = (triangle_uv[:, 0] + num_walls) / (num_walls + 2)
     triangle_uvs.append(triangle_uv)
     # normalize to [0, 1]
     triangle_uv = (vertices_floor[:, :2] - vertices_floor_min) / (
         vertices_floor_max - vertices_floor_min)
     triangle_uv[:,
                 0] = (triangle_uv[:, 0] + num_walls + 1) / (num_walls + 2)
     triangle_uvs.append(triangle_uv)
     # 3. Merge wall, floor, and ceiling
     vertices.append(vertices_floor)
     vertices.append(vertices_floor + delta_height)
     vertices = np.concatenate(vertices, axis=0)
     triangles = np.concatenate(triangles, axis=0)
     textures.append(texture_floor)
     textures.append(texture_ceiling)
     textures = np.concatenate(textures, axis=1)
     triangle_uvs = np.concatenate(triangle_uvs, axis=0)
     mesh = open3d.geometry.TriangleMesh(
         vertices=open3d.utility.Vector3dVector(vertices),
         triangles=open3d.utility.Vector3iVector(triangles))
     mesh.compute_vertex_normals()
     # mesh.compute_vertex_normals()
     mesh.texture = open3d.geometry.Image(textures)
     mesh.triangle_uvs = np.array(triangle_uvs[triangles.reshape(-1), :],
                                  dtype=np.float64)
     return mesh
Ejemplo n.º 10
0
 def create_mesh(
     self,
     panorama: np.array,
     corners: np.array,
     floor_z: float = -1.6,
     ignore_ceiling: bool = True,
 ):
     H, W, C = panorama.shape
     top_corners, bottom_corners = corners[::2], corners[1::2]
     junctions_floor, junctions_ceiling = self._uv2xyz(
         bottom_corners, top_corners, floor_z, W, H)
     junctions_floor[:, -1] = 0.0
     wall_height = np.mean(junctions_ceiling, axis=0)[-1] + abs(floor_z)
     delta_height = np.array([0, 0, wall_height])
     corners, textures = [], []  # 3D coordinate & texture  for each wall
     camera_center = np.array([0.0, 0.0, abs(floor_z)])
     for corner_i, corner_j in zip(
             junctions_floor, np.roll(junctions_floor, shift=-1, axis=0)):
         corner_j, corner_i = corner_i, corner_j
         texture = self._equirectangular_to_perspective(panorama,
                                                        corner_i,
                                                        corner_j,
                                                        wall_height,
                                                        camera_center,
                                                        input_resolution=W,
                                                        output_resolution=W,
                                                        is_wall=True)
         corner = np.array([
             corner_i, corner_i + delta_height, corner_j + delta_height,
             corner_j
         ])
         corners.append(corner)
         textures.append(texture)
     # the floor/ceiling texture is cropped by the maximum bounding box
     corner_min = np.min(junctions_floor, axis=0)
     corner_max = np.max(junctions_floor, axis=0)
     texture_floor = self._equirectangular_to_perspective(
         panorama,
         corner_min,
         corner_max,
         0,
         camera_center,
         input_resolution=W,
         output_resolution=W,
         is_wall=False)
     texture_ceiling = self._equirectangular_to_perspective(
         panorama,
         corner_min,
         corner_max,
         wall_height,
         camera_center,
         input_resolution=W,
         output_resolution=W,
         is_wall=False)
     mesh = self._create_plane_mesh(corners,
                                    junctions_floor,
                                    textures,
                                    texture_floor,
                                    texture_ceiling,
                                    delta_height,
                                    ignore_ceiling=ignore_ceiling)
     return mesh