예제 #1
0
def test_without_changes():
    """
    Plots the rectangles on top of the image, without converting to a 5-D representation. Useful only for debugging
    of the 5-D transform
    Returns:

    """
    path = "../debug_dataset"  # Only add a couple of pictures to this path
    images, pos_rectangles, neg_rectangles = load_data(path)
    df = pd.DataFrame(columns=["filenames", "p1", "p2", "p3", "p4"])
    for filename in pos_rectangles.filenames.unique():
        points = pos_rectangles[pos_rectangles["filenames"] == filename]
        points = points.loc[:, ['x', 'y']]
        for i in range(0, len(points), 4):
            x1, y1 = points.iloc[i][0], points.iloc[i][1]
            x2, y2 = points.iloc[i + 1][0], points.iloc[i + 1][1]
            x3, y3 = points.iloc[i + 2][0], points.iloc[i + 2][1]
            x4, y4 = points.iloc[i + 3][0], points.iloc[i + 3][1]
            new_row = [
                filename,
                np.asarray([int(x1), int(y1)]),
                np.asarray([int(x2), int(y2)]),
                np.asarray([int(x3), int(y3)]),
                np.asarray([int(x4), int(y4)])
            ]
            df.loc[len(df)] = new_row
    print(df)
    for i, j in images.iterrows():
        rectangles = df[df["filenames"] == j["filenames"]]
        plot(j["images"], j["filenames"], rectangles)
예제 #2
0
def test():
    """
    Plots the rectangles on top of the image. To see if the 5-D transformation works well.
    Returns:

    """
    path = "../debug_dataset"  # Only add a couple of pictures to this path
    images, pos_rectangles, neg_rectangles = load_data(path)
    pos_rectangles = to_five_dimensional(pos_rectangles)
    replicated_imgs = replicate_images(images, pos_rectangles)
    x_train, y_train, x_test, y_test = split_train_test_data(
        replicated_imgs, pos_rectangles)
    df = to_four_points(pos_rectangles)
    for i, j in x_test.iterrows():
        rectangles = df[df.filenames == j["filenames"]]
        plot(j["images"], j["filenames"], rectangles)
예제 #3
0
def infer():

    exe = fluid.Executor(fluid.CPUPlace())
    if args.use_gpu:
        exe = fluid.Executor(fluid.CUDAPlace(0))
    exe.run(fluid.default_startup_program())

    noise_data = np.random.uniform(low=-1.0,
                                   high=1.0,
                                   size=[args.batch_size,
                                         NOISE_SIZE]).astype('float32')

    save_path = 'freeze_model'

    [infer_program, feeded_var_names,
     target_var] = fluid.io.load_inference_model(dirname=save_path,
                                                 executor=exe)

    generated_image = exe.run(infer_program,
                              feed={feeded_var_names[0]: noise_data},
                              fetch_list=target_var)[0]
    if not os.path.exists(args.output):
        os.makedirs(args.output)

    total_images = generated_image
    fig = plot(total_images)
    plt.savefig('{}/generated_image.png'.format(args.output),
                bbox_inches='tight')
    plt.close(fig)
예제 #4
0
def train_straight_DDPG(episodes, agent):
    # Currently we need one action output that will be
    # amount of acceleration of straight vehicle
    # Shape is the number of neural inputs or output
    action_space = 1
    state_space = 1

    # Currently we didn't resize the Radar data to (4, len(radar))
    # as we have to flatten it anyway
    radar_space = 600

    # Get the first state (speed, distance from junction)
    # Create model
    straight_model = md.DDPG(action_space, state_space, radar_space,
                             'Straight_Model')

    # Update rate of target
    tau = 0.005

    # To store reward history of each episode
    ep_reward_list = []
    # To store average reward history of last few episodes
    avg_reward_list = []
    # To store actor and critic loss
    actor_loss = []
    critic_loss = []

    for epi in range(episodes):
        try:
            radar_state_prev = agent.reset(False)
            time.sleep(1)
            radar_state_prev = np.reshape(radar_state_prev, (1, radar_space))

            start_state = [0]
            state = np.reshape(start_state, (1, state_space))
            score = 0
            max_step = 5_000

            actor_loss_epi = []
            critic_loss_epi = []
            length_epi = []
            for i in range(max_step):
                choice = straight_model.policy(radar_state_prev, state)
                action = choose_action_straight(choice)

                print(
                    f"action----{action}-----epsilon----{straight_model.epsilon}"
                )

                radar_state_next, next_state, reward, done, length_traversed = agent.step_straight(
                    action, 1)
                time.sleep(0.5)

                score += reward
                next_state = np.reshape(next_state, (1, state_space))
                straight_model.remember(radar_state_prev, radar_state_next,
                                        state, choice, reward, next_state,
                                        done)
                state = next_state
                radar_state_prev = np.reshape(radar_state_next,
                                              (1, radar_space))

                # This is back-prop, updating weights
                lossActor, lossCritic = straight_model.replay()
                actor_loss_epi.append(lossActor)
                critic_loss_epi.append(lossCritic)

                # Update the target model, we do it slowly as it keep things stable, SOFT VERSION
                straight_model.update_target(tau, epi)
                if done:
                    length_epi.append(length_traversed)
                    break

            actor_loss.append(np.mean(actor_loss_epi))
            critic_loss.append(np.mean(critic_loss_epi))

            # Will do a HARD update now, setting it to critic and actor, set tau=1
            straight_model.update_target(0.01, epi)

            ep_reward_list.append(score)
            print("\nepisode: {}/{}, score: {}".format(epi, episodes, score))

            avg_reward = np.mean(ep_reward_list[-AGGREGATE_STATS_EVERY:])
            avg_length = np.mean(length_epi[-AGGREGATE_STATS_EVERY:])
            print(
                "\nEpisode * {} * Avg Reward is ==> {} Avg Length is ==> {}\n".
                format(epi, avg_reward, avg_length))
            avg_reward_list.append(avg_reward)

            # Update log stats (every given number of episodes)
            min_reward = min(ep_reward_list[-AGGREGATE_STATS_EVERY:])
            max_reward = max(ep_reward_list[-AGGREGATE_STATS_EVERY:])
            # straight_model.tensorboard.update_stats(reward_avg=avg_reward, reward_min=min_reward, reward_max=max_reward, epsilon=straight_model.epsilon)
            straight_model.tensorboard.update_stats(
                reward_avg=[None, avg_reward],
                critic_loss=[None, np.mean(critic_loss_epi)],
                actor_loss=[None, np.mean(actor_loss_epi)],
                lenght_covered=[None, np.mean(avg_length)])

            if (epi % 100 == 0 and epi > 0):
                x_label = 'Episodes'
                y_label = 'Actor Loss'
                ut.plot(actor_loss, x_label, y_label, epi)
                time.sleep(1)
                y_label = 'Critic Loss'
                ut.plot(critic_loss, x_label, y_label, epi)
                time.sleep(1)

        finally:
            print(f"Task Completed! Episode {epi}")

            straight_model.save_model()
            if agent != None:
                agent.destroy()
                time.sleep(1)

    return actor_loss, critic_loss
예제 #5
0
			print('Here')
			agent.step([0.5, 0.0, 0.0, False])
			time.sleep(0.5)
			count=count+1
		agent.step([0.0, 0.0, 1.0, False])
		time.sleep(5)
	finally:
		agent.destroy()
		time.sleep(5)

"""

if __name__ == '__main__':
    """
	Main function
	"""
    agent = env.CarlaVehicle()

    #Code to train the models
    episodes = 5_000
    actor_Loss, critic_Loss = train_straight_DDPG(episodes, agent)

    print("\n\n--We need to Maxmise Actor Loss--Minimise Critic Loss--\n\n")
    x_label = 'Episodes'
    y_label = 'Actor Loss'
    ut.plot(actor_Loss, x_label, y_label)
    y_label = 'Critic Loss'
    ut.plot(critic_Loss, x_label, y_label)
    #Code for Manual Control
    #manual_control()
예제 #6
0
def train_rightturn_DDPG(episodes, agent):
    # Two action choice for output
    # amount of acceleration of straight vehicle
    # Shape is the number of neural inputs or output
    action_space = 1
    state_space = 2

    radar_space = 400

    # Get the first state (speed, distance from junction)
    # Create model
    rightturn_model = md.DDPG(action_space, state_space, radar_space,
                              'Right_Turn_Model')

    # Update rate of target
    tau = 0.005

    # To store reward history of each episode
    ep_reward_list = []
    # To store average reward history of last few episodes
    avg_reward_list = []
    # To store actor and critic loss
    actor_loss = []
    critic_loss = []

    #For debugging the reward function
    epi_count = 150
    epirange = 200
    for epi in range(episodes):
        try:
            loc = random.randint(30, 130)
            print(f'--------Spawn Succeded RightTurn-----------')
            radar_state_prev = agent.reset(False, loc)
            radar_state_prev = np.reshape(radar_state_prev, (1, radar_space))
            start_state = [50, 90]
            state = np.reshape(start_state, (1, state_space))
            score = 0
            max_step = 5_00

            actor_loss_epi = []
            critic_loss_epi = []
            for i in range(max_step):
                choice = rightturn_model.policy(radar_state_prev, state)
                action = choose_action_rightturn(choice)
                # print(f'action1------------{action}')
                # if(epi>=epi_count and epi_count<epirange):
                # 	action =  choose_action_rightturn(0.2)
                # 	choice = 0.2
                print(
                    f'action----{action}-------epsilon----{rightturn_model.epsilon}'
                )
                radar_state_next, next_state, reward, done, _ = agent.step_rightturn(
                    action, 1)
                # print(f'next_state-----{next_state}-----reward---{next_state}----{done}')
                time.sleep(0.2)

                score += reward
                next_state = np.reshape(next_state, (1, state_space))
                rightturn_model.remember(radar_state_prev, radar_state_next,
                                         state, choice, reward, next_state,
                                         done)
                state = next_state
                radar_state_prev = np.reshape(radar_state_next,
                                              (1, radar_space))

                # This is back-prop, updating weights
                lossActor, lossCritic = rightturn_model.replay()
                actor_loss_epi.append(lossActor)
                critic_loss_epi.append(lossCritic)

                # Update the target model, we do it slowly as it keep things stable, SOFT VERSION
                rightturn_model.update_target(tau)

                if done:
                    break
            actor_loss.append(np.mean(actor_loss_epi))
            critic_loss.append(np.mean(critic_loss_epi))

            # Will do a HARD update now, setting it to critic and actor, set tau=1
            rightturn_model.update_target(0.01)

            ep_reward_list.append(score)
            print("\nepisode: {}/{}, score: {}".format(epi, episodes, score))

            avg_reward = np.mean(ep_reward_list[-AGGREGATE_STATS_EVERY:])
            print("\nEpisode * {} * Avg Reward is ==> {}\n".format(
                epi, avg_reward))
            avg_reward_list.append(avg_reward)

            # Update log stats (every given number of episodes)
            min_reward = min(ep_reward_list[-AGGREGATE_STATS_EVERY:])
            max_reward = max(ep_reward_list[-AGGREGATE_STATS_EVERY:])
            # straight_model.tensorboard.update_stats(reward_avg=avg_reward, reward_min=min_reward, reward_max=max_reward, epsilon=straight_model.epsilon)
            rightturn_model.tensorboard.update_stats(
                reward_avg=avg_reward,
                critic_loss=np.mean(critic_loss_epi),
                actor_loss=np.mean(actor_loss_epi))

            if (epi % 100 == 0 and epi > 1):
                x_label = 'Episodes'
                y_label = 'Actor Loss'
                ut.plot(actor_loss, x_label, y_label, epi)
                y_label = 'Critic Loss'
                ut.plot(critic_loss, x_label, y_label, epi)

            # # Average score of last 100 episode
            # if avg_reward > 500:
            # 	print('\n Task Completed! \n')
            # 	break

        finally:
            print(f"Task Completed! Episode {epi}")

            rightturn_model.save_model()
            if agent != None:
                agent.destroy()
                time.sleep(1)

    return actor_loss, critic_loss
예제 #7
0
def run_model_run(dataset=None, *, session="s7"):
    try:
        train_transforms, test_transforms = train_test_dataloader.define_train_test_transformers(
            session=session)
        train_data, test_data = train_test_dataloader.download_data(
            dataset_name=utility.get_dataset_name(session=session),
            train_transforms=train_transforms,
            test_transforms=test_transforms)

        train_loader, test_loader = train_test_dataloader.get_train_test_dataloaders(
            train_data=train_data,
            test_data=test_data,
            data_loader_args=utility.get_dataloader_args())

        all_regularizations_list, tracker = utility.get_combos_and_trackers()
        device = utility.get_device()
        # utility.get_all_models_summary()
        loss_fn = nn.functional.nll_loss
        model = None

        for combo in all_regularizations_list:
            print("\nRunning for: ", combo)

            if dataset and dataset.lower() == "mnist":
                if CONSTANTS.GBN in combo.lower():
                    model = basic_mnist.GBNNet().to(device)
                else:
                    model = basic_mnist.S6_MNIST().to(device)
            elif "s7" in session.lower() or dataset.lower() == "cifar10":
                model = cifar10_groups_dws_s7_model.S7_CIFAR10()
                model = model.to(device)
                loss_fn = nn.CrossEntropyLoss()

            optimizer = utility.get_optimizer(model=model)
            scheduler = utility.get_scheduler(optimizer=optimizer)
            utility.show_model_summary(
                title=model.__doc__,
                model=model,
                input_size=utility.get_input_size(
                    dataset=utility.get_dataset_name(session=session)))

            train_test.train_test(
                model=model,
                device=device,
                train_loader=train_loader,
                optimizer=optimizer,
                epochs=int(utility.get_config_details()[CONSTANTS.MODEL_CONFIG]
                           [CONSTANTS.EPOCHS]),
                scheduler=scheduler,
                test=True,
                test_loader=test_loader,
                type_=combo,
                tracker=tracker,
                loss_fn=loss_fn)

        for plot_type in utility.get_config_details()[CONSTANTS.PLOTS][
                CONSTANTS.TO_PLOT].strip().split(','):
            utility.plot(title="Plot is for:" + plot_type,
                         x_label='Epochs',
                         y_label=plot_type.lower(),
                         tracker=tracker,
                         category=plot_type)
    except Exception as e:
        print(traceback.format_exc(e))
예제 #8
0
		agent.destroy()
		time.sleep(5)
		



# ==============================================================================
# -- Manual Method------------------------------------------------------------
# -- Call function train_agent_name(episodes,agent) to train ------------------
# -- Call function manual_control() to see the task ---------------------------
# -- Call function nn_control() to test the nn agents --------------------------
# ==============================================================================
if __name__ == '__main__':
	"""
	Main function
	"""
	agent = env.CarlaVehicle()
	#Code to train the models
	episodes = 500
	loss = train_hierarchical_dqn(episodes,agent)
	ut.plot(loss)

	#nn_lane_change()
	#Code for Manual Control
	#manual_control()
	#lane_control()
	#left_turn()
	#right_lane_control()
	#nn_right_lane_change()

예제 #9
0
def train(args):

    if args.run_ce:
        np.random.seed(10)
        fluid.default_startup_program().random_seed = 90
    d_program = fluid.Program()
    dg_program = fluid.Program()

    with fluid.program_guard(d_program):
        img = fluid.layers.data(name='img', shape=[784], dtype='float32')
        label = fluid.layers.data(name='label', shape=[1], dtype='float32')
        d_logit = D(img)
        d_loss = loss(d_logit, label)

    with fluid.program_guard(dg_program):
        noise = fluid.layers.data(
            name='noise', shape=[NOISE_SIZE], dtype='float32')
        g_img = G(x=noise)

        g_program = dg_program.clone()
        g_program_test = dg_program.clone(for_test=True)

        dg_logit = D(g_img)
        dg_loss = loss(
            dg_logit,
            fluid.layers.fill_constant_batch_size_like(
                input=noise, dtype='float32', shape=[-1, 1], value=1.0))

    opt = fluid.optimizer.Adam(learning_rate=LEARNING_RATE)

    opt.minimize(loss=d_loss)
    parameters = [p.name for p in g_program.global_block().all_parameters()]

    opt.minimize(loss=dg_loss, parameter_list=parameters)

    exe = fluid.Executor(fluid.CPUPlace())
    if args.use_gpu:
        exe = fluid.Executor(fluid.CUDAPlace(0))
    exe.run(fluid.default_startup_program())

    if args.run_ce:
        train_reader = paddle.batch(
                paddle.dataset.mnist.train(),
                batch_size=args.batch_size)
    else:
        train_reader = paddle.batch(
            paddle.reader.shuffle(
                paddle.dataset.mnist.train(), buf_size=60000),
            batch_size=args.batch_size)

    NUM_TRAIN_TIMES_OF_DG = 2
    const_n = np.random.uniform(
        low=-1.0, high=1.0,
        size=[args.batch_size, NOISE_SIZE]).astype('float32')

    t_time = 0
    losses = [[], []]
    for pass_id in range(args.epoch):
        for batch_id, data in enumerate(train_reader()):
            if len(data) != args.batch_size:
                continue
            noise_data = np.random.uniform(
                low=-1.0, high=1.0,
                size=[args.batch_size, NOISE_SIZE]).astype('float32')
            real_image = np.array(list(map(lambda x: x[0], data))).reshape(
                -1, 784).astype('float32')
            real_labels = np.ones(
                shape=[real_image.shape[0], 1], dtype='float32')
            fake_labels = np.zeros(
                shape=[real_image.shape[0], 1], dtype='float32')
            total_label = np.concatenate([real_labels, fake_labels])
            s_time = time.time()
            generated_image = exe.run(g_program,
                                      feed={'noise': noise_data},
                                      fetch_list={g_img})[0]

            total_images = np.concatenate([real_image, generated_image])

            d_loss_1 = exe.run(d_program,
                               feed={
                                   'img': generated_image,
                                   'label': fake_labels,
                               },
                               fetch_list={d_loss})[0][0]

            d_loss_2 = exe.run(d_program,
                               feed={
                                   'img': real_image,
                                   'label': real_labels,
                               },
                               fetch_list={d_loss})[0][0]

            d_loss_n = d_loss_1 + d_loss_2
            losses[0].append(d_loss_n)
            for _ in six.moves.xrange(NUM_TRAIN_TIMES_OF_DG):
                noise_data = np.random.uniform(
                    low=-1.0, high=1.0,
                    size=[args.batch_size, NOISE_SIZE]).astype('float32')
                dg_loss_n = exe.run(dg_program,
                                     feed={'noise': noise_data},
                                     fetch_list={dg_loss})[0][0]
                losses[1].append(dg_loss_n)
            t_time += (time.time() - s_time)
            if batch_id % 10 == 0 and not args.run_ce:
                if not os.path.exists(args.output):
                    os.makedirs(args.output)
                # generate image each batch
                generated_images = exe.run(g_program_test,
                                           feed={'noise': const_n},
                                           fetch_list={g_img})[0]
                total_images = np.concatenate([real_image, generated_images])
                fig = plot(total_images)
                msg = "Epoch ID={0} Batch ID={1} D-Loss={2} DG-Loss={3}\n gen={4}".format(
                    pass_id, batch_id,
                    d_loss_n, dg_loss_n, check(generated_images))
                print(msg)
                plt.title(msg)
                plt.savefig(
                    '{}/{:04d}_{:04d}.png'.format(args.output, pass_id,
                                                  batch_id),
                    bbox_inches='tight')
                plt.close(fig)
    if args.run_ce:
        print("kpis,dcgan_d_train_cost,{}".format(np.mean(losses[0])))
        print("kpis,dcgan_g_train_cost,{}".format(np.mean(losses[1])))
        print("kpis,dcgan_duration,{}".format(t_time / args.epoch))
예제 #10
0
            agent_levels_list[i] = level_target

    # calculate the least square difference of count change
    loss = np.sum((count_levels_combined_copy - count_levels_combined)**2)

    # update state variable(s)
    # count_levels_combined[:] = count_levels_combined_copy[:]

    count_levels_combined = count_levels_combined_copy
    return loss


if __name__ == '__main__':
    setup()
    print("Started... ")
    utility.plot(num_levels, num_classes, count_levels_list,
                 count_levels_combined)  #initial

    import time
    start_time = time.time()
    loss = epsilon + 1
    epoch = 0
    while loss > epsilon and epoch < epoch_max:
        loss = turtle()
        print("Epoch " + str(epoch) + " Loss: " + str(loss))
        epoch += 1
        # util.plot(num_levels,num_classes, count_levels_list,count_levels_combined)
    # util.plot_wealth(count_levels_combined,"Count Levels Plot")
    util.plot(num_levels, num_classes, count_levels_list,
              count_levels_combined)

    print("Converged after " + str(epoch) + " epoches. ")
예제 #11
0
def train(args):

    d_program = fluid.Program()
    dg_program = fluid.Program()

    #image = fluid.layers.data(name='image', shape=[3, 32, 32], dtype='float32')

    with fluid.program_guard(d_program):
        img = fluid.layers.data(name='img',
                                shape=[IMG_SIZE * IMG_SIZE],
                                dtype='float32')
        label = fluid.layers.data(name='label', shape=[1], dtype='float32')

        d_logit = D(img)
        ################################################################
        d_loss = loss(d_logit, label)
        ################################################################
    with fluid.program_guard(dg_program):
        noise = fluid.layers.data(name='noise',
                                  shape=[NOISE_SIZE, 1, 1],
                                  dtype='float32')
        g_img = G(x=noise)  #generate fake image

        g_program = dg_program.clone()
        g_program_test = dg_program.clone(for_test=True)

        dg_logit = D(g_img)
        ################################################################
        dg_loss = loss(
            dg_logit,
            fluid.layers.fill_constant_batch_size_like(input=noise,
                                                       dtype='float32',
                                                       shape=[-1, 1],
                                                       value=1.0))
        ################################################################
    opt = fluid.optimizer.Adam(learning_rate=LEARNING_RATE, beta1=0.5)

    opt.minimize(loss=d_loss)
    parameters = [p.name for p in g_program.global_block().all_parameters()]

    opt.minimize(loss=dg_loss, parameter_list=parameters)

    exe = fluid.Executor(fluid.CPUPlace())
    if args.use_gpu:
        exe = fluid.Executor(fluid.CUDAPlace(0))
    exe.run(fluid.default_startup_program())

    #download data
    data_dir = os.path.join(BASE_DIR, 'data')
    if not os.path.exists(data_dir):
        os.mkdir(data_dir)
    data_dir = os.path.join(data_dir, 'data65')
    if not os.path.exists(data_dir):
        os.mkdir(data_dir)
    print("start to download image data...")
    image_filename = os.path.join(BASE_DIR,
                                  'data/data65/train-images-idx3-ubyte.gz')
    if not os.path.exists(image_filename):
        r = requests.get(
            'http://yann.lecun.com/exdb/mnist/train-images-idx3-ubyte.gz')
        with open(image_filename, 'wb') as f:
            f.write(r.content)
    print("start to downlaod image data...")
    label_filename = os.path.join(BASE_DIR,
                                  'data/data65/train-labels-idx1-ubyte.gz')
    if not os.path.exists(label_filename):
        r = requests.get(
            'http://yann.lecun.com/exdb/mnist/train-labels-idx1-ubyte.gz')
        with open(label_filename, 'wb') as f:
            f.write(r.content)
    print("finished download")

    train_reader = reader_creator(image_filename, label_filename, 500)
    train_reader = batch(train_reader, args.batch_size, drop_last=False)
    NUM_TRAIN_TIMES_OF_DG = 2
    const_n = np.random.uniform(low=-1.0,
                                high=1.0,
                                size=[args.batch_size, NOISE_SIZE, 1,
                                      1]).astype('float32')

    t_time = 0
    losses = [[], []]
    for pass_id in range(args.epoch):
        for batch_id, data in enumerate(train_reader()):
            if len(data) != args.batch_size:
                continue
            noise_data = np.random.uniform(
                low=-1.0, high=1.0, size=[args.batch_size, NOISE_SIZE, 1,
                                          1]).astype('float32')
            real_image = np.array(list(map(lambda x: x[0], data))).reshape(
                -1, IMG_SIZE * IMG_SIZE).astype('float32')
            real_labels = np.ones(shape=[real_image.shape[0], 1],
                                  dtype='float32')
            fake_labels = np.zeros(shape=[real_image.shape[0], 1],
                                   dtype='float32')
            total_label = np.concatenate([real_labels, fake_labels])
            s_time = time.time()
            generated_image = exe.run(g_program,
                                      feed={'noise': noise_data},
                                      fetch_list=[g_img
                                                  ])[0]  #generate fake image

            total_images = np.concatenate([real_image, generated_image])

            d_loss_1 = exe.run(d_program,
                               feed={
                                   'img': generated_image,
                                   'label': fake_labels,
                               },
                               fetch_list=[d_loss])[0][0]  #tell fake images

            d_loss_2 = exe.run(d_program,
                               feed={
                                   'img': real_image,
                                   'label': real_labels,
                               },
                               fetch_list=[d_loss])[0][0]  #tell real image
            ##############two losses above to trian D
            d_loss_n = d_loss_1 + d_loss_2
            losses[0].append(d_loss_n)
            for _ in six.moves.xrange(NUM_TRAIN_TIMES_OF_DG):
                noise_data = np.random.uniform(
                    low=-1.0,
                    high=1.0,
                    size=[args.batch_size, NOISE_SIZE, 1, 1]).astype('float32')
                dg_loss_n = exe.run(dg_program,
                                    feed={'noise': noise_data},
                                    fetch_list=[dg_loss])[0][0]  ### G_loss
                losses[1].append(dg_loss_n)
            t_time += (time.time() - s_time)
            if batch_id % 200 == 0 and not args.run_ce:
                # if not os.path.exists(args.output):
                #     os.makedirs(args.output)
                # generate image each batch
                generated_images = exe.run(g_program_test,
                                           feed={'noise': const_n},
                                           fetch_list=[g_img])[0]
                total_images = np.concatenate([real_image, generated_images])
                fig = plot(total_images)
                msg = "Epoch ID={0} Batch ID={1} D-Loss={2} DG-Loss={3}\n gen={4}".format(
                    pass_id, batch_id, d_loss_n, dg_loss_n,
                    check(generated_images))
                print(msg)
                #plt.title(msg)
                #plt.savefig(
                #    os.path.join(BASE_DIR, 'result/dcgan/{:04d}_{:04d}.png'.format(pass_id,
                #                                  batch_id)),
                #    bbox_inches='tight')
                #plt.close(fig)

        #save_path = os.path.join(BASE_DIR, 'model/dcgan_model')
        # delete old model file
        #shutil.rmtree(save_path, ignore_errors=True)
        #os.makedirs(save_path)
        # save prediction model
        #fluid.io.save_inference_model(main_program=g_program_test, dirname=save_path, feeded_var_names=['noise'], target_vars=g_img, executor=exe)

    if args.run_ce:
        print("kpis,dcgan_d_train_cost,{}".format(np.mean(losses[0])))
        print("kpis,dcgan_g_train_cost,{}".format(np.mean(losses[1])))
        print("kpis,dcgan_duration,{}".format(t_time / args.epoch))

    result_dir = os.path.join(BASE_DIR, 'result')
    if not os.path.exists(result_dir):
        os.mkdir(result_dir)
    RESULT_FILE = os.path.join(result_dir, 'results_dcgan.txt')
    with open(RESULT_FILE, 'a') as f:
        f.write('\n\n\n\ dcgan results:\n')
        f.write("kpis,dcgan_d_train_cost,{}".format(np.mean(losses[0])))
        f.write("kpis,dcgan_g_train_cost,{}".format(np.mean(losses[1])))
        f.write("kpis,dcgan_duration,{}".format(t_time))

    index = 0
    while os.path.exists(
            os.path.join(
                result_dir,
                'minst_dcgan_tf_epoch_{}_{}.png'.format(args.epoch, index))):
        index += 1
    imgname = os.path.join(
        result_dir, 'minst_dcgan_tf_epoch_{}_{}.png'.format(args.epoch, index))
    generated_images = exe.run(g_program_test,
                               feed={'noise': const_n},
                               fetch_list=[g_img])[0]
    total_images = np.concatenate([real_image, generated_images])
    fig = plot(total_images)
    plt.savefig(imgname, bbox_inches='tight')
    plt.close(fig)