def __init__(self, model_file, weights_file, mean_file):
if not os.path.exists(model_file) or \
not os.path.exists(weights_file) or \
not os.path.exists(mean_file):
raise ValueError('Invalid model: {}, \nweights file: {}, \nmean file: {}'
.format(model_file, weights_file, mean_file))
# Init caffe with model
self.net_ = caffe.Net(model_file, weights_file, caffe.TEST)
self.mean_file_ = mean_file
self.input_shape_ = self.net_.blobs['data'].data.shape
# Initialize mean file
blob_meanfile = caffe.proto.caffe_pb2.BlobProto()
data_meanfile = open(mean_file , 'rb' ).read()
blob_meanfile.ParseFromString(data_meanfile)
meanfile = np.squeeze(np.array(caffe.io.blobproto_to_array(blob_meanfile)))
self.meanfile_ = meanfile.transpose((1,2,0))
self.meanfile_image_ = None
python类TEST的实例源码
def __init__(self, rcnn_dir, net='vgg_cnn_m_1024'):
model_file = os.path.join(
rcnn_dir, 'models',
FastRCNNDescription.NETS[net][0], 'test.prototxt')
pretrained_file = os.path.join(
rcnn_dir, 'data', 'fast_rcnn_models',
FastRCNNDescription.NETS[net][1])
if not os.path.exists(model_file) or \
not os.path.exists(pretrained_file):
raise ValueError('Unknown net {}, use one of {}, \n'
'model: {}, \npretrained file: {}'
.format(net,
FastRCNNDescription.NETS.keys(),
model_file, pretrained_file))
# Init caffe with model
cfg.TEST.BBOX_REG = False
caffe.Net.__init__(self, model_file, pretrained_file, caffe.TEST)
def __init__(self, net_proto, net_weights, device_id, input_size=None):
caffe.set_mode_gpu()
caffe.set_device(device_id)
self._net = caffe.Net(net_proto, net_weights, caffe.TEST)
input_shape = self._net.blobs['data'].data.shape
if input_size is not None:
input_shape = input_shape[:2] + input_size
transformer = caffe.io.Transformer({'data': input_shape})
if self._net.blobs['data'].data.shape[1] == 3:
transformer.set_transpose('data', (2, 0, 1)) # move image channels to outermost dimension
transformer.set_mean('data', np.array([104, 117, 123])) # subtract the dataset-mean value in each channel
else:
pass # non RGB data need not use transformer
self._transformer = transformer
self._sample_shape = self._net.blobs['data'].data.shape
action_caffe.py 文件源码
项目:Video-Classification-Action-Recognition
作者: qijiezhao
项目源码
文件源码
阅读 26
收藏 0
点赞 0
评论 0
def __init__(self, net_proto, net_weights, device_id, input_size=None):
caffe.set_mode_gpu()
caffe.set_device(device_id)
self._net = caffe.Net(net_proto, net_weights, caffe.TEST)
input_shape = self._net.blobs['data'].data.shape
if input_size is not None:
input_shape = input_shape[:2] + input_size
transformer = caffe.io.Transformer({'data': input_shape})
if self._net.blobs['data'].data.shape[1] == 3:
transformer.set_transpose('data', (2, 0, 1)) # move image channels to outermost dimension
transformer.set_mean('data', np.array([104, 117, 123])) # subtract the dataset-mean value in each channel
else:
pass # non RGB data need not use transformer
self._transformer = transformer
self._sample_shape = self._net.blobs['data'].data.shape
def net(weights=WEIGHTS):
"""
Get the caffe net that has been trained to segment facade features.
This initializes or re-initializes the global network with weights. There are certainly side-effects!
The weights default to a caffe model that is part of the same sourcecode repository as this file.
They can be changed by setting the I12_WEIGHTS environment variable, by passing a command line argument
to some programs, or programatically (of course).
:param weights: The weights to use for the net.
:return:
"""
global WEIGHTS
global _net
if _net is None or weights != WEIGHTS:
if weights is not None:
WEIGHTS = weights
_net = caffe.Net(LAYERS, WEIGHTS, caffe.TEST)
return _net
def __init__(self,
minsize = 20,
threshold = [0.6, 0.7, 0.7],
factor = 0.709,
fastresize = False,
gpuid = 0):
self.minsize = minsize
self.threshold = threshold
self.factor = factor
self.fastresize = fastresize
model_P = './model/det1.prototxt'
weights_P = './model/det1.caffemodel'
model_R = './model/det2.prototxt'
weights_R = './model/det2.caffemodel'
model_O = './model/det3.prototxt'
weights_O = './model/det3.caffemodel'
caffe.set_mode_gpu()
caffe.set_device(gpuid)
self.PNet = caffe.Net(model_P, weights_P, caffe.TEST)
self.RNet = caffe.Net(model_R, weights_R, caffe.TEST)
self.ONet = caffe.Net(model_O, weights_O, caffe.TEST)
def load_nets(args, cur_gpu):
# initialize solver and feature net,
# RNN should be initialized before CNN, because CNN cudnn conv layers
# may assume using all available memory
caffe.set_mode_gpu()
caffe.set_device(cur_gpu)
solver = caffe.SGDSolver(args.solver)
if args.snapshot:
print "Restoring history from {}".format(args.snapshot)
solver.restore(args.snapshot)
rnn = solver.net
if args.weights:
rnn.copy_from(args.weights)
feature_net = caffe.Net(args.feature_net, args.feature_param, caffe.TEST)
# apply bbox regression normalization on the net weights
with open(args.bbox_mean, 'rb') as f:
bbox_means = cPickle.load(f)
with open(args.bbox_std, 'rb') as f:
bbox_stds = cPickle.load(f)
feature_net.params['bbox_pred_vid'][0].data[...] = \
feature_net.params['bbox_pred_vid'][0].data * bbox_stds[:, np.newaxis]
feature_net.params['bbox_pred_vid'][1].data[...] = \
feature_net.params['bbox_pred_vid'][1].data * bbox_stds + bbox_means
return solver, feature_net, rnn, bbox_means, bbox_stds
def load_models(args):
# load rnn model
caffe.set_mode_gpu()
if args.gpus is None:
caffe.set_device(args.job_id - 1)
else:
assert args.job_id <= len(args.gpus)
caffe.set_device(args.gpus[args.job_id-1])
if args.lstm_param is not '':
rnn_net = caffe.Net(args.lstm_def, args.lstm_param, caffe.TEST)
print 'Loaded RNN network from {:s}.'.format(args.lstm_def)
else:
rnn_net = caffe.Net(args.lstm_def, caffe.TEST)
print 'WARNING: dummy RNN network created.'
# load feature model
feature_net = caffe.Net(args.def_file, args.param, caffe.TEST)
print 'Loaded feature network from {:s}.'.format(args.def_file)
return feature_net, rnn_net
def add_batchnormscale(self, input, name):
if True : # necessary?
batch_norm_param={'moving_average_fraction': 0.95, 'use_global_stats': True }
param = [dict(lr_mult=0),dict(lr_mult=0),dict(lr_mult=0)]
l = L.BatchNorm(input, name=name+'_bn', batch_norm_param=batch_norm_param, param=param, include={'phase': caffe.TEST}, ntop=1)
setattr(self.net_spec, name+'_bn', l)
batch_norm_param={'moving_average_fraction': 0.95, 'use_global_stats': False }
l = L.BatchNorm(input, name=name+'_bn', top=name+'_bn', batch_norm_param=batch_norm_param, param=param, include={'phase': caffe.TRAIN}, ntop=0)
setattr(self.net_spec, name+'_bn' + '_train', l)
l = L.Scale(getattr(self.net_spec, name+'_bn'), scale_param = { 'bias_term': True } )
setattr(self.net_spec, name, l)
else : # here without split in use_global_stats True/False
l = L.Scale(L.BatchNorm(input), scale_param={'bias_term': True})
setattr(self.net_spec, name, l)
return l
def add_batchnormscale(self, input, name):
if True: # necessary?
batch_norm_param = {'moving_average_fraction': 0.95, 'use_global_stats': True}
param = [dict(lr_mult=0), dict(lr_mult=0), dict(lr_mult=0)]
l = L.BatchNorm(input, name=name + '_bn', batch_norm_param=batch_norm_param, param=param, include={'phase': caffe.TEST}, ntop=1)
setattr(self.net_spec, name + '_bn', l)
batch_norm_param = {'moving_average_fraction': 0.95, 'use_global_stats': False}
l = L.BatchNorm(input, name=name + '_bn', top=name + '_bn', batch_norm_param=batch_norm_param, param=param, include={'phase': caffe.TRAIN}, ntop=0)
setattr(self.net_spec, name + '_bn' + '_train', l)
l = L.Scale(getattr(self.net_spec, name + '_bn'), scale_param={'bias_term': True})
setattr(self.net_spec, name, l)
else: # here without split in use_global_stats True/False
l = L.Scale(L.BatchNorm(input), scale_param={'bias_term': True})
setattr(self.net_spec, name, l)
return l
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 eval_prune_threshold(threshold_list, test_prototxt, caffemodel, prune_layers):
def net_prune(threshold, test_prototx, caffemodel, prune_layers):
test_net = caffe.Net(test_prototx, caffemodel, caffe.TEST)
return prune(threshold, test_net, prune_layers)
accuracy = []
for threshold in threshold_list:
results = net_prune(threshold, test_prototxt, caffemodel, prune_layers)
print 'threshold: ', results[0]
print '\ntotal_percentage: ', results[1]
print '\npercentage_list: ', results[2]
print '\ntest_loss: ', results[3]
print '\naccuracy: ', results[4]
accuracy.append(results[4])
plt.plot(accuracy,'r.')
plt.show()
# ?????????
def __init__(self, model_file, pretrained_file, mean_value=None,
layer=['pool5'], input_size = None ):
caffe.set_mode_gpu()
caffe.Net.__init__(self, model_file, pretrained_file, caffe.TEST)
# get name input layer
self.list_layers = layer
self.mean_value = mean_value
# set transformer object
self.transformer = caffe.io.Transformer({'data': self.blobs['data'].data.shape})
self.transformer.set_transpose( 'data', (2,0,1) )
if mean_value is not None:
self.transformer.set_mean('data', mean_value)
self.transformer.set_raw_scale('data', 255)
self.transformer.set_channel_swap('data', (2,1,0))
if input_size is not None:
#reshape the input
print "New input! {}".format(input_size)
self.reshape_input( input_size[0], input_size[1], input_size[2], input_size[3] )
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 main():
# Create tempfile to hold prototxt
tmp = tempfile.NamedTemporaryFile(mode='w', delete=True)
# Parse prototxt and inject `vars`
proto = open(arch['DEPLOY_PROTOTXT']).readlines()
for line in proto:
for key, value in vars.items():
tag = "$%s$" % key
line = line.replace(tag, str(value))
tmp.write(line)
tmp.flush()
# Instantiate Caffe Model
net = caffe.Net(tmp.name, arch['CAFFEMODEL'], caffe.TEST)
out = {}
for (caffe_param, tf_param) in arch['PARAMS'].items():
# Caffe stores weights as (channels_out, channels_in, h, w)
# but TF expects (h, w, channels_in, channels_out)
out[tf_param + '/weights'] = net.params[caffe_param][0].data.transpose((2, 3, 1, 0))
out[tf_param + '/biases'] = net.params[caffe_param][1].data
np.save(FLAGS.out, out)
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 add_multilabel_data_layer(net, name, phase, num_classes, class_list=None):
""" Add a MultiLabelData layer """
include_dict = {'phase': phase}
param = {'num_classes': num_classes}
if phase == caffe.TRAIN:
param['stage'] = 'TRAIN'
elif phase == caffe.TEST:
param['stage'] = 'VAL'
if class_list is not None:
assert len(class_list) == num_classes, \
'Length of class list does not match number of classes {} vs {}'.\
format(len(class_list), num_classes)
param['class_list'] = class_list
param_str = yaml.dump(param)
net[name[0]], net[name[1]] = L.Python(name=name[0], python_param=dict(module='layers.multilabel_data',
layer='MultiLabelData', param_str=param_str), include=include_dict, ntop=2)
def add_singlelabel_data_layer(net, name, phase, num_classes, class_list=None):
""" Add a MultiLabelData layer """
include_dict = {'phase': phase}
param = {'num_classes': num_classes}
if phase == caffe.TRAIN:
param['stage'] = 'TRAIN'
elif phase == caffe.TEST:
param['stage'] = 'VAL'
if class_list is not None:
assert len(class_list) == num_classes, \
'Length of class list does not match number of classes {} vs {}'.\
format(len(class_list), num_classes)
param['class_list'] = class_list
param_str = yaml.dump(param)
net[name[0]], net[name[1]] = L.Python(name=name[0], python_param=dict(module='layers.singlelabel_data',
layer='SingleLabelData', param_str=param_str), include=include_dict, ntop=2)
def printNetwork_weights(prototxt_filename, caffemodel_filename):
'''
For each CNN layer, print weight heatmap and weight histogram
'''
net = caffe.Net(prototxt_filename,caffemodel_filename, caffe.TEST)
for layerName in net.params:
# display the weights
arr = net.params[layerName][0].data
plt.clf()
fig = plt.figure(figsize=(10,10))
ax = fig.add_subplot(111)
cax = ax.matshow(arr, interpolation='none')
fig.colorbar(cax, orientation="horizontal")
plt.savefig('{0}_weights_{1}.png'.format(caffemodel_filename, layerName), dpi=100, format='png', bbox_inches='tight') # use format='svg' or 'pdf' for vectorial pictures
plt.close()
# weights histogram
plt.clf()
plt.hist(arr.tolist(), bins=20)
plt.savefig('{0}_weights_hist_{1}.png'.format(caffemodel_filename, layerName), dpi=100, format='png', bbox_inches='tight') # use format='svg' or 'pdf' for vectorial pictures
plt.close()
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 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()
# load a new model
return caffe.Net(deploy_file, caffemodel, caffe.TEST)
def __init__(self, model_path,
weights_path,
mean_path=None,
image_scale=255.0,
batch_size=1,
input_shape=(227, 227)):
self.net = caffe.Net(model_path, # defines the structure of the model
weights_path, # contains the trained weights
caffe.TEST) # use test mode (e.g., don't perform dropout)
self.net.blobs['data'].reshape(batch_size, 3, input_shape[0], input_shape[1])
self.net.blobs['prob'].reshape(batch_size, )
self.mean_path = mean_path
self.image_scale = image_scale
self.batch_size = batch_size
self.transformer = self.set_transformer()
def get_aligner(caffe_model_path, use_more_stage=False):
caffe.set_mode_gpu()
# PNet = caffe.Net(caffe_model_path + "/det1.prototxt",
# caffe_model_path + "/det1.caffemodel", caffe.TEST)
if use_more_stage:
RNet = caffe.Net(caffe_model_path + "/det2.prototxt",
caffe_model_path + "/det2.caffemodel", caffe.TEST)
else:
RNet = None
ONet = caffe.Net(caffe_model_path + "/det3.prototxt",
caffe_model_path + "/det3.caffemodel", caffe.TEST)
LNet = caffe.Net(caffe_model_path + "/det4.prototxt",
caffe_model_path + "/det4.caffemodel", caffe.TEST)
# return (PNet, RNet, ONet)
return (RNet, ONet, LNet)
# return (RNet, ONet, None)
def get_aligner(caffe_model_path, use_more_stage=False):
caffe.set_mode_gpu()
# PNet = caffe.Net(caffe_model_path + "/det1.prototxt",
# caffe_model_path + "/det1.caffemodel", caffe.TEST)
if use_more_stage:
RNet = caffe.Net(caffe_model_path + "/det2.prototxt",
caffe_model_path + "/det2.caffemodel", caffe.TEST)
else:
RNet = None
ONet = caffe.Net(caffe_model_path + "/det3.prototxt",
caffe_model_path + "/det3.caffemodel", caffe.TEST)
LNet = caffe.Net(caffe_model_path + "/det4.prototxt",
caffe_model_path + "/det4.caffemodel", caffe.TEST)
# return (PNet, RNet, ONet)
return (RNet, ONet, LNet)
# return (RNet, ONet, None)
def __load_caffe_model(self, blob_path, prototxt_path):
"""
Load caffe model to memory
Args:
blob_path: Model in HDF5 format
prototxt_path: Prototxt file. Contains Network implementation
Returns:
"""
net = caffe.Net(prototxt_path, blob_path, caffe.TEST)
input_layer = net.inputs[0]
output_layer = net.outputs[0]
#height = net.blobs["data_q"].data.shape[2]
#width = net.blobs["data_q"].data.shape[3]
height = net.blobs[input_layer].data.shape[2]
width = net.blobs[input_layer].data.shape[3]
self.logger.info("Model has been successfully loaded from Blob:" + blob_path + " , Prototxt:" + prototxt_path)
return net, height, width
classifier.py 文件源码
项目:Barebones-Flask-and-Caffe-Classifier
作者: alex-paterson
项目源码
文件源码
阅读 25
收藏 0
点赞 0
评论 0
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()
# load a new model
return caffe.Net(deploy_file, caffemodel, caffe.TEST)
def main(argv):
sport = 'long_jump'
model = 'snap_iter_50000.caffemodel'
#---
weights = model_root + 'fcn/' + sport + '/' + model
netf = './fcn/' + sport + '/deploy.prototxt'
gpu = 0
caffe.set_device(gpu)
caffe.set_mode_gpu()
net = caffe.Net(netf, weights, caffe.TEST)
im_head = '/export/home/mfrank/data/OlympicSports/clips/'
im_head = '/export/home/mfrank/data/OlympicSports/patches/'
test_path_file = 'fcn/' + sport + '/test.txt'
train_path_file = 'fcn/' + sport + '/train.txt'
inferfile(net, train_path_file, im_head)
ifp_morris.apply_overlayfcn(train_path_file, factor=4)
inferfile(net, test_path_file, im_head)
ifp_morris.apply_overlayfcn(test_path_file, factor=4)
def loadLayers(self):
mname = str(self.ui.comboBoxModel.currentText())
wname = str(self.ui.comboBoxWeights.currentText())
self.net = caffe.Net(mname,wname , caffe.TEST)
out = self.net.blobs
self.layerList = out.keys()
self.ui.comboBoxLayers.clear()
for ln in self.layerList :
self.ui.comboBoxLayers.addItem(ln)
self.ui.plainTextEdit.clear()
self.ui.plainTextEdit.appendPlainText('Caffe Model Loaded...')
self.ui.plainTextEdit.appendPlainText(' Model Name : '+mname)
self.ui.plainTextEdit.appendPlainText(' Weights Name : '+wname)
self.ui.plainTextEdit.appendPlainText("Network Layers ...")
for name, layer in zip(self.net._layer_names, self.net.layers):
if not name in self.layerList :
continue
msg = " "+name +" --> "+str(layer.type) +" --> "+ str((self.net.blobs[name].data[0]).shape)
self.ui.plainTextEdit.appendPlainText(msg)
self.isModelLoaded = True
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 load_caffe(path_prototxt='weights/resnetFCN.prototxt',
path_weights='weights/resnetFCN.caffemodel',
out_path='weights/resnetFCN.npy',
version='V1'):
# Load the caffe network
print (' --> Loading the caffe weights...')
net_caffe = caffe.Net(path_prototxt, path_weights, caffe.TEST)
layers_caffe = dict(zip(list(net_caffe._layer_names), net_caffe.layers))
# Convert weights
print (' --> Converting the caffe weights to numpy...')
weights_caffe = convert_weights(layers_caffe, v=version)
# Save weights
print (' --> Saving the weights in numpy...')
np.save(out_path, weights_caffe)
# Entry point of the script