def export_sparse_encoding(self, dataset_path):
        """
        Converts ground truth images to sparse labels and saves them to disk in PNG format.
        Ground truth images are only available for the training set.

        :param dataset_path: The root path of the dataset.

        :return: None
        """
        # Load the list of image base names
        basenames = self.get_basenames(is_training=True, dataset_path=dataset_path)

        gt_path = os.path.join(dataset_path, TRAIN_GT_PATH)
        gt_sparse_path = os.path.join(dataset_path, GT_SPARSE_PATH)

        # Create sparse labels folder
        if not os.path.exists(gt_sparse_path):
            print('Creating sparse labels folder')
            os.makedirs(gt_sparse_path)
        else:
            print('Sparse labels folder already exists')

        for _, basename in basenames:
            gt = imread(os.path.join(gt_path, basename))
            gt = colors2labels(gt, self.cmap, one_hot=False)
            gt = np.dstack([gt, np.copy(gt), np.copy(gt)])
            imwrite(os.path.join(gt_sparse_path, basename), gt)
    def export_sparse_encoding(self, filename, dataset_path):
        """
        Converts ground truth images to sparse labels and saves them to disk in PNG format.

        :param filename:
        :param dataset_path:
        :return: None
        """
        # Load the list of image base names
        basenames = self.get_basenames(filename, dataset_path)

        gt_path = os.path.join(dataset_path, 'SegmentationClass')
        gt_sparse_path = os.path.join(dataset_path, 'SegmentationSparseClass')

        # Create sparse labels folder
        if not os.path.exists(gt_sparse_path):
            print('Creating sparse labels folder')
            os.makedirs(gt_sparse_path)
        else:
            print('Sparse labels folder already exists')

        for basename in basenames:
            gt = imread(os.path.join(gt_path, basename + '.png'))
            gt = colors2labels(gt, self.cmap, one_hot=False)
            gt = np.dstack([gt, np.copy(gt), np.copy(gt)])
            imwrite(os.path.join(gt_sparse_path, basename + '.png'), gt)
def generate_glued_pairs(id_type_list, n_generated_files, seed=None):
    if seed is not None:
        np.random.seed(seed)

    counter = n_generated_files
    while counter > 0:
        ind1 = np.random.randint(len(id_type_list))
        ind2 = np.random.randint(len(id_type_list))
        if ind1 == ind2:
            ind2 = (ind2 + np.random.randint(
                1, len(id_type_list))) % len(id_type_list)

        assert id_type_list[ind1][1] == id_type_list[ind2][
            1], "Image type should be the same: %s != %s" % (
                id_type_list[ind1][1], id_type_list[ind2][1])
        img1 = get_image_data(*id_type_list[ind1])
        img2 = get_image_data(*id_type_list[ind2])
        img = create_glued_image(img1, img2)

        image_id = str(id_type_list[ind1][0]) + "_" + str(
            id_type_list[ind2][0])
        image_type = 'Generated_' + id_type_list[ind1][1]

        filepath = get_filename(image_id, image_type)
        if os.path.exists(filepath):
            # Do not overwrite existing
            continue
        imwrite(img, image_id, image_type)

        counter -= 1
    def predict_dataset(self, save_path, dataset_filepath, model, batch_size):
        """
        Predicts semantic labels for all images of the speficied dataset and saves results to disk.

        :param save_path: The target directory. A sub-directory will be created from the current date and time.
        :param dataset_filepath: The filename of the TFRecordDataset to use for prediction.
        :param model: An instance of FCN Model.
        :param batch_size: The number of images per batch.
        :return: None
        """
        if not os.path.exists(dataset_filepath):
            raise ValueError('File not found: {}'.format(dataset_filepath))

        sess = tf.compat.v1.get_default_session()

        # Make the folder to save the predictions
        output_path = os.path.join(save_path, datetime.now().isoformat().split('.')[0]).split(':')
        output_path = output_path[0] + ':' + output_path[1] + 'H' + output_path[2]
        if os.path.exists(output_path):
            shutil.rmtree(output_path)
        print('Saving predictions to ' + output_path)
        os.makedirs(output_path)

        # Load the dataset and make an iterator
        dataset = tf.data.TFRecordDataset(dataset_filepath)
        dataset = dataset.map(self.parse_record)
        dataset = dataset.batch(batch_size)
        iterator = tf.compat.v1.data.make_one_shot_iterator(dataset)
        next_sample = iterator.get_next()

        idx = 0  # The image name is it's index in the TFRecordDataset
        while True:
            try:
                im_batch, _, shape_batch = sess.run(next_sample)
                # Make an array from a tuple of 3 lists each with `batch_size` elements
                shape_batch = np.swapaxes(np.asarray(shape_batch), 0, 1)
                # Returns a 1-item list containing a numpy vector of length BATCH_SIZE * N_PIXELS * N_CLASSES
                im_softmax = sess.run([tf.nn.softmax(model.logits)], {model.keep_prob: 1.0,
                                                                      model.inputs: im_batch})[0]
                im_softmax = im_softmax.reshape((len(im_batch), np.prod(model.image_shape), self.n_classes+1))

                for i in range(len(im_batch)):
                    # Predict pixel class and expand with a channel dimension.
                    im_pred = np.argmax(im_softmax[i], axis=1).reshape(model.image_shape)
                    im_pred = labels2colors(im_pred, self.cmap)
                    im_masked = center_crop(apply_mask(im_batch[i], im_pred), shape_batch[i][:2])
                    imwrite(os.path.join(output_path, str(idx) + '.jpg'), im_masked)
                    idx += 1
            except tf.errors.OutOfRangeError:
                break
def read_filter_write(filter_type, filename_in, guidance_in, sigma_color,
                      sigma_spatial, path_out):
    """Read input and guidance image, apply filter and write result."""
    # get basename for output later
    basename = os.path.splitext(os.path.basename(filename_in))[0]
    # Read the images
    image = iu.imread(filename_in)
    joint = iu.imread(guidance_in)

    filtered = apply_filter(filter_type, image, joint, sigma_color,
                            sigma_spatial)

    # save the result
    params = "_{}_c{}s{}".format(filter_type, sigma_color, sigma_spatial)
    filename = os.path.join(path_out, basename + params + '.png')
    iu.imwrite(filename, filtered)

    return filtered
def decompose_image(filename_in, path_out):
    """Run the intrinsic image decomposition with caffe."""
    network_file = os.path.join(os.path.dirname(__file__),
                                'network_definition.prototxt')
    caffemodel = os.path.join(os.path.dirname(__file__),
                              'learned_weights.caffemodel')
    net = caffe.Net(network_file,
                    caffe.TEST,
                    weights=caffemodel)

    # print("Read file:", filename_in)
    image = iu.imread(filename_in)

    # get basename for output later
    basename = os.path.splitext(os.path.basename(filename_in))[0]

    # get result from caffe
    reflectance_gray = get_reflectance_caffe(net, image)

    # save result
    filename = os.path.join(path_out, basename + '-r.png')
    iu.imwrite(filename, reflectance_gray)

    # now colorize with input image again
    reflectance, shading = iu.colorize(reflectance_gray, image)

    # save color versions in sRGB
    filename = os.path.join(path_out, basename + '-r_colorized.png')
    iu.imwrite(filename, reflectance, sRGB=True)
    filename = os.path.join(path_out, basename + '-s_colorized.png')
    iu.imwrite(filename, shading, sRGB=True)

    return reflectance_gray
示例#7
0
        print('Copy variables from % s' % ckpt_path)

    # test
    a_list = glob('./datasets/' + dataset + '/testA/*.jpg')
    b_list = glob('./datasets/' + dataset + '/testB/*.jpg')

    a_save_dir = './test_predictions/' + dataset + '/testA/'
    b_save_dir = './test_predictions/' + dataset + '/testB/'
    utils.mkdir([a_save_dir, b_save_dir])
    for i in range(len(a_list)):
        a_real_ipt = im.imresize(im.imread(a_list[i]), [crop_size, crop_size])
        a_real_ipt.shape = 1, crop_size, crop_size, 3
        a2b_opt, a2b2a_opt = sess.run([a2b, a2b2a],
                                      feed_dict={a_real: a_real_ipt})
        a_img_opt = np.concatenate((a_real_ipt, a2b_opt, a2b2a_opt), axis=0)

        img_name = os.path.basename(a_list[i])
        im.imwrite(im.immerge(a_img_opt, 1, 3), a_save_dir + img_name)
        print('Save %s' % (a_save_dir + img_name))

    for i in range(len(b_list)):
        b_real_ipt = im.imresize(im.imread(b_list[i]), [crop_size, crop_size])
        b_real_ipt.shape = 1, crop_size, crop_size, 3
        b2a_opt, b2a2b_opt = sess.run([b2a, b2a2b],
                                      feed_dict={b_real: b_real_ipt})
        b_img_opt = np.concatenate((b_real_ipt, b2a_opt, b2a2b_opt), axis=0)

        img_name = os.path.basename(b_list[i])
        im.imwrite(im.immerge(b_img_opt, 1, 3), b_save_dir + img_name)
        print('Save %s' % (b_save_dir + img_name))
def train():
    epoch = 200
    batch_size = 1
    lr = 0.0002
    crop_size = 128
    load_size = 128
    tar_db_a = "cameron_images.tgz"
    tar_db_b = "teresa_images.tgz"
    db_a_i = importer_tar.Importer(tar_db_a)
    db_b_i = importer_tar.Importer(tar_db_b)
    image_a_names = db_a_i.get_sorted_image_name()
    image_b_names = db_b_i.get_sorted_image_name()
    train_a_size = int(len(image_a_names) * 0.8)
    train_b_size = int(len(image_b_names) * 0.8)

    image_a_train_names = image_a_names[0:train_a_size]
    image_b_train_names = image_b_names[0:train_b_size]

    image_a_test_names = image_a_names[train_a_size:]
    image_b_test_names = image_b_names[train_b_size:]

    print("A train size:{},test size:{}".format(len(image_a_train_names),
                                                len(image_a_test_names)))
    print("B train size:{},test size:{}".format(len(image_b_train_names),
                                                len(image_b_test_names)))
    """ graph """
    # models
    generator_a2b = partial(models.generator, scope='a2b')
    generator_b2a = partial(models.generator, scope='b2a')
    discriminator_a = partial(models.discriminator, scope='a')
    discriminator_b = partial(models.discriminator, scope='b')

    # operations
    a_real_in = tf.placeholder(tf.float32,
                               shape=[None, load_size, load_size, 3],
                               name="a_real")
    b_real_in = tf.placeholder(tf.float32,
                               shape=[None, load_size, load_size, 3],
                               name="b_real")
    a_real = utils.preprocess_image(a_real_in, crop_size=crop_size)
    b_real = utils.preprocess_image(b_real_in, crop_size=crop_size)

    a2b = generator_a2b(a_real)
    b2a = generator_b2a(b_real)
    b2a2b = generator_a2b(b2a)
    a2b2a = generator_b2a(a2b)

    a_logit = discriminator_a(a_real)
    b2a_logit = discriminator_a(b2a)
    b_logit = discriminator_b(b_real)
    a2b_logit = discriminator_b(a2b)

    # losses
    g_loss_a2b = -tf.reduce_mean(a2b_logit)
    g_loss_b2a = -tf.reduce_mean(b2a_logit)

    cyc_loss_a = tf.losses.absolute_difference(a_real, a2b2a)
    cyc_loss_b = tf.losses.absolute_difference(b_real, b2a2b)
    g_loss = g_loss_a2b + g_loss_b2a + cyc_loss_a * 10.0 + cyc_loss_b * 10.0

    wd_a = tf.reduce_mean(a_logit) - tf.reduce_mean(b2a_logit)
    wd_b = tf.reduce_mean(b_logit) - tf.reduce_mean(a2b_logit)

    gp_a = gradient_penalty(a_real, b2a, discriminator_a)
    gp_b = gradient_penalty(b_real, a2b, discriminator_b)

    d_loss_a = -wd_a + 10.0 * gp_a
    d_loss_b = -wd_b + 10.0 * gp_b

    # summaries
    utils.summary({
        g_loss_a2b: 'g_loss_a2b',
        g_loss_b2a: 'g_loss_b2a',
        cyc_loss_a: 'cyc_loss_a',
        cyc_loss_b: 'cyc_loss_b'
    })
    utils.summary({d_loss_a: 'd_loss_a'})
    utils.summary({d_loss_b: 'd_loss_b'})
    for variable in slim.get_model_variables():
        tf.summary.histogram(variable.op.name, variable)
    merged = tf.summary.merge_all()

    im1_op = tf.summary.image("real_a", a_real_in)
    im2_op = tf.summary.image("a2b", a2b)
    im3_op = tf.summary.image("b2a2b", b2a2b)
    im4_op = tf.summary.image("real_b", b_real_in)
    im5_op = tf.summary.image("b2a", b2a)
    im6_op = tf.summary.image("b2a2b", b2a2b)

    # optim
    t_var = tf.trainable_variables()
    d_a_var = [var for var in t_var if 'a_discriminator' in var.name]
    d_b_var = [var for var in t_var if 'b_discriminator' in var.name]
    g_var = [
        var for var in t_var
        if 'a2b_generator' in var.name or 'b2a_generator' in var.name
    ]

    d_a_train_op = tf.train.AdamOptimizer(lr,
                                          beta1=0.5).minimize(d_loss_a,
                                                              var_list=d_a_var)
    d_b_train_op = tf.train.AdamOptimizer(lr,
                                          beta1=0.5).minimize(d_loss_b,
                                                              var_list=d_b_var)
    g_train_op = tf.train.AdamOptimizer(lr, beta1=0.5).minimize(g_loss,
                                                                var_list=g_var)
    """ train """
    ''' init '''
    # session
    config = tf.ConfigProto(allow_soft_placement=True)
    config.gpu_options.allow_growth = True
    sess = tf.Session(config=config)

    # counter
    it_cnt, update_cnt = utils.counter()
    ''' summary '''
    summary_writer = tf.summary.FileWriter('./outputs/summaries/', sess.graph)
    ''' saver '''
    saver = tf.train.Saver(max_to_keep=5)
    ''' restore '''
    ckpt_dir = './outputs/checkpoints/'
    utils.mkdir(ckpt_dir)
    try:
        utils.load_checkpoint(ckpt_dir, sess)
    except:
        sess.run(tf.global_variables_initializer())
    '''train'''
    try:
        batch_epoch = min(train_a_size, train_b_size) // batch_size
        max_it = epoch * batch_epoch
        for it in range(sess.run(it_cnt), max_it):
            sess.run(update_cnt)
            epoch = it // batch_epoch
            it_epoch = it % batch_epoch + 1

            # read data
            a_real_np = cv2.resize(
                db_a_i.get_image(image_a_train_names[it_epoch % batch_epoch]),
                (load_size, load_size))
            b_real_np = cv2.resize(
                db_b_i.get_image(image_b_train_names[it_epoch % batch_epoch]),
                (load_size, load_size))

            # train G
            sess.run(g_train_op,
                     feed_dict={
                         a_real_in: [a_real_np],
                         b_real_in: [b_real_np]
                     })

            # train discriminator
            sess.run([d_a_train_op, d_b_train_op],
                     feed_dict={
                         a_real_in: [a_real_np],
                         b_real_in: [b_real_np]
                     })

            # display
            if it % 100 == 0:
                # make summary
                summary = sess.run(merged,
                                   feed_dict={
                                       a_real_in: [a_real_np],
                                       b_real_in: [b_real_np]
                                   })
                summary_writer.add_summary(summary, it)
                print("Epoch: (%3d) (%5d/%5d)" %
                      (epoch, it_epoch, batch_epoch))

            # save
            if (it + 1) % 1000 == 0:
                save_path = saver.save(
                    sess, '{}/epoch_{}_{}.ckpt'.format(ckpt_dir, epoch,
                                                       it_epoch))
                print('###Model saved in file: {}'.format(save_path))

            # sample
            if (it + 1) % 1000 == 0:
                a_test_index = int(
                    np.random.uniform(high=len(image_a_test_names)))
                b_test_index = int(
                    np.random.uniform(high=len(image_b_test_names)))
                a_real_np = cv2.resize(
                    db_a_i.get_image(image_a_test_names[a_test_index]),
                    (load_size, load_size))
                b_real_np = cv2.resize(
                    db_b_i.get_image(image_b_test_names[b_test_index]),
                    (load_size, load_size))

                [a_opt, a2b_opt, a2b2a_opt, b_opt, b2a_opt, b2a2b_opt
                 ] = sess.run([a_real, a2b, a2b2a, b_real, b2a, b2a2b],
                              feed_dict={
                                  a_real_in: [a_real_np],
                                  b_real_in: [b_real_np]
                              })

                sample_opt = np.concatenate(
                    (a_opt, a2b_opt, a2b2a_opt, b_opt, b2a_opt, b2a2b_opt),
                    axis=0)
                [im1_sum,im2_sum,im3_sum,im4_sum,im5_sum,im6_sum] = \
                    sess.run([im1_op,im2_op,im3_op,im4_op,im5_op,im6_op],
                             feed_dict={a_real_in: [a_real_np], b_real_in: [b_real_np]})

                summary_writer.add_summary(im1_sum, it)
                summary_writer.add_summary(im2_sum, it)
                summary_writer.add_summary(im3_sum, it)
                summary_writer.add_summary(im4_sum, it)
                summary_writer.add_summary(im5_sum, it)
                summary_writer.add_summary(im6_sum, it)

                save_dir = './outputs/sample_images_while_training/'
                utils.mkdir(save_dir)
                im.imwrite(
                    im.immerge(sample_opt, 2, 3),
                    '{}/epoch_{}_it_{}.jpg'.format(save_dir, epoch, it_epoch))
    except:
        raise

    finally:
        save_path = saver.save(
            sess, '{}/epoch_{}_{}.ckpt'.format(ckpt_dir, epoch, it_epoch))
        print('###Model saved in file: {}'.format(save_path))
        sess.close()
示例#9
0
        if (it + 1) % 1000 == 0:
            save_path = saver.save(
                sess,
                f'{ckpt_dir}/Epoch_({epoch})_({it_epoch}of{batch_epoch}).ckpt')
            print(f'Model saved in file: {save_path}')

        if (it + 1) % 100 == 0:
            a_real_ipt = a_test_pool.batch()
            b_real_ipt = b_test_pool.batch()
            [a2b_opt, a2b2a_opt, b2a_opt,
             b2a2b_opt] = sess.run([a2b, a2b2a, b2a, b2a2b],
                                   feed_dict={
                                       a_real: a_real_ipt,
                                       b_real: b_real_ipt
                                   })
            sample_opt = np.concatenate((a_real_ipt, a2b_opt, a2b2a_opt,
                                         b_real_ipt, b2a_opt, b2a2b_opt),
                                        axis=0)

            save_dir = './outputs/sample_images_while_training/' + dataset
            utils.mkdir(save_dir)
            im.imwrite(
                im.immerge(sample_opt, 2, 3),
                f'{save_dir}/Epoch_({epoch})_({it_epoch}of{batch_epoch}).jpg')
except:
    save_path = saver.save(
        sess, f'{ckpt_dir}/Epoch_({epoch})_({it_epoch}of{batch_epoch}).ckpt')
    print(f'Model saved in file: {save_path}')
    sess.close()
示例#10
0
ab_pair_test_pool = data.ImageDataPair(sess, a_test_img_paths, batch_size=3, load_size=load_size, shuffle=False, crop_size=crop_size)

ab_pair_ipt = ab_pair_test_pool.batch_match()

a_ipt = ab_pair_ipt[:,:,:,:3]
b_ipt = ab_pair_ipt[:,:,:,3:]

predict = sess.run(predict_op, feed_dict={a_real: a_ipt})
print('predict: ', predict.shape)

sample_opt = a_ipt
sample_opt[:,:,:,0] = 0.7*sample_opt[:,:,:,0] + 0.3* predict[:,:,:,0]


im.imwrite(im.immerge(sample_opt, len(sample_opt), 1), './result.jpg')

# test_img_paths = glob(input_path + '*')



# for path in test_img_paths:
#     img = tf.read_file(path)
#     img = tf.image.decode_jpeg(img, channels=3)
#     img = tf.image.resize_images(img, load_size)
#     img = (img - tf.reduce_min(img)) / (tf.reduce_max(img) - tf.reduce_min(img))
#     img = tf.expand_dims(img, axis=0)
#     img = sess.run(img)

#     predict_op = tf.image.resize_images(predict_op, [376, 1242])
#     predict = sess.run(predict_op, feed_dict={a_real: img})
示例#11
0
            # Sample images for external evaluation (i.e. just raw single images). Note: For triplet=true, there are 2x steps involved.
            if args.samplingcycle > 0 and (it % args.samplingcycle == 0) and it > 0:
                print("Create samples for the external evaluator (aux batch {} with size {})".format(int(it/args.samplingcycle), args.online_sampling_batch_size))
                for c_i in range(args.online_sampling_batch_size):
                    fname = 'Transformed_from_%s_(%dof%d)_once.png' % (args.stage2, c_i, args.singletestN)
                    save_single_img(a_real_ipt = b_test_pool.batch(), b_real_ipt = c_test_pool.batch(), save_dir = './aux_samples/' + args.dataset + subnet_ext_maybe+'/'+args.stage2+'/'+str(int(it)), fname=fname)

            # Create sample images with a-b-a structure
            if (it + 1) % 100 == 0:
                a_real_ipt = b_test_pool.batch()
                b_real_ipt = c_test_pool.batch()
                [a2b_opt, a2b2a_opt, b2a_opt, b2a2b_opt] = sess.run([a2b, a2b2a, b2a, b2a2b], feed_dict={a_real: a_real_ipt, b_real: b_real_ipt})
                sample_opt = np.concatenate((a_real_ipt, a2b_opt, a2b2a_opt, b_real_ipt, b2a_opt, b2a2b_opt), axis=0)               

                im.imwrite(im.immerge(sample_opt, 2, 3), '%s/Epoch_(%d)_(%dof%d).png' % (online_samples_dir, epoch, it_epoch, batch_epoch))

                if args.double_cycle:
                    [a2b_opt, a2b2a_opt, b2a_opt, b2a2b_opt] = sess.run([a2b, a2b2a, b2a, b2a2b], feed_dict={a_real: a2b_opt, b_real: b2a_opt})
                    sample_opt = np.concatenate((a_real_ipt, a2b_opt, a2b2a_opt, b_real_ipt, b2a_opt, b2a2b_opt), axis=0)
                    im.imwrite(im.immerge(sample_opt, 2, 3), '%s/Epoch_(%d)_(%dof%d)_double_cycle.png' % (online_samples_dir, epoch, it_epoch, batch_epoch))

        if do_save and last_it != -1:
            save_path = saver.save(sess, '%s/Epoch_(%d)_(%dof%d)_step_(%d).ckpt' % (ckpt_dir, epoch, it_epoch, batch_epoch,last_it))
            print('Final model saved in file: % s' % save_path)

    elif args.chaintestdir:
        chaintests_N = 20
        print("Run chain test on dir {} for {} times".format(args.chaintestdir, chaintests_N))
        for c_i in range(chaintests_N):
            a_real_ipt = b_test_pool.batch()
示例#12
0
            # Inference
            for i in range(len(a_list)):
                # Define shapes for images fed to the graph
                a_feed = im.imresize(im.imread(a_list[i]),
                                     [crop_size, crop_size])
                a_feed.shape = 1, crop_size, crop_size, 3

                # Feed in images to the graph
                a2b_result = sess.run(a_output, feed_dict={a_input: a_feed})
                print(type(a2b_result))
                print(a2b_result.shape)

                # pickle.dump(a2b_result, open("C:/Users/ag17634/Desktop/save.p", "wb"))

                # Create and save the output image
                a_img_opt = a2b_result
                img_name = os.path.basename(a_list[i])

                output = im.immerge(a_img_opt, 1, 1)

                im.imwrite(output, a_save_dir + '/' + img_name)

                print('Save %s' % (a_save_dir + '/' + img_name))

                if i == 100:
                    end = time.time()
            end2 = time.time()

            # print("Time to process first 100 images:", end - start)
            print("Time to process all %d images: %f" % (i + 1, end2 - start))
        raise Exception('No checkpoint!')
    else:
        print('Copy variables from % s' % ckpt_path)

    # test
    a_list = glob('./datasets/' + dataset + '/testA/*.jpg')
    b_list = glob('./datasets/' + dataset + '/testB/*.jpg')

    a_save_dir = './test_predictions/' + dataset + '/testA/'
    b_save_dir = './test_predictions/' + dataset + '/testB/'
    utils.mkdir([a_save_dir, b_save_dir])
    for i in range(len(a_list)):
        a_real_ipt = im.imresize(im.imread(a_list[i]), [crop_size, crop_size])
        a_real_ipt.shape = 1, crop_size, crop_size, 3
        a2b_opt, a2b2a_opt = sess.run([a2b, a2b2a], feed_dict={a_real: a_real_ipt})
        a_img_opt = np.concatenate((a_real_ipt, a2b_opt, a2b2a_opt), axis=0)

        img_name = os.path.basename(a_list[i])
        im.imwrite(im.immerge(a_img_opt, 1, 3), a_save_dir + img_name)
        print('Save %s' % (a_save_dir + img_name))

    for i in range(len(b_list)):
        b_real_ipt = im.imresize(im.imread(b_list[i]), [crop_size, crop_size])
        b_real_ipt.shape = 1, crop_size, crop_size, 3
        b2a_opt, b2a2b_opt = sess.run([b2a, b2a2b], feed_dict={b_real: b_real_ipt})
        b_img_opt = np.concatenate((b_real_ipt, b2a_opt, b2a2b_opt), axis=0)

        img_name = os.path.basename(b_list[i])
        im.imwrite(im.immerge(b_img_opt, 1, 3), b_save_dir + img_name)
        print('Save %s' % (b_save_dir + img_name))
        epoch = it // batch_epoch
        it_epoch = it % batch_epoch + 1

        # display
        if it % 1 == 0:
            print("Epoch: (%3d) (%5d/%5d)" % (epoch, it_epoch, batch_epoch))

        # save
        if (it + 1) % 1000 == 0:
            save_path = saver.save(sess, '%s/Epoch_(%d)_(%dof%d).ckpt' % (ckpt_dir, epoch, it_epoch, batch_epoch))
            print('Model saved in file: % s' % save_path)

        # sample
        if (it + 1) % 100 == 0:
            a_real_ipt = a_test_pool.batch()
            b_real_ipt = b_test_pool.batch()
            [a2b_opt, a2b2a_opt, b2a_opt, b2a2b_opt] = sess.run([a2b, a2b2a, b2a, b2a2b], feed_dict={a_real: a_real_ipt, b_real: b_real_ipt})
            sample_opt = np.concatenate((a_real_ipt, a2b_opt, a2b2a_opt, b_real_ipt, b2a_opt, b2a2b_opt), axis=0)

            save_dir = './sample_images_while_training/' + dataset
            utils.mkdir(save_dir + '/')
            im.imwrite(im.immerge(sample_opt, 2, 3), '%s/Epoch_(%d)_(%dof%d).jpg' % (save_dir, epoch, it_epoch, batch_epoch))

except Exception, e:
    coord.request_stop(e)
finally:
    print("Stop threads and close session!")
    coord.request_stop()
    coord.join(threads)
    sess.close()
示例#15
0
        print(len(shape))
        variable_parameters = 1
        for dim in shape:
            print(dim)
            variable_parameters *= dim.value
        print(variable_parameters)
        total_parameters += variable_parameters
    print("\nTotal parameters:\n", total_parameters)

    # start = time.time()
    # Inference
    for i in range(len(a_list)):
        # Define shapes for images fed to the graph
        a_feed = im.imresize(im.imread(a_list[i]), [crop_size, crop_size])
        a_feed.shape = 1, crop_size, crop_size, 3

        # Feed in images to the graph
        a2b_result = sess.run(a2b, feed_dict={a_input: a_feed})

        # Create and save the output image
        a_img_opt = np.concatenate((a_feed, a2b_result), axis=0)
        img_name = os.path.basename(a_list[i])
        im.imwrite(im.immerge(a_img_opt, 1, 2), a_save_dir + '/' + img_name)
        print('Save %s' % (a_save_dir + '/' + img_name))

    #     if i == 100:
    #         end = time.time()
    # end2 = time.time()
    # print("Time to process first 100 images:", end - start)
    # print("Time to process all %d images: %f" % (i + 1, end2 - start))
            save_path = saver.save(
                sess, '%s/Epoch_(%d)_(%dof%d).ckpt' %
                (ckpt_dir, epoch, it_epoch, batch_epoch))
            print('Model saved in file: % s' % save_path)

        # sample
        if (it + 1) % 100 == 0:
            a_real_ipt = a_test_pool.batch()
            b_real_ipt = b_test_pool.batch()
            [a2b_opt, a2b2a_opt, b2a_opt,
             b2a2b_opt] = sess.run([a2b, a2b2a, b2a, b2a2b],
                                   feed_dict={
                                       a_real: a_real_ipt,
                                       b_real: b_real_ipt
                                   })
            sample_opt = np.concatenate((a_real_ipt, a2b_opt, a2b2a_opt,
                                         b_real_ipt, b2a_opt, b2a2b_opt),
                                        axis=0)

            save_dir = './outputs/sample_images_while_training/' + dataset
            utils.mkdir(save_dir)
            im.imwrite(
                im.immerge(sample_opt, 2, 3), '%s/Epoch_(%d)_(%dof%d).jpg' %
                (save_dir, epoch, it_epoch, batch_epoch))
except:
    save_path = saver.save(
        sess, '%s/Epoch_(%d)_(%dof%d).ckpt' %
        (ckpt_dir, epoch, it_epoch, batch_epoch))
    print('Model saved in file: % s' % save_path)
    sess.close()
示例#17
0
def save_single_img(a_real_ipt, b_real_ipt, save_dir, fname, forward_mapping=True):
    [a2b_opt] = sess.run([a2b if forward_mapping else b2a], feed_dict={a_real: a_real_ipt, b_real: b_real_ipt})
    sample_opt = np.array(a2b_opt)
    utils.mkdir(save_dir + '/')
    targetDir = '%s/%s' % (save_dir, fname)
    im.imwrite(im.immerge(sample_opt,1,1), targetDir)