Skip to content

Commit

Permalink
Merge pull request #4737 from rokm/matcaffe-individual-destruct
Browse files Browse the repository at this point in the history
[matcaffe] allow destruction of individual networks and solvers
  • Loading branch information
shelhamer authored Feb 17, 2017
2 parents 25dfdfb + 2f55f42 commit 475d619
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
3 changes: 3 additions & 0 deletions matlab/+caffe/Net.m
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@
self.layer_names = self.attributes.layer_names;
self.blob_names = self.attributes.blob_names;
end
function delete (self)
caffe_('delete_net', self.hNet_self);
end
function layer = layers(self, layer_name)
CHECK(ischar(layer_name), 'layer_name must be a string');
layer = self.layer_vec(self.name2layer_index(layer_name));
Expand Down
3 changes: 3 additions & 0 deletions matlab/+caffe/Solver.m
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@
self.test_nets(n) = caffe.Net(self.attributes.hNet_test_nets(n));
end
end
function delete (self)
caffe_('delete_solver', self.hSolver_self);
end
function iter = iter(self)
iter = caffe_('solver_get_iter', self.hSolver_self);
end
Expand Down
24 changes: 24 additions & 0 deletions matlab/+caffe/private/caffe_.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,17 @@ static void get_solver(MEX_ARGS) {
mxFree(solver_file);
}

// Usage: caffe_('delete_solver', hSolver)
static void delete_solver(MEX_ARGS) {
mxCHECK(nrhs == 1 && mxIsStruct(prhs[0]),
"Usage: caffe_('delete_solver', hSolver)");
Solver<float>* solver = handle_to_ptr<Solver<float> >(prhs[0]);
solvers_.erase(std::remove_if(solvers_.begin(), solvers_.end(),
[solver] (const shared_ptr< Solver<float> > &solverPtr) {
return solverPtr.get() == solver;
}), solvers_.end());
}

// Usage: caffe_('solver_get_attr', hSolver)
static void solver_get_attr(MEX_ARGS) {
mxCHECK(nrhs == 1 && mxIsStruct(prhs[0]),
Expand Down Expand Up @@ -271,6 +282,17 @@ static void get_net(MEX_ARGS) {
mxFree(phase_name);
}

// Usage: caffe_('delete_solver', hSolver)
static void delete_net(MEX_ARGS) {
mxCHECK(nrhs == 1 && mxIsStruct(prhs[0]),
"Usage: caffe_('delete_solver', hNet)");
Net<float>* net = handle_to_ptr<Net<float> >(prhs[0]);
nets_.erase(std::remove_if(nets_.begin(), nets_.end(),
[net] (const shared_ptr< Net<float> > &netPtr) {
return netPtr.get() == net;
}), nets_.end());
}

// Usage: caffe_('net_get_attr', hNet)
static void net_get_attr(MEX_ARGS) {
mxCHECK(nrhs == 1 && mxIsStruct(prhs[0]),
Expand Down Expand Up @@ -522,12 +544,14 @@ struct handler_registry {
static handler_registry handlers[] = {
// Public API functions
{ "get_solver", get_solver },
{ "delete_solver", delete_solver },
{ "solver_get_attr", solver_get_attr },
{ "solver_get_iter", solver_get_iter },
{ "solver_restore", solver_restore },
{ "solver_solve", solver_solve },
{ "solver_step", solver_step },
{ "get_net", get_net },
{ "delete_net", delete_net },
{ "net_get_attr", net_get_attr },
{ "net_forward", net_forward },
{ "net_backward", net_backward },
Expand Down

0 comments on commit 475d619

Please sign in to comment.