Python caffe 模块,Net() 实例源码
我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用caffe.Net()。
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, mean_file))
# Init caffe with model
self.net_ = caffe.Net(model_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
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, 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, 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 __init__(self, 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, 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, 1))
net = caffe.Net(
"train_val.prototxt",
"train_iter_" + str(num) + ".caffemodel",
# "/data/VGG16/caffemodel",
# "../fcn-32s/good.caffemodel", *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, prune_layers):
test_net = caffe.Net(test_prototx, caffe.TEST)
return prune(threshold, test_net, prune_layers)
accuracy = []
for threshold in threshold_list:
results = net_prune(threshold, 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, mean_value=None,
layer=['pool5'], input_size = None ):
caffe.set_mode_gpu()
caffe.Net.__init__(self, 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',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',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',1))
transformer.set_mean('data',np.array([129.1863,104.7624,93.5940]))
# BGR -> RGB
transformer.set_channel_swap('data',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), 2)
return output
def __init__(self, deploy, mean=(0, 0), shapes=None, placeholder=False):
self.deploy = deploy
self.weights = weights
self.mean = np.float32(mean).reshape((3, 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, 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_out)
out[tf_param + '/weights'] = net.params[caffe_param][0].data.transpose((2, 3, 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, caffe.TEST)
net.blobs['data'].data[0] = img_p.transpose((2, 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, 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, caffe.TEST)
# Transformer function to perform image transformation
def get_net(caffemodel,use the GPU for inference
"""
if use_gpu:
caffe.set_mode_gpu()
# load a new model
return caffe.Net(deploy_file, 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, 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 __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, 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
def get_net(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,out_channels]. in_channels must match between input and filter.
if 'conv' in layer:
w = np.transpose(net.params[layer][0].data, 4, 0))
elif 'fc' in layer:
w = net.params[layer][0].data[0, :, :].T
b = np.squeeze(net.params[layer][1].data)
netdata.update({layer: (w, b)})
np.save(output, netdata)
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, 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
def dump_caffemodel_weights():
net = caffe.Net(args.prototxt_path, args.caffemodel_path, caffe.TEST)
weights = {}
n_layers = len(net.layers)
for i in range(n_layers):
layer_name = net._layer_names[i]
layer = net.layers[i]
layer_blobs = [o.data for o in layer.blobs]
weights[layer_name] = layer_blobs
joblib.dump(weights, args.caffe_weights_path)
def __init__(self, with_rpn=True, net='zf', model_dir='pascal_voc', opt_dir='fast_rcnn_end2end'):
"""
net: vgg16,zf
model_dir: [pascal_voc,coco]
opt_dir: [fast_rcnn,fast_rcnn_alt_opt,fast_rcnn_end2end]
"""
model_file = os.path.join(
rcnn_dir, model_dir,
FasterRCNNDescription.NETS[net][0],
opt_dir, 'faster_rcnn_models',
FasterRCNNDescription.NETS[net][1])
if not os.path.exists(model_file) or \
not os.path.exists(pretrained_file):
raise ValueError('UnkNown net {},
FasterRCNNDescription.NETS.keys(), pretrained_file))
# Init caffe with model
cfg.TEST.HAS_rpn = with_rpn
cfg.TEST.BBox_REG = False
caffe.Net.__init__(self, caffe.TEST)
def __init__(self, weights_file):
if not os.path.exists(model_file) or \
not os.path.exists(weights_file):
raise ValueError('Invalid model: {},\nweights file: {}'
.format(model_file, weights_file))
# Init caffe with model
self.net_ = caffe.Net(model_file, caffe.TEST)
self.input_shape_ = self.net_.blobs['data'].data.shape
def rpn_generate(queue=None, imdb_name=None, rpn_model_path=None, cfg=None,
rpn_test_prototxt=None):
"""Use a trained rpn to generate proposals.
"""
cfg.TEST.rpn_PRE_NMS_TOP_N = -1 # no pre NMS filtering
cfg.TEST.rpn_POST_NMS_TOP_N = 2000 # limit top Boxes after NMS
print 'rpn model: {}'.format(rpn_model_path)
print('Using config:')
pprint.pprint(cfg)
import caffe
_init_caffe(cfg)
# NOTE: the matlab implementation computes proposals on flipped images,too.
# We compute them on the image once and then flip the already computed
# proposals. This might cause a minor loss in mAP (less proposal jittering).
imdb = get_imdb(imdb_name)
print 'Loaded dataset `{:s}` for proposal generation'.format(imdb.name)
# Load rpn and configure output directory
rpn_net = caffe.Net(rpn_test_prototxt, rpn_model_path, caffe.TEST)
output_dir = get_output_dir(imdb)
print 'Output will be saved to `{:s}`'.format(output_dir)
# Generate proposals on the imdb
rpn_proposals = imdb_proposals(rpn_net, imdb)
# Write proposals to disk and send the proposal file path through the
# multiprocessing queue
rpn_net_name = os.path.splitext(os.path.basename(rpn_model_path))[0]
rpn_proposals_path = os.path.join(
output_dir, rpn_net_name + '_proposals.pkl')
with open(rpn_proposals_path, 'wb') as f:
cPickle.dump(rpn_proposals, f, cPickle.HIGHEST_PROTOCOL)
print 'Wrote rpn proposals to {}'.format(rpn_proposals_path)
queue.put({'proposal_path': rpn_proposals_path})
def setup():
global resnet_mean
global resnet_net
global vqa_net
# data provider
vqa_data_provider_layer.CURRENT_DATA_SHAPE = EXTRACT_LAYER_SIZE
# mean substraction
blob = caffe.proto.caffe_pb2.BlobProto()
data = open( resnet_MEAN_PATH , 'rb').read()
blob.ParseFromString(data)
resnet_mean = np.array( caffe.io.blobproto_to_array(blob)).astype(np.float32).reshape(3,224,224)
resnet_mean = np.transpose(cv2.resize(np.transpose(resnet_mean,(1,0)), (448,448)),(2,1))
# resnet
caffe.set_device(GPU_ID)
caffe.set_mode_gpu()
resnet_net = caffe.Net(resnet_LARGE_PROTOTXT_PATH, resnet_CAFFEMODEL_PATH, caffe.TEST)
# our net
vqa_net = caffe.Net(VQA_PROTOTXT_PATH, VQA_CAFFEMODEL_PATH, caffe.TEST)
# uploads
if not os.path.exists(UPLOAD_FOLDER):
os.makedirs(UPLOAD_FOLDER)
if not os.path.exists(VIZ_FOLDER):
os.makedirs(VIZ_FOLDER)
print 'Finished setup'
def Feed_net(model_file, imagemean_file, image_files, show_pred):
"""Feed network"""
n_files = len(image_files)
net = caffe.Net(deploy_file, caffe.TEST)
# define transformer for preprocessing
transformer = caffe.io.Transformer({'data': net.blobs['data'].data.shape})
transformer.set_mean('data', np.load(imagemean_file).mean(1).mean(1))
transformer.set_transpose('data', 1))
transformer.set_channel_swap('data', 0))
transformer.set_raw_scale('data', 255.0)
net.blobs['data'].reshape(n_files, 227, 227)
idx = 0
for image in image_files:
try:
im = caffe.io.load_image(image)
transformed_im = transformer.preprocess('data', im)
net.blobs['data'].data[idx, :] = transformed_im
idx += 1
except Exception:
pass
out = net.forward()
if show_pred:
print(out['prob'].argmax())
return net
def main():
"""Extract and save network skeleton with the corresponding weights.
Raises:
ImportError: PyCaffe module is not found."""
args = get_arguments()
sys.path.append(args.pycaffe_path)
try:
import caffe
except ImportError:
raise
# Load net deFinition.
net = caffe.Net('./util/deploy.prototxt', args.caffemodel, caffe.TEST)
# Check the existence of output_dir.
if not os.path.exists(args.output_dir):
os.makedirs(args.output_dir)
# Net skeleton with parameters names and shapes.
# In TF,the filter shape is as follows: [ks,ks,input_channels,output_channels],
# while in Caffe it looks like this: [output_channels,ks].
net_skeleton = list()
for name, item in net.params.iteritems():
net_skeleton.append([name + '/w', item[0].data.shape[::-1]]) # See the explanataion on filter formats above.
net_skeleton.append([name + '/b', item[1].data.shape])
with open(os.path.join(args.output_dir, 'net_skeleton.ckpt'), 'wb') as f:
cPickle.dump(net_skeleton, protocol=cPickle.HIGHEST_PROTOCOL)
# Net weights.
net_weights = dict()
for name, item in net.params.iteritems():
net_weights[name + '/w'] = item[0].data.transpose(2, 0) # See the explanation on filter formats above.
net_weights[name + '/b'] = item[1].data
with open(os.path.join(args.output_dir,'net_weights.ckpt'), 'wb') as f:
cPickle.dump(net_weights, protocol=cPickle.HIGHEST_PROTOCOL)
del net, net_skeleton, net_weights
def create_models_tiny(phase = caffe.TEST):
baseDir = os.path.dirname(os.path.abspath(__file__))
proposal_net = caffe.Net('{0}/models/tiny.prototxt'.format(baseDir), '{0}/models/tiny.caffemodel'.format(baseDir), phase)
recog = caffe.Net('{0}/models/model_cz.prototxt'.format(baseDir), '{0}/models/model.caffemodel'.format(baseDir), phase)
return proposal_net, recog
def __init__(self, prototxt, mean, use_gpu=False):
self.net = caffe.Net(prototxt, caffe.TEST)
if use_gpu:
caffe.set_mode_gpu()
if type(mean) == str:
if mean.endswith('.binaryproto'):
self.mean = CaffeAdapter._load_binaryproto(mean)
elif mean.endswith('.npy'):
self.mean = np.load(mean)
else:
raise ValueError('UnkNown mean file format. KNown formats: .binaryproto,.npy')
elif type(mean) == np.ndarray:
self.mean = mean
elif mean is not None:
raise ValueError('UnkNown mean format. Expected .binaryproto/.npy file or numpy array.')
self.transformer = caffe.io.Transformer({'data': self.net.blobs['data'].data.shape})
self.transformer.set_transpose('data', 1))
if self.mean is not None:
self.transformer.set_mean('data', self.mean.mean(1).mean(1))
else:
print('Warning. No mean specified.')
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', 0)) # the reference model has channels in BGR order instead of RGB
self.layer_types = self._load_layer_types(prototxt)
self.ready = False
self.use_gpu = use_gpu
def __init__(self, weight, K, num_act, num_step=1, data_path='test'):
self.K = K
self.num_act = num_act
self.num_step = num_step
caffe.set_mode_gpu()
caffe.set_device(0)
test_net_file, net_proto = N.create_netfile(1, data_path,
1, num_step=self.num_step, mode='test')
self.test_net = caffe.Net(test_net_file, caffe.TEST)
self.test_net.copy_from(weight)
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, caffe.TEST)
def rpn_generate(queue=None, caffe.TEST)
output_dir = get_output_dir(imdb, None)
print 'Output will be saved to `{:s}`'.format(output_dir)
# Generate proposals on the imdb
rpn_proposals = imdb_proposals(rpn_net, cPickle.HIGHEST_PROTOCOL)
print 'Wrote rpn proposals to {}'.format(rpn_proposals_path)
queue.put({'proposal_path': rpn_proposals_path})
def dump_caffemodel_weights():
net = caffe.Net(args.prototxt_path, args.caffe_weights_path)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。