def __init__(self, videoThread):
threading.Thread.__init__(self)
print "Initializing recognition thread..."
self.videoThread = videoThread
#caffe.set_mode_cpu()
caffe.set_mode_gpu()
caffe.set_device(0)
# Model file and parameters are written by trainDnn.py
# Take the most recent parameter set
genderPath = "./dcnn_gender"
genderParamFiles = glob.glob(genderPath + os.sep + "*.caffemodel")
genderParamFiles = sorted(genderParamFiles, key=lambda x:os.path.getctime(x))
MODEL_FILE_GENDER = genderPath + os.sep + "deploy_gender.prototxt"
PRETRAINED_GENDER = genderParamFiles[-1]
MEAN_FILE_GENDER = genderPath + os.sep + "mean.binaryproto"
proto_data = open(MEAN_FILE_GENDER, 'rb').read()
a = caffe.io.caffe_pb2.BlobProto.FromString(proto_data)
mean = caffe.io.blobproto_to_array(a)[0]
# Initialize net
self.gender_net = caffe.Classifier(MODEL_FILE_GENDER, PRETRAINED_GENDER, image_dims=(227,227),)
python类set_mode_cpu()的实例源码
def __init__(self, videoThread):
threading.Thread.__init__(self)
print "Initializing age recognition thread..."
self.videoThread = videoThread
#caffe.set_mode_cpu()
caffe.set_mode_gpu()
# Model file and parameters are written by trainDnn.py
# Take the most recent parameter set
dcnnPath = "./dcnn_age"
paramFiles = glob.glob(dcnnPath + os.sep + "*.caffemodel")
paramFiles = sorted(paramFiles, key=lambda x:os.path.getctime(x))
MODEL_FILE = dcnnPath + os.sep + "deploy.prototxt"
PRETRAINED = paramFiles[-1]
MEAN_FILE = dcnnPath + os.sep + "mean.binaryproto"
blob = caffe.proto.caffe_pb2.BlobProto()
with open(MEAN_FILE, 'rb') as f:
data = f.read()
blob.ParseFromString(data)
# mean = np.array( caffe.io.blobproto_to_array(blob) ) [0]
# Added simple mean
mean = np.array([93.5940, 104.7624, 129.1863])
# Initialize net
self.net = caffe.Classifier(MODEL_FILE, PRETRAINED, image_dims=(224,224), mean=mean)
def __init__(self, model_def_file, pretrained_model_file,
class_labels_file, gpu_mode):
logging.info('Loading net and associated files...')
if gpu_mode:
caffe.set_mode_gpu()
else:
caffe.set_mode_cpu()
self.net = caffe.Classifier(
model_def_file, pretrained_model_file,
image_dims=(400, 400), raw_scale=400,
mean=np.load('{}/mean.npy'.format(REPO_DIRNAME)).mean(1).mean(1), channel_swap=(2, 1, 0)
)
with open(class_labels_file) as f:
labels_df = pd.DataFrame([
{
'synset_id': l.strip().split(' ')[0],
'name': ' '.join(l.strip().split(' ')[1:]).split(',')[0]
}
for l in f.readlines()
])
self.labels = labels_df.sort('synset_id')['name'].values
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, layer="fc7", oversample = True):
self.caffe_root = "caffe"
self.model_prototxt = os.path.join(self.caffe_root, 'deploy.prototxt')
self.model_trained = os.path.join(self.caffe_root, "bvlc_reference_caffenet.caffemodel")
self.mean_image = os.path.join(self.caffe_root, 'python/caffe/imagenet/ilsvrc_2012_mean.npy')
self.layer = layer
caffe.set_mode_cpu()
self.net = caffe.Classifier(self.model_prototxt, self.model_trained,
mean=np.load(self.mean_image).mean(1).mean(1),
channel_swap=(2,1,0),
raw_scale=255,
image_dims=(256, 256))
self.size = (256,256)
self.patch_size = 224
self.prefix = "DeCAF7"
self.oversample = oversample
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 retrain_pruned(solver, pruned_caffemodel, threshold, prune_layers):
#solver = caffe.SGDSolver(solver_proto)
retrain_iter = 20
accuracys = []
for i in range(retrain_iter):
solver.net.copy_from(pruned_caffemodel)
# solver.solve()
solver.step(500)
_,_,_,_,accuracy=prune(threshold, solver.test_nets[0], prune_layers)
solver.test_nets[0].save(pruned_caffemodel)
accuracys.append(accuracy)
plt.plot(accuracys, 'r.-')
plt.show()
#CPU?GPU????
#caffe.set_mode_cpu()
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 __init__(self, hyperparams, dO, dU):
config = copy.deepcopy(POLICY_OPT_CAFFE)
config.update(hyperparams)
PolicyOpt.__init__(self, config, dO, dU)
self.batch_size = self._hyperparams['batch_size']
if self._hyperparams['use_gpu']:
caffe.set_device(self._hyperparams['gpu_id'])
caffe.set_mode_gpu()
else:
caffe.set_mode_cpu()
self.init_solver()
self.caffe_iter = 0
self.var = self._hyperparams['init_var'] * np.ones(dU)
self.policy = CaffePolicy(self.solver.test_nets[0],
self.solver.test_nets[1],
self.var)
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
nolog_caffe_predict_top2_scroces_bot_gpu.py 文件源码
项目:huaat_ml_dl
作者: ieee820
项目源码
文件源码
阅读 24
收藏 0
点赞 0
评论 0
def main(argv):
#input e.g : python caffe_main_test_top2_scores.py ./test_dir/ ./0908.log
#get argvs
caffe_root = '/opt/caffe/'
model_path = '/opt/model_0908_12c/'
parser = argparse.ArgumentParser()
# Required arguments: input and output files.
parser.add_argument(
"image_path",
help="Input image, directory, or npy."
)
parser.add_argument(
"log_path",
help="Output npy filename."
)
args = parser.parse_args()
#set vars from argvs
image_path = args.image_path
log_path = args.log_path
#program begin...
#caffe.set_mode_cpu()
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 getNet():
global TRANSFORMER, NET
if TRANSFORMER is None or NET is None:
import caffe
os.environ['GLOG_minloglevel'] = '2'
caffe.set_mode_cpu()
## Opening mean average image
proto_data = open(mean_path, "rb").read()
a = caffe.io.caffe_pb2.BlobProto.FromString(proto_data)
mean = caffe.io.blobproto_to_array(a)[0]
## Loading the CNN
NET = caffe.Classifier(deploy_path, model_path)
## Setting up the right transformer for an input image
TRANSFORMER = caffe.io.Transformer({'data': NET.blobs['data'].data.shape})
TRANSFORMER.set_transpose('data', (2, 0, 1))
TRANSFORMER.set_channel_swap('data', (2, 1, 0))
TRANSFORMER.set_raw_scale('data', 255.0)
TRANSFORMER.set_mean('data', mean)
print('> CNN Model loaded to regress 3D Shape and Texture!')
return TRANSFORMER, NET
def __init__(self, args):
super().__init__(args, with_video_output=False)
if self.vgg_model_path is None:
self.vgg_model_path = "/media/" + getpass.getuser() + "/Data/AMBR_data/ml"
self.vgg_model_filename = os.path.join(self.vgg_model_path, self.vgg_model_filename)
self.vgg_pretrained_filename = os.path.join(self.vgg_model_path, self.vgg_pretrained_filename)
if self.output_datafile is None:
self.output_datafile = "{:s}_features.npz".format(self.in_video[:-4])
self.prev_frame_centroid = None
if self.caffe_cpu:
caffe.set_mode_cpu()
else:
caffe.set_mode_gpu()
self.extractor = None
self.blank_features = None
if not self.no_vgg:
self.extractor = VGGFeatureExtractor(model_file=self.vgg_model_filename,
pretrained_file=self.vgg_pretrained_filename)
self.blank_features = self.extractor.extract_single(np.zeros((256, 256, 3), dtype=np.uint8), blobs=['fc7'])[
'fc7']
self.features = []
self.present_flags = []
def get_predictions(region_crops):
if os.environ["IS_GPU"]:
caffe.set_device(0)
caffe.set_mode_gpu()
else:
caffe.set_mode_cpu()
classifier = caffe.Classifier(os.path.join(os.environ["TEXT_NOTEXT_MODELS_DIR"], "deploy.prototxt"),
os.path.join(os.environ["TEXT_NOTEXT_MODELS_DIR"], "weights.caffemodel"),
mean=np.array([104, 117, 123], dtype='f4'),
image_dims=[224, 224],
raw_scale=255.0,
channel_swap=[2, 1, 0])
LOGGER.info("Classifying " + str(len(region_crops)) + " inputs.")
predictions = classifier.predict(region_crops)
return predictions
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 _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 get_caffe_model(caffe_dir, caffe_model, gpu=True,
image_dims=(256, 256),
mean_file='default',
raw_scale=255.0,
channel_swap=(2,1,0),
input_scale=None):
if mean_file == 'default':
mean_file = os.path.join(caffe_dir, 'python', 'caffe', 'imagenet', 'ilsvrc_2012_mean.npy')
model_path = os.path.join(caffe_dir, 'models', caffe_model, '%s.caffemodel'%caffe_model)
model_def = os.path.join(caffe_dir, 'models', caffe_model, 'deploy.prototxt')
print('Loading mean file %s' % mean_file)
mean = np.load(mean_file).mean(1).mean(1)
if gpu:
caffe.set_mode_gpu()
else:
caffe.set_mode_cpu()
net = caffe.Classifier(model_def, model_path,
image_dims=image_dims, mean=mean,
input_scale=input_scale, raw_scale=raw_scale,
channel_swap=channel_swap)
return net
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 main():
parser = argparse.ArgumentParser()
parser.add_argument('dataset', nargs='?',
choices=['pascal_voc', 'camvid', 'kitti', 'cityscapes'])
parser.add_argument('input_path', nargs='?', default='',
help='Required path to input image')
parser.add_argument('-o', '--output_path', default=None)
parser.add_argument('--gpu', type=int, default=-1,
help='GPU ID to run CAFFE. '
'If -1 (default), CPU is used')
args = parser.parse_args()
if args.input_path == '':
raise IOError('Error: No path to input image')
if not exists(args.input_path):
raise IOError("Error: Can't find input image " + args.input_path)
if args.gpu >= 0:
caffe.set_mode_gpu()
caffe.set_device(args.gpu)
print('Using GPU ', args.gpu)
else:
caffe.set_mode_cpu()
print('Using CPU')
if args.output_path is None:
args.output_path = '{}_{}.png'.format(
splitext(args.input_path)[0], args.dataset)
predict(args.dataset, args.input_path, args.output_path)
unet_segmentation_no_db_example.py 文件源码
项目:peters-stuff
作者: peterneher
项目源码
文件源码
阅读 43
收藏 0
点赞 0
评论 0
def train_network(solver_file, num_classes, batch_size, num_iterations, use_gpu=True) :
if use_gpu :
caffe.set_mode_gpu()
else :
caffe.set_mode_cpu()
solver = caffe.get_solver(solver_file)
solver.net.blobs['data'].reshape(batch_size, solver.net.blobs['data'].shape[1], solver.net.blobs['data'].shape[2], solver.net.blobs['data'].shape[3])
solver.net.blobs['target'].reshape(batch_size, solver.net.blobs['target'].shape[1], solver.net.blobs['target'].shape[2], solver.net.blobs['target'].shape[3])
solver.net.reshape()
for i in range(num_iterations):
data, target = get_data(batch_size, numclasses=num_classes)
solver.net.blobs['data'].data[...] = data
solver.net.blobs['target'].data[...] = target
solver.step(1)
output = solver.net.blobs['argmax'].data[...]
fig, sub = plt.subplots(ncols=3, figsize=(15, 5))
sub[0].set_title('Input')
sub[0].imshow(data[0, 0, :, :])
sub[1].set_title('Ground Truth')
sub[1].imshow(np.argmax(target[0, :, :, :], axis=0))
sub[2].set_title('Segmentation')
sub[2].imshow(output[0, 0, :, :])
plt.show()
def __init__(self, protoTXTPath, weightsPath):
import caffe
caffe.set_mode_cpu()
self.net = caffe.Net(protoTXTPath, weightsPath, caffe.TEST)
self.mean = cv2.imread(os.path.join(Predictor.ROOT, 'trainMean.png')).astype('float')
self.mean = cv2.resize(self.mean, (60,60), interpolation=cv2.INTER_CUBIC)
self.std = cv2.imread(os.path.join(Predictor.ROOT,'trainSTD.png')).astype('float')
self.std = cv2.resize(self.std, (60,60), interpolation=cv2.INTER_CUBIC)
def netsurgery(protofile, paramfile, params, protofile_full_conv, params_full_conv, opath):
'''
See https://github.com/BVLC/caffe/blob/master/examples/net_surgery.ipynb for
details.
protofile: prototxt of fully-connected model (read-only pathname)
paramfile: weights of fully-connected model (read-only pathname)
params: list of fully-connected blob names (list of str)
protofile_full_conv: prototxt of fully-convolutional model (read-only pathname)
params_full_conv: list of fully-convolutional blob names (list of str)
opath: weights of fully-connected model (write pathname)
'''
caffe.set_mode_cpu()
net = caffe.Net(protofile, paramfile, caffe.TEST)
fc_params = {pr: (net.params[pr][0].data, net.params[pr][1].data) for pr in params}
for fc in params:
print('{} weights are {} dimensional and biases are {} dimensional'.format(fc, fc_params[fc][0].shape, fc_params[fc][1].shape))
net_full_conv = caffe.Net(protofile_full_conv, paramfile, caffe.TEST)
conv_params = {pr: (net_full_conv.params[pr][0].data, net_full_conv.params[pr][1].data) for pr in params_full_conv}
for conv in params_full_conv:
print('{} weights are {} dimensional and biases are {} dimensional'.format(conv, conv_params[conv][0].shape, conv_params[conv][1].shape))
for pr, pr_conv in zip(params, params_full_conv):
conv_params[pr_conv][0].flat = fc_params[pr][0].flat # flat unrolls the arrays
conv_params[pr_conv][1][...] = fc_params[pr][1]
net_full_conv.save(opath)
def __init__(self, path_to_deploy_file, path_to_model_file, input_layer_name="data_q", gpu_mode=True, device_id=1,
height=None, width=None):
self.path_to_deploy_file = path_to_deploy_file
self.path_to_model_file = path_to_model_file
if gpu_mode:
caffe.set_mode_gpu()
caffe.set_device(device_id)
else:
caffe.set_mode_cpu()
self.net = caffe.Net(path_to_deploy_file, path_to_model_file, caffe.TEST)
self.input_layer_name = input_layer_name
self.height = height or self.net.blobs[self.input_layer_name].data.shape[2]
self.width = width or self.net.blobs[self.input_layer_name].data.shape[3]
eval.py 文件源码
项目:fully-convolutional-network-semantic-segmentation
作者: alecng94
项目源码
文件源码
阅读 31
收藏 0
点赞 0
评论 0
def eval(testSetPath, clipSize):
checkTestAndTruths(testSetPath)
checkDirs(testSetPath)
imgTestDir = os.path.join(testSetPath, 'test/')
caffe.set_mode_cpu()
printTitle("Loading caffe model")
net = caffe.Net(
'src/deploy.prototxt'
, 'src/fcn8s-heavy-pascal.caffemodel'
, caffe.TEST)
print("Model loaded.")
testImgPaths = []
for testImg in os.listdir(imgTestDir):
testImgPaths.append(imgTestDir + testImg)
testImgPaths.sort()
printTitle("Generating predictions")
totalPredictTime = 0.00
for count in range(0, len(testImgPaths)):
totalPredictTime += predict(testImgPaths[count], testSetPath, clipSize, net)
printTitle("Average prediction time: %.3f" % (totalPredictTime / len(testImgPaths)))
printTitle("Generating scores")
segPath = os.path.join(testSetPath, 'seg/')
truthPath = os.path.join(testSetPath, 'truth/')
computeAccuracies(testSetPath, segPath, truthPath)
print("eval.py has completed.")
# Make folders for seg, labelled and mask predictions
def load_caffe(model_desc, model_file):
"""
return a dict of params
"""
import caffe
caffe.set_mode_cpu()
net = caffe.Net(model_desc, model_file, caffe.TEST)
param_dict = CaffeLayerProcessor(net).process()
return param_dict
def start(self, rank):
self.rank = rank
if len(self.gpus) > 0:
self.device = self.gpus[rank]
if debug:
s = 'solver gpu %d' % self.gpus[self.rank] + \
' pid %d' % os.getpid() + ' size %d' % self.size + \
' rank %d' % self.rank
print(s, file = sys.stderr)
caffe.set_mode_gpu()
caffe.set_device(self.device)
caffe.set_solver_count(self.size)
caffe.set_solver_rank(self.rank)
caffe.set_multiprocess(True)
else:
print('solver cpu', file = sys.stderr)
caffe.set_mode_cpu()
if self.cmd.graph.endswith('.json'):
with open(self.cmd.graph, mode = 'r') as f:
graph = caffe_pb2.SolverParameter()
text_format.Merge(f.read(), graph)
self.graph = graph
else:
self.graph = self.solver_graph()
import tempfile
with tempfile.NamedTemporaryFile(mode = 'w+', delete = False) as f:
text_format.PrintMessage(self.graph, f)
tmp = f.name
self.caffe = caffe.AdamSolver(tmp)
if self.uid:
self.nccl = caffe.NCCL(self.caffe, self.uid)
self.nccl.bcast()
self.caffe.add_callback(self.nccl)
if self.caffe.param.layer_wise_reduce:
self.caffe.net.after_backward(self.nccl)
def initFaceDetector():
minsize = 20
caffe_model_path = "/home/duino/iactive/mtcnn/model"
threshold = [0.6, 0.7, 0.7]
factor = 0.709
caffe.set_mode_cpu()
PNet = caffe.Net(caffe_model_path+"/det1.prototxt", caffe_model_path+"/det1.caffemodel", caffe.TEST)
RNet = caffe.Net(caffe_model_path+"/det2.prototxt", caffe_model_path+"/det2.caffemodel", caffe.TEST)
ONet = caffe.Net(caffe_model_path+"/det3.prototxt", caffe_model_path+"/det3.caffemodel", caffe.TEST)
return (minsize, PNet, RNet, ONet, threshold, factor)
def run(self):
"""This method runs in the new process."""
global logger
setup_exceptions()
logger = log_utils.setup_logger('tile_worker')
if self.caffe_path is not None:
sys.path.append(self.caffe_path + '/python')
if self.device >= 0:
os.environ['CUDA_VISIBLE_DEVICES'] = str(self.device)
import caffe
if self.device >= 0:
caffe.set_mode_gpu()
else:
caffe.set_mode_cpu()
caffe.set_random_seed(0)
np.random.seed(0)
self.model = CaffeModel(*self.model_info)
self.model.img = np.zeros((3, 1, 1), dtype=np.float32)
while True:
try:
self.process_one_request()
except KeyboardInterrupt:
break
def init_model(resp_q, caffe_path, model, weights, mean):
"""Puts the list of layer shapes into resp_q. To be run in a separate process."""
global logger
setup_exceptions()
logger = log_utils.setup_logger('init_model')
if caffe_path:
sys.path.append(caffe_path + '/python')
import caffe
caffe.set_mode_cpu()
model = CaffeModel(model, weights, mean)
shapes = OrderedDict()
for layer in model.layers():
shapes[layer] = model.data[layer].shape
resp_q.put(shapes)