def Test(self, test_idx): """Test the specific net. Parameters ---------- test_idx : int The idx of test net. Returns ------- None References ---------- The implementation of `Test(solver.cpp, L328)`_. """ from dragon.config import logger test_score = [] output_id = [] test_iter = self._param.test_iter[test_idx] net = self._test_nets[test_idx] for iter in xrange(test_iter): self.tests[test_idx](return_outputs=False) if not root_solver(): continue if iter == 0: for net_output in net._net_outputs: vals = ws.FetchTensor(net.blobs[net_output].data) for idx, val in enumerate(vals): test_score.append(val) output_id.append(net_output) else: i = 0 for net_output in net._net_outputs: vals = ws.FetchTensor(net.blobs[net_output].data) for idx, val in enumerate(vals): test_score[i] += val i += 1 if not root_solver(): return logger.info('Iteration {}, Test net #{}'.format(self._iter, test_idx)) for idx, score in enumerate(test_score): logger.info(' Test net output #%d(%s): %.4f' % (idx, output_id[idx], score / test_iter)) self.scalar_writer.add_summary((output_id[idx], score / test_iter), self._iter)
def __init__(self, prototxt): """Construct a Solver. Parameters ---------- prototxt : str The path of ``.prototxt`` file. Returns ------- Solver The solver. Examples -------- >>> solver = Solver('solver.prototxt') """ self._param = pb.SolverParameter() Parse(open(prototxt, 'r').read(), self._param) self.ParseUpdateParam() self._net = None self._test_nets = [] self._layer_blobs = [] self._iter = self._current_step = 0 self._optimizer = None self.scalar_writer = sw.ScalarSummary() if root_solver() else None self.InitTrainNet() self.InitTestNets() self.BuildNets()
def Test(self, test_idx): """Test the specific net. Parameters ---------- test_idx : int The idx of test net. Returns ------- None References ---------- The implementation of `Test(solver.cpp, L328)`_. """ from dragon.config import logger test_score = [] output_id = [] test_iter = self._param.test_iter[test_idx] net = self._test_nets[test_idx] for iter in xrange(test_iter): self.tests[test_idx](return_outputs=False) if not root_solver(): continue if iter == 0: for net_output in net._net_outputs: vals = ws.FetchTensor(net.blobs[net_output].data) for idx, val in enumerate(vals): test_score.append(val) output_id.append(net_output) else: i = 0 for net_output in net._net_outputs: vals = ws.FetchTensor(net.blobs[net_output].data) for idx, val in enumerate(vals): test_score[i] += val i += 1 if not root_solver(): return logger.info('Iteration {}, Test net #{}'.format(self._iter, test_idx)) for idx, score in enumerate(test_score): logger.info(' Test net output #%d(%s): %.4f' % (idx, output_id[idx], score / test_iter)) self.scalar_writer.add_summary((output_id[idx], score / test_iter), self._iter)
def Test(self, test_idx): """Test the specific net. Parameters ---------- test_idx : int The idx of test net. Returns ------- None References ---------- The implementation of `Test(solver.cpp, L328)`_. """ test_score, output_id = [], [] net = self._test_nets[test_idx] test_iter = self._param.test_iter[test_idx] for iter in range(test_iter): self.tests[test_idx](return_outputs=False) if not root_solver(): continue if iter == 0: for key in net.outputs: values = net.blobs[key].data.get_value().flatten() for idx, value in enumerate(values): test_score.append(value) output_id.append(key) else: i = 0 for key in net.outputs: values = net.blobs[key].data.get_value().flatten() for idx, value in enumerate(values): test_score[i] += value i += 1 if not root_solver(): return print('Iteration {}, Test net #{}'.format(self.iter, test_idx)) for idx, score in enumerate(test_score): print(' Test net output #%d(%s): %.4f' % (idx, output_id[idx], score / test_iter))
def step(self, iters): """Step the train net. [**PyCaffe Style**] Parameters ---------- iters : int The number of iterations to step. Returns ------- None References ---------- The implementation of `Step(solver.cpp, L180)`_. """ from dragon.config import logger start_iter = self._iter; stop_iter = self._iter + iters loss_vec = []; smoothed_loss = 0 tic = time.time() while self._iter < stop_iter: # test if necessary if self._param.test_interval and \ self._iter % self._param.test_interval == 0: if (self._iter == 0 and self._param.test_initialization) or self._iter != 0: for test_id in xrange(len(self.tests)): self.Test(test_id) # forward & backward & compute_loss loss = 0.0 for i in xrange(self._param.iter_size): self.train(return_outputs=False) if root_solver(): for cost in self._net._costs: cost_value = ws.FetchTensor(cost) if cost_value.size == 1: loss += cost_value[0] if root_solver(): loss /= self._param.iter_size if len(loss_vec) < self._param.average_loss: loss_vec.append(loss) smoothed_loss = (smoothed_loss * (len(loss_vec) - 1) + loss) / len(loss_vec); else: idx = (self._iter - start_iter) % self._param.average_loss smoothed_loss += ((loss - loss_vec[idx]) / self._param.average_loss) loss_vec[idx] = loss # apply update self.GetLearningRate() self.update() # display if root_solver() and self._param.display: if self._iter % self._param.display == 0: base_lr = self._optimizer.lr logger.info('Iteration %d, lr = %s, loss = %f, time = %.2fs' % \ (self._iter, str(base_lr), smoothed_loss, time.time() - tic)) tic = time.time() for idx, net_output in enumerate(self._net.outputs): vals = ws.FetchTensor(self._net.blobs[net_output].data) for val in vals: logger.info(' Train net output #{}({}): {}'.format(idx, net_output, val)) self.scalar_writer.add_summary((net_output, val), self._iter) self._iter = self._iter + 1 # snapshot if self._param.snapshot: if self._iter % self._param.snapshot == 0: self.snapshot()
def step(self, iters): """Step the train net. [**PyCaffe Style**] Parameters ---------- iters : int The number of iterations to step. Returns ------- None References ---------- The implementation of `Step(solver.cpp, L180)`_. """ start_iter, stop_iter = self.iter, self.iter + iters loss_vec, smoothed_loss = [], 0. tic = time.time() while self.iter < stop_iter: # Test if necessary if self._param.test_interval and \ self.iter % self._param.test_interval == 0: if (self.iter == 0 and self._param.test_initialization) or self.iter != 0: for test_id in range(len(self.tests)): self.Test(test_id) # Forward && Backward && Compute Loss loss = 0.0 for i in range(self._param.iter_size): self.train(return_outputs=False) if root_solver(): for e in self.net.losses: values = e.get_value().flatten() for v in values: loss += v if root_solver(): loss /= self._param.iter_size if len(loss_vec) < self._param.average_loss: loss_vec.append(loss) smoothed_loss = (smoothed_loss * (len(loss_vec) - 1) + loss) / len(loss_vec) else: idx = (self.iter - start_iter) % self._param.average_loss smoothed_loss += ((loss - loss_vec[idx]) / self._param.average_loss) loss_vec[idx] = loss # Apply Update self.GetLearningRate() self.update() # Display if root_solver() and self._param.display: if self.iter % self._param.display == 0: base_lr = self.optimizer.base_lr print('Iteration %d, lr = %s, loss = %f, time = %.2fs' % \ (self.iter, str(base_lr), smoothed_loss, time.time() - tic)) tic = time.time() for idx, net_output in enumerate(self.net.outputs): values = self.net.blobs[net_output].data.get_value( ).flatten() for v in values: print(' Train net output #{}({}): {}'.format( idx, net_output, v)) # Inc Iterations self.iter = self.iter + 1 # Snapshot if self._param.snapshot: if self.iter % self._param.snapshot == 0: self.snapshot()