Exemple #1
0
def create_mine_canvas(rows, cols, y, x, anchors, spherical=True, gaussian=False):
    lerpv = get_interpfn(spherical, gaussian)
    x = np.clip(x, 0, 1)
    y = np.clip(y, 0, 1)

    _, dim = anchors.shape
    u_list = np.zeros((rows, cols, dim))
    # compute anchors
    cur_anchor = 0
    for ys in range(rows):
        for xs in range(cols):
            if anchors is not None and cur_anchor < len(anchors):
                u_list[ys,xs,:] = anchors[cur_anchor]
                cur_anchor = cur_anchor + 1
            else:
                u_list[ys,xs,:] = np.random.normal(0,1, (1, dim))

    # compute coords
    spaceX = 1.0 / (cols - 1)
    scaledX = x * (cols - 1)
    intX = int(scaledX)
    nextX = intX + 1
    fracX = scaledX - intX

    spaceY = 1.0 / (rows - 1)
    scaledY = y * (rows - 1)
    intY = int(scaledY)
    nextY = intY + 1
    fracY = scaledY - intY

    # hack to allow x=1.0 and y=1.0
    if fracX == 0:
        nextX = intX
    if fracY == 0:
        nextY = intY

    h1 = lerpv(fracX, u_list[intY, intX, :], u_list[intY, nextX, :])
    h2 = lerpv(fracX, u_list[nextY, intX, :], u_list[nextY, nextX, :])

    # interpolate vertically
    result = lerpv(fracY, h1, h2)
    if np.isnan(result[0]):
        print("NAN FOUND")
        print("h1: ", h1)
        print("h2: ", h2)
        print("fracx,y", fracX, fracY)
        print("xvars", spaceX, scaledX, intX, nextX, fracX)
        print("yvars", spaceY, scaledY, intY, nextY, fracY)
        print("inputs", x, y, rows, cols)
    return result
Exemple #2
0
def create_gradient_grid(rows, cols, dim, analogy, anchors, spherical,
                         gaussian):
    """Create a grid of latents with gradient layout (includes analogy)"""
    lerpv = get_interpfn(spherical, gaussian)
    hyper = False

    numsamples = rows * cols
    u_list = np.zeros((numsamples, dim))
    if anchors is not None:
        # xmin_ymin, xmax_ymin, xmin_ymax = anchors[0:3]
        xmin_ymin, xmin_ymax, xmax_ymin = anchors[0:3]
    else:
        xmin_ymin = np.random.normal(0, 1, dim)
        xmax_ymin = np.random.normal(0, 1, dim)
        xmin_ymax = np.random.normal(0, 1, dim)
    if (analogy):
        xmax_ymax = xmin_ymax + (xmax_ymin - xmin_ymin)
        if hyper:
            tl = xmin_ymin
            tr = xmax_ymin
            bl = xmin_ymax
            xmax_ymax = bl + (tr - tl)
            xmin_ymax = bl - (tr - tl)
            xmax_ymin = tr + (tl - bl)
            xmin_ymin = xmin_ymax + (xmax_ymin - xmax_ymax)
    elif anchors is not None:
        xmax_ymax = anchors[3]
    else:
        xmax_ymax = np.random.normal(0, 1, dim)

    for y in range(rows):
        if y == 0:
            # allows rows == 0
            y_frac = 0
        else:
            y_frac = y / (rows - 1.0)
        xmin_ycur = lerpv(y_frac, xmin_ymin, xmin_ymax)
        xmax_ycur = lerpv(y_frac, xmax_ymin, xmax_ymax)
        for x in range(cols):
            if x == 0:
                # allows cols == 0
                x_frac = 0
            else:
                x_frac = x / (cols - 1.0)
            xcur_ycur = lerpv(x_frac, xmin_ycur, xmax_ycur)
            n = y * cols + x
            u_list[n:n + 1, :] = xcur_ycur

    return u_list
Exemple #3
0
def create_mine_grid(rows, cols, dim, space, anchors, spherical, gaussian):
    """Create a grid of latents with splash layout"""
    lerpv = get_interpfn(spherical, gaussian)

    u_list = np.zeros((rows, cols, dim))
    # compute anchors
    cur_anchor = 0
    for y in range(rows):
        for x in range(cols):
            if y % space == 0 and x % space == 0:
                if anchors is not None and cur_anchor < len(anchors):
                    u_list[y, x, :] = anchors[cur_anchor]
                    cur_anchor = cur_anchor + 1
                else:
                    u_list[y, x, :] = np.random.normal(0, 1, (1, dim))
    # interpolate horizontally
    for y in range(rows):
        for x in range(cols):
            if y % space == 0 and x % space != 0:
                lastX = space * (x // space)
                nextX = lastX + space
                fracX = (x - lastX) / float(space)
                #                 print("{} - {} - {}".format(lastX, nextX, fracX))
                u_list[y, x, :] = lerpv(fracX, u_list[y, lastX, :],
                                        u_list[y, nextX, :])
    # interpolate vertically
    for y in range(rows):
        for x in range(cols):
            if y % space != 0:
                lastY = space * (y // space)
                nextY = lastY + space
                fracY = (y - lastY) / float(space)
                u_list[y, x, :] = lerpv(fracY, u_list[lastY, x, :],
                                        u_list[nextY, x, :])

    u_grid = u_list.reshape(rows * cols, dim)

    return u_grid