本文整理汇总了Python中caffe2.python.workspace.FetchBlob方法的典型用法代码示例。如果您正苦于以下问题:Python workspace.FetchBlob方法的具体用法?Python workspace.FetchBlob怎么用?Python workspace.FetchBlob使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类caffe2.python.workspace
的用法示例。
在下文中一共展示了workspace.FetchBlob方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: run_add5_and_add5gradient_op
# 需要导入模块: from caffe2.python import workspace [as 别名]
# 或者: from caffe2.python.workspace import FetchBlob [as 别名]
def run_add5_and_add5gradient_op(device):
# clear the workspace before running the operator
workspace.ResetWorkspace()
add5 = core.CreateOperator("Add5",
["X"],
["Y"],
device_option=device)
print("==> Running Add5 op:")
workspace.FeedBlob("X", (np.random.rand(5, 5)), device_option=device)
print("Input of Add5: ", workspace.FetchBlob("X"))
workspace.RunOperatorOnce(add5)
print("Output of Add5: ", workspace.FetchBlob("Y"))
print("\n\n==> Running Add5Gradient op:")
print("Input of Add5Gradient: ", workspace.FetchBlob("Y"))
add5gradient = core.CreateOperator("Add5Gradient",
["Y"],
["Z"],
device_option=device)
workspace.RunOperatorOnce(add5gradient)
print("Output of Add5Gradient: ", workspace.FetchBlob("Z"))
示例2: accuracy
# 需要导入模块: from caffe2.python import workspace [as 别名]
# 或者: from caffe2.python.workspace import FetchBlob [as 别名]
def accuracy(model):
accuracy = []
prefix = model.net.Proto().name
for device in model._devices:
accuracy.append(
np.asscalar(workspace.FetchBlob("gpu_{}/{}_accuracy".format(device, prefix))))
return np.average(accuracy)
# ## Part 11: Run Multi-GPU Training and Get Test Results
# You've come a long way. Now is the time to see it all pay off. Since you already ran ResNet once, you can glance at the code below and run it. The big difference this time is your model is parallelized!
#
# The additional components at the end deal with accuracy so you may want to dig into those specifics as a bonus task. You can try it again: just adjust the `num_epochs` value below, run the block, and see the results. You can also go back to Part 10 to reinitialize the model, and run this step again. (You may want to add `workspace.ResetWorkspace()` before you run the new models again.)
#
# Go back and check the images/sec from when you ran single GPU. Note how you can scale up with a small amount of overhead.
#
# ### Task: How many GPUs would it take to train ImageNet in under a minute?
# In[ ]:
# Start looping through epochs where we run the batches of images to cover the entire dataset
# Usually you would want to run a lot more epochs to increase your model's accuracy
示例3: BroacastParameters
# 需要导入模块: from caffe2.python import workspace [as 别名]
# 或者: from caffe2.python.workspace import FetchBlob [as 别名]
def BroacastParameters(model, src_gpu, gpus):
log.info("Broadcasting parameters from gpu {} to gpu: {}".format(
src_gpu, ','.join([str(g) for g in gpus]))
)
for param in model.params:
if 'gpu_{}'.format(gpus[0]) in str(param):
for i in gpus:
blob = workspace.FetchBlob(str(param))
target_blob_name = str(param).replace(
'gpu_{}'.format(src_gpu),
'gpu_{}'.format(i)
)
log.info('broadcast {} -> {}'.format(
str(param), target_blob_name)
)
workspace.FetchBlob(str(param))
with core.DeviceScope(
core.DeviceOption(caffe2_pb2.CUDA, i)):
workspace.FeedBlob(target_blob_name, blob)
示例4: GetModelWeights
# 需要导入模块: from caffe2.python import workspace [as 别名]
# 或者: from caffe2.python.workspace import FetchBlob [as 别名]
def GetModelWeights(model, gpu_id=0):
'''
function that returns all the model weights in a dict
'''
model_ops = model.net.Proto().op
master_gpu = 'gpu_{}'.format(gpu_id)
param_ops = []
for idx in range(len(model_ops)):
op_type = model.net.Proto().op[idx].type
op_input = model.net.Proto().op[idx].input[0]
if op_type in ['Conv', 'FC'] and op_input.find(master_gpu) >= 0:
param_ops.append(model.net.Proto().op[idx])
weight_dict = {}
for idx in range(len(param_ops)):
# op_type = op.type
op_inputs = param_ops[idx].input
# op_output = param_ops[idx].output[0]
for op_input in op_inputs:
param_blob = op_input
weights = np.array(workspace.FetchBlob(str(param_blob)))
weight_dict[param_blob] = weights
return weight_dict
示例5: broadcast_parameters
# 需要导入模块: from caffe2.python import workspace [as 别名]
# 或者: from caffe2.python.workspace import FetchBlob [as 别名]
def broadcast_parameters(model):
"""Copy parameter blobs from GPU 0 over the corresponding parameter blobs
on GPUs 1 through cfg.NUM_GPUS - 1.
"""
if cfg.NUM_GPUS == 1:
# no-op if only running on a single GPU
return
def _do_broadcast(all_blobs):
assert len(all_blobs) % cfg.NUM_GPUS == 0, \
('Unexpected value for NUM_GPUS. Make sure you are not '
'running single-GPU inference with NUM_GPUS > 1.')
blobs_per_gpu = int(len(all_blobs) / cfg.NUM_GPUS)
for i in range(blobs_per_gpu):
blobs = [p for p in all_blobs[i::blobs_per_gpu]]
data = workspace.FetchBlob(blobs[0])
logger.debug('Broadcasting {} to'.format(str(blobs[0])))
for i, p in enumerate(blobs[1:]):
logger.debug(' |-> {}'.format(str(p)))
with c2_utils.CudaScope(i + 1):
workspace.FeedBlob(p, data)
_do_broadcast(model.params)
_do_broadcast([b + '_momentum' for b in model.TrainableParams()])
示例6: _run_test
# 需要导入模块: from caffe2.python import workspace [as 别名]
# 或者: from caffe2.python.workspace import FetchBlob [as 别名]
def _run_test(self, A, B, check_grad=False):
with core.DeviceScope(core.DeviceOption(caffe2_pb2.CUDA, 0)):
op = core.CreateOperator('SpatialNarrowAs', ['A', 'B'], ['C'])
workspace.FeedBlob('A', A)
workspace.FeedBlob('B', B)
workspace.RunOperatorOnce(op)
C = workspace.FetchBlob('C')
if check_grad:
gc = gradient_checker.GradientChecker(
stepsize=0.005,
threshold=0.005,
device_option=core.DeviceOption(caffe2_pb2.CUDA, 0)
)
res, grad, grad_estimated = gc.CheckSimple(op, [A, B], 0, [0])
self.assertTrue(res, 'Grad check failed')
dims = C.shape
C_ref = A[:dims[0], :dims[1], :dims[2], :dims[3]]
np.testing.assert_allclose(C, C_ref, rtol=1e-5, atol=1e-08)
示例7: _run_op_test
# 需要导入模块: from caffe2.python import workspace [as 别名]
# 或者: from caffe2.python.workspace import FetchBlob [as 别名]
def _run_op_test(self, X, I, check_grad=False):
with core.DeviceScope(core.DeviceOption(caffe2_pb2.CUDA, 0)):
op = core.CreateOperator('BatchPermutation', ['X', 'I'], ['Y'])
workspace.FeedBlob('X', X)
workspace.FeedBlob('I', I)
workspace.RunOperatorOnce(op)
Y = workspace.FetchBlob('Y')
if check_grad:
gc = gradient_checker.GradientChecker(
stepsize=0.1,
threshold=0.001,
device_option=core.DeviceOption(caffe2_pb2.CUDA, 0)
)
res, grad, grad_estimated = gc.CheckSimple(op, [X, I], 0, [0])
self.assertTrue(res, 'Grad check failed')
Y_ref = X[I]
np.testing.assert_allclose(Y, Y_ref, rtol=1e-5, atol=1e-08)
示例8: _run_speed_test
# 需要导入模块: from caffe2.python import workspace [as 别名]
# 或者: from caffe2.python.workspace import FetchBlob [as 别名]
def _run_speed_test(self, iters=5, N=1024):
"""This function provides an example of how to benchmark custom
operators using the Caffe2 'prof_dag' network execution type. Please
note that for 'prof_dag' to work, Caffe2 must be compiled with profiling
support using the `-DUSE_PROF=ON` option passed to `cmake` when building
Caffe2.
"""
net = core.Net('test')
net.Proto().type = 'prof_dag'
net.Proto().num_workers = 2
Y = net.BatchPermutation(['X', 'I'], 'Y')
Y_flat = net.FlattenToVec([Y], 'Y_flat')
loss = net.AveragedLoss([Y_flat], 'loss')
net.AddGradientOperators([loss])
workspace.CreateNet(net)
X = np.random.randn(N, 256, 14, 14)
for _i in range(iters):
I = np.random.permutation(N)
workspace.FeedBlob('X', X.astype(np.float32))
workspace.FeedBlob('I', I.astype(np.int32))
workspace.RunNet(net.Proto().name)
np.testing.assert_allclose(
workspace.FetchBlob('Y'), X[I], rtol=1e-5, atol=1e-08
)
示例9: get_params
# 需要导入模块: from caffe2.python import workspace [as 别名]
# 或者: from caffe2.python.workspace import FetchBlob [as 别名]
def get_params(model):
blobs = {} # gpu_0 blobs with unscoped_name as key
all_blobs = {} # all blobs with scoped name as key
# Save all parameters
for param in model.params:
scoped_name = str(param)
unscoped_name = c2_utils.UnscopeName(scoped_name)
if 'gpu_0' in scoped_name:
blobs[unscoped_name] = workspace.FetchBlob(scoped_name)
all_blobs[scoped_name] = workspace.FetchBlob(scoped_name)
for param in model.TrainableParams():
scoped_name = str(param) + '_momentum'
unscoped_name = c2_utils.UnscopeName(scoped_name)
if 'gpu_0' in scoped_name:
blobs[unscoped_name] = workspace.FetchBlob(scoped_name)
all_blobs[scoped_name] = workspace.FetchBlob(scoped_name)
return blobs, all_blobs
示例10: test_constant
# 需要导入模块: from caffe2.python import workspace [as 别名]
# 或者: from caffe2.python.workspace import FetchBlob [as 别名]
def test_constant():
workspace.ResetWorkspace()
shape = [10, 10]
val = random.random()
net = core.Net("net")
net.ConstantFill([], ["Y"], shape=shape, value=val, run_once=0, name="Y")
# Execute via Caffe2
workspace.RunNetOnce(net)
# Import caffe2 network into ngraph
importer = C2Importer()
importer.parse_net_def(net.Proto(), verbose=False)
# Get handle
f_ng = importer.get_op_handle("Y")
# Execute
with ExecutorFactory() as ex:
f_result = ex.executor(f_ng)()
# compare Caffe2 and ngraph results
assert(np.ma.allequal(f_result, workspace.FetchBlob("Y")))
assert(np.isclose(f_result[0][0], val, atol=1e-6, rtol=0))
示例11: test_SquaredL2Distance
# 需要导入模块: from caffe2.python import workspace [as 别名]
# 或者: from caffe2.python.workspace import FetchBlob [as 别名]
def test_SquaredL2Distance():
workspace.ResetWorkspace()
shape = (10, 10)
net = core.Net("net")
Y = net.GivenTensorFill([], "Y", shape=shape, values=np.random.uniform(-1, 1, shape))
T = net.GivenTensorFill([], "T", shape=shape, values=np.random.uniform(-1, 1, shape))
net.SquaredL2Distance([Y, T], "dist")
# Execute via Caffe2
workspace.RunNetOnce(net)
# Import caffe2 network into ngraph
importer = C2Importer()
importer.parse_net_def(net.Proto(), verbose=False)
# Get handle
f_ng = importer.get_op_handle("dist")
# Execute
with ExecutorFactory() as ex:
f_result = ex.executor(f_ng)()
assert(np.allclose(f_result, workspace.FetchBlob("dist"), equal_nan=False))
示例12: test_AveragedLoss
# 需要导入模块: from caffe2.python import workspace [as 别名]
# 或者: from caffe2.python.workspace import FetchBlob [as 别名]
def test_AveragedLoss():
workspace.ResetWorkspace()
shape = (32,)
net = core.Net("net")
X = net.GivenTensorFill([], "Y", shape=shape, values=np.random.uniform(-1, 1, shape))
X.AveragedLoss([], ["loss"])
# Execute via Caffe2
workspace.RunNetOnce(net)
# Import caffe2 network into ngraph
importer = C2Importer()
importer.parse_net_def(net.Proto(), verbose=False)
# Get handle
f_ng = importer.get_op_handle("loss")
# Execute
with ExecutorFactory() as ex:
f_result = ex.executor(f_ng)()
assert(np.allclose(f_result, workspace.FetchBlob("loss"), equal_nan=False))
示例13: FetchBlobWrapper
# 需要导入模块: from caffe2.python import workspace [as 别名]
# 或者: from caffe2.python.workspace import FetchBlob [as 别名]
def FetchBlobWrapper(self, tag, add_prefix=True, reduce_across=None, device_id=-1):
if self.ndevices > 1 and add_prefix:
# fetch from multiple devices
vals = []
for d in range(self.ndevices):
if tag.__class__ == list:
tag_on_device = tag[d]
else:
tag_on_device = "gpu_" + str(0) + "/" + tag
val = workspace.FetchBlob(tag_on_device)
vals.append(val)
# reduce across devices
if reduce_across == "add":
return functools.reduce(operator.add, vals)
elif reduce_across == "concat":
return np.concatenate(vals)
else:
return vals
else:
# fetch from a single device (named or not)
if device_id >= 0:
tag_on_device = "gpu_" + str(device_id) + "/" + tag
return workspace.FetchBlob(tag_on_device)
else:
return workspace.FetchBlob(tag)
示例14: display_images_and_confidence
# 需要导入模块: from caffe2.python import workspace [as 别名]
# 或者: from caffe2.python.workspace import FetchBlob [as 别名]
def display_images_and_confidence():
images = []
confidences = []
n = 16
data = workspace.FetchBlob("gpu_0/data")
label = workspace.FetchBlob("gpu_0/label")
softmax = workspace.FetchBlob("gpu_0/softmax")
for arr in zip(data[0:n], label[0:n], softmax[0:n]):
# CHW to HWC, normalize to [0.0, 1.0], and BGR to RGB
bgr = (arr[0].swapaxes(0, 1).swapaxes(1, 2) + 1.0) / 2.0
rgb = bgr[...,::-1]
images.append(rgb)
confidences.append(arr[2][arr[1]])
# Create grid for images
fig, rows = plt.subplots(nrows=4, ncols=4, figsize=(12, 12))
plt.tight_layout(h_pad=2)
# Display images and the models confidence in their label
items = zip([ax for cols in rows for ax in cols], images, confidences)
for (ax, image, confidence) in items:
ax.imshow(image)
if confidence >= 0.5:
ax.set_title("RIGHT ({:.1f}%)".format(confidence * 100.0), color='green')
else:
ax.set_title("WRONG ({:.1f}%)".format(confidence * 100.0), color='red')
plt.show()
示例15: ConvertModel
# 需要导入模块: from caffe2.python import workspace [as 别名]
# 或者: from caffe2.python.workspace import FetchBlob [as 别名]
def ConvertModel(args):
meta_net_def = pred_exp.load_from_db(args.load_model_path, args.db_type)
net = core.Net(
pred_utils.GetNet(meta_net_def, predictor_constants.PREDICT_NET_TYPE)
)
init_net = core.Net(
pred_utils.
GetNet(meta_net_def, predictor_constants.GLOBAL_INIT_NET_TYPE)
)
init_net.RunAllOnGPU()
assert workspace.RunNetOnce(init_net)
pred_params = list(set(net.Proto().external_input) - set(['gpu_0/data']))
save_params = [str(param) for param in pred_params]
save_blobs = {}
for param in save_params:
scoped_blob_name = str(param)
unscoped_blob_name = unscope_name(scoped_blob_name)
if unscoped_blob_name not in save_blobs:
save_blobs[unscoped_blob_name] = workspace.FetchBlob(
scoped_blob_name)
log.info(
'{:s} -> {:s}'.format(scoped_blob_name, unscoped_blob_name))
log.info('saving weights to {}'.format(args.save_model_path))
with open(args.save_model_path, 'w') as fwrite:
pickle.dump(dict(blobs=save_blobs), fwrite, pickle.HIGHEST_PROTOCOL)