예제 #1
0
 def test_input_pipeline(self):
     Xs, Ys = dsu.tiny_imagenet_load()
     n_batches = 0
     batch_size = 10
     with tf.Graph().as_default(), tf.Session() as sess:
         batch_generator = dsu.create_input_pipeline(
             Xs[:100],
             batch_size=batch_size,
             n_epochs=1,
             shape=(64, 64, 3),
             crop_shape=(64, 64, 3))
         init_op = tf.group(tf.global_variables_initializer(),
                            tf.local_variables_initializer())
         sess.run(init_op)
         coord = tf.train.Coordinator()
         tf.get_default_graph().finalize()
         threads = tf.train.start_queue_runners(sess=sess, coord=coord)
         try:
             while not coord.should_stop():
                 batch = sess.run(batch_generator)
                 assert (batch.shape == (batch_size, 64, 64, 3))
                 n_batches += 1
         except tf.errors.OutOfRangeError:
             pass
         finally:
             coord.request_stop()
         coord.join(threads)
     assert (n_batches == 10)
예제 #2
0
 def test_draw_train_input_pipeline(self):
     Xs, ys = dsu.tiny_imagenet_load()
     draw.train_input_pipeline(Xs[:100],
                               batch_size=20,
                               n_epochs=1,
                               A=64,
                               B=64,
                               C=3,
                               input_shape=(64, 64, 3))
예제 #3
0
파일: pixelcnn.py 프로젝트: pradeeps/pycadl
def generate():
    """Summary
    """
    # Parameters for generation
    ckpt_path = 'pixelcnn'
    B = None
    H = 64
    W = 64
    C = 3

    with tf.Graph().as_default(), tf.Session() as sess:
        # Not actually conditioning on anything here just using the gated cnn model
        net = build_conditional_pixel_cnn_model(B=B, H=H, W=W, C=C)

        # Load a list of files for tiny imagenet, downloading if necessary
        imagenet_files = dsu.tiny_imagenet_load()

        saver = tf.train.Saver()
        init_op = tf.group(tf.global_variables_initializer(),
                           tf.local_variables_initializer())
        sess.run(init_op)
        saver.restore(sess, tf.train.latest_checkpoint(ckpt_path))

        import matplotlib.pyplot as plt
        img = plt.imread(imagenet_files[0][1000])
        from scipy.misc import imresize
        og_img = imresize(img, (H, W))
        img = og_img.copy()
        # Zero out bottom half of image and let's try to synthesize it
        img[H // 2:, :, :] = 0

        for h_i in range(H // 2, H):
            for w_i in range(W):
                for c_i in range(C):
                    print(h_i, w_i, c_i, end='\r')
                    X = img.copy()
                    preds = sess.run(
                        net['sampled_preds'],
                        feed_dict={net['X']: X[np.newaxis]})
                    X = preds.reshape((1, H, W, C)).astype(np.uint8)
                    img[h_i, w_i, c_i] = X[0, h_i, w_i, c_i]

        fig, axs = plt.subplots(1, 2)
        axs[0].imshow(og_img)
        axs[1].imshow(img)
예제 #4
0
def generate():
    """Summary
    """
    # Parameters for generation
    ckpt_path = 'pixelcnn'
    B = None
    H = 64
    W = 64
    C = 3

    with tf.Graph().as_default(), tf.Session() as sess:
        # Not actually conditioning on anything here just using the gated cnn model
        net = build_conditional_pixel_cnn_model(B=B, H=H, W=W, C=C)

        # Load a list of files for tiny imagenet, downloading if necessary
        imagenet_files = dsu.tiny_imagenet_load()

        saver = tf.train.Saver()
        init_op = tf.group(tf.global_variables_initializer(),
                           tf.local_variables_initializer())
        sess.run(init_op)
        saver.restore(sess, tf.train.latest_checkpoint(ckpt_path))

        import matplotlib.pyplot as plt
        img = plt.imread(imagenet_files[0][1000])
        from scipy.misc import imresize
        og_img = imresize(img, (H, W))
        img = og_img.copy()
        # Zero out bottom half of image and let's try to synthesize it
        img[H // 2:, :, :] = 0

        for h_i in range(H // 2, H):
            for w_i in range(W):
                for c_i in range(C):
                    print(h_i, w_i, c_i, end='\r')
                    X = img.copy()
                    preds = sess.run(net['sampled_preds'],
                                     feed_dict={net['X']: X[np.newaxis]})
                    X = preds.reshape((1, H, W, C)).astype(np.uint8)
                    img[h_i, w_i, c_i] = X[0, h_i, w_i, c_i]

        fig, axs = plt.subplots(1, 2)
        axs[0].imshow(og_img)
        axs[1].imshow(img)
예제 #5
0
파일: datasets.py 프로젝트: pradeeps/pycadl
def TINYIMAGENET(path='./tiny_imagenet/'):
    """Attempt to load the files of the Tiny ImageNet dataset.

    http://cs231n.stanford.edu/tiny-imagenet-200.zip
    https://tiny-imagenet.herokuapp.com/

    Parameters
    ----------
    path : str, optional
        Directory where the dataset can be found or else will be placed.

    Returns
    -------
    files : list
        List of file paths to the dataset.
    labels : list
        List of labels for each file (only training files have labels)
    """
    files, labels = tiny_imagenet_load(path)
    return files, labels
예제 #6
0
파일: datasets.py 프로젝트: zhf459/pycadl
def TINYIMAGENET(path='./tiny_imagenet/'):
    """Attempt to load the files of the Tiny ImageNet dataset.

    http://cs231n.stanford.edu/tiny-imagenet-200.zip
    https://tiny-imagenet.herokuapp.com/

    Parameters
    ----------
    path : str, optional
        Directory where the dataset can be found or else will be placed.

    Returns
    -------
    files : list
        List of file paths to the dataset.
    labels : list
        List of labels for each file (only training files have labels)
    """
    files, labels = tiny_imagenet_load(path)
    return files, labels
예제 #7
0
파일: pixelcnn.py 프로젝트: pradeeps/pycadl
def train_tiny_imagenet(ckpt_path='pixelcnn',
                        n_epochs=1000,
                        save_step=100,
                        write_step=25,
                        B=32,
                        H=64,
                        W=64,
                        C=3):
    """Summary

    Parameters
    ----------
    ckpt_path : str, optional
        Description
    n_epochs : int, optional
        Description
    save_step : int, optional
        Description
    write_step : int, optional
        Description
    B : int, optional
        Description
    H : int, optional
        Description
    W : int, optional
        Description
    C : int, optional
        Description
    """
    ckpt_name = os.path.join(ckpt_path, 'pixelcnn.ckpt')

    with tf.Graph().as_default(), tf.Session() as sess:
        # Not actually conditioning on anything here just using the gated cnn model
        net = build_conditional_pixel_cnn_model(B=B, H=H, W=W, C=C)

        # build the optimizer (this will take a while!)
        optimizer = tf.train.AdamOptimizer(
            learning_rate=0.001).minimize(net['cost'])

        # Load a list of files for tiny imagenet, downloading if necessary
        imagenet_files = dsu.tiny_imagenet_load()

        # Create a threaded image pipeline which will load/shuffle/crop/resize
        batch = dsu.create_input_pipeline(
            imagenet_files[0],
            batch_size=B,
            n_epochs=n_epochs,
            shape=[64, 64, 3],
            crop_shape=[H, W, C],
            crop_factor=1.0,
            n_threads=8)

        saver = tf.train.Saver()
        writer = tf.summary.FileWriter(ckpt_path)
        init_op = tf.group(tf.global_variables_initializer(),
                           tf.local_variables_initializer())
        sess.run(init_op)

        # This will handle our threaded image pipeline
        coord = tf.train.Coordinator()

        # Ensure no more changes to graph
        tf.get_default_graph().finalize()

        # Start up the queues for handling the image pipeline
        threads = tf.train.start_queue_runners(sess=sess, coord=coord)

        if os.path.exists(ckpt_name + '.index') or os.path.exists(ckpt_name):
            saver.restore(sess, ckpt_name)
            saver.restore(sess, tf.train.latest_checkpoint(ckpt_path))

        epoch_i = 0
        batch_i = 0
        try:
            while not coord.should_stop() and epoch_i < n_epochs:
                batch_i += 1
                batch_xs = sess.run(batch)
                train_cost = sess.run(
                    [net['cost'], optimizer], feed_dict={net['X']: batch_xs})[0]

                print(batch_i, train_cost)
                if batch_i % write_step == 0:
                    summary = sess.run(
                        net['summaries'], feed_dict={net['X']: batch_xs})
                    writer.add_summary(summary, batch_i)

                if batch_i % save_step == 0:
                    # Save the variables to disk.  Don't write the meta graph
                    # since we can use the code to create it, and it takes a long
                    # time to create the graph since it is so deep
                    saver.save(
                        sess,
                        ckpt_name,
                        global_step=batch_i,
                        write_meta_graph=True)
        except tf.errors.OutOfRangeError:
            print('Done.')
        finally:
            # One of the threads has issued an exception.  So let's tell all the
            # threads to shutdown.
            coord.request_stop()

        # Wait until all threads have finished.
        coord.join(threads)
예제 #8
0
 def test_tiny_imagenet_loads(self):
     Xs, Ys = dsu.tiny_imagenet_load()
     assert (len(Xs) == 120000)
     assert (Xs[0].endswith('n02226429_40.JPEG'))
     assert (Ys[0] == 'grasshopper, hopper')
예제 #9
0
def train_tiny_imagenet():
    """Summary
    """
    net = build_pixel_rnn_basic_model()

    # build the optimizer (this will take a while!)
    optimizer = tf.train.AdamOptimizer(
        learning_rate=0.001).minimize(net['cost'])

    # Load a list of files for tiny imagenet, downloading if necessary
    imagenet_files = dsu.tiny_imagenet_load()

    # Create a threaded image pipeline which will load/shuffle/crop/resize
    batch = dsu.create_input_pipeline(
        imagenet_files,
        batch_size=B,
        n_epochs=n_epochs,
        shape=[64, 64, 3],
        crop_shape=[32, 32, 3],
        crop_factor=0.5,
        n_threads=8)

    sess = tf.Session()
    saver = tf.train.Saver()
    init_op = tf.group(tf.global_variables_initializer(),
                       tf.local_variables_initializer())
    sess.run(init_op)

    # This will handle our threaded image pipeline
    coord = tf.train.Coordinator()

    # Ensure no more changes to graph
    tf.get_default_graph().finalize()

    # Start up the queues for handling the image pipeline
    threads = tf.train.start_queue_runners(sess=sess, coord=coord)

    if os.path.exists(ckpt_name + '.index') or os.path.exists(ckpt_name):
        saver.restore(sess, ckpt_name)
        saver.restore(sess, tf.train.latest_checkpoint('./'))

    epoch_i = 0
    batch_i = 0
    save_step = 100
    try:
        while not coord.should_stop() and epoch_i < n_epochs:
            batch_i += 1
            batch_xs = sess.run(batch)
            train_cost = sess.run(
                [net['cost'], optimizer], feed_dict={net['X']: batch_xs})[0]
            print(batch_i, train_cost)
            if batch_i % save_step == 0:
                # Save the variables to disk.  Don't write the meta graph
                # since we can use the code to create it, and it takes a long
                # time to create the graph since it is so deep
                saver.save(
                    sess,
                    ckpt_name,
                    global_step=batch_i,
                    write_meta_graph=False)
    except tf.errors.OutOfRangeError:
        print('Done.')
    finally:
        # One of the threads has issued an exception.  So let's tell all the
        # threads to shutdown.
        coord.request_stop()

    # Wait until all threads have finished.
    coord.join(threads)

    # Clean up the session.
    sess.close()
예제 #10
0
 def test_draw_train_input_pipeline(self):
     Xs, ys = dsu.tiny_imagenet_load()
     draw.train_input_pipeline(
             Xs[:100], batch_size=20, n_epochs=1, A=64, B=64, C=3,
             input_shape=(64, 64, 3))
예제 #11
0
def train_tiny_imagenet(ckpt_path='pixelcnn',
                        n_epochs=1000,
                        save_step=100,
                        write_step=25,
                        B=32,
                        H=64,
                        W=64,
                        C=3):
    """Summary

    Parameters
    ----------
    ckpt_path : str, optional
        Description
    n_epochs : int, optional
        Description
    save_step : int, optional
        Description
    write_step : int, optional
        Description
    B : int, optional
        Description
    H : int, optional
        Description
    W : int, optional
        Description
    C : int, optional
        Description
    """
    ckpt_name = os.path.join(ckpt_path, 'pixelcnn.ckpt')

    with tf.Graph().as_default(), tf.Session() as sess:
        # Not actually conditioning on anything here just using the gated cnn model
        net = build_conditional_pixel_cnn_model(B=B, H=H, W=W, C=C)

        # build the optimizer (this will take a while!)
        optimizer = tf.train.AdamOptimizer(learning_rate=0.001).minimize(
            net['cost'])

        # Load a list of files for tiny imagenet, downloading if necessary
        imagenet_files = dsu.tiny_imagenet_load()

        # Create a threaded image pipeline which will load/shuffle/crop/resize
        batch = dsu.create_input_pipeline(imagenet_files[0],
                                          batch_size=B,
                                          n_epochs=n_epochs,
                                          shape=[64, 64, 3],
                                          crop_shape=[H, W, C],
                                          crop_factor=1.0,
                                          n_threads=8)

        saver = tf.train.Saver()
        writer = tf.summary.FileWriter(ckpt_path)
        init_op = tf.group(tf.global_variables_initializer(),
                           tf.local_variables_initializer())
        sess.run(init_op)

        # This will handle our threaded image pipeline
        coord = tf.train.Coordinator()

        # Ensure no more changes to graph
        tf.get_default_graph().finalize()

        # Start up the queues for handling the image pipeline
        threads = tf.train.start_queue_runners(sess=sess, coord=coord)

        if os.path.exists(ckpt_name + '.index') or os.path.exists(ckpt_name):
            saver.restore(sess, ckpt_name)
            saver.restore(sess, tf.train.latest_checkpoint(ckpt_path))

        epoch_i = 0
        batch_i = 0
        try:
            while not coord.should_stop() and epoch_i < n_epochs:
                batch_i += 1
                batch_xs = sess.run(batch)
                train_cost = sess.run([net['cost'], optimizer],
                                      feed_dict={net['X']: batch_xs})[0]

                print(batch_i, train_cost)
                if batch_i % write_step == 0:
                    summary = sess.run(net['summaries'],
                                       feed_dict={net['X']: batch_xs})
                    writer.add_summary(summary, batch_i)

                if batch_i % save_step == 0:
                    # Save the variables to disk.  Don't write the meta graph
                    # since we can use the code to create it, and it takes a long
                    # time to create the graph since it is so deep
                    saver.save(sess,
                               ckpt_name,
                               global_step=batch_i,
                               write_meta_graph=True)
        except tf.errors.OutOfRangeError:
            print('Done.')
        finally:
            # One of the threads has issued an exception.  So let's tell all the
            # threads to shutdown.
            coord.request_stop()

        # Wait until all threads have finished.
        coord.join(threads)