Exemplo n.º 1
0
 def test(self):
     y_train, train_mask = self.y_test, self.test_mask
     hidden = forward_hidden(self.adj, self.features, self.weight_hidden, activation=lambda x: np.maximum(x, 0))
     outputs = forward_hidden(self.adj, hidden, self.weight_outputs)
     loss = forward_cross_entropy_loss(outputs, y_train, train_mask)
     loss += self.weight_decay * l2_loss(self.weight_hidden)
     acc = masked_accuracy(outputs, y_train, train_mask)
     return loss, acc
Exemplo n.º 2
0
def compile_model(model,
                  optimizer='adam',
                  loss='bce-dice',
                  threshold=0.5,
                  dice=False,
                  weight_decay=0.0,
                  exclude_bn=True,
                  deep_supervised=False):
    if loss == 'bce':
        _loss = weighted_binary_crossentropy
    elif loss == 'bce-dice':
        _loss = weighted_bce_dice_loss
    elif loss == 'lovasz':
        _loss = weighted_lovasz_hinge
    elif loss == 'lovasz-dice':
        _loss = weighted_lovasz_dice_loss
    elif loss == 'lovasz-inv':
        _loss = weighted_lovasz_hinge_inversed
    elif loss == 'lovasz-double':
        _loss = weighted_lovasz_hinge_double

    if weight_decay != 0.0:
        _l2_loss = l2_loss(weight_decay, exclude_bn)
        loss = lambda true, pred: _loss(true, pred) + _l2_loss
    else:
        loss = _loss

    if optimizer == ('msgd'):
        optimizer = optimizers.SGD(momentum=0.9)

    if not deep_supervised:
        model.compile(optimizer=optimizer,
                      loss=loss,
                      metrics=get_metrics(threshold))
    else:
        loss_pixel = loss_noempty(loss)
        losses = {
            'output_final': loss,
            'output_pixel': loss_pixel,
            'output_image': bce_with_logits
        }
        loss_weights = {
            'output_final': 1.0,
            'output_pixel': 0.5,
            'output_image': 0.1
        }
        metrics = {
            'output_final': get_metrics(threshold),
            'output_pixel': get_metrics(threshold),
            'output_image': accuracy_with_logits
        }
        model.compile(optimizer=optimizer,
                      loss=losses,
                      loss_weights=loss_weights,
                      metrics=metrics)
    return model
Exemplo n.º 3
0
 def train(self):
     # outputs的输出形状(2708,16),即(batch, hidden)
     self.hidden = forward_hidden(self.adj, self.features, self.weight_hidden, activation=lambda x: np.maximum(x, 0))
     # outputs的输出形状(2708,7),即(batch, classes)
     self.outputs = forward_hidden(self.adj, self.hidden, self.weight_outputs)
     # 正向传播过程中的loss
     loss = forward_cross_entropy_loss(self.outputs, self.y_train, self.train_mask)
     # 正则项
     weight_decay_loss = self.weight_decay * l2_loss(self.weight_hidden)
     loss += weight_decay_loss
     # 计算准确率
     acc = masked_accuracy(self.outputs, self.y_train, self.train_mask)
     return loss, acc
Exemplo n.º 4
0
def cross_val_score(estimator_instance,
                    X,
                    y,
                    is_shuffle=False,
                    cv='full',
                    scoring='score',
                    random_state=None,
                    return_mean=False,
                    verbose=False):
    assert ((type(cv) == int and cv > 1) or cv == 'full')
    assert (scoring == 'score' or scoring == 'loss')

    if type(cv) == int:
        assert (cv < len(X))
    if is_shuffle:
        X, y = shuffle(X, y=y, random_state=random_state)
    N = len(X)
    K = N if cv == 'full' else cv

    h = len(X) / float(K)

    scores = []
    losses = []
    for i in range(K):
        s = int(round((i * h)))
        e = int(round((i + 1) * h))

        X_train, Y_train = [], []
        X_train.extend(X[:s])
        X_train.extend(X[e:])
        Y_train.extend(y[:s])
        Y_train.extend(y[e:])

        X_val, Y_val = X[s:e], y[s:e]
        estimator_instance.fit(X_train, Y_train)
        p = estimator_instance.predict(X_val)
        score = official_score(p, Y_val)
        loss = l2_loss(p, Y_val)
        # score = estimator_instance.score(X_val,Y_val)
        scores.append(score)
        losses.append(loss)

    # print(scores)
    if return_mean:
        if scoring == 'score':
            # print(scores)
            std = sqrt(mean(square(minus(scores, mean(scores)))))
            return (sorted(scores)[len(scores) / 2] + mean(scores) -
                    0.5 * std) / 2.0
            # return (sorted(scores)[len(scores)/2] + mean(scores) - std)/2.0
            # return sorted(scores)[len(scores)/2] - std
            # return max(scores)
            # return mean(scores[:len(scores)/2])
            # return mean(sorted(scores)[::-1][:len(scores)/2])
            # return (mean(scores) + max(scores))/2.0
            # return mean(scores)
            # return mean(scores) -0.5*std
        elif scoring == 'loss':
            # return mean(losses)
            std = sqrt(mean(square(minus(losses, mean(losses)))))
            # return mean(losses)
            return ((sorted(losses)[len(losses) / 2] + mean(losses) + std) /
                    2.0)

    else:
        if scoring == 'score':
            return scores
        elif scoring == 'loss':
            return losses
Exemplo n.º 5
0
def early_stoping(estimator,
                  paramaters,
                  X,
                  y,
                  X_val,
                  Y_val,
                  scoring='score',
                  verbose=False):
    assert (scoring == 'score' or scoring == 'loss')

    def paramater_gen(paramaters):
        N = len(paramaters)
        from itertools import product
        value = list(product(*paramaters.values()))
        for v in value:
            yield dict(zip(paramaters.keys(), v))

    max_model = None
    max_parameter = None
    max_score = None
    min_loss = None

    last_score = None
    last_loss = None

    score = None
    loss = None

    for p in paramater_gen(paramaters):
        clf = estimator(**p)
        clf.fit(X, y)

        last_score = score
        last_loss = loss

        score = official_score(Y_val, clf.predict(X_val))
        loss = l2_loss(Y_val, clf.predict(X_val))

        if verbose:
            # print(p,score,loss)
            pass

        if last_loss != None and last_loss < loss:
            return max_model
        if last_score != None and last_score > score:
            return max_model

        if scoring == "score":
            if max_parameter == None or max_score < score:
                max_parameter = p
                max_score = score
                max_model = clf
        if scoring == "loss":
            if max_parameter == None or min_loss > score:
                max_parameter = p
                min_loss = score
                max_model = clf

    if verbose:
        print("max_parameter", max_parameter)

    return max_model
Exemplo n.º 6
0
def main(args, model_params, data_params):
    procname = os.path.basename(args.checkpoint_dir)
    setproctitle.setproctitle('hdrnet_{}'.format(procname))

    log.info('Preparing summary and checkpoint directory {}'.format(
        args.checkpoint_dir))
    if not os.path.exists(args.checkpoint_dir):
        os.makedirs(args.checkpoint_dir)

    tf.set_random_seed(1234)  # Make experiments repeatable

    # Select an architecture
    mdl = getattr(models, args.model_name)

    # Add model parameters to the graph (so they are saved to disk at checkpoint)
    for p in model_params:
        p_ = tf.convert_to_tensor(model_params[p], name=p)
        tf.add_to_collection('model_params', p_)

    # --- Train/Test datasets ---------------------------------------------------
    data_pipe = getattr(dp, args.data_pipeline)
    with tf.variable_scope('train_data'):
        train_data_pipeline = data_pipe(
            args.data_dir,
            shuffle=True,
            batch_size=args.batch_size,
            nthreads=args.data_threads,
            fliplr=args.fliplr,
            flipud=args.flipud,
            rotate=args.rotate,
            random_crop=args.random_crop,
            params=data_params,
            output_resolution=args.output_resolution)
        train_samples = train_data_pipeline.samples

    if args.eval_data_dir is not None:
        with tf.variable_scope('eval_data'):
            eval_data_pipeline = data_pipe(
                args.eval_data_dir,
                shuffle=False,
                batch_size=1,
                nthreads=1,
                fliplr=False,
                flipud=False,
                rotate=False,
                random_crop=False,
                params=data_params,
                output_resolution=args.output_resolution)
            eval_samples = train_data_pipeline.samples
    # ---------------------------------------------------------------------------

    # Training graph
    with tf.name_scope('train'):
        with tf.variable_scope('inference'):
            prediction = mdl.inference(train_samples['lowres_input'],
                                       train_samples['image_input'],
                                       model_params,
                                       is_training=True)
        loss = metrics.l2_loss(train_samples['image_output'], prediction)
        psnr = metrics.psnr(train_samples['image_output'], prediction)

    # Evaluation graph
    if args.eval_data_dir is not None:
        with tf.name_scope('eval'):
            with tf.variable_scope('inference', reuse=True):
                eval_prediction = mdl.inference(eval_samples['lowres_input'],
                                                eval_samples['image_input'],
                                                model_params,
                                                is_training=False)
            eval_psnr = metrics.psnr(eval_samples['image_output'], prediction)

    # Optimizer
    global_step = tf.contrib.framework.get_or_create_global_step()
    with tf.name_scope('optimizer'):
        update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
        updates = tf.group(*update_ops, name='update_ops')
        log.info("Adding {} update ops".format(len(update_ops)))
        with tf.control_dependencies([updates]):
            opt = tf.train.AdamOptimizer(args.learning_rate)
            minimize = opt.minimize(loss,
                                    name='optimizer',
                                    global_step=global_step)

    # # Average loss and psnr for display
    # with tf.name_scope("moving_averages"):
    #   ema = tf.train.ExponentialMovingAverage(decay=0.99)
    #   update_ma = ema.apply([loss, psnr])
    #   loss = ema.average(loss)
    #   psnr = ema.average(psnr)

    # Training stepper operation
    train_op = tf.group(minimize)  # , update_ma)

    # Save a few graphs to tensorboard
    summaries = [
        tf.summary.scalar('loss', loss),
        tf.summary.scalar('psnr', psnr),
        tf.summary.scalar('learning_rate', args.learning_rate),
        tf.summary.scalar('batch_size', args.batch_size),
    ]

    log_fetches = {"step": global_step, "loss": loss, "psnr": psnr}

    # Train config
    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True  # Do not canibalize the entire GPU
    sv = tf.train.Supervisor(logdir=args.checkpoint_dir,
                             save_summaries_secs=args.summary_interval,
                             save_model_secs=args.checkpoint_interval)

    # Train loop
    with sv.managed_session(config=config) as sess:
        sv.loop(args.log_interval, log_hook, (sess, log_fetches))
        last_eval = time.time()
        while True:
            if sv.should_stop():
                log.info("stopping supervisor")
                break
            try:
                step, _ = sess.run([global_step, train_op])
                since_eval = time.time() - last_eval

                if args.eval_data_dir is not None and since_eval > args.eval_interval:
                    log.info("Evaluating on {} images at step {}".format(
                        eval_data_pipeline.nsamples, step))

                    p_ = 0
                    for it in range(eval_data_pipeline.nsamples):
                        p_ += sess.run(eval_psnr)
                    p_ /= eval_data_pipeline.nsamples

                    sv.summary_writer.add_summary(tf.Summary(value=[
                        tf.Summary.Value(tag="psnr/eval", simple_value=p_)
                    ]),
                                                  global_step=step)

                    log.info("  Evaluation PSNR = {:.1f} dB".format(p_))

                    last_eval = time.time()

            except tf.errors.AbortedError:
                log.error("Aborted")
                break
            except KeyboardInterrupt:
                break
        chkpt_path = os.path.join(args.checkpoint_dir, 'on_stop.ckpt')
        log.info("Training complete, saving chkpt {}".format(chkpt_path))
        sv.saver.save(sess, chkpt_path)
        sv.request_stop()
Exemplo n.º 7
0
 def loss(self,X,y):
     y_ = self.predict(X)
     return l2_loss(y,y_)
Exemplo n.º 8
0
def main(args, data_params):
  procname = os.path.basename(args.checkpoint_dir)
  #setproctitle.setproctitle('hdrnet_{}'.format(procname))

  log.info('Preparing summary and checkpoint directory {}'.format(
      args.checkpoint_dir))
  if not os.path.exists(args.checkpoint_dir):
    os.makedirs(args.checkpoint_dir)

  tf.set_random_seed(1234)  # Make experiments repeatable

  # Select an architecture

  # Add model parameters to the graph (so they are saved to disk at checkpoint)

  # --- Train/Test datasets ---------------------------------------------------
  data_pipe = getattr(dp, args.data_pipeline)
  with tf.variable_scope('train_data'):
    train_data_pipeline = data_pipe(
        args.data_dir,
        shuffle=True,
        batch_size=args.batch_size, nthreads=args.data_threads,
        fliplr=args.fliplr, flipud=args.flipud, rotate=args.rotate,
        random_crop=args.random_crop, params=data_params,
        output_resolution=args.output_resolution,scale=args.scale)
    train_samples = train_data_pipeline.samples

    train_samples['high_input'] = Getfilter(5,train_samples['image_input'])

    train_samples['lowres_input1'] = blur(5,train_samples['lowres_input'])
    train_samples['low_input'] = tf.image.resize_images(train_samples['lowres_input1'],
                      [args.output_resolution[0]/args.scale, args.output_resolution[1]/args.scale],
                      method = tf.image.ResizeMethod.BICUBIC)



  if args.eval_data_dir is not None:
    with tf.variable_scope('eval_data'):
      eval_data_pipeline = data_pipe(
          args.eval_data_dir,
          shuffle=False,
          batch_size=1, nthreads=1,
          fliplr=False, flipud=False, rotate=False,
          random_crop=False, params=data_params,
          output_resolution=args.output_resolution,scale=args.scale)
      eval_samples = train_data_pipeline.samples
  # ---------------------------------------------------------------------------
  swaps = np.reshape(np.random.randint(0, 2, args.batch_size), [args.batch_size, 1])
  swaps = tf.convert_to_tensor(swaps)
  swaps = tf.cast(swaps, tf.float32)

  swaps1 = np.reshape(np.random.randint(0, 2, args.batch_size), [args.batch_size, 1])
  swaps1 = tf.convert_to_tensor(swaps1)
  swaps1 = tf.cast(swaps1, tf.float32)

  # Training graph
  with tf.variable_scope('inference'):
    prediction = models.Resnet(train_samples['low_input'],train_samples['high_input'],train_samples['image_input'])
    loss,loss_content,loss_color,loss_filter,loss_texture,loss_tv,discim_accuracy,discim_accuracy1 =\
      metrics.l2_loss(train_samples['image_output'], prediction, swaps, swaps1, args.batch_size)
    psnr = metrics.psnr(train_samples['image_output'], prediction)
    loss_ssim = MultiScaleSSIM(train_samples['image_output'],prediction)


  # Evaluation graph
  if args.eval_data_dir is not None:
    with tf.name_scope('eval'):
      with tf.variable_scope('inference', reuse=True):
        eval_prediction = models.Resnet( eval_samples['low_input'],eval_samples['high_input'],eval_samples['image_input'])
      eval_psnr = metrics.psnr(eval_samples['image_output'], eval_prediction)

  # Optimizer
  model_vars = [v for v in tf.global_variables() if not v.name.startswith("inference/l2_loss/discriminator") or  v.name.startswith("inference/l2_loss/discriminator1")]
  discriminator_vars = [v for v in tf.global_variables() if v.name.startswith("inference/l2_loss/discriminator")]
  discriminator_vars1 = [v for v in tf.global_variables() if v.name.startswith("inference/l2_loss/discriminator1")]

  global_step = tf.contrib.framework.get_or_create_global_step()
  with tf.name_scope('optimizer'):
    update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
    updates = tf.group(*update_ops, name='update_ops')
    log.info("Adding {} update ops".format(len(update_ops)))

    reg_losses = tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES)
    if reg_losses and args.weight_decay is not None and args.weight_decay > 0:
      print("Regularization losses:")
      for rl in reg_losses:
        print(" ", rl.name)
      opt_loss = loss + args.weight_decay*sum(reg_losses)
    else:
      print("No regularization.")
      opt_loss = loss


    with tf.control_dependencies([updates]):
      opt = tf.train.AdamOptimizer(args.learning_rate)
      minimize = opt.minimize(opt_loss, name='optimizer', global_step=global_step,var_list=model_vars)
      minimize1 = opt.minimize(-loss_filter, name='optimizer1', global_step=global_step,var_list=discriminator_vars)
      minimize2 = opt.minimize(-loss_texture, name='optimizer2', global_step=global_step, var_list=discriminator_vars1)


  # Average loss and psnr for display
  with tf.name_scope("moving_averages"):
    ema = tf.train.ExponentialMovingAverage(decay=0.99)
    update_ma = ema.apply([loss,loss_content,loss_color,loss_filter,loss_texture,loss_tv,discim_accuracy,discim_accuracy1,psnr,loss_ssim])
    loss = ema.average(loss)
    loss_content=ema.average(loss_content)
    loss_color=ema.average(loss_color)
    loss_filter=ema.average(loss_filter)
    loss_texture=ema.average(loss_texture)
    loss_tv=ema.average(loss_tv)
    discim_accuracy = ema.average(discim_accuracy)
    discim_accuracy1 = ema.average(discim_accuracy1)
    psnr = ema.average(psnr)
    loss_ssim = ema.average(loss_ssim)

  # Training stepper operation
  train_op = tf.group(minimize,minimize1,minimize2,update_ma)

  # Save a few graphs to tensorboard
  summaries = [
    tf.summary.scalar('loss', loss),
    tf.summary.scalar('loss_content',loss_content),
    tf.summary.scalar('loss_color',loss_color),
    tf.summary.scalar('loss_filter', loss_filter),
    tf.summary.scalar('loss_texture', loss_texture),
    tf.summary.scalar('loss_tv', loss_tv),
    tf.summary.scalar('discim_accuracy',discim_accuracy),
    tf.summary.scalar('discim_accuracy1', discim_accuracy1),
    tf.summary.scalar('psnr', psnr),
    tf.summary.scalar('ssim', loss_ssim),
    tf.summary.scalar('learning_rate', args.learning_rate),
    tf.summary.scalar('batch_size', args.batch_size),
  ]

  log_fetches = {
      "loss_content":loss_content,
      "loss_color":loss_color,
      "loss_filter":loss_filter,
      "loss_texture": loss_texture,
      "loss_tv":loss_tv,
      "discim_accuracy":discim_accuracy,
      "discim_accuracy1": discim_accuracy1,
      "step": global_step,
      "loss": loss,
      "psnr": psnr,
      "loss_ssim":loss_ssim}

  model_vars = [v for v in tf.global_variables() if not v.name.startswith("inference/l2_loss/discriminator" or "inference/l2_loss/discriminator1")]
  discriminator_vars = [v for v in tf.global_variables() if v.name.startswith("inference/l2_loss/discriminator")]
  discriminator_vars1 = [v for v in tf.global_variables() if v.name.startswith("inference/l2_loss/discriminator1")]

  # Train config
  config = tf.ConfigProto()
  config.gpu_options.allow_growth = True  # Do not canibalize the entire GPU

  sv = tf.train.Supervisor(
      local_init_op=tf.initialize_variables(discriminator_vars),
      saver=tf.train.Saver(var_list=model_vars,max_to_keep=100),
      logdir=args.checkpoint_dir,
      save_summaries_secs=args.summary_interval,
      save_model_secs=args.checkpoint_interval)
  # Train loop
  with sv.managed_session(config=config) as sess:
    sv.loop(args.log_interval, log_hook, (sess,log_fetches))
    last_eval = time.time()
    while True:
      if sv.should_stop():
        log.info("stopping supervisor")
        break
      try:
        step, _ = sess.run([global_step, train_op])
        since_eval = time.time()-last_eval

        if args.eval_data_dir is not None and since_eval > args.eval_interval:
          log.info("Evaluating on {} images at step {}".format(
              eval_data_pipeline.nsamples, step))

          p_ = 0
          eval_data_pipeline.nsamples = 3
          for it in range(eval_data_pipeline.nsamples):
            p_ += sess.run(eval_psnr)
          p_ /= eval_data_pipeline.nsamples

          sv.summary_writer.add_summary(tf.Summary(value=[
            tf.Summary.Value(tag="psnr/eval", simple_value=p_)]), global_step=step)

          log.info("  Evaluation PSNR = {:.1f} dB".format(p_))


          last_eval = time.time()

      except tf.errors.AbortedError:
        log.error("Aborted")
        break
      except KeyboardInterrupt:
        break
    chkpt_path = os.path.join(args.checkpoint_dir, 'on_stop.ckpt')
    log.info("Training complete, saving chkpt {}".format(chkpt_path))
    sv.saver.save(sess, chkpt_path)
    sv.request_stop()