def cntk_device(device_id): ''' Converts the legacy device ID as it was used in CNTK 1 to a :class:`~cntk.device.DeviceDescriptor` instance. Args: device_id (int): device id, -1 for CPU, 0 or higher for GPU Returns: :class:`~cntk.device.DeviceDescriptor` ''' if device_id == -1: return cpu() else: return gpu(device_id)
def test_native_fasterrcnn_eval(tmpdir, device_id): from config import cfg cfg["CNTK"].FORCE_DETERMINISTIC = True cfg["CNTK"].DEBUG_OUTPUT = False cfg["CNTK"].VISUALIZE_RESULTS = False cfg["CNTK"].FAST_MODE = True cfg["CNTK"].MAP_FILE_PATH = grocery_path from FasterRCNN import set_global_vars set_global_vars(False) if cntk_device(device_id).type() != DeviceKind_GPU: pytest.skip('test only runs on GPU') # it runs very slow in CPU try_set_default_device(cntk_device(device_id)) # since we do not use a reader for evaluation we need unzipped data externalData = 'CNTK_EXTERNAL_TESTDATA_SOURCE_DIRECTORY' in os.environ if externalData: extPath = os.environ['CNTK_EXTERNAL_TESTDATA_SOURCE_DIRECTORY'] model_file = os.path.join(extPath, "PreTrainedModels", "AlexNet", "v0", "AlexNet.model") else: model_file = os.path.join( abs_path, *"../../../../Examples/Image/PretrainedModels/AlexNet.model".split( "/")) from FasterRCNN import train_faster_rcnn_e2e, eval_faster_rcnn_mAP np.random.seed(seed=3) eval_model = train_faster_rcnn_e2e(model_file, debug_output=False) meanAP_python = eval_faster_rcnn_mAP(eval_model) cntk_py.always_allow_setting_default_device() try_set_default_device(cpu()) from native_proposal_layer import clone_with_native_proposal_layer model_with_native_pl = clone_with_native_proposal_layer(eval_model) meanAP_native = eval_faster_rcnn_mAP(model_with_native_pl) # 0.2067 (python) vs 0.2251 (native) -- the difference stems # from different sorting algorithms: quicksort in python and # heapsort in c++ (both are not stable). assert abs(meanAP_python - meanAP_native) < 0.02
def variable_value_to_seq(value, variable): ''' Convert a Value to a sequence of NumPy arrays that have their masked entries removed. Args: value (:class:`~cntk.core.Value`): Value as it is returned by Swig Returns: a list of NumPy arrays ''' mask = value.mask() if mask: value_sequences = value.unpack_variable_value(variable, cpu()) return [np.asarray(seq) for seq in value_sequences] else: return np.asarray(value)
def variable_value_to_seq(value, variable): ''' Convert a Value to a sequence of NumPy arrays that have their masked entries removed. Args: value (:class:`~cntk.core.Value`): Value as it is returned by Swig Returns: a list of NumPy arrays ''' mask = value.mask() if mask: value_sequences = value.unpack_variable_value(variable, True, cpu()) return [np.asarray(seq) for seq in value_sequences[0]] else: return np.asarray(value)
def decode_model(use_gpu=True, gpu_id=0): # use GPU or CPU according to parameters try_set_default_device(gpu(gpu_id) if use_gpu else cpu()) model_dnn = load_model("./model/speech_enhancement.model") features_file = "./test_normed.scp" feature_dim = 257 test_reader = MinibatchSource(HTKFeatureDeserializer(StreamDefs( amazing_features=StreamDef( shape=feature_dim, context=(3, 3), scp=features_file))), randomize=False, frame_mode=False) eval_input_map = {input: test_reader.streams.amazing_features} f = open(features_file) line = f.readline() while line: temp_input_path = line.split(']')[0] mb_size = temp_input_path.split(',')[-1] mb_size = int(mb_size) + 1 noisy_fea = test_reader.next_minibatch( mb_size, input_map=eval_input_map) real_noisy_fea = noisy_fea[input].data node_in_graph = model_dnn.find_by_name('irm') output_nodes = combine([node_in_graph.owner]) out_noisy_fea = output_nodes.eval(real_noisy_fea) # out_noisy_fea = as_composite(model_dnn.output1[0].owner).eval( # real_noisy_fea) out_SE_noisy_fea = np.concatenate((out_noisy_fea), axis=0) out_file_path = line.split('=')[0] out_file_name = os.path.join('./enhanced_norm_fea_mat', out_file_path) out_file_fullpath = os.path.split(out_file_name)[0] # print (out_file_fullpath) if not os.path.exists(out_file_fullpath): os.makedirs(out_file_fullpath) sio.savemat(out_file_name, {'SE': out_SE_noisy_fea}) line = f.readline() f.close()
def reenable_once_sorting_is_stable_test_native_fasterrcnn_eval(device_id): if cntk_device(device_id).type() != DeviceKind_GPU: pytest.skip('test only runs on GPU') # it runs very slow in CPU try_set_default_device(cntk_device(device_id)) from FasterRCNN_eval import compute_test_set_aps eval_model, meanAP_python, cfg = run_fasterrcnn_grocery_training(True) cntk_py.always_allow_setting_default_device() try_set_default_device(cpu()) sys.path.append(os.path.join(abs_path, "..", "..", "..", "..", "Examples", "Extensibility", "ProposalLayer")) from native_proposal_layer import clone_with_native_proposal_layer model_with_native_pl = clone_with_native_proposal_layer(eval_model) eval_results = compute_test_set_aps(model_with_native_pl, cfg) meanAP_native = np.nanmean(list(eval_results.values())) # 0.2067 (python) vs 0.2251 (native) -- the difference stems # from different sorting algorithms: quicksort in python and # heapsort in c++ (both are not stable). print("Python: {}, native: {}".format(meanAP_python, meanAP_native)) assert abs(meanAP_python - meanAP_native) < 0.1
def reenable_once_sorting_is_stable_test_native_fasterrcnn_eval(device_id): if cntk_device(device_id).type() != DeviceKind_GPU: pytest.skip('test only runs on GPU') # it runs very slow in CPU try_set_default_device(cntk_device(device_id)) from FasterRCNN_eval import compute_test_set_aps eval_model, meanAP_python, cfg = run_fasterrcnn_grocery_training(True) cntk_py.always_allow_setting_default_device() try_set_default_device(cpu()) sys.path.append( os.path.join(abs_path, "..", "..", "..", "..", "Examples", "Extensibility", "ProposalLayer")) from native_proposal_layer import clone_with_native_proposal_layer model_with_native_pl = clone_with_native_proposal_layer(eval_model) eval_results = compute_test_set_aps(model_with_native_pl, cfg) meanAP_native = np.nanmean(list(eval_results.values())) # 0.2067 (python) vs 0.2251 (native) -- the difference stems # from different sorting algorithms: quicksort in python and # heapsort in c++ (both are not stable). print("Python: {}, native: {}".format(meanAP_python, meanAP_native)) assert abs(meanAP_python - meanAP_native) < 0.1
def create(var, batch, seq_starts=None, device=None, read_only=False): ''' Creates a :class:`Value` object. Args: var (:class:`~cntk.ops.variables.Variable`): input variable into which ``batch`` is passed batch: batch input. It can be * a single NumPy array denoting the full minibatch * a list of NumPy arrays or SciPy sparse CSR matrices seq_starts (list of `bool`s or None): if None, every sequence is treated as a new sequence. Otherwise, it is interpreted as a list of Booleans that tell whether a sequence is a new sequence (`True`) or a continuation of the sequence in the same slot of the previous minibatch (`False`) device (:class:`~cntk.device.DeviceDescriptor`, default None): device this value should be put on read_only (bool, default False): whether the data is read only Returns: :class:`Value` object. ''' if isinstance(batch, np.ndarray): # The outermost axis has to be Python list. If the user passes a # full minibatch as one NumPy array, we have to convert it. if batch.dtype == object: raise ValueError( 'dtype object is not supported. If this is a batch ' 'of sequences, you need to pass them as a pure-Python list ' 'of NumPy arrays') # FIXME if not seq_starts: directly pass it to Value constructor batch = list(batch) if not isinstance(batch, list): raise ValueError('batch has to be a list of NumPy arrays or ' 'SciPy CSR matrices') list_of_ndavs = [] # NDArrayViews are all created on CPU. The Value object later then will # move it to the requested device. cpu_dev = cpu() for sample in batch: if isinstance(sample, list): sample = np.asarray(sample, dtype=var.dtype) if sample.dtype != var.dtype: raise ValueError('could not convert sample data to ' 'NumPy array') if not (isinstance(sample, np.ndarray) or sparse.issparse(sample)): raise ValueError( 'sample type "%s" is not supported. Please ' 'provide the data as a Python list of NumPy arrays ' 'or Scipy CSR matrices.' % type(sample)) if np.issubdtype(sample.dtype, int): sample = sample.astype(var.dtype) elif sample.dtype not in (np.float32, np.float64): raise ValueError( 'only integer, float32 and float64 are supported, ' 'you gave %s' % sample.dtype) else: sample = sample.astype(var.dtype) if isinstance(sample, np.ndarray): if not _is_c_contiguous(sample): raise ValueError( 'supplied data is not C contiguous; use ' 'np.ascontiguousarray (slow) or rearrange your data/computation' ) ndav = _create_NDArrayView_from_NumPy(sample, cpu_dev) elif sparse.issparse(sample): if not sparse.isspmatrix_csr(sample): raise ValueError("only CSR is supported as of now. Please " "convert your data using 'tocsr()'") ndav = cntk_py.NDArrayView(sample.shape, sample.data, sample.indptr, sample.indices, cpu_dev, False) list_of_ndavs.append(ndav) return cntk_py.Value_create(_as_tuple(var.shape), list_of_ndavs, seq_starts or [], device or use_default_device(), read_only)
def create(var, batch, seq_starts=None, device=None, read_only=False): ''' Creates a :class:`Value` object. Args: var (:class:`~cntk.ops.variables.Variable`): input variable into which ``batch`` is passed batch: batch input. It can be * a single NumPy array denoting the full minibatch * a list of NumPy arrays or SciPy sparse CSR matrices seq_starts (list of `bool`s or None): if None, every sequence is treated as a new sequence. Otherwise, it is interpreted as a list of Booleans that tell whether a sequence is a new sequence (`True`) or a continuation of the sequence in the same slot of the previous minibatch (`False`) device (:class:`~cntk.device.DeviceDescriptor`, default None): device this value should be put on read_only (bool, default False): whether the data is read only Returns: :class:`Value` object. ''' if isinstance(batch, np.ndarray): # The outermost axis has to be Python list. If the user passes a # full minibatch as one NumPy array, we have to convert it. if batch.dtype == object: raise ValueError('dtype object is not supported. If this is a batch ' 'of sequences, you need to pass them as a pure-Python list ' 'of NumPy arrays') # FIXME if not seq_starts: directly pass it to Value constructor batch = list(batch) if not isinstance(batch, list): raise ValueError('batch has to be a list of NumPy arrays or ' 'SciPy CSR matrices') list_of_ndavs = [] # NDArrayViews are all created on CPU. The Value object later then will # move it to the requested device. cpu_dev = cpu() for sample in batch: if isinstance(sample, list): sample = np.asarray(sample, dtype=var.dtype) if sample.dtype != var.dtype: raise ValueError('could not convert sample data to ' 'NumPy array') if not (isinstance(sample, np.ndarray) or sparse.issparse(sample)): raise ValueError('sample type "%s" is not supported. Please ' 'provide the data as a Python list of NumPy arrays ' 'or Scipy CSR matrices.'%type(sample)) if np.issubdtype(sample.dtype, int): sample = sample.astype(var.dtype) elif sample.dtype not in (np.float32, np.float64): raise ValueError('only integer, float32 and float64 are supported, ' 'you gave %s'%sample.dtype) if isinstance(sample, np.ndarray): if not _is_c_contiguous(sample): raise ValueError('supplied data is not C contiguous; use ' 'np.ascontiguousarray (slow) or rearrange your data/computation') ndav = _create_NDArrayView_from_NumPy(sample, cpu_dev) elif sparse.issparse(sample): if not sparse.isspmatrix_csr(sample): raise ValueError("only CSR is supported as of now. Please " "convert your data using 'tocsr()'") ndav = cntk_py.NDArrayView(sample.shape, sample.data, sample.indptr, sample.indices, cpu_dev, False) list_of_ndavs.append(ndav) return cntk_py.Value_create( _as_tuple(var.shape), list_of_ndavs, seq_starts or [], device or use_default_device(), read_only)
def create_model(X): # defines few stacked GRUs l1 = Recurrence(step_function=GRU(shape=20))(X) l2 = Recurrence(step_function=GRU(shape=20))(l1) l3 = Dense(shape=1)(l2) return l3 EPOCHS = 10 #try_set_default_device(gpu(0)) try_set_default_device(cpu()) batch_size, seq_len, input_dim = (None, None, 5) output_shape = (batch_size, seq_len, 1) input_shape = [batch_size, seq_len, input_dim] #Y = C.input_variable(shape=output_shape) X = C.sequence.input_variable(input_dim) Y = C.sequence.input_variable(1) Y_model = create_model(X) # loss function
def decode_model(features_file, irm_mat_dir, feature_dim, use_gpu=True, gpu_id=0): """Applies model to LPS features to generate ideal ratio mask. Parameters ---------- features_file : str Path to HTK script file for chunks of LPS features to be processed. irm_mat_dir : str Path to output directory for ``.mat`` files containing ideal ratio masks. feature_dim : int Feature dimensionality. Needed to parse HTK binary file containing features. use_gpu : bool, optional If True and GPU is available, perform all processing on GPU. (Default: True) gpu_id : int, optional Id of GPU on which to do computation. (Default: 0) """ if not os.path.exists(irm_mat_dir): os.makedirs(irm_mat_dir) # Load model. with wurlitzer.pipes() as (stdout, stderr): try_set_default_device(gpu(gpu_id) if use_gpu else cpu()) model_dnn = load_model(MODELF) # Compute ideal ratio masks for all chunks of LPS features specified in # the script file and save as .mat files in irm_mat_dir. with wurlitzer.pipes() as (stdout, stderr): test_reader = MinibatchSource(HTKFeatureDeserializer( StreamDefs(amazing_features=StreamDef( shape=feature_dim, context=(3, 3), scp=features_file))), randomize=False, frame_mode=False, trace_level=0) eval_input_map = {input: test_reader.streams.amazing_features} with open(features_file, 'r') as f: for line in f: # Parse line of script file to get id for chunk and location of # corresponding LPS features. Each line has the format: # # {CHUNK_ID}={PATH_TO_HTK_BIN}[{START_FRAME_INDEX},{END_FRAME_INDEX}] line = line.strip() chunk_id, htk_bin_path, start_ind, end_ind = re.match( r'(\S+)=(\S+)\[(\d+),(\d+)\]$', line).groups() start_ind = int(start_ind) end_ind = int(end_ind) mb_size = end_ind - start_ind + 1 # Determine IRM features for frames in chunk. noisy_fea = test_reader.next_minibatch(mb_size, input_map=eval_input_map) real_noisy_fea = noisy_fea[input].data node_name = b'irm' if PY2 else 'irm' node_in_graph = model_dnn.find_by_name(node_name) output_nodes = combine([node_in_graph.owner]) with wurlitzer.pipes() as (stdout, stderr): irm = output_nodes.eval(real_noisy_fea) if len(irm) == 1: irm = irm[0] else: raise Exception("Unexpected IRM shape: " + str(np.shape(irm))) # Write .mat file. sio.savemat(os.path.join(irm_mat_dir, chunk_id + '.mat'), {'IRM': irm})
def decode_model(features_file, irm_mat_dir, feature_dim, use_gpu=True, gpu_id=0, mode=1, model_select='400h', stage_select=3): """Applies model to LPS features to generate ideal ratio mask. Parameters ---------- features_file : str Path to HTK script file for chunks of LPS features to be processed. irm_mat_dir : str Path to output directory for ``.mat`` files containing ideal ratio masks. feature_dim : int Feature dimensionality. Needed to parse HTK binary file containing features. use_gpu : bool, optional If True and GPU is available, perform all processing on GPU. (Default: True) gpu_id : int, optional Id of GPU on which to do computation. (Default: 0) """ if not os.path.exists(irm_mat_dir): os.makedirs(irm_mat_dir) model_select = str(model_select) # Load model. with wurlitzer.pipes() as (stdout, stderr): try_set_default_device(gpu(gpu_id) if use_gpu else cpu()) if model_select.lower() == '400h': MODELF = os.path.join(HERE, "model", "speech_enhancement_400h.model") model_dnn = load_model(MODELF) elif model_select.lower() == '1000h': MODELF = os.path.join(HERE, "model", "speech_enhancement_1000h.model") model_dnn = load_model(MODELF) # Compute ideal ratio masks for all chunks of LPS features specified in # the script file and save as .mat files in irm_mat_dir. with wurlitzer.pipes() as (stdout, stderr): test_reader = MinibatchSource(HTKFeatureDeserializer( StreamDefs(amazing_features=StreamDef( shape=feature_dim, context=(3, 3), scp=features_file))), randomize=False, frame_mode=False, trace_level=0) eval_input_map = {input: test_reader.streams.amazing_features} with open(features_file, 'r') as f: for line in f: # Parse line of script file to get id for chunk and location of # corresponding LPS features. Each line has the format: # # {CHUNK_ID}={PATH_TO_HTK_BIN}[{START_FRAME_INDEX},{END_FRAME_INDEX}] line = line.strip() chunk_id, htk_bin_path, start_ind, end_ind = re.match( r'(\S+)=(\S+)\[(\d+),(\d+)\]$', line).groups() start_ind = int(start_ind) end_ind = int(end_ind) mb_size = end_ind - start_ind + 1 # Determine IRM features for frames in chunk. noisy_fea = test_reader.next_minibatch(mb_size, input_map=eval_input_map) real_noisy_fea = noisy_fea[input].data if model_select.lower() == '400h': node_names = [ b'irm' if PY2 else 'irm', b'lps' if PY2 else 'lps' ] elif model_select.lower() == '1000h': node_names = [ b'irm_s' + str(stage_select) if PY2 else 'irm_s' + str(stage_select), b'lps_s' + str(stage_select) if PY2 else 'lps_s' + str(stage_select) ] else: utils.error('Invalid parameter of model_select!!!!!') outputs_dict = {} for node_name in node_names: node_in_graph = model_dnn.find_by_name(node_name) output_nodes = combine([node_in_graph.owner]) with wurlitzer.pipes() as (stdout, stderr): value = output_nodes.eval(real_noisy_fea) value = np.concatenate((value), axis=0) outputs_dict[node_name] = value if model_select.lower() == '400h': sio.savemat(os.path.join(irm_mat_dir, chunk_id + '.mat'), { 'IRM': outputs_dict['irm'], 'LPS': outputs_dict['lps'] }) elif model_select.lower() == '1000h': sio.savemat( os.path.join(irm_mat_dir, chunk_id + '.mat'), { 'IRM': outputs_dict['irm_s' + str(stage_select)], 'LPS': outputs_dict['lps_s' + str(stage_select)] })