Esempio n. 1
0
def _default_printprogress(self):
    # This just formats all of them nicely together and prints it.
    s_tr = _report_train(self.trcosts[-1], self.trnlls[-1], self.trts[-1])
    s_va = _report_score(
        self.vanlls[-1], self.vaerrs[-1], self.vats[-1],
        self.last_yva_len) if self.vaepochs[-1] == self.e else None
    printnow(self.progressout, "{}\n",
             _report_epoch(self.e, s_tr, s_va, self.e == self.ebest))
Esempio n. 2
0
def compute_precrecs(scans,
                     pred_conf,
                     pred_offs,
                     gt_wcs,
                     gt_was,
                     ts=(1e-5, 1e-3, 0.01, 0.05, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6,
                         0.7, 0.8, 0.9, 0.95, 0.99, 1 - 1e-3, 1 - 1e-5),
                     rs=(0.1, 0.3, 0.5, 0.7, 0.9)):
    gt_all = [wcs + was for wcs, was in zip(gt_wcs, gt_was)]

    if isinstance(rs, (float, int)):
        rs = (rs, )

    mkgt = lambda: np.full((len(ts), len(rs)), np.nan)
    precs, recs = mkgt(), mkgt()
    precs_wc, recs_wc = mkgt(), mkgt()
    precs_wa, recs_wa = mkgt(), mkgt()

    for i, t in enumerate(ts):
        alldets, (wcdets, wadets) = pred2det_comb(scans,
                                                  pred_conf,
                                                  pred_offs,
                                                  thresh=t,
                                                  out_rphi=False)
        for j, r in enumerate(rs):
            precs[i, j], recs[i, j] = u.precrec(alldets,
                                                gt_all,
                                                r,
                                                pred_rphi=False,
                                                gt_rphi=True)
            precs_wc[i, j], recs_wc[i, j] = u.precrec(wcdets,
                                                      gt_wcs,
                                                      r,
                                                      pred_rphi=False,
                                                      gt_rphi=True)
            precs_wa[i, j], recs_wa[i, j] = u.precrec(wadets,
                                                      gt_was,
                                                      r,
                                                      pred_rphi=False,
                                                      gt_rphi=True)
        lbu.printnow(".")
    return (precs, recs), (precs_wc, recs_wc), (precs_wa, recs_wa)
Esempio n. 3
0
    def fit(self,
            Xtr,
            ytr,
            Xva,
            yva,
            epochs,
            lr0,
            lr_decay=None,
            aug=None,
            min_nll_diff=1e-3,
            valid_freq=1,
            full_valid_freq=1,
            savename='/tmp/model',
            shuf=None):

        # This is quite a small hack for percents in the default post-epoch reporting.
        self.last_yva_len = len(yva)

        lr = lr0
        tstart = time.clock()
        try:
            for e in range(epochs):
                # This arrangement is a bit weird.

                # We first validate (and potentially test), then train.
                # This is because `train` computes the nll+cost on the trainset
                # for free as a side-product of training. But it computes the
                # value of it before the weight update.

                do_valid = False
                if self.e % valid_freq == 0:
                    do_valid = True
                    fast = True
                if self.e % full_valid_freq == 0:
                    do_valid = True
                    fast = False

                if do_valid:
                    t0 = time.clock()
                    nll, err = self.valid_epoch(Xva, yva, aug=aug, fast=fast)
                    t1 = time.clock()

                    # Store for posteriority.
                    self.vanlls.append(nll)
                    self.vaerrs.append(err)
                    self.vaepochs.append(self.e)
                    self.vats.append(t1 - t0)

                # Save the model that performs best on the validation set.
                # Best is: "significantly" lower nll.
                if self.vanlls[-1] < self.bestvanll - min_nll_diff:
                    self.ebest = self.e
                    self.bestva = self.vaerrs[-1]
                    self.bestvanll = self.vanlls[-1]
                    save_model(self.optim.model, savename)

                t0 = time.clock()
                kw = dict(aug=aug, shuf=shuf)
                if lr is not None:
                    kw['lrate'] = lr
                cost, nll = self.fit_epoch(Xtr, ytr, **kw)
                t1 = time.clock()

                # Store for posteriority.
                self.trnlls.append(nll)
                self.trcosts.append(cost)
                self.trepochs.append(self.e)
                self.trts.append(t1 - t0)

                # Call whatever is registered as post-epoch callback.
                self.postepoch(self)

                # And only now did we advance an epoch.
                # That is because the cost and nll computed by the training step
                # are those *before* updating the gradient.
                self.e += 1

                if lr is not None and lr_decay is not None:
                    lr *= lr_decay

        except KeyboardInterrupt:
            # Just stop the loop on keyboard interrupt.
            # But also reload the best model we had.
            printnow(self.progressout, "Interrupted.")
            if True:  #input("Reload best model? [y/n]") == 'y':
                load_model(self.optim.model, savename)
                printnow(self.progressout, "Reloaded!")

        tend = time.clock()
        self.t += tend - tstart

        printnow(
            self.progressout,
            'Best in validation@{ebest}e ({t:.1f}s in total, i.e. {spe:.1f}s/e)\n',
            ebest=self.ebest,
            t=tend - tstart,
            spe=(tend - tstart) / e)
Esempio n. 4
0
    args = parser.parse_args()
    print(args.criterion + " criterion will be used")

    if args.criterion == 'cosine':
        crit = BiternionCriterion()
    elif args.criterion == 'von-mises':
        crit = BiternionCriterion(kappa=1)
    else:
        print("ERROR: You specified wrong criterion. Sorry =(")
        sys.exit(1)

    for d in args.modeldir.split(':'):
        sys.path.append(d)
    netlib = import_module(args.net)

    printnow("Loading data from {}\n", args.datadir)
    Xtr, ytr, Xte, yte, nte = prepare_data(args.datadir, netlib)
    ytr = ytr.astype(df.floatX)
    yte = yte.astype(df.floatX)
    printnow("Got {:.2f}k training images after flipping\n", len(Xtr) / 1000.0)

    aug = netlib.mkaug(Xtr, ytr)
    net = netlib.mknet()
    printnow('Network has {:.3f}M params in {} layers\n',
             df.utils.count_params(net) / 1000.0 / 1000.0, len(net.modules))

    print(net[:21].forward(aug.augbatch_train(Xtr[:100])[0]).shape)

    costs = dotrain(net, crit, aug, Xtr, ytr, nepochs=args.epochs)
    print("Costs: {}".format(' ; '.join(map(str, costs))))
Esempio n. 5
0
    def fit(self, Xtr, ytr, Xva, yva, epochs,
            lr0,
            lr_decay=None,
            aug=None,
            min_nll_diff=1e-3,
            valid_freq=1,
            full_valid_freq=1,
            savename='/tmp/model',
            shuf=None):

        # This is quite a small hack for percents in the default post-epoch reporting.
        self.last_yva_len = len(yva)

        lr = lr0
        tstart = time.clock()
        try:
          for e in range(epochs):
            # This arrangement is a bit weird.

            # We first validate (and potentially test), then train.
            # This is because `train` computes the nll+cost on the trainset
            # for free as a side-product of training. But it computes the
            # value of it before the weight update.

            do_valid = False
            if self.e % valid_freq == 0:
                do_valid = True
                fast = True
            if self.e % full_valid_freq == 0:
                do_valid = True
                fast = False

            if do_valid:
                t0 = time.clock()
                nll, err = self.valid_epoch(Xva, yva, aug=aug, fast=fast)
                t1 = time.clock()

                # Store for posteriority.
                self.vanlls.append(nll)
                self.vaerrs.append(err)
                self.vaepochs.append(self.e)
                self.vats.append(t1-t0)

            # Save the model that performs best on the validation set.
            # Best is: "significantly" lower nll.
            if self.vanlls[-1] < self.bestvanll - min_nll_diff:
                self.ebest = self.e
                self.bestva = self.vaerrs[-1]
                self.bestvanll = self.vanlls[-1]
                save_model(self.optim.model, savename)

            t0 = time.clock()
            kw = dict(aug=aug, shuf=shuf)
            if lr is not None:
                kw['lrate'] = lr
            cost, nll = self.fit_epoch(Xtr, ytr, **kw)
            t1 = time.clock()

            # Store for posteriority.
            self.trnlls.append(nll)
            self.trcosts.append(cost)
            self.trepochs.append(self.e)
            self.trts.append(t1-t0)

            # Call whatever is registered as post-epoch callback.
            self.postepoch(self)

            # And only now did we advance an epoch.
            # That is because the cost and nll computed by the training step
            # are those *before* updating the gradient.
            self.e += 1

            if lr is not None and lr_decay is not None:
                lr *= lr_decay

        except KeyboardInterrupt:
            # Just stop the loop on keyboard interrupt.
            # But also reload the best model we had.
            printnow(self.progressout, "Interrupted.")
            if True: #input("Reload best model? [y/n]") == 'y':
                load_model(self.optim.model, savename)
                printnow(self.progressout, "Reloaded!")

        tend = time.clock()
        self.t += tend-tstart

        printnow(self.progressout, 'Best in validation@{ebest}e ({t:.1f}s in total, i.e. {spe:.1f}s/e)\n',
            ebest = self.ebest,
            t = tend - tstart,
            spe = (tend - tstart)/e
        )
Esempio n. 6
0
def _default_printprogress(self):
    # This just formats all of them nicely together and prints it.
    s_tr = _report_train(self.trcosts[-1], self.trnlls[-1], self.trts[-1])
    s_va = _report_score(self.vanlls[-1], self.vaerrs[-1], self.vats[-1], self.last_yva_len) if self.vaepochs[-1] == self.e else None
    printnow(self.progressout, "{}\n", _report_epoch(self.e, s_tr, s_va, self.e == self.ebest))
  args = parser.parse_args()
  print(args.criterion + " criterion will be used")

  if args.criterion == 'cosine':
    crit = BiternionCriterion()
  elif args.criterion == 'von-mises':
    crit = BiternionCriterion(kappa=1)
  else:
    print("ERROR: You specified wrong criterion. Sorry =(")
    sys.exit(1)

  for d in args.modeldir.split(':'):
    sys.path.append(d)
  netlib = import_module(args.net)

  printnow("Loading data from {}\n", args.datadir)
  Xtr, ytr, Xte, yte, nte = prepare_data(args.datadir, netlib)
  ytr = ytr.astype(df.floatX)
  yte = yte.astype(df.floatX)
  printnow("Got {:.2f}k training images after flipping\n", len(Xtr)/1000.0)

  aug = netlib.mkaug(Xtr, ytr)
  net = netlib.mknet()
  printnow('Network has {:.3f}M params in {} layers\n', df.utils.count_params(net)/1000.0/1000.0, len(net.modules))

  print(net[:21].forward(aug.augbatch_train(Xtr[:100])[0]).shape)

  costs = dotrain(net, crit, aug, Xtr, ytr, nepochs=args.epochs)
  print("Costs: {}".format(' ; '.join(map(str, costs))))

  dostats(net, aug, Xtr, batchsize=64)