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类Net()的实例源码
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
项目源码
文件源码
阅读 25
收藏 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, 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,
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 gen_net():
caffe.set_device(1)
caffe.set_mode_gpu()
filename = '2007_000032.jpg'
im = Image.open(filename)
m = np.asarray(im, dtype=np.float32)
m = m[:,:,::-1]
m -= np.array((104.00698793,116.66876762,122.67891434))
m = m.transpose((2, 0, 1))
net = caffe.Net(
"deploy.prototxt",
#"train_iter_" + str(num) + ".caffemodel",
#"/data/VGG16/caffemodel",
"good.caffemodel",
caffe.TRAIN)
net.blobs["data"].reshape(1, *m.shape)
net.blobs["data"].data[...] = m
net.forward()
return net
def gen_net(num):
caffe.set_device(0)
caffe.set_mode_gpu()
filename = '2007_000032.jpg'
im = Image.open(filename)
m = np.asarray(im, dtype=np.float32)
m = m[:,:,::-1]
m -= np.array((104.00698793,116.66876762,122.67891434))
m = m.transpose((2, 0, 1))
net = caffe.Net(
"train_val.prototxt",
"train_iter_" + str(num) + ".caffemodel",
# "/data/VGG16/caffemodel",
# "../fcn-32s/good.caffemodel",
caffe.TRAIN)
net.blobs["data"].reshape(1, *m.shape)
net.blobs["data"].data[...] = m
net.forward()
return net
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 build(self, network_filename='inception.prototxt'):
"""main method."""
network = self._build_network()
print network
with open(network_filename, 'w') as network_file:
network_file.write(text_format.MessageToString(network))
return Net(network_filename)
#if __name__ == '__main__':
#__inception_builder__ = InceptionBuilder(
#training_source='examples/imagenet/ilsvrc12_train_lmdb',
#training_batch_size=32,
#testing_source='examples/imagenet/ilsvrc12_val_lmdb',
#testing_batch_size=6,
#task_name='ilsvrc12',
#mean_file='data/ilsvrc12/imagenet_mean.binaryproto',
#labels=[('class_id', 1000)],
#)
#__inception_builder__.build()
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 process_patches(images, net, transformer):
""" Process a patch through the neural network and extract the predictions
Args:
images (array list): list of images to process (length = batch_size)
net (obj): the Caffe Net
transformer (obj): the Caffe Transformer for preprocessing
"""
# caffe.io.load_image converts to [0,1], so our transformer sets it back to [0,255]
# but the skimage lib already works with [0, 255] so we convert it to float with img_as_float
data = np.zeros(net.blobs['data'].data.shape)
for i in range(len(images)):
data[i] = transformer.preprocess('data', img_as_float(images[i]))
net.forward(data=data)
output = net.blobs['conv1_1_D'].data[:len(images)]
output = np.swapaxes(np.swapaxes(output, 1, 3), 1, 2)
return output
def process_patches(images, net, transformer):
""" Process a patch through the neural network and extract the predictions
Args:
images (array list): list of images to process (length = batch_size)
net (obj): the Caffe Net
transformer (obj): the Caffe Transformer for preprocessing
"""
# caffe.io.load_image converts to [0,1], so our transformer sets it back to [0,255]
# but the skimage lib already works with [0, 255] so we convert it to float with img_as_float
data = np.zeros(net.blobs['data'].data.shape)
for i in range(len(images)):
data[i] = transformer.preprocess('data', img_as_float(images[i]))
net.forward(data=data)
output = net.blobs['conv1_1_D'].data[:len(images)]
output = np.swapaxes(np.swapaxes(output, 1, 3), 1, 2)
return output
def __init__(self, deploy, weights, mean=(0, 0, 0), shapes=None, placeholder=False):
self.deploy = deploy
self.weights = weights
self.mean = np.float32(mean).reshape((3, 1, 1))
self.bgr = True
self.shapes = shapes
self.last_layer = None
if shapes:
self.last_layer = list(shapes)[-1]
if not placeholder:
import caffe
self.net = caffe.Net(self.deploy, 1, weights=self.weights)
self.data = LayerIndexer(self.net, 'data')
self.diff = LayerIndexer(self.net, 'diff')
self.contents = []
self.styles = []
self.img = None
self._arr_pool = ArrayPool()
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 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
项目源码
文件源码
阅读 31
收藏 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():
# Per https://www.tensorflow.org/versions/r0.11/api_docs/python/nn.html#conv3d
# Filter has shape: [filter_depth, filter_height, filter_width, in_channels, out_channels]
net = caffe.Net(model, weights)
netdata = dict()
for layer in layers:
print "{}: w_shape, b_shape={}, {}".format(layer, net.params[layer][0].data.shape, net.params[layer][1].data.shape)
# per https://github.com/tensorflow/tensorflow/blob/master/tensorflow/g3doc/api_docs/python/functions_and_classes/shard5/tf.nn.conv3d.md
# filter: A Tensor. Must have the same type as input. Shape [filter_depth, filter_height, filter_width, in_channels, out_channels]. in_channels must match between input and filter.
if 'conv' in layer:
w = np.transpose(net.params[layer][0].data, (2, 3, 4, 1, 0))
elif 'fc' in layer:
w = net.params[layer][0].data[0, 0, 0, :, :].T
b = np.squeeze(net.params[layer][1].data)
netdata.update({layer: (w, b)})
np.save(output, netdata)