partition
stringclasses
3 values
func_name
stringlengths
1
134
docstring
stringlengths
1
46.9k
path
stringlengths
4
223
original_string
stringlengths
75
104k
code
stringlengths
75
104k
docstring_tokens
listlengths
1
1.97k
repo
stringlengths
7
55
language
stringclasses
1 value
url
stringlengths
87
315
code_tokens
listlengths
19
28.4k
sha
stringlengths
40
40
train
BaseSparseNDArray._aux_types
The data types of the aux data for the BaseSparseNDArray.
python/mxnet/ndarray/sparse.py
def _aux_types(self): """The data types of the aux data for the BaseSparseNDArray. """ aux_types = [] num_aux = self._num_aux for i in range(num_aux): aux_types.append(self._aux_type(i)) return aux_types
def _aux_types(self): """The data types of the aux data for the BaseSparseNDArray. """ aux_types = [] num_aux = self._num_aux for i in range(num_aux): aux_types.append(self._aux_type(i)) return aux_types
[ "The", "data", "types", "of", "the", "aux", "data", "for", "the", "BaseSparseNDArray", "." ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/ndarray/sparse.py#L183-L190
[ "def", "_aux_types", "(", "self", ")", ":", "aux_types", "=", "[", "]", "num_aux", "=", "self", ".", "_num_aux", "for", "i", "in", "range", "(", "num_aux", ")", ":", "aux_types", ".", "append", "(", "self", ".", "_aux_type", "(", "i", ")", ")", "re...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
BaseSparseNDArray.astype
Return a copy of the array after casting to a specified type. Parameters ---------- dtype : numpy.dtype or str The type of the returned array. copy : bool Default `True`. By default, astype always returns a newly allocated ndarray on the same context. If this is set to `False`, and the dtype requested is the same as the ndarray's dtype, the ndarray is returned instead of a copy. Examples -------- >>> x = mx.nd.sparse.zeros('row_sparse', (2,3), dtype='float32') >>> y = x.astype('int32') >>> y.dtype <type 'numpy.int32'>
python/mxnet/ndarray/sparse.py
def astype(self, dtype, copy=True): """Return a copy of the array after casting to a specified type. Parameters ---------- dtype : numpy.dtype or str The type of the returned array. copy : bool Default `True`. By default, astype always returns a newly allocated ndarray on the same context. If this is set to `False`, and the dtype requested is the same as the ndarray's dtype, the ndarray is returned instead of a copy. Examples -------- >>> x = mx.nd.sparse.zeros('row_sparse', (2,3), dtype='float32') >>> y = x.astype('int32') >>> y.dtype <type 'numpy.int32'> """ if not copy and np.dtype(dtype) == self.dtype: return self res = zeros(shape=self.shape, ctx=self.context, dtype=dtype, stype=self.stype) self.copyto(res) return res
def astype(self, dtype, copy=True): """Return a copy of the array after casting to a specified type. Parameters ---------- dtype : numpy.dtype or str The type of the returned array. copy : bool Default `True`. By default, astype always returns a newly allocated ndarray on the same context. If this is set to `False`, and the dtype requested is the same as the ndarray's dtype, the ndarray is returned instead of a copy. Examples -------- >>> x = mx.nd.sparse.zeros('row_sparse', (2,3), dtype='float32') >>> y = x.astype('int32') >>> y.dtype <type 'numpy.int32'> """ if not copy and np.dtype(dtype) == self.dtype: return self res = zeros(shape=self.shape, ctx=self.context, dtype=dtype, stype=self.stype) self.copyto(res) return res
[ "Return", "a", "copy", "of", "the", "array", "after", "casting", "to", "a", "specified", "type", "." ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/ndarray/sparse.py#L197-L223
[ "def", "astype", "(", "self", ",", "dtype", ",", "copy", "=", "True", ")", ":", "if", "not", "copy", "and", "np", ".", "dtype", "(", "dtype", ")", "==", "self", ".", "dtype", ":", "return", "self", "res", "=", "zeros", "(", "shape", "=", "self", ...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
BaseSparseNDArray.check_format
Check whether the NDArray format is valid. Parameters ---------- full_check : bool, optional If `True`, rigorous check, O(N) operations. Otherwise basic check, O(1) operations (default True).
python/mxnet/ndarray/sparse.py
def check_format(self, full_check=True): """Check whether the NDArray format is valid. Parameters ---------- full_check : bool, optional If `True`, rigorous check, O(N) operations. Otherwise basic check, O(1) operations (default True). """ check_call(_LIB.MXNDArraySyncCheckFormat(self.handle, ctypes.c_bool(full_check)))
def check_format(self, full_check=True): """Check whether the NDArray format is valid. Parameters ---------- full_check : bool, optional If `True`, rigorous check, O(N) operations. Otherwise basic check, O(1) operations (default True). """ check_call(_LIB.MXNDArraySyncCheckFormat(self.handle, ctypes.c_bool(full_check)))
[ "Check", "whether", "the", "NDArray", "format", "is", "valid", "." ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/ndarray/sparse.py#L252-L261
[ "def", "check_format", "(", "self", ",", "full_check", "=", "True", ")", ":", "check_call", "(", "_LIB", ".", "MXNDArraySyncCheckFormat", "(", "self", ".", "handle", ",", "ctypes", ".", "c_bool", "(", "full_check", ")", ")", ")" ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
BaseSparseNDArray._data
A deep copy NDArray of the data array associated with the BaseSparseNDArray. This function blocks. Do not use it in performance critical code.
python/mxnet/ndarray/sparse.py
def _data(self): """A deep copy NDArray of the data array associated with the BaseSparseNDArray. This function blocks. Do not use it in performance critical code. """ self.wait_to_read() hdl = NDArrayHandle() check_call(_LIB.MXNDArrayGetDataNDArray(self.handle, ctypes.byref(hdl))) return NDArray(hdl)
def _data(self): """A deep copy NDArray of the data array associated with the BaseSparseNDArray. This function blocks. Do not use it in performance critical code. """ self.wait_to_read() hdl = NDArrayHandle() check_call(_LIB.MXNDArrayGetDataNDArray(self.handle, ctypes.byref(hdl))) return NDArray(hdl)
[ "A", "deep", "copy", "NDArray", "of", "the", "data", "array", "associated", "with", "the", "BaseSparseNDArray", "." ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/ndarray/sparse.py#L263-L271
[ "def", "_data", "(", "self", ")", ":", "self", ".", "wait_to_read", "(", ")", "hdl", "=", "NDArrayHandle", "(", ")", "check_call", "(", "_LIB", ".", "MXNDArrayGetDataNDArray", "(", "self", ".", "handle", ",", "ctypes", ".", "byref", "(", "hdl", ")", ")...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
BaseSparseNDArray._aux_data
Get a deep copy NDArray of the i-th aux data array associated with the BaseSparseNDArray. This function blocks. Do not use it in performance critical code.
python/mxnet/ndarray/sparse.py
def _aux_data(self, i): """ Get a deep copy NDArray of the i-th aux data array associated with the BaseSparseNDArray. This function blocks. Do not use it in performance critical code. """ self.wait_to_read() hdl = NDArrayHandle() check_call(_LIB.MXNDArrayGetAuxNDArray(self.handle, i, ctypes.byref(hdl))) return NDArray(hdl)
def _aux_data(self, i): """ Get a deep copy NDArray of the i-th aux data array associated with the BaseSparseNDArray. This function blocks. Do not use it in performance critical code. """ self.wait_to_read() hdl = NDArrayHandle() check_call(_LIB.MXNDArrayGetAuxNDArray(self.handle, i, ctypes.byref(hdl))) return NDArray(hdl)
[ "Get", "a", "deep", "copy", "NDArray", "of", "the", "i", "-", "th", "aux", "data", "array", "associated", "with", "the", "BaseSparseNDArray", "." ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/ndarray/sparse.py#L274-L283
[ "def", "_aux_data", "(", "self", ",", "i", ")", ":", "self", ".", "wait_to_read", "(", ")", "hdl", "=", "NDArrayHandle", "(", ")", "check_call", "(", "_LIB", ".", "MXNDArrayGetAuxNDArray", "(", "self", ".", "handle", ",", "i", ",", "ctypes", ".", "byre...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
CSRNDArray.asscipy
Returns a ``scipy.sparse.csr.csr_matrix`` object with value copied from this array Examples -------- >>> x = mx.nd.sparse.zeros('csr', (2,3)) >>> y = x.asscipy() >>> type(y) <type 'scipy.sparse.csr.csr_matrix'> >>> y <2x3 sparse matrix of type '<type 'numpy.float32'>' with 0 stored elements in Compressed Sparse Row format>
python/mxnet/ndarray/sparse.py
def asscipy(self): """Returns a ``scipy.sparse.csr.csr_matrix`` object with value copied from this array Examples -------- >>> x = mx.nd.sparse.zeros('csr', (2,3)) >>> y = x.asscipy() >>> type(y) <type 'scipy.sparse.csr.csr_matrix'> >>> y <2x3 sparse matrix of type '<type 'numpy.float32'>' with 0 stored elements in Compressed Sparse Row format> """ data = self.data.asnumpy() indices = self.indices.asnumpy() indptr = self.indptr.asnumpy() if not spsp: raise ImportError("scipy is not available. \ Please check if the scipy python bindings are installed.") return spsp.csr_matrix((data, indices, indptr), shape=self.shape, dtype=self.dtype)
def asscipy(self): """Returns a ``scipy.sparse.csr.csr_matrix`` object with value copied from this array Examples -------- >>> x = mx.nd.sparse.zeros('csr', (2,3)) >>> y = x.asscipy() >>> type(y) <type 'scipy.sparse.csr.csr_matrix'> >>> y <2x3 sparse matrix of type '<type 'numpy.float32'>' with 0 stored elements in Compressed Sparse Row format> """ data = self.data.asnumpy() indices = self.indices.asnumpy() indptr = self.indptr.asnumpy() if not spsp: raise ImportError("scipy is not available. \ Please check if the scipy python bindings are installed.") return spsp.csr_matrix((data, indices, indptr), shape=self.shape, dtype=self.dtype)
[ "Returns", "a", "scipy", ".", "sparse", ".", "csr", ".", "csr_matrix", "object", "with", "value", "copied", "from", "this", "array" ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/ndarray/sparse.py#L539-L558
[ "def", "asscipy", "(", "self", ")", ":", "data", "=", "self", ".", "data", ".", "asnumpy", "(", ")", "indices", "=", "self", ".", "indices", ".", "asnumpy", "(", ")", "indptr", "=", "self", ".", "indptr", ".", "asnumpy", "(", ")", "if", "not", "s...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
RowSparseNDArray.tostype
Return a copy of the array with chosen storage type. Returns ------- NDArray or RowSparseNDArray A copy of the array with the chosen storage stype
python/mxnet/ndarray/sparse.py
def tostype(self, stype): """Return a copy of the array with chosen storage type. Returns ------- NDArray or RowSparseNDArray A copy of the array with the chosen storage stype """ # pylint: disable= no-member, protected-access if stype == 'csr': raise ValueError("cast_storage from row_sparse to csr is not supported") return op.cast_storage(self, stype=stype)
def tostype(self, stype): """Return a copy of the array with chosen storage type. Returns ------- NDArray or RowSparseNDArray A copy of the array with the chosen storage stype """ # pylint: disable= no-member, protected-access if stype == 'csr': raise ValueError("cast_storage from row_sparse to csr is not supported") return op.cast_storage(self, stype=stype)
[ "Return", "a", "copy", "of", "the", "array", "with", "chosen", "storage", "type", "." ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/ndarray/sparse.py#L740-L751
[ "def", "tostype", "(", "self", ",", "stype", ")", ":", "# pylint: disable= no-member, protected-access", "if", "stype", "==", "'csr'", ":", "raise", "ValueError", "(", "\"cast_storage from row_sparse to csr is not supported\"", ")", "return", "op", ".", "cast_storage", ...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
RowSparseNDArray.copyto
Copies the value of this array to another array. If ``other`` is a ``NDArray`` or ``RowSparseNDArray`` object, then ``other.shape`` and ``self.shape`` should be the same. This function copies the value from ``self`` to ``other``. If ``other`` is a context, a new ``RowSparseNDArray`` will be first created on the target context, and the value of ``self`` is copied. Parameters ---------- other : NDArray or RowSparseNDArray or Context The destination array or context. Returns ------- NDArray or RowSparseNDArray The copied array. If ``other`` is an ``NDArray`` or ``RowSparseNDArray``, then the return value and ``other`` will point to the same ``NDArray`` or ``RowSparseNDArray``.
python/mxnet/ndarray/sparse.py
def copyto(self, other): """Copies the value of this array to another array. If ``other`` is a ``NDArray`` or ``RowSparseNDArray`` object, then ``other.shape`` and ``self.shape`` should be the same. This function copies the value from ``self`` to ``other``. If ``other`` is a context, a new ``RowSparseNDArray`` will be first created on the target context, and the value of ``self`` is copied. Parameters ---------- other : NDArray or RowSparseNDArray or Context The destination array or context. Returns ------- NDArray or RowSparseNDArray The copied array. If ``other`` is an ``NDArray`` or ``RowSparseNDArray``, then the return value and ``other`` will point to the same ``NDArray`` or ``RowSparseNDArray``. """ if isinstance(other, Context): return super(RowSparseNDArray, self).copyto(other) elif isinstance(other, NDArray): stype = other.stype if stype in ('default', 'row_sparse'): return super(RowSparseNDArray, self).copyto(other) else: raise TypeError('copyto does not support destination NDArray stype ' + str(stype)) else: raise TypeError('copyto does not support type ' + str(type(other)))
def copyto(self, other): """Copies the value of this array to another array. If ``other`` is a ``NDArray`` or ``RowSparseNDArray`` object, then ``other.shape`` and ``self.shape`` should be the same. This function copies the value from ``self`` to ``other``. If ``other`` is a context, a new ``RowSparseNDArray`` will be first created on the target context, and the value of ``self`` is copied. Parameters ---------- other : NDArray or RowSparseNDArray or Context The destination array or context. Returns ------- NDArray or RowSparseNDArray The copied array. If ``other`` is an ``NDArray`` or ``RowSparseNDArray``, then the return value and ``other`` will point to the same ``NDArray`` or ``RowSparseNDArray``. """ if isinstance(other, Context): return super(RowSparseNDArray, self).copyto(other) elif isinstance(other, NDArray): stype = other.stype if stype in ('default', 'row_sparse'): return super(RowSparseNDArray, self).copyto(other) else: raise TypeError('copyto does not support destination NDArray stype ' + str(stype)) else: raise TypeError('copyto does not support type ' + str(type(other)))
[ "Copies", "the", "value", "of", "this", "array", "to", "another", "array", "." ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/ndarray/sparse.py#L754-L784
[ "def", "copyto", "(", "self", ",", "other", ")", ":", "if", "isinstance", "(", "other", ",", "Context", ")", ":", "return", "super", "(", "RowSparseNDArray", ",", "self", ")", ".", "copyto", "(", "other", ")", "elif", "isinstance", "(", "other", ",", ...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
export_model
Exports the MXNet model file, passed as a parameter, into ONNX model. Accepts both symbol,parameter objects as well as json and params filepaths as input. Operator support and coverage - https://cwiki.apache.org/confluence/display/MXNET/MXNet-ONNX+Integration Parameters ---------- sym : str or symbol object Path to the json file or Symbol object params : str or symbol object Path to the params file or params dictionary. (Including both arg_params and aux_params) input_shape : List of tuple Input shape of the model e.g [(1,3,224,224)] input_type : data type Input data type e.g. np.float32 onnx_file_path : str Path where to save the generated onnx file verbose : Boolean If true will print logs of the model conversion Returns ------- onnx_file_path : str Onnx file path Notes ----- This method is available when you ``import mxnet.contrib.onnx``
python/mxnet/contrib/onnx/mx2onnx/export_model.py
def export_model(sym, params, input_shape, input_type=np.float32, onnx_file_path='model.onnx', verbose=False): """Exports the MXNet model file, passed as a parameter, into ONNX model. Accepts both symbol,parameter objects as well as json and params filepaths as input. Operator support and coverage - https://cwiki.apache.org/confluence/display/MXNET/MXNet-ONNX+Integration Parameters ---------- sym : str or symbol object Path to the json file or Symbol object params : str or symbol object Path to the params file or params dictionary. (Including both arg_params and aux_params) input_shape : List of tuple Input shape of the model e.g [(1,3,224,224)] input_type : data type Input data type e.g. np.float32 onnx_file_path : str Path where to save the generated onnx file verbose : Boolean If true will print logs of the model conversion Returns ------- onnx_file_path : str Onnx file path Notes ----- This method is available when you ``import mxnet.contrib.onnx`` """ try: from onnx import helper, mapping except ImportError: raise ImportError("Onnx and protobuf need to be installed. " + "Instructions to install - https://github.com/onnx/onnx") converter = MXNetGraph() data_format = np.dtype(input_type) # if input parameters are strings(file paths), load files and create symbol parameter objects if isinstance(sym, string_types) and isinstance(params, string_types): logging.info("Converting json and weight file to sym and params") sym_obj, params_obj = load_module(sym, params) onnx_graph = converter.create_onnx_graph_proto(sym_obj, params_obj, input_shape, mapping.NP_TYPE_TO_TENSOR_TYPE[data_format], verbose=verbose) elif isinstance(sym, symbol.Symbol) and isinstance(params, dict): onnx_graph = converter.create_onnx_graph_proto(sym, params, input_shape, mapping.NP_TYPE_TO_TENSOR_TYPE[data_format], verbose=verbose) else: raise ValueError("Input sym and params should either be files or objects") # Create the model (ModelProto) onnx_model = helper.make_model(onnx_graph) # Save model on disk with open(onnx_file_path, "wb") as file_handle: serialized = onnx_model.SerializeToString() file_handle.write(serialized) logging.info("Input shape of the model %s ", input_shape) logging.info("Exported ONNX file %s saved to disk", onnx_file_path) return onnx_file_path
def export_model(sym, params, input_shape, input_type=np.float32, onnx_file_path='model.onnx', verbose=False): """Exports the MXNet model file, passed as a parameter, into ONNX model. Accepts both symbol,parameter objects as well as json and params filepaths as input. Operator support and coverage - https://cwiki.apache.org/confluence/display/MXNET/MXNet-ONNX+Integration Parameters ---------- sym : str or symbol object Path to the json file or Symbol object params : str or symbol object Path to the params file or params dictionary. (Including both arg_params and aux_params) input_shape : List of tuple Input shape of the model e.g [(1,3,224,224)] input_type : data type Input data type e.g. np.float32 onnx_file_path : str Path where to save the generated onnx file verbose : Boolean If true will print logs of the model conversion Returns ------- onnx_file_path : str Onnx file path Notes ----- This method is available when you ``import mxnet.contrib.onnx`` """ try: from onnx import helper, mapping except ImportError: raise ImportError("Onnx and protobuf need to be installed. " + "Instructions to install - https://github.com/onnx/onnx") converter = MXNetGraph() data_format = np.dtype(input_type) # if input parameters are strings(file paths), load files and create symbol parameter objects if isinstance(sym, string_types) and isinstance(params, string_types): logging.info("Converting json and weight file to sym and params") sym_obj, params_obj = load_module(sym, params) onnx_graph = converter.create_onnx_graph_proto(sym_obj, params_obj, input_shape, mapping.NP_TYPE_TO_TENSOR_TYPE[data_format], verbose=verbose) elif isinstance(sym, symbol.Symbol) and isinstance(params, dict): onnx_graph = converter.create_onnx_graph_proto(sym, params, input_shape, mapping.NP_TYPE_TO_TENSOR_TYPE[data_format], verbose=verbose) else: raise ValueError("Input sym and params should either be files or objects") # Create the model (ModelProto) onnx_model = helper.make_model(onnx_graph) # Save model on disk with open(onnx_file_path, "wb") as file_handle: serialized = onnx_model.SerializeToString() file_handle.write(serialized) logging.info("Input shape of the model %s ", input_shape) logging.info("Exported ONNX file %s saved to disk", onnx_file_path) return onnx_file_path
[ "Exports", "the", "MXNet", "model", "file", "passed", "as", "a", "parameter", "into", "ONNX", "model", ".", "Accepts", "both", "symbol", "parameter", "objects", "as", "well", "as", "json", "and", "params", "filepaths", "as", "input", ".", "Operator", "suppor...
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/contrib/onnx/mx2onnx/export_model.py#L35-L101
[ "def", "export_model", "(", "sym", ",", "params", ",", "input_shape", ",", "input_type", "=", "np", ".", "float32", ",", "onnx_file_path", "=", "'model.onnx'", ",", "verbose", "=", "False", ")", ":", "try", ":", "from", "onnx", "import", "helper", ",", "...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
bench_dot
Benchmarking both storage and dot
benchmark/python/sparse/memory_benchmark.py
def bench_dot(lhs_row_dim, lhs_col_dim, rhs_col_dim, density, rhs_density, dot_func, trans_lhs, lhs_stype, rhs_stype, only_storage, distribution="uniform"): """ Benchmarking both storage and dot """ lhs_nd = rand_ndarray((lhs_row_dim, lhs_col_dim), lhs_stype, density, distribution=distribution) if not only_storage: rhs_nd = rand_ndarray((lhs_col_dim, rhs_col_dim), rhs_stype, density=rhs_density, distribution=distribution) out = dot_func(lhs_nd, rhs_nd, trans_lhs) mx.nd.waitall()
def bench_dot(lhs_row_dim, lhs_col_dim, rhs_col_dim, density, rhs_density, dot_func, trans_lhs, lhs_stype, rhs_stype, only_storage, distribution="uniform"): """ Benchmarking both storage and dot """ lhs_nd = rand_ndarray((lhs_row_dim, lhs_col_dim), lhs_stype, density, distribution=distribution) if not only_storage: rhs_nd = rand_ndarray((lhs_col_dim, rhs_col_dim), rhs_stype, density=rhs_density, distribution=distribution) out = dot_func(lhs_nd, rhs_nd, trans_lhs) mx.nd.waitall()
[ "Benchmarking", "both", "storage", "and", "dot" ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/benchmark/python/sparse/memory_benchmark.py#L79-L89
[ "def", "bench_dot", "(", "lhs_row_dim", ",", "lhs_col_dim", ",", "rhs_col_dim", ",", "density", ",", "rhs_density", ",", "dot_func", ",", "trans_lhs", ",", "lhs_stype", ",", "rhs_stype", ",", "only_storage", ",", "distribution", "=", "\"uniform\"", ")", ":", "...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
convert_mean
Convert caffe mean Parameters ---------- binaryproto_fname : str Filename of the mean output : str, optional Save the mean into mxnet's format Returns ------- NDArray Mean in ndarray
tools/caffe_converter/convert_mean.py
def convert_mean(binaryproto_fname, output=None): """Convert caffe mean Parameters ---------- binaryproto_fname : str Filename of the mean output : str, optional Save the mean into mxnet's format Returns ------- NDArray Mean in ndarray """ mean_blob = caffe_parser.caffe_pb2.BlobProto() with open(binaryproto_fname, 'rb') as f: mean_blob.ParseFromString(f.read()) img_mean_np = np.array(mean_blob.data) img_mean_np = img_mean_np.reshape( mean_blob.channels, mean_blob.height, mean_blob.width ) # swap channels from Caffe BGR to RGB img_mean_np[[0, 2], :, :] = img_mean_np[[2, 0], :, :] nd = mx.nd.array(img_mean_np) if output is not None: mx.nd.save(output, {"mean_image": nd}) return nd
def convert_mean(binaryproto_fname, output=None): """Convert caffe mean Parameters ---------- binaryproto_fname : str Filename of the mean output : str, optional Save the mean into mxnet's format Returns ------- NDArray Mean in ndarray """ mean_blob = caffe_parser.caffe_pb2.BlobProto() with open(binaryproto_fname, 'rb') as f: mean_blob.ParseFromString(f.read()) img_mean_np = np.array(mean_blob.data) img_mean_np = img_mean_np.reshape( mean_blob.channels, mean_blob.height, mean_blob.width ) # swap channels from Caffe BGR to RGB img_mean_np[[0, 2], :, :] = img_mean_np[[2, 0], :, :] nd = mx.nd.array(img_mean_np) if output is not None: mx.nd.save(output, {"mean_image": nd}) return nd
[ "Convert", "caffe", "mean" ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/tools/caffe_converter/convert_mean.py#L25-L53
[ "def", "convert_mean", "(", "binaryproto_fname", ",", "output", "=", "None", ")", ":", "mean_blob", "=", "caffe_parser", ".", "caffe_pb2", ".", "BlobProto", "(", ")", "with", "open", "(", "binaryproto_fname", ",", "'rb'", ")", "as", "f", ":", "mean_blob", ...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
get_densenet
r"""Densenet-BC model from the `"Densely Connected Convolutional Networks" <https://arxiv.org/pdf/1608.06993.pdf>`_ paper. Parameters ---------- num_layers : int Number of layers for the variant of densenet. Options are 121, 161, 169, 201. pretrained : bool, default False Whether to load the pretrained weights for model. ctx : Context, default CPU The context in which to load the pretrained weights. root : str, default $MXNET_HOME/models Location for keeping the model parameters.
python/mxnet/gluon/model_zoo/vision/densenet.py
def get_densenet(num_layers, pretrained=False, ctx=cpu(), root=os.path.join(base.data_dir(), 'models'), **kwargs): r"""Densenet-BC model from the `"Densely Connected Convolutional Networks" <https://arxiv.org/pdf/1608.06993.pdf>`_ paper. Parameters ---------- num_layers : int Number of layers for the variant of densenet. Options are 121, 161, 169, 201. pretrained : bool, default False Whether to load the pretrained weights for model. ctx : Context, default CPU The context in which to load the pretrained weights. root : str, default $MXNET_HOME/models Location for keeping the model parameters. """ num_init_features, growth_rate, block_config = densenet_spec[num_layers] net = DenseNet(num_init_features, growth_rate, block_config, **kwargs) if pretrained: from ..model_store import get_model_file net.load_parameters(get_model_file('densenet%d'%(num_layers), root=root), ctx=ctx) return net
def get_densenet(num_layers, pretrained=False, ctx=cpu(), root=os.path.join(base.data_dir(), 'models'), **kwargs): r"""Densenet-BC model from the `"Densely Connected Convolutional Networks" <https://arxiv.org/pdf/1608.06993.pdf>`_ paper. Parameters ---------- num_layers : int Number of layers for the variant of densenet. Options are 121, 161, 169, 201. pretrained : bool, default False Whether to load the pretrained weights for model. ctx : Context, default CPU The context in which to load the pretrained weights. root : str, default $MXNET_HOME/models Location for keeping the model parameters. """ num_init_features, growth_rate, block_config = densenet_spec[num_layers] net = DenseNet(num_init_features, growth_rate, block_config, **kwargs) if pretrained: from ..model_store import get_model_file net.load_parameters(get_model_file('densenet%d'%(num_layers), root=root), ctx=ctx) return net
[ "r", "Densenet", "-", "BC", "model", "from", "the", "Densely", "Connected", "Convolutional", "Networks", "<https", ":", "//", "arxiv", ".", "org", "/", "pdf", "/", "1608", ".", "06993", ".", "pdf", ">", "_", "paper", "." ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/gluon/model_zoo/vision/densenet.py#L125-L146
[ "def", "get_densenet", "(", "num_layers", ",", "pretrained", "=", "False", ",", "ctx", "=", "cpu", "(", ")", ",", "root", "=", "os", ".", "path", ".", "join", "(", "base", ".", "data_dir", "(", ")", ",", "'models'", ")", ",", "*", "*", "kwargs", ...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
load_module
Loads the MXNet model file and returns MXNet symbol and params (weights). Parameters ---------- json_path : str Path to the json file params_path : str Path to the params file Returns ------- sym : MXNet symbol Model symbol object params : params object Model weights including both arg and aux params.
python/mxnet/contrib/onnx/mx2onnx/_export_helper.py
def load_module(sym_filepath, params_filepath): """Loads the MXNet model file and returns MXNet symbol and params (weights). Parameters ---------- json_path : str Path to the json file params_path : str Path to the params file Returns ------- sym : MXNet symbol Model symbol object params : params object Model weights including both arg and aux params. """ if not (os.path.isfile(sym_filepath) and os.path.isfile(params_filepath)): raise ValueError("Symbol and params files provided are invalid") else: try: # reads symbol.json file from given path and # retrieves model prefix and number of epochs model_name = sym_filepath.rsplit('.', 1)[0].rsplit('-', 1)[0] params_file_list = params_filepath.rsplit('.', 1)[0].rsplit('-', 1) # Setting num_epochs to 0 if not present in filename num_epochs = 0 if len(params_file_list) == 1 else int(params_file_list[1]) except IndexError: logging.info("Model and params name should be in format: " "prefix-symbol.json, prefix-epoch.params") raise sym, arg_params, aux_params = mx.model.load_checkpoint(model_name, num_epochs) # Merging arg and aux parameters params = {} params.update(arg_params) params.update(aux_params) return sym, params
def load_module(sym_filepath, params_filepath): """Loads the MXNet model file and returns MXNet symbol and params (weights). Parameters ---------- json_path : str Path to the json file params_path : str Path to the params file Returns ------- sym : MXNet symbol Model symbol object params : params object Model weights including both arg and aux params. """ if not (os.path.isfile(sym_filepath) and os.path.isfile(params_filepath)): raise ValueError("Symbol and params files provided are invalid") else: try: # reads symbol.json file from given path and # retrieves model prefix and number of epochs model_name = sym_filepath.rsplit('.', 1)[0].rsplit('-', 1)[0] params_file_list = params_filepath.rsplit('.', 1)[0].rsplit('-', 1) # Setting num_epochs to 0 if not present in filename num_epochs = 0 if len(params_file_list) == 1 else int(params_file_list[1]) except IndexError: logging.info("Model and params name should be in format: " "prefix-symbol.json, prefix-epoch.params") raise sym, arg_params, aux_params = mx.model.load_checkpoint(model_name, num_epochs) # Merging arg and aux parameters params = {} params.update(arg_params) params.update(aux_params) return sym, params
[ "Loads", "the", "MXNet", "model", "file", "and", "returns", "MXNet", "symbol", "and", "params", "(", "weights", ")", "." ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/contrib/onnx/mx2onnx/_export_helper.py#L24-L65
[ "def", "load_module", "(", "sym_filepath", ",", "params_filepath", ")", ":", "if", "not", "(", "os", ".", "path", ".", "isfile", "(", "sym_filepath", ")", "and", "os", ".", "path", ".", "isfile", "(", "params_filepath", ")", ")", ":", "raise", "ValueErro...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
import_module
Helper function to import module
example/ssd/symbol/symbol_builder.py
def import_module(module_name): """Helper function to import module""" import sys, os import importlib sys.path.append(os.path.dirname(__file__)) return importlib.import_module(module_name)
def import_module(module_name): """Helper function to import module""" import sys, os import importlib sys.path.append(os.path.dirname(__file__)) return importlib.import_module(module_name)
[ "Helper", "function", "to", "import", "module" ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/example/ssd/symbol/symbol_builder.py#L22-L27
[ "def", "import_module", "(", "module_name", ")", ":", "import", "sys", ",", "os", "import", "importlib", "sys", ".", "path", ".", "append", "(", "os", ".", "path", ".", "dirname", "(", "__file__", ")", ")", "return", "importlib", ".", "import_module", "(...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
get_symbol_train
Build network symbol for training SSD Parameters ---------- network : str base network symbol name num_classes : int number of object classes not including background from_layers : list of str feature extraction layers, use '' for add extra layers For example: from_layers = ['relu4_3', 'fc7', '', '', '', ''] which means extract feature from relu4_3 and fc7, adding 4 extra layers on top of fc7 num_filters : list of int number of filters for extra layers, you can use -1 for extracted features, however, if normalization and scale is applied, the number of filter for that layer must be provided. For example: num_filters = [512, -1, 512, 256, 256, 256] strides : list of int strides for the 3x3 convolution appended, -1 can be used for extracted feature layers pads : list of int paddings for the 3x3 convolution, -1 can be used for extracted layers sizes : list or list of list [min_size, max_size] for all layers or [[], [], []...] for specific layers ratios : list or list of list [ratio1, ratio2...] for all layers or [[], [], ...] for specific layers normalizations : int or list of int use normalizations value for all layers or [...] for specific layers, -1 indicate no normalizations and scales steps : list specify steps for each MultiBoxPrior layer, leave empty, it will calculate according to layer dimensions min_filter : int minimum number of filters used in 1x1 convolution nms_thresh : float non-maximum suppression threshold force_suppress : boolean whether suppress different class objects nms_topk : int apply NMS to top K detections Returns ------- mx.Symbol
example/ssd/symbol/symbol_builder.py
def get_symbol_train(network, num_classes, from_layers, num_filters, strides, pads, sizes, ratios, normalizations=-1, steps=[], min_filter=128, nms_thresh=0.5, force_suppress=False, nms_topk=400, **kwargs): """Build network symbol for training SSD Parameters ---------- network : str base network symbol name num_classes : int number of object classes not including background from_layers : list of str feature extraction layers, use '' for add extra layers For example: from_layers = ['relu4_3', 'fc7', '', '', '', ''] which means extract feature from relu4_3 and fc7, adding 4 extra layers on top of fc7 num_filters : list of int number of filters for extra layers, you can use -1 for extracted features, however, if normalization and scale is applied, the number of filter for that layer must be provided. For example: num_filters = [512, -1, 512, 256, 256, 256] strides : list of int strides for the 3x3 convolution appended, -1 can be used for extracted feature layers pads : list of int paddings for the 3x3 convolution, -1 can be used for extracted layers sizes : list or list of list [min_size, max_size] for all layers or [[], [], []...] for specific layers ratios : list or list of list [ratio1, ratio2...] for all layers or [[], [], ...] for specific layers normalizations : int or list of int use normalizations value for all layers or [...] for specific layers, -1 indicate no normalizations and scales steps : list specify steps for each MultiBoxPrior layer, leave empty, it will calculate according to layer dimensions min_filter : int minimum number of filters used in 1x1 convolution nms_thresh : float non-maximum suppression threshold force_suppress : boolean whether suppress different class objects nms_topk : int apply NMS to top K detections Returns ------- mx.Symbol """ label = mx.sym.Variable('label') body = import_module(network).get_symbol(num_classes, **kwargs) layers = multi_layer_feature(body, from_layers, num_filters, strides, pads, min_filter=min_filter) loc_preds, cls_preds, anchor_boxes = multibox_layer(layers, \ num_classes, sizes=sizes, ratios=ratios, normalization=normalizations, \ num_channels=num_filters, clip=False, interm_layer=0, steps=steps) tmp = mx.symbol.contrib.MultiBoxTarget( *[anchor_boxes, label, cls_preds], overlap_threshold=.5, \ ignore_label=-1, negative_mining_ratio=3, minimum_negative_samples=0, \ negative_mining_thresh=.5, variances=(0.1, 0.1, 0.2, 0.2), name="multibox_target") loc_target = tmp[0] loc_target_mask = tmp[1] cls_target = tmp[2] cls_prob = mx.symbol.SoftmaxOutput(data=cls_preds, label=cls_target, \ ignore_label=-1, use_ignore=True, grad_scale=1., multi_output=True, \ normalization='valid', name="cls_prob") loc_loss_ = mx.symbol.smooth_l1(name="loc_loss_", \ data=loc_target_mask * (loc_preds - loc_target), scalar=1.0) loc_loss = mx.symbol.MakeLoss(loc_loss_, grad_scale=1., \ normalization='valid', name="loc_loss") # monitoring training status cls_label = mx.symbol.MakeLoss(data=cls_target, grad_scale=0, name="cls_label") det = mx.symbol.contrib.MultiBoxDetection(*[cls_prob, loc_preds, anchor_boxes], \ name="detection", nms_threshold=nms_thresh, force_suppress=force_suppress, variances=(0.1, 0.1, 0.2, 0.2), nms_topk=nms_topk) det = mx.symbol.MakeLoss(data=det, grad_scale=0, name="det_out") # group output out = mx.symbol.Group([cls_prob, loc_loss, cls_label, det]) return out
def get_symbol_train(network, num_classes, from_layers, num_filters, strides, pads, sizes, ratios, normalizations=-1, steps=[], min_filter=128, nms_thresh=0.5, force_suppress=False, nms_topk=400, **kwargs): """Build network symbol for training SSD Parameters ---------- network : str base network symbol name num_classes : int number of object classes not including background from_layers : list of str feature extraction layers, use '' for add extra layers For example: from_layers = ['relu4_3', 'fc7', '', '', '', ''] which means extract feature from relu4_3 and fc7, adding 4 extra layers on top of fc7 num_filters : list of int number of filters for extra layers, you can use -1 for extracted features, however, if normalization and scale is applied, the number of filter for that layer must be provided. For example: num_filters = [512, -1, 512, 256, 256, 256] strides : list of int strides for the 3x3 convolution appended, -1 can be used for extracted feature layers pads : list of int paddings for the 3x3 convolution, -1 can be used for extracted layers sizes : list or list of list [min_size, max_size] for all layers or [[], [], []...] for specific layers ratios : list or list of list [ratio1, ratio2...] for all layers or [[], [], ...] for specific layers normalizations : int or list of int use normalizations value for all layers or [...] for specific layers, -1 indicate no normalizations and scales steps : list specify steps for each MultiBoxPrior layer, leave empty, it will calculate according to layer dimensions min_filter : int minimum number of filters used in 1x1 convolution nms_thresh : float non-maximum suppression threshold force_suppress : boolean whether suppress different class objects nms_topk : int apply NMS to top K detections Returns ------- mx.Symbol """ label = mx.sym.Variable('label') body = import_module(network).get_symbol(num_classes, **kwargs) layers = multi_layer_feature(body, from_layers, num_filters, strides, pads, min_filter=min_filter) loc_preds, cls_preds, anchor_boxes = multibox_layer(layers, \ num_classes, sizes=sizes, ratios=ratios, normalization=normalizations, \ num_channels=num_filters, clip=False, interm_layer=0, steps=steps) tmp = mx.symbol.contrib.MultiBoxTarget( *[anchor_boxes, label, cls_preds], overlap_threshold=.5, \ ignore_label=-1, negative_mining_ratio=3, minimum_negative_samples=0, \ negative_mining_thresh=.5, variances=(0.1, 0.1, 0.2, 0.2), name="multibox_target") loc_target = tmp[0] loc_target_mask = tmp[1] cls_target = tmp[2] cls_prob = mx.symbol.SoftmaxOutput(data=cls_preds, label=cls_target, \ ignore_label=-1, use_ignore=True, grad_scale=1., multi_output=True, \ normalization='valid', name="cls_prob") loc_loss_ = mx.symbol.smooth_l1(name="loc_loss_", \ data=loc_target_mask * (loc_preds - loc_target), scalar=1.0) loc_loss = mx.symbol.MakeLoss(loc_loss_, grad_scale=1., \ normalization='valid', name="loc_loss") # monitoring training status cls_label = mx.symbol.MakeLoss(data=cls_target, grad_scale=0, name="cls_label") det = mx.symbol.contrib.MultiBoxDetection(*[cls_prob, loc_preds, anchor_boxes], \ name="detection", nms_threshold=nms_thresh, force_suppress=force_suppress, variances=(0.1, 0.1, 0.2, 0.2), nms_topk=nms_topk) det = mx.symbol.MakeLoss(data=det, grad_scale=0, name="det_out") # group output out = mx.symbol.Group([cls_prob, loc_loss, cls_label, det]) return out
[ "Build", "network", "symbol", "for", "training", "SSD" ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/example/ssd/symbol/symbol_builder.py#L29-L116
[ "def", "get_symbol_train", "(", "network", ",", "num_classes", ",", "from_layers", ",", "num_filters", ",", "strides", ",", "pads", ",", "sizes", ",", "ratios", ",", "normalizations", "=", "-", "1", ",", "steps", "=", "[", "]", ",", "min_filter", "=", "1...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
get_symbol
Build network for testing SSD Parameters ---------- network : str base network symbol name num_classes : int number of object classes not including background from_layers : list of str feature extraction layers, use '' for add extra layers For example: from_layers = ['relu4_3', 'fc7', '', '', '', ''] which means extract feature from relu4_3 and fc7, adding 4 extra layers on top of fc7 num_filters : list of int number of filters for extra layers, you can use -1 for extracted features, however, if normalization and scale is applied, the number of filter for that layer must be provided. For example: num_filters = [512, -1, 512, 256, 256, 256] strides : list of int strides for the 3x3 convolution appended, -1 can be used for extracted feature layers pads : list of int paddings for the 3x3 convolution, -1 can be used for extracted layers sizes : list or list of list [min_size, max_size] for all layers or [[], [], []...] for specific layers ratios : list or list of list [ratio1, ratio2...] for all layers or [[], [], ...] for specific layers normalizations : int or list of int use normalizations value for all layers or [...] for specific layers, -1 indicate no normalizations and scales steps : list specify steps for each MultiBoxPrior layer, leave empty, it will calculate according to layer dimensions min_filter : int minimum number of filters used in 1x1 convolution nms_thresh : float non-maximum suppression threshold force_suppress : boolean whether suppress different class objects nms_topk : int apply NMS to top K detections Returns ------- mx.Symbol
example/ssd/symbol/symbol_builder.py
def get_symbol(network, num_classes, from_layers, num_filters, sizes, ratios, strides, pads, normalizations=-1, steps=[], min_filter=128, nms_thresh=0.5, force_suppress=False, nms_topk=400, **kwargs): """Build network for testing SSD Parameters ---------- network : str base network symbol name num_classes : int number of object classes not including background from_layers : list of str feature extraction layers, use '' for add extra layers For example: from_layers = ['relu4_3', 'fc7', '', '', '', ''] which means extract feature from relu4_3 and fc7, adding 4 extra layers on top of fc7 num_filters : list of int number of filters for extra layers, you can use -1 for extracted features, however, if normalization and scale is applied, the number of filter for that layer must be provided. For example: num_filters = [512, -1, 512, 256, 256, 256] strides : list of int strides for the 3x3 convolution appended, -1 can be used for extracted feature layers pads : list of int paddings for the 3x3 convolution, -1 can be used for extracted layers sizes : list or list of list [min_size, max_size] for all layers or [[], [], []...] for specific layers ratios : list or list of list [ratio1, ratio2...] for all layers or [[], [], ...] for specific layers normalizations : int or list of int use normalizations value for all layers or [...] for specific layers, -1 indicate no normalizations and scales steps : list specify steps for each MultiBoxPrior layer, leave empty, it will calculate according to layer dimensions min_filter : int minimum number of filters used in 1x1 convolution nms_thresh : float non-maximum suppression threshold force_suppress : boolean whether suppress different class objects nms_topk : int apply NMS to top K detections Returns ------- mx.Symbol """ body = import_module(network).get_symbol(num_classes, **kwargs) layers = multi_layer_feature(body, from_layers, num_filters, strides, pads, min_filter=min_filter) loc_preds, cls_preds, anchor_boxes = multibox_layer(layers, \ num_classes, sizes=sizes, ratios=ratios, normalization=normalizations, \ num_channels=num_filters, clip=False, interm_layer=0, steps=steps) cls_prob = mx.symbol.softmax(data=cls_preds, axis=1, name='cls_prob') out = mx.symbol.contrib.MultiBoxDetection(*[cls_prob, loc_preds, anchor_boxes], \ name="detection", nms_threshold=nms_thresh, force_suppress=force_suppress, variances=(0.1, 0.1, 0.2, 0.2), nms_topk=nms_topk) return out
def get_symbol(network, num_classes, from_layers, num_filters, sizes, ratios, strides, pads, normalizations=-1, steps=[], min_filter=128, nms_thresh=0.5, force_suppress=False, nms_topk=400, **kwargs): """Build network for testing SSD Parameters ---------- network : str base network symbol name num_classes : int number of object classes not including background from_layers : list of str feature extraction layers, use '' for add extra layers For example: from_layers = ['relu4_3', 'fc7', '', '', '', ''] which means extract feature from relu4_3 and fc7, adding 4 extra layers on top of fc7 num_filters : list of int number of filters for extra layers, you can use -1 for extracted features, however, if normalization and scale is applied, the number of filter for that layer must be provided. For example: num_filters = [512, -1, 512, 256, 256, 256] strides : list of int strides for the 3x3 convolution appended, -1 can be used for extracted feature layers pads : list of int paddings for the 3x3 convolution, -1 can be used for extracted layers sizes : list or list of list [min_size, max_size] for all layers or [[], [], []...] for specific layers ratios : list or list of list [ratio1, ratio2...] for all layers or [[], [], ...] for specific layers normalizations : int or list of int use normalizations value for all layers or [...] for specific layers, -1 indicate no normalizations and scales steps : list specify steps for each MultiBoxPrior layer, leave empty, it will calculate according to layer dimensions min_filter : int minimum number of filters used in 1x1 convolution nms_thresh : float non-maximum suppression threshold force_suppress : boolean whether suppress different class objects nms_topk : int apply NMS to top K detections Returns ------- mx.Symbol """ body = import_module(network).get_symbol(num_classes, **kwargs) layers = multi_layer_feature(body, from_layers, num_filters, strides, pads, min_filter=min_filter) loc_preds, cls_preds, anchor_boxes = multibox_layer(layers, \ num_classes, sizes=sizes, ratios=ratios, normalization=normalizations, \ num_channels=num_filters, clip=False, interm_layer=0, steps=steps) cls_prob = mx.symbol.softmax(data=cls_preds, axis=1, name='cls_prob') out = mx.symbol.contrib.MultiBoxDetection(*[cls_prob, loc_preds, anchor_boxes], \ name="detection", nms_threshold=nms_thresh, force_suppress=force_suppress, variances=(0.1, 0.1, 0.2, 0.2), nms_topk=nms_topk) return out
[ "Build", "network", "for", "testing", "SSD" ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/example/ssd/symbol/symbol_builder.py#L118-L182
[ "def", "get_symbol", "(", "network", ",", "num_classes", ",", "from_layers", ",", "num_filters", ",", "sizes", ",", "ratios", ",", "strides", ",", "pads", ",", "normalizations", "=", "-", "1", ",", "steps", "=", "[", "]", ",", "min_filter", "=", "128", ...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
_get_grad
This is an internal helper function that can be used for either of these but not both at the same time: 1. Record the output and gradient of output of an intermediate convolutional layer. 2. Record the gradients of the image. Parameters ---------- image : NDArray Image to visuaize. This is an NDArray with the preprocessed image. class_id : int Category ID this image belongs to. If not provided, network's prediction will be used. conv_layer_name: str Name of the convolutional layer whose output and output's gradients need to be acptured. image_grad: bool Whether to capture gradients of the image.
docs/tutorial_utils/vision/cnn_visualization/gradcam.py
def _get_grad(net, image, class_id=None, conv_layer_name=None, image_grad=False): """This is an internal helper function that can be used for either of these but not both at the same time: 1. Record the output and gradient of output of an intermediate convolutional layer. 2. Record the gradients of the image. Parameters ---------- image : NDArray Image to visuaize. This is an NDArray with the preprocessed image. class_id : int Category ID this image belongs to. If not provided, network's prediction will be used. conv_layer_name: str Name of the convolutional layer whose output and output's gradients need to be acptured. image_grad: bool Whether to capture gradients of the image.""" if image_grad: image.attach_grad() Conv2D.capture_layer_name = None Activation.set_guided_backprop(True) else: # Tell convviz.Conv2D which layer's output and gradient needs to be recorded Conv2D.capture_layer_name = conv_layer_name Activation.set_guided_backprop(False) # Run the network with autograd.record(train_mode=False): out = net(image) # If user didn't provide a class id, we'll use the class that the network predicted if class_id == None: model_output = out.asnumpy() class_id = np.argmax(model_output) # Create a one-hot target with class_id and backprop with the created target one_hot_target = mx.nd.one_hot(mx.nd.array([class_id]), 1000) out.backward(one_hot_target, train_mode=False) if image_grad: return image.grad[0].asnumpy() else: # Return the recorded convolution output and gradient conv_out = Conv2D.conv_output return conv_out[0].asnumpy(), conv_out.grad[0].asnumpy()
def _get_grad(net, image, class_id=None, conv_layer_name=None, image_grad=False): """This is an internal helper function that can be used for either of these but not both at the same time: 1. Record the output and gradient of output of an intermediate convolutional layer. 2. Record the gradients of the image. Parameters ---------- image : NDArray Image to visuaize. This is an NDArray with the preprocessed image. class_id : int Category ID this image belongs to. If not provided, network's prediction will be used. conv_layer_name: str Name of the convolutional layer whose output and output's gradients need to be acptured. image_grad: bool Whether to capture gradients of the image.""" if image_grad: image.attach_grad() Conv2D.capture_layer_name = None Activation.set_guided_backprop(True) else: # Tell convviz.Conv2D which layer's output and gradient needs to be recorded Conv2D.capture_layer_name = conv_layer_name Activation.set_guided_backprop(False) # Run the network with autograd.record(train_mode=False): out = net(image) # If user didn't provide a class id, we'll use the class that the network predicted if class_id == None: model_output = out.asnumpy() class_id = np.argmax(model_output) # Create a one-hot target with class_id and backprop with the created target one_hot_target = mx.nd.one_hot(mx.nd.array([class_id]), 1000) out.backward(one_hot_target, train_mode=False) if image_grad: return image.grad[0].asnumpy() else: # Return the recorded convolution output and gradient conv_out = Conv2D.conv_output return conv_out[0].asnumpy(), conv_out.grad[0].asnumpy()
[ "This", "is", "an", "internal", "helper", "function", "that", "can", "be", "used", "for", "either", "of", "these", "but", "not", "both", "at", "the", "same", "time", ":", "1", ".", "Record", "the", "output", "and", "gradient", "of", "output", "of", "an...
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/docs/tutorial_utils/vision/cnn_visualization/gradcam.py#L122-L167
[ "def", "_get_grad", "(", "net", ",", "image", ",", "class_id", "=", "None", ",", "conv_layer_name", "=", "None", ",", "image_grad", "=", "False", ")", ":", "if", "image_grad", ":", "image", ".", "attach_grad", "(", ")", "Conv2D", ".", "capture_layer_name",...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
get_conv_out_grad
Get the output and gradients of output of a convolutional layer. Parameters: ---------- net: Block Network to use for visualization. image: NDArray Preprocessed image to use for visualization. class_id: int Category ID this image belongs to. If not provided, network's prediction will be used. conv_layer_name: str Name of the convolutional layer whose output and output's gradients need to be acptured.
docs/tutorial_utils/vision/cnn_visualization/gradcam.py
def get_conv_out_grad(net, image, class_id=None, conv_layer_name=None): """Get the output and gradients of output of a convolutional layer. Parameters: ---------- net: Block Network to use for visualization. image: NDArray Preprocessed image to use for visualization. class_id: int Category ID this image belongs to. If not provided, network's prediction will be used. conv_layer_name: str Name of the convolutional layer whose output and output's gradients need to be acptured.""" return _get_grad(net, image, class_id, conv_layer_name, image_grad=False)
def get_conv_out_grad(net, image, class_id=None, conv_layer_name=None): """Get the output and gradients of output of a convolutional layer. Parameters: ---------- net: Block Network to use for visualization. image: NDArray Preprocessed image to use for visualization. class_id: int Category ID this image belongs to. If not provided, network's prediction will be used. conv_layer_name: str Name of the convolutional layer whose output and output's gradients need to be acptured.""" return _get_grad(net, image, class_id, conv_layer_name, image_grad=False)
[ "Get", "the", "output", "and", "gradients", "of", "output", "of", "a", "convolutional", "layer", "." ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/docs/tutorial_utils/vision/cnn_visualization/gradcam.py#L169-L183
[ "def", "get_conv_out_grad", "(", "net", ",", "image", ",", "class_id", "=", "None", ",", "conv_layer_name", "=", "None", ")", ":", "return", "_get_grad", "(", "net", ",", "image", ",", "class_id", ",", "conv_layer_name", ",", "image_grad", "=", "False", ")...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
get_image_grad
Get the gradients of the image. Parameters: ---------- net: Block Network to use for visualization. image: NDArray Preprocessed image to use for visualization. class_id: int Category ID this image belongs to. If not provided, network's prediction will be used.
docs/tutorial_utils/vision/cnn_visualization/gradcam.py
def get_image_grad(net, image, class_id=None): """Get the gradients of the image. Parameters: ---------- net: Block Network to use for visualization. image: NDArray Preprocessed image to use for visualization. class_id: int Category ID this image belongs to. If not provided, network's prediction will be used.""" return _get_grad(net, image, class_id, image_grad=True)
def get_image_grad(net, image, class_id=None): """Get the gradients of the image. Parameters: ---------- net: Block Network to use for visualization. image: NDArray Preprocessed image to use for visualization. class_id: int Category ID this image belongs to. If not provided, network's prediction will be used.""" return _get_grad(net, image, class_id, image_grad=True)
[ "Get", "the", "gradients", "of", "the", "image", "." ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/docs/tutorial_utils/vision/cnn_visualization/gradcam.py#L185-L197
[ "def", "get_image_grad", "(", "net", ",", "image", ",", "class_id", "=", "None", ")", ":", "return", "_get_grad", "(", "net", ",", "image", ",", "class_id", ",", "image_grad", "=", "True", ")" ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
grad_to_image
Convert gradients of image obtained using `get_image_grad` into image. This shows parts of the image that is most strongly activating the output neurons.
docs/tutorial_utils/vision/cnn_visualization/gradcam.py
def grad_to_image(gradient): """Convert gradients of image obtained using `get_image_grad` into image. This shows parts of the image that is most strongly activating the output neurons.""" gradient = gradient - gradient.min() gradient /= gradient.max() gradient = np.uint8(gradient * 255).transpose(1, 2, 0) gradient = gradient[..., ::-1] return gradient
def grad_to_image(gradient): """Convert gradients of image obtained using `get_image_grad` into image. This shows parts of the image that is most strongly activating the output neurons.""" gradient = gradient - gradient.min() gradient /= gradient.max() gradient = np.uint8(gradient * 255).transpose(1, 2, 0) gradient = gradient[..., ::-1] return gradient
[ "Convert", "gradients", "of", "image", "obtained", "using", "get_image_grad", "into", "image", ".", "This", "shows", "parts", "of", "the", "image", "that", "is", "most", "strongly", "activating", "the", "output", "neurons", "." ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/docs/tutorial_utils/vision/cnn_visualization/gradcam.py#L199-L207
[ "def", "grad_to_image", "(", "gradient", ")", ":", "gradient", "=", "gradient", "-", "gradient", ".", "min", "(", ")", "gradient", "/=", "gradient", ".", "max", "(", ")", "gradient", "=", "np", ".", "uint8", "(", "gradient", "*", "255", ")", ".", "tr...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
get_cam
Compute CAM. Refer section 3 of https://arxiv.org/abs/1610.02391 for details
docs/tutorial_utils/vision/cnn_visualization/gradcam.py
def get_cam(imggrad, conv_out): """Compute CAM. Refer section 3 of https://arxiv.org/abs/1610.02391 for details""" weights = np.mean(imggrad, axis=(1, 2)) cam = np.ones(conv_out.shape[1:], dtype=np.float32) for i, w in enumerate(weights): cam += w * conv_out[i, :, :] cam = cv2.resize(cam, (imggrad.shape[1], imggrad.shape[2])) cam = np.maximum(cam, 0) cam = (cam - np.min(cam)) / (np.max(cam) - np.min(cam)) cam = np.uint8(cam * 255) return cam
def get_cam(imggrad, conv_out): """Compute CAM. Refer section 3 of https://arxiv.org/abs/1610.02391 for details""" weights = np.mean(imggrad, axis=(1, 2)) cam = np.ones(conv_out.shape[1:], dtype=np.float32) for i, w in enumerate(weights): cam += w * conv_out[i, :, :] cam = cv2.resize(cam, (imggrad.shape[1], imggrad.shape[2])) cam = np.maximum(cam, 0) cam = (cam - np.min(cam)) / (np.max(cam) - np.min(cam)) cam = np.uint8(cam * 255) return cam
[ "Compute", "CAM", ".", "Refer", "section", "3", "of", "https", ":", "//", "arxiv", ".", "org", "/", "abs", "/", "1610", ".", "02391", "for", "details" ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/docs/tutorial_utils/vision/cnn_visualization/gradcam.py#L209-L219
[ "def", "get_cam", "(", "imggrad", ",", "conv_out", ")", ":", "weights", "=", "np", ".", "mean", "(", "imggrad", ",", "axis", "=", "(", "1", ",", "2", ")", ")", "cam", "=", "np", ".", "ones", "(", "conv_out", ".", "shape", "[", "1", ":", "]", ...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
get_img_heatmap
Draw a heatmap on top of the original image using intensities from activation_map
docs/tutorial_utils/vision/cnn_visualization/gradcam.py
def get_img_heatmap(orig_img, activation_map): """Draw a heatmap on top of the original image using intensities from activation_map""" heatmap = cv2.applyColorMap(activation_map, cv2.COLORMAP_COOL) heatmap = cv2.cvtColor(heatmap, cv2.COLOR_BGR2RGB) img_heatmap = np.float32(heatmap) + np.float32(orig_img) img_heatmap = img_heatmap / np.max(img_heatmap) img_heatmap *= 255 return img_heatmap.astype(int)
def get_img_heatmap(orig_img, activation_map): """Draw a heatmap on top of the original image using intensities from activation_map""" heatmap = cv2.applyColorMap(activation_map, cv2.COLORMAP_COOL) heatmap = cv2.cvtColor(heatmap, cv2.COLOR_BGR2RGB) img_heatmap = np.float32(heatmap) + np.float32(orig_img) img_heatmap = img_heatmap / np.max(img_heatmap) img_heatmap *= 255 return img_heatmap.astype(int)
[ "Draw", "a", "heatmap", "on", "top", "of", "the", "original", "image", "using", "intensities", "from", "activation_map" ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/docs/tutorial_utils/vision/cnn_visualization/gradcam.py#L225-L232
[ "def", "get_img_heatmap", "(", "orig_img", ",", "activation_map", ")", ":", "heatmap", "=", "cv2", ".", "applyColorMap", "(", "activation_map", ",", "cv2", ".", "COLORMAP_COOL", ")", "heatmap", "=", "cv2", ".", "cvtColor", "(", "heatmap", ",", "cv2", ".", ...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
to_grayscale
Convert gradients to grayscale. This gives a saliency map.
docs/tutorial_utils/vision/cnn_visualization/gradcam.py
def to_grayscale(cv2im): """Convert gradients to grayscale. This gives a saliency map.""" # How strongly does each position activate the output grayscale_im = np.sum(np.abs(cv2im), axis=0) # Normalize between min and 99th percentile im_max = np.percentile(grayscale_im, 99) im_min = np.min(grayscale_im) grayscale_im = np.clip((grayscale_im - im_min) / (im_max - im_min), 0, 1) grayscale_im = np.expand_dims(grayscale_im, axis=0) return grayscale_im
def to_grayscale(cv2im): """Convert gradients to grayscale. This gives a saliency map.""" # How strongly does each position activate the output grayscale_im = np.sum(np.abs(cv2im), axis=0) # Normalize between min and 99th percentile im_max = np.percentile(grayscale_im, 99) im_min = np.min(grayscale_im) grayscale_im = np.clip((grayscale_im - im_min) / (im_max - im_min), 0, 1) grayscale_im = np.expand_dims(grayscale_im, axis=0) return grayscale_im
[ "Convert", "gradients", "to", "grayscale", ".", "This", "gives", "a", "saliency", "map", "." ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/docs/tutorial_utils/vision/cnn_visualization/gradcam.py#L234-L245
[ "def", "to_grayscale", "(", "cv2im", ")", ":", "# How strongly does each position activate the output", "grayscale_im", "=", "np", ".", "sum", "(", "np", ".", "abs", "(", "cv2im", ")", ",", "axis", "=", "0", ")", "# Normalize between min and 99th percentile", "im_ma...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
check_label_shapes
Helper function for checking shape of label and prediction Parameters ---------- labels : list of `NDArray` The labels of the data. preds : list of `NDArray` Predicted values. wrap : boolean If True, wrap labels/preds in a list if they are single NDArray shape : boolean If True, check the shape of labels and preds; Otherwise only check their length.
python/mxnet/metric.py
def check_label_shapes(labels, preds, wrap=False, shape=False): """Helper function for checking shape of label and prediction Parameters ---------- labels : list of `NDArray` The labels of the data. preds : list of `NDArray` Predicted values. wrap : boolean If True, wrap labels/preds in a list if they are single NDArray shape : boolean If True, check the shape of labels and preds; Otherwise only check their length. """ if not shape: label_shape, pred_shape = len(labels), len(preds) else: label_shape, pred_shape = labels.shape, preds.shape if label_shape != pred_shape: raise ValueError("Shape of labels {} does not match shape of " "predictions {}".format(label_shape, pred_shape)) if wrap: if isinstance(labels, ndarray.ndarray.NDArray): labels = [labels] if isinstance(preds, ndarray.ndarray.NDArray): preds = [preds] return labels, preds
def check_label_shapes(labels, preds, wrap=False, shape=False): """Helper function for checking shape of label and prediction Parameters ---------- labels : list of `NDArray` The labels of the data. preds : list of `NDArray` Predicted values. wrap : boolean If True, wrap labels/preds in a list if they are single NDArray shape : boolean If True, check the shape of labels and preds; Otherwise only check their length. """ if not shape: label_shape, pred_shape = len(labels), len(preds) else: label_shape, pred_shape = labels.shape, preds.shape if label_shape != pred_shape: raise ValueError("Shape of labels {} does not match shape of " "predictions {}".format(label_shape, pred_shape)) if wrap: if isinstance(labels, ndarray.ndarray.NDArray): labels = [labels] if isinstance(preds, ndarray.ndarray.NDArray): preds = [preds] return labels, preds
[ "Helper", "function", "for", "checking", "shape", "of", "label", "and", "prediction" ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/metric.py#L33-L66
[ "def", "check_label_shapes", "(", "labels", ",", "preds", ",", "wrap", "=", "False", ",", "shape", "=", "False", ")", ":", "if", "not", "shape", ":", "label_shape", ",", "pred_shape", "=", "len", "(", "labels", ")", ",", "len", "(", "preds", ")", "el...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
create
Creates evaluation metric from metric names or instances of EvalMetric or a custom metric function. Parameters ---------- metric : str or callable Specifies the metric to create. This argument must be one of the below: - Name of a metric. - An instance of `EvalMetric`. - A list, each element of which is a metric or a metric name. - An evaluation function that computes custom metric for a given batch of labels and predictions. *args : list Additional arguments to metric constructor. Only used when metric is str. **kwargs : dict Additional arguments to metric constructor. Only used when metric is str Examples -------- >>> def custom_metric(label, pred): ... return np.mean(np.abs(label - pred)) ... >>> metric1 = mx.metric.create('acc') >>> metric2 = mx.metric.create(custom_metric) >>> metric3 = mx.metric.create([metric1, metric2, 'rmse'])
python/mxnet/metric.py
def create(metric, *args, **kwargs): """Creates evaluation metric from metric names or instances of EvalMetric or a custom metric function. Parameters ---------- metric : str or callable Specifies the metric to create. This argument must be one of the below: - Name of a metric. - An instance of `EvalMetric`. - A list, each element of which is a metric or a metric name. - An evaluation function that computes custom metric for a given batch of labels and predictions. *args : list Additional arguments to metric constructor. Only used when metric is str. **kwargs : dict Additional arguments to metric constructor. Only used when metric is str Examples -------- >>> def custom_metric(label, pred): ... return np.mean(np.abs(label - pred)) ... >>> metric1 = mx.metric.create('acc') >>> metric2 = mx.metric.create(custom_metric) >>> metric3 = mx.metric.create([metric1, metric2, 'rmse']) """ if callable(metric): return CustomMetric(metric, *args, **kwargs) elif isinstance(metric, list): composite_metric = CompositeEvalMetric() for child_metric in metric: composite_metric.add(create(child_metric, *args, **kwargs)) return composite_metric return _create(metric, *args, **kwargs)
def create(metric, *args, **kwargs): """Creates evaluation metric from metric names or instances of EvalMetric or a custom metric function. Parameters ---------- metric : str or callable Specifies the metric to create. This argument must be one of the below: - Name of a metric. - An instance of `EvalMetric`. - A list, each element of which is a metric or a metric name. - An evaluation function that computes custom metric for a given batch of labels and predictions. *args : list Additional arguments to metric constructor. Only used when metric is str. **kwargs : dict Additional arguments to metric constructor. Only used when metric is str Examples -------- >>> def custom_metric(label, pred): ... return np.mean(np.abs(label - pred)) ... >>> metric1 = mx.metric.create('acc') >>> metric2 = mx.metric.create(custom_metric) >>> metric3 = mx.metric.create([metric1, metric2, 'rmse']) """ if callable(metric): return CustomMetric(metric, *args, **kwargs) elif isinstance(metric, list): composite_metric = CompositeEvalMetric() for child_metric in metric: composite_metric.add(create(child_metric, *args, **kwargs)) return composite_metric return _create(metric, *args, **kwargs)
[ "Creates", "evaluation", "metric", "from", "metric", "names", "or", "instances", "of", "EvalMetric", "or", "a", "custom", "metric", "function", "." ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/metric.py#L234-L273
[ "def", "create", "(", "metric", ",", "*", "args", ",", "*", "*", "kwargs", ")", ":", "if", "callable", "(", "metric", ")", ":", "return", "CustomMetric", "(", "metric", ",", "*", "args", ",", "*", "*", "kwargs", ")", "elif", "isinstance", "(", "met...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
np
Creates a custom evaluation metric that receives its inputs as numpy arrays. Parameters ---------- numpy_feval : callable(label, pred) Custom evaluation function that receives labels and predictions for a minibatch as numpy arrays and returns the corresponding custom metric as a floating point number. name : str, optional Name of the custom metric. allow_extra_outputs : bool, optional Whether prediction output is allowed to have extra outputs. This is useful in cases like RNN where states are also part of output which can then be fed back to the RNN in the next step. By default, extra outputs are not allowed. Returns ------- float Custom metric corresponding to the provided labels and predictions. Example ------- >>> def custom_metric(label, pred): ... return np.mean(np.abs(label-pred)) ... >>> metric = mx.metric.np(custom_metric)
python/mxnet/metric.py
def np(numpy_feval, name=None, allow_extra_outputs=False): """Creates a custom evaluation metric that receives its inputs as numpy arrays. Parameters ---------- numpy_feval : callable(label, pred) Custom evaluation function that receives labels and predictions for a minibatch as numpy arrays and returns the corresponding custom metric as a floating point number. name : str, optional Name of the custom metric. allow_extra_outputs : bool, optional Whether prediction output is allowed to have extra outputs. This is useful in cases like RNN where states are also part of output which can then be fed back to the RNN in the next step. By default, extra outputs are not allowed. Returns ------- float Custom metric corresponding to the provided labels and predictions. Example ------- >>> def custom_metric(label, pred): ... return np.mean(np.abs(label-pred)) ... >>> metric = mx.metric.np(custom_metric) """ def feval(label, pred): """Internal eval function.""" return numpy_feval(label, pred) feval.__name__ = numpy_feval.__name__ return CustomMetric(feval, name, allow_extra_outputs)
def np(numpy_feval, name=None, allow_extra_outputs=False): """Creates a custom evaluation metric that receives its inputs as numpy arrays. Parameters ---------- numpy_feval : callable(label, pred) Custom evaluation function that receives labels and predictions for a minibatch as numpy arrays and returns the corresponding custom metric as a floating point number. name : str, optional Name of the custom metric. allow_extra_outputs : bool, optional Whether prediction output is allowed to have extra outputs. This is useful in cases like RNN where states are also part of output which can then be fed back to the RNN in the next step. By default, extra outputs are not allowed. Returns ------- float Custom metric corresponding to the provided labels and predictions. Example ------- >>> def custom_metric(label, pred): ... return np.mean(np.abs(label-pred)) ... >>> metric = mx.metric.np(custom_metric) """ def feval(label, pred): """Internal eval function.""" return numpy_feval(label, pred) feval.__name__ = numpy_feval.__name__ return CustomMetric(feval, name, allow_extra_outputs)
[ "Creates", "a", "custom", "evaluation", "metric", "that", "receives", "its", "inputs", "as", "numpy", "arrays", "." ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/metric.py#L1747-L1778
[ "def", "np", "(", "numpy_feval", ",", "name", "=", "None", ",", "allow_extra_outputs", "=", "False", ")", ":", "def", "feval", "(", "label", ",", "pred", ")", ":", "\"\"\"Internal eval function.\"\"\"", "return", "numpy_feval", "(", "label", ",", "pred", ")"...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
EvalMetric.get_config
Save configurations of metric. Can be recreated from configs with metric.create(``**config``)
python/mxnet/metric.py
def get_config(self): """Save configurations of metric. Can be recreated from configs with metric.create(``**config``) """ config = self._kwargs.copy() config.update({ 'metric': self.__class__.__name__, 'name': self.name, 'output_names': self.output_names, 'label_names': self.label_names}) return config
def get_config(self): """Save configurations of metric. Can be recreated from configs with metric.create(``**config``) """ config = self._kwargs.copy() config.update({ 'metric': self.__class__.__name__, 'name': self.name, 'output_names': self.output_names, 'label_names': self.label_names}) return config
[ "Save", "configurations", "of", "metric", ".", "Can", "be", "recreated", "from", "configs", "with", "metric", ".", "create", "(", "**", "config", ")" ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/metric.py#L100-L110
[ "def", "get_config", "(", "self", ")", ":", "config", "=", "self", ".", "_kwargs", ".", "copy", "(", ")", "config", ".", "update", "(", "{", "'metric'", ":", "self", ".", "__class__", ".", "__name__", ",", "'name'", ":", "self", ".", "name", ",", "...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
EvalMetric.update_dict
Update the internal evaluation with named label and pred Parameters ---------- labels : OrderedDict of str -> NDArray name to array mapping for labels. preds : OrderedDict of str -> NDArray name to array mapping of predicted outputs.
python/mxnet/metric.py
def update_dict(self, label, pred): """Update the internal evaluation with named label and pred Parameters ---------- labels : OrderedDict of str -> NDArray name to array mapping for labels. preds : OrderedDict of str -> NDArray name to array mapping of predicted outputs. """ if self.output_names is not None: pred = [pred[name] for name in self.output_names] else: pred = list(pred.values()) if self.label_names is not None: label = [label[name] for name in self.label_names] else: label = list(label.values()) self.update(label, pred)
def update_dict(self, label, pred): """Update the internal evaluation with named label and pred Parameters ---------- labels : OrderedDict of str -> NDArray name to array mapping for labels. preds : OrderedDict of str -> NDArray name to array mapping of predicted outputs. """ if self.output_names is not None: pred = [pred[name] for name in self.output_names] else: pred = list(pred.values()) if self.label_names is not None: label = [label[name] for name in self.label_names] else: label = list(label.values()) self.update(label, pred)
[ "Update", "the", "internal", "evaluation", "with", "named", "label", "and", "pred" ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/metric.py#L112-L133
[ "def", "update_dict", "(", "self", ",", "label", ",", "pred", ")", ":", "if", "self", ".", "output_names", "is", "not", "None", ":", "pred", "=", "[", "pred", "[", "name", "]", "for", "name", "in", "self", ".", "output_names", "]", "else", ":", "pr...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
EvalMetric.reset
Resets the internal evaluation result to initial state.
python/mxnet/metric.py
def reset(self): """Resets the internal evaluation result to initial state.""" self.num_inst = 0 self.sum_metric = 0.0 self.global_num_inst = 0 self.global_sum_metric = 0.0
def reset(self): """Resets the internal evaluation result to initial state.""" self.num_inst = 0 self.sum_metric = 0.0 self.global_num_inst = 0 self.global_sum_metric = 0.0
[ "Resets", "the", "internal", "evaluation", "result", "to", "initial", "state", "." ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/metric.py#L148-L153
[ "def", "reset", "(", "self", ")", ":", "self", ".", "num_inst", "=", "0", "self", ".", "sum_metric", "=", "0.0", "self", ".", "global_num_inst", "=", "0", "self", ".", "global_sum_metric", "=", "0.0" ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
EvalMetric.get
Gets the current evaluation result. Returns ------- names : list of str Name of the metrics. values : list of float Value of the evaluations.
python/mxnet/metric.py
def get(self): """Gets the current evaluation result. Returns ------- names : list of str Name of the metrics. values : list of float Value of the evaluations. """ if self.num_inst == 0: return (self.name, float('nan')) else: return (self.name, self.sum_metric / self.num_inst)
def get(self): """Gets the current evaluation result. Returns ------- names : list of str Name of the metrics. values : list of float Value of the evaluations. """ if self.num_inst == 0: return (self.name, float('nan')) else: return (self.name, self.sum_metric / self.num_inst)
[ "Gets", "the", "current", "evaluation", "result", "." ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/metric.py#L161-L174
[ "def", "get", "(", "self", ")", ":", "if", "self", ".", "num_inst", "==", "0", ":", "return", "(", "self", ".", "name", ",", "float", "(", "'nan'", ")", ")", "else", ":", "return", "(", "self", ".", "name", ",", "self", ".", "sum_metric", "/", ...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
EvalMetric.get_global
Gets the current global evaluation result. Returns ------- names : list of str Name of the metrics. values : list of float Value of the evaluations.
python/mxnet/metric.py
def get_global(self): """Gets the current global evaluation result. Returns ------- names : list of str Name of the metrics. values : list of float Value of the evaluations. """ if self._has_global_stats: if self.global_num_inst == 0: return (self.name, float('nan')) else: return (self.name, self.global_sum_metric / self.global_num_inst) else: return self.get()
def get_global(self): """Gets the current global evaluation result. Returns ------- names : list of str Name of the metrics. values : list of float Value of the evaluations. """ if self._has_global_stats: if self.global_num_inst == 0: return (self.name, float('nan')) else: return (self.name, self.global_sum_metric / self.global_num_inst) else: return self.get()
[ "Gets", "the", "current", "global", "evaluation", "result", "." ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/metric.py#L176-L192
[ "def", "get_global", "(", "self", ")", ":", "if", "self", ".", "_has_global_stats", ":", "if", "self", ".", "global_num_inst", "==", "0", ":", "return", "(", "self", ".", "name", ",", "float", "(", "'nan'", ")", ")", "else", ":", "return", "(", "self...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
EvalMetric.get_name_value
Returns zipped name and value pairs. Returns ------- list of tuples A (name, value) tuple list.
python/mxnet/metric.py
def get_name_value(self): """Returns zipped name and value pairs. Returns ------- list of tuples A (name, value) tuple list. """ name, value = self.get() if not isinstance(name, list): name = [name] if not isinstance(value, list): value = [value] return list(zip(name, value))
def get_name_value(self): """Returns zipped name and value pairs. Returns ------- list of tuples A (name, value) tuple list. """ name, value = self.get() if not isinstance(name, list): name = [name] if not isinstance(value, list): value = [value] return list(zip(name, value))
[ "Returns", "zipped", "name", "and", "value", "pairs", "." ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/metric.py#L194-L207
[ "def", "get_name_value", "(", "self", ")", ":", "name", ",", "value", "=", "self", ".", "get", "(", ")", "if", "not", "isinstance", "(", "name", ",", "list", ")", ":", "name", "=", "[", "name", "]", "if", "not", "isinstance", "(", "value", ",", "...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
EvalMetric.get_global_name_value
Returns zipped name and value pairs for global results. Returns ------- list of tuples A (name, value) tuple list.
python/mxnet/metric.py
def get_global_name_value(self): """Returns zipped name and value pairs for global results. Returns ------- list of tuples A (name, value) tuple list. """ if self._has_global_stats: name, value = self.get_global() if not isinstance(name, list): name = [name] if not isinstance(value, list): value = [value] return list(zip(name, value)) else: return self.get_name_value()
def get_global_name_value(self): """Returns zipped name and value pairs for global results. Returns ------- list of tuples A (name, value) tuple list. """ if self._has_global_stats: name, value = self.get_global() if not isinstance(name, list): name = [name] if not isinstance(value, list): value = [value] return list(zip(name, value)) else: return self.get_name_value()
[ "Returns", "zipped", "name", "and", "value", "pairs", "for", "global", "results", "." ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/metric.py#L209-L225
[ "def", "get_global_name_value", "(", "self", ")", ":", "if", "self", ".", "_has_global_stats", ":", "name", ",", "value", "=", "self", ".", "get_global", "(", ")", "if", "not", "isinstance", "(", "name", ",", "list", ")", ":", "name", "=", "[", "name",...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
_BinaryClassificationMetrics.update_binary_stats
Update various binary classification counts for a single (label, pred) pair. Parameters ---------- label : `NDArray` The labels of the data. pred : `NDArray` Predicted values.
python/mxnet/metric.py
def update_binary_stats(self, label, pred): """ Update various binary classification counts for a single (label, pred) pair. Parameters ---------- label : `NDArray` The labels of the data. pred : `NDArray` Predicted values. """ pred = pred.asnumpy() label = label.asnumpy().astype('int32') pred_label = numpy.argmax(pred, axis=1) check_label_shapes(label, pred) if len(numpy.unique(label)) > 2: raise ValueError("%s currently only supports binary classification." % self.__class__.__name__) pred_true = (pred_label == 1) pred_false = 1 - pred_true label_true = (label == 1) label_false = 1 - label_true true_pos = (pred_true * label_true).sum() false_pos = (pred_true * label_false).sum() false_neg = (pred_false * label_true).sum() true_neg = (pred_false * label_false).sum() self.true_positives += true_pos self.global_true_positives += true_pos self.false_positives += false_pos self.global_false_positives += false_pos self.false_negatives += false_neg self.global_false_negatives += false_neg self.true_negatives += true_neg self.global_true_negatives += true_neg
def update_binary_stats(self, label, pred): """ Update various binary classification counts for a single (label, pred) pair. Parameters ---------- label : `NDArray` The labels of the data. pred : `NDArray` Predicted values. """ pred = pred.asnumpy() label = label.asnumpy().astype('int32') pred_label = numpy.argmax(pred, axis=1) check_label_shapes(label, pred) if len(numpy.unique(label)) > 2: raise ValueError("%s currently only supports binary classification." % self.__class__.__name__) pred_true = (pred_label == 1) pred_false = 1 - pred_true label_true = (label == 1) label_false = 1 - label_true true_pos = (pred_true * label_true).sum() false_pos = (pred_true * label_false).sum() false_neg = (pred_false * label_true).sum() true_neg = (pred_false * label_false).sum() self.true_positives += true_pos self.global_true_positives += true_pos self.false_positives += false_pos self.global_false_positives += false_pos self.false_negatives += false_neg self.global_false_negatives += false_neg self.true_negatives += true_neg self.global_true_negatives += true_neg
[ "Update", "various", "binary", "classification", "counts", "for", "a", "single", "(", "label", "pred", ")", "pair", "." ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/metric.py#L612-L649
[ "def", "update_binary_stats", "(", "self", ",", "label", ",", "pred", ")", ":", "pred", "=", "pred", ".", "asnumpy", "(", ")", "label", "=", "label", ".", "asnumpy", "(", ")", ".", "astype", "(", "'int32'", ")", "pred_label", "=", "numpy", ".", "argm...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
_BinaryClassificationMetrics.matthewscc
Calculate the Matthew's Correlation Coefficent
python/mxnet/metric.py
def matthewscc(self, use_global=False): """ Calculate the Matthew's Correlation Coefficent """ if use_global: if not self.global_total_examples: return 0. true_pos = float(self.global_true_positives) false_pos = float(self.global_false_positives) false_neg = float(self.global_false_negatives) true_neg = float(self.global_true_negatives) else: if not self.total_examples: return 0. true_pos = float(self.true_positives) false_pos = float(self.false_positives) false_neg = float(self.false_negatives) true_neg = float(self.true_negatives) terms = [(true_pos + false_pos), (true_pos + false_neg), (true_neg + false_pos), (true_neg + false_neg)] denom = 1. for t in filter(lambda t: t != 0., terms): denom *= t return ((true_pos * true_neg) - (false_pos * false_neg)) / math.sqrt(denom)
def matthewscc(self, use_global=False): """ Calculate the Matthew's Correlation Coefficent """ if use_global: if not self.global_total_examples: return 0. true_pos = float(self.global_true_positives) false_pos = float(self.global_false_positives) false_neg = float(self.global_false_negatives) true_neg = float(self.global_true_negatives) else: if not self.total_examples: return 0. true_pos = float(self.true_positives) false_pos = float(self.false_positives) false_neg = float(self.false_negatives) true_neg = float(self.true_negatives) terms = [(true_pos + false_pos), (true_pos + false_neg), (true_neg + false_pos), (true_neg + false_neg)] denom = 1. for t in filter(lambda t: t != 0., terms): denom *= t return ((true_pos * true_neg) - (false_pos * false_neg)) / math.sqrt(denom)
[ "Calculate", "the", "Matthew", "s", "Correlation", "Coefficent" ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/metric.py#L693-L721
[ "def", "matthewscc", "(", "self", ",", "use_global", "=", "False", ")", ":", "if", "use_global", ":", "if", "not", "self", ".", "global_total_examples", ":", "return", "0.", "true_pos", "=", "float", "(", "self", ".", "global_true_positives", ")", "false_pos...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
Dataset.transform
Returns a new dataset with each sample transformed by the transformer function `fn`. Parameters ---------- fn : callable A transformer function that takes a sample as input and returns the transformed sample. lazy : bool, default True If False, transforms all samples at once. Otherwise, transforms each sample on demand. Note that if `fn` is stochastic, you must set lazy to True or you will get the same result on all epochs. Returns ------- Dataset The transformed dataset.
python/mxnet/gluon/data/dataset.py
def transform(self, fn, lazy=True): """Returns a new dataset with each sample transformed by the transformer function `fn`. Parameters ---------- fn : callable A transformer function that takes a sample as input and returns the transformed sample. lazy : bool, default True If False, transforms all samples at once. Otherwise, transforms each sample on demand. Note that if `fn` is stochastic, you must set lazy to True or you will get the same result on all epochs. Returns ------- Dataset The transformed dataset. """ trans = _LazyTransformDataset(self, fn) if lazy: return trans return SimpleDataset([i for i in trans])
def transform(self, fn, lazy=True): """Returns a new dataset with each sample transformed by the transformer function `fn`. Parameters ---------- fn : callable A transformer function that takes a sample as input and returns the transformed sample. lazy : bool, default True If False, transforms all samples at once. Otherwise, transforms each sample on demand. Note that if `fn` is stochastic, you must set lazy to True or you will get the same result on all epochs. Returns ------- Dataset The transformed dataset. """ trans = _LazyTransformDataset(self, fn) if lazy: return trans return SimpleDataset([i for i in trans])
[ "Returns", "a", "new", "dataset", "with", "each", "sample", "transformed", "by", "the", "transformer", "function", "fn", "." ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/gluon/data/dataset.py#L43-L66
[ "def", "transform", "(", "self", ",", "fn", ",", "lazy", "=", "True", ")", ":", "trans", "=", "_LazyTransformDataset", "(", "self", ",", "fn", ")", "if", "lazy", ":", "return", "trans", "return", "SimpleDataset", "(", "[", "i", "for", "i", "in", "tra...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
Dataset.transform_first
Returns a new dataset with the first element of each sample transformed by the transformer function `fn`. This is useful, for example, when you only want to transform data while keeping label as is. Parameters ---------- fn : callable A transformer function that takes the first elemtn of a sample as input and returns the transformed element. lazy : bool, default True If False, transforms all samples at once. Otherwise, transforms each sample on demand. Note that if `fn` is stochastic, you must set lazy to True or you will get the same result on all epochs. Returns ------- Dataset The transformed dataset.
python/mxnet/gluon/data/dataset.py
def transform_first(self, fn, lazy=True): """Returns a new dataset with the first element of each sample transformed by the transformer function `fn`. This is useful, for example, when you only want to transform data while keeping label as is. Parameters ---------- fn : callable A transformer function that takes the first elemtn of a sample as input and returns the transformed element. lazy : bool, default True If False, transforms all samples at once. Otherwise, transforms each sample on demand. Note that if `fn` is stochastic, you must set lazy to True or you will get the same result on all epochs. Returns ------- Dataset The transformed dataset. """ return self.transform(_TransformFirstClosure(fn), lazy)
def transform_first(self, fn, lazy=True): """Returns a new dataset with the first element of each sample transformed by the transformer function `fn`. This is useful, for example, when you only want to transform data while keeping label as is. Parameters ---------- fn : callable A transformer function that takes the first elemtn of a sample as input and returns the transformed element. lazy : bool, default True If False, transforms all samples at once. Otherwise, transforms each sample on demand. Note that if `fn` is stochastic, you must set lazy to True or you will get the same result on all epochs. Returns ------- Dataset The transformed dataset. """ return self.transform(_TransformFirstClosure(fn), lazy)
[ "Returns", "a", "new", "dataset", "with", "the", "first", "element", "of", "each", "sample", "transformed", "by", "the", "transformer", "function", "fn", "." ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/gluon/data/dataset.py#L68-L91
[ "def", "transform_first", "(", "self", ",", "fn", ",", "lazy", "=", "True", ")", ":", "return", "self", ".", "transform", "(", "_TransformFirstClosure", "(", "fn", ")", ",", "lazy", ")" ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
lstm_ocr_model.forward_ocr
Forward the image through the LSTM network model Parameters ---------- img_: int of array Returns ---------- label_list: string of list
example/ctc/ocr_predict.py
def forward_ocr(self, img_): """Forward the image through the LSTM network model Parameters ---------- img_: int of array Returns ---------- label_list: string of list """ img_ = cv2.resize(img_, (80, 30)) img_ = img_.transpose(1, 0) print(img_.shape) img_ = img_.reshape((1, 80, 30)) print(img_.shape) # img_ = img_.reshape((80 * 30)) img_ = np.multiply(img_, 1 / 255.0) self.predictor.forward(data=img_, **self.init_state_dict) prob = self.predictor.get_output(0) label_list = [] for p in prob: print(np.argsort(p)) max_index = np.argsort(p)[::-1][0] label_list.append(max_index) return self.__get_string(label_list)
def forward_ocr(self, img_): """Forward the image through the LSTM network model Parameters ---------- img_: int of array Returns ---------- label_list: string of list """ img_ = cv2.resize(img_, (80, 30)) img_ = img_.transpose(1, 0) print(img_.shape) img_ = img_.reshape((1, 80, 30)) print(img_.shape) # img_ = img_.reshape((80 * 30)) img_ = np.multiply(img_, 1 / 255.0) self.predictor.forward(data=img_, **self.init_state_dict) prob = self.predictor.get_output(0) label_list = [] for p in prob: print(np.argsort(p)) max_index = np.argsort(p)[::-1][0] label_list.append(max_index) return self.__get_string(label_list)
[ "Forward", "the", "image", "through", "the", "LSTM", "network", "model" ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/example/ctc/ocr_predict.py#L63-L88
[ "def", "forward_ocr", "(", "self", ",", "img_", ")", ":", "img_", "=", "cv2", ".", "resize", "(", "img_", ",", "(", "80", ",", "30", ")", ")", "img_", "=", "img_", ".", "transpose", "(", "1", ",", "0", ")", "print", "(", "img_", ".", "shape", ...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
read_prototxt
Return a caffe_pb2.NetParameter object that defined in a prototxt file
tools/caffe_converter/caffe_parser.py
def read_prototxt(fname): """Return a caffe_pb2.NetParameter object that defined in a prototxt file """ proto = caffe_pb2.NetParameter() with open(fname, 'r') as f: text_format.Merge(str(f.read()), proto) return proto
def read_prototxt(fname): """Return a caffe_pb2.NetParameter object that defined in a prototxt file """ proto = caffe_pb2.NetParameter() with open(fname, 'r') as f: text_format.Merge(str(f.read()), proto) return proto
[ "Return", "a", "caffe_pb2", ".", "NetParameter", "object", "that", "defined", "in", "a", "prototxt", "file" ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/tools/caffe_converter/caffe_parser.py#L34-L40
[ "def", "read_prototxt", "(", "fname", ")", ":", "proto", "=", "caffe_pb2", ".", "NetParameter", "(", ")", "with", "open", "(", "fname", ",", "'r'", ")", "as", "f", ":", "text_format", ".", "Merge", "(", "str", "(", "f", ".", "read", "(", ")", ")", ...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
get_layers
Returns layers in a caffe_pb2.NetParameter object
tools/caffe_converter/caffe_parser.py
def get_layers(proto): """Returns layers in a caffe_pb2.NetParameter object """ if len(proto.layer): return proto.layer elif len(proto.layers): return proto.layers else: raise ValueError('Invalid proto file.')
def get_layers(proto): """Returns layers in a caffe_pb2.NetParameter object """ if len(proto.layer): return proto.layer elif len(proto.layers): return proto.layers else: raise ValueError('Invalid proto file.')
[ "Returns", "layers", "in", "a", "caffe_pb2", ".", "NetParameter", "object" ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/tools/caffe_converter/caffe_parser.py#L42-L50
[ "def", "get_layers", "(", "proto", ")", ":", "if", "len", "(", "proto", ".", "layer", ")", ":", "return", "proto", ".", "layer", "elif", "len", "(", "proto", ".", "layers", ")", ":", "return", "proto", ".", "layers", "else", ":", "raise", "ValueError...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
read_caffemodel
Return a caffe_pb2.NetParameter object that defined in a binary caffemodel file
tools/caffe_converter/caffe_parser.py
def read_caffemodel(prototxt_fname, caffemodel_fname): """Return a caffe_pb2.NetParameter object that defined in a binary caffemodel file """ if use_caffe: caffe.set_mode_cpu() net = caffe.Net(prototxt_fname, caffemodel_fname, caffe.TEST) layer_names = net._layer_names layers = net.layers return (layers, layer_names) else: proto = caffe_pb2.NetParameter() with open(caffemodel_fname, 'rb') as f: proto.ParseFromString(f.read()) return (get_layers(proto), None)
def read_caffemodel(prototxt_fname, caffemodel_fname): """Return a caffe_pb2.NetParameter object that defined in a binary caffemodel file """ if use_caffe: caffe.set_mode_cpu() net = caffe.Net(prototxt_fname, caffemodel_fname, caffe.TEST) layer_names = net._layer_names layers = net.layers return (layers, layer_names) else: proto = caffe_pb2.NetParameter() with open(caffemodel_fname, 'rb') as f: proto.ParseFromString(f.read()) return (get_layers(proto), None)
[ "Return", "a", "caffe_pb2", ".", "NetParameter", "object", "that", "defined", "in", "a", "binary", "caffemodel", "file" ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/tools/caffe_converter/caffe_parser.py#L52-L66
[ "def", "read_caffemodel", "(", "prototxt_fname", ",", "caffemodel_fname", ")", ":", "if", "use_caffe", ":", "caffe", ".", "set_mode_cpu", "(", ")", "net", "=", "caffe", ".", "Net", "(", "prototxt_fname", ",", "caffemodel_fname", ",", "caffe", ".", "TEST", ")...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
layer_iter
Iterate over all layers
tools/caffe_converter/caffe_parser.py
def layer_iter(layers, layer_names): """Iterate over all layers""" if use_caffe: for layer_idx, layer in enumerate(layers): layer_name = re.sub('[-/]', '_', layer_names[layer_idx]) layer_type = layer.type layer_blobs = layer.blobs yield (layer_name, layer_type, layer_blobs) else: for layer in layers: layer_name = re.sub('[-/]', '_', layer.name) layer_type = layer.type layer_blobs = layer.blobs yield (layer_name, layer_type, layer_blobs)
def layer_iter(layers, layer_names): """Iterate over all layers""" if use_caffe: for layer_idx, layer in enumerate(layers): layer_name = re.sub('[-/]', '_', layer_names[layer_idx]) layer_type = layer.type layer_blobs = layer.blobs yield (layer_name, layer_type, layer_blobs) else: for layer in layers: layer_name = re.sub('[-/]', '_', layer.name) layer_type = layer.type layer_blobs = layer.blobs yield (layer_name, layer_type, layer_blobs)
[ "Iterate", "over", "all", "layers" ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/tools/caffe_converter/caffe_parser.py#L68-L81
[ "def", "layer_iter", "(", "layers", ",", "layer_names", ")", ":", "if", "use_caffe", ":", "for", "layer_idx", ",", "layer", "in", "enumerate", "(", "layers", ")", ":", "layer_name", "=", "re", ".", "sub", "(", "'[-/]'", ",", "'_'", ",", "layer_names", ...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
set_config
Set up the configure of profiler (only accepts keyword arguments). Parameters ---------- filename : string, output file for profile data profile_all : boolean, all profile types enabled profile_symbolic : boolean, whether to profile symbolic operators profile_imperative : boolean, whether to profile imperative operators profile_memory : boolean, whether to profile memory usage profile_api : boolean, whether to profile the C API contiguous_dump : boolean, whether to periodically dump profiling data to file dump_period : float, seconds between profile data dumps aggregate_stats : boolean, whether to maintain aggregate stats in memory for console dump. Has some negative performance impact. profile_process : string whether to profile kvstore `server` or `worker`. server can only be profiled when kvstore is of type dist. if this is not passed, defaults to `worker`
python/mxnet/profiler.py
def set_config(**kwargs): """Set up the configure of profiler (only accepts keyword arguments). Parameters ---------- filename : string, output file for profile data profile_all : boolean, all profile types enabled profile_symbolic : boolean, whether to profile symbolic operators profile_imperative : boolean, whether to profile imperative operators profile_memory : boolean, whether to profile memory usage profile_api : boolean, whether to profile the C API contiguous_dump : boolean, whether to periodically dump profiling data to file dump_period : float, seconds between profile data dumps aggregate_stats : boolean, whether to maintain aggregate stats in memory for console dump. Has some negative performance impact. profile_process : string whether to profile kvstore `server` or `worker`. server can only be profiled when kvstore is of type dist. if this is not passed, defaults to `worker` """ kk = kwargs.keys() vv = kwargs.values() check_call(_LIB.MXSetProcessProfilerConfig(len(kwargs), c_str_array([key for key in kk]), c_str_array([str(val) for val in vv]), profiler_kvstore_handle))
def set_config(**kwargs): """Set up the configure of profiler (only accepts keyword arguments). Parameters ---------- filename : string, output file for profile data profile_all : boolean, all profile types enabled profile_symbolic : boolean, whether to profile symbolic operators profile_imperative : boolean, whether to profile imperative operators profile_memory : boolean, whether to profile memory usage profile_api : boolean, whether to profile the C API contiguous_dump : boolean, whether to periodically dump profiling data to file dump_period : float, seconds between profile data dumps aggregate_stats : boolean, whether to maintain aggregate stats in memory for console dump. Has some negative performance impact. profile_process : string whether to profile kvstore `server` or `worker`. server can only be profiled when kvstore is of type dist. if this is not passed, defaults to `worker` """ kk = kwargs.keys() vv = kwargs.values() check_call(_LIB.MXSetProcessProfilerConfig(len(kwargs), c_str_array([key for key in kk]), c_str_array([str(val) for val in vv]), profiler_kvstore_handle))
[ "Set", "up", "the", "configure", "of", "profiler", "(", "only", "accepts", "keyword", "arguments", ")", "." ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/profiler.py#L33-L67
[ "def", "set_config", "(", "*", "*", "kwargs", ")", ":", "kk", "=", "kwargs", ".", "keys", "(", ")", "vv", "=", "kwargs", ".", "values", "(", ")", "check_call", "(", "_LIB", ".", "MXSetProcessProfilerConfig", "(", "len", "(", "kwargs", ")", ",", "c_st...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
profiler_set_config
Set up the configure of profiler (Deprecated). Parameters ---------- mode : string, optional Indicates whether to enable the profiler, can be 'symbolic', or 'all'. Defaults to `symbolic`. filename : string, optional The name of output trace file. Defaults to 'profile.json'.
python/mxnet/profiler.py
def profiler_set_config(mode='symbolic', filename='profile.json'): """Set up the configure of profiler (Deprecated). Parameters ---------- mode : string, optional Indicates whether to enable the profiler, can be 'symbolic', or 'all'. Defaults to `symbolic`. filename : string, optional The name of output trace file. Defaults to 'profile.json'. """ warnings.warn('profiler.profiler_set_config() is deprecated. ' 'Please use profiler.set_config() instead') keys = c_str_array([key for key in ["profile_" + mode, "filename"]]) values = c_str_array([str(val) for val in [True, filename]]) assert len(keys) == len(values) check_call(_LIB.MXSetProcessProfilerConfig(len(keys), keys, values, profiler_kvstore_handle))
def profiler_set_config(mode='symbolic', filename='profile.json'): """Set up the configure of profiler (Deprecated). Parameters ---------- mode : string, optional Indicates whether to enable the profiler, can be 'symbolic', or 'all'. Defaults to `symbolic`. filename : string, optional The name of output trace file. Defaults to 'profile.json'. """ warnings.warn('profiler.profiler_set_config() is deprecated. ' 'Please use profiler.set_config() instead') keys = c_str_array([key for key in ["profile_" + mode, "filename"]]) values = c_str_array([str(val) for val in [True, filename]]) assert len(keys) == len(values) check_call(_LIB.MXSetProcessProfilerConfig(len(keys), keys, values, profiler_kvstore_handle))
[ "Set", "up", "the", "configure", "of", "profiler", "(", "Deprecated", ")", "." ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/profiler.py#L70-L86
[ "def", "profiler_set_config", "(", "mode", "=", "'symbolic'", ",", "filename", "=", "'profile.json'", ")", ":", "warnings", ".", "warn", "(", "'profiler.profiler_set_config() is deprecated. '", "'Please use profiler.set_config() instead'", ")", "keys", "=", "c_str_array", ...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
set_state
Set up the profiler state to 'run' or 'stop'. Parameters ---------- state : string, optional Indicates whether to run the profiler, can be 'stop' or 'run'. Default is `stop`. profile_process : string whether to profile kvstore `server` or `worker`. server can only be profiled when kvstore is of type dist. if this is not passed, defaults to `worker`
python/mxnet/profiler.py
def set_state(state='stop', profile_process='worker'): """Set up the profiler state to 'run' or 'stop'. Parameters ---------- state : string, optional Indicates whether to run the profiler, can be 'stop' or 'run'. Default is `stop`. profile_process : string whether to profile kvstore `server` or `worker`. server can only be profiled when kvstore is of type dist. if this is not passed, defaults to `worker` """ state2int = {'stop': 0, 'run': 1} profile_process2int = {'worker': 0, 'server': 1} check_call(_LIB.MXSetProcessProfilerState(ctypes.c_int(state2int[state]), profile_process2int[profile_process], profiler_kvstore_handle))
def set_state(state='stop', profile_process='worker'): """Set up the profiler state to 'run' or 'stop'. Parameters ---------- state : string, optional Indicates whether to run the profiler, can be 'stop' or 'run'. Default is `stop`. profile_process : string whether to profile kvstore `server` or `worker`. server can only be profiled when kvstore is of type dist. if this is not passed, defaults to `worker` """ state2int = {'stop': 0, 'run': 1} profile_process2int = {'worker': 0, 'server': 1} check_call(_LIB.MXSetProcessProfilerState(ctypes.c_int(state2int[state]), profile_process2int[profile_process], profiler_kvstore_handle))
[ "Set", "up", "the", "profiler", "state", "to", "run", "or", "stop", "." ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/profiler.py#L89-L106
[ "def", "set_state", "(", "state", "=", "'stop'", ",", "profile_process", "=", "'worker'", ")", ":", "state2int", "=", "{", "'stop'", ":", "0", ",", "'run'", ":", "1", "}", "profile_process2int", "=", "{", "'worker'", ":", "0", ",", "'server'", ":", "1"...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
dump
Dump profile and stop profiler. Use this to save profile in advance in case your program cannot exit normally. Parameters ---------- finished : boolean Indicates whether to stop statistic output (dumping) after this dump. Default is True profile_process : string whether to profile kvstore `server` or `worker`. server can only be profiled when kvstore is of type dist. if this is not passed, defaults to `worker`
python/mxnet/profiler.py
def dump(finished=True, profile_process='worker'): """Dump profile and stop profiler. Use this to save profile in advance in case your program cannot exit normally. Parameters ---------- finished : boolean Indicates whether to stop statistic output (dumping) after this dump. Default is True profile_process : string whether to profile kvstore `server` or `worker`. server can only be profiled when kvstore is of type dist. if this is not passed, defaults to `worker` """ fin = 1 if finished is True else 0 profile_process2int = {'worker': 0, 'server': 1} check_call(_LIB.MXDumpProcessProfile(fin, profile_process2int[profile_process], profiler_kvstore_handle))
def dump(finished=True, profile_process='worker'): """Dump profile and stop profiler. Use this to save profile in advance in case your program cannot exit normally. Parameters ---------- finished : boolean Indicates whether to stop statistic output (dumping) after this dump. Default is True profile_process : string whether to profile kvstore `server` or `worker`. server can only be profiled when kvstore is of type dist. if this is not passed, defaults to `worker` """ fin = 1 if finished is True else 0 profile_process2int = {'worker': 0, 'server': 1} check_call(_LIB.MXDumpProcessProfile(fin, profile_process2int[profile_process], profiler_kvstore_handle))
[ "Dump", "profile", "and", "stop", "profiler", ".", "Use", "this", "to", "save", "profile", "in", "advance", "in", "case", "your", "program", "cannot", "exit", "normally", "." ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/profiler.py#L122-L140
[ "def", "dump", "(", "finished", "=", "True", ",", "profile_process", "=", "'worker'", ")", ":", "fin", "=", "1", "if", "finished", "is", "True", "else", "0", "profile_process2int", "=", "{", "'worker'", ":", "0", ",", "'server'", ":", "1", "}", "check_...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
dumps
Return a printable string of aggregate profile stats. Parameters ---------- reset: boolean Indicates whether to clean aggeregate statistical data collected up to this point
python/mxnet/profiler.py
def dumps(reset=False): """Return a printable string of aggregate profile stats. Parameters ---------- reset: boolean Indicates whether to clean aggeregate statistical data collected up to this point """ debug_str = ctypes.c_char_p() do_reset = 1 if reset is True else 0 check_call(_LIB.MXAggregateProfileStatsPrint(ctypes.byref(debug_str), int(do_reset))) return py_str(debug_str.value)
def dumps(reset=False): """Return a printable string of aggregate profile stats. Parameters ---------- reset: boolean Indicates whether to clean aggeregate statistical data collected up to this point """ debug_str = ctypes.c_char_p() do_reset = 1 if reset is True else 0 check_call(_LIB.MXAggregateProfileStatsPrint(ctypes.byref(debug_str), int(do_reset))) return py_str(debug_str.value)
[ "Return", "a", "printable", "string", "of", "aggregate", "profile", "stats", "." ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/profiler.py#L151-L162
[ "def", "dumps", "(", "reset", "=", "False", ")", ":", "debug_str", "=", "ctypes", ".", "c_char_p", "(", ")", "do_reset", "=", "1", "if", "reset", "is", "True", "else", "0", "check_call", "(", "_LIB", ".", "MXAggregateProfileStatsPrint", "(", "ctypes", "....
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
pause
Pause profiling. Parameters ---------- profile_process : string whether to profile kvstore `server` or `worker`. server can only be profiled when kvstore is of type dist. if this is not passed, defaults to `worker`
python/mxnet/profiler.py
def pause(profile_process='worker'): """Pause profiling. Parameters ---------- profile_process : string whether to profile kvstore `server` or `worker`. server can only be profiled when kvstore is of type dist. if this is not passed, defaults to `worker` """ profile_process2int = {'worker': 0, 'server': 1} check_call(_LIB.MXProcessProfilePause(int(1), profile_process2int[profile_process], profiler_kvstore_handle))
def pause(profile_process='worker'): """Pause profiling. Parameters ---------- profile_process : string whether to profile kvstore `server` or `worker`. server can only be profiled when kvstore is of type dist. if this is not passed, defaults to `worker` """ profile_process2int = {'worker': 0, 'server': 1} check_call(_LIB.MXProcessProfilePause(int(1), profile_process2int[profile_process], profiler_kvstore_handle))
[ "Pause", "profiling", "." ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/profiler.py#L165-L178
[ "def", "pause", "(", "profile_process", "=", "'worker'", ")", ":", "profile_process2int", "=", "{", "'worker'", ":", "0", ",", "'server'", ":", "1", "}", "check_call", "(", "_LIB", ".", "MXProcessProfilePause", "(", "int", "(", "1", ")", ",", "profile_proc...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
resume
Resume paused profiling. Parameters ---------- profile_process : string whether to profile kvstore `server` or `worker`. server can only be profiled when kvstore is of type dist. if this is not passed, defaults to `worker`
python/mxnet/profiler.py
def resume(profile_process='worker'): """ Resume paused profiling. Parameters ---------- profile_process : string whether to profile kvstore `server` or `worker`. server can only be profiled when kvstore is of type dist. if this is not passed, defaults to `worker` """ profile_process2int = {'worker': 0, 'server': 1} check_call(_LIB.MXProcessProfilePause(int(0), profile_process2int[profile_process], profiler_kvstore_handle))
def resume(profile_process='worker'): """ Resume paused profiling. Parameters ---------- profile_process : string whether to profile kvstore `server` or `worker`. server can only be profiled when kvstore is of type dist. if this is not passed, defaults to `worker` """ profile_process2int = {'worker': 0, 'server': 1} check_call(_LIB.MXProcessProfilePause(int(0), profile_process2int[profile_process], profiler_kvstore_handle))
[ "Resume", "paused", "profiling", "." ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/profiler.py#L181-L195
[ "def", "resume", "(", "profile_process", "=", "'worker'", ")", ":", "profile_process2int", "=", "{", "'worker'", ":", "0", ",", "'server'", ":", "1", "}", "check_call", "(", "_LIB", ".", "MXProcessProfilePause", "(", "int", "(", "0", ")", ",", "profile_pro...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
Counter.set_value
Set counter value. Parameters ---------- value : int Value for the counter
python/mxnet/profiler.py
def set_value(self, value): """Set counter value. Parameters ---------- value : int Value for the counter """ check_call(_LIB.MXProfileSetCounter(self.handle, int(value)))
def set_value(self, value): """Set counter value. Parameters ---------- value : int Value for the counter """ check_call(_LIB.MXProfileSetCounter(self.handle, int(value)))
[ "Set", "counter", "value", "." ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/profiler.py#L405-L413
[ "def", "set_value", "(", "self", ",", "value", ")", ":", "check_call", "(", "_LIB", ".", "MXProfileSetCounter", "(", "self", ".", "handle", ",", "int", "(", "value", ")", ")", ")" ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
Counter.increment
Increment counter value. Parameters ---------- value_change : int Amount by which to add to the counter
python/mxnet/profiler.py
def increment(self, delta=1): """Increment counter value. Parameters ---------- value_change : int Amount by which to add to the counter """ check_call(_LIB.MXProfileAdjustCounter(self.handle, int(delta)))
def increment(self, delta=1): """Increment counter value. Parameters ---------- value_change : int Amount by which to add to the counter """ check_call(_LIB.MXProfileAdjustCounter(self.handle, int(delta)))
[ "Increment", "counter", "value", "." ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/profiler.py#L415-L423
[ "def", "increment", "(", "self", ",", "delta", "=", "1", ")", ":", "check_call", "(", "_LIB", ".", "MXProfileAdjustCounter", "(", "self", ".", "handle", ",", "int", "(", "delta", ")", ")", ")" ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
Counter.decrement
Decrement counter value. Parameters ---------- value_change : int Amount by which to subtract from the counter
python/mxnet/profiler.py
def decrement(self, delta=1): """Decrement counter value. Parameters ---------- value_change : int Amount by which to subtract from the counter """ check_call(_LIB.MXProfileAdjustCounter(self.handle, -int(delta)))
def decrement(self, delta=1): """Decrement counter value. Parameters ---------- value_change : int Amount by which to subtract from the counter """ check_call(_LIB.MXProfileAdjustCounter(self.handle, -int(delta)))
[ "Decrement", "counter", "value", "." ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/profiler.py#L425-L433
[ "def", "decrement", "(", "self", ",", "delta", "=", "1", ")", ":", "check_call", "(", "_LIB", ".", "MXProfileAdjustCounter", "(", "self", ".", "handle", ",", "-", "int", "(", "delta", ")", ")", ")" ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
Marker.mark
Set up the profiler state to record operator. Parameters ---------- scope : string, optional Indicates what scope the marker should refer to. Can be 'global', 'process', thread', task', and 'marker' Default is `process`.
python/mxnet/profiler.py
def mark(self, scope='process'): """Set up the profiler state to record operator. Parameters ---------- scope : string, optional Indicates what scope the marker should refer to. Can be 'global', 'process', thread', task', and 'marker' Default is `process`. """ check_call(_LIB.MXProfileSetMarker(self.domain.handle, c_str(self.name), c_str(scope)))
def mark(self, scope='process'): """Set up the profiler state to record operator. Parameters ---------- scope : string, optional Indicates what scope the marker should refer to. Can be 'global', 'process', thread', task', and 'marker' Default is `process`. """ check_call(_LIB.MXProfileSetMarker(self.domain.handle, c_str(self.name), c_str(scope)))
[ "Set", "up", "the", "profiler", "state", "to", "record", "operator", "." ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/profiler.py#L463-L473
[ "def", "mark", "(", "self", ",", "scope", "=", "'process'", ")", ":", "check_call", "(", "_LIB", ".", "MXProfileSetMarker", "(", "self", ".", "domain", ".", "handle", ",", "c_str", "(", "self", ".", "name", ")", ",", "c_str", "(", "scope", ")", ")", ...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
CudaModule.get_kernel
r"""Get CUDA kernel from compiled module. Parameters ---------- name : str String name of the kernel. signature : str Function signature for the kernel. For example, if a kernel is declared as:: extern "C" __global__ void axpy(const float *x, double *y, int alpha) Then its signature should be:: const float *x, double *y, int alpha or:: const float *, double *, int Note that `*` in signature marks an argument as array and `const` marks an argument as constant (input) array. Returns ------- CudaKernel CUDA kernels that can be launched on GPUs.
python/mxnet/rtc.py
def get_kernel(self, name, signature): r"""Get CUDA kernel from compiled module. Parameters ---------- name : str String name of the kernel. signature : str Function signature for the kernel. For example, if a kernel is declared as:: extern "C" __global__ void axpy(const float *x, double *y, int alpha) Then its signature should be:: const float *x, double *y, int alpha or:: const float *, double *, int Note that `*` in signature marks an argument as array and `const` marks an argument as constant (input) array. Returns ------- CudaKernel CUDA kernels that can be launched on GPUs. """ hdl = CudaKernelHandle() is_ndarray = [] is_const = [] dtypes = [] pattern = re.compile(r"""^\s*(const)?\s*([\w_]+)\s*(\*)?\s*([\w_]+)?\s*$""") args = re.sub(r"\s+", " ", signature).split(",") for arg in args: match = pattern.match(arg) if not match or match.groups()[1] == 'const': raise ValueError( 'Invalid function prototype "%s". Must be in the ' 'form of "(const) type (*) (name)"'%arg) is_const.append(bool(match.groups()[0])) dtype = match.groups()[1] is_ndarray.append(bool(match.groups()[2])) if dtype not in _DTYPE_CPP_TO_NP: raise TypeError( "Unsupported kernel argument type %s. Supported types are: %s."%( arg, ','.join(_DTYPE_CPP_TO_NP.keys()))) dtypes.append(_DTYPE_NP_TO_MX[_DTYPE_CPP_TO_NP[dtype]]) check_call(_LIB.MXRtcCudaKernelCreate( self.handle, c_str(name), len(dtypes), c_array_buf(ctypes.c_int, array('i', is_ndarray)), c_array_buf(ctypes.c_int, array('i', is_const)), c_array_buf(ctypes.c_int, array('i', dtypes)), ctypes.byref(hdl))) return CudaKernel(hdl, name, is_ndarray, dtypes)
def get_kernel(self, name, signature): r"""Get CUDA kernel from compiled module. Parameters ---------- name : str String name of the kernel. signature : str Function signature for the kernel. For example, if a kernel is declared as:: extern "C" __global__ void axpy(const float *x, double *y, int alpha) Then its signature should be:: const float *x, double *y, int alpha or:: const float *, double *, int Note that `*` in signature marks an argument as array and `const` marks an argument as constant (input) array. Returns ------- CudaKernel CUDA kernels that can be launched on GPUs. """ hdl = CudaKernelHandle() is_ndarray = [] is_const = [] dtypes = [] pattern = re.compile(r"""^\s*(const)?\s*([\w_]+)\s*(\*)?\s*([\w_]+)?\s*$""") args = re.sub(r"\s+", " ", signature).split(",") for arg in args: match = pattern.match(arg) if not match or match.groups()[1] == 'const': raise ValueError( 'Invalid function prototype "%s". Must be in the ' 'form of "(const) type (*) (name)"'%arg) is_const.append(bool(match.groups()[0])) dtype = match.groups()[1] is_ndarray.append(bool(match.groups()[2])) if dtype not in _DTYPE_CPP_TO_NP: raise TypeError( "Unsupported kernel argument type %s. Supported types are: %s."%( arg, ','.join(_DTYPE_CPP_TO_NP.keys()))) dtypes.append(_DTYPE_NP_TO_MX[_DTYPE_CPP_TO_NP[dtype]]) check_call(_LIB.MXRtcCudaKernelCreate( self.handle, c_str(name), len(dtypes), c_array_buf(ctypes.c_int, array('i', is_ndarray)), c_array_buf(ctypes.c_int, array('i', is_const)), c_array_buf(ctypes.c_int, array('i', dtypes)), ctypes.byref(hdl))) return CudaKernel(hdl, name, is_ndarray, dtypes)
[ "r", "Get", "CUDA", "kernel", "from", "compiled", "module", "." ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/rtc.py#L112-L171
[ "def", "get_kernel", "(", "self", ",", "name", ",", "signature", ")", ":", "hdl", "=", "CudaKernelHandle", "(", ")", "is_ndarray", "=", "[", "]", "is_const", "=", "[", "]", "dtypes", "=", "[", "]", "pattern", "=", "re", ".", "compile", "(", "r\"\"\"^...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
CudaKernel.launch
Launch cuda kernel. Parameters ---------- args : tuple of NDArray or numbers List of arguments for kernel. NDArrays are expected for pointer types (e.g. `float*`, `double*`) while numbers are expected for non-pointer types (e.g. `int`, `float`). ctx : Context The context to launch kernel on. Must be GPU context. grid_dims : tuple of 3 integers Grid dimensions for CUDA kernel. block_dims : tuple of 3 integers Block dimensions for CUDA kernel. shared_mem : integer, optional Size of dynamically allocated shared memory. Defaults to 0.
python/mxnet/rtc.py
def launch(self, args, ctx, grid_dims, block_dims, shared_mem=0): """Launch cuda kernel. Parameters ---------- args : tuple of NDArray or numbers List of arguments for kernel. NDArrays are expected for pointer types (e.g. `float*`, `double*`) while numbers are expected for non-pointer types (e.g. `int`, `float`). ctx : Context The context to launch kernel on. Must be GPU context. grid_dims : tuple of 3 integers Grid dimensions for CUDA kernel. block_dims : tuple of 3 integers Block dimensions for CUDA kernel. shared_mem : integer, optional Size of dynamically allocated shared memory. Defaults to 0. """ assert ctx.device_type == 'gpu', "Cuda kernel can only be launched on GPU" assert len(grid_dims) == 3, "grid_dims must be a tuple of 3 integers" assert len(block_dims) == 3, "grid_dims must be a tuple of 3 integers" assert len(args) == len(self._dtypes), \ "CudaKernel(%s) expects %d arguments but got %d"%( self._name, len(self._dtypes), len(args)) void_args = [] ref_holder = [] for i, (arg, is_nd, dtype) in enumerate(zip(args, self._is_ndarray, self._dtypes)): if is_nd: assert isinstance(arg, NDArray), \ "The %d-th argument is expected to be a NDArray but got %s"%( i, type(arg)) void_args.append(arg.handle) else: assert isinstance(arg, numeric_types), \ "The %d-th argument is expected to be a number, but got %s"%( i, type(arg)) ref_holder.append(np.array(arg, dtype=dtype)) void_args.append(ref_holder[-1].ctypes.data_as(ctypes.c_void_p)) check_call(_LIB.MXRtcCudaKernelCall( self.handle, ctx.device_id, c_array(ctypes.c_void_p, void_args), mx_uint(grid_dims[0]), mx_uint(grid_dims[1]), mx_uint(grid_dims[2]), mx_uint(block_dims[0]), mx_uint(block_dims[1]), mx_uint(block_dims[2]), mx_uint(shared_mem)))
def launch(self, args, ctx, grid_dims, block_dims, shared_mem=0): """Launch cuda kernel. Parameters ---------- args : tuple of NDArray or numbers List of arguments for kernel. NDArrays are expected for pointer types (e.g. `float*`, `double*`) while numbers are expected for non-pointer types (e.g. `int`, `float`). ctx : Context The context to launch kernel on. Must be GPU context. grid_dims : tuple of 3 integers Grid dimensions for CUDA kernel. block_dims : tuple of 3 integers Block dimensions for CUDA kernel. shared_mem : integer, optional Size of dynamically allocated shared memory. Defaults to 0. """ assert ctx.device_type == 'gpu', "Cuda kernel can only be launched on GPU" assert len(grid_dims) == 3, "grid_dims must be a tuple of 3 integers" assert len(block_dims) == 3, "grid_dims must be a tuple of 3 integers" assert len(args) == len(self._dtypes), \ "CudaKernel(%s) expects %d arguments but got %d"%( self._name, len(self._dtypes), len(args)) void_args = [] ref_holder = [] for i, (arg, is_nd, dtype) in enumerate(zip(args, self._is_ndarray, self._dtypes)): if is_nd: assert isinstance(arg, NDArray), \ "The %d-th argument is expected to be a NDArray but got %s"%( i, type(arg)) void_args.append(arg.handle) else: assert isinstance(arg, numeric_types), \ "The %d-th argument is expected to be a number, but got %s"%( i, type(arg)) ref_holder.append(np.array(arg, dtype=dtype)) void_args.append(ref_holder[-1].ctypes.data_as(ctypes.c_void_p)) check_call(_LIB.MXRtcCudaKernelCall( self.handle, ctx.device_id, c_array(ctypes.c_void_p, void_args), mx_uint(grid_dims[0]), mx_uint(grid_dims[1]), mx_uint(grid_dims[2]), mx_uint(block_dims[0]), mx_uint(block_dims[1]), mx_uint(block_dims[2]), mx_uint(shared_mem)))
[ "Launch", "cuda", "kernel", "." ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/rtc.py#L185-L230
[ "def", "launch", "(", "self", ",", "args", ",", "ctx", ",", "grid_dims", ",", "block_dims", ",", "shared_mem", "=", "0", ")", ":", "assert", "ctx", ".", "device_type", "==", "'gpu'", ",", "\"Cuda kernel can only be launched on GPU\"", "assert", "len", "(", "...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
MApMetric.reset
Clear the internal statistics to initial state.
example/ssd/evaluate/eval_metric.py
def reset(self): """Clear the internal statistics to initial state.""" if getattr(self, 'num', None) is None: self.num_inst = 0 self.sum_metric = 0.0 else: self.num_inst = [0] * self.num self.sum_metric = [0.0] * self.num self.records = dict() self.counts = dict()
def reset(self): """Clear the internal statistics to initial state.""" if getattr(self, 'num', None) is None: self.num_inst = 0 self.sum_metric = 0.0 else: self.num_inst = [0] * self.num self.sum_metric = [0.0] * self.num self.records = dict() self.counts = dict()
[ "Clear", "the", "internal", "statistics", "to", "initial", "state", "." ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/example/ssd/evaluate/eval_metric.py#L53-L62
[ "def", "reset", "(", "self", ")", ":", "if", "getattr", "(", "self", ",", "'num'", ",", "None", ")", "is", "None", ":", "self", ".", "num_inst", "=", "0", "self", ".", "sum_metric", "=", "0.0", "else", ":", "self", ".", "num_inst", "=", "[", "0",...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
MApMetric.update
Update internal records. This function now only update internal buffer, sum_metric and num_inst are updated in _update() function instead when get() is called to return results. Params: ---------- labels: mx.nd.array (n * 6) or (n * 5), difficult column is optional 2-d array of ground-truths, n objects(id-xmin-ymin-xmax-ymax-[difficult]) preds: mx.nd.array (m * 6) 2-d array of detections, m objects(id-score-xmin-ymin-xmax-ymax)
example/ssd/evaluate/eval_metric.py
def update(self, labels, preds): """ Update internal records. This function now only update internal buffer, sum_metric and num_inst are updated in _update() function instead when get() is called to return results. Params: ---------- labels: mx.nd.array (n * 6) or (n * 5), difficult column is optional 2-d array of ground-truths, n objects(id-xmin-ymin-xmax-ymax-[difficult]) preds: mx.nd.array (m * 6) 2-d array of detections, m objects(id-score-xmin-ymin-xmax-ymax) """ def iou(x, ys): """ Calculate intersection-over-union overlap Params: ---------- x : numpy.array single box [xmin, ymin ,xmax, ymax] ys : numpy.array multiple box [[xmin, ymin, xmax, ymax], [...], ] Returns: ----------- numpy.array [iou1, iou2, ...], size == ys.shape[0] """ ixmin = np.maximum(ys[:, 0], x[0]) iymin = np.maximum(ys[:, 1], x[1]) ixmax = np.minimum(ys[:, 2], x[2]) iymax = np.minimum(ys[:, 3], x[3]) iw = np.maximum(ixmax - ixmin, 0.) ih = np.maximum(iymax - iymin, 0.) inters = iw * ih uni = (x[2] - x[0]) * (x[3] - x[1]) + (ys[:, 2] - ys[:, 0]) * \ (ys[:, 3] - ys[:, 1]) - inters ious = inters / uni ious[uni < 1e-12] = 0 # in case bad boxes return ious # independant execution for each image for i in range(labels[0].shape[0]): # get as numpy arrays label = labels[0][i].asnumpy() if np.sum(label[:, 0] >= 0) < 1: continue pred = preds[self.pred_idx][i].asnumpy() # calculate for each class while (pred.shape[0] > 0): cid = int(pred[0, 0]) indices = np.where(pred[:, 0].astype(int) == cid)[0] if cid < 0: pred = np.delete(pred, indices, axis=0) continue dets = pred[indices] pred = np.delete(pred, indices, axis=0) # sort by score, desceding dets = dets[dets[:,1].argsort()[::-1]] records = np.hstack((dets[:, 1][:, np.newaxis], np.zeros((dets.shape[0], 1)))) # ground-truths label_indices = np.where(label[:, 0].astype(int) == cid)[0] gts = label[label_indices, :] label = np.delete(label, label_indices, axis=0) if gts.size > 0: found = [False] * gts.shape[0] for j in range(dets.shape[0]): # compute overlaps ious = iou(dets[j, 2:], gts[:, 1:5]) ovargmax = np.argmax(ious) ovmax = ious[ovargmax] if ovmax > self.ovp_thresh: if (not self.use_difficult and gts.shape[1] >= 6 and gts[ovargmax, 5] > 0): pass else: if not found[ovargmax]: records[j, -1] = 1 # tp found[ovargmax] = True else: # duplicate records[j, -1] = 2 # fp else: records[j, -1] = 2 # fp else: # no gt, mark all fp records[:, -1] = 2 # ground truth count if (not self.use_difficult and gts.shape[1] >= 6): gt_count = np.sum(gts[:, 5] < 1) else: gt_count = gts.shape[0] # now we push records to buffer # first column: score, second column: tp/fp # 0: not set(matched to difficult or something), 1: tp, 2: fp records = records[np.where(records[:, -1] > 0)[0], :] if records.size > 0: self._insert(cid, records, gt_count) # add missing class if not present in prediction while (label.shape[0] > 0): cid = int(label[0, 0]) label_indices = np.where(label[:, 0].astype(int) == cid)[0] label = np.delete(label, label_indices, axis=0) if cid < 0: continue gt_count = label_indices.size self._insert(cid, np.array([[0, 0]]), gt_count)
def update(self, labels, preds): """ Update internal records. This function now only update internal buffer, sum_metric and num_inst are updated in _update() function instead when get() is called to return results. Params: ---------- labels: mx.nd.array (n * 6) or (n * 5), difficult column is optional 2-d array of ground-truths, n objects(id-xmin-ymin-xmax-ymax-[difficult]) preds: mx.nd.array (m * 6) 2-d array of detections, m objects(id-score-xmin-ymin-xmax-ymax) """ def iou(x, ys): """ Calculate intersection-over-union overlap Params: ---------- x : numpy.array single box [xmin, ymin ,xmax, ymax] ys : numpy.array multiple box [[xmin, ymin, xmax, ymax], [...], ] Returns: ----------- numpy.array [iou1, iou2, ...], size == ys.shape[0] """ ixmin = np.maximum(ys[:, 0], x[0]) iymin = np.maximum(ys[:, 1], x[1]) ixmax = np.minimum(ys[:, 2], x[2]) iymax = np.minimum(ys[:, 3], x[3]) iw = np.maximum(ixmax - ixmin, 0.) ih = np.maximum(iymax - iymin, 0.) inters = iw * ih uni = (x[2] - x[0]) * (x[3] - x[1]) + (ys[:, 2] - ys[:, 0]) * \ (ys[:, 3] - ys[:, 1]) - inters ious = inters / uni ious[uni < 1e-12] = 0 # in case bad boxes return ious # independant execution for each image for i in range(labels[0].shape[0]): # get as numpy arrays label = labels[0][i].asnumpy() if np.sum(label[:, 0] >= 0) < 1: continue pred = preds[self.pred_idx][i].asnumpy() # calculate for each class while (pred.shape[0] > 0): cid = int(pred[0, 0]) indices = np.where(pred[:, 0].astype(int) == cid)[0] if cid < 0: pred = np.delete(pred, indices, axis=0) continue dets = pred[indices] pred = np.delete(pred, indices, axis=0) # sort by score, desceding dets = dets[dets[:,1].argsort()[::-1]] records = np.hstack((dets[:, 1][:, np.newaxis], np.zeros((dets.shape[0], 1)))) # ground-truths label_indices = np.where(label[:, 0].astype(int) == cid)[0] gts = label[label_indices, :] label = np.delete(label, label_indices, axis=0) if gts.size > 0: found = [False] * gts.shape[0] for j in range(dets.shape[0]): # compute overlaps ious = iou(dets[j, 2:], gts[:, 1:5]) ovargmax = np.argmax(ious) ovmax = ious[ovargmax] if ovmax > self.ovp_thresh: if (not self.use_difficult and gts.shape[1] >= 6 and gts[ovargmax, 5] > 0): pass else: if not found[ovargmax]: records[j, -1] = 1 # tp found[ovargmax] = True else: # duplicate records[j, -1] = 2 # fp else: records[j, -1] = 2 # fp else: # no gt, mark all fp records[:, -1] = 2 # ground truth count if (not self.use_difficult and gts.shape[1] >= 6): gt_count = np.sum(gts[:, 5] < 1) else: gt_count = gts.shape[0] # now we push records to buffer # first column: score, second column: tp/fp # 0: not set(matched to difficult or something), 1: tp, 2: fp records = records[np.where(records[:, -1] > 0)[0], :] if records.size > 0: self._insert(cid, records, gt_count) # add missing class if not present in prediction while (label.shape[0] > 0): cid = int(label[0, 0]) label_indices = np.where(label[:, 0].astype(int) == cid)[0] label = np.delete(label, label_indices, axis=0) if cid < 0: continue gt_count = label_indices.size self._insert(cid, np.array([[0, 0]]), gt_count)
[ "Update", "internal", "records", ".", "This", "function", "now", "only", "update", "internal", "buffer", "sum_metric", "and", "num_inst", "are", "updated", "in", "_update", "()", "function", "instead", "when", "get", "()", "is", "called", "to", "return", "resu...
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/example/ssd/evaluate/eval_metric.py#L86-L195
[ "def", "update", "(", "self", ",", "labels", ",", "preds", ")", ":", "def", "iou", "(", "x", ",", "ys", ")", ":", "\"\"\"\n Calculate intersection-over-union overlap\n Params:\n ----------\n x : numpy.array\n single box [...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
MApMetric._update
update num_inst and sum_metric
example/ssd/evaluate/eval_metric.py
def _update(self): """ update num_inst and sum_metric """ aps = [] for k, v in self.records.items(): recall, prec = self._recall_prec(v, self.counts[k]) ap = self._average_precision(recall, prec) aps.append(ap) if self.num is not None and k < (self.num - 1): self.sum_metric[k] = ap self.num_inst[k] = 1 if self.num is None: self.num_inst = 1 self.sum_metric = np.mean(aps) else: self.num_inst[-1] = 1 self.sum_metric[-1] = np.mean(aps)
def _update(self): """ update num_inst and sum_metric """ aps = [] for k, v in self.records.items(): recall, prec = self._recall_prec(v, self.counts[k]) ap = self._average_precision(recall, prec) aps.append(ap) if self.num is not None and k < (self.num - 1): self.sum_metric[k] = ap self.num_inst[k] = 1 if self.num is None: self.num_inst = 1 self.sum_metric = np.mean(aps) else: self.num_inst[-1] = 1 self.sum_metric[-1] = np.mean(aps)
[ "update", "num_inst", "and", "sum_metric" ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/example/ssd/evaluate/eval_metric.py#L197-L212
[ "def", "_update", "(", "self", ")", ":", "aps", "=", "[", "]", "for", "k", ",", "v", "in", "self", ".", "records", ".", "items", "(", ")", ":", "recall", ",", "prec", "=", "self", ".", "_recall_prec", "(", "v", ",", "self", ".", "counts", "[", ...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
MApMetric._recall_prec
get recall and precision from internal records
example/ssd/evaluate/eval_metric.py
def _recall_prec(self, record, count): """ get recall and precision from internal records """ record = np.delete(record, np.where(record[:, 1].astype(int) == 0)[0], axis=0) sorted_records = record[record[:,0].argsort()[::-1]] tp = np.cumsum(sorted_records[:, 1].astype(int) == 1) fp = np.cumsum(sorted_records[:, 1].astype(int) == 2) if count <= 0: recall = tp * 0.0 else: recall = tp / float(count) prec = tp.astype(float) / (tp + fp) return recall, prec
def _recall_prec(self, record, count): """ get recall and precision from internal records """ record = np.delete(record, np.where(record[:, 1].astype(int) == 0)[0], axis=0) sorted_records = record[record[:,0].argsort()[::-1]] tp = np.cumsum(sorted_records[:, 1].astype(int) == 1) fp = np.cumsum(sorted_records[:, 1].astype(int) == 2) if count <= 0: recall = tp * 0.0 else: recall = tp / float(count) prec = tp.astype(float) / (tp + fp) return recall, prec
[ "get", "recall", "and", "precision", "from", "internal", "records" ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/example/ssd/evaluate/eval_metric.py#L214-L225
[ "def", "_recall_prec", "(", "self", ",", "record", ",", "count", ")", ":", "record", "=", "np", ".", "delete", "(", "record", ",", "np", ".", "where", "(", "record", "[", ":", ",", "1", "]", ".", "astype", "(", "int", ")", "==", "0", ")", "[", ...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
MApMetric._average_precision
calculate average precision Params: ---------- rec : numpy.array cumulated recall prec : numpy.array cumulated precision Returns: ---------- ap as float
example/ssd/evaluate/eval_metric.py
def _average_precision(self, rec, prec): """ calculate average precision Params: ---------- rec : numpy.array cumulated recall prec : numpy.array cumulated precision Returns: ---------- ap as float """ # append sentinel values at both ends mrec = np.concatenate(([0.], rec, [1.])) mpre = np.concatenate(([0.], prec, [0.])) # compute precision integration ladder for i in range(mpre.size - 1, 0, -1): mpre[i - 1] = np.maximum(mpre[i - 1], mpre[i]) # look for recall value changes i = np.where(mrec[1:] != mrec[:-1])[0] # sum (\delta recall) * prec ap = np.sum((mrec[i + 1] - mrec[i]) * mpre[i + 1]) return ap
def _average_precision(self, rec, prec): """ calculate average precision Params: ---------- rec : numpy.array cumulated recall prec : numpy.array cumulated precision Returns: ---------- ap as float """ # append sentinel values at both ends mrec = np.concatenate(([0.], rec, [1.])) mpre = np.concatenate(([0.], prec, [0.])) # compute precision integration ladder for i in range(mpre.size - 1, 0, -1): mpre[i - 1] = np.maximum(mpre[i - 1], mpre[i]) # look for recall value changes i = np.where(mrec[1:] != mrec[:-1])[0] # sum (\delta recall) * prec ap = np.sum((mrec[i + 1] - mrec[i]) * mpre[i + 1]) return ap
[ "calculate", "average", "precision" ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/example/ssd/evaluate/eval_metric.py#L227-L254
[ "def", "_average_precision", "(", "self", ",", "rec", ",", "prec", ")", ":", "# append sentinel values at both ends", "mrec", "=", "np", ".", "concatenate", "(", "(", "[", "0.", "]", ",", "rec", ",", "[", "1.", "]", ")", ")", "mpre", "=", "np", ".", ...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
MApMetric._insert
Insert records according to key
example/ssd/evaluate/eval_metric.py
def _insert(self, key, records, count): """ Insert records according to key """ if key not in self.records: assert key not in self.counts self.records[key] = records self.counts[key] = count else: self.records[key] = np.vstack((self.records[key], records)) assert key in self.counts self.counts[key] += count
def _insert(self, key, records, count): """ Insert records according to key """ if key not in self.records: assert key not in self.counts self.records[key] = records self.counts[key] = count else: self.records[key] = np.vstack((self.records[key], records)) assert key in self.counts self.counts[key] += count
[ "Insert", "records", "according", "to", "key" ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/example/ssd/evaluate/eval_metric.py#L256-L265
[ "def", "_insert", "(", "self", ",", "key", ",", "records", ",", "count", ")", ":", "if", "key", "not", "in", "self", ".", "records", ":", "assert", "key", "not", "in", "self", ".", "counts", "self", ".", "records", "[", "key", "]", "=", "records", ...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
VOC07MApMetric._average_precision
calculate average precision, override the default one, special 11-point metric Params: ---------- rec : numpy.array cumulated recall prec : numpy.array cumulated precision Returns: ---------- ap as float
example/ssd/evaluate/eval_metric.py
def _average_precision(self, rec, prec): """ calculate average precision, override the default one, special 11-point metric Params: ---------- rec : numpy.array cumulated recall prec : numpy.array cumulated precision Returns: ---------- ap as float """ ap = 0. for t in np.arange(0., 1.1, 0.1): if np.sum(rec >= t) == 0: p = 0 else: p = np.max(prec[rec >= t]) ap += p / 11. return ap
def _average_precision(self, rec, prec): """ calculate average precision, override the default one, special 11-point metric Params: ---------- rec : numpy.array cumulated recall prec : numpy.array cumulated precision Returns: ---------- ap as float """ ap = 0. for t in np.arange(0., 1.1, 0.1): if np.sum(rec >= t) == 0: p = 0 else: p = np.max(prec[rec >= t]) ap += p / 11. return ap
[ "calculate", "average", "precision", "override", "the", "default", "one", "special", "11", "-", "point", "metric" ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/example/ssd/evaluate/eval_metric.py#L273-L295
[ "def", "_average_precision", "(", "self", ",", "rec", ",", "prec", ")", ":", "ap", "=", "0.", "for", "t", "in", "np", ".", "arange", "(", "0.", ",", "1.1", ",", "0.1", ")", ":", "if", "np", ".", "sum", "(", "rec", ">=", "t", ")", "==", "0", ...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
get_fine_tune_model
symbol: the pre-trained network symbol arg_params: the argument parameters of the pre-trained model num_classes: the number of classes for the fine-tune datasets layer_name: the layer name before the last fully-connected layer
example/image-classification/fine-tune.py
def get_fine_tune_model(symbol, arg_params, num_classes, layer_name, dtype='float32'): """ symbol: the pre-trained network symbol arg_params: the argument parameters of the pre-trained model num_classes: the number of classes for the fine-tune datasets layer_name: the layer name before the last fully-connected layer """ all_layers = symbol.get_internals() net = all_layers[layer_name+'_output'] net = mx.symbol.FullyConnected(data=net, num_hidden=num_classes, name='fc') if dtype == 'float16': net = mx.sym.Cast(data=net, dtype=np.float32) net = mx.symbol.SoftmaxOutput(data=net, name='softmax') new_args = dict({k:arg_params[k] for k in arg_params if 'fc' not in k}) return (net, new_args)
def get_fine_tune_model(symbol, arg_params, num_classes, layer_name, dtype='float32'): """ symbol: the pre-trained network symbol arg_params: the argument parameters of the pre-trained model num_classes: the number of classes for the fine-tune datasets layer_name: the layer name before the last fully-connected layer """ all_layers = symbol.get_internals() net = all_layers[layer_name+'_output'] net = mx.symbol.FullyConnected(data=net, num_hidden=num_classes, name='fc') if dtype == 'float16': net = mx.sym.Cast(data=net, dtype=np.float32) net = mx.symbol.SoftmaxOutput(data=net, name='softmax') new_args = dict({k:arg_params[k] for k in arg_params if 'fc' not in k}) return (net, new_args)
[ "symbol", ":", "the", "pre", "-", "trained", "network", "symbol", "arg_params", ":", "the", "argument", "parameters", "of", "the", "pre", "-", "trained", "model", "num_classes", ":", "the", "number", "of", "classes", "for", "the", "fine", "-", "tune", "dat...
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/example/image-classification/fine-tune.py#L28-L42
[ "def", "get_fine_tune_model", "(", "symbol", ",", "arg_params", ",", "num_classes", ",", "layer_name", ",", "dtype", "=", "'float32'", ")", ":", "all_layers", "=", "symbol", ".", "get_internals", "(", ")", "net", "=", "all_layers", "[", "layer_name", "+", "'...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
LipsDataset._list_images
Description : generate list for lip images
example/gluon/lipnet/data_loader.py
def _list_images(self, root): """ Description : generate list for lip images """ self.labels = [] self.items = [] valid_unseen_sub_idx = [1, 2, 20, 22] skip_sub_idx = [21] if self._mode == 'train': sub_idx = ['s' + str(i) for i in range(1, 35) \ if i not in valid_unseen_sub_idx + skip_sub_idx] elif self._mode == 'valid': sub_idx = ['s' + str(i) for i in valid_unseen_sub_idx] folder_path = [] for i in sub_idx: folder_path.extend(glob.glob(os.path.join(root, i, "*"))) for folder in folder_path: filename = glob.glob(os.path.join(folder, "*")) if len(filename) != self._seq_len: continue filename.sort() label = os.path.split(folder)[-1] self.items.append((filename, label))
def _list_images(self, root): """ Description : generate list for lip images """ self.labels = [] self.items = [] valid_unseen_sub_idx = [1, 2, 20, 22] skip_sub_idx = [21] if self._mode == 'train': sub_idx = ['s' + str(i) for i in range(1, 35) \ if i not in valid_unseen_sub_idx + skip_sub_idx] elif self._mode == 'valid': sub_idx = ['s' + str(i) for i in valid_unseen_sub_idx] folder_path = [] for i in sub_idx: folder_path.extend(glob.glob(os.path.join(root, i, "*"))) for folder in folder_path: filename = glob.glob(os.path.join(folder, "*")) if len(filename) != self._seq_len: continue filename.sort() label = os.path.split(folder)[-1] self.items.append((filename, label))
[ "Description", ":", "generate", "list", "for", "lip", "images" ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/example/gluon/lipnet/data_loader.py#L45-L71
[ "def", "_list_images", "(", "self", ",", "root", ")", ":", "self", ".", "labels", "=", "[", "]", "self", ".", "items", "=", "[", "]", "valid_unseen_sub_idx", "=", "[", "1", ",", "2", ",", "20", ",", "22", "]", "skip_sub_idx", "=", "[", "21", "]",...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
LipsDataset.align_generation
Description : Align to lip position
example/gluon/lipnet/data_loader.py
def align_generation(self, file_nm, padding=75): """ Description : Align to lip position """ align = Align(self._align_root + '/' + file_nm + '.align') return nd.array(align.sentence(padding))
def align_generation(self, file_nm, padding=75): """ Description : Align to lip position """ align = Align(self._align_root + '/' + file_nm + '.align') return nd.array(align.sentence(padding))
[ "Description", ":", "Align", "to", "lip", "position" ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/example/gluon/lipnet/data_loader.py#L73-L78
[ "def", "align_generation", "(", "self", ",", "file_nm", ",", "padding", "=", "75", ")", ":", "align", "=", "Align", "(", "self", ".", "_align_root", "+", "'/'", "+", "file_nm", "+", "'.align'", ")", "return", "nd", ".", "array", "(", "align", ".", "s...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
Initializer.set_verbosity
Switch on/off verbose mode Parameters ---------- verbose : bool switch on/off verbose mode print_func : function A function that computes statistics of initialized arrays. Takes an `NDArray` and returns an `str`. Defaults to mean absolute value str((abs(x)/size(x)).asscalar()).
python/mxnet/initializer.py
def set_verbosity(self, verbose=False, print_func=None): """Switch on/off verbose mode Parameters ---------- verbose : bool switch on/off verbose mode print_func : function A function that computes statistics of initialized arrays. Takes an `NDArray` and returns an `str`. Defaults to mean absolute value str((abs(x)/size(x)).asscalar()). """ self._verbose = verbose if print_func is None: def asum_stat(x): """returns |x|/size(x), async execution.""" return str((ndarray.norm(x)/sqrt(x.size)).asscalar()) print_func = asum_stat self._print_func = print_func return self
def set_verbosity(self, verbose=False, print_func=None): """Switch on/off verbose mode Parameters ---------- verbose : bool switch on/off verbose mode print_func : function A function that computes statistics of initialized arrays. Takes an `NDArray` and returns an `str`. Defaults to mean absolute value str((abs(x)/size(x)).asscalar()). """ self._verbose = verbose if print_func is None: def asum_stat(x): """returns |x|/size(x), async execution.""" return str((ndarray.norm(x)/sqrt(x.size)).asscalar()) print_func = asum_stat self._print_func = print_func return self
[ "Switch", "on", "/", "off", "verbose", "mode" ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/initializer.py#L61-L80
[ "def", "set_verbosity", "(", "self", ",", "verbose", "=", "False", ",", "print_func", "=", "None", ")", ":", "self", ".", "_verbose", "=", "verbose", "if", "print_func", "is", "None", ":", "def", "asum_stat", "(", "x", ")", ":", "\"\"\"returns |x|/size(x),...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
Initializer._verbose_print
Internal verbose print function Parameters ---------- desc : InitDesc or str name of the array init : str initializer pattern arr : NDArray initialized array
python/mxnet/initializer.py
def _verbose_print(self, desc, init, arr): """Internal verbose print function Parameters ---------- desc : InitDesc or str name of the array init : str initializer pattern arr : NDArray initialized array """ if self._verbose and self._print_func: logging.info('Initialized %s as %s: %s', desc, init, self._print_func(arr))
def _verbose_print(self, desc, init, arr): """Internal verbose print function Parameters ---------- desc : InitDesc or str name of the array init : str initializer pattern arr : NDArray initialized array """ if self._verbose and self._print_func: logging.info('Initialized %s as %s: %s', desc, init, self._print_func(arr))
[ "Internal", "verbose", "print", "function" ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/initializer.py#L82-L95
[ "def", "_verbose_print", "(", "self", ",", "desc", ",", "init", ",", "arr", ")", ":", "if", "self", ".", "_verbose", "and", "self", ".", "_print_func", ":", "logging", ".", "info", "(", "'Initialized %s as %s: %s'", ",", "desc", ",", "init", ",", "self",...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
Initializer._legacy_init
Legacy initialization method. Parameters ---------- name : str Name of corresponding NDArray. arr : NDArray NDArray to be initialized.
python/mxnet/initializer.py
def _legacy_init(self, name, arr): """Legacy initialization method. Parameters ---------- name : str Name of corresponding NDArray. arr : NDArray NDArray to be initialized. """ warnings.warn( "\033[91mCalling initializer with init(str, NDArray) has been deprecated." \ "please use init(mx.init.InitDesc(...), NDArray) instead.\033[0m", DeprecationWarning, stacklevel=3) if not isinstance(name, string_types): raise TypeError('name must be string') if not isinstance(arr, NDArray): raise TypeError('arr must be NDArray') if name.startswith('upsampling'): self._init_bilinear(name, arr) elif name.startswith('stn_loc') and name.endswith('weight'): self._init_zero(name, arr) elif name.startswith('stn_loc') and name.endswith('bias'): self._init_loc_bias(name, arr) elif name.endswith('bias'): self._init_bias(name, arr) elif name.endswith('gamma'): self._init_gamma(name, arr) elif name.endswith('beta'): self._init_beta(name, arr) elif name.endswith('weight'): self._init_weight(name, arr) elif name.endswith("moving_mean"): self._init_zero(name, arr) elif name.endswith("moving_var"): self._init_one(name, arr) elif name.endswith("moving_inv_var"): self._init_zero(name, arr) elif name.endswith("moving_avg"): self._init_zero(name, arr) elif name.endswith('min'): self._init_zero(name, arr) elif name.endswith('max'): self._init_one(name, arr) else: self._init_default(name, arr)
def _legacy_init(self, name, arr): """Legacy initialization method. Parameters ---------- name : str Name of corresponding NDArray. arr : NDArray NDArray to be initialized. """ warnings.warn( "\033[91mCalling initializer with init(str, NDArray) has been deprecated." \ "please use init(mx.init.InitDesc(...), NDArray) instead.\033[0m", DeprecationWarning, stacklevel=3) if not isinstance(name, string_types): raise TypeError('name must be string') if not isinstance(arr, NDArray): raise TypeError('arr must be NDArray') if name.startswith('upsampling'): self._init_bilinear(name, arr) elif name.startswith('stn_loc') and name.endswith('weight'): self._init_zero(name, arr) elif name.startswith('stn_loc') and name.endswith('bias'): self._init_loc_bias(name, arr) elif name.endswith('bias'): self._init_bias(name, arr) elif name.endswith('gamma'): self._init_gamma(name, arr) elif name.endswith('beta'): self._init_beta(name, arr) elif name.endswith('weight'): self._init_weight(name, arr) elif name.endswith("moving_mean"): self._init_zero(name, arr) elif name.endswith("moving_var"): self._init_one(name, arr) elif name.endswith("moving_inv_var"): self._init_zero(name, arr) elif name.endswith("moving_avg"): self._init_zero(name, arr) elif name.endswith('min'): self._init_zero(name, arr) elif name.endswith('max'): self._init_one(name, arr) else: self._init_default(name, arr)
[ "Legacy", "initialization", "method", "." ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/initializer.py#L171-L217
[ "def", "_legacy_init", "(", "self", ",", "name", ",", "arr", ")", ":", "warnings", ".", "warn", "(", "\"\\033[91mCalling initializer with init(str, NDArray) has been deprecated.\"", "\"please use init(mx.init.InitDesc(...), NDArray) instead.\\033[0m\"", ",", "DeprecationWarning", ...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
Imdb.save_imglist
save imglist to disk Parameters: ---------- fname : str saved filename
example/ssd/dataset/imdb.py
def save_imglist(self, fname=None, root=None, shuffle=False): """ save imglist to disk Parameters: ---------- fname : str saved filename """ def progress_bar(count, total, suffix=''): import sys bar_len = 24 filled_len = int(round(bar_len * count / float(total))) percents = round(100.0 * count / float(total), 1) bar = '=' * filled_len + '-' * (bar_len - filled_len) sys.stdout.write('[%s] %s%s ...%s\r' % (bar, percents, '%', suffix)) sys.stdout.flush() str_list = [] for index in range(self.num_images): progress_bar(index, self.num_images) label = self.label_from_index(index) if label.size < 1: continue path = self.image_path_from_index(index) if root: path = osp.relpath(path, root) str_list.append('\t'.join([str(index), str(2), str(label.shape[1])] \ + ["{0:.4f}".format(x) for x in label.ravel()] + [path,]) + '\n') if str_list: if shuffle: import random random.shuffle(str_list) if not fname: fname = self.name + '.lst' with open(fname, 'w') as f: for line in str_list: f.write(line) else: raise RuntimeError("No image in imdb")
def save_imglist(self, fname=None, root=None, shuffle=False): """ save imglist to disk Parameters: ---------- fname : str saved filename """ def progress_bar(count, total, suffix=''): import sys bar_len = 24 filled_len = int(round(bar_len * count / float(total))) percents = round(100.0 * count / float(total), 1) bar = '=' * filled_len + '-' * (bar_len - filled_len) sys.stdout.write('[%s] %s%s ...%s\r' % (bar, percents, '%', suffix)) sys.stdout.flush() str_list = [] for index in range(self.num_images): progress_bar(index, self.num_images) label = self.label_from_index(index) if label.size < 1: continue path = self.image_path_from_index(index) if root: path = osp.relpath(path, root) str_list.append('\t'.join([str(index), str(2), str(label.shape[1])] \ + ["{0:.4f}".format(x) for x in label.ravel()] + [path,]) + '\n') if str_list: if shuffle: import random random.shuffle(str_list) if not fname: fname = self.name + '.lst' with open(fname, 'w') as f: for line in str_list: f.write(line) else: raise RuntimeError("No image in imdb")
[ "save", "imglist", "to", "disk" ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/example/ssd/dataset/imdb.py#L70-L110
[ "def", "save_imglist", "(", "self", ",", "fname", "=", "None", ",", "root", "=", "None", ",", "shuffle", "=", "False", ")", ":", "def", "progress_bar", "(", "count", ",", "total", ",", "suffix", "=", "''", ")", ":", "import", "sys", "bar_len", "=", ...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
Imdb._load_class_names
load class names from text file Parameters: ---------- filename: str file stores class names dirname: str file directory
example/ssd/dataset/imdb.py
def _load_class_names(self, filename, dirname): """ load class names from text file Parameters: ---------- filename: str file stores class names dirname: str file directory """ full_path = osp.join(dirname, filename) classes = [] with open(full_path, 'r') as f: classes = [l.strip() for l in f.readlines()] return classes
def _load_class_names(self, filename, dirname): """ load class names from text file Parameters: ---------- filename: str file stores class names dirname: str file directory """ full_path = osp.join(dirname, filename) classes = [] with open(full_path, 'r') as f: classes = [l.strip() for l in f.readlines()] return classes
[ "load", "class", "names", "from", "text", "file" ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/example/ssd/dataset/imdb.py#L112-L127
[ "def", "_load_class_names", "(", "self", ",", "filename", ",", "dirname", ")", ":", "full_path", "=", "osp", ".", "join", "(", "dirname", ",", "filename", ")", "classes", "=", "[", "]", "with", "open", "(", "full_path", ",", "'r'", ")", "as", "f", ":...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
read_data
download and read data into numpy
example/image-classification/train_mnist.py
def read_data(label, image): """ download and read data into numpy """ base_url = 'http://yann.lecun.com/exdb/mnist/' with gzip.open(download_file(base_url+label, os.path.join('data',label))) as flbl: magic, num = struct.unpack(">II", flbl.read(8)) label = np.fromstring(flbl.read(), dtype=np.int8) with gzip.open(download_file(base_url+image, os.path.join('data',image)), 'rb') as fimg: magic, num, rows, cols = struct.unpack(">IIII", fimg.read(16)) image = np.fromstring(fimg.read(), dtype=np.uint8).reshape(len(label), rows, cols) return (label, image)
def read_data(label, image): """ download and read data into numpy """ base_url = 'http://yann.lecun.com/exdb/mnist/' with gzip.open(download_file(base_url+label, os.path.join('data',label))) as flbl: magic, num = struct.unpack(">II", flbl.read(8)) label = np.fromstring(flbl.read(), dtype=np.int8) with gzip.open(download_file(base_url+image, os.path.join('data',image)), 'rb') as fimg: magic, num, rows, cols = struct.unpack(">IIII", fimg.read(16)) image = np.fromstring(fimg.read(), dtype=np.uint8).reshape(len(label), rows, cols) return (label, image)
[ "download", "and", "read", "data", "into", "numpy" ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/example/image-classification/train_mnist.py#L31-L42
[ "def", "read_data", "(", "label", ",", "image", ")", ":", "base_url", "=", "'http://yann.lecun.com/exdb/mnist/'", "with", "gzip", ".", "open", "(", "download_file", "(", "base_url", "+", "label", ",", "os", ".", "path", ".", "join", "(", "'data'", ",", "la...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
get_mnist_iter
create data iterator with NDArrayIter
example/image-classification/train_mnist.py
def get_mnist_iter(args, kv): """ create data iterator with NDArrayIter """ (train_lbl, train_img) = read_data( 'train-labels-idx1-ubyte.gz', 'train-images-idx3-ubyte.gz') (val_lbl, val_img) = read_data( 't10k-labels-idx1-ubyte.gz', 't10k-images-idx3-ubyte.gz') train = mx.io.NDArrayIter( to4d(train_img), train_lbl, args.batch_size, shuffle=True) val = mx.io.NDArrayIter( to4d(val_img), val_lbl, args.batch_size) return (train, val)
def get_mnist_iter(args, kv): """ create data iterator with NDArrayIter """ (train_lbl, train_img) = read_data( 'train-labels-idx1-ubyte.gz', 'train-images-idx3-ubyte.gz') (val_lbl, val_img) = read_data( 't10k-labels-idx1-ubyte.gz', 't10k-images-idx3-ubyte.gz') train = mx.io.NDArrayIter( to4d(train_img), train_lbl, args.batch_size, shuffle=True) val = mx.io.NDArrayIter( to4d(val_img), val_lbl, args.batch_size) return (train, val)
[ "create", "data", "iterator", "with", "NDArrayIter" ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/example/image-classification/train_mnist.py#L51-L63
[ "def", "get_mnist_iter", "(", "args", ",", "kv", ")", ":", "(", "train_lbl", ",", "train_img", ")", "=", "read_data", "(", "'train-labels-idx1-ubyte.gz'", ",", "'train-images-idx3-ubyte.gz'", ")", "(", "val_lbl", ",", "val_img", ")", "=", "read_data", "(", "'t...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
make_file_extension_assertion
Function factory for file extension argparse assertion Args: extension (string): the file extension to assert Returns: string: the supplied extension, if assertion is successful.
example/fcn-xs/image_segmentaion.py
def make_file_extension_assertion(extension): """Function factory for file extension argparse assertion Args: extension (string): the file extension to assert Returns: string: the supplied extension, if assertion is successful. """ def file_extension_assertion(file_path): base, ext = os.path.splitext(file_path) if ext.lower() != extension: raise argparse.ArgumentTypeError('File must have ' + extension + ' extension') return file_path return file_extension_assertion
def make_file_extension_assertion(extension): """Function factory for file extension argparse assertion Args: extension (string): the file extension to assert Returns: string: the supplied extension, if assertion is successful. """ def file_extension_assertion(file_path): base, ext = os.path.splitext(file_path) if ext.lower() != extension: raise argparse.ArgumentTypeError('File must have ' + extension + ' extension') return file_path return file_extension_assertion
[ "Function", "factory", "for", "file", "extension", "argparse", "assertion", "Args", ":", "extension", "(", "string", ")", ":", "the", "file", "extension", "to", "assert" ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/example/fcn-xs/image_segmentaion.py#L31-L45
[ "def", "make_file_extension_assertion", "(", "extension", ")", ":", "def", "file_extension_assertion", "(", "file_path", ")", ":", "base", ",", "ext", "=", "os", ".", "path", ".", "splitext", "(", "file_path", ")", "if", "ext", ".", "lower", "(", ")", "!="...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
get_palette
generates the colormap for visualizing the segmentation mask Args: num_colors (int): the number of colors to generate in the output palette Returns: string: the supplied extension, if assertion is successful.
example/fcn-xs/image_segmentaion.py
def get_palette(num_colors=256): """generates the colormap for visualizing the segmentation mask Args: num_colors (int): the number of colors to generate in the output palette Returns: string: the supplied extension, if assertion is successful. """ pallete = [0]*(num_colors*3) for j in range(0, num_colors): lab = j pallete[j*3+0] = 0 pallete[j*3+1] = 0 pallete[j*3+2] = 0 i = 0 while (lab > 0): pallete[j*3+0] |= (((lab >> 0) & 1) << (7-i)) pallete[j*3+1] |= (((lab >> 1) & 1) << (7-i)) pallete[j*3+2] |= (((lab >> 2) & 1) << (7-i)) i = i + 1 lab >>= 3 return pallete
def get_palette(num_colors=256): """generates the colormap for visualizing the segmentation mask Args: num_colors (int): the number of colors to generate in the output palette Returns: string: the supplied extension, if assertion is successful. """ pallete = [0]*(num_colors*3) for j in range(0, num_colors): lab = j pallete[j*3+0] = 0 pallete[j*3+1] = 0 pallete[j*3+2] = 0 i = 0 while (lab > 0): pallete[j*3+0] |= (((lab >> 0) & 1) << (7-i)) pallete[j*3+1] |= (((lab >> 1) & 1) << (7-i)) pallete[j*3+2] |= (((lab >> 2) & 1) << (7-i)) i = i + 1 lab >>= 3 return pallete
[ "generates", "the", "colormap", "for", "visualizing", "the", "segmentation", "mask", "Args", ":", "num_colors", "(", "int", ")", ":", "the", "number", "of", "colors", "to", "generate", "in", "the", "output", "palette" ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/example/fcn-xs/image_segmentaion.py#L47-L69
[ "def", "get_palette", "(", "num_colors", "=", "256", ")", ":", "pallete", "=", "[", "0", "]", "*", "(", "num_colors", "*", "3", ")", "for", "j", "in", "range", "(", "0", ",", "num_colors", ")", ":", "lab", "=", "j", "pallete", "[", "j", "*", "3...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
get_data
get the (1, 3, h, w) np.array data for the supplied image Args: img_path (string): the input image path Returns: np.array: image data in a (1, 3, h, w) shape
example/fcn-xs/image_segmentaion.py
def get_data(img_path): """get the (1, 3, h, w) np.array data for the supplied image Args: img_path (string): the input image path Returns: np.array: image data in a (1, 3, h, w) shape """ mean = np.array([123.68, 116.779, 103.939]) # (R,G,B) img = Image.open(img_path) img = np.array(img, dtype=np.float32) reshaped_mean = mean.reshape(1, 1, 3) img = img - reshaped_mean img = np.swapaxes(img, 0, 2) img = np.swapaxes(img, 1, 2) img = np.expand_dims(img, axis=0) return img
def get_data(img_path): """get the (1, 3, h, w) np.array data for the supplied image Args: img_path (string): the input image path Returns: np.array: image data in a (1, 3, h, w) shape """ mean = np.array([123.68, 116.779, 103.939]) # (R,G,B) img = Image.open(img_path) img = np.array(img, dtype=np.float32) reshaped_mean = mean.reshape(1, 1, 3) img = img - reshaped_mean img = np.swapaxes(img, 0, 2) img = np.swapaxes(img, 1, 2) img = np.expand_dims(img, axis=0) return img
[ "get", "the", "(", "1", "3", "h", "w", ")", "np", ".", "array", "data", "for", "the", "supplied", "image", "Args", ":", "img_path", "(", "string", ")", ":", "the", "input", "image", "path" ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/example/fcn-xs/image_segmentaion.py#L71-L88
[ "def", "get_data", "(", "img_path", ")", ":", "mean", "=", "np", ".", "array", "(", "[", "123.68", ",", "116.779", ",", "103.939", "]", ")", "# (R,G,B)", "img", "=", "Image", ".", "open", "(", "img_path", ")", "img", "=", "np", ".", "array", "(", ...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
main
Module main execution
example/fcn-xs/image_segmentaion.py
def main(): """Module main execution""" # Initialization variables - update to change your model and execution context model_prefix = "FCN8s_VGG16" epoch = 19 # By default, MXNet will run on the CPU. Change to ctx = mx.gpu() to run on GPU. ctx = mx.cpu() fcnxs, fcnxs_args, fcnxs_auxs = mx.model.load_checkpoint(model_prefix, epoch) fcnxs_args["data"] = mx.nd.array(get_data(args.input), ctx) data_shape = fcnxs_args["data"].shape label_shape = (1, data_shape[2]*data_shape[3]) fcnxs_args["softmax_label"] = mx.nd.empty(label_shape, ctx) exector = fcnxs.bind(ctx, fcnxs_args, args_grad=None, grad_req="null", aux_states=fcnxs_args) exector.forward(is_train=False) output = exector.outputs[0] out_img = np.uint8(np.squeeze(output.asnumpy().argmax(axis=1))) out_img = Image.fromarray(out_img) out_img.putpalette(get_palette()) out_img.save(args.output)
def main(): """Module main execution""" # Initialization variables - update to change your model and execution context model_prefix = "FCN8s_VGG16" epoch = 19 # By default, MXNet will run on the CPU. Change to ctx = mx.gpu() to run on GPU. ctx = mx.cpu() fcnxs, fcnxs_args, fcnxs_auxs = mx.model.load_checkpoint(model_prefix, epoch) fcnxs_args["data"] = mx.nd.array(get_data(args.input), ctx) data_shape = fcnxs_args["data"].shape label_shape = (1, data_shape[2]*data_shape[3]) fcnxs_args["softmax_label"] = mx.nd.empty(label_shape, ctx) exector = fcnxs.bind(ctx, fcnxs_args, args_grad=None, grad_req="null", aux_states=fcnxs_args) exector.forward(is_train=False) output = exector.outputs[0] out_img = np.uint8(np.squeeze(output.asnumpy().argmax(axis=1))) out_img = Image.fromarray(out_img) out_img.putpalette(get_palette()) out_img.save(args.output)
[ "Module", "main", "execution" ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/example/fcn-xs/image_segmentaion.py#L90-L110
[ "def", "main", "(", ")", ":", "# Initialization variables - update to change your model and execution context", "model_prefix", "=", "\"FCN8s_VGG16\"", "epoch", "=", "19", "# By default, MXNet will run on the CPU. Change to ctx = mx.gpu() to run on GPU.", "ctx", "=", "mx", ".", "cp...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
ConcatDB._check_classes
check input imdbs, make sure they have same classes
example/ssd/dataset/concat_db.py
def _check_classes(self): """ check input imdbs, make sure they have same classes """ try: self.classes = self.imdbs[0].classes self.num_classes = len(self.classes) except AttributeError: # fine, if no classes is provided pass if self.num_classes > 0: for db in self.imdbs: assert self.classes == db.classes, "Multiple imdb must have same classes"
def _check_classes(self): """ check input imdbs, make sure they have same classes """ try: self.classes = self.imdbs[0].classes self.num_classes = len(self.classes) except AttributeError: # fine, if no classes is provided pass if self.num_classes > 0: for db in self.imdbs: assert self.classes == db.classes, "Multiple imdb must have same classes"
[ "check", "input", "imdbs", "make", "sure", "they", "have", "same", "classes" ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/example/ssd/dataset/concat_db.py#L40-L53
[ "def", "_check_classes", "(", "self", ")", ":", "try", ":", "self", ".", "classes", "=", "self", ".", "imdbs", "[", "0", "]", ".", "classes", "self", ".", "num_classes", "=", "len", "(", "self", ".", "classes", ")", "except", "AttributeError", ":", "...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
ConcatDB._load_image_set_index
get total number of images, init indices Parameters ---------- shuffle : bool whether to shuffle the initial indices
example/ssd/dataset/concat_db.py
def _load_image_set_index(self, shuffle): """ get total number of images, init indices Parameters ---------- shuffle : bool whether to shuffle the initial indices """ self.num_images = 0 for db in self.imdbs: self.num_images += db.num_images indices = list(range(self.num_images)) if shuffle: random.shuffle(indices) return indices
def _load_image_set_index(self, shuffle): """ get total number of images, init indices Parameters ---------- shuffle : bool whether to shuffle the initial indices """ self.num_images = 0 for db in self.imdbs: self.num_images += db.num_images indices = list(range(self.num_images)) if shuffle: random.shuffle(indices) return indices
[ "get", "total", "number", "of", "images", "init", "indices" ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/example/ssd/dataset/concat_db.py#L55-L70
[ "def", "_load_image_set_index", "(", "self", ",", "shuffle", ")", ":", "self", ".", "num_images", "=", "0", "for", "db", "in", "self", ".", "imdbs", ":", "self", ".", "num_images", "+=", "db", ".", "num_images", "indices", "=", "list", "(", "range", "(...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
ConcatDB._locate_index
given index, find out sub-db and sub-index Parameters ---------- index : int index of a specific image Returns ---------- a tuple (sub-db, sub-index)
example/ssd/dataset/concat_db.py
def _locate_index(self, index): """ given index, find out sub-db and sub-index Parameters ---------- index : int index of a specific image Returns ---------- a tuple (sub-db, sub-index) """ assert index >= 0 and index < self.num_images, "index out of range" pos = self.image_set_index[index] for k, v in enumerate(self.imdbs): if pos >= v.num_images: pos -= v.num_images else: return (k, pos)
def _locate_index(self, index): """ given index, find out sub-db and sub-index Parameters ---------- index : int index of a specific image Returns ---------- a tuple (sub-db, sub-index) """ assert index >= 0 and index < self.num_images, "index out of range" pos = self.image_set_index[index] for k, v in enumerate(self.imdbs): if pos >= v.num_images: pos -= v.num_images else: return (k, pos)
[ "given", "index", "find", "out", "sub", "-", "db", "and", "sub", "-", "index" ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/example/ssd/dataset/concat_db.py#L72-L91
[ "def", "_locate_index", "(", "self", ",", "index", ")", ":", "assert", "index", ">=", "0", "and", "index", "<", "self", ".", "num_images", ",", "\"index out of range\"", "pos", "=", "self", ".", "image_set_index", "[", "index", "]", "for", "k", ",", "v",...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
ConcatDB.image_path_from_index
given image index, find out full path Parameters ---------- index: int index of a specific image Returns ---------- full path of this image
example/ssd/dataset/concat_db.py
def image_path_from_index(self, index): """ given image index, find out full path Parameters ---------- index: int index of a specific image Returns ---------- full path of this image """ assert self.image_set_index is not None, "Dataset not initialized" pos = self.image_set_index[index] n_db, n_index = self._locate_index(index) return self.imdbs[n_db].image_path_from_index(n_index)
def image_path_from_index(self, index): """ given image index, find out full path Parameters ---------- index: int index of a specific image Returns ---------- full path of this image """ assert self.image_set_index is not None, "Dataset not initialized" pos = self.image_set_index[index] n_db, n_index = self._locate_index(index) return self.imdbs[n_db].image_path_from_index(n_index)
[ "given", "image", "index", "find", "out", "full", "path" ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/example/ssd/dataset/concat_db.py#L93-L109
[ "def", "image_path_from_index", "(", "self", ",", "index", ")", ":", "assert", "self", ".", "image_set_index", "is", "not", "None", ",", "\"Dataset not initialized\"", "pos", "=", "self", ".", "image_set_index", "[", "index", "]", "n_db", ",", "n_index", "=", ...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
module_checkpoint
Callback to checkpoint Module to prefix every epoch. Parameters ---------- mod : subclass of BaseModule The module to checkpoint. prefix : str The file prefix for this checkpoint. period : int How many epochs to wait before checkpointing. Defaults to 1. save_optimizer_states : bool Indicates whether or not to save optimizer states for continued training. Returns ------- callback : function The callback function that can be passed as iter_end_callback to fit.
python/mxnet/callback.py
def module_checkpoint(mod, prefix, period=1, save_optimizer_states=False): """Callback to checkpoint Module to prefix every epoch. Parameters ---------- mod : subclass of BaseModule The module to checkpoint. prefix : str The file prefix for this checkpoint. period : int How many epochs to wait before checkpointing. Defaults to 1. save_optimizer_states : bool Indicates whether or not to save optimizer states for continued training. Returns ------- callback : function The callback function that can be passed as iter_end_callback to fit. """ period = int(max(1, period)) # pylint: disable=unused-argument def _callback(iter_no, sym=None, arg=None, aux=None): """The checkpoint function.""" if (iter_no + 1) % period == 0: mod.save_checkpoint(prefix, iter_no + 1, save_optimizer_states) return _callback
def module_checkpoint(mod, prefix, period=1, save_optimizer_states=False): """Callback to checkpoint Module to prefix every epoch. Parameters ---------- mod : subclass of BaseModule The module to checkpoint. prefix : str The file prefix for this checkpoint. period : int How many epochs to wait before checkpointing. Defaults to 1. save_optimizer_states : bool Indicates whether or not to save optimizer states for continued training. Returns ------- callback : function The callback function that can be passed as iter_end_callback to fit. """ period = int(max(1, period)) # pylint: disable=unused-argument def _callback(iter_no, sym=None, arg=None, aux=None): """The checkpoint function.""" if (iter_no + 1) % period == 0: mod.save_checkpoint(prefix, iter_no + 1, save_optimizer_states) return _callback
[ "Callback", "to", "checkpoint", "Module", "to", "prefix", "every", "epoch", "." ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/callback.py#L27-L52
[ "def", "module_checkpoint", "(", "mod", ",", "prefix", ",", "period", "=", "1", ",", "save_optimizer_states", "=", "False", ")", ":", "period", "=", "int", "(", "max", "(", "1", ",", "period", ")", ")", "# pylint: disable=unused-argument", "def", "_callback"...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
do_checkpoint
A callback that saves a model checkpoint every few epochs. Each checkpoint is made up of a couple of binary files: a model description file and a parameters (weights and biases) file. The model description file is named `prefix`--symbol.json and the parameters file is named `prefix`-`epoch_number`.params Parameters ---------- prefix : str Prefix for the checkpoint filenames. period : int, optional Interval (number of epochs) between checkpoints. Default `period` is 1. Returns ------- callback : function A callback function that can be passed as `epoch_end_callback` to fit. Example ------- >>> module.fit(iterator, num_epoch=n_epoch, ... epoch_end_callback = mx.callback.do_checkpoint("mymodel", 1)) Start training with [cpu(0)] Epoch[0] Resetting Data Iterator Epoch[0] Time cost=0.100 Saved checkpoint to "mymodel-0001.params" Epoch[1] Resetting Data Iterator Epoch[1] Time cost=0.060 Saved checkpoint to "mymodel-0002.params"
python/mxnet/callback.py
def do_checkpoint(prefix, period=1): """A callback that saves a model checkpoint every few epochs. Each checkpoint is made up of a couple of binary files: a model description file and a parameters (weights and biases) file. The model description file is named `prefix`--symbol.json and the parameters file is named `prefix`-`epoch_number`.params Parameters ---------- prefix : str Prefix for the checkpoint filenames. period : int, optional Interval (number of epochs) between checkpoints. Default `period` is 1. Returns ------- callback : function A callback function that can be passed as `epoch_end_callback` to fit. Example ------- >>> module.fit(iterator, num_epoch=n_epoch, ... epoch_end_callback = mx.callback.do_checkpoint("mymodel", 1)) Start training with [cpu(0)] Epoch[0] Resetting Data Iterator Epoch[0] Time cost=0.100 Saved checkpoint to "mymodel-0001.params" Epoch[1] Resetting Data Iterator Epoch[1] Time cost=0.060 Saved checkpoint to "mymodel-0002.params" """ period = int(max(1, period)) def _callback(iter_no, sym, arg, aux): """The checkpoint function.""" if (iter_no + 1) % period == 0: save_checkpoint(prefix, iter_no + 1, sym, arg, aux) return _callback
def do_checkpoint(prefix, period=1): """A callback that saves a model checkpoint every few epochs. Each checkpoint is made up of a couple of binary files: a model description file and a parameters (weights and biases) file. The model description file is named `prefix`--symbol.json and the parameters file is named `prefix`-`epoch_number`.params Parameters ---------- prefix : str Prefix for the checkpoint filenames. period : int, optional Interval (number of epochs) between checkpoints. Default `period` is 1. Returns ------- callback : function A callback function that can be passed as `epoch_end_callback` to fit. Example ------- >>> module.fit(iterator, num_epoch=n_epoch, ... epoch_end_callback = mx.callback.do_checkpoint("mymodel", 1)) Start training with [cpu(0)] Epoch[0] Resetting Data Iterator Epoch[0] Time cost=0.100 Saved checkpoint to "mymodel-0001.params" Epoch[1] Resetting Data Iterator Epoch[1] Time cost=0.060 Saved checkpoint to "mymodel-0002.params" """ period = int(max(1, period)) def _callback(iter_no, sym, arg, aux): """The checkpoint function.""" if (iter_no + 1) % period == 0: save_checkpoint(prefix, iter_no + 1, sym, arg, aux) return _callback
[ "A", "callback", "that", "saves", "a", "model", "checkpoint", "every", "few", "epochs", ".", "Each", "checkpoint", "is", "made", "up", "of", "a", "couple", "of", "binary", "files", ":", "a", "model", "description", "file", "and", "a", "parameters", "(", ...
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/callback.py#L55-L90
[ "def", "do_checkpoint", "(", "prefix", ",", "period", "=", "1", ")", ":", "period", "=", "int", "(", "max", "(", "1", ",", "period", ")", ")", "def", "_callback", "(", "iter_no", ",", "sym", ",", "arg", ",", "aux", ")", ":", "\"\"\"The checkpoint fun...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
log_train_metric
Callback to log the training evaluation result every period. Parameters ---------- period : int The number of batch to log the training evaluation metric. auto_reset : bool Reset the metric after each log. Returns ------- callback : function The callback function that can be passed as iter_epoch_callback to fit.
python/mxnet/callback.py
def log_train_metric(period, auto_reset=False): """Callback to log the training evaluation result every period. Parameters ---------- period : int The number of batch to log the training evaluation metric. auto_reset : bool Reset the metric after each log. Returns ------- callback : function The callback function that can be passed as iter_epoch_callback to fit. """ def _callback(param): """The checkpoint function.""" if param.nbatch % period == 0 and param.eval_metric is not None: name_value = param.eval_metric.get_name_value() for name, value in name_value: logging.info('Iter[%d] Batch[%d] Train-%s=%f', param.epoch, param.nbatch, name, value) if auto_reset: param.eval_metric.reset_local() return _callback
def log_train_metric(period, auto_reset=False): """Callback to log the training evaluation result every period. Parameters ---------- period : int The number of batch to log the training evaluation metric. auto_reset : bool Reset the metric after each log. Returns ------- callback : function The callback function that can be passed as iter_epoch_callback to fit. """ def _callback(param): """The checkpoint function.""" if param.nbatch % period == 0 and param.eval_metric is not None: name_value = param.eval_metric.get_name_value() for name, value in name_value: logging.info('Iter[%d] Batch[%d] Train-%s=%f', param.epoch, param.nbatch, name, value) if auto_reset: param.eval_metric.reset_local() return _callback
[ "Callback", "to", "log", "the", "training", "evaluation", "result", "every", "period", "." ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/callback.py#L93-L117
[ "def", "log_train_metric", "(", "period", ",", "auto_reset", "=", "False", ")", ":", "def", "_callback", "(", "param", ")", ":", "\"\"\"The checkpoint function.\"\"\"", "if", "param", ".", "nbatch", "%", "period", "==", "0", "and", "param", ".", "eval_metric",...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
Monitor.install
install callback to executor. Supports installing to multiple exes. Parameters ---------- exe : mx.executor.Executor The Executor (returned by symbol.bind) to install to.
python/mxnet/monitor.py
def install(self, exe): """install callback to executor. Supports installing to multiple exes. Parameters ---------- exe : mx.executor.Executor The Executor (returned by symbol.bind) to install to. """ exe.set_monitor_callback(self.stat_helper, self.monitor_all) self.exes.append(exe)
def install(self, exe): """install callback to executor. Supports installing to multiple exes. Parameters ---------- exe : mx.executor.Executor The Executor (returned by symbol.bind) to install to. """ exe.set_monitor_callback(self.stat_helper, self.monitor_all) self.exes.append(exe)
[ "install", "callback", "to", "executor", ".", "Supports", "installing", "to", "multiple", "exes", "." ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/monitor.py#L76-L86
[ "def", "install", "(", "self", ",", "exe", ")", ":", "exe", ".", "set_monitor_callback", "(", "self", ".", "stat_helper", ",", "self", ".", "monitor_all", ")", "self", ".", "exes", ".", "append", "(", "exe", ")" ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
Monitor.tic
Start collecting stats for current batch. Call before calling forward.
python/mxnet/monitor.py
def tic(self): """Start collecting stats for current batch. Call before calling forward.""" if self.step % self.interval == 0: for exe in self.exes: for array in exe.arg_arrays: array.wait_to_read() for array in exe.aux_arrays: array.wait_to_read() self.queue = [] self.activated = True self.step += 1
def tic(self): """Start collecting stats for current batch. Call before calling forward.""" if self.step % self.interval == 0: for exe in self.exes: for array in exe.arg_arrays: array.wait_to_read() for array in exe.aux_arrays: array.wait_to_read() self.queue = [] self.activated = True self.step += 1
[ "Start", "collecting", "stats", "for", "current", "batch", ".", "Call", "before", "calling", "forward", "." ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/monitor.py#L88-L99
[ "def", "tic", "(", "self", ")", ":", "if", "self", ".", "step", "%", "self", ".", "interval", "==", "0", ":", "for", "exe", "in", "self", ".", "exes", ":", "for", "array", "in", "exe", ".", "arg_arrays", ":", "array", ".", "wait_to_read", "(", ")...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
Monitor.toc
End collecting for current batch and return results. Call after computation of current batch. Returns ------- res : list of
python/mxnet/monitor.py
def toc(self): """End collecting for current batch and return results. Call after computation of current batch. Returns ------- res : list of """ if not self.activated: return [] for exe in self.exes: for array in exe.arg_arrays: array.wait_to_read() for array in exe.aux_arrays: array.wait_to_read() for exe in self.exes: for name, array in zip(exe._symbol.list_arguments(), exe.arg_arrays): if self.re_prog.match(name): self.queue.append((self.step, name, self.stat_func(array))) for name, array in zip(exe._symbol.list_auxiliary_states(), exe.aux_arrays): if self.re_prog.match(name): self.queue.append((self.step, name, self.stat_func(array))) self.activated = False res = [] if self.sort: self.queue.sort(key=lambda x: x[1]) for n, k, v_list in self.queue: if isinstance(v_list, NDArray): v_list = [v_list] assert isinstance(v_list, list) s = '' for v in v_list: assert isinstance(v, NDArray) if v.shape == (1,): s += str(v.asscalar()) + '\t' else: s += str(v.asnumpy()) + '\t' res.append((n, k, s)) self.queue = [] return res
def toc(self): """End collecting for current batch and return results. Call after computation of current batch. Returns ------- res : list of """ if not self.activated: return [] for exe in self.exes: for array in exe.arg_arrays: array.wait_to_read() for array in exe.aux_arrays: array.wait_to_read() for exe in self.exes: for name, array in zip(exe._symbol.list_arguments(), exe.arg_arrays): if self.re_prog.match(name): self.queue.append((self.step, name, self.stat_func(array))) for name, array in zip(exe._symbol.list_auxiliary_states(), exe.aux_arrays): if self.re_prog.match(name): self.queue.append((self.step, name, self.stat_func(array))) self.activated = False res = [] if self.sort: self.queue.sort(key=lambda x: x[1]) for n, k, v_list in self.queue: if isinstance(v_list, NDArray): v_list = [v_list] assert isinstance(v_list, list) s = '' for v in v_list: assert isinstance(v, NDArray) if v.shape == (1,): s += str(v.asscalar()) + '\t' else: s += str(v.asnumpy()) + '\t' res.append((n, k, s)) self.queue = [] return res
[ "End", "collecting", "for", "current", "batch", "and", "return", "results", ".", "Call", "after", "computation", "of", "current", "batch", "." ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/monitor.py#L102-L140
[ "def", "toc", "(", "self", ")", ":", "if", "not", "self", ".", "activated", ":", "return", "[", "]", "for", "exe", "in", "self", ".", "exes", ":", "for", "array", "in", "exe", ".", "arg_arrays", ":", "array", ".", "wait_to_read", "(", ")", "for", ...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
Monitor.toc_print
End collecting and print results.
python/mxnet/monitor.py
def toc_print(self): """End collecting and print results.""" res = self.toc() for n, k, v in res: logging.info('Batch: {:7d} {:30s} {:s}'.format(n, k, v))
def toc_print(self): """End collecting and print results.""" res = self.toc() for n, k, v in res: logging.info('Batch: {:7d} {:30s} {:s}'.format(n, k, v))
[ "End", "collecting", "and", "print", "results", "." ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/monitor.py#L142-L146
[ "def", "toc_print", "(", "self", ")", ":", "res", "=", "self", ".", "toc", "(", ")", "for", "n", ",", "k", ",", "v", "in", "res", ":", "logging", ".", "info", "(", "'Batch: {:7d} {:30s} {:s}'", ".", "format", "(", "n", ",", "k", ",", "v", ")", ...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
BucketSentenceIter.make_data_iter_plan
make a random data iteration plan
example/rnn/old/bucket_io.py
def make_data_iter_plan(self): "make a random data iteration plan" # truncate each bucket into multiple of batch-size bucket_n_batches = [] for i in range(len(self.data)): bucket_n_batches.append(np.floor((self.data[i]) / self.batch_size)) self.data[i] = self.data[i][:int(bucket_n_batches[i]*self.batch_size)] bucket_plan = np.hstack([np.zeros(n, int)+i for i, n in enumerate(bucket_n_batches)]) np.random.shuffle(bucket_plan) bucket_idx_all = [np.random.permutation(len(x)) for x in self.data] self.bucket_plan = bucket_plan self.bucket_idx_all = bucket_idx_all self.bucket_curr_idx = [0 for x in self.data] self.data_buffer = [] self.label_buffer = [] for i_bucket in range(len(self.data)): if not self.model_parallel: data = np.zeros((self.batch_size, self.buckets[i_bucket])) label = np.zeros((self.batch_size, self.buckets[i_bucket])) self.data_buffer.append(data) self.label_buffer.append(label) else: data = np.zeros((self.buckets[i_bucket], self.batch_size)) self.data_buffer.append(data) if self.model_parallel: # Transpose data if model parallel for i in range(len(self.data)): bucket_data = self.data[i] self.data[i] = np.transpose(bucket_data)
def make_data_iter_plan(self): "make a random data iteration plan" # truncate each bucket into multiple of batch-size bucket_n_batches = [] for i in range(len(self.data)): bucket_n_batches.append(np.floor((self.data[i]) / self.batch_size)) self.data[i] = self.data[i][:int(bucket_n_batches[i]*self.batch_size)] bucket_plan = np.hstack([np.zeros(n, int)+i for i, n in enumerate(bucket_n_batches)]) np.random.shuffle(bucket_plan) bucket_idx_all = [np.random.permutation(len(x)) for x in self.data] self.bucket_plan = bucket_plan self.bucket_idx_all = bucket_idx_all self.bucket_curr_idx = [0 for x in self.data] self.data_buffer = [] self.label_buffer = [] for i_bucket in range(len(self.data)): if not self.model_parallel: data = np.zeros((self.batch_size, self.buckets[i_bucket])) label = np.zeros((self.batch_size, self.buckets[i_bucket])) self.data_buffer.append(data) self.label_buffer.append(label) else: data = np.zeros((self.buckets[i_bucket], self.batch_size)) self.data_buffer.append(data) if self.model_parallel: # Transpose data if model parallel for i in range(len(self.data)): bucket_data = self.data[i] self.data[i] = np.transpose(bucket_data)
[ "make", "a", "random", "data", "iteration", "plan" ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/example/rnn/old/bucket_io.py#L200-L233
[ "def", "make_data_iter_plan", "(", "self", ")", ":", "# truncate each bucket into multiple of batch-size", "bucket_n_batches", "=", "[", "]", "for", "i", "in", "range", "(", "len", "(", "self", ".", "data", ")", ")", ":", "bucket_n_batches", ".", "append", "(", ...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
expand
Expand the pending files in the current stage. Parameters ---------- x: str The file to expand. pending : str The list of pending files to expand. stage: str The current stage for file expansion, used for matching the prefix of files.
amalgamation/amalgamation.py
def expand(x, pending, stage): """ Expand the pending files in the current stage. Parameters ---------- x: str The file to expand. pending : str The list of pending files to expand. stage: str The current stage for file expansion, used for matching the prefix of files. """ if x in history and x not in ['mshadow/mshadow/expr_scalar-inl.h']: # MULTIPLE includes return if x in pending: #print('loop found: {} in {}'.format(x, pending)) return whtspace = ' ' * expand.treeDepth expand.fileCount += 1 comment = u"//=====[{:3d}] STAGE:{:>4} {}EXPANDING: {} =====\n\n".format(expand.fileCount, stage, whtspace, x) out.write(comment.encode('ascii')) print(comment) with open(x, 'rb') as x_h: for line in x_h.readlines(): uline = line.decode('utf-8') if '#define DMLC_LOG_STACK_TRACE 1' in uline.strip(): # Do not enable stacktrace logging continue if uline.find('#include') < 0: out.write(line) continue if uline.strip().find('#include') > 0: print(uline) continue m = re1.search(uline) if not m: m = re2.search(uline) if m: path = m.groups()[0] else: m = re3.search(uline) if m: path = 'execinfo.h' else: print(uline + ' not found') continue h = path.strip('./') if "../3rdparty/" not in path else path if h.endswith('complex.h') and x.endswith('openblas_config.h'): source = '' elif h.startswith('ps/'): source = '../3rdparty/ps-lite/include/' + h else: source = find_source(h, x, stage) if not source: if (h not in blacklist and h not in sysheaders and 'mkl' not in h and 'nnpack' not in h and 'tensorrt' not in h and not h.endswith('.cuh')): sysheaders.append(h) else: expand.treeDepth += 1 expand(source, pending + [x], stage) expand.treeDepth -= 1 out.write(u"//===== EXPANDED : {} =====\n\n".format(x).encode('ascii')) history.add(x)
def expand(x, pending, stage): """ Expand the pending files in the current stage. Parameters ---------- x: str The file to expand. pending : str The list of pending files to expand. stage: str The current stage for file expansion, used for matching the prefix of files. """ if x in history and x not in ['mshadow/mshadow/expr_scalar-inl.h']: # MULTIPLE includes return if x in pending: #print('loop found: {} in {}'.format(x, pending)) return whtspace = ' ' * expand.treeDepth expand.fileCount += 1 comment = u"//=====[{:3d}] STAGE:{:>4} {}EXPANDING: {} =====\n\n".format(expand.fileCount, stage, whtspace, x) out.write(comment.encode('ascii')) print(comment) with open(x, 'rb') as x_h: for line in x_h.readlines(): uline = line.decode('utf-8') if '#define DMLC_LOG_STACK_TRACE 1' in uline.strip(): # Do not enable stacktrace logging continue if uline.find('#include') < 0: out.write(line) continue if uline.strip().find('#include') > 0: print(uline) continue m = re1.search(uline) if not m: m = re2.search(uline) if m: path = m.groups()[0] else: m = re3.search(uline) if m: path = 'execinfo.h' else: print(uline + ' not found') continue h = path.strip('./') if "../3rdparty/" not in path else path if h.endswith('complex.h') and x.endswith('openblas_config.h'): source = '' elif h.startswith('ps/'): source = '../3rdparty/ps-lite/include/' + h else: source = find_source(h, x, stage) if not source: if (h not in blacklist and h not in sysheaders and 'mkl' not in h and 'nnpack' not in h and 'tensorrt' not in h and not h.endswith('.cuh')): sysheaders.append(h) else: expand.treeDepth += 1 expand(source, pending + [x], stage) expand.treeDepth -= 1 out.write(u"//===== EXPANDED : {} =====\n\n".format(x).encode('ascii')) history.add(x)
[ "Expand", "the", "pending", "files", "in", "the", "current", "stage", "." ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/amalgamation/amalgamation.py#L112-L182
[ "def", "expand", "(", "x", ",", "pending", ",", "stage", ")", ":", "if", "x", "in", "history", "and", "x", "not", "in", "[", "'mshadow/mshadow/expr_scalar-inl.h'", "]", ":", "# MULTIPLE includes", "return", "if", "x", "in", "pending", ":", "#print('loop foun...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
get_imagenet_iterator
Dataset loader with preprocessing.
example/gluon/data.py
def get_imagenet_iterator(root, batch_size, num_workers, data_shape=224, dtype='float32'): """Dataset loader with preprocessing.""" train_dir = os.path.join(root, 'train') train_transform, val_transform = get_imagenet_transforms(data_shape, dtype) logging.info("Loading image folder %s, this may take a bit long...", train_dir) train_dataset = ImageFolderDataset(train_dir, transform=train_transform) train_data = DataLoader(train_dataset, batch_size, shuffle=True, last_batch='discard', num_workers=num_workers) val_dir = os.path.join(root, 'val') if not os.path.isdir(os.path.expanduser(os.path.join(root, 'val', 'n01440764'))): user_warning = 'Make sure validation images are stored in one subdir per category, a helper script is available at https://git.io/vNQv1' raise ValueError(user_warning) logging.info("Loading image folder %s, this may take a bit long...", val_dir) val_dataset = ImageFolderDataset(val_dir, transform=val_transform) val_data = DataLoader(val_dataset, batch_size, last_batch='keep', num_workers=num_workers) return DataLoaderIter(train_data, dtype), DataLoaderIter(val_data, dtype)
def get_imagenet_iterator(root, batch_size, num_workers, data_shape=224, dtype='float32'): """Dataset loader with preprocessing.""" train_dir = os.path.join(root, 'train') train_transform, val_transform = get_imagenet_transforms(data_shape, dtype) logging.info("Loading image folder %s, this may take a bit long...", train_dir) train_dataset = ImageFolderDataset(train_dir, transform=train_transform) train_data = DataLoader(train_dataset, batch_size, shuffle=True, last_batch='discard', num_workers=num_workers) val_dir = os.path.join(root, 'val') if not os.path.isdir(os.path.expanduser(os.path.join(root, 'val', 'n01440764'))): user_warning = 'Make sure validation images are stored in one subdir per category, a helper script is available at https://git.io/vNQv1' raise ValueError(user_warning) logging.info("Loading image folder %s, this may take a bit long...", val_dir) val_dataset = ImageFolderDataset(val_dir, transform=val_transform) val_data = DataLoader(val_dataset, batch_size, last_batch='keep', num_workers=num_workers) return DataLoaderIter(train_data, dtype), DataLoaderIter(val_data, dtype)
[ "Dataset", "loader", "with", "preprocessing", "." ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/example/gluon/data.py#L76-L91
[ "def", "get_imagenet_iterator", "(", "root", ",", "batch_size", ",", "num_workers", ",", "data_shape", "=", "224", ",", "dtype", "=", "'float32'", ")", ":", "train_dir", "=", "os", ".", "path", ".", "join", "(", "root", ",", "'train'", ")", "train_transfor...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
create
Creates an instance of token embedding. Creates a token embedding instance by loading embedding vectors from an externally hosted pre-trained token embedding file, such as those of GloVe and FastText. To get all the valid `embedding_name` and `pretrained_file_name`, use `mxnet.contrib.text.embedding.get_pretrained_file_names()`. Parameters ---------- embedding_name : str The token embedding name (case-insensitive). Returns ------- An instance of `mxnet.contrib.text.glossary._TokenEmbedding`: A token embedding instance that loads embedding vectors from an externally hosted pre-trained token embedding file.
python/mxnet/contrib/text/embedding.py
def create(embedding_name, **kwargs): """Creates an instance of token embedding. Creates a token embedding instance by loading embedding vectors from an externally hosted pre-trained token embedding file, such as those of GloVe and FastText. To get all the valid `embedding_name` and `pretrained_file_name`, use `mxnet.contrib.text.embedding.get_pretrained_file_names()`. Parameters ---------- embedding_name : str The token embedding name (case-insensitive). Returns ------- An instance of `mxnet.contrib.text.glossary._TokenEmbedding`: A token embedding instance that loads embedding vectors from an externally hosted pre-trained token embedding file. """ create_text_embedding = registry.get_create_func(_TokenEmbedding, 'token embedding') return create_text_embedding(embedding_name, **kwargs)
def create(embedding_name, **kwargs): """Creates an instance of token embedding. Creates a token embedding instance by loading embedding vectors from an externally hosted pre-trained token embedding file, such as those of GloVe and FastText. To get all the valid `embedding_name` and `pretrained_file_name`, use `mxnet.contrib.text.embedding.get_pretrained_file_names()`. Parameters ---------- embedding_name : str The token embedding name (case-insensitive). Returns ------- An instance of `mxnet.contrib.text.glossary._TokenEmbedding`: A token embedding instance that loads embedding vectors from an externally hosted pre-trained token embedding file. """ create_text_embedding = registry.get_create_func(_TokenEmbedding, 'token embedding') return create_text_embedding(embedding_name, **kwargs)
[ "Creates", "an", "instance", "of", "token", "embedding", "." ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/contrib/text/embedding.py#L63-L87
[ "def", "create", "(", "embedding_name", ",", "*", "*", "kwargs", ")", ":", "create_text_embedding", "=", "registry", ".", "get_create_func", "(", "_TokenEmbedding", ",", "'token embedding'", ")", "return", "create_text_embedding", "(", "embedding_name", ",", "*", ...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
get_pretrained_file_names
Get valid token embedding names and their pre-trained file names. To load token embedding vectors from an externally hosted pre-trained token embedding file, such as those of GloVe and FastText, one should use `mxnet.contrib.text.embedding.create(embedding_name, pretrained_file_name)`. This method returns all the valid names of `pretrained_file_name` for the specified `embedding_name`. If `embedding_name` is set to None, this method returns all the valid names of `embedding_name` with their associated `pretrained_file_name`. Parameters ---------- embedding_name : str or None, default None The pre-trained token embedding name. Returns ------- dict or list: A list of all the valid pre-trained token embedding file names (`pretrained_file_name`) for the specified token embedding name (`embedding_name`). If the text embeding name is set to None, returns a dict mapping each valid token embedding name to a list of valid pre-trained files (`pretrained_file_name`). They can be plugged into `mxnet.contrib.text.embedding.create(embedding_name, pretrained_file_name)`.
python/mxnet/contrib/text/embedding.py
def get_pretrained_file_names(embedding_name=None): """Get valid token embedding names and their pre-trained file names. To load token embedding vectors from an externally hosted pre-trained token embedding file, such as those of GloVe and FastText, one should use `mxnet.contrib.text.embedding.create(embedding_name, pretrained_file_name)`. This method returns all the valid names of `pretrained_file_name` for the specified `embedding_name`. If `embedding_name` is set to None, this method returns all the valid names of `embedding_name` with their associated `pretrained_file_name`. Parameters ---------- embedding_name : str or None, default None The pre-trained token embedding name. Returns ------- dict or list: A list of all the valid pre-trained token embedding file names (`pretrained_file_name`) for the specified token embedding name (`embedding_name`). If the text embeding name is set to None, returns a dict mapping each valid token embedding name to a list of valid pre-trained files (`pretrained_file_name`). They can be plugged into `mxnet.contrib.text.embedding.create(embedding_name, pretrained_file_name)`. """ text_embedding_reg = registry.get_registry(_TokenEmbedding) if embedding_name is not None: if embedding_name not in text_embedding_reg: raise KeyError('Cannot find `embedding_name` %s. Use ' '`get_pretrained_file_names(' 'embedding_name=None).keys()` to get all the valid embedding ' 'names.' % embedding_name) return list(text_embedding_reg[embedding_name].pretrained_file_name_sha1.keys()) else: return {embedding_name: list(embedding_cls.pretrained_file_name_sha1.keys()) for embedding_name, embedding_cls in registry.get_registry(_TokenEmbedding).items()}
def get_pretrained_file_names(embedding_name=None): """Get valid token embedding names and their pre-trained file names. To load token embedding vectors from an externally hosted pre-trained token embedding file, such as those of GloVe and FastText, one should use `mxnet.contrib.text.embedding.create(embedding_name, pretrained_file_name)`. This method returns all the valid names of `pretrained_file_name` for the specified `embedding_name`. If `embedding_name` is set to None, this method returns all the valid names of `embedding_name` with their associated `pretrained_file_name`. Parameters ---------- embedding_name : str or None, default None The pre-trained token embedding name. Returns ------- dict or list: A list of all the valid pre-trained token embedding file names (`pretrained_file_name`) for the specified token embedding name (`embedding_name`). If the text embeding name is set to None, returns a dict mapping each valid token embedding name to a list of valid pre-trained files (`pretrained_file_name`). They can be plugged into `mxnet.contrib.text.embedding.create(embedding_name, pretrained_file_name)`. """ text_embedding_reg = registry.get_registry(_TokenEmbedding) if embedding_name is not None: if embedding_name not in text_embedding_reg: raise KeyError('Cannot find `embedding_name` %s. Use ' '`get_pretrained_file_names(' 'embedding_name=None).keys()` to get all the valid embedding ' 'names.' % embedding_name) return list(text_embedding_reg[embedding_name].pretrained_file_name_sha1.keys()) else: return {embedding_name: list(embedding_cls.pretrained_file_name_sha1.keys()) for embedding_name, embedding_cls in registry.get_registry(_TokenEmbedding).items()}
[ "Get", "valid", "token", "embedding", "names", "and", "their", "pre", "-", "trained", "file", "names", "." ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/contrib/text/embedding.py#L90-L130
[ "def", "get_pretrained_file_names", "(", "embedding_name", "=", "None", ")", ":", "text_embedding_reg", "=", "registry", ".", "get_registry", "(", "_TokenEmbedding", ")", "if", "embedding_name", "is", "not", "None", ":", "if", "embedding_name", "not", "in", "text_...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
_TokenEmbedding._load_embedding
Load embedding vectors from the pre-trained token embedding file. For every unknown token, if its representation `self.unknown_token` is encountered in the pre-trained token embedding file, index 0 of `self.idx_to_vec` maps to the pre-trained token embedding vector loaded from the file; otherwise, index 0 of `self.idx_to_vec` maps to the text embedding vector initialized by `init_unknown_vec`. If a token is encountered multiple times in the pre-trained text embedding file, only the first-encountered token embedding vector will be loaded and the rest will be skipped.
python/mxnet/contrib/text/embedding.py
def _load_embedding(self, pretrained_file_path, elem_delim, init_unknown_vec, encoding='utf8'): """Load embedding vectors from the pre-trained token embedding file. For every unknown token, if its representation `self.unknown_token` is encountered in the pre-trained token embedding file, index 0 of `self.idx_to_vec` maps to the pre-trained token embedding vector loaded from the file; otherwise, index 0 of `self.idx_to_vec` maps to the text embedding vector initialized by `init_unknown_vec`. If a token is encountered multiple times in the pre-trained text embedding file, only the first-encountered token embedding vector will be loaded and the rest will be skipped. """ pretrained_file_path = os.path.expanduser(pretrained_file_path) if not os.path.isfile(pretrained_file_path): raise ValueError('`pretrained_file_path` must be a valid path to ' 'the pre-trained token embedding file.') logging.info('Loading pre-trained token embedding vectors from %s', pretrained_file_path) vec_len = None all_elems = [] tokens = set() loaded_unknown_vec = None line_num = 0 with io.open(pretrained_file_path, 'r', encoding=encoding) as f: for line in f: line_num += 1 elems = line.rstrip().split(elem_delim) assert len(elems) > 1, 'At line %d of the pre-trained text embedding file: the ' \ 'data format of the pre-trained token embedding file %s ' \ 'is unexpected.' % (line_num, pretrained_file_path) token, elems = elems[0], [float(i) for i in elems[1:]] if token == self.unknown_token and loaded_unknown_vec is None: loaded_unknown_vec = elems tokens.add(self.unknown_token) elif token in tokens: warnings.warn('At line %d of the pre-trained token embedding file: the ' 'embedding vector for token %s has been loaded and a duplicate ' 'embedding for the same token is seen and skipped.' % (line_num, token)) elif len(elems) == 1: warnings.warn('At line %d of the pre-trained text embedding file: token %s ' 'with 1-dimensional vector %s is likely a header and is ' 'skipped.' % (line_num, token, elems)) else: if vec_len is None: vec_len = len(elems) # Reserve a vector slot for the unknown token at the very beggining because # the unknown index is 0. all_elems.extend([0] * vec_len) else: assert len(elems) == vec_len, \ 'At line %d of the pre-trained token embedding file: the dimension ' \ 'of token %s is %d but the dimension of previous tokens is %d. ' \ 'Dimensions of all the tokens must be the same.' \ % (line_num, token, len(elems), vec_len) all_elems.extend(elems) self._idx_to_token.append(token) self._token_to_idx[token] = len(self._idx_to_token) - 1 tokens.add(token) self._vec_len = vec_len self._idx_to_vec = nd.array(all_elems).reshape((-1, self.vec_len)) if loaded_unknown_vec is None: self._idx_to_vec[C.UNKNOWN_IDX] = init_unknown_vec(shape=self.vec_len) else: self._idx_to_vec[C.UNKNOWN_IDX] = nd.array(loaded_unknown_vec)
def _load_embedding(self, pretrained_file_path, elem_delim, init_unknown_vec, encoding='utf8'): """Load embedding vectors from the pre-trained token embedding file. For every unknown token, if its representation `self.unknown_token` is encountered in the pre-trained token embedding file, index 0 of `self.idx_to_vec` maps to the pre-trained token embedding vector loaded from the file; otherwise, index 0 of `self.idx_to_vec` maps to the text embedding vector initialized by `init_unknown_vec`. If a token is encountered multiple times in the pre-trained text embedding file, only the first-encountered token embedding vector will be loaded and the rest will be skipped. """ pretrained_file_path = os.path.expanduser(pretrained_file_path) if not os.path.isfile(pretrained_file_path): raise ValueError('`pretrained_file_path` must be a valid path to ' 'the pre-trained token embedding file.') logging.info('Loading pre-trained token embedding vectors from %s', pretrained_file_path) vec_len = None all_elems = [] tokens = set() loaded_unknown_vec = None line_num = 0 with io.open(pretrained_file_path, 'r', encoding=encoding) as f: for line in f: line_num += 1 elems = line.rstrip().split(elem_delim) assert len(elems) > 1, 'At line %d of the pre-trained text embedding file: the ' \ 'data format of the pre-trained token embedding file %s ' \ 'is unexpected.' % (line_num, pretrained_file_path) token, elems = elems[0], [float(i) for i in elems[1:]] if token == self.unknown_token and loaded_unknown_vec is None: loaded_unknown_vec = elems tokens.add(self.unknown_token) elif token in tokens: warnings.warn('At line %d of the pre-trained token embedding file: the ' 'embedding vector for token %s has been loaded and a duplicate ' 'embedding for the same token is seen and skipped.' % (line_num, token)) elif len(elems) == 1: warnings.warn('At line %d of the pre-trained text embedding file: token %s ' 'with 1-dimensional vector %s is likely a header and is ' 'skipped.' % (line_num, token, elems)) else: if vec_len is None: vec_len = len(elems) # Reserve a vector slot for the unknown token at the very beggining because # the unknown index is 0. all_elems.extend([0] * vec_len) else: assert len(elems) == vec_len, \ 'At line %d of the pre-trained token embedding file: the dimension ' \ 'of token %s is %d but the dimension of previous tokens is %d. ' \ 'Dimensions of all the tokens must be the same.' \ % (line_num, token, len(elems), vec_len) all_elems.extend(elems) self._idx_to_token.append(token) self._token_to_idx[token] = len(self._idx_to_token) - 1 tokens.add(token) self._vec_len = vec_len self._idx_to_vec = nd.array(all_elems).reshape((-1, self.vec_len)) if loaded_unknown_vec is None: self._idx_to_vec[C.UNKNOWN_IDX] = init_unknown_vec(shape=self.vec_len) else: self._idx_to_vec[C.UNKNOWN_IDX] = nd.array(loaded_unknown_vec)
[ "Load", "embedding", "vectors", "from", "the", "pre", "-", "trained", "token", "embedding", "file", "." ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/contrib/text/embedding.py#L232-L303
[ "def", "_load_embedding", "(", "self", ",", "pretrained_file_path", ",", "elem_delim", ",", "init_unknown_vec", ",", "encoding", "=", "'utf8'", ")", ":", "pretrained_file_path", "=", "os", ".", "path", ".", "expanduser", "(", "pretrained_file_path", ")", "if", "...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
_TokenEmbedding._set_idx_to_vec_by_embeddings
Sets the mapping between token indices and token embedding vectors. Parameters ---------- token_embeddings : instance or list `mxnet.contrib.text.embedding._TokenEmbedding` One or multiple pre-trained token embeddings to load. If it is a list of multiple embeddings, these embedding vectors will be concatenated for each token. vocab_len : int Length of vocabulary whose tokens are indexed in the token embedding. vocab_idx_to_token: list of str A list of indexed tokens in the vocabulary. These tokens are indexed in the token embedding.
python/mxnet/contrib/text/embedding.py
def _set_idx_to_vec_by_embeddings(self, token_embeddings, vocab_len, vocab_idx_to_token): """Sets the mapping between token indices and token embedding vectors. Parameters ---------- token_embeddings : instance or list `mxnet.contrib.text.embedding._TokenEmbedding` One or multiple pre-trained token embeddings to load. If it is a list of multiple embeddings, these embedding vectors will be concatenated for each token. vocab_len : int Length of vocabulary whose tokens are indexed in the token embedding. vocab_idx_to_token: list of str A list of indexed tokens in the vocabulary. These tokens are indexed in the token embedding. """ new_vec_len = sum(embed.vec_len for embed in token_embeddings) new_idx_to_vec = nd.zeros(shape=(vocab_len, new_vec_len)) col_start = 0 # Concatenate all the embedding vectors in token_embeddings. for embed in token_embeddings: col_end = col_start + embed.vec_len # Cancatenate vectors of the unknown token. new_idx_to_vec[0, col_start:col_end] = embed.idx_to_vec[0] new_idx_to_vec[1:, col_start:col_end] = embed.get_vecs_by_tokens(vocab_idx_to_token[1:]) col_start = col_end self._vec_len = new_vec_len self._idx_to_vec = new_idx_to_vec
def _set_idx_to_vec_by_embeddings(self, token_embeddings, vocab_len, vocab_idx_to_token): """Sets the mapping between token indices and token embedding vectors. Parameters ---------- token_embeddings : instance or list `mxnet.contrib.text.embedding._TokenEmbedding` One or multiple pre-trained token embeddings to load. If it is a list of multiple embeddings, these embedding vectors will be concatenated for each token. vocab_len : int Length of vocabulary whose tokens are indexed in the token embedding. vocab_idx_to_token: list of str A list of indexed tokens in the vocabulary. These tokens are indexed in the token embedding. """ new_vec_len = sum(embed.vec_len for embed in token_embeddings) new_idx_to_vec = nd.zeros(shape=(vocab_len, new_vec_len)) col_start = 0 # Concatenate all the embedding vectors in token_embeddings. for embed in token_embeddings: col_end = col_start + embed.vec_len # Cancatenate vectors of the unknown token. new_idx_to_vec[0, col_start:col_end] = embed.idx_to_vec[0] new_idx_to_vec[1:, col_start:col_end] = embed.get_vecs_by_tokens(vocab_idx_to_token[1:]) col_start = col_end self._vec_len = new_vec_len self._idx_to_vec = new_idx_to_vec
[ "Sets", "the", "mapping", "between", "token", "indices", "and", "token", "embedding", "vectors", "." ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/contrib/text/embedding.py#L314-L343
[ "def", "_set_idx_to_vec_by_embeddings", "(", "self", ",", "token_embeddings", ",", "vocab_len", ",", "vocab_idx_to_token", ")", ":", "new_vec_len", "=", "sum", "(", "embed", ".", "vec_len", "for", "embed", "in", "token_embeddings", ")", "new_idx_to_vec", "=", "nd"...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
_TokenEmbedding.get_vecs_by_tokens
Look up embedding vectors of tokens. Parameters ---------- tokens : str or list of strs A token or a list of tokens. lower_case_backup : bool, default False If False, each token in the original case will be looked up; if True, each token in the original case will be looked up first, if not found in the keys of the property `token_to_idx`, the token in the lower case will be looked up. Returns ------- mxnet.ndarray.NDArray: The embedding vector(s) of the token(s). According to numpy conventions, if `tokens` is a string, returns a 1-D NDArray of shape `self.vec_len`; if `tokens` is a list of strings, returns a 2-D NDArray of shape=(len(tokens), self.vec_len).
python/mxnet/contrib/text/embedding.py
def get_vecs_by_tokens(self, tokens, lower_case_backup=False): """Look up embedding vectors of tokens. Parameters ---------- tokens : str or list of strs A token or a list of tokens. lower_case_backup : bool, default False If False, each token in the original case will be looked up; if True, each token in the original case will be looked up first, if not found in the keys of the property `token_to_idx`, the token in the lower case will be looked up. Returns ------- mxnet.ndarray.NDArray: The embedding vector(s) of the token(s). According to numpy conventions, if `tokens` is a string, returns a 1-D NDArray of shape `self.vec_len`; if `tokens` is a list of strings, returns a 2-D NDArray of shape=(len(tokens), self.vec_len). """ to_reduce = False if not isinstance(tokens, list): tokens = [tokens] to_reduce = True if not lower_case_backup: indices = [self.token_to_idx.get(token, C.UNKNOWN_IDX) for token in tokens] else: indices = [self.token_to_idx[token] if token in self.token_to_idx else self.token_to_idx.get(token.lower(), C.UNKNOWN_IDX) for token in tokens] vecs = nd.Embedding(nd.array(indices), self.idx_to_vec, self.idx_to_vec.shape[0], self.idx_to_vec.shape[1]) return vecs[0] if to_reduce else vecs
def get_vecs_by_tokens(self, tokens, lower_case_backup=False): """Look up embedding vectors of tokens. Parameters ---------- tokens : str or list of strs A token or a list of tokens. lower_case_backup : bool, default False If False, each token in the original case will be looked up; if True, each token in the original case will be looked up first, if not found in the keys of the property `token_to_idx`, the token in the lower case will be looked up. Returns ------- mxnet.ndarray.NDArray: The embedding vector(s) of the token(s). According to numpy conventions, if `tokens` is a string, returns a 1-D NDArray of shape `self.vec_len`; if `tokens` is a list of strings, returns a 2-D NDArray of shape=(len(tokens), self.vec_len). """ to_reduce = False if not isinstance(tokens, list): tokens = [tokens] to_reduce = True if not lower_case_backup: indices = [self.token_to_idx.get(token, C.UNKNOWN_IDX) for token in tokens] else: indices = [self.token_to_idx[token] if token in self.token_to_idx else self.token_to_idx.get(token.lower(), C.UNKNOWN_IDX) for token in tokens] vecs = nd.Embedding(nd.array(indices), self.idx_to_vec, self.idx_to_vec.shape[0], self.idx_to_vec.shape[1]) return vecs[0] if to_reduce else vecs
[ "Look", "up", "embedding", "vectors", "of", "tokens", "." ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/contrib/text/embedding.py#L366-L403
[ "def", "get_vecs_by_tokens", "(", "self", ",", "tokens", ",", "lower_case_backup", "=", "False", ")", ":", "to_reduce", "=", "False", "if", "not", "isinstance", "(", "tokens", ",", "list", ")", ":", "tokens", "=", "[", "tokens", "]", "to_reduce", "=", "T...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
_TokenEmbedding.update_token_vectors
Updates embedding vectors for tokens. Parameters ---------- tokens : str or a list of strs A token or a list of tokens whose embedding vector are to be updated. new_vectors : mxnet.ndarray.NDArray An NDArray to be assigned to the embedding vectors of `tokens`. Its length must be equal to the number of `tokens` and its width must be equal to the dimension of embeddings of the glossary. If `tokens` is a singleton, it must be 1-D or 2-D. If `tokens` is a list of multiple strings, it must be 2-D.
python/mxnet/contrib/text/embedding.py
def update_token_vectors(self, tokens, new_vectors): """Updates embedding vectors for tokens. Parameters ---------- tokens : str or a list of strs A token or a list of tokens whose embedding vector are to be updated. new_vectors : mxnet.ndarray.NDArray An NDArray to be assigned to the embedding vectors of `tokens`. Its length must be equal to the number of `tokens` and its width must be equal to the dimension of embeddings of the glossary. If `tokens` is a singleton, it must be 1-D or 2-D. If `tokens` is a list of multiple strings, it must be 2-D. """ assert self.idx_to_vec is not None, 'The property `idx_to_vec` has not been properly set.' if not isinstance(tokens, list) or len(tokens) == 1: assert isinstance(new_vectors, nd.NDArray) and len(new_vectors.shape) in [1, 2], \ '`new_vectors` must be a 1-D or 2-D NDArray if `tokens` is a singleton.' if not isinstance(tokens, list): tokens = [tokens] if len(new_vectors.shape) == 1: new_vectors = new_vectors.expand_dims(0) else: assert isinstance(new_vectors, nd.NDArray) and len(new_vectors.shape) == 2, \ '`new_vectors` must be a 2-D NDArray if `tokens` is a list of multiple strings.' assert new_vectors.shape == (len(tokens), self.vec_len), \ 'The length of new_vectors must be equal to the number of tokens and the width of' \ 'new_vectors must be equal to the dimension of embeddings of the glossary.' indices = [] for token in tokens: if token in self.token_to_idx: indices.append(self.token_to_idx[token]) else: raise ValueError('Token %s is unknown. To update the embedding vector for an ' 'unknown token, please specify it explicitly as the ' '`unknown_token` %s in `tokens`. This is to avoid unintended ' 'updates.' % (token, self.idx_to_token[C.UNKNOWN_IDX])) self._idx_to_vec[nd.array(indices)] = new_vectors
def update_token_vectors(self, tokens, new_vectors): """Updates embedding vectors for tokens. Parameters ---------- tokens : str or a list of strs A token or a list of tokens whose embedding vector are to be updated. new_vectors : mxnet.ndarray.NDArray An NDArray to be assigned to the embedding vectors of `tokens`. Its length must be equal to the number of `tokens` and its width must be equal to the dimension of embeddings of the glossary. If `tokens` is a singleton, it must be 1-D or 2-D. If `tokens` is a list of multiple strings, it must be 2-D. """ assert self.idx_to_vec is not None, 'The property `idx_to_vec` has not been properly set.' if not isinstance(tokens, list) or len(tokens) == 1: assert isinstance(new_vectors, nd.NDArray) and len(new_vectors.shape) in [1, 2], \ '`new_vectors` must be a 1-D or 2-D NDArray if `tokens` is a singleton.' if not isinstance(tokens, list): tokens = [tokens] if len(new_vectors.shape) == 1: new_vectors = new_vectors.expand_dims(0) else: assert isinstance(new_vectors, nd.NDArray) and len(new_vectors.shape) == 2, \ '`new_vectors` must be a 2-D NDArray if `tokens` is a list of multiple strings.' assert new_vectors.shape == (len(tokens), self.vec_len), \ 'The length of new_vectors must be equal to the number of tokens and the width of' \ 'new_vectors must be equal to the dimension of embeddings of the glossary.' indices = [] for token in tokens: if token in self.token_to_idx: indices.append(self.token_to_idx[token]) else: raise ValueError('Token %s is unknown. To update the embedding vector for an ' 'unknown token, please specify it explicitly as the ' '`unknown_token` %s in `tokens`. This is to avoid unintended ' 'updates.' % (token, self.idx_to_token[C.UNKNOWN_IDX])) self._idx_to_vec[nd.array(indices)] = new_vectors
[ "Updates", "embedding", "vectors", "for", "tokens", "." ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/contrib/text/embedding.py#L405-L447
[ "def", "update_token_vectors", "(", "self", ",", "tokens", ",", "new_vectors", ")", ":", "assert", "self", ".", "idx_to_vec", "is", "not", "None", ",", "'The property `idx_to_vec` has not been properly set.'", "if", "not", "isinstance", "(", "tokens", ",", "list", ...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
_TokenEmbedding._check_pretrained_file_names
Checks if a pre-trained token embedding file name is valid. Parameters ---------- pretrained_file_name : str The pre-trained token embedding file.
python/mxnet/contrib/text/embedding.py
def _check_pretrained_file_names(cls, pretrained_file_name): """Checks if a pre-trained token embedding file name is valid. Parameters ---------- pretrained_file_name : str The pre-trained token embedding file. """ embedding_name = cls.__name__.lower() if pretrained_file_name not in cls.pretrained_file_name_sha1: raise KeyError('Cannot find pretrained file %s for token embedding %s. Valid ' 'pretrained files for embedding %s: %s' % (pretrained_file_name, embedding_name, embedding_name, ', '.join(cls.pretrained_file_name_sha1.keys())))
def _check_pretrained_file_names(cls, pretrained_file_name): """Checks if a pre-trained token embedding file name is valid. Parameters ---------- pretrained_file_name : str The pre-trained token embedding file. """ embedding_name = cls.__name__.lower() if pretrained_file_name not in cls.pretrained_file_name_sha1: raise KeyError('Cannot find pretrained file %s for token embedding %s. Valid ' 'pretrained files for embedding %s: %s' % (pretrained_file_name, embedding_name, embedding_name, ', '.join(cls.pretrained_file_name_sha1.keys())))
[ "Checks", "if", "a", "pre", "-", "trained", "token", "embedding", "file", "name", "is", "valid", "." ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/contrib/text/embedding.py#L450-L465
[ "def", "_check_pretrained_file_names", "(", "cls", ",", "pretrained_file_name", ")", ":", "embedding_name", "=", "cls", ".", "__name__", ".", "lower", "(", ")", "if", "pretrained_file_name", "not", "in", "cls", ".", "pretrained_file_name_sha1", ":", "raise", "KeyE...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
calc_grad
Calculate gradient
example/bayesian-methods/algos.py
def calc_grad(exe, exe_grads, params, X, Y, label_name=None, outgrad_f=None): """Calculate gradient""" exe.copy_params_from(params) exe.arg_dict['data'][:] = X if outgrad_f is None: exe.arg_dict[label_name][:] = Y exe.forward(is_train=True) exe.backward() else: exe.forward(is_train=True) exe.backward(outgrad_f(exe.outpus, Y)) for k, v in exe_grads.items(): v.wait_to_read()
def calc_grad(exe, exe_grads, params, X, Y, label_name=None, outgrad_f=None): """Calculate gradient""" exe.copy_params_from(params) exe.arg_dict['data'][:] = X if outgrad_f is None: exe.arg_dict[label_name][:] = Y exe.forward(is_train=True) exe.backward() else: exe.forward(is_train=True) exe.backward(outgrad_f(exe.outpus, Y)) for k, v in exe_grads.items(): v.wait_to_read()
[ "Calculate", "gradient" ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/example/bayesian-methods/algos.py#L37-L49
[ "def", "calc_grad", "(", "exe", ",", "exe_grads", ",", "params", ",", "X", ",", "Y", ",", "label_name", "=", "None", ",", "outgrad_f", "=", "None", ")", ":", "exe", ".", "copy_params_from", "(", "params", ")", "exe", ".", "arg_dict", "[", "'data'", "...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
step_HMC
Generate the implementation of step HMC
example/bayesian-methods/algos.py
def step_HMC(exe, exe_params, exe_grads, label_key, noise_precision, prior_precision, L=10, eps=1E-6): """Generate the implementation of step HMC""" init_params = {k: v.copyto(v.context) for k, v in exe_params.items()} end_params = {k: v.copyto(v.context) for k, v in exe_params.items()} init_momentums = {k: mx.random.normal(0, 1, v.shape) for k, v in init_params.items()} end_momentums = {k: v.copyto(v.context) for k, v in init_momentums.items()} init_potential = calc_potential(exe, init_params, label_key, noise_precision, prior_precision) # 0. Calculate Initial Energy and Kinetic init_kinetic = sum([nd.sum(nd.square(momentum)) / 2.0 for momentum in init_momentums.values()]).asscalar() # 1. Make a half step for momentum at the beginning exe.copy_params_from(end_params) exe.forward(is_train=True) exe.backward() for k, v in exe_grads.items(): v.wait_to_read() for k, momentum in end_momentums.items(): momentum[:] = momentum - (eps / 2) * exe_grads[k] # 2. Alternate full steps for position and momentum for i in range(L): # 2.1 Full step for position for k, param in exe_params.items(): param[:] = param + eps * end_momentums[k] # 2.2 Full step for the momentum, except at the end of trajectory we perform a half step exe.forward(is_train=True) exe.backward() for v in exe_grads.values(): v.wait_to_read() if i != L - 1: for k, momentum in end_momentums.items(): momentum[:] = momentum - eps * exe_grads[k] else: for k, momentum in end_momentums.items(): # We should reverse the sign of the momentum at the end momentum[:] = -(momentum - eps / 2.0 * exe_grads[k]) copy_param(exe, end_params) # 3. Calculate acceptance ratio and accept/reject the move end_potential = calc_potential(exe, end_params, label_key, noise_precision, prior_precision) end_kinetic = sum([nd.sum(nd.square(momentum)) / 2.0 for momentum in end_momentums.values()]).asscalar() # print init_potential, init_kinetic, end_potential, end_kinetic r = numpy.random.rand(1) if r < numpy.exp(-(end_potential + end_kinetic) + (init_potential + init_kinetic)): exe.copy_params_from(end_params) return end_params, 1 else: exe.copy_params_from(init_params) return init_params, 0
def step_HMC(exe, exe_params, exe_grads, label_key, noise_precision, prior_precision, L=10, eps=1E-6): """Generate the implementation of step HMC""" init_params = {k: v.copyto(v.context) for k, v in exe_params.items()} end_params = {k: v.copyto(v.context) for k, v in exe_params.items()} init_momentums = {k: mx.random.normal(0, 1, v.shape) for k, v in init_params.items()} end_momentums = {k: v.copyto(v.context) for k, v in init_momentums.items()} init_potential = calc_potential(exe, init_params, label_key, noise_precision, prior_precision) # 0. Calculate Initial Energy and Kinetic init_kinetic = sum([nd.sum(nd.square(momentum)) / 2.0 for momentum in init_momentums.values()]).asscalar() # 1. Make a half step for momentum at the beginning exe.copy_params_from(end_params) exe.forward(is_train=True) exe.backward() for k, v in exe_grads.items(): v.wait_to_read() for k, momentum in end_momentums.items(): momentum[:] = momentum - (eps / 2) * exe_grads[k] # 2. Alternate full steps for position and momentum for i in range(L): # 2.1 Full step for position for k, param in exe_params.items(): param[:] = param + eps * end_momentums[k] # 2.2 Full step for the momentum, except at the end of trajectory we perform a half step exe.forward(is_train=True) exe.backward() for v in exe_grads.values(): v.wait_to_read() if i != L - 1: for k, momentum in end_momentums.items(): momentum[:] = momentum - eps * exe_grads[k] else: for k, momentum in end_momentums.items(): # We should reverse the sign of the momentum at the end momentum[:] = -(momentum - eps / 2.0 * exe_grads[k]) copy_param(exe, end_params) # 3. Calculate acceptance ratio and accept/reject the move end_potential = calc_potential(exe, end_params, label_key, noise_precision, prior_precision) end_kinetic = sum([nd.sum(nd.square(momentum)) / 2.0 for momentum in end_momentums.values()]).asscalar() # print init_potential, init_kinetic, end_potential, end_kinetic r = numpy.random.rand(1) if r < numpy.exp(-(end_potential + end_kinetic) + (init_potential + init_kinetic)): exe.copy_params_from(end_params) return end_params, 1 else: exe.copy_params_from(init_params) return init_params, 0
[ "Generate", "the", "implementation", "of", "step", "HMC" ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/example/bayesian-methods/algos.py#L52-L100
[ "def", "step_HMC", "(", "exe", ",", "exe_params", ",", "exe_grads", ",", "label_key", ",", "noise_precision", ",", "prior_precision", ",", "L", "=", "10", ",", "eps", "=", "1E-6", ")", ":", "init_params", "=", "{", "k", ":", "v", ".", "copyto", "(", ...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
train
HMC
Generate the implementation of HMC
example/bayesian-methods/algos.py
def HMC(sym, data_inputs, X, Y, X_test, Y_test, sample_num, initializer=None, noise_precision=1 / 9.0, prior_precision=0.1, learning_rate=1E-6, L=10, dev=mx.gpu()): """Generate the implementation of HMC""" label_key = list(set(data_inputs.keys()) - set(['data']))[0] exe, exe_params, exe_grads, _ = get_executor(sym, dev, data_inputs, initializer) exe.arg_dict['data'][:] = X exe.arg_dict[label_key][:] = Y sample_pool = [] accept_num = 0 start = time.time() for i in range(sample_num): sample_params, is_accept = step_HMC(exe, exe_params, exe_grads, label_key, noise_precision, prior_precision, L, learning_rate) accept_num += is_accept if (i + 1) % 10 == 0: sample_pool.append(sample_params) if (i + 1) % 100000 == 0: end = time.time() print("Current Iter Num: %d" % (i + 1), "Time Spent: %f" % (end - start), "MSE:", sample_test_regression(exe, X=X_test, Y=Y_test, sample_pool=sample_pool, minibatch_size=Y.shape[0], save_path='regression_HMC.txt')) start = time.time() exe.copy_params_from(sample_params) print('accept ratio', accept_num / float(sample_num)) return sample_pool
def HMC(sym, data_inputs, X, Y, X_test, Y_test, sample_num, initializer=None, noise_precision=1 / 9.0, prior_precision=0.1, learning_rate=1E-6, L=10, dev=mx.gpu()): """Generate the implementation of HMC""" label_key = list(set(data_inputs.keys()) - set(['data']))[0] exe, exe_params, exe_grads, _ = get_executor(sym, dev, data_inputs, initializer) exe.arg_dict['data'][:] = X exe.arg_dict[label_key][:] = Y sample_pool = [] accept_num = 0 start = time.time() for i in range(sample_num): sample_params, is_accept = step_HMC(exe, exe_params, exe_grads, label_key, noise_precision, prior_precision, L, learning_rate) accept_num += is_accept if (i + 1) % 10 == 0: sample_pool.append(sample_params) if (i + 1) % 100000 == 0: end = time.time() print("Current Iter Num: %d" % (i + 1), "Time Spent: %f" % (end - start), "MSE:", sample_test_regression(exe, X=X_test, Y=Y_test, sample_pool=sample_pool, minibatch_size=Y.shape[0], save_path='regression_HMC.txt')) start = time.time() exe.copy_params_from(sample_params) print('accept ratio', accept_num / float(sample_num)) return sample_pool
[ "Generate", "the", "implementation", "of", "HMC" ]
apache/incubator-mxnet
python
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/example/bayesian-methods/algos.py#L103-L130
[ "def", "HMC", "(", "sym", ",", "data_inputs", ",", "X", ",", "Y", ",", "X_test", ",", "Y_test", ",", "sample_num", ",", "initializer", "=", "None", ",", "noise_precision", "=", "1", "/", "9.0", ",", "prior_precision", "=", "0.1", ",", "learning_rate", ...
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7