Clone this net. Args: name: name of the cloned net blob_remap: optional map with list of blob names to replace op_id_mask: optional list of operator indices to include in the cloned net. If not provided, all ops a
(
self,
name,
blob_remap=None,
op_id_mask=None,
remap_funcs=None,
keep_schema=True,
update_external_list=False,
)
| 1698 | return BlobReference(blob_name, self) |
| 1699 | |
| 1700 | def Clone( |
| 1701 | self, |
| 1702 | name, |
| 1703 | blob_remap=None, |
| 1704 | op_id_mask=None, |
| 1705 | remap_funcs=None, |
| 1706 | keep_schema=True, |
| 1707 | update_external_list=False, |
| 1708 | ): |
| 1709 | """ |
| 1710 | Clone this net. |
| 1711 | Args: |
| 1712 | name: name of the cloned net |
| 1713 | blob_remap: optional map with list of blob names to replace |
| 1714 | op_id_mask: optional list of operator indices to include in |
| 1715 | the cloned net. If not provided, all ops are included. |
| 1716 | """ |
| 1717 | orig_remap_funcs = {} if remap_funcs is None else remap_funcs |
| 1718 | # by default we want to put RecurrentNetworkOp and |
| 1719 | # RecurrentNetworkGradientOp into remap_funcs, as these two operators |
| 1720 | # also take blobs and proto into the arguments. |
| 1721 | remap_funcs = DEFAULT_REMAP_FUNCS.copy() |
| 1722 | remap_funcs.update(orig_remap_funcs) |
| 1723 | proto = self._net |
| 1724 | new_proto = caffe2_pb2.NetDef() |
| 1725 | new_proto.CopyFrom(proto) |
| 1726 | new_proto.name = name |
| 1727 | |
| 1728 | if blob_remap is None: |
| 1729 | blob_remap = {} |
| 1730 | if op_id_mask is None: |
| 1731 | op_id_mask = list(range(0, len(proto.op))) |
| 1732 | |
| 1733 | def get_remapped_str(blob): |
| 1734 | blob_str = str(blob) |
| 1735 | return str(blob_remap.get(blob_str, blob_str)) |
| 1736 | |
| 1737 | def remap_list(proto_list): |
| 1738 | new_list = [get_remapped_str(b) for b in proto_list] |
| 1739 | del proto_list[:] |
| 1740 | proto_list.extend(new_list) |
| 1741 | |
| 1742 | def remap_op(op): |
| 1743 | new_op = caffe2_pb2.OperatorDef() |
| 1744 | new_op.CopyFrom(op) |
| 1745 | remap_list(new_op.input) |
| 1746 | remap_list(new_op.output) |
| 1747 | if new_op.type in remap_funcs: |
| 1748 | remap_funcs[new_op.type]( |
| 1749 | new_op, |
| 1750 | (name + '/') if name else '', |
| 1751 | blob_remap, |
| 1752 | ) |
| 1753 | return new_op |
| 1754 | |
| 1755 | del new_proto.op[:] |
| 1756 | new_proto.op.extend([remap_op(proto.op[op_id]) for op_id in op_id_mask]) |
| 1757 | remap_list(new_proto.external_input) |