def plot_2d_embedded(model,
                     feeder,
                     mode,
                     save_path=None,
                     input_image=False,
                     n=10000):
    y, z, cnt = [], [], 0
    while cnt < n:
        _x, _y = feeder.train.next_batch(model.batch_size)
        y.append(_y)
        _x = shape_2d(_x, model.batch_size) if input_image else _x
        if mode == "conditional":
            _z = model.encode(_x, _y)
        elif mode == "unsupervised":
            _z = model.encode(_x)
        else:
            sys.exit("unknown mode %s !" % mode)
        z.append(_z)
        cnt += model.batch_size
    z = np.vstack(z)
    y = np.vstack(y)
    # plot
    plt.figure(figsize=(8, 6))
    plt.scatter(z[:, 0], z[:, 1], c=np.argmax(y, 1))
    plt.colorbar()
    plt.grid()
    if save_path:
        # plt.savefig("%sembedding.eps" % save_path, bbox_inches="tight")
        plt.savefig("%sembedding.png" % save_path, bbox_inches="tight")
Пример #2
0
def plot_classify(model,
                  mode,
                  feeder,
                  _n=5,
                  save_path=None,
                  input_image=False):
    _x, _y = feeder.test.next_batch(model.batch_size)
    _x = shape_2d(_x, model.batch_size) if input_image else _x

    pred = model.predict(_x, _y)
    # plot
    plt.figure(figsize=(8, 12))
    for i in range(_n):
        plt.subplot(_n, 2, 2 * i + 1)
        plt.imshow(_x[i].reshape(28, 28))
        plt.title("True label: %i; prediction: %i" %
                  (np.argmax(_y[i]), np.argmax(pred[i])))
        plt.colorbar()
        plt.subplot(_n, 2, 2 * i + 2)
        plt.imshow(_x[i].reshape(28, 28))
        plt.title("True label: %i; prediction: %i" %
                  (np.argmax(_y[i]), np.argmax(pred[i])))
        plt.colorbar()
        plt.tight_layout()
    if save_path:
        plt.savefig(save_path + "classification.png", bbox_inches="tight")
def plot_reconstruct(model,
                     mode,
                     feeder,
                     _n=5,
                     save_path=None,
                     input_image=False):
    # feed test data and reconstruct
    _x, _y = feeder.test.next_batch(model.batch_size)
    _x = shape_2d(_x, model.batch_size) if input_image else _x
    if mode == "conditional":
        reconstruction = model.reconstruct(_x, _y)
    elif mode == "unsupervised":
        reconstruction = model.reconstruct(_x)
    # plot
    plt.figure(figsize=(8, 12))
    for i in range(_n):
        plt.subplot(_n, 2, 2 * i + 1)
        plt.imshow(_x[i].reshape(28, 28), vmin=0, vmax=1, cmap="gray")
        plt.title("Test input %i" % np.argmax(_y[i]))
        plt.colorbar()
        plt.subplot(_n, 2, 2 * i + 2)
        plt.imshow(reconstruction[i].reshape(28, 28),
                   vmin=0,
                   vmax=1,
                   cmap="gray")
        plt.title("Reconstruction")
        plt.colorbar()
        plt.tight_layout()
    if save_path:
        # plt.savefig(save_path + "reconstruction.eps", bbox_inches="tight")
        plt.savefig(save_path + "reconstruction.png", bbox_inches="tight")
Пример #4
0
def generate_image_mean(model, feeder, save_path=None, input_image=False):
    _code = {
        0: [],
        1: [],
        2: [],
        3: [],
        4: [],
        5: [],
        6: [],
        7: [],
        8: [],
        9: []
    }
    for i in range(500):
        _x, _y = feeder.test.next_batch(model.batch_size)
        _x = shape_2d(_x, model.batch_size) if input_image else _x
        __code = model.encode(_x, _y).tolist()
        for __x, __y, _c in zip(_x, _y, __code):
            _code[int(np.argmax(__y))].append(_c)
    # Fill each label in _code with the corresponding encodings

    # convert label to one hot vector
    o_h = np.eye(model.label_size)[[i for i in range(model.label_size)]]
    # o_h is just np.eye(model.label_size) itself

    true_label = np.tile(o_h, [int(model.batch_size / model.label_size), 1])

    tmp = np.vstack([np.mean(_code[_a], 0) for _a in range(model.label_size)])
    # Get the average encoding for each digit
    z = np.tile(tmp, [int(model.batch_size / model.label_size), 1])
    # Tile the average codes
    generated = model.decode(true_label, z)
    plt.figure(figsize=(6, 10))
    for i in range(10):
        plt.subplot(5, 2, i + 1)
        plt.imshow(generated[i].reshape(28, 28), vmin=0, vmax=1, cmap="gray")
        plt.title("Generated %i" % np.argmax(true_label[i]))
        plt.colorbar()
        plt.tight_layout()
    if save_path:
        # plt.savefig(save_path + "generated_image_mean.eps", bbox_inches="tight")
        plt.savefig(save_path + "generated_image_mean_" + timestamp + ".png",
                    bbox_inches="tight")
def generate_image_random(model,
                          feeder,
                          save_path=None,
                          n=10,
                          target_digit=None,
                          std=0.01,
                          input_image=False,
                          seed=True):
    # generate latent vector
    if seed:
        if target_digit:
            _code = []
            for i in range(500):
                _x, _y = feeder.test.next_batch(model.batch_size)
                _x = shape_2d(_x, model.batch_size) if input_image else _x
                __code = model.encode(_x, _y).tolist()
                for __x, __y, _c in zip(_x, _y, __code):
                    if np.argmax(__y) == target_digit:
                        _code.append(_c)
            o_h = np.zeros(model.label_size)
            o_h[target_digit] = 1
            _len = model.batch_size
            z = np.tile(np.mean(_code, 0), [_len, 1])
        else:
            target_digit = "all"
            _code = {
                0: [],
                1: [],
                2: [],
                3: [],
                4: [],
                5: [],
                6: [],
                7: [],
                8: [],
                9: []
            }
            for i in range(500):
                _x, _y = mnist.test.next_batch(model.batch_size)
                _x = shape_2d(_x, model.batch_size) if input_image else _x
                __code = model.encode(_x, _y).tolist()
                for __x, __y, _c in zip(_x, _y, __code):
                    _code[int(np.argmax(__y))].append(_c)
            # convert label to one hot vector
            target_vector = [i for i in range(model.label_size)]
            o_h = np.eye(model.label_size)[target_vector]
            _len = int(model.batch_size / model.label_size)
            tmp = np.vstack(
                [np.mean(_code[_a], 0) for _a in range(model.label_size)])
            z = np.tile(tmp, [_len, 1])
        true_label = np.tile(o_h, reps=[_len, 1])
        z += np.random.randn(model.batch_size,
                             model.network_architecture["n_z"]) * std
        generated = model.decode(true_label, z)
    else:
        if target_digit:
            o_h = np.zeros(model.label_size)
            o_h[target_digit] = 1
            _len = model.batch_size
        else:
            target_vector = [i for i in range(model.label_size)]
            o_h = np.eye(model.label_size)[target_vector]
            _len = int(model.batch_size / model.label_size)
        true_label = np.tile(o_h, reps=[_len, 1])
        generated = model.decode(true_label, std=std)

    plt.figure(figsize=(6, 10))
    for i in range(n):
        plt.subplot(5, 2, i + 1)
        plt.imshow(generated[i].reshape(28, 28), vmin=0, vmax=1, cmap="gray")
        plt.title("Generated %i" % np.argmax(true_label[i]))
        plt.colorbar()
        plt.tight_layout()
    if save_path:
        # plt.savefig("%sgenerated_image_rand_%i_%0.3f.eps" % (save_path, target_digit, std), bbox_inches="tight")
        plt.savefig("%sgenerated_image_rand_%s_%0.3f.png" %
                    (save_path, str(target_digit), std),
                    bbox_inches="tight")