Пример #1
0
    def map_to_level(self, boxes, canonical_size=224, canonical_level=1, min_level=0, max_level=4):
        x1 = boxes[:, 0]
        y1 = boxes[:, 1]
        x2 = boxes[:, 2]
        y2 = boxes[:, 3]

        w = x2 - x1
        h = y2 - y1

        size = keras.backend.sqrt(w * h)

        levels = backend.floor(canonical_level + backend.log2(size / canonical_size + keras.backend.epsilon()))
        levels = keras.backend.clip(levels, min_level, max_level)

        return levels
Пример #2
0
    def _interpolate(im, x, y, z, out_height, out_width, out_depth):
        # *_f are floats
        num_batch, height, width, depth, channels = im.shape
        height_f = K.cast(height, K.floatx)
        width_f = K.cast(width, K.floatx)
        depth_f = K.cast(depth, K.floatx)

        # clip coordinates to [-1, 1]
        x = K.clip(x, -1, 1)
        y = K.clip(y, -1, 1)
        z = K.clip(z, -1, 1)

        # scale coordinates from [-1, 1] to [0, width/height/depth - 1]
        x = (x + 1) / 2 * (width_f - 1)
        y = (y + 1) / 2 * (height_f - 1)
        z = (z + 1) / 2 * (depth_f - 1)

        # obtain indices of the 2x2x2 pixel neighborhood surrounding the coordinates;
        # we need those in floatX for interpolation and in int64 for indexing. for
        # indexing, we need to take care they do not extend past the image.
        x0_f = K.floor(x)
        y0_f = K.floor(y)
        z0_f = K.floor(z)
        x1_f = x0_f + 1
        y1_f = y0_f + 1
        z1_f = z0_f + 1
        x0 = K.cast(x0_f, 'int64')
        y0 = K.cast(y0_f, 'int64')
        z0 = K.cast(z0_f, 'int64')
        x1 = K.cast(K.minimum(x1_f, width_f - 1), 'int64')
        y1 = K.cast(K.minimum(y1_f, height_f - 1), 'int64')
        z1 = K.cast(K.minimum(z1_f, depth_f - 1), 'int64')

        # The input is [num_batch, height, width, depth, channels]. We do the lookup in
        # the flattened input, i.e [num_batch*height*width*depth, channels]. We need
        # to offset all indices to match the flat version
        dim1 = height * width * depth
        dim2 = width * depth
        dim3 = depth
        base = K.repeat(
            K.arange(num_batch, dtype='int64') * dim1,
            out_height * out_width * out_depth)
        base_y0 = base + y0 * dim2
        base_y1 = base + y1 * dim2
        base_x0 = x0 * dim3
        base_x1 = x1 * dim3
        idx_a = base_y0 + base_x0 + z0
        idx_b = base_y1 + base_x0 + z0
        idx_c = base_y0 + base_x1 + z0
        idx_d = base_y1 + base_x1 + z0
        idx_e = base_y0 + base_x0 + z1
        idx_f = base_y1 + base_x0 + z1
        idx_g = base_y0 + base_x1 + z1
        idx_h = base_y1 + base_x1 + z1

        # use indices to lookup pixels for all samples
        im_flat = im.reshape((-1, channels))
        Ia = im_flat[idx_a]
        Ib = im_flat[idx_b]
        Ic = im_flat[idx_c]
        Id = im_flat[idx_d]
        Ie = im_flat[idx_e]
        If = im_flat[idx_f]
        Ig = im_flat[idx_g]
        Ih = im_flat[idx_h]

        # calculate interpolated values
        wa = ((x1_f - x) * (y1_f - y) * (z1_f - z)).dimshuffle(0, 'x')
        wb = ((x1_f - x) * (y - y0_f) * (z1_f - z)).dimshuffle(0, 'x')
        wc = ((x - x0_f) * (y1_f - y) * (z1_f - z)).dimshuffle(0, 'x')
        wd = ((x - x0_f) * (y - y0_f) * (z1_f - z)).dimshuffle(0, 'x')
        we = ((x1_f - x) * (y1_f - y) * (z0_f - z)).dimshuffle(0, 'x')
        wf = ((x1_f - x) * (y - y0_f) * (z0_f - z)).dimshuffle(0, 'x')
        wg = ((x - x0_f) * (y1_f - y) * (z0_f - z)).dimshuffle(0, 'x')
        wh = ((x - x0_f) * (y - y0_f) * (z0_f - z)).dimshuffle(0, 'x')
        output = K.sum([
            wa * Ia, wb * Ib, wc * Ic, wd * Id, we * Ie, wf * If, wg * Ig,
            wh * Ih
        ],
                       axis=0)
        return output
def FL(x):
    return K.floor(x)