我们从Python开源项目中,提取了以下20个代码示例,用于说明如何使用caffe.get_solver()。
def get_solver(folder): s = caffe_pb2.SolverParameter() s.train_net = './%s/proto_train.prototxt'%folder s.snapshot = 10000 s.snapshot_prefix = './%s/'%folder s.max_iter = int(config.MAX_ITERATIONS) s.display = int(config.VALIDATE_INTERVAL) s.type = 'Adam' s.stepsize = int(config.MAX_ITERATIONS*0.2) s.gamma = 0.5 s.lr_policy = "step" s.base_lr = 0.0007 s.momentum = 0.9 s.momentum2 = 0.999 s.weight_decay = 0.000 s.clip_gradients = 10 return s
def get_solver(folder): s = caffe_pb2.SolverParameter() s.train_net = './%s/proto_train.prototxt'%folder s.snapshot = int(config.VALIDATE_INTERVAL) s.snapshot_prefix = './%s/'%folder s.max_iter = int(config.MAX_ITERATIONS) s.display = int(config.VALIDATE_INTERVAL) s.type = 'Adam' s.stepsize = int(config.MAX_ITERATIONS*0.4) s.gamma = 0.5 s.lr_policy = "step" s.base_lr = 0.0007 s.momentum = 0.9 s.momentum2 = 0.999 s.weight_decay = 0.000 s.clip_gradients = 10 return s
def get_solver(folder): s = caffe_pb2.SolverParameter() s.train_net = './%s/proto_train.prototxt'%folder s.snapshot = 10000 s.snapshot_prefix = './%s/'%folder s.max_iter = int(config.MAX_ITERATIONS) s.display = int(config.VALIDATE_INTERVAL) s.type = 'Adam' s.stepsize = int(config.MAX_ITERATIONS*0.4) s.gamma = 0.25 s.lr_policy = "step" s.base_lr = 0.0007 s.momentum = 0.9 s.momentum2 = 0.999 s.weight_decay = 0.000 s.clip_gradients = 10 return s
def create_tiny_yolo_solver(args): solver = caffe.get_solver('models/tiny_solver.prototxt') #solver.restore('backup/yolo_mobile_iter_357000.solverstate') return solver
def create_recognizer_solver(args): solver = caffe.get_solver('models/solver_ctc.prototxt') #solver.restore('backup/recog_iter_195000.solverstate') return solver
def __init__(self, action_space, model=pms.newModel): self.action_space = action_space actionSolver = None actionSolver = caffe.get_solver(pms.actionSolverPath) actionSolver.net.copy_from(model) # test net share weights with train net actionSolver.test_nets[0].share_with(actionSolver.net) self.solver = actionSolver self.targetNet = caffe.Net(pms.actionTestNetPath, model, caffe.TEST)
def train_network(solver_file, num_classes, batch_size, num_iterations, use_gpu=True) : if use_gpu : caffe.set_mode_gpu() else : caffe.set_mode_cpu() solver = caffe.get_solver(solver_file) solver.net.blobs['data'].reshape(batch_size, solver.net.blobs['data'].shape[1], solver.net.blobs['data'].shape[2], solver.net.blobs['data'].shape[3]) solver.net.blobs['target'].reshape(batch_size, solver.net.blobs['target'].shape[1], solver.net.blobs['target'].shape[2], solver.net.blobs['target'].shape[3]) solver.net.reshape() for i in range(num_iterations): data, target = get_data(batch_size, numclasses=num_classes) solver.net.blobs['data'].data[...] = data solver.net.blobs['target'].data[...] = target solver.step(1) output = solver.net.blobs['argmax'].data[...] fig, sub = plt.subplots(ncols=3, figsize=(15, 5)) sub[0].set_title('Input') sub[0].imshow(data[0, 0, :, :]) sub[1].set_title('Ground Truth') sub[1].imshow(np.argmax(target[0, :, :, :], axis=0)) sub[2].set_title('Segmentation') sub[2].imshow(output[0, 0, :, :]) plt.show()
def train(solverPrototxtFilename): ''' Train the CNN model ''' solver = caffe.get_solver(solverPrototxtFilename) solver.solve()
def train(): solver = caffe.get_solver('solver.prototxt') solver.solve()
def __init__(self, hyperparams): self._hyperparams = hyperparams if self._hyperparams['use_gpu']: caffe.set_device(self._hyperparams['gpu_id']) caffe.set_mode_gpu() else: caffe.set_mode_cpu() self.solver = caffe.get_solver(self._hyperparams['solver']) self.X = None self.caffe_iter = 0
def run_solver(self, unit, n_units, arm, disp_interval=100): #print(arm['dir']) caffe.set_device(self.device) caffe.set_mode_gpu() s = caffe.get_solver(arm['solver_file']) if arm['n_iter']>0: prefix=arm['dir']+"/cifar10_data_iter_" s.restore(prefix+str(arm['n_iter'])+".solverstate") s.net.copy_from(prefix+str(arm['n_iter'])+".caffemodel") s.test_nets[0].share_with(s.net) s.test_nets[1].share_with(s.net) start=time.time() if unit=='time': while time.time()-start<n_units: s.step(1) arm['n_iter']+=1 #print time.localtime(time.time()) elif unit=='iter': n_units=min(n_units,self.max_iter-arm['n_iter']) s.step(n_units) arm['n_iter']+=n_units s.snapshot() train_loss = s.net.blobs['loss'].data val_acc=0 test_acc=0 batches=100 for i in range(batches): s.test_nets[0].forward() val_acc += s.test_nets[0].blobs['acc'].data if arm['n_iter']==self.max_iter: for i in range(batches): s.test_nets[1].forward() test_acc += s.test_nets[1].blobs['acc'].data val_acc=val_acc/batches test_acc=test_acc/batches del s return train_loss,val_acc, test_acc
def run_solver(self, unit, n_units, arm, disp_interval=100): #print(arm['dir']) caffe.set_device(self.device) caffe.set_mode_gpu() s = caffe.get_solver(arm['solver_file']) if arm['n_iter']>0: prefix=arm['dir']+"/cifar10_data_iter_" s.restore(prefix+str(arm['n_iter'])+".solverstate") s.net.copy_from(prefix+str(arm['n_iter'])+".caffemodel") s.test_nets[0].share_with(s.net) s.test_nets[1].share_with(s.net) start=time.time() if unit=='time': while time.time()-start<n_units: s.step(1) arm['n_iter']+=1 #print time.localtime(time.time()) elif unit=='iter': n_units=min(n_units,self.max_iter-arm['n_iter']) s.step(n_units) arm['n_iter']+=n_units s.snapshot() train_loss = s.net.blobs['loss'].data val_acc=0 test_acc=0 test_batches=500 val_batches=20 for i in range(val_batches): s.test_nets[0].forward() val_acc += s.test_nets[0].blobs['acc'].data if arm['n_iter']==self.max_iter: for i in range(test_batches): s.test_nets[1].forward() test_acc += s.test_nets[1].blobs['acc'].data val_acc=val_acc/val_batches test_acc=test_acc/test_batches return train_loss,val_acc, test_acc
def run_solver(self, unit, n_units, arm, disp_interval=100): #print(arm['dir']) caffe.set_device(self.device) caffe.set_mode_gpu() s = caffe.get_solver(arm['solver_file']) if arm['n_iter']>0: prefix=arm['dir']+"/"+str(self.problem)+"_data_iter_" s.restore(prefix+str(arm['n_iter'])+".solverstate") s.net.copy_from(prefix+str(arm['n_iter'])+".caffemodel") s.test_nets[0].share_with(s.net) s.test_nets[1].share_with(s.net) start=time.time() if unit=='time': while time.time()-start<n_units: s.step(1) arm['n_iter']+=1 #print time.localtime(time.time()) elif unit=='iter': n_units=min(n_units,400*150-arm['n_iter']) s.step(n_units) arm['n_iter']+=n_units s.snapshot() train_loss = s.net.blobs['loss'].data val_acc=0 test_acc=0 test_batches=100 val_batches=100 for i in range(val_batches): s.test_nets[0].forward() val_acc += s.test_nets[0].blobs['acc'].data for i in range(test_batches): s.test_nets[1].forward() test_acc += s.test_nets[1].blobs['acc'].data val_acc=val_acc/val_batches test_acc=test_acc/test_batches del s return train_loss,val_acc, test_acc
def run_solver(self, unit, n_units, arm, disp_interval=100): #print(arm['dir']) caffe.set_device(self.device) caffe.set_mode_gpu() s = caffe.get_solver(arm['solver_file']) if arm['n_iter']>0: prefix=arm['dir']+"/cifar10_data_iter_" s.restore(prefix+str(arm['n_iter'])+".solverstate") s.net.copy_from(prefix+str(arm['n_iter'])+".caffemodel") s.test_nets[0].share_with(s.net) s.test_nets[1].share_with(s.net) start=time.time() if unit=='time': while time.time()-start<n_units: s.step(1) arm['n_iter']+=1 #print time.localtime(time.time()) elif unit=='iter': n_units=min(n_units,self.max_iter-arm['n_iter']) s.step(n_units) arm['n_iter']+=n_units s.snapshot() train_loss = s.net.blobs['loss'].data val_acc=0 test_acc=0 test_batches=260 val_batches=60 for i in range(val_batches): s.test_nets[0].forward() val_acc += s.test_nets[0].blobs['acc'].data if arm['n_iter']==self.max_iter: for i in range(test_batches): s.test_nets[1].forward() test_acc += s.test_nets[1].blobs['acc'].data val_acc=val_acc/val_batches test_acc=test_acc/test_batches return train_loss,val_acc, test_acc
def main(): if not os.path.exists('./result'): os.makedirs('./result') question_vocab, answer_vocab = {}, {} if os.path.exists('./result/vdict.json') and os.path.exists('./result/adict.json'): print 'restoring vocab' with open('./result/vdict.json','r') as f: question_vocab = json.load(f) with open('./result/adict.json','r') as f: answer_vocab = json.load(f) else: question_vocab, answer_vocab = make_vocab_files() with open('./result/vdict.json','w') as f: json.dump(question_vocab, f) with open('./result/adict.json','w') as f: json.dump(answer_vocab, f) print 'question vocab size:', len(question_vocab) print 'answer vocab size:', len(answer_vocab) with open('./result/proto_train.prototxt', 'w') as f: f.write(str(qlstm(config.TRAIN_DATA_SPLITS, config.BATCH_SIZE, \ config.MAX_WORDS_IN_QUESTION, len(question_vocab)))) with open('./result/proto_test.prototxt', 'w') as f: f.write(str(qlstm('val', config.VAL_BATCH_SIZE, \ config.MAX_WORDS_IN_QUESTION, len(question_vocab)))) caffe.set_device(config.GPU_ID) caffe.set_mode_gpu() solver = caffe.get_solver('./qlstm_solver.prototxt') train_loss = np.zeros(config.MAX_ITERATIONS) results = [] for it in range(config.MAX_ITERATIONS): solver.step(1) # store the train loss train_loss[it] = solver.net.blobs['loss'].data if it % config.PRINT_INTERVAL == 0: print 'Iteration:', it c_mean_loss = train_loss[it-config.PRINT_INTERVAL:it].mean() print 'Train loss:', c_mean_loss if it != 0 and it % config.VALIDATE_INTERVAL == 0: solver.test_nets[0].save('./result/tmp.caffemodel') print 'Validating...' test_loss, acc_overall, acc_per_ques, acc_per_ans = exec_validation(config.GPU_ID, 'val', it=it) print 'Test loss:', test_loss print 'Accuracy:', acc_overall results.append([it, c_mean_loss, test_loss, acc_overall, acc_per_ques, acc_per_ans]) best_result_idx = np.array([x[3] for x in results]).argmax() print 'Best accuracy of', results[best_result_idx][3], 'was at iteration', results[best_result_idx][0] drawgraph(results)
def main(): if not os.path.exists('./result'): os.makedirs('./result') question_vocab, answer_vocab = {}, {} if os.path.exists('./result/vdict.json') and os.path.exists('./result/adict.json'): print 'restoring vocab' with open('./result/vdict.json','r') as f: question_vocab = json.load(f) with open('./result/adict.json','r') as f: answer_vocab = json.load(f) else: question_vocab, answer_vocab = make_vocab_files() with open('./result/vdict.json','w') as f: json.dump(question_vocab, f) with open('./result/adict.json','w') as f: json.dump(answer_vocab, f) print 'question vocab size:', len(question_vocab) print 'answer vocab size:', len(answer_vocab) with open('./result/proto_train.prototxt', 'w') as f: f.write(str(qlstm(config.TRAIN_DATA_SPLITS, config.BATCH_SIZE, \ config.MAX_WORDS_IN_QUESTION, len(question_vocab)))) with open('./result/proto_test.prototxt', 'w') as f: f.write(str(qlstm('val', config.BATCH_SIZE, \ config.MAX_WORDS_IN_QUESTION, len(question_vocab)))) caffe.set_device(config.GPU_ID) caffe.set_mode_gpu() solver = caffe.get_solver('./qlstm_solver.prototxt') train_loss = np.zeros(config.MAX_ITERATIONS) results = [] for it in range(config.MAX_ITERATIONS): solver.step(1) # store the train loss train_loss[it] = solver.net.blobs['loss'].data if it % config.PRINT_INTERVAL == 0: print 'Iteration:', it c_mean_loss = train_loss[it-config.PRINT_INTERVAL:it].mean() print 'Train loss:', c_mean_loss if it % config.VALIDATE_INTERVAL == 0: print 'Validating...' solver.test_nets[0].save('./result/tmp.caffemodel') test_loss, acc_overall, acc_per_ques, acc_per_ans = exec_validation(config.GPU_ID, 'val', it=it) print 'Test loss:', test_loss print 'Accuracy:', acc_overall results.append([it, c_mean_loss, test_loss, acc_overall, acc_per_ques, acc_per_ans]) best_result_idx = np.array([x[3] for x in results]).argmax() print 'Best accuracy of', results[best_result_idx][3], 'was at iteration', results[best_result_idx][0] drawgraph(results)
def init_solver(self): """ Helper method to initialize the solver. """ solver_param = SolverParameter() solver_param.snapshot_prefix = self._hyperparams['weights_file_prefix'] solver_param.display = 0 # Don't display anything. solver_param.base_lr = self._hyperparams['lr'] solver_param.lr_policy = self._hyperparams['lr_policy'] solver_param.momentum = self._hyperparams['momentum'] solver_param.weight_decay = self._hyperparams['weight_decay'] solver_param.type = self._hyperparams['solver_type'] solver_param.random_seed = self._hyperparams['random_seed'] # Pass in net parameter either by filename or protostring. if isinstance(self._hyperparams['network_model'], basestring): self.solver = caffe.get_solver(self._hyperparams['network_model']) else: network_arch_params = self._hyperparams['network_arch_params'] network_arch_params['dim_input'] = self._dO network_arch_params['dim_output'] = self._dU network_arch_params['batch_size'] = self.batch_size network_arch_params['phase'] = TRAIN solver_param.train_net_param.CopyFrom( self._hyperparams['network_model'](**network_arch_params) ) # For running forward in python. network_arch_params['batch_size'] = 1 network_arch_params['phase'] = TEST solver_param.test_net_param.add().CopyFrom( self._hyperparams['network_model'](**network_arch_params) ) # For running forward on the robot. network_arch_params['batch_size'] = 1 network_arch_params['phase'] = 'deploy' solver_param.test_net_param.add().CopyFrom( self._hyperparams['network_model'](**network_arch_params) ) # These are required by Caffe to be set, but not used. solver_param.test_iter.append(1) solver_param.test_iter.append(1) solver_param.test_interval = 1000000 f = tempfile.NamedTemporaryFile(mode='w+', delete=False) f.write(MessageToString(solver_param)) f.close() self.solver = caffe.get_solver(f.name) # TODO - This assumes that the obs is a vector being passed into the # network in the same place. # (won't work with images or multimodal networks)