本文整理汇总了Python中caffe2.python.core.CreateOperator方法的典型用法代码示例。如果您正苦于以下问题:Python core.CreateOperator方法的具体用法?Python core.CreateOperator怎么用?Python core.CreateOperator使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类caffe2.python.core
的用法示例。
在下文中一共展示了core.CreateOperator方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: run_add5_and_add5gradient_op
# 需要导入模块: from caffe2.python import core [as 别名]
# 或者: from caffe2.python.core import CreateOperator [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: test_add5
# 需要导入模块: from caffe2.python import core [as 别名]
# 或者: from caffe2.python.core import CreateOperator [as 别名]
def test_add5(self, N, C, gc, dc):
# set th seed
np.random.seed(101)
# TODO: test double, int and int64
data = np.random.rand(N, C).astype(np.float32)
op = core.CreateOperator("Add5", ["data"], ["output"])
# device check
self.assertDeviceChecks(dc, op, [data], [0])
# gradient check
self.assertGradientChecks(gc, op, [data], 0, [0])
# reference check
def ref_add5(input):
return [input + 5]
self.assertReferenceChecks(gc, op, [data], ref_add5)
示例3: enqueue_blobs
# 需要导入模块: from caffe2.python import core [as 别名]
# 或者: from caffe2.python.core import CreateOperator [as 别名]
def enqueue_blobs(self, gpu_id, blob_names, blobs):
"""Put a mini-batch on a BlobsQueue."""
assert len(blob_names) == len(blobs)
t = time.time()
dev = c2_utils.CudaDevice(gpu_id)
queue_name = 'gpu_{}/{}'.format(gpu_id, self._blobs_queue_name)
blob_names = ['gpu_{}/{}'.format(gpu_id, b) for b in blob_names]
for (blob_name, blob) in zip(blob_names, blobs):
workspace.FeedBlob(blob_name, blob, device_option=dev)
logger.debug(
'enqueue_blobs {}: workspace.FeedBlob: {}'.
format(gpu_id, time.time() - t)
)
t = time.time()
op = core.CreateOperator(
'SafeEnqueueBlobs', [queue_name] + blob_names,
blob_names + [queue_name + '_enqueue_status'],
device_option=dev
)
workspace.RunOperatorOnce(op)
logger.debug(
'enqueue_blobs {}: workspace.RunOperatorOnce: {}'.
format(gpu_id, time.time() - t)
)
示例4: _CorrectMomentum
# 需要导入模块: from caffe2.python import core [as 别名]
# 或者: from caffe2.python.core import CreateOperator [as 别名]
def _CorrectMomentum(self, correction):
"""The MomentumSGDUpdate op implements the update V as
V := mu * V + lr * grad,
where mu is the momentum factor, lr is the learning rate, and grad is
the stochastic gradient. Since V is not defined independently of the
learning rate (as it should ideally be), when the learning rate is
changed we should scale the update history V in order to make it
compatible in scale with lr * grad.
"""
logger.info(
'Scaling update history by {:.6f} (new lr / old lr)'.
format(correction))
for i in range(cfg.NUM_GPUS):
with c2_utils.CudaScope(i):
for param in self.TrainableParams(gpu_id=i):
op = core.CreateOperator(
'Scale', [param + '_momentum'], [param + '_momentum'],
scale=correction)
workspace.RunOperatorOnce(op)
示例5: _run_op_test
# 需要导入模块: from caffe2.python import core [as 别名]
# 或者: from caffe2.python.core import CreateOperator [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)
示例6: ConvertTensorProtosToInitNet
# 需要导入模块: from caffe2.python import core [as 别名]
# 或者: from caffe2.python.core import CreateOperator [as 别名]
def ConvertTensorProtosToInitNet(net_params, input_name):
"""Takes the net_params returned from TranslateModel, and wrap it as an
init net that contain GivenTensorFill.
This is a very simple feature that only works with float tensors, and is
only intended to be used in an environment where you want a single
initialization file - for more complex cases, use a db to store the
parameters.
"""
init_net = caffe2_pb2.NetDef()
for tensor in net_params.protos:
if len(tensor.float_data) == 0:
raise RuntimeError(
"Only float tensors are supported in this util.")
op = core.CreateOperator(
"GivenTensorFill", [], [tensor.name],
arg=[
utils.MakeArgument("shape", list(tensor.dims)),
utils.MakeArgument("values", tensor.float_data)])
init_net.op.extend([op])
init_net.op.extend([core.CreateOperator("ConstantFill", [], [input_name], shape=[1])])
return init_net
示例7: enqueue_blobs
# 需要导入模块: from caffe2.python import core [as 别名]
# 或者: from caffe2.python.core import CreateOperator [as 别名]
def enqueue_blobs(
self,
gpu_id,
enqueue_blobs_names,
blob_values,
):
enqueue_blobs_names = [
'gpu_{}/{}'.format(
gpu_id, enqueue_blob_name
) for enqueue_blob_name in enqueue_blobs_names
]
deviceOption = core.DeviceOption(caffe2_pb2.CUDA, gpu_id)
for (blob_name, blob) in zip(enqueue_blobs_names, blob_values):
workspace.FeedBlob(blob_name, blob, device_option=deviceOption)
queue_name = 'gpu_{}/{}'.format(gpu_id, self._blobs_queue_name)
workspace.RunOperatorOnce(
core.CreateOperator(
'SafeEnqueueBlobs',
[queue_name] + enqueue_blobs_names,
enqueue_blobs_names + [queue_name + '_enqueue_status'],
device_option=deviceOption,
)
)
示例8: _run_test
# 需要导入模块: from caffe2.python import core [as 别名]
# 或者: from caffe2.python.core import CreateOperator [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)
示例9: _create_const_fill_op_from_numpy
# 需要导入模块: from caffe2.python import core [as 别名]
# 或者: from caffe2.python.core import CreateOperator [as 别名]
def _create_const_fill_op_from_numpy(name, tensor, device_option=None):
assert type(tensor) == np.ndarray
kTypeNameMapper = {
np.dtype("float32"): "GivenTensorFill",
np.dtype("int32"): "GivenTensorIntFill",
np.dtype("int64"): "GivenTensorInt64Fill",
np.dtype("uint8"): "GivenTensorStringFill",
}
args_dict = {}
if tensor.dtype == np.dtype("uint8"):
args_dict.update({"values": [str(tensor.data)], "shape": [1]})
else:
args_dict.update({"values": tensor, "shape": tensor.shape})
if device_option is not None:
args_dict["device_option"] = device_option
return core.CreateOperator(kTypeNameMapper[tensor.dtype], [], [name], **args_dict)
示例10: _create_const_fill_op_from_c2_int8_tensor
# 需要导入模块: from caffe2.python import core [as 别名]
# 或者: from caffe2.python.core import CreateOperator [as 别名]
def _create_const_fill_op_from_c2_int8_tensor(name, int8_tensor):
assert type(int8_tensor) == workspace.Int8Tensor
kTypeNameMapper = {
np.dtype("int32"): "Int8GivenIntTensorFill",
np.dtype("uint8"): "Int8GivenTensorFill",
}
tensor = int8_tensor.data
assert tensor.dtype in [np.dtype("uint8"), np.dtype("int32")]
values = tensor.tobytes() if tensor.dtype == np.dtype("uint8") else tensor
return core.CreateOperator(
kTypeNameMapper[tensor.dtype],
[],
[name],
values=values,
shape=tensor.shape,
Y_scale=int8_tensor.scale,
Y_zero_point=int8_tensor.zero_point,
)
示例11: create_blobs_queues
# 需要导入模块: from caffe2.python import core [as 别名]
# 或者: from caffe2.python.core import CreateOperator [as 别名]
def create_blobs_queues(self):
"""Create one BlobsQueue for each GPU to hold mini-batches."""
for gpu_id in range(self._num_gpus):
with c2_utils.GpuNameScope(gpu_id):
workspace.RunOperatorOnce(
core.CreateOperator(
'CreateBlobsQueue', [], [self._blobs_queue_name],
num_blobs=len(self.get_output_names()),
capacity=self._blobs_queue_capacity
)
)
return self.create_enqueue_blobs()
示例12: close_blobs_queues
# 需要导入模块: from caffe2.python import core [as 别名]
# 或者: from caffe2.python.core import CreateOperator [as 别名]
def close_blobs_queues(self):
"""Close a BlobsQueue."""
for gpu_id in range(self._num_gpus):
with core.NameScope('gpu_{}'.format(gpu_id)):
workspace.RunOperatorOnce(
core.CreateOperator(
'CloseBlobsQueue', [self._blobs_queue_name], []
)
)
示例13: add_tensor
# 需要导入模块: from caffe2.python import core [as 别名]
# 或者: from caffe2.python.core import CreateOperator [as 别名]
def add_tensor(net, name, blob):
''' Create an operator to store the tensor 'blob',
run the operator to put the blob to workspace.
uint8 is stored as an array of string with one element.
'''
kTypeNameMapper = {
np.dtype('float32'): "GivenTensorFill",
np.dtype('int32'): "GivenTensorIntFill",
np.dtype('int64'): "GivenTensorInt64Fill",
np.dtype('uint8'): "GivenTensorStringFill",
}
shape = blob.shape
values = blob
# pass array of uint8 as a string to save storage
# storing uint8_t has a large overhead for now
if blob.dtype == np.dtype('uint8'):
shape = [1]
values = [str(blob.data)]
op = core.CreateOperator(
kTypeNameMapper[blob.dtype],
[], [name],
shape=shape,
values=values,
# arg=[
# putils.MakeArgument("shape", shape),
# putils.MakeArgument("values", values),
# ]
)
net.op.extend([op])
示例14: _run_zero_even_op
# 需要导入模块: from caffe2.python import core [as 别名]
# 或者: from caffe2.python.core import CreateOperator [as 别名]
def _run_zero_even_op(self, X):
op = core.CreateOperator('ZeroEven', ['X'], ['Y'])
workspace.FeedBlob('X', X)
workspace.RunOperatorOnce(op)
Y = workspace.FetchBlob('Y')
return Y
示例15: _run_zero_even_op_gpu
# 需要导入模块: from caffe2.python import core [as 别名]
# 或者: from caffe2.python.core import CreateOperator [as 别名]
def _run_zero_even_op_gpu(self, X):
with core.DeviceScope(core.DeviceOption(caffe2_pb2.CUDA, 0)):
op = core.CreateOperator('ZeroEven', ['X'], ['Y'])
workspace.FeedBlob('X', X)
workspace.RunOperatorOnce(op)
Y = workspace.FetchBlob('Y')
return Y