示例#1
0
            ax.plot(inner_ring[0, i:i + 2], inner_ring[1, i:i + 2])
            ax.plot(outer_ring[0, i:i + 2], outer_ring[1, i:i + 2])

    plt.xlim(xmin, xmax)
    plt.ylim(ymin, ymax)
    cur_axes = plt.gca()
    cur_axes.axes.get_xaxis().set_ticks([])
    cur_axes.axes.get_yaxis().set_ticks([])
    if SAVE_SVG:
        plt.savefig("euclidean_center.svg", bbox_inches='tight')
    if SAVE_PNG:
        plt.savefig("euclidean_center.png", dpi=300, bbox_inches='tight')

all_rings = [ring.T for ring in all_rings]

vwb = VOT(all_rings[0][0:-1:5, :], all_rings, verbose=False)
tick = time.clock()
vwb.cluster(lr=0.5, max_iter_h=3000, max_iter_y=1)
tock = time.clock()
print(tock - tick)

if SAVE_OR_PLOT_FIGURE:
    fig = plt.figure(figure_id, figsize=(2, 2))
    ax = fig.add_subplot(111)

    for i in range(N):
        plt.plot(vwb.x[i][:, 0], vwb.x[i][:, 1], linewidth=5, c='lightgray')
    size = vwb.y.shape[0] // 2

    ax.set_prop_cycle(
        cycler(color=[cm(1. * i / (size - 1)) for i in range(size - 1)]))
示例#2
0
from sklearn.cluster import KMeans
kmeans = KMeans(n_clusters=K, init=y).fit(x)

label = kmeans.predict(x)
y = kmeans.cluster_centers_

color_map = np.array([[237, 125, 49, 255], [112, 173, 71, 255], [91, 155, 213, 255]]) / 255

# ---------------VWB---------------

for reg in [0.5, 2, 1e9]:

    y_copy = y.copy()
    x_copy = x.copy()

    vot = VOT(y_copy, [x_copy], verbose=False)
    vot.cluster(lr=0.5, max_iter_h=1000, max_iter_y=1, beta=0.5, reg=reg)

    idx = vot.idx

    fig = plt.figure(figsize=(4, 4))
    ce = color_map[idx[0]]
    utils.scatter_otsamples(vot.y, vot.x[0], size_p=30, marker_p='o', color_x=ce,
                            xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax, facecolor_p='none')
    plt.axis('off')
    # plt.savefig(str(int(reg)) + ".svg", bbox_inches='tight')
    plt.savefig(str(reg) + ".png", dpi=300, bbox_inches='tight')


plt.figure(figsize=(4, 4))
示例#3
0
cov1 = [[0.03, 0], [0, 0.01]]
x1, y1 = np.random.multivariate_normal(mean1, cov1, 5000).T
x1 = np.stack((x1, y1), axis=1).clip(-0.99, 0.99)

mean2 = [0.5, 0.5]
cov2 = [[0.01, 0.], [0., 0.03]]
x2, y2 = np.random.multivariate_normal(mean2, cov2, 1000).T
x2 = np.stack((x2, y2), axis=1).clip(-0.99, 0.99)

mean = [0.0, -0.5]
cov = [[0.02, 0], [0, 0.02]]
K = 50
x, y = np.random.multivariate_normal(mean, cov, K).T
x = np.stack((x, y), axis=1).clip(-0.99, 0.99)

vot = VOT(x, [x1, x2], verbose=False)
vot.cluster(max_iter_h=3000, max_iter_y=1)
idx = vot.idx

xmin, xmax, ymin, ymax = -1., 1., 0., 1.

for k in [21]:
    plt.figure(figsize=(8, 4))
    for i in range(2):
        ce = np.array(plt.get_cmap('viridis')(idx[i] / (K - 1)))
        utils.scatter_otsamples(vot.y,
                                vot.x[i],
                                size_p=30,
                                marker_p='o',
                                color_e=ce,
                                xmin=xmin,
示例#4
0
cys = cys_base[labels]
ys, xs = 15, 3

plt.figure(figsize=(12, 8))
plt.subplot(231)
plt.xlim(xmin, xmax)
plt.ylim(ymin, ymax)
plt.grid(True)
plt.title('w/o reg before')

plt.scatter(x[:, 0], x[:, 1], s=xs, color=utils.COLOR_LIGHT_GREY)
for p, cy in zip(y, cys):
    plt.scatter(p[0], p[1], s=ys, color=cy)

# ------- run WM -------- #
vot = VOT(y.copy(), [x.copy()], label_y=labels, verbose=False)
print("running Wasserstein clustering...")
tick = time.time()
vot.cluster(max_iter_y=1)
tock = time.time()
print("total running time : {0:g} seconds".format(tock - tick))
cxs = cxs_base[vot.label_x[0]]

# ------ plot map ------- #
fig232 = plt.subplot(232)
plt.xlim(xmin, xmax)
plt.ylim(ymin, ymax)
plt.grid(True)
plt.title('w/o reg map')

for p, p0 in zip(vot.y, vot.y_original):
示例#5
0
downsample = 100
x1_down = x1[0:-1:downsample, :]


# ------- run WM -------- #

iterP = 8

downsample = 10
x = x1_down.copy()
x1_copy = x1.copy()
x2_copy = x2.copy()
x3_copy = x3.copy()


vwb = VOT(x, [x1_copy, x2_copy, x3_copy], verbose=False)
output = vwb.cluster(lr=1, max_iter_h=3000, max_iter_y=iterP, lr_decay=500, beta=0.5, icp=True)

fig2 = plt.figure(figsize=(8, 8))
#
ax2 = fig2.add_subplot(111, projection='3d')
#
outE1 = vwb.x[0]
outE2 = vwb.x[1]
outE3 = vwb.x[2]
outP = vwb.y

ax2.scatter(x1[:, 0], x1[:, 1], x1[:, 2], s=dot_size, color=color_map[2], alpha=alpha)
ax2.scatter(x2[:, 0], x2[:, 1], x2[:, 2], s=dot_size, color=color_map[1], alpha=alpha)
ax2.scatter(x3[:, 0], x3[:, 1], x3[:, 2], s=dot_size, color=color_map[2], alpha=alpha)
示例#6
0
y1 = np.cos(u0) * np.sin(v0)
y2 = np.sin(u0) * np.sin(v0)
y3 = np.cos(v0)


plt.show()

x1 = np.stack((x11, x12, x13), axis=1).clip(-0.99, 0.99)
x2 = np.stack((x21, x22, x23), axis=1).clip(-0.99, 0.99)
y = np.stack((y1, y2, y3), axis=1).clip(-0.99, 0.99)

y /= np.linalg.norm(y, axis=1, keepdims=True)
x1 /= np.linalg.norm(x1, axis=1, keepdims=True)
x2 /= np.linalg.norm(x2, axis=1, keepdims=True)

vwb = VOT(y, [x1, x2], verbose=False)
output = vwb.cluster(max_iter_h=5000, max_iter_y=1, space='spherical')
idx = output['idx']

xmin, xmax, ymin, ymax = -1.0, 1.0, -0.5, 0.5
u, v = np.mgrid[np.pi/4:np.pi*5/4:1000j, np.pi/2:np.pi*3/2:1000j]
gx = np.cos(u)*np.sin(v)
gy = np.sin(u)*np.sin(v)
gz = np.cos(v)

for k in [12]:
    fig = plt.figure(figsize=(8, 8))
    ax = fig.add_subplot(111, projection='3d')
    colors = plt.cm.magma((gx - gx.min()) / float((gx - gx.min()).max()))
    ax.plot_surface(gx * 0.95, gy * 0.95, gz * 0.95, antialiased=False, facecolors=colors, linewidth=0, shade=False)
    for i in range(2):
示例#7
0
cov1 = [[0.02, 0], [0, 0.02]]
x1, y1 = np.random.multivariate_normal(mean1, cov1, 5000).T
x1 = np.stack((x1, y1), axis=1).clip(-0.99, 0.99)

mean2 = [0.5, 0.]
cov2 = [[0.02, 0], [0, 0.02]]
x2, y2 = np.random.multivariate_normal(mean2, cov2, 5000).T
x2 = np.stack((x2, y2), axis=1).clip(-0.99, 0.99)

mean = [0.0, -0.5]
cov = [[0.02, 0], [0, 0.02]]
K = 50
x, y = np.random.multivariate_normal(mean, cov, K).T
x = np.stack((x, y), axis=1).clip(-0.99, 0.99)

vot = VOT(x, [x1, x2], verbose=False)
output = vot.cluster(max_iter_h=5000, max_iter_y=1)
idx = vot.idx

xmin, xmax, ymin, ymax = -1.0, 1.0, -0.5, 0.5

for k in [33]:
    plt.figure(figsize=(8, 4))
    for i in range(2):
        ce = np.array(plt.get_cmap('viridis')(idx[i] / (K - 1)))
        utils.scatter_otsamples(vot.y,
                                vot.x[i],
                                size_p=30,
                                marker_p='o',
                                color_x=ce,
                                xmin=xmin,
示例#8
0
cys = cys_base[labels]
ys, xs = 15, 3

plt.figure(figsize=(12, 7))
plt.subplot(231)
plt.xlim(xmin, xmax)
plt.ylim(ymin, ymax)
plt.grid(True)
plt.title('w/o reg before')

plt.scatter(x[:, 0], x[:, 1], s=xs, color=utils.COLOR_LIGHT_GREY)
for p, cy in zip(y, cys):
    plt.scatter(p[0], p[1], s=ys, color=cy)

# ------- run WM -------- #
vot = VOT(y.copy(), [x.copy()], label_y=labels, verbose=False)
print("running Wasserstein clustering...")
tick = time.time()
vot.cluster(lr=0.5, max_iter_y=1)
tock = time.time()
print("total running time : {0:.4f} seconds".format(tock - tick))
cxs = cxs_base[vot.label_x[0]]

# ------ plot map ------- #
fig232 = plt.subplot(232)
plt.xlim(xmin, xmax)
plt.ylim(ymin, ymax)
plt.grid(True)
plt.title('w/o reg map')

for p, p0 in zip(vot.y, vot.y_original):
示例#9
0
mean2 = [0.5, 0.]
cov2 = [[0.02, 0], [0, 0.02]]
x2, y2 = np.random.multivariate_normal(mean2, cov2, 5000).T
x2 = np.stack((x2, y2), axis=1).clip(-0.99, 0.99)

wd = np.zeros((50, 50))

k = 0
for K in [50, 125, 250, 500, 750, 1000, 1500, 2500]:
    for i in range(10):
        mean = [0.0, -0.5]
        cov = [[0.02, 0], [0, 0.02]]
        x, y = np.random.multivariate_normal(mean, cov, K).T
        x = np.stack((x, y), axis=1).clip(-0.99, 0.99)

        vwb = VOT(x, [x1, x2], verbose=False)
        output = vwb.cluster(lr=1, max_iter_h=10000, max_iter_y=1, beta=0.9, lr_decay=500)
        wd[k, i] = output['wd']
        print(output['wd'])
        # np.savetxt(f, output['wd'], delimiter=',')
        del x
        del vwb
    k += 1

# np.savetxt('ship_error/K_total.csv', wd, delimiter=',')


import ot
import ot.plot

M = ot.dist(x1, x2, 'sqeuclidean')
示例#10
0
plt.figure(figsize=(8, 8))
minx, maxx, miny, maxy = -1, 1, -1, 1
plot_map = True


# -------------------- #
# ------- VOT -------- #
# -------------------- #
print('---- Running VOT ----')
dist = cdist(y, x, 'sqeuclidean')

mass_e = np.ones(N0) / N0
mass_p = np.ones(K) / K

tick = time.clock()
vot = VOT(y=y, x=x, verbose=False)
output = vot.cluster(max_iter_y=1, max_iter_h=3000, lr=1, lr_decay=200, beta=0.9)
tock = time.clock()
print('total time: {0:.4f}'.format(tock-tick))


if plot_map:
    # ----- plot before ----- #
    plt.subplot(331)
    plt.xlim(minx, maxx)
    plt.ylim(miny, maxy)
    plt.grid(True)
    plt.title('before')
    plt.scatter(vot.x[0][:, 0], vot.x[0][:, 1], marker='x', color=utils.COLOR_RED)
    plt.scatter(p_coor_before[:, 0], p_coor_before[:, 1], marker='+', color=utils.COLOR_DARK_BLUE)
示例#11
0
                            marker_p='o',
                            color_e=ce,
                            xmin=xmin,
                            xmax=xmax,
                            ymin=ymin,
                            ymax=ymax,
                            facecolor_p='none')
plt.axis('off')
# plt.savefig("kmeans.svg", bbox_inches='tight')
plt.savefig("kmeans.png", dpi=300, bbox_inches='tight')

# --------------- OT ---------------
y_copy = y.copy()
x_copy = x.copy()

vwb = VOT(y_copy, [x_copy], verbose=False)
output = vwb.cluster(lr=0.5,
                     max_iter_h=20,
                     max_iter_y=1,
                     beta=0.5,
                     keep_idx=True)
idx, idxs = output['idx'], output['idxs'][0]

for i in range(0, min(21, len(idxs))):
    fig = plt.figure(figsize=(4, 4))
    ce = color_map[idxs[i]]
    utils.scatter_otsamples(vwb.y,
                            vwb.x[0],
                            nop=True,
                            size_p=30,
                            marker_p='o',
示例#12
0
            color='k',
            zorder=1)
ax6.set_xlabel('R')
ax6.set_ylabel('G')
ax6.set_zlabel('B')
# fig6.savefig("x3_histogram_kmeans1.svg", bbox_inches='tight')
fig6.savefig("x3_histogram_kmeans1.png", dpi=300, bbox_inches='tight')

# -------------- VWB ------------------ #

x1 = x1.clip(-0.99, 0.99)
x2 = x2.clip(-0.99, 0.99)
x3 = x3.clip(-0.99, 0.99)

x = C1_16.clip(-0.99, 0.99)
vwb = VOT(x, [x1], verbose=False)
vwb.cluster(lr=1, max_iter_h=5000, max_iter_y=1)
idx = vwb.idx[0]
x1_vwb_16 = vwb.y[idx, :]
x1_vwb_16 = np.transpose(np.reshape(x1_vwb_16 * 255, (128, 128, 3)), (1, 0, 2))
imageio.imwrite("x1_vwb_16.png", x1_vwb_16)
fig1 = plt.figure(figsize=(8, 8))
ax1 = fig1.add_subplot(111, projection='3d')
ce2 = vwb.y[idx]
p11 = vwb.y
ax1.scatter(x1[:, 0], x1[:, 1], x1[:, 2], s=dot_size, color=ce2, alpha=alpha)
ax1.xaxis.pane.fill = False
ax1.yaxis.pane.fill = False
ax1.zaxis.pane.fill = False
ax1.scatter(p11[:, 0],
            p11[:, 1],