MCPcopy Create free account
hub / github.com/BVLC/caffe / train

Function train

tools/caffe.cpp:180–261  ·  view source on GitHub ↗

Train / Finetune a model.

Source from the content-addressed store, hash-verified

178
179// Train / Finetune a model.
180int 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.has_solver_mode()
199 && solver_param.solver_mode() == caffe::SolverParameter_SolverMode_GPU) {
200 if (solver_param.has_device_id()) {
201 FLAGS_gpu = "" +
202 boost::lexical_cast<string>(solver_param.device_id());
203 } else { // Set default GPU if unspecified
204 FLAGS_gpu = "" + boost::lexical_cast<string>(0);
205 }
206 }
207
208 vector<int> gpus;
209 get_gpus(&gpus);
210 if (gpus.size() == 0) {
211 LOG(INFO) << "Use CPU.";
212 Caffe::set_mode(Caffe::CPU);
213 } else {
214 ostringstream s;
215 for (int i = 0; i < gpus.size(); ++i) {
216 s << (i ? ", " : "") << gpus[i];
217 }
218 LOG(INFO) << "Using GPUs " << s.str();
219#ifndef CPU_ONLY
220 cudaDeviceProp device_prop;
221 for (int i = 0; i < gpus.size(); ++i) {
222 cudaGetDeviceProperties(&device_prop, gpus[i]);
223 LOG(INFO) << "GPU " << gpus[i] << ": " << device_prop.name;
224 }
225#endif
226 solver_param.set_device_id(gpus[0]);
227 Caffe::SetDevice(gpus[0]);
228 Caffe::set_mode(Caffe::GPU);
229 Caffe::set_solver_count(gpus.size());
230 }
231
232 caffe::SignalHandler signal_handler(
233 GetRequestedAction(FLAGS_sigint_effect),
234 GetRequestedAction(FLAGS_sighup_effect));
235
236 shared_ptr<caffe::Solver<float> >
237 solver(caffe::SolverRegistry<float>::CreateSolver(solver_param));

Callers

nothing calls this directly

Calls 12

get_stages_from_flagsFunction · 0.85
get_gpusFunction · 0.85
GetRequestedActionFunction · 0.85
CopyLayersFunction · 0.85
SetActionFunctionMethod · 0.80
GetActionFunctionMethod · 0.80
RestoreMethod · 0.80
sizeMethod · 0.45
getMethod · 0.45
RunMethod · 0.45
SolveMethod · 0.45

Tested by

no test coverage detected