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)
python类Net()的实例源码
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, rcnn_dir, 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, 'models', model_dir,
FasterRCNNDescription.NETS[net][0],
opt_dir, 'test.prototxt')
pretrained_file = os.path.join(
rcnn_dir, 'data', '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 {}, use one of {}, \n'
'model: {}, \npretrained file: {}'
.format(net,
FasterRCNNDescription.NETS.keys(),
model_file, pretrained_file))
# Init caffe with model
cfg.TEST.HAS_RPN = with_rpn
cfg.TEST.BBOX_REG = False
caffe.Net.__init__(self, model_file, pretrained_file, caffe.TEST)
def __init__(self, model_file, 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, weights_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,2,0)), (448,448)),(2,0,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, deploy_file, imagemean_file, image_files, show_pred):
"""feed network"""
n_files = len(image_files)
net = caffe.Net(deploy_file, model_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', (2, 0, 1))
transformer.set_channel_swap('data', (2, 1, 0))
transformer.set_raw_scale('data', 255.0)
net.blobs['data'].reshape(n_files, 3, 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
train_faster_rcnn_alt_opt.py 文件源码
项目:faster-rcnn-resnet
作者: Eniac-Xie
项目源码
文件源码
阅读 20
收藏 0
点赞 0
评论 0
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 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, input_channels, ks, 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, f, 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, 3, 1, 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, f, 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, caffemodel, mean, use_gpu=False):
self.net = caffe.Net(prototxt, caffemodel, 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', (2, 0, 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', (2, 1, 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, prototxt, model, gpu_id=-1):
caffe.Net.__init__(self, prototxt, model)
self.set_phase_test()
if gpu_id < 0:
self.set_mode_cpu()
else:
self.set_mode_gpu()
self.set_device(gpu_id)
test_caffe.py 文件源码
项目:tensorflow-action-conditional-video-prediction
作者: williamd4112
项目源码
文件源码
阅读 24
收藏 0
点赞 0
评论 0
def __init__(self, mean, 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, mean, K, K,
1, num_act, 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, model, caffe.TEST)
def __init__(self, mean, 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, mean, K, K,
1, num_act, num_step=self.num_step, mode='test')
self.test_net = caffe.Net(test_net_file, caffe.TEST)
self.test_net.copy_from(weight)
train_faster_rcnn_alt_opt.py 文件源码
项目:py-faster-rcnn-tk1
作者: joeking11829
项目源码
文件源码
阅读 27
收藏 0
点赞 0
评论 0
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, None)
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 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)