Пример #1
0
def create_vanilla_auto_encoder(n_features, dimensions):
    ds = MNIST()
    # X is the list of all the images in MNIST dataset
    imgs = ds.X[:1000].reshape((-1, 28, 28))
    # Then create a montage and draw the montage
    plt.imshow(montage(imgs), cmap='gray')
    plt.show()
    mean_img = np.mean(ds.X, axis=0)
    std_img = np.std(imgs, axis=0)
    X = tf.placeholder(tf.float32, [None, n_features])
    current_input = X
    n_input = n_features
    Ws = []
    for layer_i, dimension_i in enumerate(dimensions, start=1):
        with tf.variable_scope("encoder/layer/{}".format(layer_i)):
            w = tf.get_variable(name='W',
                                shape=[n_input, dimension_i],
                                initializer=tf.random_normal_initializer(
                                    mean=0.0, stddev=2.0))
            b = tf.get_variable(name='b',
                                shape=[dimension_i],
                                dtype=tf.float32,
                                initializer=tf.constant_initializer(0.0))

            h = tf.nn.bias_add(name='h',
                               value=tf.matmul(current_input, w),
                               bias=b)
            current_input = tf.nn.relu(h)
            Ws.append(w)
            n_input = dimension_i

    Ws = Ws[::-1]
    dimensions = dimensions[::-1][1:] + [n_features]
    print('dimensions=', dimensions)
    for layer_i, dimension_i in enumerate(dimensions):
        with tf.variable_scope("decoder/layer/{}".format(layer_i)):
            w = tf.transpose(Ws[layer_i])
            b = tf.get_variable(name='b',
                                shape=[dimension_i],
                                dtype=tf.float32,
                                initializer=tf.constant_initializer(0.0))

            print('current_input= ', current_input)
            print('w = ', w)

            h = tf.nn.bias_add(name='h',
                               value=tf.matmul(current_input, w),
                               bias=b)
            current_input = tf.nn.relu(h)
            n_input = dimension_i

    Y = current_input
    cost = tf.reduce_mean(tf.squared_difference(X, Y), 1)
    print('cost.getshape', cost.get_shape())
    cost = tf.reduce_mean(cost)
    learning_rate = 0.01
    optimizer = tf.train.AdamOptimizer(learning_rate).minimize(cost)
    sess = tf.Session()
    sess.run(tf.global_variables_initializer())
    batch_size = 100
    n_epochs = 60
    # We'll try to reconstruct the same first 100 images and show how The network does over the course of training.
    examples = ds.X[:100]
    mean_img = np.mean(examples, axis=0)
    #recon0 = np.clip(examples.reshape((-1, 28, 28)), 0, 255)
    #img_or = montage(recon0).astype(np.uint8)
    #img_or.append('0')
    #gif.build_gif(img_or, saveto='example.{}.gif'.format(np.random.rand()), cmap='gray')
    #plt.show()
    # We'll store the reconstructions in a list
    imgs = []
    fig, ax = plt.subplots(1, 1)
    for epoch_i in range(n_epochs):
        for batch_X, _ in ds.train.next_batch():
            sess.run(optimizer, feed_dict={X: batch_X - mean_img})
        recon = sess.run(Y, feed_dict={X: examples - mean_img})
        recon = np.clip((recon + mean_img).reshape((-1, 28, 28)), 0, 255)
        img_i = montage(recon).astype(np.uint8)
        imgs.append(img_i)
        ax.imshow(img_i, cmap='gray')
        fig.canvas.draw()
        #plt.imshow(img_i, cmap='gray')
        #plt.show()
        print(epoch_i, sess.run(cost, feed_dict={X: batch_X - mean_img}))
    gif.build_gif(imgs,
                  saveto='ae6-learning0.{}.gif'.format(np.random.rand()),
                  cmap='gray')
    ipyd.Image(url='ae.gif?{}'.format(np.random.rand()), height=500, width=500)
    return Y
Пример #2
0
fig, ax = plt.subplots(1, 1)
for epoch_i in range(n_epochs):
    for batch_X, _ in ds.train.next_batch(
    ):  # need to change the output of ds.train.next_batch() to gray-> batch_X.shape (100, 100, 100, 3)
        batch_X_gray = images_to_gray(batch_X)
        batch_X_gray = batch_X_gray.reshape((-1, 10000))
        mean_img = mean_img.reshape(10000, )
        print('batch_X_gray.shape',
              np.array(
                  batch_X_gray).shape)  # batch_X.shape should be (100,10000)
        print('mean_img.shape', np.array(mean_img).shape)
        sess.run(optimizer, feed_dict={X: batch_X_gray - mean_img})
    recon = sess.run(Y, feed_dict={X: examples - mean_img})
    recon = np.clip((recon + mean_img).reshape((-1, 100, 100)), 0, 255)
    img_i = montage(recon).astype(np.uint8)
    imgs.append(img_i)
    ax.imshow(img_i, cmap='gray')
    fig.canvas.draw()
    print(epoch_i, sess.run(cost, feed_dict={X: batch_X_gray - mean_img}))
gif.build_gif(imgs, saveto='Mypic-conv-ae.gif', cmap='gray')
ipyd.Image(url='Mypic-conv-ae.gif?{}'.format(np.random.rand()),
           height=500,
           width=500)

# Visualize the filters
# W1 = sess.run(Ws[1])
#plt.figure(figsize=(10, 10))
#plt.imshow(montage_filters(W1), cmap='coolwarm', interpolation='nearest')
#W2 = sess.run(Ws[2])
#plt.imshow(montage_filters(W2 / np.max(W2)), cmap='coolwarm')
Пример #3
0
    loss = 5.0 * content_loss + 1.0 * style_loss + 0.001 * tv_loss
    optimizer = tf.train.AdamOptimizer(0.05).minimize(loss)

imgs = []
with tf.Session(graph=g) as sess, g.device(device):
    sess.run(tf.initialize_all_variables())

    # map input to noise
    og_img = net_input.eval()

    for it_i in range(n_iterations):
        _, this_loss, synth = sess.run(
            [optimizer, loss, net_input],
            feed_dict={
                'net/dropout_1/random_uniform:0':
                np.ones(
                    g.get_tensor_by_name('net/dropout_1/random_uniform:0').
                    get_shape().as_list()),
                'net/dropout/random_uniform:0':
                np.ones(
                    g.get_tensor_by_name(
                        'net/dropout/random_uniform:0').get_shape().as_list())
            })
        print("%d: %f, (%f - %f)" %
              (it_i, this_loss, np.min(synth), np.max(synth)))
        if it_i % 5 == 0:
            m = vgg16.deprocess(synth[0])
            imgs.append(m)

    gif.build_gif(imgs, saveto='stylenet.gif')
Пример #4
0
# We'll run it for 50 iterations
n_iterations = 50

# Think of this as our learning rate.  This is how much of the gradient we'll add to the input image
step = 1.0

# Every 10 iterations, we'll add an image to a GIF
gif_step = 10

# Storage for our GIF
imgs = []
for it_i in range(n_iterations):
    print(it_i, end=', ')

    # This will calculate the gradient of the layer we chose with respect to the input image.
    this_res = sess.run(gradient[0], feed_dict={x: img_copy})[0]

    # Let's normalize it by the maximum activation
    this_res /= (np.max(np.abs(this_res)) + 1e-8)

    # Then add it to the input image
    img_copy += this_res * step

    # And add to our gif
    if it_i % gif_step == 0:
        imgs.append(normalize(img_copy[0]))

# Build the gif
gif.build_gif(imgs, saveto='1-simplest-mean-layer.gif')
Пример #5
0
optimizer = tf.train.AdamOptimizer(learning_rate).minimize(cost)

# Session to manage vars/train
sess = tf.Session()
sess.run(tf.global_variables_initializer())

# Some parameters for training
batch_size = 100
n_epochs = 5

# We'll try to reconstruct the same first 100 images and show how
# The network does over the course of training.
examples = ds.X[:100]

# We'll store the reconstructions in a list
images = []
fig, ax = plt.subplots(1, 1)
for epoch_i in range(n_epochs):
    for batch_X, _ in ds.train.next_batch():
        sess.run(optimizer, feed_dict={X: batch_X - mean_img})
    recon = sess.run(Y, feed_dict={X: examples - mean_img})
    recon = np.clip((recon + mean_img).reshape((-1, 28, 28)), 0, 255)
    img_i = montage(recon).astype(np.uint8)
    images.append(img_i)
    ax.imshow(img_i, cmap='gray')
    fig.canvas.draw()
    print(epoch_i, sess.run(cost, feed_dict={X: batch_X - mean_img}))
gif.build_gif(images, saveto='conv-ae.gif', cmap='gray')

ipyd.Image(url='conv-ae.gif?{}'.format(np.random.rand()), height=500, width=500)
Пример #6
0
        img = np.clip(ys_pred.reshape(img.shape), 0, 1)
        imgs.append(img)
        # Plot the cost over time
        fig, ax = plt.subplots(1, 2)
        ax[0].plot(costs)
        ax[0].set_xlabel('Iteration')
        ax[0].set_ylabel('Cost')
        ax[1].imshow(img)
        fig.suptitle('Iteration {}'.format(it_i))
        plt.show()


# In[ ]:

# Save the images as a GIF
_ = gif.build_gif(imgs, saveto='single.gif', show_gif=False)


# Let's now display the GIF we've just created:

# In[ ]:

ipyd.Image(url='single.gif?{}'.format(np.random.rand()),
           height=500, width=500)


# <a name="explore"></a>
# ## Explore
# 
# Go back over the previous cells and exploring changing different parameters of the network.  I would suggest first trying to change the `learning_rate` parameter to different values and see how the cost curve changes.  What do you notice?  Try exponents of $10$, e.g. $10^1$, $10^2$, $10^3$... and so on.  Also try changing the `batch_size`: $50, 100, 200, 500, ...$ How does it effect how the cost changes over time?
#