Train / Finetune a model.
| 178 | |
| 179 | // Train / Finetune a model. |
| 180 | int train() { |
| 181 | CHECK_GT(FLAGS_solver.size(), 0) << "Need a solver definition to train."; |
| 182 | CHECK(!FLAGS_snapshot.size() || !FLAGS_weights.size()) |
| 183 | << "Give a snapshot to resume training or weights to finetune " |
| 184 | "but not both."; |
| 185 | vector<string> stages = get_stages_from_flags(); |
| 186 | |
| 187 | caffe::SolverParameter solver_param; |
| 188 | caffe::ReadSolverParamsFromTextFileOrDie(FLAGS_solver, &solver_param); |
| 189 | |
| 190 | solver_param.mutable_train_state()->set_level(FLAGS_level); |
| 191 | for (int i = 0; i < stages.size(); i++) { |
| 192 | solver_param.mutable_train_state()->add_stage(stages[i]); |
| 193 | } |
| 194 | |
| 195 | // If the gpus flag is not provided, allow the mode and device to be set |
| 196 | // in the solver prototxt. |
| 197 | if (FLAGS_gpu.size() == 0 |
| 198 | && solver_param.solver_mode() == caffe::SolverParameter_SolverMode_GPU) { |
| 199 | if (solver_param.has_device_id()) { |
| 200 | FLAGS_gpu = "" + |
| 201 | boost::lexical_cast<string>(solver_param.device_id()); |
| 202 | } else { // Set default GPU if unspecified |
| 203 | FLAGS_gpu = "" + boost::lexical_cast<string>(0); |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | vector<int> gpus; |
| 208 | get_gpus(&gpus); |
| 209 | if (gpus.size() == 0) { |
| 210 | LOG(INFO) << "Use CPU."; |
| 211 | Caffe::set_mode(Caffe::CPU); |
| 212 | } else { |
| 213 | ostringstream s; |
| 214 | for (int i = 0; i < gpus.size(); ++i) { |
| 215 | s << (i ? ", " : "") << gpus[i]; |
| 216 | } |
| 217 | LOG(INFO) << "Using GPUs " << s.str(); |
| 218 | #ifndef CPU_ONLY |
| 219 | cudaDeviceProp device_prop; |
| 220 | for (int i = 0; i < gpus.size(); ++i) { |
| 221 | cudaGetDeviceProperties(&device_prop, gpus[i]); |
| 222 | LOG(INFO) << "GPU " << gpus[i] << ": " << device_prop.name; |
| 223 | } |
| 224 | #endif |
| 225 | solver_param.set_device_id(gpus[0]); |
| 226 | Caffe::SetDevice(gpus[0]); |
| 227 | Caffe::set_mode(Caffe::GPU); |
| 228 | Caffe::set_solver_count(gpus.size()); |
| 229 | } |
| 230 | |
| 231 | caffe::SignalHandler signal_handler( |
| 232 | GetRequestedAction(FLAGS_sigint_effect), |
| 233 | GetRequestedAction(FLAGS_sighup_effect)); |
| 234 | |
| 235 | shared_ptr<caffe::Solver<float> > |
| 236 | solver(caffe::SolverRegistry<float>::CreateSolver(solver_param)); |
| 237 |
nothing calls this directly
no test coverage detected