Exemplo n.º 1
0
def train(add, num_testing, object_dim, job_name, **kwargs):

	coco_train = COCO('./annotations/instances_train2014.json')
	coco_test = COCO('./annotations/instances_val2014.json')

	training_generator = create_generator(coco = coco_train, mode = 'training', add = add, object_dim = object_dim, **kwargs)
	testing_generator = create_generator(coco = coco_test, mode = 'testing', add = add, object_dim = object_dim, **kwargs)

	model = build_model(add = add, object_dim = object_dim, **kwargs)

	model.compile(loss = 'categorical_crossentropy', optimizer = Adam(1e-6, beta_1=.9, beta_2=.99), metrics = ['accuracy'])

	# callbacks_list = [ModelCheckpoint('./'+job_name+'.h5', monitor='val_loss', verbose=1, save_best_only=True, mode='min')]

	callbacks_list = [ModelCheckpoint('./'+job_name+'.h5', monitor='val_loss', verbose=1, mode='min')]

	print(model.summary())

	history = model.fit_generator(
		training_generator, \
		validation_steps = num_testing, \
		validation_data = training_generator, \
		steps_per_epoch = 100, # 5000, \
		epochs = 5, # 500, \
		# callbacks = callbacks_list,\
		verbose=1, \
		max_queue_size = 2, # 10, \
		workers = 1, \
		)
Exemplo n.º 2
0
def train(add, num_testing, object_dim, job_name, **kwargs):
    coco_train = COCO(
        '/home/leonard/../../media/data/Datasets/MSCOCO2014/annotations/instances_train2014.json'
    )
    coco_test = COCO(
        '/home/leonard/../../media/data/Datasets/MSCOCO2014/annotations/instances_val2014.json'
    )

    training_generator = create_generator(coco=coco_train,
                                          mode='training',
                                          add=add,
                                          object_dim=object_dim,
                                          **kwargs)
    testing_generator = create_generator(coco=coco_test,
                                         mode='testing',
                                         add=add,
                                         object_dim=object_dim,
                                         **kwargs)

    model = build_model(add=add, object_dim=object_dim, **kwargs)

    model.compile(loss='categorical_crossentropy',
                  optimizer=Adam(1e-6, beta_1=.9, beta_2=.99),
                  metrics=['accuracy'])

    callbacks_list = [
        ModelCheckpoint('../model_weights/' + job_name + '.h5',
                        monitor='val_loss',
                        verbose=1,
                        save_best_only=True,
                        mode='min')
    ]

    print(model.summary())

    history = model.fit(
        training_generator,
        validation_steps=num_testing,
        validation_data=testing_generator,
        steps_per_epoch=5000,
        epochs=500,
        callbacks=callbacks_list,
        verbose=1,
        max_queue_size=10,
        workers=1,
    )
Exemplo n.º 3
0
def main():
    if tf.__version__.split('.')[0] != "1":
        raise Exception("Tensorflow version 1 required")

    if a.seed is None:
        a.seed = random.randint(0, 2**31 - 1)

    tf.set_random_seed(a.seed)
    np.random.seed(a.seed)
    random.seed(a.seed)

    if not os.path.exists(a.output_dir):
        os.makedirs(a.output_dir)
#%% test
    if a.mode == "test" or a.mode == "export":
        if a.checkpoint is None:
            raise Exception("checkpoint required for test mode")

        # load some options from the checkpoint
        options = {"which_direction", "ngf", "ndf", "lab_colorization"}
        with open(os.path.join(a.checkpoint, "options.json")) as f:
            for key, val in json.loads(f.read()).items():
                if key in options:
                    print("loaded", key, "=", val)
                    setattr(a, key, val)
        # disable these features in test mode
        a.scale_size = CROP_SIZE
        a.flip = False
#%%
    for k, v in a._get_kwargs():
        print(k, "=", v)

    with open(os.path.join(a.output_dir, "options.json"), "w") as f:
        f.write(json.dumps(vars(a), sort_keys=True, indent=4))
#%%    export the meta
    if a.mode == "export":
        # export the generator to a meta graph that can be imported later for standalone generation
        if a.lab_colorization:
            raise Exception("export not supported for lab_colorization")

        input = tf.placeholder(tf.string, shape=[1])
        input_data = tf.decode_base64(input[0])
        input_image = tf.image.decode_png(input_data)

        # remove alpha channel if present
        #if true, excute the former ,otherwise the latter
        input_image = tf.cond(tf.equal(tf.shape(input_image)[2],
                                       4), lambda: input_image[:, :, :3],
                              lambda: input_image)
        # convert grayscale to RGB
        input_image = tf.cond(tf.equal(tf.shape(input_image)[2], 1),
                              lambda: tf.image.grayscale_to_rgb(input_image),
                              lambda: input_image)

        input_image = tf.image.convert_image_dtype(input_image,
                                                   dtype=tf.float32)
        input_image.set_shape([CROP_SIZE, CROP_SIZE, 3])
        batch_input = tf.expand_dims(input_image, axis=0)

        with tf.variable_scope("generator"):
            batch_output = tools.deprocess(
                create_generator(tools.preprocess(batch_input), 3))

        output_image = tf.image.convert_image_dtype(batch_output,
                                                    dtype=tf.uint8)[0]
        if a.output_filetype == "png":
            output_data = tf.image.encode_png(output_image)
        elif a.output_filetype == "jpeg":
            output_data = tf.image.encode_jpeg(output_image, quality=80)
        else:
            raise Exception("invalid filetype")
        output = tf.convert_to_tensor([tf.encode_base64(output_data)])

        key = tf.placeholder(tf.string, shape=[1])
        inputs = {"key": key.name, "input": input.name}
        tf.add_to_collection("inputs", json.dumps(inputs))
        outputs = {
            "key": tf.identity(key).name,
            "output": output.name,
        }
        tf.add_to_collection("outputs", json.dumps(outputs))

        init_op = tf.global_variables_initializer()
        restore_saver = tf.train.Saver()
        export_saver = tf.train.Saver()

        with tf.Session() as sess:
            sess.run(init_op)
            print("loading model from checkpoint")
            checkpoint = tf.train.latest_checkpoint(a.checkpoint)
            restore_saver.restore(sess, checkpoint)
            print("exporting model")
            export_saver.export_meta_graph(
                filename=os.path.join(a.output_dir, "export.meta"))
            export_saver.save(sess,
                              os.path.join(a.output_dir, "export"),
                              write_meta_graph=False)

        return


#%%
    examples = load_examples(a)
    print("examples count = %d" % examples.count)

    # inputs and targets are [batch_size, height, width, channels]
    model = create_model(examples.inputs, examples.targets, a)

    # undo colorization splitting on images that we use for display/output
    if a.lab_colorization:
        if a.which_direction == "AtoB":
            # inputs is brightness, this will be handled fine as a grayscale image
            # need to augment targets and outputs with brightness
            targets = tools.augment(examples.targets, examples.inputs)
            outputs = tools.augment(model.outputs, examples.inputs)
            # inputs can be deprocessed normally and handled as if they are single channel
            # grayscale images
            inputs = tools.deprocess(examples.inputs)
        elif a.which_direction == "BtoA":
            # inputs will be color channels only, get brightness from targets
            inputs = tools.augment(examples.inputs, examples.targets)
            targets = tools.deprocess(examples.targets)
            outputs = tools.deprocess(model.outputs)
        else:
            raise Exception("invalid direction")
    else:
        inputs = tools.deprocess(examples.inputs)
        targets = tools.deprocess(examples.targets)
        outputs = tools.deprocess(model.outputs)

    # reverse any processing on images so they can be written to disk or displayed to user
    with tf.name_scope("convert_inputs"):
        converted_inputs = convert(inputs)

    with tf.name_scope("convert_targets"):
        converted_targets = convert(targets)

    with tf.name_scope("convert_outputs"):
        converted_outputs = convert(outputs)

    with tf.name_scope("encode_images"):
        display_fetches = {
            "paths":
            examples.paths,
            "inputs":
            tf.map_fn(tf.image.encode_png,
                      converted_inputs,
                      dtype=tf.string,
                      name="input_pngs"),
            "targets":
            tf.map_fn(tf.image.encode_png,
                      converted_targets,
                      dtype=tf.string,
                      name="target_pngs"),
            "outputs":
            tf.map_fn(tf.image.encode_png,
                      converted_outputs,
                      dtype=tf.string,
                      name="output_pngs"),
        }

    # summaries
    with tf.name_scope("inputs_summary"):
        tf.summary.image("inputs", converted_inputs)

    with tf.name_scope("targets_summary"):
        tf.summary.image("targets", converted_targets)

    with tf.name_scope("outputs_summary"):
        tf.summary.image("outputs", converted_outputs)

    with tf.name_scope("predict_real_summary"):
        tf.summary.image(
            "predict_real",
            tf.image.convert_image_dtype(model.predict_real, dtype=tf.uint8))

    with tf.name_scope("predict_fake_summary"):
        tf.summary.image(
            "predict_fake",
            tf.image.convert_image_dtype(model.predict_fake, dtype=tf.uint8))

    tf.summary.scalar("discriminator_loss", model.discrim_loss)
    tf.summary.scalar("generator_loss_GAN", model.gen_loss_GAN)
    tf.summary.scalar("generator_loss_L1", model.gen_loss_L1)

    for var in tf.trainable_variables():
        tf.summary.histogram(var.op.name + "/values", var)

    for grad, var in model.discrim_grads_and_vars + model.gen_grads_and_vars:
        tf.summary.histogram(var.op.name + "/gradients", grad)

    with tf.name_scope("parameter_count"):
        parameter_count = tf.reduce_sum(
            [tf.reduce_prod(tf.shape(v)) for v in tf.trainable_variables()])

    saver = tf.train.Saver(max_to_keep=1)

    logdir = a.output_dir if (a.trace_freq > 0 or a.summary_freq > 0) else None
    sv = tf.train.Supervisor(logdir=logdir, save_summaries_secs=0, saver=None)
    with sv.managed_session() as sess:
        print("parameter_count =", sess.run(parameter_count))

        if a.checkpoint is not None:
            print("loading model from checkpoint")
            checkpoint = tf.train.latest_checkpoint(a.checkpoint)
            saver.restore(sess, checkpoint)

        max_steps = 2**32
        if a.max_epochs is not None:
            max_steps = examples.steps_per_epoch * a.max_epochs
        if a.max_steps is not None:
            max_steps = a.max_steps

        if a.mode == "test":
            # testing
            # at most, process the test data once
            max_steps = min(examples.steps_per_epoch, max_steps)
            for step in range(max_steps):
                results = sess.run(display_fetches)
                filesets = save_images(results)
                for i, f in enumerate(filesets):
                    print("evaluated image", f["name"])
                index_path = append_index(filesets)

            print("wrote index at", index_path)
        else:
            # training
            start = time.time()

            for step in range(max_steps):

                def should(freq):
                    return freq > 0 and ((step + 1) % freq == 0
                                         or step == max_steps - 1)

                options = None
                run_metadata = None
                if should(a.trace_freq):
                    options = tf.RunOptions(
                        trace_level=tf.RunOptions.FULL_TRACE)
                    run_metadata = tf.RunMetadata()

                fetches = {
                    "train": model.train,
                    "global_step": sv.global_step,
                }

                if should(a.progress_freq):
                    fetches["discrim_loss"] = model.discrim_loss
                    fetches["gen_loss_GAN"] = model.gen_loss_GAN
                    fetches["gen_loss_L1"] = model.gen_loss_L1

                if should(a.summary_freq):
                    fetches["summary"] = sv.summary_op

                if should(a.display_freq):
                    fetches["display"] = display_fetches

                results = sess.run(fetches,
                                   options=options,
                                   run_metadata=run_metadata)

                if should(a.summary_freq):
                    print("recording summary")
                    sv.summary_writer.add_summary(results["summary"],
                                                  results["global_step"])

                if should(a.display_freq):
                    print("saving display images")
                    filesets = save_images(results["display"],
                                           step=results["global_step"])
                    append_index(filesets, step=True)

                if should(a.trace_freq):
                    print("recording trace")
                    sv.summary_writer.add_run_metadata(
                        run_metadata, "step_%d" % results["global_step"])

                if should(a.progress_freq):
                    # global_step will have the correct step count if we resume from a checkpoint
                    train_epoch = math.ceil(results["global_step"] /
                                            examples.steps_per_epoch)
                    train_step = (results["global_step"] -
                                  1) % examples.steps_per_epoch + 1
                    rate = (step + 1) * a.batch_size / (time.time() - start)
                    remaining = (max_steps - step) * a.batch_size / rate
                    print(
                        "progress  epoch %d  step %d  image/sec %0.1f  remaining %dm"
                        % (train_epoch, train_step, rate, remaining / 60))
                    print("discrim_loss", results["discrim_loss"])
                    print("gen_loss_GAN", results["gen_loss_GAN"])
                    print("gen_loss_L1", results["gen_loss_L1"])

                if should(a.save_freq):
                    print("saving model")
                    saver.save(sess,
                               os.path.join(a.output_dir, "model"),
                               global_step=sv.global_step)

                if sv.should_stop():
                    break
Exemplo n.º 4
0
def create_model(input_img, target_img):

    epsilon = 1e-12
    #生成器
    output_channels = int(target_img.get_shape()[-1])
    generator = create_generator(input_img, output_channels)

    #关于真实图片的判别器
    real_discriminator = create_discriminator(input_img, target_img)

    #关于生成图片的判别器
    fake_discriminator = create_discriminator(generator, target_img)

    #生成器损失
    gen_gan_loss = tf.reduce_mean(-tf.log(fake_discriminator + epsilon))
    gen_l1_loss = tf.reduce(tf.abs(target_img - generator))
    gen_loss = args.gen_wight * gen_gan_loss + args.l1_wight * gen_l1_loss
    #判别器损失
    dis_loss = tf.reduce_mean(-(tf.log(real_discriminator + epsilon) +
                                tf.log(1 - fake_discriminator + epsilon)))

    #生成器需要更新的变量列表
    gen_vars = [
        var for var in tf.trainable_variables()
        if var.name.startswith('generator')
    ]
    #判别器需要更新的变量列表
    dis_vars = [
        var for var in tf.trainable_variables()
        if var.name.startswith("discriminator")
    ]

    #生成器优化器
    gen_optimizer = tf.train.AdamOptimizer(0.0002, 0.5)
    #判别器优化器
    dis_optimizer = tf.train.AdamOptimizer(0.0002, 0.5)

    #使用损失 对指定变量进行训练更新
    #与使用optimizer.minimize()区别????
    gen_gradients = gen_optimizer.compute_gradients(gen_loss,
                                                    var_list=gen_vars)
    gen_train = gen_optimizer.apply_gradients(gen_gradients)
    dis_gradients = dis_optimizer.compute_gradients(dis_loss,
                                                    var_list=dis_vars)
    dis_train = dis_optimizer.apply_gradients(dis_gradients)

    #更新参数
    ema = tf.train.ExponentialMovingAverage(decay=0.99)
    update_losses = ema.apply([dis_loss, gen_gan_loss, gen_l1_loss])

    global_step = tf.train.get_or_create_global_step()
    incr_global_step = tf.assign(global_step, global_step + 1)

    return Model(generator=generator,
                 real_discriminator=real_discriminator,
                 fake_discriminator=fake_discriminator,
                 discrim_loss=ema.average(dis_loss),
                 gen_loss_GAN=ema.average(gen_gan_loss),
                 gen_loss_L1=ema.average(gen_l1_loss),
                 gen_gradients=gen_gradients,
                 dis_gradients=dis_gradients,
                 train=tf.group(update_losses, incr_global_step, gen_train))
Exemplo n.º 5
0
def train(inputfolder):
    epochs = config['epochs']

    ## -----------------------------------------------------------
    ## Dataset preparation
    #

    files = [
        join(inputfolder, f) for f in listdir(inputfolder)
        if isfile(join(inputfolder, f))
    ]

    # Func for turning strings like '0, 3, 11' into [1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1]
    def to_slice(string):
        ans = [random.uniform(-1, -0.3) for x in range(12)]
        for j in [int(s) for s in string.split(',')]:
            ans[j] = random.uniform(0.3, 1)
        return np.array(ans)

    dataset = []
    # Read strings, turn them into np.arrays and add dims (output - (1, 1, 12))
    for file in files:
        f = open(file, 'r')
        dataset.append(
            np.array([
                np.expand_dims(np.expand_dims(to_slice(st.rstrip()), axis=0),
                               axis=0) for st in f.readlines()
            ]))
        f.close()

    dataset = np.array(dataset)
    np.random.shuffle(dataset)

    #
    ## -----------------------------------------------------------

    sess = tf.Session()

    # Define session for Keras and set learning flag to true (batch normalization etc)
    K.set_session(sess)
    K.set_learning_phase(True)

    ## -----------------------------------------------------------
    ## Start stacking GAN
    # Here because we need to call reset_states() from discriminator and generator models
    #

    # input for choosing real/fake data
    if_real = tf.placeholder(tf.bool, name="IF_REAL_INPUT")

    # input for GAN
    inp = tf.placeholder(tf.float32, shape=(1, 1, 12), name="DATA_INPUT")

    # Create generator model
    gen = gn.create_generator(inp)
    if isfile(config['base_folder'] + "/" + config['gen_weights']):
        gen.load_weights(config['base_folder'] + "/" + config['gen_weights'])

    with tf.name_scope("condition"):
        # Get output tensor and do some reshaping
        g_out = gen.output

        # Get output tensor and do some reshaping
        g_out = tf.expand_dims(g_out, axis=1)

        # If on current step we want to feed discriminator with real data then create
        # tensor from inp. else from result of generator
        x = tf.cond(if_real, lambda: inp, lambda: g_out)

    # Create discriminator model
    dis = ds.create_discriminator(x)
    if isfile(config['base_folder'] + "/" + config['dis_weights']):
        dis.load_weights(config['base_folder'] + "/" + config['dis_weights'])

    # Finish stacking GAN: add loss functions and different updates
    # So note_gan is a function with such signature:
    # note_gan(if_real=<if_real value>, inp=<12-bit vector value>,
    # dis=<discriminator model>, gen=<generator model>)
    note_gan = GAN(if_real=if_real, inp=inp, dis=dis, gen=gen)

    #
    ## -----------------------------------------------------------

    # Create writer for tensorboard
    summary_writer = tf.summary.FileWriter(config['tensorflow_logs'],
                                           graph=sess.graph)

    # Initialize vars
    init = tf.global_variables_initializer()
    sess.run(init)

    # Main cycle
    gloss_history = []
    dloss_history = []
    for i in range(epochs):
        accum_g = 0
        accum_d = 0
        print(datetime.datetime.now())
        print("Epoch: ", i)
        print("Gen loss, Dis loss")
        cnt = 0
        for song in dataset:
            # x - one song, numpy.array of strings
            losses = [0, 0]
            for slc in song:
                step_bool = random.random() > 0.5
                step = note_gan(sess=sess,
                                vector_input=slc,
                                if_real_input=step_bool)
                losses[0] += step[1][0]  # gen loss
                losses[1] += step[1][1]  # dis loss

            accum_g += losses[0] / len(song)
            accum_d += losses[1] / len(song)
            print("\n" + str(cnt))
            print(losses[0] / len(song), losses[1] / len(song))

            # After feeding entire song reset models
            gen.reset_states()
            dis.reset_states()
            cnt += 1

        gloss_history.append(accum_g)
        dloss_history.append(accum_d)
        print("Gen loss: %d, Dis loss: %d" % (accum_g, accum_d))

    # Save generator description...
    with open(config['base_folder'] + "/" + config['gen_model'], "w") as file:
        file.write(gen.to_yaml())
    # ...PNG visualization...
    plot_model(gen,
               to_file=config['base_folder'] + "/" + config['gen_picture'],
               show_shapes=True)
    # ...and weights
    gen.save_weights(config['base_folder'] + "/" + config['gen_weights'])

    # Similar with discriminator
    with open(config['base_folder'] + "/" + config['dis_model'], "w") as file:
        file.write(dis.to_yaml())
    plot_model(dis,
               to_file=config['base_folder'] + "/" + config['dis_picture'],
               show_shapes=True)
    dis.save_weights(config['base_folder'] + "/" + config['dis_weights'])

    # Close session
    summary_writer.close()
    sess.close()

    print(gloss_history)
    print(dloss_history)

    plt.plot(range(0, epochs), gloss_history, color='red', linewidth=2.)
    plt.plot(range(0, epochs), dloss_history, color='blue', linewidth=2.)
    plt.show()
def create_model(inputs, targets,a):
    EPS = 1e-12
    
    with tf.variable_scope("generator") as scope:
        out_channels = int(targets.get_shape()[-1])
        outputs = create_generator(inputs, out_channels)

    # create two copies of discriminator, one for real pairs and one for fake pairs
    # they share the same underlying variables
    with tf.name_scope("real_discriminator"):
        with tf.variable_scope("discriminator"):
            # 2x [batch, height, width, channels] => [batch, 30, 30, 1]
            predict_real = create_discriminator(inputs, targets)

    with tf.name_scope("fake_discriminator"):
        with tf.variable_scope("discriminator", reuse=True):
            # 2x [batch, height, width, channels] => [batch, 30, 30, 1]
            predict_fake = create_discriminator(inputs, outputs)

    with tf.name_scope("discriminator_loss"):
        # minimizing -tf.log will try to get inputs to 1
        # predict_real => 1
        # predict_fake => 0
        discrim_loss = tf.reduce_mean(-(tf.log(predict_real + EPS) + tf.log(1 - predict_fake + EPS)))

    with tf.name_scope("generator_loss"):
        # predict_fake => 1
        # abs(targets - outputs) => 0
        gen_loss_GAN = tf.reduce_mean(-tf.log(predict_fake + EPS))
        gen_loss_L1 = tf.reduce_mean(tf.abs(targets - outputs))
        gen_loss = gen_loss_GAN * a.gan_weight + gen_loss_L1 * a.l1_weight

    with tf.name_scope("discriminator_train"):
        discrim_tvars = [var for var in tf.trainable_variables() if var.name.startswith("discriminator")]
        discrim_optim = tf.train.AdamOptimizer(a.lr, a.beta1)
        discrim_grads_and_vars = discrim_optim.compute_gradients(discrim_loss, var_list=discrim_tvars)
        discrim_train = discrim_optim.apply_gradients(discrim_grads_and_vars)

    with tf.name_scope("generator_train"):
        with tf.control_dependencies([discrim_train]):
            gen_tvars = [var for var in tf.trainable_variables() if var.name.startswith("generator")]
            gen_optim = tf.train.AdamOptimizer(a.lr, a.beta1)
            gen_grads_and_vars = gen_optim.compute_gradients(gen_loss, var_list=gen_tvars)
            gen_train = gen_optim.apply_gradients(gen_grads_and_vars)

    ema = tf.train.ExponentialMovingAverage(decay=0.99)
    update_losses = ema.apply([discrim_loss, gen_loss_GAN, gen_loss_L1])

    global_step = tf.contrib.framework.get_or_create_global_step()
    incr_global_step = tf.assign(global_step, global_step+1)

    return Model(
        predict_real=predict_real,
        predict_fake=predict_fake,
        discrim_loss=ema.average(discrim_loss),
        discrim_grads_and_vars=discrim_grads_and_vars,
        gen_loss_GAN=ema.average(gen_loss_GAN),
        gen_loss_L1=ema.average(gen_loss_L1),
        gen_grads_and_vars=gen_grads_and_vars,
        outputs=outputs,
        train=tf.group(update_losses, incr_global_step, gen_train),
    )
Exemplo n.º 7
0
def train():

    epsilon = 1e-12

    input_img = tf.placeholder(shape=[args.batch_size, 512, 512, 3],
                               dtype=tf.float32)
    target_img = tf.placeholder(shape=[args.batch_size, 512, 512, 3],
                                dtype=tf.float32)

    #生成器
    with tf.variable_scope("generator"):
        output_channels = int(target_img.get_shape()[-1])
        generator = create_generator(input_img, output_channels)

    #关于真实图片的判别器
    with tf.variable_scope('discriminator'):
        real_discriminator = create_discriminator(input_img, target_img)

    #关于生成图片的判别器
    with tf.variable_scope('discriminator', reuse=True):
        fake_discriminator = create_discriminator(input_img, generator)

    #定义生成器、判别器损失
    gen_gan_loss = tf.reduce_mean(-tf.log(fake_discriminator + epsilon))
    gen_l1_loss = tf.reduce_mean(tf.abs(target_img - generator))
    gen_loss = args.gan_weight * gen_gan_loss + args.l1_weight * gen_l1_loss
    dis_loss = tf.reduce_mean(-(tf.log(real_discriminator + epsilon) +
                                tf.log(1 - fake_discriminator + epsilon)))

    #需要更新的变量列表
    gen_vars = [
        var for var in tf.trainable_variables()
        if var.name.startswith('generator')
    ]
    dis_vars = [
        var for var in tf.trainable_variables()
        if var.name.startswith("discriminator")
    ]

    #定义优化器
    global_step = tf.Variable(0, name='global_step', trainable=False)
    gen_optimizer = tf.train.AdamOptimizer(0.0002, 0.5)
    dis_optimizer = tf.train.AdamOptimizer(0.0002, 0.5)
    '''
    使用损失 对指定变量进行训练更新
    '''
    gen_gradients = gen_optimizer.compute_gradients(gen_loss,
                                                    var_list=gen_vars)
    gen_train = gen_optimizer.apply_gradients(gen_gradients,
                                              global_step=global_step)
    dis_gradients = dis_optimizer.compute_gradients(dis_loss,
                                                    var_list=dis_vars)
    dis_train = dis_optimizer.apply_gradients(dis_gradients,
                                              global_step=global_step)

    #存储模型
    # saver = tf.train.Saver(max_to_keep=1)

    #初始化所有变量
    init_global = tf.global_variables_initializer()
    '''
    #更新参数
    # ema = tf.train.ExponentialMovingAverage(decay=0.99)
    # update_losses = ema.apply([dis_loss, gen_gan_loss, gen_l1_loss])
    
    # global_step = tf.train.get_or_create_global_step()
    # incr_global_step = tf.assign(global_step,global_step+1)
    '''
    dataset = Dataset(args)
    one_img = dataset.load()

    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True

    with tf.Session(config=config) as sess:
        sess.run(init_global)
        '''
        #加载已有的模型,断点训练
        #在config中添加self.args.retraining、self.args.model_path参数
        if self.args.retraining:
            last_model = tf.train.latest_checkpoint(self.args.model_path)
            print("Restoring model from {}".format(last_model))
            saver.restore(sess, last_model)
        '''
        '''
        #保存打印summary
        '''
        i = 1
        while True:
            #不断的获得下一个样本
            try:
                input_img_, target_img_ = sess.run(
                    [one_img['input_img'], one_img['target_img']])
                r_input_img = sess.run(resize(input_img_))
                r_target_img = sess.run(resize(target_img_))
                _, _, gen_loss_, dis_loss_ = sess.run(
                    [gen_train, dis_train, gen_loss, dis_loss],
                    feed_dict={
                        input_img: r_input_img,
                        target_img: r_target_img
                    })
                print('gen_loss:', gen_loss_, 'dis_loss:', dis_loss_)
            #如果遍历完了数据集,则返回错误
            except tf.errors.OutOfRangeError:
                print("End of dataset")
                break
            i += 1