Python caffe.set_mode_cpu() Examples
The following are 30
code examples of caffe.set_mode_cpu().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
caffe
, or try the search function
.
Example #1
Source File: convert.py From tensorflow-resnet with MIT License | 6 votes |
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, 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
Example #2
Source File: caffe_launcher.py From open_model_zoo with Apache License 2.0 | 6 votes |
def __init__(self, config_entry: dict, *args, **kwargs): super().__init__(config_entry, *args, **kwargs) self._delayed_model_loading = kwargs.get('delayed_model_loading', False) caffe_launcher_config = LauncherConfigValidator( 'Caffe_Launcher', fields=self.parameters(), delayed_model_loading=self._delayed_model_loading ) caffe_launcher_config.validate(self.config) self._do_reshape = False if not self._delayed_model_loading: self.model, self.weights = self.automatic_model_search() self.network = caffe.Net(str(self.model), str(self.weights), caffe.TEST) self.allow_reshape_input = self.get_value_from_config('allow_reshape_input') match = re.match(DEVICE_REGEX, self.get_value_from_config('device').lower()) if match.group('device') == 'gpu': caffe.set_mode_gpu() identifier = match.group('identifier') or 0 caffe.set_device(int(identifier)) elif match.group('device') == 'cpu': caffe.set_mode_cpu() self._batch = self.get_value_from_config('batch')
Example #3
Source File: loadcaffe.py From dataflow with Apache License 2.0 | 6 votes |
def load_caffe(model_desc, model_file): """ Load a caffe model. You must be able to ``import caffe`` to use this function. Args: model_desc (str): path to caffe model description file (.prototxt). model_file (str): path to caffe model parameter file (.caffemodel). Returns: dict: the parameters. """ with change_env('GLOG_minloglevel', '2'): import caffe caffe.set_mode_cpu() net = caffe.Net(model_desc, model_file, caffe.TEST) param_dict = CaffeLayerProcessor(net).process() logger.info("Model loaded from caffe. Params: " + ", ".join(sorted(param_dict.keys()))) return param_dict
Example #4
Source File: colorize_image.py From interactive-deep-colorization with MIT License | 6 votes |
def prep_net(self, gpu_id, prototxt_path='', caffemodel_path=''): import caffe print('gpu_id = %d, net_path = %s, model_path = %s' % (gpu_id, prototxt_path, caffemodel_path)) if gpu_id == -1: caffe.set_mode_cpu() else: caffe.set_device(gpu_id) caffe.set_mode_gpu() self.gpu_id = gpu_id self.net = caffe.Net(prototxt_path, caffemodel_path, caffe.TEST) self.net_set = True # automatically set cluster centers if len(self.net.params[self.pred_ab_layer][0].data[...].shape) == 4 and self.net.params[self.pred_ab_layer][0].data[...].shape[1] == 313: print('Setting ab cluster centers in layer: %s' % self.pred_ab_layer) self.net.params[self.pred_ab_layer][0].data[:, :, 0, 0] = self.pts_in_hull.T # automatically set upsampling kernel for layer in self.net._layer_names: if layer[-3:] == '_us': print('Setting upsampling layer kernel: %s' % layer) self.net.params[layer][0].data[:, 0, :, :] = np.array(((.25, .5, .25, 0), (.5, 1., .5, 0), (.25, .5, .25, 0), (0, 0, 0, 0)))[np.newaxis, :, :] # ***** Call forward *****
Example #5
Source File: ssd_net.py From Hand-Keypoint-Detection with GNU General Public License v3.0 | 6 votes |
def __init__(self, model_weights, model_def, threshold=0.5, GPU_MODE=False): if GPU_MODE: caffe.set_device(0) caffe.set_mode_gpu() else: caffe.set_mode_cpu() self.net = caffe.Net(model_def, # defines the structure of the model model_weights, # contains the trained weights caffe.TEST) # use test mode (e.g., don't perform dropout) self.threshold = threshold self.transformer = caffe.io.Transformer({'data': self.net.blobs['data'].data.shape}) self.transformer.set_transpose('data', (2, 0, 1)) self.transformer.set_mean('data', np.array([127.0, 127.0, 127.0])) # mean pixel self.transformer.set_raw_scale('data', 255) # the reference model operates on images in [0,255] range instead of [0,1] self.transformer.set_channel_swap('data', (2, 1, 0)) # the reference model has channels in BGR order instead of RGB image_resize = 300 self.net.blobs['data'].reshape(1, 3, image_resize, image_resize)
Example #6
Source File: cnn_feature.py From neural-image-captioning with MIT License | 6 votes |
def load_network(proto_txt, caffe_model, device): if 'gpu' in device: caffe.set_mode_gpu() device_id = int(device.split('gpu')[-1]) caffe.set_device(device_id) else: caffe.set_mode_cpu() # load network net = caffe.Net(proto_txt, caffe_model, caffe.TEST) # tansformer mu = np.load(osp.join(CAFFE_ROOT, 'models', 'ResNet', 'ResNet_mean.npy')) mu = mu.mean(1).mean(1) transformer = caffe.io.Transformer({'data': net.blobs['data'].data.shape}) transformer.set_transpose('data', (2, 0, 1)) # move image channels to outermost dimension transformer.set_mean('data', mu) # subtract the dataset-mean value in each channel transformer.set_raw_scale('data', 255) # rescale from [0, 1] to [0, 255] transformer.set_channel_swap('data', (2, 1, 0)) # swap channels from RGB to BGR # reshape input net.blobs['data'].reshape(BS, 3, 224, 224) return net, transformer
Example #7
Source File: extract_features.py From dnn-model-services with MIT License | 6 votes |
def __init__(self, weights_path, image_net_proto, device_id=-1): if device_id >= 0: caffe.set_mode_gpu() caffe.set_device(device_id) else: caffe.set_mode_cpu() # Setup image processing net. phase = caffe.TEST self.image_net = caffe.Net(image_net_proto, weights_path, phase) image_data_shape = self.image_net.blobs['data'].data.shape self.transformer = caffe.io.Transformer({'data': image_data_shape}) channel_mean = np.zeros(image_data_shape[1:]) channel_mean_values = [104, 117, 123] assert channel_mean.shape[0] == len(channel_mean_values) for channel_index, mean_val in enumerate(channel_mean_values): channel_mean[channel_index, ...] = mean_val self.transformer.set_mean('data', channel_mean) self.transformer.set_channel_swap('data', (2, 1, 0)) # BGR self.transformer.set_transpose('data', (2, 0, 1))
Example #8
Source File: test_solver.py From Deep-Learning-Based-Structural-Damage-Detection with MIT License | 6 votes |
def setUp(self): self.num_output = 13 net_f = simple_net_file(self.num_output) f = tempfile.NamedTemporaryFile(mode='w+', delete=False) f.write("""net: '""" + net_f + """' test_iter: 10 test_interval: 10 base_lr: 0.01 momentum: 0.9 weight_decay: 0.0005 lr_policy: 'inv' gamma: 0.0001 power: 0.75 display: 100 max_iter: 100 snapshot_after_train: false snapshot_prefix: "model" """) f.close() self.solver = caffe.SGDSolver(f.name) # also make sure get_solver runs caffe.get_solver(f.name) caffe.set_mode_cpu() # fill in valid labels self.solver.net.blobs['label'].data[...] = \ np.random.randint(self.num_output, size=self.solver.net.blobs['label'].data.shape) self.solver.test_nets[0].blobs['label'].data[...] = \ np.random.randint(self.num_output, size=self.solver.test_nets[0].blobs['label'].data.shape) os.remove(f.name) os.remove(net_f)
Example #9
Source File: test_solver.py From mix-and-match with MIT License | 6 votes |
def setUp(self): self.num_output = 13 net_f = simple_net_file(self.num_output) f = tempfile.NamedTemporaryFile(delete=False) f.write("""net: '""" + net_f + """' test_iter: 10 test_interval: 10 base_lr: 0.01 momentum: 0.9 weight_decay: 0.0005 lr_policy: 'inv' gamma: 0.0001 power: 0.75 display: 100 max_iter: 100 snapshot_after_train: false""") f.close() self.solver = caffe.SGDSolver(f.name) # also make sure get_solver runs caffe.get_solver(f.name) caffe.set_mode_cpu() # fill in valid labels self.solver.net.blobs['label'].data[...] = \ np.random.randint(self.num_output, size=self.solver.net.blobs['label'].data.shape) self.solver.test_nets[0].blobs['label'].data[...] = \ np.random.randint(self.num_output, size=self.solver.test_nets[0].blobs['label'].data.shape) os.remove(f.name) os.remove(net_f)
Example #10
Source File: classify_caffe_server.py From BerryNet with GNU General Public License v3.0 | 6 votes |
def create_classifier(pretrained_model): """Creates a model from saved caffemodel file and returns a classifier.""" # Creates model from saved .caffemodel. # The following file are shipped inside caffe-doc Debian package model_def = os.path.join("/", "usr", "share", "doc", "caffe-doc", "models","bvlc_reference_caffenet", "deploy.prototxt") image_dims = [ 256, 256 ] # The following file are shipped inside python3-caffe-cpu Debian package mean = np.load(os.path.join('/', 'usr', 'lib', 'python3', 'dist-packages', 'caffe', 'imagenet', 'ilsvrc_2012_mean.npy')) channel_swap = [2, 1, 0] raw_scale = 255.0 caffe.set_mode_cpu() classifier = caffe.Classifier(model_def, pretrained_model, image_dims=image_dims, mean=mean, raw_scale=raw_scale, channel_swap=channel_swap) return classifier
Example #11
Source File: features.py From retrieval-2016-deepvision with MIT License | 6 votes |
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)
Example #12
Source File: loadcaffe.py From ADL with MIT License | 6 votes |
def load_caffe(model_desc, model_file): """ Load a caffe model. You must be able to ``import caffe`` to use this function. Args: model_desc (str): path to caffe model description file (.prototxt). model_file (str): path to caffe model parameter file (.caffemodel). Returns: dict: the parameters. """ with change_env('GLOG_minloglevel', '2'): import caffe caffe.set_mode_cpu() net = caffe.Net(model_desc, model_file, caffe.TEST) param_dict = CaffeLayerProcessor(net).process() logger.info("Model loaded from caffe. Params: " + ", ".join(sorted(param_dict.keys()))) return param_dict
Example #13
Source File: predict.py From iLID with MIT License | 6 votes |
def predict(sound_file, prototxt, model, output_path): image_files = wav_to_images(sound_file, output_path) caffe.set_mode_cpu() net = caffe.Classifier(prototxt, model, #image_dims=(224, 224) #channel_swap=(2,1,0), raw_scale=255 # convert 0..255 values into range 0..1 #caffe.TEST ) input_images = np.array([caffe.io.load_image(image_file, color=False) for image_file in image_files["melfilter"]]) #input_images = np.swapaxes(input_images, 1, 3) #prediction = net.forward_all(data=input_images)["prob"] prediction = net.predict(input_images, False) # predict takes any number of images, and formats them for the Caffe net automatically print prediction print 'prediction shape:', prediction[0].shape print 'predicted class:', prediction[0].argmax() print image_files return prediction
Example #14
Source File: loadcaffe.py From petridishnn with MIT License | 6 votes |
def load_caffe(model_desc, model_file): """ Load a caffe model. You must be able to ``import caffe`` to use this function. Args: model_desc (str): path to caffe model description file (.prototxt). model_file (str): path to caffe model parameter file (.caffemodel). Returns: dict: the parameters. """ with change_env('GLOG_minloglevel', '2'): import caffe caffe.set_mode_cpu() net = caffe.Net(model_desc, model_file, caffe.TEST) param_dict = CaffeLayerProcessor(net).process() logger.info("Model loaded from caffe. Params: " + ", ".join(sorted(param_dict.keys()))) return param_dict
Example #15
Source File: loadcaffe.py From ternarynet with Apache License 2.0 | 5 votes |
def load_caffe(model_desc, model_file): """ return a dict of params """ param_dict = {} param_processors = get_processor() with change_env('GLOG_minloglevel', '2'): import caffe caffe.set_mode_cpu() net = caffe.Net(model_desc, model_file, caffe.TEST) layer_names = net._layer_names blob_names = net.blobs.keys() for layername, layer in zip(layer_names, net.layers): try: prev_blob_name = blob_names[blob_names.index(layername)-1] prev_data_shape = net.blobs[prev_blob_name].data.shape[1:] except ValueError: prev_data_shape = None logger.info("Processing layer {} of type {}".format( layername, layer.type)) if layer.type in param_processors: param_dict.update(param_processors[layer.type]( layername, layer.blobs, prev_data_shape)) else: if len(layer.blobs) != 0: logger.warn("Layer type {} not supported!".format(layer.type)) logger.info("Model loaded from caffe. Params: " + \ " ".join(sorted(param_dict.keys()))) return param_dict
Example #16
Source File: style_transfer.py From style_transfer with MIT License | 5 votes |
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 = {} for layer in model.layers(): shapes[layer] = model.data[layer].shape resp_q.put(shapes)
Example #17
Source File: style_transfer.py From style_transfer with MIT License | 5 votes |
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
Example #18
Source File: predict.py From personal-photos-model with Apache License 2.0 | 5 votes |
def test_clusters(data=None, weight_file=constants.TRAINED_WEIGHTS): """ Tests a few people to see how they cluster across the training and validation data, producing image files. """ print "Generating cluster details..." cluster_details = data.get_clustered_faces() print "\tInitializing Caffe using weight file %s..." % (weight_file) caffe.set_mode_cpu() net = caffe.Net(constants.TRAINED_MODEL, weight_file, caffe.TEST) test_cluster(net, cluster_details["train"], "train") test_cluster(net, cluster_details["validation"], "validation")
Example #19
Source File: hand_model.py From region-ensemble-network with GNU General Public License v2.0 | 5 votes |
def __init__(self, dataset, model, center_loader=None, param=None, use_gpu=False): self._dataset = dataset self._center_loader = center_loader proto_name, model_name = util.get_model(dataset, model) self._fx, self._fy, self._ux, self._uy = util.get_param(dataset) if param is None else param self._net = caffe.Net(proto_name, caffe.TEST, weights=model_name) self._input_size = self._net.blobs['data'].shape[-1] self._cube_size = 150 if use_gpu: caffe.set_mode_gpu() caffe.set_device(0) else: caffe.set_mode_cpu()
Example #20
Source File: caffe_parser.py From SNIPER-mxnet with Apache License 2.0 | 5 votes |
def read_caffemodel(prototxt_fname, caffemodel_fname): """Return a caffe_pb2.NetParameter object that defined in a binary caffemodel file """ if use_caffe: caffe.set_mode_cpu() net = caffe.Net(prototxt_fname, caffemodel_fname, caffe.TEST) layer_names = net._layer_names layers = net.layers return (layers, layer_names) else: proto = caffe_pb2.NetParameter() with open(caffemodel_fname, 'rb') as f: proto.ParseFromString(f.read()) return (get_layers(proto), None)
Example #21
Source File: predict.py From dilation with MIT License | 5 votes |
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)
Example #22
Source File: mtcnn_inference.py From uai-sdk with Apache License 2.0 | 5 votes |
def load_model(self): caffe_model_path = "./model" 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) self._PNet = PNet self._RNet = RNet self._ONet = ONet
Example #23
Source File: demo_service.py From uai-sdk with Apache License 2.0 | 5 votes |
def load_model(self): caffe.set_mode_cpu() text_proposals_detector=TextProposalDetector(CaffeModel(NET_DEF_FILE, MODEL_FILE)) self.text_detector=TextDetector(text_proposals_detector)
Example #24
Source File: train.py From uai-sdk with Apache License 2.0 | 5 votes |
def cpu_solve(proto, snapshot, timing): caffe.set_mode_cpu() solver = caffe.SGDSolver(proto) if snapshot and len(snapshot) != 0: solver.restore(snapshot) solver.step(solver.param.max_iter)
Example #25
Source File: train.py From uai-sdk with Apache License 2.0 | 5 votes |
def cpu_solve(proto, snapshot, timing): caffe.set_mode_cpu() solver = caffe.SGDSolver(proto) if snapshot and len(snapshot) != 0: solver.restore(snapshot) solver.step(solver.param.max_iter)
Example #26
Source File: train.py From uai-sdk with Apache License 2.0 | 5 votes |
def cpu_solve(proto, snapshot, timing): caffe.set_mode_cpu() solver = caffe.SGDSolver(proto) if snapshot and len(snapshot) != 0: solver.restore(snapshot) solver.step(solver.param.max_iter)
Example #27
Source File: train_large_file.py From uai-sdk with Apache License 2.0 | 5 votes |
def cpu_solve(proto, snapshot, timing): caffe.set_mode_cpu() solver = caffe.SGDSolver(proto) if snapshot and len(snapshot) != 0: solver.restore(snapshot) solver.step(solver.param.max_iter)
Example #28
Source File: ioutil.py From open-vot with MIT License | 5 votes |
def load_goturn_from_caffe(net_path, proto_path, model): import caffe caffe.set_mode_cpu() net = caffe.Net(proto_path, net_path, caffe.TEST) params = net.params conv_branches = [model.branch_z, model.branch_x] for i, branch in enumerate(conv_branches): if i == 0: param_names = ['conv1', 'conv2', 'conv3', 'conv4', 'conv5'] else: param_names = ['conv1_p', 'conv2_p', 'conv3_p', 'conv4_p', 'conv5_p'] conv_layers = [ net.conv1[0], net.conv2[0], net.conv3[0], net.conv4[0], net.conv5[0]] for l, conv in enumerate(conv_layers): name = param_names[l] conv.weight.data[:] = torch.from_numpy(params[name][0].data) conv.bias.data[:] = torch.from_numpy(params[name][1].data) fc_layers = [ model.fc[0], model.fc7[0], model.fc7b[0], model.fc8[0]] params_names = ['fc6-new', 'fc7-new', 'fc7-newb', 'fc8-shapes'] for l, fc in enumerate(fc_layers): name = param_names[l] fc.weight.data[:] = torch.from_numpy(params[name][0].data) fc.bias.data[:] = torch.from_numpy(params[name][1].data) return model
Example #29
Source File: caffe_parser.py From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 | 5 votes |
def read_caffemodel(prototxt_fname, caffemodel_fname): """Return a caffe_pb2.NetParameter object that defined in a binary caffemodel file """ if use_caffe: caffe.set_mode_cpu() net = caffe.Net(prototxt_fname, caffemodel_fname, caffe.TEST) layer_names = net._layer_names layers = net.layers return (layers, layer_names) else: proto = caffe_pb2.NetParameter() with open(caffemodel_fname, 'rb') as f: proto.ParseFromString(f.read()) return (get_layers(proto), None)
Example #30
Source File: tester.py From mix-and-match with MIT License | 5 votes |
def __init__(self, net, weights, gpu=-1): if gpu != -1: caffe.set_mode_gpu() caffe.set_device(gpu) else: caffe.set_mode_cpu() caffe.Net.__init__(self, net, weights, caffe.TEST)