Example #1
0
def test_worker(args, shared_model, total_steps, optimizer):
    args.environment.clip_rewards = False
    env = make_env(args)

    log_path = '{}/{}'.format(args.train.experiment_path, 'log.txt')
    logging.basicConfig(filename=log_path, level=logging.INFO)
    logging.info("STARTED TRAINING PROCESS {}".format(
        time.strftime("%Y.%m.%d_%H:%M", time.localtime())))

    model = ActorCritic(env.observation_space.shape, env.action_space.n)
    model.eval()

    start_time = time.time()

    reward_history = []
    while True:
        model.load_state_dict(shared_model.state_dict())
        if (len(reward_history) + 1) % args.train.save_frequency == 0:
            save_progress(args, model, optimizer, total_steps.value)
        total_reward, _ = play_game(model, env)
        reward_history.append(total_reward)

        log_message = "Time {}, num steps {}, FPS {:.0f}, curr episode reward {}, mean episode reward: {}".format(
            time.strftime("%Hh %Mm %Ss",
                          time.gmtime(time.time() - start_time)),
            total_steps.value,
            total_steps.value / (time.time() - start_time),
            total_reward,
            np.mean(reward_history[-60:]),
        )
        print(log_message)
        logging.info(log_message)
        time.sleep(60)
Example #2
0
	def stop(self):
		''' stop actual playing an store path and position to cache file '''
		self.set_status(False)
		# save to cache file
		save_progress(self.book.get_path(), self.get_chapter(), 
			pygame.mixer.music.get_pos(), self.book.get_cover())
		# stop the music
		pygame.mixer.music.stop()
Example #3
0
def test_worker(args, shared_model, total_steps, optimizer):
    args.environment.clip_rewards = False
    env = make_env(args.environment)

    log_path = '{}/{}'.format(args.train.experiment_folder, 'log.txt')
    logging.basicConfig(filename=log_path, level=logging.INFO)
    logging.info("STARTED TRAINING PROCESS {}".format(time.strftime("%Y.%m.%d_%H:%M", time.localtime())))

    model = ActorCritic(env.observation_space.shape, env.action_space.n)
    model = BaseWrapper(model)
    if (args.train.use_pixel_control or
            args.train.use_reward_prediction):
        model = ExperienceWrapper(model)
    if args.train.use_pixel_control:
        model = PixelControlWrapper(model, args.train.gamma, args.train.pc_coef)
    if args.train.use_reward_prediction:
        model = RewardPredictionWrapper(model, args.train.rp_coef)
    if args.train.use_value_replay:
        model = ValueReplayWrapper(model)
    model.config = args
    model.eval()

    start_time = time.time()

    reward_history = []
    while True:
        model.load_state_dict(shared_model.state_dict())
        if (len(reward_history) + 1) % args.train.save_frequency == 0:
            save_progress(args, model, optimizer, total_steps.value)
        stats = play_game(model, env)
        reward_history.append(stats['total_reward'])

        log_message = (
                'Time {}, num steps {}, FPS {:.0f}, '+
                'curr episode reward {:.2f}, mean episode reward: {:.2f}, '+
                'mean policy loss {:.2f}, mean value loss {:.2f}, '+
                'mean entropy percentage {:.2f}'
            ).format(
            time.strftime("%Hh %Mm %Ss", time.gmtime(time.time() - start_time)),
            total_steps.value,
            total_steps.value / (time.time() - start_time),
            stats['total_reward'],
            np.mean(reward_history[-60:]),
            stats['policy_loss'],
            stats['value_loss'],
            stats['entropy']
        )
        if args.train.use_pixel_control:
            log_message += ', pixel control loss %.2f' %stats['pc_loss']
        if args.train.use_reward_prediction:
            log_message += ', reward prediction loss %.2f' %stats['rp_loss']
        if args.train.use_value_replay:
            log_message += ', value replay loss %.2f' %stats['vr_loss']
        print(log_message)
        logging.info(log_message)
        time.sleep(60)
    def run(
        self,
        n_episodes,
        level=0,
        load=False,
        save_frequency=10,
        threshold=70,
        test=True,
        verbose=False,
        visualize=True,
        save_video=True,
        visualize_directory=None,
    ):
        """
        ### NO WORK NEEDED ###
        You can look at the structure but you do not need to modify it.
        You can print whatever you feel necessary.
        ######################

        Train agent for n_episodes

        if test == True the agent will not explore and will only exploit.
        if verbose == True the function will print more information during the training (this will messup the progress bar)
        if visualize == True the function will display an animation of the rocket landing for every episode.
        This is at the expense of speed though. If false it will only show it every n episodes.
        """
        self.level_number = level
        self.env = tensorforce.environments.OpenAIGym("RocketLander-v0",
                                                      level_number=level)

        if n_episodes < save_frequency:
            str_error = f"n_episodes<save frequency, the model won't be able to save, set n_episodes to a value >={save_frenquency}"
            raise (ValueError(str_error))

        agent = self.create_agent(self.env, n_episodes, save_frequency, load)

        # Loop over episodes
        reward_list_episodes = []
        reward_list = []
        score_list = []
        landing_fraction_list = []
        tqdm_bar = tqdm(range(1, n_episodes + 1))
        self.number_of_landings = 0

        for i in tqdm_bar:
            self.fraction_good_landings = self.number_of_landings * 100 / i
            if i > 1:
                tqdm_bar.set_description(
                    "Episode %d/%d reward: %d (max:%d, min:%d, mean:%d, std:%d), successful landings:%d(%d%%)"
                    % (
                        i,
                        n_episodes,
                        np.round(np.sum(reward_list), 3),
                        np.max(reward_list_episodes),
                        np.min(reward_list_episodes),
                        np.round(np.mean(reward_list_episodes), 3),
                        np.round(np.std(reward_list_episodes), 3),
                        self.number_of_landings,
                        np.round(self.fraction_good_landings, 3),
                    ))

            if i % save_frequency == 0:
                if save_video:
                    self.env = tensorforce.environments.OpenAIGym(
                        "RocketLander-v0",
                        visualize=True,
                        visualize_directory=visualize_directory,
                        level_number=level,
                    )
                else:
                    self.env = tensorforce.environments.OpenAIGym(
                        "RocketLander-v0", visualize=True, level_number=level)

                reward_list, score = self.episode(self.env,
                                                  i,
                                                  agent,
                                                  test=True,
                                                  verbose=False)
            else:
                self.env = tensorforce.environments.OpenAIGym(
                    "RocketLander-v0", level_number=level)
                reward_list, score = self.episode(self.env,
                                                  i,
                                                  agent,
                                                  test=False,
                                                  verbose=False)
            score_list.append(score)
            reward_list_episodes.append(np.sum(reward_list))
            landing_fraction_list.append(self.fraction_good_landings)
            if self.env.environment.landed_ticks > 59:
                self.number_of_landings += 1
            if (self.fraction_good_landings > threshold) and (i > 50):
                print("level cracked")
                self.cracked = True
                break

        # Show Sum of reward over 1 episode vs number of episodes graph
        reward_list_episodes = save_progress(load,
                                             reward_list_episodes,
                                             "reward_list_episodes.txt",
                                             level=level)
        score_list = save_progress(load,
                                   score_list,
                                   "score_list.txt",
                                   level=level)
        landing_fraction_list = save_progress(load,
                                              landing_fraction_list,
                                              "landing_fraction.txt",
                                              level=level)
        save_graph(
            reward_list_episodes,
            "Sum of reward over 1 episode vs number of episodes",
            "rewards_vs_episodes.png",
            rolling=True,
            level=level,
        )
        save_graph(
            score_list,
            "Score vs number of episodes",
            "score_vs_episodes.png",
            rolling=True,
            level=level,
        )
        save_graph(
            landing_fraction_list,
            "Landing fraction vs number of episodes",
            "landing_fraction_vs_episodes.png",
            level=level,
        )

        # SENDING INFO TO DATABASE
        DATE = str(datetime.datetime.today())
        inputs = [np.mean(score_list), n_episodes]
        result_data = prep_data_to_send(inputs, GROUP_NAME, DATE)
        send_result(result_data)
Example #5
0
def train(device,
          model_dir,
          in_data,
          out_data,
          model,
          loss_func,
          batchsize,
          epochs,
          ratio=0.1,
          stepsize=1,
          additional_conditions={}):

    # データセットを分割
    x_train, x_valid, y_train, y_valid = split(in_data, out_data, ratio)

    # データ数を確認
    n_samples = len(x_train)
    n_samples_ev = len(x_valid)

    # 入力データタイプの確認
    if type(x_train[0]) == str:
        # 画像の場合は画像サイズ・チャンネル数の情報を取得しておく
        x_type_str = True
        x_channels, x_height, x_width = load_single_image(x_train[0]).shape
        x_size = None
        if 'input_scale' in additional_conditions:
            x_width = round(x_width * additional_conditions['input_scale'])
            x_height = round(x_height * additional_conditions['input_scale'])
            x_size = (x_width, x_height)
        if 'in_channels' in additional_conditions:
            x_channels = additional_conditions['in_channels']
        x_color_mode = 0 if x_channels == 1 else 1
    else:
        # 数値の場合は numpy.ndarray に変換しておく
        x_type_str = False
        x_train = to_nparray(x_train)
        x_valid = to_nparray(x_valid)

    # 出力データタイプも同様にして確認
    if type(y_train[0]) == str:
        y_type_str = True
        y_channels, y_height, y_width = load_single_image(y_train[0]).shape
        y_size = None
        if 'output_scale' in additional_conditions:
            y_width = round(y_width * additional_conditions['output_scale'])
            y_height = round(y_height * additional_conditions['output_scale'])
            y_size = (y_width, y_height)
        if 'out_channels' in additional_conditions:
            y_channels = additional_conditions['out_channels']
        y_color_mode = 0 if y_channels == 1 else 1
    else:
        y_type_str = False
        y_train = to_nparray(y_train)
        y_valid = to_nparray(y_valid)

    # 追加情報の確認
    x_with_noise = False
    y_with_noise = False
    x_noise_points = 0
    y_noise_points = 0
    if 'input_with_noise' in additional_conditions:
        x_with_noise = True
        x_noise_points = additional_conditions['input_with_noise']
    if 'output_with_noise' in additional_conditions:
        y_with_noise = True
        y_noise_points = additional_conditions['output_with_noise']

    # モデルをGPU上に移動
    model = model.to(device)

    # オプティマイザーの用意
    optimizer = optim.Adam(model.parameters())

    # 学習処理ループ
    for e in range(epochs):

        # 現在のエポック番号を表示
        print('Epoch {0}'.format(e + 1), file=sys.stderr)

        # 損失関数の値が小さくなるようにモデルパラメータを更新
        model.train()
        n_input = 0
        sum_loss = 0
        perm = np.random.permutation(n_samples)
        for i in range(0, n_samples, batchsize * stepsize):
            model.zero_grad()
            # ミニバッチ(入力側)を作成
            if x_type_str:
                x = torch.tensor(load_images(x_train,
                                             ids=perm[i:i + batchsize],
                                             size=x_size,
                                             with_noise=x_with_noise,
                                             n_noise_points=x_noise_points,
                                             mode=x_color_mode),
                                 device=device)
            elif x_train.dtype == np.int32:
                x = torch.tensor(x_train[perm[i:i + batchsize]],
                                 device=device,
                                 dtype=torch.long)
            else:
                x = torch.tensor(x_train[perm[i:i + batchsize]], device=device)
            # ミニバッチ(出力側)を作成
            if y_type_str:
                y = torch.tensor(load_images(y_train,
                                             ids=perm[i:i + batchsize],
                                             size=y_size,
                                             with_noise=y_with_noise,
                                             n_noise_points=y_noise_points,
                                             mode=y_color_mode),
                                 device=device)
            elif y_train.dtype == np.int32:
                y = torch.tensor(y_train[perm[i:i + batchsize]],
                                 device=device,
                                 dtype=torch.long)
            else:
                y = torch.tensor(y_train[perm[i:i + batchsize]], device=device)
            loss = loss_func(model(x), y)
            loss.backward()
            optimizer.step()
            sum_loss += float(loss) * len(x)
            n_input += len(x)
            del loss
            del y
            del x

        # 損失関数の現在値を表示
        print('  train loss = {0:.6f}'.format(sum_loss / n_input),
              file=sys.stderr)

        # 検証用データを用いた場合の損失を計算・表示
        model.eval()
        n_input = 0
        n_failed = 0
        sum_loss = 0
        perm = np.arange(0, n_samples_ev)
        for i in range(0, n_samples_ev, batchsize):
            # ミニバッチ(入力側)を作成
            if x_type_str:
                x = torch.tensor(load_images(x_valid,
                                             ids=perm[i:i + batchsize],
                                             size=x_size,
                                             with_noise=x_with_noise,
                                             n_noise_points=x_noise_points,
                                             mode=x_color_mode),
                                 device=device)
            elif x_valid.dtype == np.int32:
                x = torch.tensor(x_valid[i:i + batchsize],
                                 device=device,
                                 dtype=torch.long)
            else:
                x = torch.tensor(x_valid[i:i + batchsize], device=device)
            # ミニバッチ(出力側)を作成
            calc_accuracy = False
            if y_type_str:
                y = torch.tensor(load_images(y_valid,
                                             ids=perm[i:i + batchsize],
                                             size=y_size,
                                             with_noise=y_with_noise,
                                             n_noise_points=y_noise_points,
                                             mode=y_color_mode),
                                 device=device)
            elif y_valid.dtype == np.int32:
                y = torch.tensor(y_valid[i:i + batchsize],
                                 device=device,
                                 dtype=torch.long)
                calc_accuracy = True
            else:
                y = torch.tensor(y_valid[i:i + batchsize], device=device)
            z = model(x)
            loss = loss_func(z, y)
            sum_loss += float(loss) * len(x)
            n_input += len(x)
            if calc_accuracy:
                y_cpu = y.to('cpu').detach().numpy().copy()
                z_cpu = z.to('cpu').detach().numpy().copy()
                n_failed += np.count_nonzero(np.argmax(z_cpu, axis=1) - y_cpu)
                del y_cpu
                del z_cpu
            if y_type_str and i == 0:
                if e == 0:
                    x_cpu = x.to('cpu').detach().numpy().copy()
                    y_cpu = y.to('cpu').detach().numpy().copy()
                    save_progress(model_dir + 'input.png',
                                  x_cpu,
                                  n_data_max=25,
                                  n_data_per_row=5,
                                  mode=x_color_mode)
                    save_progress(model_dir + 'ground_truth.png',
                                  y_cpu,
                                  n_data_max=25,
                                  n_data_per_row=5,
                                  mode=y_color_mode)
                    del x_cpu
                    del y_cpu
                z_cpu = z.to('cpu').detach().numpy().copy()
                save_progress(model_dir + 'output_ep{0}.png'.format(e + 1),
                              z_cpu,
                              n_data_max=25,
                              n_data_per_row=5,
                              mode=y_color_mode)
                del z_cpu
            del loss
            del z
            del y
            del x
        print('  valid loss = {0:.6f}'.format(sum_loss / n_input),
              file=sys.stderr)
        if calc_accuracy:
            acc = (n_samples_ev - n_failed) / n_samples_ev
            print('  accuracy = {0:.2f}%'.format(100 * acc), file=sys.stderr)

        print('', file=sys.stderr)

    return model.to('cpu')
Example #6
0
    # 損失関数の定義
    loss_func = nn.MSELoss()  # mean squared error

    # オプティマイザーの用意
    optimizer = optim.Adam(model.parameters())

    # 学習処理ループ
    perm = np.random.permutation(n_samples_ev)
    g_gray = load_images(imgfiles_ev, ids=perm[:batchsize],
                         mode=0)  # 評価用データの一部をグレースケール画像として読み込む
    g_color = load_images(imgfiles_ev, ids=perm[:batchsize],
                          mode=1)  # 評価用データの一部をカラー画像として読み込む
    save_progress(MODEL_DIR + 'input.png',
                  g_gray,
                  n_data_max=25,
                  n_data_per_row=5,
                  mode=0)  # 確認用に評価用データ(グレースケール版)を保存
    save_progress(MODEL_DIR + 'original.png',
                  g_color,
                  n_data_max=25,
                  n_data_per_row=5,
                  mode=1)  # 比較用に評価用データ(カラー版)を保存
    for e in range(epochs):

        # 現在のエポック番号を表示
        print('Epoch {0}'.format(e + 1), file=sys.stderr)

        # 損失関数の値が小さくなるように識別器のパラメータを更新
        model.train()
        n_input = 0
Example #7
0
File: main.py Project: JunxuLiu/PFA
def main(unused_argv):

  print(FLAGS.model)
  project_path = os.getcwd()
  # load dataset
  x_train, y_train, x_test, y_test = load_dataset(FLAGS.dataset, project_path)
  print('x_train:{} y_train:{} / x_test:{}, y_test:{}'.format(len(x_train), len(y_train), len(x_test), len(y_test)))
  # split data
  client_set_path = os.path.join(project_path, 
                                 'dataset', FLAGS.dataset, \
                                 'clients', \
                                 ('noniid' if FLAGS.noniid else 'iid'), \
                                 'v{}'.format(FLAGS.version))
  #client_set_path = project_path + '/dataset/' + FLAGS.dataset + '/clients/' + ('noniid' if FLAGS.noniid else 'iid')
  client_dataset_size = len(x_train) // FLAGS.N if FLAGS.client_dataset_size is None else FLAGS.client_dataset_size
  if not FLAGS.noniid:
    client_set = create_iid_clients(FLAGS.N, len(x_train), 10, client_dataset_size, client_set_path)
  else:  
    client_set = create_noniid_clients(FLAGS.N, len(x_train), 10, client_dataset_size, client_set_path)
  print('client dataset size: {}'.format(len(client_set[0])))

  COMM_ROUND = int(FLAGS.max_steps / FLAGS.local_steps)
  print('communication rounds:{}'.format(COMM_ROUND))

  # set personalized privacy budgets  
  if FLAGS.dpsgd:
    if (FLAGS.eps == 'epsilons' or FLAGS.eps == 'epsilonsu'):
        epsilons, threshold = set_epsilons(FLAGS.eps, FLAGS.N, is_distributions = False)
    else:
        epsilons, threshold = set_epsilons(FLAGS.eps, FLAGS.N, is_distributions = True)

    print('epsilons:{}, \nthreshold:{}'.format(epsilons, threshold))

    noise_multiplier = []
    for i in range(FLAGS.N):
      q = FLAGS.client_batch_size / len(client_set[i])
      nm = 10 * q * math.sqrt(FLAGS.max_steps * FLAGS.sample_ratio * (-math.log10(FLAGS.delta))) / epsilons[i]
      noise_multiplier.append(nm)
    print('noise_multiplier:', noise_multiplier)
    budgets_accountant = BudgetsAccountant(FLAGS.N, epsilons, FLAGS.delta, noise_multiplier, FLAGS.local_steps, threshold)


  if FLAGS.sample_mode is None:
    m = FLAGS.N
  else:
    m = int(FLAGS.sample_ratio * FLAGS.N)
  print('number of clients per round: {}'.format(m))
 
  start_time = time.time()

  accuracy_accountant = []
  # define tensors and operators in the graph 'g_c'
  with tf.Graph().as_default():
    # build model
    if FLAGS.dpsgd:
      train_op_list, eval_op, loss, data_placeholder, labels_placeholder = nets.mnist_model(FLAGS, \
                epsilons, noise_multiplier)
    else:
      train_op_list, eval_op, loss, data_placeholder, labels_placeholder = nets.mnist_model(FLAGS)

    # increase and set global step
    increase_global_step, set_global_step = global_step_creator()

    # dict, each key-value pair corresponds to the placeholder_name of each tf.trainable_variables
    # and its placeholder.
    # trainable_variables: the placeholder name corresponding to each tf.trainable variable.
    model_placeholder = dict(zip([Vname_to_FeedPname(var) for var in tf.trainable_variables()],
                                 [tf.placeholder(name=Vname_to_Pname(var),
                                                 shape=var.get_shape(),
                                                 dtype=tf.float32)
                                  for var in tf.trainable_variables()]))

    # all trainable variables are set to the value specified through
    # the placeholders in 'model_placeholder'.
    assignments = [tf.assign(var, model_placeholder[Vname_to_FeedPname(var)]) for var in
                   tf.trainable_variables()]

    #init = tf.global_variables_initializer()
    with tf.Session(config=tf.ConfigProto(
            log_device_placement=False, \
            allow_soft_placement=True, \
            gpu_options=tf.GPUOptions(allow_growth=True))) as sess:

      #sess.run(tf.global_variables_initializer())
      sess.run(tf.initialize_all_variables())
      # initial global model and errors
      model = dict(zip([Vname_to_FeedPname(var) for var in tf.trainable_variables()],
                       [sess.run(var) for var in tf.trainable_variables()]))
      model['global_step_placeholder:0'] = 0
      #errors = [ np.zeros(var.get_shape()) for var in tf.trainable_variables()]
      errors = list(model.values()) if FLAGS.error_feedback else [0]*len(tf.trainable_variables())
      #server.set_global_model(model)

      # initial server aggregation
      #w = weights if FLAGS.wei_avg else None
      server = ServerAggregation(model, FLAGS.dpsgd, FLAGS.projection, FLAGS.proj_dims, FLAGS.lanczos_iter, FLAGS.wei_avg)

      # initial local update
      local = LocalUpdate(x_train, y_train, client_set, FLAGS.client_batch_size, data_placeholder, labels_placeholder)

      for r in range(COMM_ROUND):
        print_new_comm_round(r)
        comm_start_time = time.time()
        # select the participating clients
        if FLAGS.dpsgd:
          participating_clients = sampling(FLAGS.N, m, client_set, FLAGS.client_batch_size, \
                                           FLAGS.sample_mode, budgets_accountant)
        else:
          participating_clients = range(FLAGS.N) # temporary

        # if the condition of training cannot be satisfied. (no public clients or no sufficient candidates.
        if not len(participating_clients):
          print("the condition of training cannot be satisfied. (no public clients or no sufficient candidates.")
          print('Done! The procedure time:', time.time() - start_time)
          break
        print(participating_clients)

        ############################################################################################################
        # For each client c (out of the m chosen ones):
        for c in participating_clients:
        
          #########################################################################################################
          # Start local update
          # Setting the trainable Variables in the graph to the values stored in feed_dict 'model'
          #sess.run(assignments, feed_dict=model)
          update = local.update(sess, assignments, c, model, FLAGS.local_steps, train_op_list[c])
          server.aggregate(c, update, is_public = (c in budgets_accountant._public if FLAGS.dpsgd else True))

          if FLAGS.dpsgd:
            print('For client %d and delta=%f, the budget is %f and the used budget is: %f' %
               (c, float(FLAGS.delta), epsilons[c], budgets_accountant.get_accumulation(c)))
          #print('local update procedure time:', time.time() - start_time)
          # End of the local update
          ############################################################################################################

        # average and update the global model, apply_gradients(grads_and_vars, global_step)
        if FLAGS.fedavg:
          n_clients = len(participating_clients)
          w = np.array([1/n_clients] * n_clients)
          print(w)
        elif FLAGS.wei_avg:
          epsSubset = np.array(epsilons)[participating_clients]
          eps_sum = sum(epsSubset)
          w = np.array([eps/eps_sum for eps in epsSubset])
          print(epsSubset, w)
        else:
          w = None

        if FLAGS.error_feedback:
          model, errors = server.fedavg(model, errors, w)
        else:
          model = server.fedavg(model, None, w)

        # Setting the trainable Variables in the graph to the values stored in feed_dict 'model'
        sess.run(assignments + [increase_global_step], feed_dict=model)

        # validate the (current) global model using validation set.
        # create a feed-dict holding the validation set.
        feed_dict = {str(data_placeholder.name): x_test,
                     str(labels_placeholder.name): y_test}

        # compute the loss on the validation set.
        global_loss = sess.run(loss, feed_dict=feed_dict)
        count = sess.run(eval_op, feed_dict=feed_dict)
        accuracy = float(count) / float(len(y_test))
        accuracy_accountant.append(accuracy)
        print_loss_and_accuracy(global_loss, accuracy, stage='test')
        print('time of one communication:', time.time() - comm_start_time)        
        if FLAGS.dpsgd:
          save_progress(FLAGS, model, accuracy_accountant, budgets_accountant.get_global_budget())
        else:
          save_progress(FLAGS, model, accuracy_accountant)
        

    print('Done! The procedure time:', time.time() - start_time)
Example #8
0
    # 次元圧縮に用いるオートエンコーダの作成
    model = myAutoEncoder(width, height, channels, dimension)
    model = model.to(dev)

    # 損失関数の定義
    loss_func = nn.L1Loss()  # mean absolute error

    # オプティマイザーの用意
    optimizer = optim.Adam(model.parameters())

    # 学習処理ループ
    perm = np.random.permutation(n_samples_ev)
    g = load_images(imgfiles_ev, ids=perm[:batchsize],
                    mode=color_mode)  # 評価用データを読み込む
    save_progress(MODEL_DIR + 'original.png', g,
                  mode=color_mode)  # 比較用に評価用データを保存
    for e in range(epochs):

        # 現在のエポック番号を表示
        print('Epoch {0}'.format(e + 1), file=sys.stderr)

        # 損失関数の値が小さくなるように識別器のパラメータを更新
        model.train()
        n_input = 0
        sum_loss = 0
        perm = np.random.permutation(n_samples)
        for i in range(0, n_samples,
                       batchsize * 10):  # 高速化のため,ミニバッチ10個につき1個しか学習に用いないことにする
            model.zero_grad()
            x = torch.tensor(load_images(imgfiles,
                                         ids=perm[i:i + batchsize],
Example #9
0
def stylize(network,
            semantic_transfer,
            initial,
            content,
            style,
            mask,
            sem_style_images,
            gradient_capping,
            capped_objs,
            auto_tuning,
            erosion,
            preserve_colors,
            iterations,
            content_weight,
            style_weight,
            tv_weight,
            learning_rate,
            print_iterations=None,
            checkpoint_iterations=None):
    """
    Stylize images.

    This function yields tuples (iteration, image); `iteration` is None
    if this is the final image (the last iteration).  Other tuples are yielded
    every `checkpoint_iterations` iterations.

    :rtype: iterator[tuple[int|None,image]]
    """

    t = time.time()

    # Load network
    vgg_weights, vgg_mean_pixel = vgg.load_net(network)

    # Dictionaries = features maps for each considered layers
    content_features = {}
    if semantic_transfer:
        style_semantic_features = [{} for _ in sem_style_images]
        guidance_maps = [{} for _ in mask]
        ratio = []  # Auto tuning
        net_gradient = []  # For Gradient Capping
    else:
        style_features = {}

    # Batch
    shape = (1, ) + content.shape

    # To vizualize the loss curves
    if SAVE_ITERATIONS:
        loss_sheet = []

    style_layers_weights = ops.compute_style_layers_weight(
        weight_scheme, STYLE_LAYERS, STYLE_LAYER_WEIGHT_EXP)

    # Content features of content image
    g = tf.Graph()
    with g.as_default(), g.device('/cpu:0'), tf.Session() as sess:
        image = tf.placeholder('float', shape=shape)
        net = vgg.net_preloaded(vgg_weights, image, POOLING)
        content_pre = np.array([vgg.preprocess(content, vgg_mean_pixel)])

        for layer in CONTENT_LAYERS:
            content_features[layer] = net[layer].eval(
                feed_dict={image: content_pre})

    # Style features of style images
    g = tf.Graph()
    with g.as_default(), g.device('/cpu:0'), tf.Session() as sess:
        image = tf.placeholder('float', shape=shape)
        net = vgg.net_preloaded(vgg_weights, image, POOLING)

        # Guided Gram Matrices (Semantic style transfer)
        if semantic_transfer:
            # Downsample guidance channels
            ops.down_sample_guidance_channels(mask, auto_tuning, erosion, net,
                                              STYLE_LAYERS, guidance_maps,
                                              ratio)
            for idx, img in enumerate(sem_style_images):
                style_pre = np.array([vgg.preprocess(img, vgg_mean_pixel)])

                for layer in STYLE_LAYERS:
                    features = net[layer].eval(feed_dict={image: style_pre})
                    features = features * guidance_maps[idx][layer]
                    features = np.reshape(features, (-1, features.shape[3]))
                    features = features - 1
                    gram = np.matmul(features.T, features)
                    style_semantic_features[idx][layer] = gram

        # Gram Matrices (Whole style transfer)
        else:
            style_pre = np.array([vgg.preprocess(style, vgg_mean_pixel)])

            for layer in STYLE_LAYERS:
                features = net[layer].eval(feed_dict={image: style_pre})
                features = np.reshape(features, (-1, features.shape[3]))
                features = features - 1
                gram = np.matmul(features.T, features) / features.size
                style_features[layer] = gram

    # Initial noise
    initial_content_noise_coeff = 1.0 - INITIAL_NOISEBLEND

    # Optimization
    with tf.Graph().as_default():

        # Initialisation
        if initial is None:
            initial = tf.random_normal(shape) * 0.256
        else:
            initial = np.array([vgg.preprocess(initial, vgg_mean_pixel)])
            initial = initial.astype('float32')
            initial = initial * initial_content_noise_coeff + (
                tf.random_normal(shape) *
                0.256) * (1.0 - initial_content_noise_coeff)

        image = tf.Variable(initial)

        # Content loss
        net = vgg.net_preloaded(vgg_weights, image, POOLING)
        content_layers_weights = {}
        content_layers_weights['relu4_2'] = CONTENT_WEIGHT_BLEND
        content_layers_weights['relu5_2'] = 1.0 - CONTENT_WEIGHT_BLEND
        content_loss = 0
        content_losses = []
        for content_layer in CONTENT_LAYERS:
            content_losses.append(
                content_layers_weights[content_layer] * content_weight *
                (2 * tf.nn.l2_loss(net[content_layer] -
                                   content_features[content_layer]) /
                 content_features[content_layer].size))
        content_loss += reduce(tf.add, content_losses)

        style_loss = 0
        style_losses = []

        # Semantic Style Loss
        if semantic_transfer:

            for i in range(len(sem_style_images)):

                segmented_obj = guidance_maps[i]

                if gradient_capping:
                    if capped_objs[i] == 1:
                        mask_tmp = np.expand_dims(mask[i], axis=0)
                        mask_tmp = np.expand_dims(mask_tmp, axis=3)
                        image_tmp = image * tf.stop_gradient(
                            tf.convert_to_tensor(mask_tmp, dtype=tf.float32))
                        net_gradient.append(
                            vgg.net_preloaded(vgg_weights, image_tmp, POOLING))
                    else:
                        net_gradient.append(net)

                for idx, style_layer in enumerate(STYLE_LAYERS):
                    if gradient_capping:
                        layer = net_gradient[i][style_layer]
                    else:
                        layer = net[style_layer]

                    _, height, width, number = map(lambda i: i.value,
                                                   layer.get_shape())
                    size = number
                    feats = layer * segmented_obj[style_layer]

                    # Gram of the stylized image
                    feats = tf.reshape(feats, (-1, number))
                    feats = feats - 1
                    gram = tf.matmul(tf.transpose(feats), feats)

                    # Precomputed Gram of the style image
                    style_gram = style_semantic_features[i][style_layer]
                    style_losses.append(style_layers_weights[style_layer] * 2 *
                                        tf.nn.l2_loss(gram - style_gram) /
                                        (2 * size**2))

                style_loss += style_weight * reduce(tf.add,
                                                    style_losses) * ratio[i]

        # Full Style Loss
        else:
            for style_layer in STYLE_LAYERS:
                layer = net[style_layer]
                _, height, width, number = map(lambda i: i.value,
                                               layer.get_shape())
                size = height * width * number  # Ml * Nl
                feats = tf.reshape(layer, (-1, number))
                feats = feats - 1
                gram = tf.matmul(tf.transpose(feats), feats) / size
                style_gram = style_features[style_layer]
                style_losses.append(style_layers_weights[style_layer] * 2 *
                                    tf.nn.l2_loss(gram - style_gram) /
                                    style_gram.size)
            style_loss += style_weight * reduce(tf.add, style_losses)

        # Regularization Loss
        tv_loss = ops.regularization_loss(image, tv_weight)

        # Total Loss
        loss = content_loss + style_loss + tv_loss

        # Optimizer
        train_step = tf.train.AdamOptimizer(learning_rate, BETA1, BETA2,
                                            EPSILON).minimize(loss)

        # best is the image returned after optimization
        best_loss = float('inf')
        best = None

        # Optimization
        with tf.Session() as sess:
            sess.run(tf.global_variables_initializer())
            stderr.write('Optimization started...\n')

            # Print the progress for every print_iterations
            if (print_iterations and print_iterations != 0):
                utils.print_progress(content_loss.eval(), style_loss.eval(),
                                     tv_loss.eval(), loss.eval())

            # Optimize + print loss + return final image
            for i in range(iterations):
                train_step.run()
                last_step = (i == iterations - 1)

                if print_iterations and i % print_iterations == 0:
                    utils.print_progress(content_loss.eval(),
                                         style_loss.eval(), tv_loss.eval(),
                                         loss.eval())

                if SAVE_ITERATIONS and i % SAVE_ITERATIONS == 0:
                    utils.save_progress(i,
                                        time.time() - t, style_loss.eval(),
                                        content_loss.eval(), loss_sheet)

                if last_step:
                    utils.print_progress(content_loss.eval(),
                                         style_loss.eval(), tv_loss.eval(),
                                         loss.eval())
                    if SAVE_ITERATIONS:
                        utils.save_progress(i,
                                            time.time() - t, style_loss.eval(),
                                            content_loss.eval(), loss_sheet)
                        pyexcel.save_as(records=loss_sheet,
                                        dest_file_name="loss.csv")

                if (checkpoint_iterations
                        and i % checkpoint_iterations == 0) or last_step:

                    this_loss = loss.eval()
                    if this_loss < best_loss:
                        best_loss = this_loss
                        best = image.eval()

                    img_out = vgg.unprocess(best.reshape(shape[1:]),
                                            vgg_mean_pixel)

                    # Color preservation
                    if preserve_colors and preserve_colors == True:
                        img_out = colors.preserve_colors(content, img_out)

                    yield ((None if last_step else i), img_out)
Example #10
0
    loss_func = nn.L1Loss()  # mean absolute error

    # オプティマイザーの用意
    optimizer = optim.Adam(model.parameters())

    # 学習処理ループ
    perm = np.random.permutation(n_samples_ev)
    g_input = load_images(imgfiles_ev,
                          ids=perm[:batchsize],
                          size=in_size,
                          mode=color_mode)  # 評価用データを半分のサイズにリサイズして読み込む
    g_origin = load_images(imgfiles_ev, ids=perm[:batchsize],
                           mode=color_mode)  # 評価用データを読み込む
    save_progress(MODEL_DIR + 'input.png',
                  g_input,
                  n_data_max=25,
                  n_data_per_row=5,
                  mode=color_mode)
    save_progress(MODEL_DIR + 'original.png',
                  g_origin,
                  n_data_max=25,
                  n_data_per_row=5,
                  mode=color_mode)
    for e in range(epochs):

        # 現在のエポック番号を表示
        print('Epoch {0}'.format(e + 1), file=sys.stderr)

        # 損失関数の値が小さくなるように識別器のパラメータを更新
        model.train()
        n_input = 0