def main(args):
    g = tf.Graph()      # A new graph
    with g.as_default():
        with tf.Session() as sess:
            # Building graph.
            image_data = tf.placeholder(tf.int32, name='input_image')
            height = tf.placeholder(tf.int32, name='height')
            width = tf.placeholder(tf.int32, name='width')

            # Reshape data
            image = tf.reshape(image_data, [height, width, 3])

            processed_image = utils.mean_image_subtraction(
                image, [123.68, 116.779, 103.939])                    # Preprocessing image
            batched_image = tf.expand_dims(processed_image, 0)        # Add batch dimension
            generated_image = model.net(batched_image, training=False)
            casted_image = tf.cast(generated_image, tf.int32)
            # Remove batch dimension
            squeezed_image = tf.squeeze(casted_image, [0])
            cropped_image = tf.slice(squeezed_image, [0, 0, 0], [height, width, 3])
            # stylized_image = tf.image.encode_jpeg(squeezed_image, name='output_image')
            stylized_image_data = tf.reshape(cropped_image, [-1], name='output_image')

            # Restore model variables.
            saver = tf.train.Saver(tf.global_variables(), write_version=tf.train.SaverDef.V1)
            sess.run([tf.global_variables_initializer(), tf.local_variables_initializer()])
            # Use absolute path.
            model_file = os.path.abspath(args.model_file)
            saver.restore(sess, model_file)

            if args.is_debug:
                content_file = '/Users/Lex/Desktop/t.jpg'
                generated_file = '/Users/Lex/Desktop/xwz-stylized.jpg'

                with open(generated_file, 'wb') as img:
                    image_bytes = tf.read_file(content_file)
                    input_array, decoded_image = sess.run([
                        tf.reshape(tf.image.decode_jpeg(image_bytes, channels=3), [-1]),
                        tf.image.decode_jpeg(image_bytes, channels=3)])

                    start_time = time.time()
                    img.write(sess.run(tf.image.encode_jpeg(tf.cast(cropped_image, tf.uint8)), feed_dict={
                              image_data: input_array,
                              height: decoded_image.shape[0],
                              width: decoded_image.shape[1]}))
                    end_time = time.time()

                    tf.logging.info('Elapsed time: %fs' % (end_time - start_time))
            else:
                output_graph_def = tf.graph_util.convert_variables_to_constants(
                    sess, sess.graph_def, output_node_names=['output_image'])

                with tf.gfile.FastGFile('/Users/Lex/Desktop/' + args.model_name + '.pb', mode='wb') as f:
                    f.write(output_graph_def.SerializeToString())
示例#2
0
def main(_):

    # Get image's height and width.
    height = 0
    width = 0
    with open(FLAGS.image_file, 'rb') as img:
        with tf.Session().as_default() as sess:
            if FLAGS.image_file.lower().endswith('png'):
                image = sess.run(tf.image.decode_png(img.read()))
            else:
                image = sess.run(tf.image.decode_jpeg(img.read()))
            height = image.shape[0]
            width = image.shape[1]
    tf.logging.info('Image size: %dx%d' % (width, height))

    with tf.Graph().as_default():
        with tf.Session().as_default() as sess:

            # Read image data.
            image_preprocessing_fn, _ = preprocessing_factory.get_preprocessing(
                FLAGS.loss_model,
                is_training=False)
            image = reader.get_image(FLAGS.image_file, height, width, image_preprocessing_fn)

            # Add batch dimension
            image = tf.expand_dims(image, 0)

            generated = model.net(image, training=False)
            generated = tf.cast(generated, tf.uint8)

            # Remove batch dimension
            generated = tf.squeeze(generated, [0])

            # Restore model variables.
            saver = tf.train.Saver(tf.global_variables(), write_version=tf.train.SaverDef.V1)
            sess.run([tf.global_variables_initializer(), tf.local_variables_initializer()])
            # Use absolute path
            FLAGS.model_file = os.path.abspath(FLAGS.model_file)
            saver.restore(sess, FLAGS.model_file)

            # Make sure 'generated' directory exists.
            generated_file = 'generated/res.jpg'
            if os.path.exists('generated') is False:
                os.makedirs('generated')

            # Generate and write image data to file.
            with open(generated_file, 'wb') as img:
                start_time = time.time()
                img.write(sess.run(tf.image.encode_jpeg(generated)))
                end_time = time.time()
                tf.logging.info('Elapsed time: %fs' % (end_time - start_time))

                tf.logging.info('Done. Please check %s.' % generated_file)
示例#3
0
    def run(optim):
        progress = make_progressbar("Training with " + str(optim), 5)
        progress.start()

        model = net()
        model.training()
        for epoch in range(5):
            train(Xtrain, ytrain, model, optim, criterion, batch_size, "train")
            train(Xtrain, ytrain, model, optim, criterion, batch_size, "stats")
            progress.update(epoch + 1)

        progress.finish()

        model.evaluate()
        nll, _ = test(Xtrain, ytrain, model, batch_size)
        _, nerr = test(Xval, yval, model, batch_size)

        print("Trainset NLL: {:.2f}".format(nll))
        print("Testset errors: {}".format(nerr))
示例#4
0
def main(_):
    height = 0
    width = 0
    with open(FLAGS.image_file, 'rb') as img:
        with tf.Session().as_default() as sess:
            if FLAGS.image_file.lower().endswith('png'):
                image = sess.run(tf.image.decode_png(img.read()))
            else:
                image = sess.run(tf.image.decode_jpeg(img.read()))
            height = image.shape[0]
            width = image.shape[1]
    tf.logging.info('Image size: %dx%d' % (width, height))

    with tf.Graph().as_default():
        with tf.Session().as_default() as sess:
            image_preprocessing_fn, _ = preprocessing_factory.get_preprocessing(
                FLAGS.loss_model,
                is_training=False)
            image = reader.get_image(FLAGS.image_file, height, width, image_preprocessing_fn)
            image = tf.expand_dims(image, 0)
            generated = model.net(image, training=False)
            generated = tf.squeeze(generated, [0])
            saver = tf.train.Saver(tf.all_variables())
            sess.run([tf.initialize_all_variables(), tf.initialize_local_variables()])
            FLAGS.model_file = os.path.abspath(FLAGS.model_file)
            saver.restore(sess, FLAGS.model_file)

            start_time = time.time()
            generated = sess.run(generated)
            generated = tf.cast(generated, tf.uint8)
            end_time = time.time()
            tf.logging.info('Elapsed time: %fs' % (end_time - start_time))
            generated_file = 'generated/res.jpg'
            if os.path.exists('generated') is False:
                os.makedirs('generated')
            with open(generated_file, 'wb') as img:
                img.write(sess.run(tf.image.encode_jpeg(generated)))
                tf.logging.info('Done. Please check %s.' % generated_file)
示例#5
0
def main(argv=None):
    with open(FLAGS.IMAGE_PATH, 'rb') as f:
        jpg = f.read()

    image = tf.image.convert_image_dtype(tf.image.decode_jpeg(jpg, channels=3), tf.float32) * 255.
    images = tf.expand_dims(image, 0)

    generated_images = model.net(images - reader.mean_pixel, training=False)

    output_format = tf.cast(generated_images, tf.uint8)
    jpegs = tf.map_fn(lambda image: tf.image.encode_jpeg(image), output_format, dtype=tf.string)

    with tf.Session() as sess:
        file = tf.train.latest_checkpoint(model_path)
        if not file:
            print('Could not find trained model in %s' % model_path)
            return
        print('Using model from %s' % file)
        saver = tf.train.Saver()
        saver.restore(sess, file)

        images_t = sess.run(jpegs)
        with open('res.jpg', 'wb') as f:
            f.write(images_t[0])
示例#6
0
from training_schedules import POLICY
from src.dataloader import load_batch
from src.utils import schedule_verbose, ckpt_reader
from model import net
# DATA_PATH = '/home/jaehyuk/code/own/tracker/data/vot2015'

# check POLICY
# check load_batch, train, sample, test
# check log_dir(slim get latest ckpt)

# ckpt = './logs/Tracker/model.ckpt-1043223'
ckpt = None
log_dir = './logs/Tracker/'
data_type = 'train'

Tracker = net()
schedule_verbose(POLICY, data_type)

# data loader
# TODO, dimension check
pimg_resize, cimg_resize, pbox_xy, pROI, pROI_anchor,\
    confs, coord, areas, upleft, botright = load_batch(POLICY, data_type)

# check ckpt variable
if ckpt:
    ckpt_reader(ckpt, value=False)

Tracker.train(log_dir=log_dir,
              training_schedule=POLICY,
              pimg_resize=pimg_resize,
              cimg_resize=cimg_resize,
示例#7
0
def transform(image_file="img/test4.jpg",
              model_file="models/cubist.ckpt-done",
              loss_model="vgg_16"):

    tf.logging.set_verbosity(tf.logging.INFO)
    # Get image's height and width.
    height = 0
    width = 0
    with open(image_file, 'rb') as img:
        with tf.Session().as_default() as sess:
            if image_file.lower().endswith('png'):
                image = sess.run(tf.image.decode_png(img.read()))
            else:
                image = sess.run(tf.image.decode_jpeg(img.read()))
            height = image.shape[0]
            width = image.shape[1]
    tf.logging.info('图像尺寸为: %dx%d' % (width, height))

    with tf.Graph().as_default():
        with tf.Session().as_default() as sess:

            # Read image data.
            image_preprocessing_fn, _ = preprocessing_factory.get_preprocessing(
                loss_model, is_training=False)
            image = reader.get_image(image_file, height, width,
                                     image_preprocessing_fn)

            # Add batch dimension
            image = tf.expand_dims(image, 0)

            generated = model.net(image, training=False)
            generated = tf.cast(generated, tf.uint8)

            # Remove batch dimension
            generated = tf.squeeze(generated, [0])

            # Restore model variables.
            saver = tf.train.Saver(tf.global_variables(),
                                   write_version=tf.train.SaverDef.V1)
            sess.run([
                tf.global_variables_initializer(),
                tf.local_variables_initializer()
            ])
            # Use absolute path
            model_file = os.path.abspath(model_file)
            saver.restore(sess, model_file)

            # Make sure 'generated' directory exists.
            localtime = time.time()  #这样生成的是当前时间戳,不包含空格
            generatedname = localtime + '.jpg'
            generated_file = os.path.join(os.getcwd(), generatedname)
            if os.path.exists('generated') is False:
                os.makedirs('generated')

            # Generate and write image data to file.
            with open(generated_file, 'wb') as img:
                start_time = time.time()
                img.write(sess.run(tf.image.encode_jpeg(generated)))
                end_time = time.time()
                tf.logging.info('耗时: %fs' % (end_time - start_time))

                tf.logging.info('图像风格转换完成. 请查看 %s.' % generated_file)
示例#8
0
def main(FLAGS):
    style_features_t = losses.get_style_features(FLAGS)

    # Make sure the training path exists.
    training_path = os.path.join(FLAGS.model_path, FLAGS.naming)
    if not (os.path.exists(training_path)):
        os.makedirs(training_path)

    with tf.Graph().as_default():
        with tf.Session() as sess:
            """Build Network"""
            network_fn = nets_factory.get_network_fn(FLAGS.loss_model,
                                                     num_classes=1,
                                                     is_training=False)

            image_preprocessing_fn, image_unprocessing_fn = preprocessing_factory.get_preprocessing(
                FLAGS.loss_model, is_training=False)
            processed_images = reader.image(FLAGS.batch_size,
                                            FLAGS.image_size,
                                            FLAGS.image_size,
                                            'train2014/',
                                            image_preprocessing_fn,
                                            epochs=FLAGS.epoch)
            generated = model.net(processed_images, FLAGS=FLAGS, training=True)
            processed_generated = [
                image_preprocessing_fn(image, FLAGS.image_size,
                                       FLAGS.image_size) for image in
                tf.unstack(generated, axis=0, num=FLAGS.batch_size)
            ]
            processed_generated = tf.stack(processed_generated)
            _, endpoints_dict = network_fn(tf.concat(
                [processed_generated, processed_images], 0),
                                           spatial_squeeze=False)

            # Log the structure of loss network
            tf.logging.info(
                'Loss network layers(You can define them in "content_layers" and "style_layers"):'
            )
            for key in endpoints_dict:
                tf.logging.info(key)
            """Build Losses"""
            content_loss = losses.content_loss(endpoints_dict,
                                               FLAGS.content_layers)
            style_loss, style_loss_summary = losses.style_loss(
                endpoints_dict, style_features_t, FLAGS.style_layers)
            tv_loss = losses.total_variation_loss(
                generated)  # use the unprocessed image

            loss = FLAGS.style_weight * style_loss + FLAGS.content_weight * content_loss + FLAGS.tv_weight * tv_loss

            # Add Summary for visualization in tensorboard.
            """Add Summary"""
            tf.summary.scalar('losses/content_loss', content_loss)
            tf.summary.scalar('losses/style_loss', style_loss)
            tf.summary.scalar('losses/regularizer_loss', tv_loss)

            tf.summary.scalar('weighted_losses/weighted_content_loss',
                              content_loss * FLAGS.content_weight)
            tf.summary.scalar('weighted_losses/weighted_style_loss',
                              style_loss * FLAGS.style_weight)
            tf.summary.scalar('weighted_losses/weighted_regularizer_loss',
                              tv_loss * FLAGS.tv_weight)
            tf.summary.scalar('total_loss', loss)

            for layer in FLAGS.style_layers:
                tf.summary.scalar('style_losses/' + layer,
                                  style_loss_summary[layer])
            tf.summary.image('generated', generated)
            # tf.image_summary('processed_generated', processed_generated)  # May be better?
            tf.summary.image(
                'origin',
                tf.stack([
                    image_unprocessing_fn(image) for image in tf.unstack(
                        processed_images, axis=0, num=FLAGS.batch_size)
                ]))
            summary = tf.summary.merge_all()
            writer = tf.summary.FileWriter(training_path)
            """Prepare to Train"""
            global_step = tf.Variable(0, name="global_step", trainable=False)

            variable_to_train = []
            for variable in tf.trainable_variables():
                if not (variable.name.startswith(FLAGS.loss_model)):
                    variable_to_train.append(variable)
            train_op = tf.train.AdamOptimizer(1e-3).minimize(
                loss, global_step=global_step, var_list=variable_to_train)

            variables_to_restore = []
            for v in tf.global_variables():
                if not (v.name.startswith(FLAGS.loss_model)):
                    variables_to_restore.append(v)
            variables_to_restore = [
                var for var in variables_to_restore if 'Adam' not in var.name
            ]
            saver = tf.train.Saver(variables_to_restore,
                                   write_version=tf.train.SaverDef.V1)

            sess.run([
                tf.global_variables_initializer(),
                tf.local_variables_initializer()
            ])

            # Restore variables for loss network.
            init_func = utils._get_init_fn(FLAGS)
            init_func(sess)

            # Restore variables for training model if the checkpoint file exists.
            last_file = tf.train.latest_checkpoint(training_path)
            if last_file:
                tf.logging.info('Restoring model from {}'.format(last_file))
                saver.restore(sess, last_file)
            """Start Training"""
            coord = tf.train.Coordinator()
            threads = tf.train.start_queue_runners(coord=coord)
            start_time = time.time()
            try:
                while not coord.should_stop():
                    _, loss_t, step = sess.run([train_op, loss, global_step])
                    elapsed_time = time.time() - start_time
                    start_time = time.time()
                    """logging"""
                    # print(step)
                    if step % 10 == 0:
                        tf.logging.info(
                            'step: %d,  total Loss %f, secs/step: %f' %
                            (step, loss_t, elapsed_time))
                    """summary"""
                    if step % 25 == 0:
                        tf.logging.info('adding summary...')
                        summary_str = sess.run(summary)
                        writer.add_summary(summary_str, step)
                        writer.flush()
                    """checkpoint"""
                    if step % 1000 == 0:
                        saver.save(sess,
                                   os.path.join(training_path,
                                                'fast-style-model.ckpt'),
                                   global_step=step)
                    if step == 20000:
                        saver.save(
                            sess,
                            os.path.join(training_path,
                                         'fast-style-model.ckpt-done'))
                        break
            except tf.errors.OutOfRangeError:
                saver.save(
                    sess,
                    os.path.join(training_path, 'fast-style-model.ckpt-done'))
                tf.logging.info('Done training -- epoch limit reached')
            finally:
                coord.request_stop()
            coord.join(threads)
示例#9
0
def general_nfold_cv(XD, XT, Y, label_row_inds, label_col_inds, prfmeasure,
                     FLAGS, labeled_sets, val_sets, get_aupr, get_rm2):

    paramset1 = FLAGS.num_windows
    paramset2 = FLAGS.smi_window_lengths
    paramset3 = FLAGS.seq_window_lengths
    lamda_set = FLAGS.lamda
    batchsz = FLAGS.batch_size  # 256

    logging("---Parameter Search-----", FLAGS)

    w = len(val_sets)
    h = len(paramset1) * len(paramset2) * len(paramset3) * len(lamda_set)

    all_predictions = [[0 for x in range(w)] for y in range(h)]
    all_losses = [[0 for x in range(w)] for y in range(h)]

    for foldind in range(len(val_sets)):
        valinds = val_sets[foldind]
        labeledinds = labeled_sets[foldind]

        trrows = label_row_inds[labeledinds]
        trcols = label_col_inds[labeledinds]

        train_dataset = prepare_interaction_pairs(XD, XT, Y, trrows, trcols)

        terows = label_row_inds[valinds]
        tecols = label_col_inds[valinds]

        test_dataset = prepare_interaction_pairs(XD, XT, Y, terows, tecols)

        pointer = 0

        train_loader = DataLoader(dataset=train_dataset,
                                  batch_size=batchsz,
                                  shuffle=True)
        test_loader = DataLoader(dataset=test_dataset, batch_size=batchsz)
        for param1value in paramset1:  # hidden neurons
            for param2value in paramset2:  # learning rate
                for param3value in paramset3:
                    for lamda in lamda_set:
                        model = net(FLAGS, param1value, param2value,
                                    param3value).cuda()
                        model.apply(weights_init)
                        rperf_list = []
                        for epochind in range(FLAGS.num_epoch):
                            model = train(train_loader, model, FLAGS,
                                          param1value, param2value,
                                          param3value, lamda)
                            rperf, loss, rm2, auc = test(
                                model, test_loader, FLAGS, param1value,
                                param2value, param3value, lamda)
                            rperf_list.append(rperf)
                            ##Set the conditions for early stopping
                            if (epochind + 1) % 5 == 0:
                                print(
                                    'val: epoch:{},p1:{},p2:{},p3:{},loss:{:.5f},rperf:{:.5f}, rm2:{:.5f}'
                                    .format(epochind, param1value, param2value,
                                            param3value, loss, rperf, rm2))

                                if rperf >= max(rperf_list):
                                    torch.save(model, 'checkpoint.pth')
                                if rperf < max(rperf_list) - 0.1:
                                    print(
                                        'The program is stopped early for better performance.'
                                    )
                                    break
                        logging(
                            "P1 = %d,  P2 = %d, P3 = %d, Fold = %d, CI-i = %f, MSE = %f, rm2 = %f"
                            % (param1value, param2value, param3value, foldind,
                               rperf, loss, rm2), FLAGS)

                        all_predictions[pointer][
                            foldind] = rperf  # TODO FOR EACH VAL SET allpredictions[pointer][foldind]
                        all_losses[pointer][foldind] = loss

                    pointer += 1

    bestperf = -float('Inf')
    bestpointer = None

    best_param_list = []
    ##Take average according to folds, then chooose best params
    pointer = 0
    for param1value in paramset1:
        for param2value in paramset2:
            for param3value in paramset3:
                for lamda in lamda_set:
                    avgperf = 0.
                    for foldind in range(len(val_sets)):
                        foldperf = all_predictions[pointer][foldind]
                        avgperf += foldperf
                    avgperf /= len(val_sets)

                    if avgperf > bestperf:
                        bestperf = avgperf
                        bestpointer = pointer
                        best_param_list = [
                            param1value,
                            param2value,
                            param3value,
                            lamda,
                        ]

                    pointer += 1

    return bestpointer, best_param_list, bestperf, all_predictions, all_losses
示例#10
0
def main(args):
    g = tf.Graph()  # A new graph
    with g.as_default():
        with tf.Session() as sess:
            # Building graph.
            image_data = tf.placeholder(tf.int32, name='input_image')
            height = tf.placeholder(tf.int32, name='height')
            width = tf.placeholder(tf.int32, name='width')

            # Reshape data
            image = tf.reshape(image_data, [height, width, 3])

            processed_image = utils.mean_image_subtraction(
                image, [123.68, 116.779, 103.939])  # Preprocessing image
            batched_image = tf.expand_dims(processed_image,
                                           0)  # Add batch dimension
            generated_image = model.net(batched_image, training=False)
            casted_image = tf.cast(generated_image, tf.int32)
            # Remove batch dimension
            squeezed_image = tf.squeeze(casted_image, [0])
            cropped_image = tf.slice(squeezed_image, [0, 0, 0],
                                     [height, width, 3])
            # stylized_image = tf.image.encode_jpeg(squeezed_image, name='output_image')
            stylized_image_data = tf.reshape(cropped_image, [-1],
                                             name='output_image')

            # Restore model variables.
            saver = tf.train.Saver(tf.global_variables(),
                                   write_version=tf.train.SaverDef.V1)
            sess.run([
                tf.global_variables_initializer(),
                tf.local_variables_initializer()
            ])
            # Use absolute path.
            model_file = os.path.abspath(args.model_file)
            saver.restore(sess, model_file)

            if args.is_debug:
                content_file = '/Users/Lex/Desktop/t.jpg'
                generated_file = '/Users/Lex/Desktop/xwz-stylized.jpg'

                with open(generated_file, 'wb') as img:
                    image_bytes = tf.read_file(content_file)
                    input_array, decoded_image = sess.run([
                        tf.reshape(
                            tf.image.decode_jpeg(image_bytes, channels=3),
                            [-1]),
                        tf.image.decode_jpeg(image_bytes, channels=3)
                    ])

                    start_time = time.time()
                    img.write(
                        sess.run(tf.image.encode_jpeg(
                            tf.cast(cropped_image, tf.uint8)),
                                 feed_dict={
                                     image_data: input_array,
                                     height: decoded_image.shape[0],
                                     width: decoded_image.shape[1]
                                 }))
                    end_time = time.time()

                    tf.logging.info('Elapsed time: %fs' %
                                    (end_time - start_time))
            else:
                output_graph_def = tf.graph_util.convert_variables_to_constants(
                    sess, sess.graph_def, output_node_names=['output_image'])

                with tf.gfile.FastGFile('/Users/Lex/Desktop/' +
                                        args.model_name + '.pb',
                                        mode='wb') as f:
                    f.write(output_graph_def.SerializeToString())
def main(argv=None):
    run_id = FLAGS.NAME if FLAGS.NAME else str(uuid.uuid4())
    model_path = '%s/%s' % (FLAGS.MODEL_PATH, run_id)
    if not os.path.exists(model_path):
        os.makedirs(model_path)
    summary_path = '%s/%s' % (FLAGS.SUMMARY_PATH, run_id)
    if not os.path.exists(summary_path):
        os.makedirs(summary_path)

    style_paths = FLAGS.STYLE_IMAGES.split(',')
    style_layers = FLAGS.STYLE_LAYERS.split(',')
    content_layers = FLAGS.CONTENT_LAYERS.split(',')

    style_features_t = get_style_features(style_paths, style_layers)

    images = reader.image(FLAGS.BATCH_SIZE, FLAGS.IMAGE_SIZE, FLAGS.TRAIN_IMAGES_PATH)
    generated = model.net(images - reader.mean_pixel, training=True)

    # Put both generated and training images in same batch through VGG net for efficiency
    net, _ = vgg.net(FLAGS.VGG_PATH, tf.concat(0, [generated, images]) - reader.mean_pixel)

    content_loss = 0
    for layer in content_layers:
        generated_images, content_images = tf.split(0, 2, net[layer])
        size = tf.size(generated_images)
        shape = tf.shape(generated_images)
        width = shape[1]
        height = shape[2]
        num_filters = shape[3]
        content_loss += tf.nn.l2_loss(generated_images - content_images) / tf.to_float(size)
    content_loss = content_loss

    style_loss = 0
    for style_grams, layer in zip(style_features_t, style_layers):
        generated_images, _ = tf.split(0, 2, net[layer])
        size = tf.size(generated_images)
        for style_gram in style_grams:
            style_loss += tf.nn.l2_loss(gram(generated_images) - style_gram) / tf.to_float(size)
    style_loss = style_loss / len(style_paths)

    tv_loss = total_variation_loss(generated)

    loss = FLAGS.STYLE_WEIGHT * style_loss + FLAGS.CONTENT_WEIGHT * content_loss + FLAGS.TV_WEIGHT * tv_loss

    global_step = tf.Variable(0, name="global_step", trainable=False)
    train_op = tf.train.AdamOptimizer(1e-3).minimize(loss, global_step=global_step)

    # Statistics
    with tf.name_scope('losses'):
        tf.scalar_summary('content loss', content_loss)
        tf.scalar_summary('style loss', style_loss)
        tf.scalar_summary('regularizer loss', tv_loss)
    with tf.name_scope('weighted_losses'):
        tf.scalar_summary('weighted content loss', content_loss * FLAGS.CONTENT_WEIGHT)
        tf.scalar_summary('weighted style loss', style_loss * FLAGS.STYLE_WEIGHT)
        tf.scalar_summary('weighted regularizer loss', tv_loss * FLAGS.TV_WEIGHT)
        tf.scalar_summary('total loss', loss)
    tf.image_summary('original', images)
    tf.image_summary('generated', generated)

    summary = tf.merge_all_summaries()
    step=0
    with tf.Session() as sess:
        writer = tf.train.SummaryWriter(summary_path, sess.graph)

        saver = tf.train.Saver(tf.all_variables())
        file = tf.train.latest_checkpoint(model_path)
        sess.run([tf.initialize_all_variables(), tf.initialize_local_variables()])
        if file:
            print('Restoring model from {}'.format(file))
            saver.restore(sess, file)     

        coord = tf.train.Coordinator()
        threads = tf.train.start_queue_runners(coord=coord)
        start_time = time.time()
        try:
            while step<=1000:
                _, loss_t, step = sess.run([train_op, loss, global_step])
                elapsed_time = time.time() - start_time
                start_time = time.time()
                if step % 10== 0:
                    print(step, loss_t, elapsed_time)
                    summary_str = sess.run(summary)
                    writer.add_summary(summary_str, step)
                if step % 10 == 0:
                    saver.save(sess, model_path + '/fast-style-model', global_step=step)
        except tf.errors.OutOfRangeError:
            saver.save(sess, model_path + '/fast-style-model-done')
            print('Done training -- epoch limit reached')
        finally:
            coord.request_stop()
        coord.join(threads)
示例#12
0
def main(argv=None):
    content_layers = FLAGS.content_layers.split(',')
    style_layers = FLAGS.style_layers.split(',')
    style_layers_weights = [
        float(i) for i in FLAGS.style_layers_weights.split(",")
    ]
    #num_steps_decay = 82786 / FLAGS.batch_size
    num_steps_decay = 10000

    style_features_t = losses.get_style_features(FLAGS)
    training_path = os.path.join(FLAGS.model_path, FLAGS.naming)
    if not (os.path.exists(training_path)):
        os.makedirs(training_path)

    with tf.Session() as sess:
        """Build Network"""
        network_fn = nets_factory.get_network_fn(FLAGS.loss_model,
                                                 num_classes=1,
                                                 is_training=False)
        image_preprocessing_fn, image_unprocessing_fn = preprocessing_factory.get_preprocessing(
            FLAGS.loss_model, is_training=False)
        processed_images = reader.image(FLAGS.batch_size,
                                        FLAGS.image_size,
                                        FLAGS.image_size,
                                        'train2014/',
                                        image_preprocessing_fn,
                                        epochs=FLAGS.epoch)
        generated = model.net(processed_images, FLAGS.alpha)
        processed_generated = [
            image_preprocessing_fn(image, FLAGS.image_size, FLAGS.image_size)
            for image in tf.unstack(generated, axis=0, num=FLAGS.batch_size)
        ]
        processed_generated = tf.stack(processed_generated)
        _, endpoints_dict = network_fn(tf.concat(
            [processed_generated, processed_images], 0),
                                       spatial_squeeze=False)
        """Build Losses"""
        content_loss = losses.content_loss(endpoints_dict, content_layers)
        style_loss, style_losses = losses.style_loss(endpoints_dict,
                                                     style_features_t,
                                                     style_layers,
                                                     style_layers_weights)
        tv_loss = losses.total_variation_loss(
            generated)  # use the unprocessed image
        content_loss = FLAGS.content_weight * content_loss
        style_loss = FLAGS.style_weight * style_loss
        tv_loss = FLAGS.tv_weight * tv_loss
        loss = style_loss + content_loss + tv_loss
        """Prepare to Train"""
        global_step = tf.Variable(0, name="global_step", trainable=False)
        variable_to_train = []
        for variable in tf.trainable_variables():
            if not (variable.name.startswith(FLAGS.loss_model)):
                variable_to_train.append(variable)

        lr = tf.train.exponential_decay(learning_rate=1e-1,
                                        global_step=global_step,
                                        decay_steps=num_steps_decay,
                                        decay_rate=1e-1,
                                        staircase=True)
        optimizer = tf.train.AdamOptimizer(learning_rate=lr, epsilon=1e-8)
        train_op = optimizer.minimize(loss,
                                      global_step=global_step,
                                      var_list=variable_to_train)
        #train_op = tf.train.AdamOptimizer(1e-3).minimize(loss, global_step=global_step, var_list=variable_to_train)
        variables_to_restore = []
        for v in tf.global_variables():
            if not (v.name.startswith(FLAGS.loss_model)):
                variables_to_restore.append(v)
        saver = tf.train.Saver(variables_to_restore)
        sess.run([
            tf.global_variables_initializer(),
            tf.local_variables_initializer()
        ])
        init_func = utils._get_init_fn(FLAGS)
        init_func(sess)
        last_file = tf.train.latest_checkpoint(training_path)
        if last_file:
            saver.restore(sess, last_file)
        """Start Training"""
        coord = tf.train.Coordinator()
        threads = tf.train.start_queue_runners(coord=coord)
        try:
            while not coord.should_stop():
                _, c_loss, s_losses, t_loss, total_loss, step = sess.run([
                    train_op, content_loss, style_losses, tv_loss, loss,
                    global_step
                ])
                """logging"""
                if step % 10 == 0:
                    print(step, c_loss, s_losses, t_loss, total_loss)
                """checkpoint"""
                if step % 10000 == 0:
                    saver.save(sess,
                               os.path.join(training_path, 'fast-style-model'),
                               global_step=step)
                if step == FLAGS.max_iter:
                    saver.save(
                        sess,
                        os.path.join(training_path, 'fast-style-model-done'))
                    break
        except tf.errors.OutOfRangeError:
            saver.save(sess,
                       os.path.join(training_path, 'fast-style-model-done'))
            tf.logging.info('Done training -- epoch limit reached')
        finally:
            coord.request_stop()
        coord.join(threads)
示例#13
0
def main():
    # get the style features of the style image in the loss model
    style_features = get_style_features()

    training_path = os.path.join(model_path, style_name)
    if os.path.exists(training_path) is False:
        os.makedirs(training_path)

    with tf.Graph().as_default():
        with tf.Session() as sess:
            """Build Network"""
            # loss network
            net_fn = nets_factory.get_network_fn(loss_model,
                                                 num_classes=1,
                                                 is_training=False)

            # get preprocessing function
            preprocessing_fn, unprocessing_fn = preprocessing_factory.get_preprocessing(
                loss_model, is_training=False)

            # read images for training
            processed_images = read_images(train_image_path,
                                           preprocessing_fn,
                                           image_size,
                                           image_size,
                                           batch_size=batch_size,
                                           epoch=2,
                                           shuffle=True)

            generated = model.net(processed_images, training=True)
            processed_generated = [
                preprocessing_fn(image, image_size, image_size)
                for image in tf.unstack(generated, axis=0, num=batch_size)
            ]
            processed_generated = tf.stack(processed_generated)

            _, endpoints_dict = net_fn(tf.concat(
                [processed_generated, processed_images], 0),
                                       spatial_squeeze=False)

            # Log the structure of loss network
            tf.logging.info(
                'Loss network layers(You can define them in "content_layers" and "style_layers"):'
            )
            for key in endpoints_dict:
                tf.logging.info(key)

            # calculate losses
            content_loss = calcu_content_loss(endpoints_dict, content_layers)
            style_loss, style_loss_summary = calcu_style_loss(
                endpoints_dict, style_features, style_layers)
            tv_loss = total_variation_loss(generated)

            loss = content_loss * content_weight + style_loss * style_weight + tv_loss * tv_weight

            # Add Summary for visualization in tensorboard.
            """Add Summary"""
            tf.summary.scalar('losses/content_loss', content_loss)
            tf.summary.scalar('losses/style_loss', style_loss)
            tf.summary.scalar('losses/regularizer_loss', tv_loss)

            tf.summary.scalar('weighted_losses/weighted_content_loss',
                              content_loss * content_weight)
            tf.summary.scalar('weighted_losses/weighted_style_loss',
                              style_loss * style_weight)
            tf.summary.scalar('weighted_losses/weighted_regularizer_loss',
                              tv_loss * tv_weight)
            tf.summary.scalar('total_loss', loss)

            for layer in style_layers:
                tf.summary.scalar('style_losses/' + layer,
                                  style_loss_summary[layer])
            tf.summary.image('generated', generated)
            # tf.image_summary('processed_generated', processed_generated)  # May be better?
            tf.summary.image(
                'origin',
                tf.stack([
                    unprocessing_fn(image) for image in tf.unstack(
                        processed_images, axis=0, num=batch_size)
                ]))
            tf.summary.image(
                'processed',
                tf.stack([
                    image for image in tf.unstack(
                        processed_images, axis=0, num=batch_size)
                ]))
            summary = tf.summary.merge_all()
            writer = tf.summary.FileWriter(training_path, sess.graph)
            """Prepare to Train"""
            global_step = tf.Variable(0, name='global_step', trainable=False)

            variable_to_train = []
            for variable in tf.trainable_variables():
                if not (variable.name.startswith(loss_model)):
                    variable_to_train.append(variable)
            train_optimizer = tf.train.AdamOptimizer(1e-3).minimize(
                loss, global_step=global_step, var_list=variable_to_train)

            variables_to_restore = []
            for variable in tf.global_variables():
                if not (variable.name.startswith(loss_model)):
                    variables_to_restore.append(variable)
            saver = tf.train.Saver(variables_to_restore,
                                   write_version=tf.train.SaverDef.V1)

            sess.run([
                tf.global_variables_initializer(),
                tf.local_variables_initializer()
            ])

            # Restore variables for loss network.
            init_func = get_init_fn(loss_model_file, checkpoint_exclude_scopes)
            init_func(sess)

            last_file = tf.train.latest_checkpoint(training_path)
            if last_file:
                tf.logging.info('Restoring model from {}'.format(last_file))
                saver.restore(sess, last_file)

            coord = tf.train.Coordinator()
            threads = tf.train.start_queue_runners(coord=coord)
            start_time = time.time()
            try:
                while not coord.should_stop():
                    _, loss_t, step = sess.run(
                        [train_optimizer, loss, global_step])
                    evaluate_time = time.time() - start_time
                    start_time = time.time()
                    if step % 10 == 0:
                        tf.logging.info(
                            'step: %d,  total Loss %f, secs/step: %f' %
                            (step, loss_t, evaluate_time))
                    if step % 25 == 0:
                        tf.logging.info('adding summary...')
                        summary_str = sess.run(summary)
                        writer.add_summary(summary_str, step)
                        writer.flush()
                    if step % 200 == 0:
                        saver.save(sess,
                                   os.path.join(training_path,
                                                'fast-style-model.ckpt'),
                                   global_step=step)
                        evaluate(
                            evaluate_test_image,
                            os.path.join(
                                training_path,
                                'fast-style-model.ckpt' + '-' + str(step)),
                            os.path.join(training_path,
                                         str(step) + '.jpg'))
            except tf.errors.OutOfRangeError:
                saver.save(
                    sess,
                    os.path.join(training_path, 'fast-style-model.ckpt-done'))
                tf.logging.info('Done training -- epoch limit reached')
            finally:
                coord.request_stop()
            coord.join(threads)
示例#14
0
styImg_H = styImgReadedsize[2]

## 读取一批内容图像(是通道,没有实际数据)
ContImgReaded = data_io.get_cont_batch(image_file_dir, 256, 256, BATCH_SIZE,
                                       num_epochs)  #---喂给content_images

## 一张固定的风格图像 通道
style_image = tf.placeholder(tf.float32, shape=[1, styImg_W, styImg_H, 3])

## 一批内容图像 的某层特征图 通道
content_images = tf.placeholder(tf.float32,
                                shape=[BATCH_SIZE, IMG_W, IMG_H, 3])

##一批风格转换网络的生成图像
#    generated_image = transform.net(content_images)
generated_image = model.net(content_images, training=True)

## 风格损失
style_gram_dict = myutil.img2Gram(style_image)  #风格图像的Gram矩阵字典
gen_gram_dict = myutil.img2Gram(generated_image)  #生成图像的Gram矩阵字典
style_loss = tools.style_loss(gen_gram_dict, style_gram_dict, style_weight)

## 内容损失
content_map_dict = myutil.img2ContMap(content_images)  #内容图像的内容特征字典
generated_feature_maps = myutil.img2ContMap(generated_image)  #生成图像的内容特征字典
content_loss = tools.content_loss(generated_feature_maps[CONTENT_LAYER],
                                  content_map_dict[CONTENT_LAYER],
                                  content_weight)

## tv损失
tv_loss = tools.tv_loss(generated_image, tv_weight)
示例#15
0
def main(_):

    # Get image's height and width.
    height = 0
    width = 0
    with tf.gfile.GFile(FLAGS.image_file, 'rb') as f:
        with tf.Session().as_default() as sess:
            if FLAGS.image_file.lower().endswith('png'):
                image = sess.run(tf.image.decode_png(f.read()))
            else:
                image = sess.run(tf.image.decode_jpeg(f.read()))
            height = image.shape[0]
            width = image.shape[1]
    tf.logging.info('Image size: %dx%d' % (width, height))

    with tf.Graph().as_default():
        with tf.Session().as_default() as sess:

            # Read image data.
            image_preprocessing_fn, _ = preprocessing_factory.get_preprocessing(
                FLAGS.loss_model,
                is_training=False)
            image = reader.get_image(FLAGS.image_file, height, width, image_preprocessing_fn)
            print(image)
            plt.subplot(121)
            plt.imshow(sess.run(image))

            # Add batch dimension
            image = tf.expand_dims(image, 0)

            generated = model.net(image, training=False)
            generated = tf.cast(generated, tf.uint8)


            # Remove batch dimension
            generated = tf.squeeze(generated, [0])

            # Restore model variables.
            saver = tf.train.Saver(tf.global_variables(), write_version=tf.train.SaverDef.V1)
            sess.run([tf.global_variables_initializer(), tf.local_variables_initializer()])
            # Use absolute path
            FLAGS.model_file = os.path.abspath(FLAGS.model_file)
            saver.restore(sess, FLAGS.model_file)
            
            summary_writer = tf.summary.FileWriter("logs",sess.graph)

            # Make sure 'generated' directory exists.
            generated_file = 'generated/res.jpg'
            if os.path.exists('generated') is False:
                os.makedirs('generated')

            # Generate and write image data to file.
            with tf.gfile.GFile(generated_file, 'wb') as f:
                
                plt.subplot(122)
                plt.imshow(sess.run(generated))
                plt.show()
                start_time = time.time()
                f.write(sess.run(tf.image.encode_jpeg(generated)))
                end_time = time.time()
                tf.logging.info('Elapsed time: %fs' % (end_time - start_time))

                tf.logging.info('Done. Please check %s.' % generated_file)
示例#16
0
def infer():
    # Load arguments
    args = parse_args()
    # args.batch_size = 1

    word2id = data.read_dictionary("data/pre_trained_word2id.pkl")
    embeddings = np.load("data/pre_trained_embeddings.npy")
    # word2id = data.read_dictionary("data/pre_trained_copy_mini_word2id.pkl")
    # embeddings = np.load("data/pre_trained_copy_mini_embeddings.npy")

    # word2id_output_mini = {}
    # for i, k in enumerate(word2id):
    #     word2id_output_mini[k] = i
    #     if i > 9100:
    #         break
    # word2id_output_mini["<S>"] = 1
    # word2id_output_mini["<E>"] = 2
    # word2id = word2id_output_mini

    word2id_output = word2id.copy()
    word_ori_size = len(word2id)
    # word_mini_size = len(word2id_output)
    # word_size = word_ori_size
    # word_size = word_mini_size

    word_size = 0
    tag_size = 0
    for k in tag2label:
        if tag2label[k] > tag_size:
            tag_size = tag2label[k]
        tag2label[k] += args.max_length
        if tag2label[k] > word_size:
            word_size = tag2label[k]
    # word2id_output.update(tag2label)
    word2id_output = tag2label
    word2id_output["<S>"] = word_size + 1
    word2id_output["<E>"] = word_size + 2
    word_size += 3
    tag_size += 3
    print("output size", word_size, tag_size)

    # # Dictrionaries init
    # word2id = data.read_dictionary("data/pre_trained_word2id.pkl")
    # embeddings = np.load("data/pre_trained_embeddings.npy")
    # word2id_output = word2id.copy()
    # word_mini_size = len(word2id)
    # word_size = 0
    # for k in tag2label:
    #     tag2label[k] += word_mini_size
    #     if tag2label[k] > word_size:
    #         word_size = tag2label[k]
    # tag2label["<S>"] = word_size + 1
    # tag2label["<E>"] = word_size + 2
    # word_size += 3
    # word2id_output.update(tag2label)
    # # print(type(word2id), len(word2id))
    # # print(type(entity2id), len(entity2id))
    # # print(type(pos2id), len(pos2id))
    # # print(type(word2id_output), len(word2id_output))
    id2entity = {}
    for k in entity2id:
        id2entity[entity2id[k]] = k
    id2word = {}
    for k in word2id:
        id2word[word2id[k]] = k
    id2word_output = {}
    for k in word2id_output:
        id2word_output[word2id_output[k]] = k
    src_dict, trg_dict = id2word, id2word_output

    # Load data
    # data_train = data_load("data/train_pos.txt",
    #         data=data, word2id=word2id, entity2id=entity2id,
    #         pos2id=pos2id, word2id_output=word2id_output,
    #         event_args=event_args)
    data_train = data_load("data/ace_data/train.txt",
                           data=data,
                           word2id=word2id,
                           entity2id=entity2id,
                           pos2id=pos2id,
                           word2id_output=word2id_output,
                           event_args=event_args,
                           generate=True)
    data_dev = data_load("data/ace_data/dev.txt",
                         data=data,
                         word2id=word2id,
                         entity2id=entity2id,
                         pos2id=pos2id,
                         word2id_output=word2id_output,
                         event_args=event_args,
                         generate=True)
    data_test = data_load("data/ace_data/test.txt",
                          data=data,
                          word2id=word2id,
                          entity2id=entity2id,
                          pos2id=pos2id,
                          word2id_output=word2id_output,
                          event_args=event_args,
                          generate=True)
    # data_test = data_train

    print("=====Init scores")
    scores = generate_pr(word_dict=id2word_output)
    scores.append_label(data_test)

    # Inference
    net = model.net(
        args.embedding_dim,
        args.encoder_size,
        args.decoder_size,
        word_ori_size,
        word_size,
        tag_size,
        True,
        # False,
        beam_size=args.beam_size,
        max_length=args.max_length,
        source_entity_dim=len(entity2id),
        source_pos_dim=len(pos2id),
        embedding_entity_dim=args.embedding_entity_dim,
        embedding_pos_dim=args.embedding_pos_dim,
        end_id=word2id_output["<E>"])

    # test_batch_generator = paddle.batch(
    #     paddle.reader.shuffle(
    #         paddle.dataset.wmt14.test(args.dict_size), buf_size=1000),
    #     batch_size=args.batch_size,
    #     drop_last=False)

    dev_batch_generator = paddle.batch(paddle.reader.buffered(data_dev,
                                                              size=1000),
                                       batch_size=args.batch_size,
                                       drop_last=False)
    test_batch_generator = paddle.batch(paddle.reader.buffered(data_test,
                                                               size=1000),
                                        batch_size=args.batch_size,
                                        drop_last=False)

    print("begin memory optimization ...")
    # fluid.memory_optimize(train_program)
    fluid.memory_optimize(framework.default_main_program())
    print("end memory optimization ...")

    place = core.CUDAPlace(0) if args.use_gpu else core.CPUPlace()
    exe = Executor(place)
    exe.run(framework.default_startup_program())
    # # exe = fluid.ParallelExecutor(use_cuda=args.use_gpu)
    # os.environ['CPU_NUM'] = "2"
    # exe = fluid.parallel_executor.ParallelExecutor(
    #         use_cuda=args.use_gpu, num_trainers=2,
    #         # loss_name=avg_cost.name,
    #         main_program=fluid.default_main_program())

    # LOAD Model
    model_path = os.path.join(args.save_dir, str(args.load_pass_num))
    fluid.io.load_persistables(executor=exe,
                               dirname=model_path,
                               main_program=framework.default_main_program())
    print("==Model loaded", args.save_dir)

    translation_ids = net.translation_ids
    translation_scores = net.translation_scores
    feed_order = net.feeding_list

    feed_list = [
        framework.default_main_program().global_block().var(var_name)
        for var_name in feed_order
    ]
    # print(feed_list)
    feeder = fluid.DataFeeder(feed_list, place)
    scores.reset()
    for batch_id, _data in enumerate(test_batch_generator()):
        print("=====", batch_id, len(_data))
        # The value of batch_size may vary in the last batch
        batch_size = len(_data)

        # Setup initial ids and scores lod tensor
        # init_ids_data = np.array([0 for _ in range(batch_size)], dtype='int64')
        init_ids_data = np.array(
            [word2id_output["<S>"] for _ in range(batch_size)], dtype='int64')
        init_scores_data = np.array([1. for _ in range(batch_size)],
                                    dtype='float32')
        init_ids_data = init_ids_data.reshape((batch_size, 1))
        init_scores_data = init_scores_data.reshape((batch_size, 1))
        init_recursive_seq_lens = [1] * batch_size
        init_recursive_seq_lens = [
            init_recursive_seq_lens, init_recursive_seq_lens
        ]
        init_ids = fluid.create_lod_tensor(init_ids_data,
                                           init_recursive_seq_lens, place)
        init_scores = fluid.create_lod_tensor(init_scores_data,
                                              init_recursive_seq_lens, place)
        # print(init_ids_data.shape)
        # print(init_recursive_seq_lens)
        # print(init_ids.lod())
        # print(init_scores.lod())

        # Feed dict for inference
        feed_dict = feeder.feed([x for x in _data])
        feed_dict['init_ids'] = init_ids
        feed_dict['init_scores'] = init_scores

        print("=====")
        fetch_outs = exe.run(
            framework.default_main_program(),
            feed=feed_dict,
            fetch_list=[translation_ids, translation_scores],
            # fetch_list=[translation_ids],
            return_numpy=False)
        # print(np.array(fetch_outs[0]))
        # print(np.array(fetch_outs[0]).shape)
        print("=====Update scores")
        scores.update(preds=fetch_outs[0],
                      labels=[_[-1] for _ in _data],
                      words_list=[_[0] for _ in _data],
                      for_generate=True)
        # Split the output words by lod levels
        end_id = word2id_output["<E>"]
        result = []
        paragraphs = []
        for ids in np.array(fetch_outs[0]):
            # print("##", ids.shape)
            # print("##", ids)
            new_ids = []
            new_words = []
            pre_id = -1
            for _id in ids:
                if _id == end_id or \
                        _id == pre_id:
                    break
                pre_id = _id
                new_ids.append(_id)
                if _id < args.max_length:
                    new_words.append(str(_id))
                else:
                    new_words.append(trg_dict[_id])
            result.append(new_ids)
            paragraphs.append(new_words)

        # lod_level_1 = fetch_outs[0].lod()[1]
        # token_array = np.array(fetch_outs[0])
        # result = []
        # for i in six.moves.xrange(len(lod_level_1) - 1):
        #     sentence_list = [
        #         trg_dict[token]
        #         for token in token_array[lod_level_1[i]:lod_level_1[i + 1]]
        #     ]
        #     sentence = " ".join(sentence_list[1:-1])
        #     result.append(sentence)
        # lod_level_0 = fetch_outs[0].lod()[0]
        # paragraphs = [
        #     result[lod_level_0[i]:lod_level_0[i + 1]]
        #     for i in six.moves.xrange(len(lod_level_0) - 1)
        # ]

        # target_sentence_list = [" ".join(
        #         [trg_dict[__]
        #         for __ in _[-1]])
        #         for _ in _data]
        target_sentence_list = []
        for item in _data:
            target_words = []
            for _id in item[-1]:
                if _id < args.max_length:
                    target_words.append(str(_id))
                else:
                    target_words.append(trg_dict[_id])
            target_sentence_list.append(" ".join(target_words))
        source_sentence_list = []
        source_entity_list = []
        for item in _data:
            target_words = []
            for _id in item[0]:
                target_words.append(src_dict[_id])
            source_sentence_list.append(target_words)
            entity_tag = []
            for _id in item[1]:
                entity_tag.append(id2entity[_id])
            source_entity_list.append(entity_tag)

        print("=====Print text")
        for paragraph, sentence, source , entities in \
                zip(paragraphs, target_sentence_list, \
                source_sentence_list, source_entity_list):
            print("-----")
            new_words = []
            indexes = range(len(source))
            for i, word, entity in zip(indexes, source, entities):
                new_words.append(word + "(" + str(i) + " " + entity + ")")
            print(" ".join(new_words))
            print("=Predict:", " ".join(paragraph[1:]))
            print("=Label:", sentence)

    scores.eval_show()
示例#17
0
def main(FLAGS):
    style_features_t = losses.get_style_features(FLAGS)

    # Make sure the training path exists.
    training_path = os.path.join(FLAGS.model_path, FLAGS.naming)
    if not(os.path.exists(training_path)):
        os.makedirs(training_path)

    with tf.Graph().as_default():
        with tf.Session() as sess:
            """Build Network"""
            network_fn = nets_factory.get_network_fn(
                FLAGS.loss_model,
                num_classes=1,
                is_training=False)

            image_preprocessing_fn, image_unprocessing_fn = preprocessing_factory.get_preprocessing(
                FLAGS.loss_model,
                is_training=False)
            processed_images = reader.image(FLAGS.batch_size, FLAGS.image_size, FLAGS.image_size,
                                            'train2014/', image_preprocessing_fn, epochs=FLAGS.epoch)
            generated = model.net(processed_images, training=True)
            processed_generated = [image_preprocessing_fn(image, FLAGS.image_size, FLAGS.image_size)
                                   for image in tf.unstack(generated, axis=0, num=FLAGS.batch_size)
                                   ]
            processed_generated = tf.stack(processed_generated)
            _, endpoints_dict = network_fn(tf.concat([processed_generated, processed_images], 0), spatial_squeeze=False)

            # Log the structure of loss network
            tf.logging.info('Loss network layers(You can define them in "content_layers" and "style_layers"):')
            for key in endpoints_dict:
                tf.logging.info(key)

            """Build Losses"""
            content_loss = losses.content_loss(endpoints_dict, FLAGS.content_layers)
            style_loss, style_loss_summary = losses.style_loss(endpoints_dict, style_features_t, FLAGS.style_layers)
            tv_loss = losses.total_variation_loss(generated)  # use the unprocessed image

            loss = FLAGS.style_weight * style_loss + FLAGS.content_weight * content_loss + FLAGS.tv_weight * tv_loss

            # Add Summary for visualization in tensorboard.
            """Add Summary"""
            tf.summary.scalar('losses/content_loss', content_loss)
            tf.summary.scalar('losses/style_loss', style_loss)
            tf.summary.scalar('losses/regularizer_loss', tv_loss)

            tf.summary.scalar('weighted_losses/weighted_content_loss', content_loss * FLAGS.content_weight)
            tf.summary.scalar('weighted_losses/weighted_style_loss', style_loss * FLAGS.style_weight)
            tf.summary.scalar('weighted_losses/weighted_regularizer_loss', tv_loss * FLAGS.tv_weight)
            tf.summary.scalar('total_loss', loss)

            for layer in FLAGS.style_layers:
                tf.summary.scalar('style_losses/' + layer, style_loss_summary[layer])
            tf.summary.image('generated', generated)
            # tf.image_summary('processed_generated', processed_generated)  # May be better?
            tf.summary.image('origin', tf.stack([
                image_unprocessing_fn(image) for image in tf.unstack(processed_images, axis=0, num=FLAGS.batch_size)
            ]))
            summary = tf.summary.merge_all()
            writer = tf.summary.FileWriter(training_path)

            """Prepare to Train"""
            global_step = tf.Variable(0, name="global_step", trainable=False)

            variable_to_train = []
            for variable in tf.trainable_variables():
                if not(variable.name.startswith(FLAGS.loss_model)):
                    variable_to_train.append(variable)
            train_op = tf.train.AdamOptimizer(1e-3).minimize(loss, global_step=global_step, var_list=variable_to_train)

            variables_to_restore = []
            for v in tf.global_variables():
                if not(v.name.startswith(FLAGS.loss_model)):
                    variables_to_restore.append(v)
            saver = tf.train.Saver(variables_to_restore, write_version=tf.train.SaverDef.V1)

            sess.run([tf.global_variables_initializer(), tf.local_variables_initializer()])

            # Restore variables for loss network.
            init_func = utils._get_init_fn(FLAGS)
            init_func(sess)

            # Restore variables for training model if the checkpoint file exists.
            last_file = tf.train.latest_checkpoint(training_path)
            if last_file:
                tf.logging.info('Restoring model from {}'.format(last_file))
                saver.restore(sess, last_file)

            """Start Training"""
            coord = tf.train.Coordinator()
            threads = tf.train.start_queue_runners(coord=coord)
            start_time = time.time()
            try:
                while not coord.should_stop():
                    _, loss_t, step = sess.run([train_op, loss, global_step])
                    elapsed_time = time.time() - start_time
                    start_time = time.time()
                    """logging"""
                    # print(step)
                    if step % 10 == 0:
                        tf.logging.info('step: %d,  total Loss %f, secs/step: %f' % (step, loss_t, elapsed_time))
                    """summary"""
                    if step % 25 == 0:
                        tf.logging.info('adding summary...')
                        summary_str = sess.run(summary)
                        writer.add_summary(summary_str, step)
                        writer.flush()
                    """checkpoint"""
                    if step % 1000 == 0:
                        saver.save(sess, os.path.join(training_path, 'fast-style-model.ckpt'), global_step=step)
            except tf.errors.OutOfRangeError:
                saver.save(sess, os.path.join(training_path, 'fast-style-model.ckpt-done'))
                tf.logging.info('Done training -- epoch limit reached')
            finally:
                coord.request_stop()
            coord.join(threads)
示例#18
0
  model_name = '%s_%s_%s_%s_%s_%s_%s' %(args.data_set,args.task,control_flow_p,time_p,resource_p,data_p,transition)

  log_config = {"control_flow_p":control_flow_p, "time_p":time_p, "resource_p":resource_p, "data_p":data_p, "transition":transition}

  # import models
  checkpoint_dir=args.checkpoint_dir
  
  if status == 'o':
    model_name = 'o-%s_%s_%s_%s_%s_%s_%s_%s_%s' %(args.data_set,args.task,control_flow_p,time_p,resource_p,data_p,transition,num_epochs,batch_size)
    print(model_name)
  elif status == 'p':
    model_name = 'p-%s_%s_%s_%s_%s_%s_%s_%s_%s' %(args.data_set,args.task,control_flow_p,time_p,resource_p,data_p,transition,num_epochs,batch_size)
    print(model_name)

  model = net()
  
  model.load(checkpoint_dir,model_name=model_name)

##Mozhgan

#   filename = "../sample_data/Testing/BPIC_2012_100_Testing.csv"

#   print("flag: loading data")
#   fg = FeatureGenerator()
#   df = fg.create_initial_log(filename, log_config)
#   print("done")

#   num_events = len(df)
#   num_cases = len(set(df["id"]))
def evaluate(opt):

    _, _, TEST = maybeExtract(opt.data, opt.patch_size)
    test_data, test_label = TEST[0], TEST[1]
    HEIGHT = test_data.shape[1]
    WIDTH = test_data.shape[2]
    CHANNELS = test_data.shape[3]
    N_PARALLEL_BAND = number_of_band[opt.data]
    NUM_CLASS = test_label.shape[1]

    graph = tf.Graph()
    with graph.as_default():

        # Define Model entry placeholder
        img_entry = tf.placeholder(tf.float32,
                                   shape=[None, WIDTH, HEIGHT, CHANNELS])
        img_label = tf.placeholder(tf.uint8, shape=[None, NUM_CLASS])

        # Get true class from one-hot encoded format
        image_true_class = tf.argmax(img_label, axis=1)

        # Dropout probability for the model
        prob = tf.placeholder(tf.float32)

        # Network model definition
        model = net(img_entry, prob, HEIGHT, WIDTH, CHANNELS, N_PARALLEL_BAND,
                    NUM_CLASS)

        # Cost Function
        final_layer = model['dense3']

        with tf.name_scope('loss'):
            cross_entropy = tf.nn.softmax_cross_entropy_with_logits_v2(
                logits=final_layer, labels=img_label)
            cost = tf.reduce_mean(cross_entropy)

        # Optimisation function
        with tf.name_scope('adam_optimizer'):
            optimizer = tf.train.AdamOptimizer(
                learning_rate=0.0005).minimize(cost)

        # Model Performance Measure
        with tf.name_scope('accuracy'):
            predict_class = model['predict_class_number']
            correction = tf.equal(predict_class, image_true_class)

        accuracy = tf.reduce_mean(tf.cast(correction, tf.float32))
        saver = tf.train.Saver()

        with tf.Session(graph=graph) as session:
            saver.restore(
                session,
                tf.train.latest_checkpoint('./Trained_model/' + str(opt.data) +
                                           '/'))

            def test(t_data, t_label, test_iterations=1, evalate=False):
                assert test_data.shape[0] == test_label.shape[0]
                y_predict_class = model['predict_class_number']
                # OverallAccuracy, averageAccuracy and accuracyPerClass
                overAllAcc, avgAcc, averageAccClass = [], [], []
                for _ in range(test_iterations):

                    pred_class = []
                    for t in tqdm(t_data):
                        t = np.expand_dims(t, axis=0)
                        feed_dict_test = {img_entry: t, prob: 1.0}
                        prediction = session.run(y_predict_class,
                                                 feed_dict=feed_dict_test)
                        pred_class.append(prediction)

                    true_class = np.argmax(t_label, axis=1)
                    conMatrix = confusion_matrix(true_class, pred_class)

                    # Calculate recall score across each class
                    classArray = []
                    for c in range(len(conMatrix)):
                        recallScore = conMatrix[c][c] / sum(conMatrix[c])
                        classArray += [recallScore]
                    averageAccClass.append(classArray)
                    avgAcc.append(sum(classArray) / len(classArray))
                    overAllAcc.append(accuracy_score(true_class, pred_class))

                averageAccClass = np.transpose(averageAccClass)
                meanPerClass = np.mean(averageAccClass, axis=1)

                showClassTable(meanPerClass, title='Class accuracy')
                print('Average Accuracy: ' + str(np.mean(avgAcc)))
                print('Overall Accuracy: ' + str(np.mean(overAllAcc)))

            # -- Pixel-wise classification --#

            def pixelClassification():
                input_mat, _ = maybeDownloadOrExtract(opt.data)
                input_height, input_width = input_mat.shape[
                    0], input_mat.shape[1]
                BAND = input_mat.shape[2]
                PATCH_SIZE = opt.patch_size

                MEAN_ARRAY = np.ndarray(shape=(BAND, 1))
                new_input_mat = []
                calib_val_pad = int((PATCH_SIZE - 1) / 2)
                for i in range(BAND):
                    MEAN_ARRAY[i] = np.mean(input_mat[:, :, i])
                    new_input_mat.append(
                        np.pad(input_mat[:, :, i],
                               calib_val_pad,
                               'constant',
                               constant_values=0))

                new_input_mat = np.transpose(new_input_mat, (1, 2, 0))
                input_mat = new_input_mat

                def Patch(height_index, width_index):

                    transpose_array = input_mat
                    height_slice = slice(height_index,
                                         height_index + PATCH_SIZE)
                    width_slice = slice(width_index, width_index + PATCH_SIZE)

                    patch = transpose_array[height_slice, width_slice, :]
                    mean_normalized_patch = []
                    for i in range(BAND):
                        mean_normalized_patch.append(patch[:, :, i] -
                                                     MEAN_ARRAY[i])

                    mean_normalized_patch = np.array(
                        mean_normalized_patch).astype(np.float16)
                    mean_normalized_patch = np.transpose(
                        mean_normalized_patch, (1, 2, 0))
                    return mean_normalized_patch

                labelled_img = np.ndarray(shape=(input_height, input_width))

                for i in tqdm(range(input_height - 1)):
                    for j in range(input_width - 1):
                        current_input = Patch(i, j)
                        current_input = np.expand_dims(current_input, axis=0)
                        feed_dict_test = {img_entry: current_input, prob: 1.0}
                        prediction = session.run(model['predict_class_number'],
                                                 feed_dict=feed_dict_test)
                        labelled_img[i, j] = prediction[0]

                labelled_img += 1
                labelled_img = np.pad(labelled_img, [(0, 1), (0, 0)],
                                      'constant',
                                      constant_values=(0, 0))

                print(np.min(labelled_img), np.max(labelled_img),
                      labelled_img.shape)
                GroundTruthVisualise(labelled_img, opt.data, False)

            # Test and plot
            test(test_data, test_label, test_iterations=1)
            pixelClassification()
示例#20
0
def train(data=data):
    # Load arguments
    args = parse_args()
    options = vars(args)
    print(json.dumps(options, ensure_ascii=False, indent=4))

    # if not conf.pre_train_word_embedding:
    #     word2id = data.read_dictionary("train_data/word2id.pkl")
    #     embeddings = data.random_embedding(word2id, conf.embedding_dim)
    # else:

    # Dictrionaries init
    word2id = data.read_dictionary("train_data/pre_trained_word2id.pkl")
    # embeddings = np.load("train_data/pre_trained_embeddings.npy")
    # word2id = data.read_dictionary("train_data/pre_trained_mini_word2id.pkl")
    # embeddings = np.load("train_data/pre_trained_mini_embeddings.npy")
    # word2id = data.read_dictionary("train_data/pre_trained_copy_mini_word2id.pkl")
    # embeddings = np.load("train_data/pre_trained_copy_mini_embeddings.npy")

    # word2id_output_mini = {}
    # for i, k in enumerate(word2id):
    #     word2id_output_mini[k] = i
    #     if i > 9100:
    #         break
    # word2id_output_mini["<S>"] = 1
    # word2id_output_mini["<E>"] = 2
    # word2id = word2id_output_mini

    word2id_output = word2id.copy()
    word_ori_size = len(word2id)
    # word_mini_size = len(word2id_output)
    # word_size = word_ori_size
    # word_size = word_mini_size

    word_size = 0
    tag_size = 0
    for k in tag2label:
        if tag2label[k] > tag_size:
            tag_size = tag2label[k]
        tag2label[k] += args.max_length
        if tag2label[k] > word_size:
            word_size = tag2label[k]
    # word2id_output.update(tag2label)
    word2id_output = tag2label
    word2id_output["<S>"] = word_size + 1
    word2id_output["<E>"] = word_size + 2
    word_size += 3
    tag_size += 3
    print("output size", word_size, tag_size)

    # print(type(word2id), len(word2id))
    # print(type(entity2id), len(entity2id))
    # print(type(pos2id), len(pos2id))
    # print(type(word2id_output), len(word2id_output))

    # Load data
    data_train = data_load("train_data/ace_data/train.txt",
                           data=data,
                           word2id=word2id,
                           entity2id=entity2id,
                           pos2id=pos2id,
                           word2id_output=word2id_output,
                           event_args=event_args)
    data_dev = data_load("train_data/ace_data/dev.txt",
                         data=data,
                         word2id=word2id,
                         entity2id=entity2id,
                         pos2id=pos2id,
                         word2id_output=word2id_output,
                         event_args=event_args,
                         generate=True)
    data_test = data_load("train_data/ace_data/test.txt",
                          data=data,
                          word2id=word2id,
                          entity2id=entity2id,
                          pos2id=pos2id,
                          word2id_output=word2id_output,
                          event_args=event_args,
                          generate=True)

    if args.enable_ce:
        framework.default_startup_program().random_seed = 111

    # # Training process
    # net = model.net(
    #     args.embedding_dim,
    #     args.encoder_size,
    #     args.decoder_size,
    #     word_ori_size,
    #     word_size,
    #     tag_size,
    #     False,
    #     beam_size=args.beam_size,
    #     max_length=args.max_length,
    #     source_entity_dim=len(entity2id),
    #     source_pos_dim=len(pos2id),
    #     embedding_entity_dim=args.embedding_entity_dim,
    #     embedding_pos_dim=args.embedding_pos_dim,
    #     end_id=word2id_output["<E>"])
    # avg_cost = net.avg_cost
    # feed_order = net.feeding_list
    # # Test net
    # net_test = model.net(
    #     args.embedding_dim,
    #     args.encoder_size,
    #     args.decoder_size,
    #     word_mini_size,
    #     word_size,
    #     True,
    #     beam_size=args.beam_size,
    #     max_length=args.max_length,
    #     source_entity_dim=len(entity2id),
    #     source_pos_dim=len(pos2id),
    #     embedding_entity_dim=args.embedding_entity_dim,
    #     embedding_pos_dim=args.embedding_pos_dim,
    #     end_id=word2id_output["<E>"])

    # # # clone from default main program and use it as the validation program
    # main_program = fluid.default_main_program()
    # inference_program = fluid.default_main_program().clone(for_test=True)

    # optimizer = fluid.optimizer.Adam(
    #     learning_rate=args.learning_rate,
    #     regularization=fluid.regularizer.L2DecayRegularizer(
    #         regularization_coeff=1e-5))

    # optimizer.minimize(avg_cost, no_grad_set=net.no_grad_set)

    # print("begin memory optimization ...")
    # # fluid.memory_optimize(train_program)
    # fluid.memory_optimize(main_program)
    # print("end memory optimization ...")

    # loss = avg_cost
    train_program = fluid.Program()
    train_startup = fluid.Program()
    # if "CE_MODE_X" in os.environ:
    #     train_program.random_seed = 110
    #     train_startup.random_seed = 110
    with fluid.program_guard(train_program, train_startup):
        with fluid.unique_name.guard():
            # Training process
            net = model.net(args.embedding_dim,
                            args.encoder_size,
                            args.decoder_size,
                            word_ori_size,
                            word_size,
                            tag_size,
                            False,
                            beam_size=args.beam_size,
                            max_length=args.max_length,
                            source_entity_dim=len(entity2id),
                            source_pos_dim=len(pos2id),
                            embedding_entity_dim=args.embedding_entity_dim,
                            embedding_pos_dim=args.embedding_pos_dim,
                            end_id=word2id_output["<E>"])
            loss = net.avg_cost
            feed_order = net.feeding_list
            # gradient clipping
            fluid.clip.set_gradient_clip(
                clip=fluid.clip.GradientClipByValue(max=1.0, min=-1.0))

            optimizer = fluid.optimizer.Adam(
                learning_rate=args.learning_rate,
                regularization=fluid.regularizer.L2DecayRegularizer(
                    regularization_coeff=1e-5))
            # optimizer = fluid.optimizer.Adam(
            #     learning_rate=fluid.layers.exponential_decay(
            #         learning_rate=args.learning_rate,
            #         decay_steps=400,
            #         decay_rate=0.9,
            #         staircase=True))
            optimizer.minimize(loss)
            avg_cost = loss
            # print("begin memory optimization ...")
            # fluid.memory_optimize(train_program)
            # print("end memory optimization ...")

    test_program = fluid.Program()
    test_startup = fluid.Program()
    # if "CE_MODE_X" in os.environ:
    #     test_program.random_seed = 110
    #     test_startup.random_seed = 110
    with fluid.program_guard(test_program, test_startup):
        with fluid.unique_name.guard():
            # Test net
            net_test = model.net(
                args.embedding_dim,
                args.encoder_size,
                args.decoder_size,
                word_ori_size,
                word_size,
                tag_size,
                True,
                beam_size=args.beam_size,
                max_length=args.max_length,
                source_entity_dim=len(entity2id),
                source_pos_dim=len(pos2id),
                embedding_entity_dim=args.embedding_entity_dim,
                embedding_pos_dim=args.embedding_pos_dim,
                end_id=word2id_output["<E>"])

    test_program = test_program.clone(for_test=True)
    main_program = train_program
    inference_program = test_program

    # print(type(paddle.dataset.wmt14.train(args.dict_size)))
    # print(type(paddle.reader.shuffle(
    #             data_train, buf_size=1000)))
    # print(args.enable_ce)
    # for batch_id, data in enumerate(paddle.batch(
    #         paddle.reader.shuffle(
    #             paddle.dataset.wmt14.train(args.dict_size), buf_size=1000),
    #         batch_size=args.batch_size,
    #         drop_last=False)()):
    #     print(data)
    #     break

    # Disable shuffle for Continuous Evaluation only
    if not args.enable_ce:
        train_batch_generator = paddle.batch(paddle.reader.shuffle(
            data_train, buf_size=1000),
                                             batch_size=args.batch_size,
                                             drop_last=False)
    else:
        train_batch_generator = paddle.batch(data_train,
                                             batch_size=args.batch_size,
                                             drop_last=False)
    dev_batch_generator = paddle.batch(paddle.reader.buffered(data_dev,
                                                              size=1000),
                                       batch_size=args.batch_size,
                                       drop_last=False)
    test_batch_generator = paddle.batch(paddle.reader.buffered(data_test,
                                                               size=1000),
                                        batch_size=args.batch_size,
                                        drop_last=False)
    # print (type(train_batch_generator))

    # Init model
    if args.use_gpu:
        place = fluid.CUDAPlace(0)
        dev_count = fluid.core.get_cuda_device_count()
    else:
        place = fluid.CPUPlace()
        dev_count = int(os.environ.get('CPU_NUM', multiprocessing.cpu_count()))
    print("device count %d" % dev_count)
    # print("theoretical memory usage: ")
    # print(fluid.contrib.memory_usage(
    #         program=main_program, batch_size=args.batch_size))

    # print("=====Init Main program")
    # exe = Executor(place)
    # # Init para
    # exe.run(framework.default_startup_program())
    # # exe = fluid.ParallelExecutor(use_cuda=args.use_gpu)
    # # os.environ['CPU_NUM'] = "2"
    # # exe = fluid.parallel_executor.ParallelExecutor(
    # #         use_cuda=args.use_gpu, num_trainers=2,
    # #         loss_name=avg_cost.name,
    # #         main_program=fluid.default_main_program())

    exe = fluid.Executor(place)
    print("=====Init train program")
    exe.run(train_startup)
    print("=====Init test program")
    exe.run(test_startup)

    # print("=====Init train exe")
    # train_exe = fluid.ParallelExecutor(
    #     use_cuda=args.use_gpu, loss_name=loss.name, main_program=train_program)

    # print("=====Init test exe")
    # test_exe = fluid.ParallelExecutor(
    #     use_cuda=args.use_gpu,
    #     main_program=test_program,
    #     share_vars_from=train_exe)

    ## Set word emb
    #print("=====Set word embedding")
    #embeddings = embeddings.astype("float32")
    #word_emb_param = fluid.global_scope().find_var(
    #    "emb").get_tensor()
    #word_emb_param.set(embeddings, place)

    print("=====Init Feeder")
    feed_list = [
        main_program.global_block().var(var_name) for var_name in feed_order
    ]
    feed_list_test = [
        inference_program.global_block().var(var_name)
        for var_name in net_test.feeding_list
    ]
    # print(feed_list)
    feeder = fluid.DataFeeder(feed_list, place)
    feeder_test = fluid.DataFeeder(feed_list_test, place)

    # return

    def validation(generater, test_scores):
        # Use test set as validation each pass
        test_scores.reset()
        total_loss = 0.0
        count = 0
        # val_feed_list = [
        #     inference_program.global_block().var(var_name)
        #     for var_name in net_test.feeding_list
        # ]
        # val_feeder = fluid.DataFeeder(val_feed_list, place)

        for batch_id, data in enumerate(generater()):
            # The value of batch_size may vary in the last batch
            batch_size = len(data)

            # Setup initial ids and scores lod tensor
            init_ids_data = np.array(
                [word2id_output["<S>"] for _ in range(batch_size)],
                dtype='int64')
            init_scores_data = np.array([1. for _ in range(batch_size)],
                                        dtype='float32')
            init_ids_data = init_ids_data.reshape((batch_size, 1))
            init_scores_data = init_scores_data.reshape((batch_size, 1))
            init_recursive_seq_lens = [1] * batch_size
            init_recursive_seq_lens = [
                init_recursive_seq_lens, init_recursive_seq_lens
            ]
            init_ids = fluid.create_lod_tensor(init_ids_data,
                                               init_recursive_seq_lens, place)
            init_scores = fluid.create_lod_tensor(init_scores_data,
                                                  init_recursive_seq_lens,
                                                  place)

            # Feed dict for inference
            # feed_dict = feeder.feed([[x[0]] for x in data])
            feed_dict = feeder_test.feed(data)
            feed_dict['init_ids'] = init_ids
            feed_dict['init_scores'] = init_scores

            val_fetch_outs = exe.run(
                inference_program,
                # test_program(),
                feed=feed_dict,
                fetch_list=[net_test.translation_ids],
                return_numpy=False)
            # test_scores.update(
            #         preds=val_fetch_outs[0],
            #         labels=[_[-1] for _ in data])
            # print("=====Update scores")
            test_scores.update(preds=val_fetch_outs[0],
                               labels=[_[-1] for _ in data],
                               words_list=[_[0] for _ in data],
                               for_generate=True)

            # val_fetch_outs = exe.run(inference_program,
            #                          feed=val_feeder.feed(data),
            #                          fetch_list=[avg_cost, net.label],
            #                          return_numpy=False)
            # test_scores.update(
            #         preds=val_fetch_outs[1],
            #         labels=[_[-1] for _ in data],
            #         words_list=[_[0] for _ in data])

            total_loss += 1.0
            count += 1
        # if batch_id > 0:
        #     break
        values = test_scores.eval()
        test_scores.eval_show()

        return total_loss / count, values

    print("=====Init scores")
    id2word_output = {}
    for k in word2id_output:
        id2word_output[word2id_output[k]] = k
    scores_train = generate_pr(word_dict=id2word_output)
    scores_train.append_label(data_train)
    scores_test = generate_pr(word_dict=id2word_output)
    scores_test.append_label(data_test)
    scores_dev = generate_pr(word_dict=id2word_output)
    scores_dev.append_label(data_dev)
    max_tri_f1 = 0.0
    max_tri_pass = -1.0
    max_arg_f1 = 0.0
    max_arg_pass = -1.0
    print("=====Start training")
    for pass_id in range(1, args.pass_num + 1):
        scores_train.reset()
        pass_start_time = time.time()
        words_seen = 0
        for batch_id, _data in enumerate(train_batch_generator()):
            batch_size = len(_data)
            words_seen += len(_data) * 2
            # print(_data)
            # print(len(_data))
            # print(sum([len(_[0]) for _ in _data]))

            # # Setup initial ids and scores lod tensor
            # init_ids_data = np.array([0 for _ in range(batch_size)], dtype='int64')
            # init_scores_data = np.array(
            # [1. for _ in range(batch_size)], dtype='float32')
            # init_ids_data = init_ids_data.reshape((batch_size, 1))
            # init_scores_data = init_scores_data.reshape((batch_size, 1))
            # init_recursive_seq_lens = [1] * batch_size
            # init_recursive_seq_lens = [
            # init_recursive_seq_lens, init_recursive_seq_lens
            # ]
            # init_ids = fluid.create_lod_tensor(init_ids_data,
            # init_recursive_seq_lens, place)
            # init_scores = fluid.create_lod_tensor(init_scores_data,
            # init_recursive_seq_lens, place)

            # # Feed dict for inference
            # # feed_dict = feeder.feed([[x[0]] for x in _data])
            # feed_dict = feeder.feed(_data)
            # feed_dict['init_ids'] = init_ids
            # feed_dict['init_scores'] = init_scores

            # avg_cost_train, preds = exe.run(
            # framework.default_main_program(),
            # # test_program(),
            # feed=feed_dict,
            # fetch_list=[avg_cost, net.predict],
            # return_numpy=False)

            avg_cost_train, preds = exe.run(
                main_program,
                # train_program(),
                feed=feeder.feed(_data),
                fetch_list=[avg_cost, net.label],
                return_numpy=False)
            # print(np.array(labels).shape)
            # print(np.array(preds).tolist())
            # print([_[-1] for _ in _data])
            #print([_[0] for _ in _data])
            avg_cost_train = np.array(avg_cost_train)
            if batch_id % 10 == 0:
                print('pass_id=%d, batch_id=%d, train_loss: %f' %
                      (pass_id, batch_id, avg_cost_train))

            scores_train.update(preds=preds,
                                labels=[_[-1] for _ in _data],
                                words_list=[_[0] for _ in _data])
        # This is for continuous evaluation only
        # if args.enable_ce and batch_id >= 100:
        # if batch_id > 0:
        #     break
        scores_train.eval_show()

        pass_end_time = time.time()
        new_max_dev = False
def main(argv=None):
    run_id = FLAGS.NAME if FLAGS.NAME else str(uuid.uuid4())
    model_path = '%s/%s' % (FLAGS.MODEL_PATH, run_id)
    if not os.path.exists(model_path):
        os.makedirs(model_path)
    summary_path = '%s/%s' % (FLAGS.SUMMARY_PATH, run_id)
    if not os.path.exists(summary_path):
        os.makedirs(summary_path)

    style_paths = FLAGS.STYLE_IMAGES.split(',')
    style_layers = FLAGS.STYLE_LAYERS.split(',')
    content_layers = FLAGS.CONTENT_LAYERS.split(',')

    style_features_t = get_style_features(style_paths, style_layers)

    images = reader.image(FLAGS.BATCH_SIZE, FLAGS.IMAGE_SIZE, FLAGS.TRAIN_IMAGES_PATH)
    generated = model.net(images - reader.mean_pixel, training=True)

    # Put both generated and training images in same batch through VGG net for efficiency
    net, _ = vgg.net(FLAGS.VGG_PATH, tf.concat(0, [generated, images]) - reader.mean_pixel)

    content_loss = 0
    for layer in content_layers:
        generated_images, content_images = tf.split(0, 2, net[layer])
        size = tf.size(generated_images)
        shape = tf.shape(generated_images)
        width = shape[1]
        height = shape[2]
        num_filters = shape[3]
        content_loss += tf.nn.l2_loss(generated_images - content_images) / tf.to_float(size)
    content_loss = content_loss

    style_loss = 0
    for style_grams, layer in zip(style_features_t, style_layers):
        generated_images, _ = tf.split(0, 2, net[layer])
        size = tf.size(generated_images)
        for style_gram in style_grams:
            style_loss += tf.nn.l2_loss(gram(generated_images) - style_gram) / tf.to_float(size)
    style_loss = style_loss / len(style_paths)

    tv_loss = total_variation_loss(generated)

    loss = FLAGS.STYLE_WEIGHT * style_loss + FLAGS.CONTENT_WEIGHT * content_loss + FLAGS.TV_WEIGHT * tv_loss

    global_step = tf.Variable(0, name="global_step", trainable=False)
    train_op = tf.train.AdamOptimizer(1e-3).minimize(loss, global_step=global_step)

    # Statistics
    with tf.name_scope('losses'):
        tf.scalar_summary('content loss', content_loss)
        tf.scalar_summary('style loss', style_loss)
        tf.scalar_summary('regularizer loss', tv_loss)
    with tf.name_scope('weighted_losses'):
        tf.scalar_summary('weighted content loss', content_loss * FLAGS.CONTENT_WEIGHT)
        tf.scalar_summary('weighted style loss', style_loss * FLAGS.STYLE_WEIGHT)
        tf.scalar_summary('weighted regularizer loss', tv_loss * FLAGS.TV_WEIGHT)
        tf.scalar_summary('total loss', loss)
    tf.image_summary('original', images)
    tf.image_summary('generated', generated)

    summary = tf.merge_all_summaries()

    with tf.Session() as sess:
        writer = tf.train.SummaryWriter(summary_path, sess.graph)

        saver = tf.train.Saver(tf.all_variables())
        file = tf.train.latest_checkpoint(model_path)
        sess.run([tf.initialize_all_variables(), tf.initialize_local_variables()])
        if file:
            print('Restoring model from {}'.format(file))
            saver.restore(sess, file)

        coord = tf.train.Coordinator()
        threads = tf.train.start_queue_runners(coord=coord)
        start_time = time.time()
        try:
            while not coord.should_stop():
                _, loss_t, step = sess.run([train_op, loss, global_step])
                elapsed_time = time.time() - start_time
                start_time = time.time()
                if step % 100 == 0:
                    print(step, loss_t, elapsed_time)
                    summary_str = sess.run(summary)
                    writer.add_summary(summary_str, step)
                if step % 10000 == 0:
                    saver.save(sess, model_path + '/fast-style-model', global_step=step)
        except tf.errors.OutOfRangeError:
            saver.save(sess, model_path + '/fast-style-model-done')
            print('Done training -- epoch limit reached')
        finally:
            coord.request_stop()
        coord.join(threads)
示例#22
0
import torch.nn as nn
import numpy as np
import data
import model as model
from tqdm import tqdm
import data
test1path = 'test1\\'
trainpath = 'train\\'

filename = os.listdir(test1path)
words = os.listdir(trainpath)  # 按时间排序 从早到晚
words = np.array(words)
testnumber = len(filename)
category_number = len(words)

net = model.net()
if torch.cuda.is_available():
    net.cuda()
net.eval()

if __name__ == '__main__':
    checkpoint = model.load_checkpoint()
    net.load_state_dict(checkpoint['state_dict'])

    testdatas = data.loadtestdata()
    testdatas.astype(np.float)
    n = 0
    N = 10000
    batch_size = 8
    pre = np.array([])
    batch_site = []
示例#23
0
def main(argv=None):
    if FLAGS.CONTENT_IMAGES_PATH:
        content_images = reader.image(FLAGS.BATCH_SIZE,
                                      FLAGS.IMAGE_SIZE,
                                      FLAGS.CONTENT_IMAGES_PATH,
                                      epochs=1,
                                      shuffle=False,
                                      crop=False)
        generated_images = model.net(content_images / 255.)

        output_format = tf.saturate_cast(generated_images + reader.mean_pixel,
                                         tf.uint8)
        with tf.Session() as sess:
            file = tf.train.latest_checkpoint(FLAGS.MODEL_PATH)
            if not file:
                print('Could not find trained model in {}'.format(
                    FLAGS.MODEL_PATH))
                return
            print('Using model from {}'.format(file))
            saver = tf.train.Saver()
            saver.restore(sess, file)
            coord = tf.train.Coordinator()
            threads = tf.train.start_queue_runners(coord=coord)
            i = 0
            start_time = time.time()
            try:
                while not coord.should_stop():
                    print(i)
                    images_t = sess.run(output_format)
                    elapsed = time.time() - start_time
                    start_time = time.time()
                    print('Time for one batch: {}'.format(elapsed))

                    for raw_image in images_t:
                        i += 1
                        misc.imsave('out{0:04d}.png'.format(i), raw_image)
            except tf.errors.OutOfRangeError:
                print('Done training -- epoch limit reached')
            finally:
                coord.request_stop()

            coord.join(threads)
        return

    if not os.path.exists(FLAGS.MODEL_PATH):
        os.makedirs(FLAGS.MODEL_PATH)

    style_paths = FLAGS.STYLE_IMAGES.split(',')
    style_layers = FLAGS.STYLE_LAYERS.split(',')
    content_layers = FLAGS.CONTENT_LAYERS.split(',')

    style_features_t = get_style_features(style_paths, style_layers)

    images = reader.image(FLAGS.BATCH_SIZE, FLAGS.IMAGE_SIZE,
                          FLAGS.TRAIN_IMAGES_PATH)
    generated = model.net(images / 255.)

    net, _ = vgg.net(FLAGS.VGG_PATH, tf.concat(0, [generated, images]))

    content_loss = 0
    for layer in content_layers:
        generated_images, content_images = tf.split(0, 2, net[layer])
        size = tf.size(generated_images)
        content_loss += tf.nn.l2_loss(generated_images -
                                      content_images) / tf.to_float(size)
    content_loss = content_loss / len(content_layers)

    style_loss = 0
    for style_gram, layer in zip(style_features_t, style_layers):
        generated_images, _ = tf.split(0, 2, net[layer])
        size = tf.size(generated_images)
        for style_image in style_gram:
            style_loss += tf.nn.l2_loss(
                tf.reduce_sum(gram(generated_images) - style_image,
                              0)) / tf.to_float(size)
    style_loss = style_loss / len(style_layers)

    loss = FLAGS.STYLE_WEIGHT * style_loss + FLAGS.CONTENT_WEIGHT * content_loss + FLAGS.TV_WEIGHT * total_variation_loss(
        generated)

    global_step = tf.Variable(0, name="global_step", trainable=False)
    train_op = tf.train.AdamOptimizer(1e-3).minimize(loss,
                                                     global_step=global_step)

    output_format = tf.saturate_cast(
        tf.concat(0, [generated, images]) + reader.mean_pixel, tf.uint8)

    with tf.Session() as sess:
        saver = tf.train.Saver(tf.all_variables())
        file = tf.train.latest_checkpoint(FLAGS.MODEL_PATH)
        if file:
            print('Restoring model from {}'.format(file))
            saver.restore(sess, file)
        else:
            print('New model initilized')
            sess.run(tf.initialize_all_variables())

        coord = tf.train.Coordinator()
        threads = tf.train.start_queue_runners(coord=coord)
        start_time = time.time()
        try:
            while not coord.should_stop():
                _, loss_t, step = sess.run([train_op, loss, global_step])
                elapsed_time = time.time() - start_time
                start_time = time.time()
                if step % 100 == 0:
                    print(step, loss_t, elapsed_time)
                    output_t = sess.run(output_format)
                    for i, raw_image in enumerate(output_t):
                        misc.imsave('out{}.png'.format(i), raw_image)
                if step % 10000 == 0:
                    saver.save(sess,
                               FLAGS.MODEL_PATH + '/fast-style-model',
                               global_step=step)
        except tf.errors.OutOfRangeError:
            print('Done training -- epoch limit reached')
        finally:
            coord.request_stop()

        coord.join(threads)
示例#24
0
import checkpoint as cp
import match_data
import model as mdl

n_epoch = 1000

x, y, xt, yt = match_data.get_train_data()
x = torch.FloatTensor(x)
y = torch.LongTensor(y)
xt = torch.FloatTensor(xt)
yt = torch.LongTensor(yt)
x, y = Variable(x), Variable(y)
xt, yt = Variable(xt), Variable(yt)

net = mdl.net()
#optimizer = torch.optim.SGD(net.parameters(), lr=0.01, weight_decay=0.002)
optimizer = torch.optim.Adam(net.parameters(),
                             lr=1e-3,
                             betas=(0.9, 0.999),
                             weight_decay=0)
loss_func = torch.nn.CrossEntropyLoss()


def now():
    return datetime.now().strftime('%c')


def train_one_epoch():
    net.train()
def main(FLAGS):
    # 得到风格特征
    style_features_t = losses.get_style_features(FLAGS)

    # Make sure the training path exists.
    training_path = os.path.join(FLAGS.model_path, FLAGS.naming)
    if not (os.path.exists(training_path)):
        os.makedirs(training_path)

    with tf.Graph().as_default():
        with tf.Session() as sess:
            """Build Network"""
            # 构造vgg网络,按照FLAGS.loss_model中的网络名字,可以在/nets/nets_factory.py 中的networks_map找到对应
            network_fn = nets_factory.get_network_fn(FLAGS.loss_model,
                                                     num_classes=1,
                                                     is_training=False)
            # 根据不同网络做不同的预处理
            image_preprocessing_fn, image_unprocessing_fn = preprocessing_factory.get_preprocessing(
                FLAGS.loss_model, is_training=False)
            # 读取一个批次的数据,并且预处理
            # 这里的数据你可以不用coco,可以直接给一个包含很多图片的文件夹即可
            # 因为coco过于大
            processed_images = reader.image(FLAGS.batch_size,
                                            FLAGS.image_height,
                                            FLAGS.image_width,
                                            'F:/CASIA/train_frame/real/',
                                            image_preprocessing_fn,
                                            epochs=FLAGS.epoch)
            # 通过生成网络,生成图片,相当于y^
            generated = model.net(processed_images, training=True)
            # 因为一会要把生成图片喂入到后面vgg进行计算两个损失,所以要先进行预处理
            processed_generated = [
                image_preprocessing_fn(image, FLAGS.image_height,
                                       FLAGS.image_width) for image in
                tf.unstack(generated, axis=0, num=FLAGS.batch_size)
            ]
            # 因为上面是list格式,所以用tf.stack堆叠成tensor
            processed_generated = tf.stack(processed_generated)
            # 按照batch那一个维度,拼起来,比如原来两个是[batch_size,h,w,c],concat后变为[2*batch_size,h,w,c]
            # 这样一次前向传播把y^ 和y_c的特征都计算出来了
            _, endpoints_dict = network_fn(tf.concat(
                [processed_generated, processed_images], 0),
                                           spatial_squeeze=False)

            # Log the structure of loss network
            tf.logging.info(
                'Loss network layers(You can define them in "content_layers" and "style_layers"):'
            )
            for key in endpoints_dict:
                tf.logging.info(key)
            """Build Losses"""
            # 计算三个损失
            content_loss = losses.content_loss(endpoints_dict,
                                               FLAGS.content_layers)
            style_loss, style_loss_summary = losses.style_loss(
                endpoints_dict, style_features_t, FLAGS.style_layers)
            tv_loss = losses.total_variation_loss(
                generated)  # use the unprocessed image

            loss = FLAGS.style_weight * style_loss + FLAGS.content_weight * content_loss + FLAGS.tv_weight * tv_loss

            # Add Summary for visualization in tensorboard.
            """Add Summary"""
            # 为了tensorboard,可以忽略
            tf.summary.scalar('losses/content_loss', content_loss)
            tf.summary.scalar('losses/style_loss', style_loss)
            tf.summary.scalar('losses/regularizer_loss', tv_loss)

            tf.summary.scalar('weighted_losses/weighted_content_loss',
                              content_loss * FLAGS.content_weight)
            tf.summary.scalar('weighted_losses/weighted_style_loss',
                              style_loss * FLAGS.style_weight)
            tf.summary.scalar('weighted_losses/weighted_regularizer_loss',
                              tv_loss * FLAGS.tv_weight)
            tf.summary.scalar('total_loss', loss)

            for layer in FLAGS.style_layers:
                tf.summary.scalar('style_losses/' + layer,
                                  style_loss_summary[layer])
            tf.summary.image('generated', generated)
            # tf.image_summary('processed_generated', processed_generated)  # May be better?
            tf.summary.image(
                'origin',
                tf.stack([
                    image_unprocessing_fn(image) for image in tf.unstack(
                        processed_images, axis=0, num=FLAGS.batch_size)
                ]))
            summary = tf.summary.merge_all()
            writer = tf.summary.FileWriter(training_path)
            """Prepare to Train"""
            # 步数
            global_step = tf.Variable(0, name="global_step", trainable=False)

            variable_to_train = []
            for variable in tf.trainable_variables():
                # 把非vgg网络里面的可训练变量加入variable_to_train
                if not (variable.name.startswith(FLAGS.loss_model)):
                    variable_to_train.append(variable)
            # 注意var_list
            train_op = tf.train.AdamOptimizer(1e-3).minimize(
                loss, global_step=global_step, var_list=variable_to_train)

            variables_to_restore = []
            for v in tf.global_variables():
                # 把非vgg中的可存储变量加入variables_to_restore
                if not (v.name.startswith(FLAGS.loss_model)):
                    variables_to_restore.append(v)

            # 注意variables_to_restore
            saver = tf.train.Saver(variables_to_restore,
                                   write_version=tf.train.SaverDef.V1)

            sess.run([
                tf.global_variables_initializer(),
                tf.local_variables_initializer()
            ])

            # Restore variables for loss network.
            # slim的,可以根据FLAGS里面配置把网络参数加载到sess这个会话里面
            init_func = utils._get_init_fn(FLAGS)
            init_func(sess)

            # Restore variables for training model if the checkpoint file exists.
            last_file = tf.train.latest_checkpoint(training_path)
            if last_file:
                tf.logging.info('Restoring model from {}'.format(last_file))
                saver.restore(sess, last_file)
            """Start Training"""
            coord = tf.train.Coordinator()
            threads = tf.train.start_queue_runners(coord=coord)
            start_time = time.time()
            try:
                while not coord.should_stop():
                    _, loss_t, step = sess.run([train_op, loss, global_step])
                    elapsed_time = time.time() - start_time
                    start_time = time.time()
                    """logging"""
                    print(step)
                    if step % 10 == 0:
                        tf.logging.info(
                            'step: %d,  total Loss %f, secs/step: %f' %
                            (step, loss_t, elapsed_time))
                    """summary"""
                    if step % 25 == 0:
                        tf.logging.info('adding summary...')
                        summary_str = sess.run(summary)
                        writer.add_summary(summary_str, step)
                        writer.flush()
                    """checkpoint"""
                    if step % 1000 == 0:
                        saver.save(sess,
                                   os.path.join(training_path,
                                                'fast-style-model.ckpt'),
                                   global_step=step)
            except tf.errors.OutOfRangeError:
                saver.save(
                    sess,
                    os.path.join(training_path, 'fast-style-model.ckpt-done'))
                tf.logging.info('Done training -- epoch limit reached')
            finally:
                coord.request_stop()
            coord.join(threads)
示例#26
0
def main(_):
    img_read = cv2.imread(FLAGS.image_file)
    shape = img_read.shape
    height = shape[0]
    width = shape[1]

    with tf.Graph().as_default():
        with tf.Session().as_default() as sess:
            X = tf.placeholder(tf.uint8, shape=[None, None, 3], name='input')

            # Read image data.
            image_preprocessing_fn, _ = preprocessing_factory.get_preprocessing(
                FLAGS.loss_model, is_training=False)
            image0 = reader.get_image_from_cam(X, height, width,
                                               image_preprocessing_fn)

            #X = tf.placeholder(tf.float32, shape=[None, None, 3], name='input')

            # Add batch dimension
            image = tf.expand_dims(image0, 0)
            #image = tf.expand_dims(image, 0)

            generated = model.net(image, training=False)
            generated = tf.cast(generated, tf.uint8)

            # Remove batch dimension
            generated = tf.squeeze(generated, [0])

            # Restore model variables.
            saver = tf.train.Saver(tf.global_variables(),
                                   write_version=tf.train.SaverDef.V1)
            sess.run([
                tf.global_variables_initializer(),
                tf.local_variables_initializer()
            ])
            # Use absolute path
            FLAGS.model_file = os.path.abspath(FLAGS.model_file)
            saver.restore(sess, FLAGS.model_file)

            # Make sure 'generated' directory exists.
            generated_file = 'generated/res.jpg'
            if os.path.exists('generated') is False:
                os.makedirs('generated')

            # Generate and write image data to file.
            with open(generated_file, 'wb') as img:
                start_time = time.time()
                img.write(
                    sess.run(tf.image.encode_jpeg(generated),
                             feed_dict={X: img_read}))
                end_time = time.time()
                # Generated the protobuf file.
                output_graph_def = tf.graph_util.convert_variables_to_constants(
                    sess, sess.graph_def, output_node_names=['output'])
                with tf.gfile.FastGFile('models/wave_small1.pb',
                                        mode='wb') as f:
                    f.write(output_graph_def.SerializeToString())

                tf.logging.info('Convert done!')
                tf.logging.info('Elapsed time: %fs' % (end_time - start_time))

                tf.logging.info('Done. Please check %s.' % generated_file)
示例#27
0
def fast_style():
    batch_size = 4
    num_epochs = 1
    style_layers = FLAGS.STYLE_LAYERS.split(',')
    content_layers = FLAGS.CONTENT_LAYERS.split(',')
    with h5py.File(''.join(['datasets/coco-256.h5']), 'r') as hf:
        content_images = hf['images'].value
        content_names = hf['filenames'].value

    total_batch = int(np.floor(len(content_images) / (batch_size)))

    total_loss = 0
    total_content = 0
    total_style = 0
    total_variation = 0

    style_names = [
        join('styles', f) for f in listdir('styles')
        if isfile(join('styles', f)) & f.lower().endswith('jpg')
    ]

    style_holder = tf.placeholder(shape=[1, None, None, 3],
                                  dtype=tf.float32)  # Real images
    content_holder = tf.placeholder(shape=[batch_size, 256, 256, 3],
                                    dtype=tf.float32)  # Random vector

    # generated = neural_model.net(content_holder)
    generated = [model.net(content_holder, training=True)]

    style_net, _ = vgg.net(FLAGS.VGG_PATH, style_holder)
    content_net, _ = vgg.net(FLAGS.VGG_PATH, content_holder)

    for i in range(len(generated)):
        generated_net, _ = vgg.net(FLAGS.VGG_PATH, generated[i])

        content_loss = 0
        for layer in content_layers:
            content_vgg = content_net[layer]
            generated_vgg = generated_net[layer]
            size = tf.size(generated_vgg)
            content_loss += tf.nn.l2_loss(generated_vgg -
                                          content_vgg) / tf.to_float(size)
        content_loss = content_loss / len(content_layers)

        style_loss = 0
        for layer in style_layers:
            generated_vgg = generated_net[layer]
            style_vgg = style_net[layer]
            # size = tf.size(style_vgg)
            size = tf.square(style_vgg.get_shape().as_list()[3]) * batch_size
            # for style_batch in style_gram:
            style_loss += tf.nn.l2_loss(gram(generated_vgg) -
                                        gram(style_vgg)) / tf.to_float(size)
        style_loss = style_loss / len(style_layers)

        total_content += content_loss
        total_style += style_loss
        total_variation += total_variation_loss(generated[i])

    total_loss = FLAGS.STYLE_WEIGHT * total_style + FLAGS.CONTENT_WEIGHT * total_content + FLAGS.TV_WEIGHT * total_variation
    output_format = tf.saturate_cast(
        tf.concat(0, [generated[-1], content_holder]) + mean_pixel, tf.uint8)

    tvars = tf.trainable_variables()
    train_op = tf.train.AdamOptimizer(1e-3)
    grads = train_op.compute_gradients(total_loss, tvars)
    update = train_op.apply_gradients(grads)

    saver = tf.train.Saver(tf.all_variables())
    sess = tf.Session()
    sess.run(tf.initialize_all_variables())
    sess.run(tf.initialize_local_variables())

    for i in xrange(len(style_names)):
        print 'style: ' + style_names[i]
        # style_image = (scipy.misc.imread(style_names[i], mode='RGB') / 255.0 - 0.5) * 2.0
        style_image = scipy.misc.imread(style_names[i],
                                        mode='RGB') - mean_pixel
        style_image = np.expand_dims(style_image, 0)
        epoch = 0
        while epoch < num_epochs:
            content_iter = data_iterator(content_images, content_names,
                                         batch_size)
            for j in tqdm.tqdm(xrange(total_batch)):
                content_image, content_name = content_iter.next()
                content_image = np.reshape(
                    content_image, [batch_size, 256, 256, 3]) - mean_pixel
                _, loss_t, loss_s, loss_c = sess.run(
                    [update, total_loss, total_style, total_content],
                    feed_dict={
                        content_holder: content_image,
                        style_holder: style_image
                    })

                if j % 100 == 0:
                    print 'epoch: ' + str(epoch) + ' loss: ' + str(
                        loss_t) + ' loss_s: ' + str(
                            loss_s) + ' loss_c: ' + str(loss_c)
                    output_t = sess.run(
                        output_format,
                        feed_dict={content_holder: content_image})
                    for j, raw_image in enumerate(output_t):
                        scipy.misc.imsave('test/out%s-%s.png' % (epoch, j + 1),
                                          raw_image)
            if epoch % 1 == 0:
                if not os.path.exists(FLAGS.MODEL_DIR):
                    os.makedirs(FLAGS.MODEL_DIR)
                saver.save(
                    sess, FLAGS.MODEL_DIR + '/model-' +
                    str(style_names[i][7:-4]) + '.ckpt')
                print "Saved Model"
            epoch += 1
示例#28
0
def general_nfold_cv_test(XD, XT, Y, label_row_inds, label_col_inds,
                          prfmeasure, FLAGS, labeled_sets, val_sets, get_rm2,
                          best_param_list, i):
    param1value = best_param_list[0]
    param2value = best_param_list[1]
    param3value = best_param_list[2]
    lamda = best_param_list[3]
    batchsz = FLAGS.batch_size  # 256

    logging("---Parameter Search-----", FLAGS)

    w = len(val_sets)

    all_predictions = [0 for x in range(w)]
    all_losses = [0 for x in range(w)]
    all_auc = [0 for x in range(w)]
    all_aupr = [0 for x in range(w)]
    all_preaffinities = []
    all_affinities = []
    for foldind in range(len(val_sets)):
        valinds = val_sets[foldind]
        labeledinds = labeled_sets[foldind]

        trrows = label_row_inds[labeledinds]
        trcols = label_col_inds[labeledinds]

        train_dataset = prepare_interaction_pairs(XD, XT, Y, trrows, trcols)

        terows = label_row_inds[valinds]
        tecols = label_col_inds[valinds]

        test_dataset = prepare_interaction_pairs(XD, XT, Y, terows, tecols)

        train_loader = DataLoader(dataset=train_dataset,
                                  batch_size=batchsz,
                                  shuffle=True)
        test_loader = DataLoader(dataset=test_dataset, batch_size=batchsz)
        model = net(FLAGS, param1value, param2value, param3value).cuda()
        model.apply(weights_init)
        rperf_list = []
        for epochind in range(FLAGS.num_epoch):
            model = train(train_loader, model, FLAGS, param1value, param2value,
                          param3value, lamda)
            if (epochind + 1) % 2 == 0:
                rperf, loss, rm2, auc = test(model, test_loader, FLAGS,
                                             param1value, param2value,
                                             param3value, lamda)
                rperf_list.append(rperf)
                print(
                    'test: epoch:{},p1:{},p2:{},p3:{},loss:{:.5f},rperf:{:.5f}, rm2:{:.5f}'
                    .format(epochind, param1value, param2value, param3value,
                            loss, rperf, rm2))

                if rperf >= max(rperf_list):
                    torch.save(model, 'checkpoint.pth')
                if rperf < max(rperf_list) - 0.1:
                    break
        loss_func = nn.MSELoss()
        affinities = []
        pre_affinities = []
        model = torch.load('checkpoint.pth')
        model.eval()
        for drug_SMILES, target_protein, affinity in test_loader:
            pre_affinity, _, _, _, _, _, _, _, _ = model(
                drug_SMILES, target_protein, FLAGS, param1value, param2value,
                param3value)
            pre_affinities += pre_affinity.cpu().detach().numpy().tolist()
            affinities += affinity.cpu().detach().numpy().tolist()

        pre_affinities = np.array(pre_affinities)
        affinities = np.array(affinities)
        if 'davis' in FLAGS.dataset_path:
            pre_label = pre_affinities
            label = np.int32(affinities > 7.0)
            auc = roc_auc_score(label, pre_label)
            aupr = get_aupr(label, pre_label)
        if 'kiba' in FLAGS.dataset_path:
            pre_label = pre_affinities
            label = np.int32(affinities > 12.1)
            auc = roc_auc_score(label, pre_label)
            aupr = get_aupr(label, pre_label)
        rperf = prfmeasure(affinities, pre_affinities)
        rm2 = get_rm2(affinities, pre_affinities)
        loss = loss_func(torch.Tensor(pre_affinities),
                         torch.Tensor(affinities))
        print('best: p1:{},p2:{},p3:{},loss:{:.5f},rperf:{:.5f}, rm2:{:.5f}'.
              format(param1value, param2value, param3value, loss, rperf, rm2))

        logging(
            "best: P1 = %d,  P2 = %d, P3 = %d, Fold = %d, CI-i = %f, MSE = %f, auc = %f, aupr = %f"
            % (param1value, param2value, param3value, foldind, rperf, loss,
               auc, aupr), FLAGS)

        all_predictions[
            foldind] = rperf  # TODO FOR EACH VAL SET allpredictions[pointer][foldind]
        all_losses[foldind] = loss
        all_auc[foldind] = auc
        all_aupr[foldind] = aupr
        all_affinities.append(affinities)
        all_preaffinities.append(pre_affinities)
    # save affinities and preaffinites for further analysis
    np.savetxt("./result/iter" + str(i) + "affinities.txt",
               np.array(all_affinities))
    np.savetxt("./result/iter" + str(i) + "preaffinities.txt",
               np.array(all_preaffinities))

    best_param_list = [param1value, param2value, param3value, lamda]
    best_perf = np.mean(all_predictions)

    return best_param_list, best_perf, all_predictions, all_losses, all_auc, all_aupr
示例#29
0
def gen_from_directory():
    """ transfer images from a directory. """
    im_name = tf.placeholder(dtype=tf.string)
    im_format = tf.placeholder(dtype=tf.string)
    content_images = reader.get_image_frame(im_name, im_format,
                                            FLAGS.image_size)
    images = tf.stack([content_images])
    generated_images = model.net(images / 255., if_train=False)

    ims = os.listdir(FLAGS.content)
    im_nums = len(ims)
    output_format = tf.saturate_cast(generated_images + reader.mean_pixel,
                                     tf.uint8)

    # Ouput path
    model_path = os.path.join('models',
                              FLAGS.model_name + utils.get_model_suffix())
    ### model_p = model_p if not model_p.endswith("/") else model_p[:-1]
    ### model_p = os.path.split(model_p)
    output_path = os.path.join("output",
                               FLAGS.model_name + utils.get_model_suffix())

    if not os.path.exists(output_path):
        os.makedirs(output_path)

    with tf.Session() as sess:
        file_ = tf.train.latest_checkpoint(model_path)
        if not file_:
            print('Could not find trained model in {}'.format(FLAGS.model))
            return
        print('Using model from {}'.format(file_))

        # Get trained step
        index = file_.rfind("-")
        trained_step = file_[index:]

        saver = tf.train.Saver()
        saver.restore(sess, file_)

        print("Transfer image:")
        start_time = time.time()

        # Run inference
        for i in range(im_nums):
            if ims[i].endswith(("png", "PNG")):
                format_ = "png"
            elif ims[i].endswith((
                    "jpeg",
                    "jpg",
                    "JPEG",
                    "JPG",
            )):
                format_ = "jpg"
            else:
                print("---Unsupported image format:", ims[i])
                continue

            images_t = sess.run(output_format,
                                feed_dict={
                                    im_name: FLAGS.content + ims[i],
                                    im_format: format_
                                })

            elapsed = time.time() - start_time
            start_time = time.time()
            print('Time: {}'.format(elapsed))
            format_index = ims[i].rfind('.')
            images_name = ims[i][:format_index]
            out_path = os.path.join(
                output_path,
                FLAGS.output + "-" + images_name + trained_step + '.png')
            print("Save result in: ", out_path)
            misc.imsave(out_path, images_t[0], format="png")

        print('------------------------------------')
        print('Finished!')
示例#30
0
def uploadImage():
    if request.method == "POST":
        # # file = request.files['Image']
        upload_base64 = request.json['Image']
        # upload_base64 = upload_base64.replace('+','-').replace(r'/','_')

        with open("./img/xdqtest_resize.png", 'wb') as fdecode:
            decode_base64 = base64.b64decode(upload_base64)
            fdecode.write(decode_base64)

        modelname = request.json['modelname']
        size = []

        # resized image and save
        with tf.Session() as sess:

            image_raw_data = tf.gfile.FastGFile("./img/xdqtest_resize.png",
                                                'rb').read()
            image_decode = tf.image.decode_png(image_raw_data)
            height_source, width_source, _ = sess.run(image_decode).shape
            size = [height_source, width_source]
            if height_source + width_source < 1000:
                image_type = tf.image.convert_image_dtype(image_decode,
                                                          dtype=tf.float32)
                image_resized = tf.image.resize_images(
                    image_type, [int(size[0] * 0.8),
                                 int(size[1] * 0.8)],
                    method=1)

                image_data = tf.image.convert_image_dtype(image_resized,
                                                          dtype=tf.uint8)
                encode_image = tf.image.encode_png(image_data)
                if os.path.exists("process") is False:
                    os.mkdir("process")
                with tf.gfile.GFile("./process/xdqtest_resize.png",
                                    'wb') as fsave:
                    fsave.write(sess.run(encode_image))

            elif height_source + width_source < 1600:
                image_type = tf.image.convert_image_dtype(image_decode,
                                                          dtype=tf.float32)
                image_resized = tf.image.resize_images(
                    image_type, [int(size[0] * 0.7),
                                 int(size[1] * 0.7)],
                    method=1)

                image_data = tf.image.convert_image_dtype(image_resized,
                                                          dtype=tf.uint8)
                encode_image = tf.image.encode_png(image_data)
                if os.path.exists("process") is False:
                    os.mkdir("process")
                with tf.gfile.GFile("./process/xdqtest_resize.png",
                                    'wb') as fsave:
                    fsave.write(sess.run(encode_image))
            else:
                image_type = tf.image.convert_image_dtype(image_decode,
                                                          dtype=tf.float32)
                image_resized = tf.image.resize_images(
                    image_type, [int(size[0] * 0.5),
                                 int(size[1] * 0.5)],
                    method=1)

                image_data = tf.image.convert_image_dtype(image_resized,
                                                          dtype=tf.uint8)
                encode_image = tf.image.encode_png(image_data)
                if os.path.exists("process") is False:
                    os.mkdir("process")
                with tf.gfile.GFile("./process/xdqtest_resize.png",
                                    'wb') as fsave:
                    fsave.write(sess.run(encode_image))

        # return jsonify({"image_base64":upload_base64})

        # base64_tensor = tf.convert_to_tensor(upload_base64, dtype=tf.string)
        # img_str = tf.decode_base64(base64_tensor)
        # img = tf.image.decode_image(img_str, channels=3)
        # with tf.Session() as sess:
        # 	img_value = sess.run([img])[0]
        # 	# return jsonify({"img_value":img_value})
        # 	# return img_value.shape
        # 	height = img_value.shape[0]
        # 	width = img_value.shape[1]

        # 	print("Image value: {}".format(img_value.shape[0]))
        # 	return "a"

        # file = request.files.get("Image")

        # modelname = request.form['model']
        # file.headers['Access-Control-Allow-Origin'] = '*'
        # modelname.headers['Access-Control-Allow-Origin'] = '*'
        path = basedir + "/img/"
        # file_path = path + file.filename
        # file_path = path + "test.png"
        # file.save(file_path)
        # return str(model)
        # return "Upload success"
        height = 0
        width = 0
        # image_path = "img/"+file.filename

        image_path = "./process/xdqtest_resize.png"
        # return image_path
        with open(image_path, 'rb') as img:
            with tf.Session().as_default() as sess:
                if image_path.lower().endswith('png'):
                    image = sess.run(tf.image.decode_png(img.read()))
                else:
                    image = sess.run(tf.image.decode_jpeg(img.read()))
                height = image.shape[0]
                width = image.shape[1]
        tf.logging.info("Image size: {}, {}".format(width, height))
        with tf.Graph().as_default():
            with tf.Session().as_default() as sess:
                # return jsonify({"height":height, "width":width})
                # tf.logging.info('Image size:{},{}'.format(weight, height))

                # Read image data
                image_preprocessing_fn, _ = preprocessing_factory.get_preprocessing(
                    FLAGS.loss_model, is_training=False)
                image = reader.get_image(image_path, height, width,
                                         image_preprocessing_fn)
                # Add batch dimension
                image = tf.expand_dims(image, 0)

                generated = model.net(image, training=False)
                generated = tf.cast(generated, tf.uint8)

                # Remove batch dimension
                generated = tf.squeeze(generated, [0])

                # Restore model variables
                saver = tf.train.Saver(tf.global_variables(),
                                       write_version=tf.train.SaverDef.V1)
                sess.run([
                    tf.global_variables_initializer(),
                    tf.local_variables_initializer()
                ])
                # Use absolute path
                model_path = basedir + "/models/" + modelname
                saver.restore(sess, model_path)
                # return model_path
                # Make sure 'generated' directory exists
                # generated_file = 'generated/' + file.filename
                generated_file = 'generated/xdqtest.png'
                if os.path.exists('generated') is False:
                    os.makedir('generated')

                # Generate and write image data to file
                with open("./" + generated_file, 'wb') as img:
                    start_time = time.time()
                    img.write(sess.run(tf.image.encode_jpeg(generated)))
                    end_time = time.time()
                    # return "Done"

                # recover image from source input
                recover_image("./" + generated_file, size[0], size[1], sess)

                # process_image = basedir + "/" + generated_file
                process_image = basedir + "/" + 'resized_image/resized.png'

                # Encode image to base64
                with open(process_image, 'rb') as img:
                    image_base64 = img.read()
                    image_base64 = base64.b64encode(image_base64)
                    # response = make_response(image_base64)
                    # response.headers['Access-Control-Allow-Origin'] = '*'
                    # response.headers['Access-Control-Methods'] = 'POST'
                    # response.headers['Access-Control-Allow-Headers'] = 'x-requested-with, content-type'
                    # print(type(response))
                    # jsonify({"Image_base64":image_base64})
                    return jsonify({"Image_base64": image_base64})
示例#31
0
    def do_transfer(self):
        tf.logging.set_verbosity(tf.logging.INFO)

        # 获得目标图片,风格图片
        target_image_path = self.target_file_name
        style_reference_image_path = self.style_file_name

        if not os.path.exists(target_image_path):
            tku.show_message("请先选择目标图片文件!")
            return

        if not os.path.exists(style_reference_image_path):
            tku.show_message("请先选择风格图片文件!")
            return

        utils.create_folder(workspace_dir)

        self.final_img.set_image(target_image_path)
        self.refresh()

        # Get image's height and width.
        height = 0
        width = 0
        with open(target_image_path, 'rb') as img:
            with tf.Session().as_default() as sess:
                # if FLAGS.image_file.lower().endswith('png'):
                if target_image_path.lower().endswith('png'):
                    image = sess.run(tf.image.decode_png(img.read()))
                else:
                    image = sess.run(tf.image.decode_jpeg(img.read()))
                height = image.shape[0]
                width = image.shape[1]
        tf.logging.info('Image size: %dx%d' % (width, height))

        with tf.Graph().as_default():
            with tf.Session().as_default() as sess:
                # Read image data.
                image_preprocessing_fn, _ = preprocessing_factory.get_preprocessing(
                    'vgg_16', is_training=False)

                # image = reader.get_image(target_image_path, int(height/2.5), int(width/2.5), image_preprocessing_fn)  # gpu
                image = reader.get_image(target_image_path, height, width,
                                         image_preprocessing_fn)  # cpu

                # Add batch dimension
                image = tf.expand_dims(image, 0)

                generated = model.net(image, training=False)
                generated = tf.cast(generated, tf.uint8)

                # Remove batch dimension
                generated = tf.squeeze(generated, [0])

                # Restore model variables.
                saver = tf.train.Saver(tf.global_variables(),
                                       write_version=tf.train.SaverDef.V1)
                sess.run([
                    tf.global_variables_initializer(),
                    tf.local_variables_initializer()
                ])

                filepath, tempfilename = os.path.split(
                    style_reference_image_path)
                style_name, extension = os.path.splitext(tempfilename)
                model_name = 'models/' + style_name + '.ckpt-done'
                saver.restore(sess, model_name)

                self.generated_file = os.path.join(
                    workspace_dir,
                    result_prefix + utils.strftime(False) + ".jpg")

                # Generate and write image data to file.
                with open(self.generated_file, 'wb') as img:
                    start_time = time.time()
                    img.write(sess.run(tf.image.encode_jpeg(generated)))
                    end_time = time.time()
                    tf.logging.info('Elapsed time: %fs' %
                                    (end_time - start_time))

                    tf.logging.info('Done. Please check %s.' %
                                    self.generated_file)
                self.final_img.set_image(self.generated_file)
示例#32
0
def main(FLAGS):
    style_features_t = losses.get_style_features(FLAGS) #style target的Gram

    #make sure the training path exists
    training_path = os.path.join(FLAGS.model_path,FLAGS.naming) #model/wave/ ;用于存放训练好的模型
    if not (os.path.exists(training_path)):
        os.makedirs(training_path)

    with tf.Graph().as_default(): #默认计算图
        with tf.Session() as sess:#没有as_default(),因此,走出with 语句,sess停止执行,不能在被用
            """build loss network"""
            network_fn =nets_factory.get_network_fn(FLAGS.loss_model,num_classes=1,is_training=False) #取出loss model,且该model不用训练
            #对要进入loss_model的content_image,和generated_image进行preprocessing
            image_preprocessing_fn,image_unpreprocessing_fn = preprocessing_factory.get_preprocessing(FLAGS.loss_model,is_training=False) #取出用于loss_model的,对image进行preprocessing和unpreprocessing的function
            processed_image = reader.image(FLAGS.batch_size,FLAGS.image_size,FLAGS.image_size,'train2014/',image_preprocessing_fn,epochs=FLAGS.epoch) #这里要preprocessing的image是一个batch,为training_data
            generated = model.net(processed_images,training=True) #输入“图像生成网络”的image为经过preprocessing_image,“图像生成网络”为要训练的网络
            processed_generated = [image_preprocessing_fn(image,FLAGS.image_size,FLAGS.image_size) for image in tf.unstack(generated,axis=0,num=FLAGS.batch_size)]
            processed_generated = tf.stack(processed_generated)
            #计算generated_image和content_image进入loss_model后,更layer的output
            _,endpoints_dict= network_fn(tf.concat([processed_generated,processed_images],0),spatial_squeeze=False)#endpoints_dict中存储的是2类image各个layer的值
            #log the structure of loss network
            tf.logging.info('loss network layers(you can define them in "content layer" and "style layer"):')
            for key in endpoints_dict:
                tf.logging.info(key) #屏幕输出loss_model的各个layer name

            """build losses"""
            content_loss = losses.content_loss(endpoints_dict,FLAGS.content_layers)
            style_loss,style_loss_summary = losses.style_loss(endpoints_dict,style_features_t,FLAGS.style_layers)
            tv_loss = losses.total_variation_loss(generated)

            loss = FLAGS.style_weight * style_loss + FLAGS.content_weight * content_loss + FLAGS.tv_weight * tv_loss

            # Add Summary for visualization in tensorboard
            """Add Summary"""
            tf.summary.scalar('losses/content_loss',content_loss)
            tf.summary.scalar('losses/style_loss',style_loss)
            tf.summary.scalar('losses/regularizer_loss',tv_loss)

            tf.summary.scalar('weighted_losses/weighted content_loss',content_loss * FLAGS.content_weight)
            tf.summary.scalar('weighted_losses/weighted style_loss',style_loss * FLAGS.style_weight)
            tf.summary.scalar('weighted_losses/weighted_regularizer_loss',tv_loss * FLAGS.tv_weight)
            tf.summary.scalar('total_loss',loss)

            for layer in FLAGS.style_layers:
                tf.summary.scalar('style_losses/' + layer,style_loss_summary[layer])
            tf.summary.image('genearted',generated)
            tf.summary.image('origin',tf.stack([image_unprocessing_fn(image) for image in tf.unstack(processed_images,axis=0,num=FLAGS.batch_size)]))
            summary = tf.summary.merge_all()
            writer = tf.summary.FileWriter(training_path)

            """prepare to train"""
            global_step = tf.Variable(0,name='global_step',trainable=False)#iteration step

            variable_to_train = []#需要训练的变量
            for variable in tf.trainable_variables():#在图像风格迁移网络(图像生成网络+损失网络)各参数中,找需要训练的参数
                if not (variable.name.startswith(FLAGS.loss_model)):
                    variable_to_train.append(variable)
            train_op = tf.train.AdamOptimizer(1e-3).minimize(loss,global_step = global_step,var_list = variable_to_train) #需要放入sess.run()

            variable_to_restore = []#在所有的全局变量中,找需要恢复默认设置的变量; 注意:local_variable指的是一些临时变量和中间变量,用于线程中,线程结束则消失
            for v tf.global_variables():
                if not (v.name.startswith(FLAGS.loss_model)):
                    variables_to_restore.append(v)
            saver = tf.train.Saver(variables_to_restore,write_version=tf.train.SaverDef.V1)#利用saver.restore()恢复默认设置;这里的variable_to_restore,是需要save and restore的var_list

            sess.run([tf.global_variables_initializer(),tf.local_variables_initializer()])#对全局变量和局部变量进行初始化操作:即恢复默认设置

            #restore variables for loss model 恢复loss model中的参数
            init_func = utils._get_init_fn(FLAGS)
            init_func(sess)

            #restore variables for training model if the checkpoint file exists. 如果training_model已有训练好的参数,将其载入
            last_file = tf.train.latest_checkpoint(training_path)#将train_path中的model参数数据取出
            if last_file:
                tf.logging.info('restoringmodel from {}'.format(last_file))
                saver.restore(sess,last_file) #那如果last_file不存在,就不执行restore操作吗?需要restore的参数只是图像生成网络吗?

            """start training"""
            coord = tf.train.Coordinator()
            threads = tf.train.start_queue_runners(coord=coord)
            start_time = time.time()
            try:
                while not coord.should_stop():#查看线程是否停止(即:是否所有数据均运行完毕)
                    _,loss_t,step = sess.run([train_op,loss,global_step])
                    elapsed_time = time.time()
                    """logging"""
                    #print(step)
                    if step % 10 == 0:
                        tf.logging.info('step:%d, total loss %f, secs/step: %f' % (step,loss_t,elapsed_time))
                    """summary"""
                    if step % 25 == 0:
                        tf.logging.info('adding summary...')
                        summary_str = sess.run(summary)
                        writer.add_summary(summary_str,step)
                        writer.flush()
                    """checkpoint"""
                    if step % 1000 == 0:
                        saver.save(sess,os.path.join(training_path,'fast-style-model.ckpt'),global_step=step)#保存variable_to_restore中的参数值
            except tf.errors.OutOfRangeError:
                saver.save(sess,os.path.join(training_path,'fast-style-model.ckpt-done'))
                tf.logging.info('Done training -- epoch limit reached')
            finally:
                coord.request_stop()#要求停止所有线程
            coord.join(threads)#将线程并入主线程,删除
示例#33
0
def main(FLAGS):
    style_features_t = losses.get_style_features(FLAGS)

    # Make sure the training path exists.
    training_path = os.path.join(FLAGS.model_path, FLAGS.naming)
    if not (os.path.exists(training_path)):
        os.makedirs(training_path)

    with tf.Graph().as_default():
        with tf.Session() as sess:
            """Build Network"""
            # 损失网路:无需训练,定义training为FALSE
            network_fn = nets_factory.get_network_fn(FLAGS.loss_model,
                                                     num_classes=1,
                                                     is_training=False)

            # 损失网络中要用的图像的预处理函数
            image_preprocessing_fn, image_unprocessing_fn = preprocessing_factory.get_preprocessing(
                FLAGS.loss_model, is_training=False)

            # 读入的训练图像
            processed_images = reader.image(FLAGS.batch_size,
                                            FLAGS.image_size,
                                            FLAGS.image_size,
                                            'train2014/',
                                            image_preprocessing_fn,
                                            epochs=FLAGS.epoch)

            # 引入图像生成网络:需要训练,定义training为TRUE
            generated = model.net(processed_images, training=True)

            # 将生成的图像同样使用预处理生成函数进行处理,因为它同样要被送到损失网路中
            processed_generated = [
                image_preprocessing_fn(image, FLAGS.image_size,
                                       FLAGS.image_size) for image in
                tf.unstack(generated, axis=0, num=FLAGS.batch_size)
            ]
            processed_generated = tf.stack(processed_generated)

            # 将原始图像和生成图像送到损失网络中,此处将二者合并送入,以加快计算速度
            _, endpoints_dict = network_fn(tf.concat(
                [processed_generated, processed_images], 0),
                                           spatial_squeeze=False)

            # Log the structure of loss network
            tf.logging.info(
                'Loss network layers(You can define them in "content_layers" and "style_layers"):'
            )
            for key in endpoints_dict:
                tf.logging.info(key)
            """Build Losses"""
            # 定义内容损失
            content_loss = losses.content_loss(endpoints_dict,
                                               FLAGS.content_layers)
            # 定义风格损失
            style_loss, style_loss_summary = losses.style_loss(
                endpoints_dict, style_features_t, FLAGS.style_layers)
            # 定义tv损失
            tv_loss = losses.total_variation_loss(
                generated)  # use the unprocessed image

            # 总损失是之前的损失加权和,最后利用总损失优化网络即可
            loss = FLAGS.style_weight * style_loss + FLAGS.content_weight * content_loss + FLAGS.tv_weight * tv_loss

            # Add Summary for visualization in tensorboard.
            """Add Summary"""
            tf.summary.scalar('losses/content_loss', content_loss)
            tf.summary.scalar('losses/style_loss', style_loss)
            tf.summary.scalar('losses/regularizer_loss', tv_loss)

            tf.summary.scalar('weighted_losses/weighted_content_loss',
                              content_loss * FLAGS.content_weight)
            tf.summary.scalar('weighted_losses/weighted_style_loss',
                              style_loss * FLAGS.style_weight)
            tf.summary.scalar('weighted_losses/weighted_regularizer_loss',
                              tv_loss * FLAGS.tv_weight)
            tf.summary.scalar('total_loss', loss)

            for layer in FLAGS.style_layers:
                tf.summary.scalar('style_losses/' + layer,
                                  style_loss_summary[layer])
            tf.summary.image('generated', generated)
            # tf.image_summary('processed_generated', processed_generated)  # May be better?
            tf.summary.image(
                'origin',
                tf.stack([
                    image_unprocessing_fn(image) for image in tf.unstack(
                        processed_images, axis=0, num=FLAGS.batch_size)
                ]))
            summary = tf.summary.merge_all()
            writer = tf.summary.FileWriter(training_path)
            """Prepare to Train"""
            global_step = tf.Variable(0, name="global_step", trainable=False)

            # 找出需要训练的变量存储到checkpoint中:生成网络中的参数训练,损失网路中的参数不动
            variable_to_train = []
            # 使用tf.trainable_variables()找出需要训练的参量
            for variable in tf.trainable_variables():
                # 如果不在损失网路中,就把这些参数加入到列表中
                if not (variable.name.startswith(FLAGS.loss_model)):
                    variable_to_train.append(variable)
            # 定义训练步骤指定var_list=variable_to_train,这样不会训练损失网络
            train_op = tf.train.AdamOptimizer(1e-3).minimize(
                loss, global_step=global_step, var_list=variable_to_train)

            # 找出需要保存的变量
            variables_to_restore = []
            # 使用tf.global_variables()找出所有变量
            for v in tf.global_variables():
                # 如果不在损失网络中就要加入列表variables_to_restore
                if not (v.name.startswith(FLAGS.loss_model)):
                    variables_to_restore.append(v)

            # 定义saver指定只会保存variables_to_restore
            saver = tf.train.Saver(variables_to_restore,
                                   write_version=tf.train.SaverDef.V1)

            sess.run([
                tf.global_variables_initializer(),
                tf.local_variables_initializer()
            ])

            # Restore variables for loss network.
            init_func = utils._get_init_fn(FLAGS)
            init_func(sess)

            # Restore variables for training model if the checkpoint file exists.
            last_file = tf.train.latest_checkpoint(training_path)
            if last_file:
                tf.logging.info('Restoring model from {}'.format(last_file))
                saver.restore(sess, last_file)
            """Start Training"""
            coord = tf.train.Coordinator()
            threads = tf.train.start_queue_runners(coord=coord)
            start_time = time.time()
            try:
                while not coord.should_stop():
                    _, loss_t, step = sess.run([train_op, loss, global_step])
                    elapsed_time = time.time() - start_time
                    start_time = time.time()
                    """logging"""
                    # print(step)
                    if step % 10 == 0:
                        tf.logging.info(
                            'step: %d,  total Loss %f, secs/step: %f' %
                            (step, loss_t, elapsed_time))
                    """summary"""
                    if step % 25 == 0:
                        tf.logging.info('adding summary...')
                        summary_str = sess.run(summary)
                        writer.add_summary(summary_str, step)
                        writer.flush()
                    """checkpoint"""
                    if step % 1000 == 0:
                        saver.save(sess,
                                   os.path.join(training_path,
                                                'fast-style-model.ckpt'),
                                   global_step=step)
            except tf.errors.OutOfRangeError:
                saver.save(
                    sess,
                    os.path.join(training_path, 'fast-style-model.ckpt-done'))
                tf.logging.info('Done training -- epoch limit reached')
            finally:
                coord.request_stop()
            coord.join(threads)
def main(argv=None):
    if FLAGS.CONTENT_IMAGES_PATH:
        content_images = reader.image(
                FLAGS.BATCH_SIZE,
                FLAGS.IMAGE_SIZE,
                FLAGS.CONTENT_IMAGES_PATH,
                epochs=1,
                shuffle=False,
                crop=False)
        generated_images = model.net(content_images / 255.)

        output_format = tf.saturate_cast(generated_images + reader.mean_pixel, tf.uint8)
        with tf.Session() as sess:
            file = tf.train.latest_checkpoint(FLAGS.MODEL_PATH)
            if not file:
                print('Could not find trained model in {}'.format(FLAGS.MODEL_PATH))
                return
            print('Using model from {}'.format(file))
            saver = tf.train.Saver()
            saver.restore(sess, file)
            coord = tf.train.Coordinator()
            threads = tf.train.start_queue_runners(coord=coord)
            i = 0
            start_time = time.time()
            try:
                while not coord.should_stop():
                    print(i)
                    images_t = sess.run(output_format)
                    elapsed = time.time() - start_time
                    start_time = time.time()
                    print('Time for one batch: {}'.format(elapsed))

                    for raw_image in images_t:
                        i += 1
                        misc.imsave('out{0:04d}.png'.format(i), raw_image)
            except tf.errors.OutOfRangeError:
                print('Done training -- epoch limit reached')
            finally:
                coord.request_stop()

            coord.join(threads)
        return

    if not os.path.exists(FLAGS.MODEL_PATH):
        os.makedirs(FLAGS.MODEL_PATH)

    style_paths = FLAGS.STYLE_IMAGES.split(',')
    style_layers = FLAGS.STYLE_LAYERS.split(',')
    content_layers = FLAGS.CONTENT_LAYERS.split(',')

    style_features_t = get_style_features(style_paths, style_layers)

    images = reader.image(FLAGS.BATCH_SIZE, FLAGS.IMAGE_SIZE, FLAGS.TRAIN_IMAGES_PATH)
    generated = model.net(images / 255.)

    net, _ = vgg.net(FLAGS.VGG_PATH, tf.concat(0, [generated, images]))

    content_loss = 0
    for layer in content_layers:
        generated_images, content_images = tf.split(0, 2, net[layer])
        size = tf.size(generated_images)
        content_loss += tf.nn.l2_loss(generated_images - content_images) / tf.to_float(size)
    content_loss = content_loss / len(content_layers)

    style_loss = 0
    for style_gram, layer in zip(style_features_t, style_layers):
        generated_images, _ = tf.split(0, 2, net[layer])
        size = tf.size(generated_images)
        for style_image in style_gram:
            style_loss += tf.nn.l2_loss(tf.reduce_sum(gram(generated_images) - style_image, 0)) / tf.to_float(size)
    style_loss = style_loss / len(style_layers)

    loss = FLAGS.STYLE_WEIGHT * style_loss + FLAGS.CONTENT_WEIGHT * content_loss + FLAGS.TV_WEIGHT * total_variation_loss(generated)

    global_step = tf.Variable(0, name="global_step", trainable=False)
    train_op = tf.train.AdamOptimizer(1e-3).minimize(loss, global_step=global_step)

    output_format = tf.saturate_cast(tf.concat(0, [generated, images]) + reader.mean_pixel, tf.uint8)

    with tf.Session() as sess:
        saver = tf.train.Saver(tf.all_variables())
        file = tf.train.latest_checkpoint(FLAGS.MODEL_PATH)
        if file:
            print('Restoring model from {}'.format(file))
            saver.restore(sess, file)
        else:
            print('New model initilized')
            sess.run(tf.initialize_all_variables())

        coord = tf.train.Coordinator()
        threads = tf.train.start_queue_runners(coord=coord)
        start_time = time.time()
        try:
            while not coord.should_stop():
                _, loss_t, step = sess.run([train_op, loss, global_step])
                elapsed_time = time.time() - start_time
                start_time = time.time()
                if step % 100 == 0:
                    print(step, loss_t, elapsed_time)
                    output_t = sess.run(output_format)
                    for i, raw_image in enumerate(output_t):
                        misc.imsave('out{}.png'.format(i), raw_image)
                if step % 10000 == 0:
                    saver.save(sess, FLAGS.MODEL_PATH + '/fast-style-model', global_step=step)
        except tf.errors.OutOfRangeError:
            print('Done training -- epoch limit reached')
        finally:
            coord.request_stop()

        coord.join(threads)
示例#35
0
def perceptual_loss(net_type):
    """Compute perceptual loss of content and style

    Return:
        generated 前向生成网络
        images 输入图片(batch based)
        loss 各种loss.
    """
    # Set style image
    style_paths = FLAGS.style_images.split(',')
    # Set style layers and content layers in vgg net
    style_layers = FLAGS.style_layers.split(',')
    content_layers = FLAGS.content_layers.split(',')
    # Get style feature, pre calculated and save it in memory
    style_features_t = get_style_features(style_paths, style_layers, net_type)

    # Read images from dataset
    images = reader.image(FLAGS.batch_size,
                          FLAGS.image_size,
                          FLAGS.train_images_path,
                          epochs=FLAGS.epoch)

    # Transfer images
    # 为什么要换成0-1编码?
    # 这里和里面的处理对应起来, 虽然这么写很丑, 也容易忘
    generated = model.net(images / 255)
    # generated = model.net(tf.truncated_normal(images.get_shape(), stddev=0.3))

    # Process generated and original images with vgg
    net, _ = vgg.net(FLAGS.vgg_path, tf.concat([generated, images], 0),
                     net_type)

    # Get content loss
    content_loss = 0
    for layer in content_layers:
        # 平均分为两组,每组都是batch长度的图片组
        gen_features, images_features = tf.split(net[layer],
                                                 num_or_size_splits=2,
                                                 axis=0)
        size = tf.size(gen_features)
        content_loss += tf.nn.l2_loss(gen_features -
                                      images_features) / tf.to_float(size)
    content_loss /= len(content_layers)

    # Get Style loss
    style_loss = 0
    for style_gram, layer in zip(style_features_t, style_layers):
        gen_features, _ = tf.split(net[layer], num_or_size_splits=2, axis=0)
        size = tf.size(gen_features)
        # Calculate style loss for each style image
        for style_image in style_gram:
            style_loss += tf.nn.l2_loss(
                model.gram(gen_features, FLAGS.batch_size) -
                style_image) / tf.to_float(size)
    style_loss /= len(style_layers)

    # Total loss
    total_v_loss = total_variation_loss(generated)
    loss = FLAGS.style_weight * style_loss + FLAGS.content_weight * content_loss + FLAGS.tv_weight * total_v_loss

    return generated, images, content_loss, style_loss, total_v_loss, loss