def __init__(self):
# load MS COCO labels
labelmap_file = os.path.join(CAFFE_ROOT, LABEL_MAP)
file = open(labelmap_file, 'r')
self._labelmap = caffe_pb2.LabelMap()
text_format.Merge(str(file.read()), self._labelmap)
model_def = os.path.join(CAFFE_ROOT, PROTO_TXT)
model_weights = os.path.join(CAFFE_ROOT, CAFFE_MODEL)
self._net = caffe.Net(model_def, model_weights, caffe.TEST)
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([104, 117, 123]))
self._transformer.set_raw_scale('data', 255)
self._transformer.set_channel_swap('data', (2, 1, 0))
# set net to batch size of 1
image_resize = IMAGE_SIZE
self._net.blobs['data'].reshape(1, 3, image_resize, image_resize)
python类TEST的实例源码
def _loadModel(self, model_dirs, id):
print 'loading model...from{}'.format(model_dirs)
model_file = osp.join(model_dirs, 'vgg16.prototxt')
model_weights = osp.join(model_dirs, 'vgg16.caffemodel')
mean_file = osp.join(model_dirs, 'vgg16_mean.npy')
if id == -1:
caffe.set_mode_cpu()
else:
caffe.set_mode_gpu()
caffe.set_device(id)
net = caffe.Net(model_file, model_weights, caffe.TEST)
transformer = caffe.io.Transformer({'data': net.blobs['data'].data.shape})
transformer.set_mean('data', np.load(mean_file).mean(1).mean(1))
transformer.set_channel_swap('data', (2, 1, 0))
transformer.set_transpose('data', (2, 0, 1))
#transformer.set_raw_scale('data', 255)
self.net = net
self.transformer = transformer
self.style_layers = VGG16_STYLES
self.content_layers = VGG16_CONTENTS
self.layers = VGG16_LAYERS
print 'model loading done'
def __init__(self, model_path, deploy_path, mean, crop = 227, layer = 'fc7'):
self.net = caffe.Net(deploy_path, model_path, caffe.TEST)
self.transformer = caffe.io.Transformer({'data': self.net.blobs['data'].data.shape})
self.transformer.set_mean('data', mean)
self.transformer.set_transpose('data', (2, 0, 1))
self.transformer.set_channel_swap('data', (2, 1, 0))
self.transformer.set_raw_scale('data', 255.0)
self.crop = crop
self.image = []
self.layer = layer
caffe.set_mode_gpu()
print "Mean:", mean
def __init__(self, deploy, pretrained, mean, labels, gpu = False):
if gpu:
caffe.set_mode_gpu()
else:
caffe.set_mode_cpu() # in windows, only CPU mode supported
self.__labels = self.load_labels(labels);
mean_ar = self.convert(mean)
if True:
self.__net = caffe.Classifier(deploy, pretrained,
mean = mean_ar.mean(1).mean(1),
channel_swap = (2, 1, 0),
raw_scale = 255,
image_dims = (256, 256))
else:
self.__net = caffe.Net(deploy, pretrained, caffe.TEST)
print self.__net.blobs['data'].data.shape
self.__transformer = caffe.io.Transformer({'data': self.__net.blobs['data'].data.shape})
self.__transformer.set_transpose('data', (2,0,1)) # height*width*channel -> channel*height*width
self.__transformer.set_mean('data', mean_ar)
self.__transformer.set_raw_scale('data', 255)
self.__transformer.set_channel_swap('data', (2,1,0)) # RGB -> BGR
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 __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 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
项目源码
文件源码
阅读 21
收藏 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
test_caffe.py 文件源码
项目:tensorflow-action-conditional-video-prediction
作者: williamd4112
项目源码
文件源码
阅读 16
收藏 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})