Example #1
0
def show06():

    start_image_file = '000238.jpg'
    end_image_file = '000193.jpg'

    images, _ = data_flow.next()

    z_points = vae.encoder.predict(images)

    interpolated_images = []

    interpolated_images.append(images[0]) # Add the first selected image.

    factors = np.arange(0, 1, 0.1)

    for factor in factors:

        z = z_points[0] * (1 - factor) + z_points[1]  * factor
        img = vae.decoder.predict(np.array([z]))[0]
        interpolated_images.append(img)

    interpolated_images.append(images[1]) # Add the second selected image.

    num_interpolated_images = len(factors)

    viewer = Viewer(num_rows=1, num_cols=num_interpolated_images+2, width=num_interpolated_images+2, height=1)
    viewer.add_row(interpolated_images)
    viewer.show()
Example #2
0
def show03():

    viewer = Viewer(num_rows=3, num_cols=10, width=10, height=3)

    for i in range(3):
        z_points = np.random.normal(size=(10, vae.z_dim))
        generated_images = vae.decoder.predict(np.array(z_points))
        viewer.add_row(generated_images)

    viewer.show()
Example #3
0
def show05():

    z_avg_pos = np.zeros(shape=vae.z_dim, dtype='float32') # for the attribute with '+1'
    z_avg_neg = np.zeros(shape=vae.z_dim, dtype='float32') # for the attribute with '-1'

    z_count_pos = 0
    z_count_neg = 0

    while True:

        images, labels = data_flow.next()

        z_points = vae.encoder.predict(images)

        z_points_pos = z_points[labels== 1]
        z_points_neg = z_points[labels==-1]

        z_avg_pos += np.sum(z_points_pos, axis=0)
        z_avg_neg += np.sum(z_points_neg, axis=0)

        z_count_pos += len(z_points_pos)
        z_count_neg += len(z_points_neg)

        if z_count_pos > 2000: # 2000 points are enough to get the center.
            break;

    z_avg_pos = z_avg_pos / z_count_pos
    z_avg_neg = z_avg_neg / z_count_neg

    z_direction = z_avg_pos - z_avg_neg
    magnitude = np.linalg.norm(z_direction) # magnitude
    z_direction = z_direction / magnitude # normalization

    # Select the first five images from the next batch.
    images, _ = data_flow.next()[:5]

    z_points = vae.encoder.predict(images)

    z_scales = [-4, -3, -2, -1, 0, 1, 2, 3, 4]

    viewer = Viewer(num_rows=5, num_cols=9, width=9, height=5)

    for i in range(5):

        images = []

        for z_scale in z_scales:

            changed_z_point = z_points[i] + z_direction * z_scale
            changed_image = vae.decoder.predict(np.array([changed_z_point]))[0]
            images.append(changed_image)

        viewer.add_row(images)

    viewer.show()
Example #4
0
def show01():

    input_images, _ = data_flow.next() # Get the first batch.
    input_images = input_images[:10] # Select the first 10 images from it.

    # method 1
    #z_points = vae.encoder.predict(input_images)
    #reconstructed_images = vae.decoder.predict(z_points)
    #print(len(z_points)) = 10: # of inputs
    #print(z_points.shape) = (10, 200): (# of inputs, z_dim)

    # method 2
    output_images = vae.model.predict(input_images)

    viewer = Viewer(num_rows=2, num_cols=10, width=10, height=2)
    viewer.add_row(input_images)
    viewer.add_row(output_images)
    viewer.show()
    def _export_result(self, epoch):

        viewer = Viewer(num_rows=5, num_cols=5, width=15, height=15)

        for i in range(5):
            noise = np.random.normal(0, 1, (5, self.z_dim))
            imgs = self.generator.predict(noise)
            imgs = ((127.5 * imgs) + 127.5).astype('uint8').clip(
                0, 255)  # from [-1.0, +1.0] to [0, 255]
            viewer.add_row(imgs)

        viewer.save('result/QuickDraw_GAN/sample_%d.png' % epoch)
Example #6
0
def show04():

    viewer = Viewer(num_rows=5, num_cols=10, width=10, height=5)

    for i in range(5):

        satisfied_images = []

        while True:

            images, labels = data_flow.next()

            for j in range(len(images)):
                if labels[j] == 1: # satisfied!
                    satisfied_images.append(images[j])

            if len(satisfied_images) > 10:
                break

        viewer.add_row(satisfied_images[:10]) # Show only the first 10 images in a column.

    viewer.show()
iterations = 5  # to augment the data 5 times

data_flow = data_gen.flow_from_directory(
    directory='./data/animals',  # data directory path
    target_size=(100,
                 100),  # from (128, 128) to (100, 100) (NOTE: no channels)
    batch_size=
    batch_size,  # batch size specifying how many images to be read at once
    shuffle=True,  # random shuffling
    class_mode='categorical'  # to get the one-hot encoded labels
    #class_mode  = 'binary' # to get the single digit labels
)

######################
# iteration method 1 #
viewer = Viewer(num_rows=iterations, num_cols=batch_size, width=10, height=10)

for i in range(iterations):

    images, labels = [], []

    image, label = data_flow.next()

    for j in range(batch_size):
        images.append(image[j])
        labels.append(label[j])

    viewer.add_row(images, labels)

viewer.show()
# shuffle
weights = model.get_weights()
weights = [np.random.permutation(w.flat).reshape(w.shape) for w in weights]
model.set_weights(weights)

# load
try:
    model = load_model('result/model_saved')
    print('Succeeded to load')
except ValueError:
    print('Failed to load.')

########################
# evaluating the model #

model.evaluate(x_test, y_test, batch_size=1000)

######################
# drawing the result #

indices = select_indices(len(x_test), 10)
x = select_items(x_test, indices)
y = select_items(y_test, indices)

answer = to_classes(y, 'CIFAR10')
predicted = to_classes(model.predict(x), 'CIFAR10')

viewer = Viewer(num_rows=2, num_cols=10, width=15, height=2)
viewer.add_row(x, answer, predicted)
viewer.show()
Example #9
0
plt.scatter(z_points[:, 0],
            z_points[:, 1],
            cmap='rainbow',
            c=y_test,
            alpha=0.5,
            s=2)
plt.colorbar()

num_samples = 30
z_samples = generate_random_points(num_samples, xMin, xMax, yMin, yMax)
plt.scatter(z_samples[:, 0], z_samples[:, 1], c='black', alpha=1, s=20)

# reconstructed images
reconstructed_images = ae.decoder.predict(z_samples)
z_samples = np.round(z_samples, 1)
viewer = Viewer(num_rows=3, num_cols=10, width=15, height=3)
viewer.add_row(reconstructed_images[0:10], z_samples[0:10])
viewer.add_row(reconstructed_images[10:20], z_samples[10:20])
viewer.add_row(reconstructed_images[20:30], z_samples[20:30])
viewer.show()

plt.show()

##############################################
# drawing the result - 20x20 uniform samples #

plt.figure(figsize=(12, 12))
plt.scatter(z_points[:, 0],
            z_points[:, 1],
            cmap='rainbow',
            c=y_test,
data_gen = ImageDataGenerator(rescale=1./255., validation_split=0.25)

data_flow = data_gen.flow_from_dataframe(
    dataframe   = labels,                  # labels
    directory   = './data/Cifar-10/train', # path to the directory which contains all the images.
    x_col       = 'id',                    # the name of the column which contains the filenames of the images
    y_col       = 'label',                 # If class_mode is not 'raw' or not 'input' you should pass the name of the column which contains the class names.
                                           # None, if used for test_generator.
    subset      = 'training',              # This is for the training.
    batch_size  = 6,                       # batch size: the number of input data that will be propagated through the network at once
    shuffle     = True,                    # to shuffle or not
    class_mode  = 'categorical',           # 'categorical'(default), 'binary', 'sparse', 'input', 'raw', 'other', None
    target_size = (32, 32)                 # image resolution
)

viewer = Viewer(num_rows=5, num_cols=6, width=10, height=10)

for i in range(5):

    # method 1
    images, labels = data_flow.next()

    # method 2
    images, labels = next(data_flow)

    # method 3
    batch = next(data_flow)
    images = batch[0]
    labels = batch[1]

    classes = to_classes(labels, 'CIFAR10')