我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用caffe.set_mode_cpu()。
def __init__(self, videoThread): threading.Thread.__init__(self) print "Initializing recognition thread..." self.videoThread = videoThread #caffe.set_mode_cpu() caffe.set_mode_gpu() caffe.set_device(0) # Model file and parameters are written by trainDnn.py # Take the most recent parameter set genderPath = "./dcnn_gender" genderParamFiles = glob.glob(genderPath + os.sep + "*.caffemodel") genderParamFiles = sorted(genderParamFiles, key=lambda x:os.path.getctime(x)) MODEL_FILE_GENDER = genderPath + os.sep + "deploy_gender.prototxt" PRETRAINED_GENDER = genderParamFiles[-1] MEAN_FILE_GENDER = genderPath + os.sep + "mean.binaryproto" proto_data = open(MEAN_FILE_GENDER, 'rb').read() a = caffe.io.caffe_pb2.BlobProto.FromString(proto_data) mean = caffe.io.blobproto_to_array(a)[0] # Initialize net self.gender_net = caffe.Classifier(MODEL_FILE_GENDER, PRETRAINED_GENDER, image_dims=(227,227),)
def __init__(self, videoThread): threading.Thread.__init__(self) print "Initializing age recognition thread..." self.videoThread = videoThread #caffe.set_mode_cpu() caffe.set_mode_gpu() # Model file and parameters are written by trainDnn.py # Take the most recent parameter set dcnnPath = "./dcnn_age" paramFiles = glob.glob(dcnnPath + os.sep + "*.caffemodel") paramFiles = sorted(paramFiles, key=lambda x:os.path.getctime(x)) MODEL_FILE = dcnnPath + os.sep + "deploy.prototxt" PRETRAINED = paramFiles[-1] MEAN_FILE = dcnnPath + os.sep + "mean.binaryproto" blob = caffe.proto.caffe_pb2.BlobProto() with open(MEAN_FILE, 'rb') as f: data = f.read() blob.ParseFromString(data) # mean = np.array( caffe.io.blobproto_to_array(blob) ) [0] # Added simple mean mean = np.array([93.5940, 104.7624, 129.1863]) # Initialize net self.net = caffe.Classifier(MODEL_FILE, PRETRAINED, image_dims=(224,224), mean=mean)
def __init__(self, model_def_file, pretrained_model_file, class_labels_file, gpu_mode): logging.info('Loading net and associated files...') if gpu_mode: caffe.set_mode_gpu() else: caffe.set_mode_cpu() self.net = caffe.Classifier( model_def_file, pretrained_model_file, image_dims=(400, 400), raw_scale=400, mean=np.load('{}/mean.npy'.format(REPO_DIRNAME)).mean(1).mean(1), channel_swap=(2, 1, 0) ) with open(class_labels_file) as f: labels_df = pd.DataFrame([ { 'synset_id': l.strip().split(' ')[0], 'name': ' '.join(l.strip().split(' ')[1:]).split(',')[0] } for l in f.readlines() ]) self.labels = labels_df.sort('synset_id')['name'].values
def __init__(self, use_gpu=True, model=[]): ''' Init net. :param model: Network definition. ''' if model == []: raise("model should not be empty!") print("Init NetTester: Use gpu: {}").format(use_gpu) print("Network: {}").format(model) if use_gpu: caffe.set_device(0) caffe.set_mode_gpu() else: caffe.set_mode_cpu() self.__net = caffe.Net(model, caffe.TRAIN)
def __init__(self, layer="fc7", oversample = True): self.caffe_root = "caffe" self.model_prototxt = os.path.join(self.caffe_root, 'deploy.prototxt') self.model_trained = os.path.join(self.caffe_root, "bvlc_reference_caffenet.caffemodel") self.mean_image = os.path.join(self.caffe_root, 'python/caffe/imagenet/ilsvrc_2012_mean.npy') self.layer = layer caffe.set_mode_cpu() self.net = caffe.Classifier(self.model_prototxt, self.model_trained, mean=np.load(self.mean_image).mean(1).mean(1), channel_swap=(2,1,0), raw_scale=255, image_dims=(256, 256)) self.size = (256,256) self.patch_size = 224 self.prefix = "DeCAF7" self.oversample = oversample
def __init__(self,params): self.dimension = params['dimension'] self.dataset = params['dataset'] self.pooling = params['pooling'] # Read image lists with open(params['query_list'],'r') as f: self.query_names = f.read().splitlines() with open(params['frame_list'],'r') as f: self.database_list = f.read().splitlines() # Parameters needed self.layer = params['layer'] self.save_db_feats = params['database_feats'] # Init network if params['gpu']: caffe.set_mode_gpu() caffe.set_device(0) else: caffe.set_mode_cpu() print "Extracting from:", params['net_proto'] cfg.TEST.HAS_RPN = True self.net = caffe.Net(params['net_proto'], params['net'], caffe.TEST)
def retrain_pruned(solver, pruned_caffemodel, threshold, prune_layers): #solver = caffe.SGDSolver(solver_proto) retrain_iter = 20 accuracys = [] for i in range(retrain_iter): solver.net.copy_from(pruned_caffemodel) # solver.solve() solver.step(500) _,_,_,_,accuracy=prune(threshold, solver.test_nets[0], prune_layers) solver.test_nets[0].save(pruned_caffemodel) accuracys.append(accuracy) plt.plot(accuracys, 'r.-') plt.show() #CPU?GPU???? #caffe.set_mode_cpu()
def set_caffe(cfg): model = cfg.VGG_model weights = cfg.VGG_weights if cfg.cpu_caffe: caffe.set_mode_cpu() net = caffe.Net(model, weights, caffe.TEST) # Set up transformer transformer = caffe.io.Transformer({'data': net.blobs['data'].data.shape}) transformer.set_transpose('data', (2,0,1)) transformer.set_mean('data',np.array([129.1863,104.7624,93.5940])) # BGR -> RGB transformer.set_channel_swap('data', (2,1,0)) transformer.set_raw_scale('data',255) return net, transformer
def __init__(self, hyperparams, dO, dU): config = copy.deepcopy(POLICY_OPT_CAFFE) config.update(hyperparams) PolicyOpt.__init__(self, config, dO, dU) self.batch_size = self._hyperparams['batch_size'] if self._hyperparams['use_gpu']: caffe.set_device(self._hyperparams['gpu_id']) caffe.set_mode_gpu() else: caffe.set_mode_cpu() self.init_solver() self.caffe_iter = 0 self.var = self._hyperparams['init_var'] * np.ones(dU) self.policy = CaffePolicy(self.solver.test_nets[0], self.solver.test_nets[1], self.var)
def load_caffe(img_p, layers=50): caffe.set_mode_cpu() prototxt = "data/ResNet-%d-deploy.prototxt" % layers caffemodel = "data/ResNet-%d-model.caffemodel" % layers # net = caffe.Net(prototxt, caffe.TEST) net = caffe.Net(prototxt, caffemodel, caffe.TEST) net.blobs['data'].data[0] = img_p.transpose((2, 0, 1)) assert net.blobs['data'].data[0].shape == (3, 224, 224) net.forward() caffe_prob = net.blobs['prob'].data[0] print_prob(caffe_prob) return net # returns the top1 string
def main(argv): #input e.g : python caffe_main_test_top2_scores.py ./test_dir/ ./0908.log #get argvs caffe_root = '/opt/caffe/' model_path = '/opt/model_0908_12c/' parser = argparse.ArgumentParser() # Required arguments: input and output files. parser.add_argument( "image_path", help="Input image, directory, or npy." ) parser.add_argument( "log_path", help="Output npy filename." ) args = parser.parse_args() #set vars from argvs image_path = args.image_path log_path = args.log_path #program begin... #caffe.set_mode_cpu()
def get_net(caffemodel, deploy_file, use_gpu=True): """ Returns an instance of caffe.Net Arguments: caffemodel -- path to a .caffemodel file deploy_file -- path to a .prototxt file Keyword arguments: use_gpu -- if True, use the GPU for inference """ #if use_gpu: # caffe.set_mode_gpu() caffe.set_mode_cpu() # load a new model return caffe.Net(deploy_file, caffemodel, caffe.TEST) # Transformer function to perform image transformation
def getNet(): global TRANSFORMER, NET if TRANSFORMER is None or NET is None: import caffe os.environ['GLOG_minloglevel'] = '2' caffe.set_mode_cpu() ## Opening mean average image proto_data = open(mean_path, "rb").read() a = caffe.io.caffe_pb2.BlobProto.FromString(proto_data) mean = caffe.io.blobproto_to_array(a)[0] ## Loading the CNN NET = caffe.Classifier(deploy_path, model_path) ## Setting up the right transformer for an input image TRANSFORMER = caffe.io.Transformer({'data': NET.blobs['data'].data.shape}) TRANSFORMER.set_transpose('data', (2, 0, 1)) TRANSFORMER.set_channel_swap('data', (2, 1, 0)) TRANSFORMER.set_raw_scale('data', 255.0) TRANSFORMER.set_mean('data', mean) print('> CNN Model loaded to regress 3D Shape and Texture!') return TRANSFORMER, NET
def __init__(self, args): super().__init__(args, with_video_output=False) if self.vgg_model_path is None: self.vgg_model_path = "/media/" + getpass.getuser() + "/Data/AMBR_data/ml" self.vgg_model_filename = os.path.join(self.vgg_model_path, self.vgg_model_filename) self.vgg_pretrained_filename = os.path.join(self.vgg_model_path, self.vgg_pretrained_filename) if self.output_datafile is None: self.output_datafile = "{:s}_features.npz".format(self.in_video[:-4]) self.prev_frame_centroid = None if self.caffe_cpu: caffe.set_mode_cpu() else: caffe.set_mode_gpu() self.extractor = None self.blank_features = None if not self.no_vgg: self.extractor = VGGFeatureExtractor(model_file=self.vgg_model_filename, pretrained_file=self.vgg_pretrained_filename) self.blank_features = self.extractor.extract_single(np.zeros((256, 256, 3), dtype=np.uint8), blobs=['fc7'])[ 'fc7'] self.features = [] self.present_flags = []
def get_predictions(region_crops): if os.environ["IS_GPU"]: caffe.set_device(0) caffe.set_mode_gpu() else: caffe.set_mode_cpu() classifier = caffe.Classifier(os.path.join(os.environ["TEXT_NOTEXT_MODELS_DIR"], "deploy.prototxt"), os.path.join(os.environ["TEXT_NOTEXT_MODELS_DIR"], "weights.caffemodel"), mean=np.array([104, 117, 123], dtype='f4'), image_dims=[224, 224], raw_scale=255.0, channel_swap=[2, 1, 0]) LOGGER.info("Classifying " + str(len(region_crops)) + " inputs.") predictions = classifier.predict(region_crops) return predictions
def run(self, _, app_context): """run the action""" import caffe # init CPU/GPU mode cpu_mode = app_context.get_config('caffe.cpu_mode') if cpu_mode: caffe.set_mode_cpu() else: caffe.set_mode_gpu() caffe.set_device(0) # load test model test_model_file = "models/" + app_context.get_config('caffe.test_model') trained_data_file = "cache/data/" + app_context.get_config('caffe.trained_data') test_net = caffe.Net(test_model_file, trained_data_file, caffe.TEST) app_context.params['test_net'] = test_net logging.getLogger(__name__).info('Loaded neural network: ' + trained_data_file)
def _loadModel(self, model_dirs, id): print 'loading model...from{}'.format(model_dirs) model_file = osp.join(model_dirs, 'vgg16.prototxt') model_weights = osp.join(model_dirs, 'vgg16.caffemodel') mean_file = osp.join(model_dirs, 'vgg16_mean.npy') if id == -1: caffe.set_mode_cpu() else: caffe.set_mode_gpu() caffe.set_device(id) net = caffe.Net(model_file, model_weights, caffe.TEST) transformer = caffe.io.Transformer({'data': net.blobs['data'].data.shape}) transformer.set_mean('data', np.load(mean_file).mean(1).mean(1)) transformer.set_channel_swap('data', (2, 1, 0)) transformer.set_transpose('data', (2, 0, 1)) #transformer.set_raw_scale('data', 255) self.net = net self.transformer = transformer self.style_layers = VGG16_STYLES self.content_layers = VGG16_CONTENTS self.layers = VGG16_LAYERS print 'model loading done'
def get_caffe_model(caffe_dir, caffe_model, gpu=True, image_dims=(256, 256), mean_file='default', raw_scale=255.0, channel_swap=(2,1,0), input_scale=None): if mean_file == 'default': mean_file = os.path.join(caffe_dir, 'python', 'caffe', 'imagenet', 'ilsvrc_2012_mean.npy') model_path = os.path.join(caffe_dir, 'models', caffe_model, '%s.caffemodel'%caffe_model) model_def = os.path.join(caffe_dir, 'models', caffe_model, 'deploy.prototxt') print('Loading mean file %s' % mean_file) mean = np.load(mean_file).mean(1).mean(1) if gpu: caffe.set_mode_gpu() else: caffe.set_mode_cpu() net = caffe.Classifier(model_def, model_path, image_dims=image_dims, mean=mean, input_scale=input_scale, raw_scale=raw_scale, channel_swap=channel_swap) return net
def __init__(self, deploy, pretrained, mean, labels, gpu = False): if gpu: caffe.set_mode_gpu() else: caffe.set_mode_cpu() # in windows, only CPU mode supported self.__labels = self.load_labels(labels); mean_ar = self.convert(mean) if True: self.__net = caffe.Classifier(deploy, pretrained, mean = mean_ar.mean(1).mean(1), channel_swap = (2, 1, 0), raw_scale = 255, image_dims = (256, 256)) else: self.__net = caffe.Net(deploy, pretrained, caffe.TEST) print self.__net.blobs['data'].data.shape self.__transformer = caffe.io.Transformer({'data': self.__net.blobs['data'].data.shape}) self.__transformer.set_transpose('data', (2,0,1)) # height*width*channel -> channel*height*width self.__transformer.set_mean('data', mean_ar) self.__transformer.set_raw_scale('data', 255) self.__transformer.set_channel_swap('data', (2,1,0)) # RGB -> BGR
def main(): parser = argparse.ArgumentParser() parser.add_argument('dataset', nargs='?', choices=['pascal_voc', 'camvid', 'kitti', 'cityscapes']) parser.add_argument('input_path', nargs='?', default='', help='Required path to input image') parser.add_argument('-o', '--output_path', default=None) parser.add_argument('--gpu', type=int, default=-1, help='GPU ID to run CAFFE. ' 'If -1 (default), CPU is used') args = parser.parse_args() if args.input_path == '': raise IOError('Error: No path to input image') if not exists(args.input_path): raise IOError("Error: Can't find input image " + args.input_path) if args.gpu >= 0: caffe.set_mode_gpu() caffe.set_device(args.gpu) print('Using GPU ', args.gpu) else: caffe.set_mode_cpu() print('Using CPU') if args.output_path is None: args.output_path = '{}_{}.png'.format( splitext(args.input_path)[0], args.dataset) predict(args.dataset, args.input_path, args.output_path)
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 __init__(self, protoTXTPath, weightsPath): import caffe caffe.set_mode_cpu() self.net = caffe.Net(protoTXTPath, weightsPath, caffe.TEST) self.mean = cv2.imread(os.path.join(Predictor.ROOT, 'trainMean.png')).astype('float') self.mean = cv2.resize(self.mean, (60,60), interpolation=cv2.INTER_CUBIC) self.std = cv2.imread(os.path.join(Predictor.ROOT,'trainSTD.png')).astype('float') self.std = cv2.resize(self.std, (60,60), interpolation=cv2.INTER_CUBIC)
def netsurgery(protofile, paramfile, params, protofile_full_conv, params_full_conv, opath): ''' See https://github.com/BVLC/caffe/blob/master/examples/net_surgery.ipynb for details. protofile: prototxt of fully-connected model (read-only pathname) paramfile: weights of fully-connected model (read-only pathname) params: list of fully-connected blob names (list of str) protofile_full_conv: prototxt of fully-convolutional model (read-only pathname) params_full_conv: list of fully-convolutional blob names (list of str) opath: weights of fully-connected model (write pathname) ''' caffe.set_mode_cpu() net = caffe.Net(protofile, paramfile, caffe.TEST) fc_params = {pr: (net.params[pr][0].data, net.params[pr][1].data) for pr in params} for fc in params: print('{} weights are {} dimensional and biases are {} dimensional'.format(fc, fc_params[fc][0].shape, fc_params[fc][1].shape)) net_full_conv = caffe.Net(protofile_full_conv, paramfile, caffe.TEST) conv_params = {pr: (net_full_conv.params[pr][0].data, net_full_conv.params[pr][1].data) for pr in params_full_conv} for conv in params_full_conv: print('{} weights are {} dimensional and biases are {} dimensional'.format(conv, conv_params[conv][0].shape, conv_params[conv][1].shape)) for pr, pr_conv in zip(params, params_full_conv): conv_params[pr_conv][0].flat = fc_params[pr][0].flat # flat unrolls the arrays conv_params[pr_conv][1][...] = fc_params[pr][1] net_full_conv.save(opath)
def __init__(self, path_to_deploy_file, path_to_model_file, input_layer_name="data_q", gpu_mode=True, device_id=1, height=None, width=None): self.path_to_deploy_file = path_to_deploy_file self.path_to_model_file = path_to_model_file if gpu_mode: caffe.set_mode_gpu() caffe.set_device(device_id) else: caffe.set_mode_cpu() self.net = caffe.Net(path_to_deploy_file, path_to_model_file, caffe.TEST) self.input_layer_name = input_layer_name self.height = height or self.net.blobs[self.input_layer_name].data.shape[2] self.width = width or self.net.blobs[self.input_layer_name].data.shape[3]
def eval(testSetPath, clipSize): checkTestAndTruths(testSetPath) checkDirs(testSetPath) imgTestDir = os.path.join(testSetPath, 'test/') caffe.set_mode_cpu() printTitle("Loading caffe model") net = caffe.Net( 'src/deploy.prototxt' , 'src/fcn8s-heavy-pascal.caffemodel' , caffe.TEST) print("Model loaded.") testImgPaths = [] for testImg in os.listdir(imgTestDir): testImgPaths.append(imgTestDir + testImg) testImgPaths.sort() printTitle("Generating predictions") totalPredictTime = 0.00 for count in range(0, len(testImgPaths)): totalPredictTime += predict(testImgPaths[count], testSetPath, clipSize, net) printTitle("Average prediction time: %.3f" % (totalPredictTime / len(testImgPaths))) printTitle("Generating scores") segPath = os.path.join(testSetPath, 'seg/') truthPath = os.path.join(testSetPath, 'truth/') computeAccuracies(testSetPath, segPath, truthPath) print("eval.py has completed.") # Make folders for seg, labelled and mask predictions
def load_caffe(model_desc, model_file): """ return a dict of params """ import caffe caffe.set_mode_cpu() net = caffe.Net(model_desc, model_file, caffe.TEST) param_dict = CaffeLayerProcessor(net).process() return param_dict
def start(self, rank): self.rank = rank if len(self.gpus) > 0: self.device = self.gpus[rank] if debug: s = 'solver gpu %d' % self.gpus[self.rank] + \ ' pid %d' % os.getpid() + ' size %d' % self.size + \ ' rank %d' % self.rank print(s, file = sys.stderr) caffe.set_mode_gpu() caffe.set_device(self.device) caffe.set_solver_count(self.size) caffe.set_solver_rank(self.rank) caffe.set_multiprocess(True) else: print('solver cpu', file = sys.stderr) caffe.set_mode_cpu() if self.cmd.graph.endswith('.json'): with open(self.cmd.graph, mode = 'r') as f: graph = caffe_pb2.SolverParameter() text_format.Merge(f.read(), graph) self.graph = graph else: self.graph = self.solver_graph() import tempfile with tempfile.NamedTemporaryFile(mode = 'w+', delete = False) as f: text_format.PrintMessage(self.graph, f) tmp = f.name self.caffe = caffe.AdamSolver(tmp) if self.uid: self.nccl = caffe.NCCL(self.caffe, self.uid) self.nccl.bcast() self.caffe.add_callback(self.nccl) if self.caffe.param.layer_wise_reduce: self.caffe.net.after_backward(self.nccl)
def initFaceDetector(): minsize = 20 caffe_model_path = "/home/duino/iactive/mtcnn/model" threshold = [0.6, 0.7, 0.7] factor = 0.709 caffe.set_mode_cpu() PNet = caffe.Net(caffe_model_path+"/det1.prototxt", caffe_model_path+"/det1.caffemodel", caffe.TEST) RNet = caffe.Net(caffe_model_path+"/det2.prototxt", caffe_model_path+"/det2.caffemodel", caffe.TEST) ONet = caffe.Net(caffe_model_path+"/det3.prototxt", caffe_model_path+"/det3.caffemodel", caffe.TEST) return (minsize, PNet, RNet, ONet, threshold, factor)
def run(self): """This method runs in the new process.""" global logger setup_exceptions() logger = log_utils.setup_logger('tile_worker') if self.caffe_path is not None: sys.path.append(self.caffe_path + '/python') if self.device >= 0: os.environ['CUDA_VISIBLE_DEVICES'] = str(self.device) import caffe if self.device >= 0: caffe.set_mode_gpu() else: caffe.set_mode_cpu() caffe.set_random_seed(0) np.random.seed(0) self.model = CaffeModel(*self.model_info) self.model.img = np.zeros((3, 1, 1), dtype=np.float32) while True: try: self.process_one_request() except KeyboardInterrupt: break
def init_model(resp_q, caffe_path, model, weights, mean): """Puts the list of layer shapes into resp_q. To be run in a separate process.""" global logger setup_exceptions() logger = log_utils.setup_logger('init_model') if caffe_path: sys.path.append(caffe_path + '/python') import caffe caffe.set_mode_cpu() model = CaffeModel(model, weights, mean) shapes = OrderedDict() for layer in model.layers(): shapes[layer] = model.data[layer].shape resp_q.put(shapes)
def get_net(self): #caffe.set_mode_cpu() net = caffe.Net(self.deploy, self.model, caffe.TEST) transformer = caffe.io.Transformer({'data':net.blobs['data'].data.shape}) transformer.set_transpose('data', (2,0,1)) transformer.set_mean('data', np.load(self.mean).mean(1).mean(1)) transformer.set_raw_scale('data', 255) transformer.set_channel_swap('data', (2,1,0)) return net, transformer
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 __init__(self, hyperparams, dO, dU): config = copy.deepcopy(POLICY_OPT_CAFFE) config.update(hyperparams) PolicyOpt.__init__(self, config, dO, dU) self.batch_size = self._hyperparams['batch_size'] if self._hyperparams['use_gpu']: caffe.set_device(self._hyperparams['gpu_id']) caffe.set_mode_gpu() else: caffe.set_mode_cpu() self.init_solver() # Load parameters from caffemodel file if 'init_net' in self._hyperparams: self.solver.net.copy_from(self._hyperparams['init_net']) self.caffe_iter = 0 self.var = self._hyperparams['init_var'] * np.ones(dU) self.policy = CaffePolicy(self.solver.test_nets[0], self.solver.test_nets[1], self.var) self.policy.bias = None self.policy.scale = None if 'init_normalization' in self._hyperparams: with open(self._hyperparams['init_normalization']) as fin: normalzation_data = pickle.load(fin) self.policy.bias = normalzation_data['bias'] self.policy.scale = normalzation_data['scale']
def initilize(): print 'initilize ... ' sys.path.insert(0, caffe_root + 'python') caffe.set_mode_cpu() net = caffe.Net(deployPrototxt, modelFile,caffe.TEST) return net # ??????
def caffe_set_device(type, id=0): if type.lower() == "gpu": caffe.set_device(id) caffe.set_mode_gpu() else: caffe.set_mode_cpu()
def __init__(self): if pa.GPU==True: caffe.set_device(pa.device) caffe.set_mode_gpu() else: caffe.set_mode_cpu() self.solver=caffe.SGDSolver(pa.solver) if pa.pretrain!="": self.solver.net.copy_from(pa.pretrain) self.solver_param=caffe_pb2.SolverParameter() with open(pa.solver,'rt') as f: pb2.text_format.Merge(f.read(),self.solver_param) #self.output_dir=pa.output_dir self.solver.net.layers[0].set_queue()
def load_weigths_from_caffe(self, protofile, caffemodel): caffe.set_mode_cpu() net = caffe.Net(protofile, caffemodel, caffe.TEST) for name, layer in self.models.items(): if isinstance(layer, nn.Conv2d): caffe_weight = net.params[name][0].data layer.weight.data = torch.from_numpy(caffe_weight) if len(net.params[name]) > 1: caffe_bias = net.params[name][1].data layer.bias.data = torch.from_numpy(caffe_bias) continue if isinstance(layer, nn.BatchNorm2d): caffe_means = net.params[name][0].data caffe_var = net.params[name][1].data layer.running_mean = torch.from_numpy(caffe_means) layer.running_var = torch.from_numpy(caffe_var) # find the scale layer top_name_of_bn = self.layer_map_to_top[name][0] scale_name = '' for caffe_layer in self.net_info['layers']: if caffe_layer['type'] == 'Scale' and caffe_layer['bottom'][0] == top_name_of_bn: scale_name = caffe_layer['name'] break if scale_name != '': caffe_weight = net.params[scale_name][0].data layer.weight.data = torch.from_numpy(caffe_weight) if len(net.params[name]) > 1: caffe_bias = net.params[scale_name][1].data layer.bias.data = torch.from_numpy(caffe_bias)
def __init__(self, model_def, model_weights, y_tag_json_path, is_mode_cpu=True, width=32, height=32): self.net = caffe.Net(model_def, model_weights, caffe.TEST) if is_mode_cpu: caffe.set_mode_cpu() self.y_tag_json = json.load(open(y_tag_json_path, "r")) self.width = width self.height = height
def __init__(self, model_def, model_weights, y_tag_json_path, is_mode_cpu=True, width=64, height=64): self.net = caffe.Net(model_def, model_weights, caffe.TEST) if is_mode_cpu: caffe.set_mode_cpu() self.y_tag_json = json.load(open(y_tag_json_path, "r")) self.width = width self.height = height
def get_symbol_classifications(symbols): if os.environ["IS_GPU"]: caffe.set_device(0) caffe.set_mode_gpu() else: caffe.set_mode_cpu() classifier = caffe.Classifier(os.path.join(os.environ["JAR_NOJAR_MODELS_DIR"], "deploy.prototxt"), os.path.join(os.environ["JAR_NOJAR_MODELS_DIR"], "weights.caffemodel"), image_dims=[64, 64], raw_scale=255.0) LOGGER.info("Classifying " + str(len(symbols)) + " inputs.") predictions = classifier.predict([s[1] for s in symbols]) symbol_sequence = list() classes = np.array([0, 1]) for i, prediction in enumerate(predictions): idx = list((-prediction).argsort()) prediction = classes[np.array(idx)] if prediction[0] == 1: symbol_sequence.append([symbols[i], "jar"]) elif prediction[0] == 0: symbol_sequence.append([symbols[i], "no-jar"]) return symbol_sequence
def get_depth(imagename): caffe.set_mode_cpu() netFile = 'model/net_deploy.prototxt' modelFile = 'model/model_norm_abs_100k.caffemodel' net = caffe.Net(netFile, modelFile, caffe.TEST) input_image = cv2.imread(imagename) res_input=cv2.resize(input_image,(420,320),interpolation=cv2.INTER_CUBIC) input = loadImage(imagename, 3, WIDTH, HEIGHT) input *= 255 input -= 127 output = testNet(net, input) outWidth = OUT_WIDTH outHeight = OUT_HEIGHT scaleW = float(GT_WIDTH) / float(OUT_WIDTH) scaleH = float(GT_HEIGHT) / float(OUT_HEIGHT) output = scipy.ndimage.zoom(output, (1,1,scaleH,scaleW), order=3) outWidth *= scaleW outHeight *= scaleH #input += 127 #input = input / 255.0 #input = np.transpose(input, (0,2,3,1)) #input = input[:,:,:,(2,1,0)] output = ProcessToOutput(output) path1 = DIR+'img.png' path2 = DIR+'depth.png' cv2.imwrite(path1, res_input) printImage(output, path2, 1, int(outWidth), int(outHeight))
def load_and_fill_biases(src_model, src_weights, dst_model, dst_weights): with open(src_model) as f: model = caffe.proto.caffe_pb2.NetParameter() pb.text_format.Merge(f.read(), model) for i, layer in enumerate(model.layer): if layer.type == 'Convolution': # or layer.type == 'Scale': # Add bias layer if needed if layer.convolution_param.bias_term == False: layer.convolution_param.bias_term = True layer.convolution_param.bias_filler.type = 'constant' layer.convolution_param.bias_filler.value = 0.0 with open(dst_model, 'w') as f: f.write(pb.text_format.MessageToString(model)) caffe.set_mode_cpu() net_src = caffe.Net(src_model, src_weights, caffe.TEST) net_dst = caffe.Net(dst_model, caffe.TEST) for key in net_src.params.keys(): for i in range(len(net_src.params[key])): net_dst.params[key][i].data[:] = net_src.params[key][i].data[:] if dst_weights is not None: # Store params pass return net_dst
def InitCaffe(): #S?n?fland?rma için hangi donan?m? kullanaca??m?z? belirtiyoruz #caffe.set_mode_cpu() #CPU yani i?lemci üzerinde caffe.set_mode_gpu() #GPU yani ekran kart? üzerinde model_def = 'deploy.prototxt' model_weights = 'bvlc_reference_caffenet.caffemodel' #imagenet model dosyas? global net net = caffe.Net(model_def, # Modelin yap?s?n? tan?mlar model_weights, # E?itilmi? a??rl?klar? içerir caffe.TEST) # Test modunda kullanaca??z #Subtraction için ortalama ImageNet görüntüsü yüklenir. mu = np.load('ilsvrc_2012_mean.npy') mu = mu.mean(1).mean(1) #Data ad? verilen transformatör giri? için olu?turulur global transformer transformer = caffe.io.Transformer({'data': net.blobs['data'].data.shape}) transformer.set_transpose('data', (2,0,1)) # Görüntü kanallar?n? en d??a ta??r transformer.set_mean('data', mu) # Her kanaldaki veri seti ortalamas? ç?kar?l?r. transformer.set_raw_scale('data', 255) # [0, 1] 'den [0, 255]' e yeniden ölçeklendirme yap?l?r. transformer.set_channel_swap('data', (2,1,0)) # Renk uzay? RGB den BGR renk uzay?na dönü?türülür. # Giri?in boyutunu ayarlan?r. # Varsay?lan olarak kals?n. ?sterseniz daha sonra farkl? y???n boyutlar? için de?i?tirebiliriz net.blobs['data'].reshape(50, # Y???n?n boyutu 3, # 3 kanall? yani BGR resimler. 227, 227) # resimlerin boyutu 227x227 olarak ayarlanacak. #Parametre ile gonderilen goruntu analiz edilecek
def __init__(self,modelFile,pretrainedFile): caffe.Net.__init__(self,modelFile, pretrainedFile, caffe.TEST) caffe.set_mode_cpu()
def set_caffe_mode(gpu): ''' Set whether caffe runs in gpu or not, input is boolean ''' if gpu: caffe.set_mode_gpu() else: caffe.set_mode_cpu()
def get_caffe_variables(self,net_proto,net_model = None,bn_name = ''): " This function get caffe variables" caffe.set_mode_cpu() self.blob_dict={} if net_model is not None: self.net_caffe = caffe.Net(net_proto,net_model,caffe.TEST) else: self.net_caffe = caffe.Net(net_proto,caffe.TEST) # caffe net params layer_name w b # bn_name : caffe bn layer name include bn_name # Note: we must match tf_variables name and caffe params name # so we modifiy caffe params name and save in bolb_dict for layer_name,param in self.net_caffe.params.items(): param_len = len(param) # find batch_normalization name must has 'bn_name' # your can modify it if param_len == 3 and layer_name.find(bn_name) >= 0: scale_factor = 1.0 / param[2].data[0] mean = param[0].data * scale_factor variance = param[1].data *scale_factor name = str(layer_name) + "/weights:0" self.blob_dict[name] = mean name = str(layer_name) + "/biases:0" self.blob_dict[name] = variance elif param_len == 2: name = str(layer_name) + "/weights:0" self.blob_dict[name] = param[0].data name = str(layer_name) + "/biases:0" self.blob_dict[name] = param[1].data elif param_len == 1: name = str(layer_name) + "/weights:0" self.blob_dict[name] = param[0].data