本文整理汇总了Python中caffe2.python.workspace.ResetWorkspace方法的典型用法代码示例。如果您正苦于以下问题:Python workspace.ResetWorkspace方法的具体用法?Python workspace.ResetWorkspace怎么用?Python workspace.ResetWorkspace使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类caffe2.python.workspace
的用法示例。
在下文中一共展示了workspace.ResetWorkspace方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: run_add5_and_add5gradient_op
# 需要导入模块: from caffe2.python import workspace [as 别名]
# 或者: from caffe2.python.workspace import ResetWorkspace [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 ResetWorkspace [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: test_exp
# 需要导入模块: from caffe2.python import workspace [as 别名]
# 或者: from caffe2.python.workspace import ResetWorkspace [as 别名]
def test_exp():
workspace.ResetWorkspace()
shape = [2, 7]
data = [
1., 2., 3., 4., 1., 2., 3.,
1., 2., 3., 4., 1., 2., 3.
]
expected = [
[2.71828, 7.3890, 20.08553, 54.59815, 2.71828, 7.3890, 20.08553],
[2.71828, 7.3890, 20.08553, 54.59815, 2.71828, 7.3890, 20.08553],
]
run_all_close_compare_initiated_with_random_gauss('Exp',
shape=shape,
data=data,
expected=expected)
示例4: test_constant
# 需要导入模块: from caffe2.python import workspace [as 别名]
# 或者: from caffe2.python.workspace import ResetWorkspace [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))
示例5: test_SquaredL2Distance
# 需要导入模块: from caffe2.python import workspace [as 别名]
# 或者: from caffe2.python.workspace import ResetWorkspace [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))
示例6: test_AveragedLoss
# 需要导入模块: from caffe2.python import workspace [as 别名]
# 或者: from caffe2.python.workspace import ResetWorkspace [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))
示例7: test_inference
# 需要导入模块: from caffe2.python import workspace [as 别名]
# 或者: from caffe2.python.workspace import ResetWorkspace [as 别名]
def test_inference(self):
"""caffe2_benchmarks -> TestCaffe2Benchmarks::test_inference [Caffe2 CPU/GPU inference.]"""
print("Testing inference")
for params in itertools.product(self.models, self.batch_sizes, self.devices):
if params[0] in self.gpu_skip_models:
continue
model = model_helper.ModelHelper(name=params[0])
name, times = benchmark_inference(
model,
{'model':params[0], 'phase':'inference', 'batch_size':params[1],
'num_batches':self.num_batches, 'num_warmup_batches':self.num_warmup_iters,
'num_gpus':self.num_gpus, 'device':params[2], 'dtype':'float',
'enable_tensor_core':False}
)
self.assertEqual(len(times), self.num_batches)
print("model=%s, name=%s, batch=%d, device=%s, time=%f" %\
(params[0], name, params[1], params[2], 1000.0*np.mean(times)))
workspace.ResetWorkspace()
示例8: test_training_cpu
# 需要导入模块: from caffe2.python import workspace [as 别名]
# 或者: from caffe2.python.workspace import ResetWorkspace [as 别名]
def test_training_cpu(self):
"""caffe2_benchmarks -> TestCaffe2Benchmarks::test_training_cpu [Caffe2 CPU training.]"""
print("Testing CPU training")
for params in itertools.product(self.models, self.batch_sizes):
model = model_helper.ModelHelper(name=params[0])
name, times = benchmark_training(
model,
{'model':params[0], 'phase':'training', 'batch_size':params[1],
'num_batches':self.num_batches, 'num_warmup_batches':self.num_warmup_iters,
'num_gpus':0, 'device':'cpu', 'dtype':'float',
'enable_tensor_core':False}
)
self.assertEqual(len(times), self.num_batches)
print("model=%s, name=%s, batch=%d, device=cpu, time=%f" %\
(params[0], name, params[1], 1000.0*np.mean(times)))
workspace.ResetWorkspace()
示例9: test_training_gpu
# 需要导入模块: from caffe2.python import workspace [as 别名]
# 或者: from caffe2.python.workspace import ResetWorkspace [as 别名]
def test_training_gpu(self):
"""caffe2_benchmarks -> TestCaffe2Benchmarks::test_training_gpu [Caffe2 GPU training.]"""
print("Testing GPU training")
for params in itertools.product(self.models, self.batch_sizes, self.gpus):
if params[0] in self.gpu_skip_models:
continue
model = model_helper.ModelHelper(name=params[0])
name, times = benchmark_training(
model,
{'model':params[0], 'phase':'training', 'batch_size':params[1],
'num_batches':self.num_batches, 'num_warmup_batches':self.num_warmup_iters,
'num_gpus':len(params[2].split()), 'device':'gpu', 'dtype':'float',
'enable_tensor_core':False}
)
self.assertEqual(len(times), self.num_batches)
print("model=%s, name=%s, batch=%d, gpus=%s, time=%f" %\
(params[0], name, params[1], params[2], 1000.0*np.mean(times)))
workspace.ResetWorkspace()
示例10: run_model_cfg
# 需要导入模块: from caffe2.python import workspace [as 别名]
# 或者: from caffe2.python.workspace import ResetWorkspace [as 别名]
def run_model_cfg(args, im, check_blobs):
workspace.ResetWorkspace()
model, _ = load_model(args)
with c2_utils.NamedCudaScope(0):
cls_boxes, cls_segms, cls_keyps = test_engine.im_detect_all(
model, im, None, None,
)
boxes, segms, keypoints, classes = vis_utils.convert_from_cls_format(
cls_boxes, cls_segms, cls_keyps)
# sort the results based on score for comparision
boxes, segms, keypoints, classes = _sort_results(
boxes, segms, keypoints, classes)
# write final results back to workspace
def _ornone(res):
return np.array(res) if res is not None else np.array([], dtype=np.float32)
with c2_utils.NamedCudaScope(0):
workspace.FeedBlob(core.ScopedName('result_boxes'), _ornone(boxes))
workspace.FeedBlob(core.ScopedName('result_segms'), _ornone(segms))
workspace.FeedBlob(core.ScopedName('result_keypoints'), _ornone(keypoints))
workspace.FeedBlob(core.ScopedName('result_classids'), _ornone(classes))
# get result blobs
with c2_utils.NamedCudaScope(0):
ret = _get_result_blobs(check_blobs)
return ret
示例11: test_model
# 需要导入模块: from caffe2.python import workspace [as 别名]
# 或者: from caffe2.python.workspace import ResetWorkspace [as 别名]
def test_model(model_file, single_gpu_testing, opts=None):
"""Test a model."""
# Clear memory before inference
workspace.ResetWorkspace()
# Run inference
run_inference(
model_file, multi_gpu_testing=single_gpu_testing,
check_expected_results=True,
)
示例12: sum_example
# 需要导入模块: from caffe2.python import workspace [as 别名]
# 或者: from caffe2.python.workspace import ResetWorkspace [as 别名]
def sum_example():
# Caffe2 - network creation
net = core.Net("net")
shape = (2, 2, 2)
A = net.GivenTensorFill([], "A", shape=shape, values=np.random.uniform(-5, 5, shape), name="A")
B = net.GivenTensorFill([], "B", shape=shape, values=np.random.uniform(-5, 5, shape), name="B")
C = net.GivenTensorFill([], "C", shape=shape, values=np.random.uniform(-5, 5, shape), name="C")
A.Sum([B, C], ["Y"], name="Y")
# Execute via Caffe2
workspace.ResetWorkspace()
workspace.RunNetOnce(net)
# Execute in numpy
a = workspace.FetchBlob("A")
b = workspace.FetchBlob("B")
c = workspace.FetchBlob("C")
np_result = np.sum([a, b, c], axis=0)
# 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 in ngraph
f_result = ngt.make_transformer().computation(f_ng)()
# compare numpy, Caffe2 and ngraph results
print("Caffe2 result: \n{}\n".format(workspace.FetchBlob("Y")))
print("ngraph result: \n{}\n".format(f_result))
print("numpy result: \n{}\n".format(np_result))
assert(np.allclose(f_result, workspace.FetchBlob("Y")))
assert(np.allclose(f_result, np_result))
示例13: fc_example
# 需要导入模块: from caffe2.python import workspace [as 别名]
# 或者: from caffe2.python.workspace import ResetWorkspace [as 别名]
def fc_example():
# Caffe2 - network creation
net = core.Net("net")
X = net.GivenTensorFill([], "X", shape=[2, 2], values=[1.0, 2.0, 3.0, 4.0], name="X")
W = net.GivenTensorFill([], "W", shape=[1, 2], values=[5.0, 6.0], name="W")
b = net.ConstantFill([], ["b"], shape=[1, ], value=0.5, run_once=0, name="b")
X.FC([W, b], ["Y"], name="Y")
# Execute via Caffe2
workspace.ResetWorkspace()
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
f_result = ngt.make_transformer().computation(f_ng)()
# compare Caffe2 and ngraph results
print("Caffe2 result: {}:\n{}".format("Y", workspace.FetchBlob("Y")))
print("ngraph result: {}:\n{}".format("Y", f_result))
assert(np.array_equal(f_result, workspace.FetchBlob("Y")))
示例14: run_all_close_compare_initiated_with_random_gauss
# 需要导入模块: from caffe2.python import workspace [as 别名]
# 或者: from caffe2.python.workspace import ResetWorkspace [as 别名]
def run_all_close_compare_initiated_with_random_gauss(c2_op_name,
shape=None,
data=None,
expected=None):
workspace.ResetWorkspace()
if not shape:
shape = [2, 7]
if not data:
data = [random.gauss(mu=0, sigma=10) for i in range(np.prod(shape))]
net = core.Net("net")
net.GivenTensorFill([], "X", shape=shape, values=data, name="X")
getattr(net, c2_op_name)(["X"], ["Y"], 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)()
c2_y = workspace.FetchBlob("Y")
# compare Caffe2 and ngraph results
assert(np.allclose(f_result, c2_y, atol=1e-4, rtol=0, equal_nan=False))
# compare expected results and ngraph results
if expected:
assert(np.allclose(f_result, expected, atol=1e-3, rtol=0, equal_nan=False))
示例15: test_NCHW2NHWC
# 需要导入模块: from caffe2.python import workspace [as 别名]
# 或者: from caffe2.python.workspace import ResetWorkspace [as 别名]
def test_NCHW2NHWC():
workspace.ResetWorkspace()
# NCHW
shape = [2, 3, 4, 5]
data1 = [float(i) for i in range(np.prod(shape))]
net = core.Net("net")
X = net.GivenTensorFill([], ["X"], shape=shape, values=data1, name="X")
X.NCHW2NHWC([], ["Y"], 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.array_equal(f_result, workspace.FetchBlob("Y")))