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
Booster.get_score
Get feature importance of each feature. Importance type can be defined as: * 'weight': the number of times a feature is used to split the data across all trees. * 'gain': the average gain across all splits the feature is used in. * 'cover': the average coverage across all splits the feature is used in. * 'total_gain': the total gain across all splits the feature is used in. * 'total_cover': the total coverage across all splits the feature is used in. .. note:: Feature importance is defined only for tree boosters Feature importance is only defined when the decision tree model is chosen as base learner (`booster=gbtree`). It is not defined for other base learner types, such as linear learners (`booster=gblinear`). Parameters ---------- fmap: str (optional) The name of feature map file. importance_type: str, default 'weight' One of the importance types defined above.
python-package/xgboost/core.py
def get_score(self, fmap='', importance_type='weight'): """Get feature importance of each feature. Importance type can be defined as: * 'weight': the number of times a feature is used to split the data across all trees. * 'gain': the average gain across all splits the feature is used in. * 'cover': the average coverage across all splits the feature is used in. * 'total_gain': the total gain across all splits the feature is used in. * 'total_cover': the total coverage across all splits the feature is used in. .. note:: Feature importance is defined only for tree boosters Feature importance is only defined when the decision tree model is chosen as base learner (`booster=gbtree`). It is not defined for other base learner types, such as linear learners (`booster=gblinear`). Parameters ---------- fmap: str (optional) The name of feature map file. importance_type: str, default 'weight' One of the importance types defined above. """ if getattr(self, 'booster', None) is not None and self.booster not in {'gbtree', 'dart'}: raise ValueError('Feature importance is not defined for Booster type {}' .format(self.booster)) allowed_importance_types = ['weight', 'gain', 'cover', 'total_gain', 'total_cover'] if importance_type not in allowed_importance_types: msg = ("importance_type mismatch, got '{}', expected one of " + repr(allowed_importance_types)) raise ValueError(msg.format(importance_type)) # if it's weight, then omap stores the number of missing values if importance_type == 'weight': # do a simpler tree dump to save time trees = self.get_dump(fmap, with_stats=False) fmap = {} for tree in trees: for line in tree.split('\n'): # look for the opening square bracket arr = line.split('[') # if no opening bracket (leaf node), ignore this line if len(arr) == 1: continue # extract feature name from string between [] fid = arr[1].split(']')[0].split('<')[0] if fid not in fmap: # if the feature hasn't been seen yet fmap[fid] = 1 else: fmap[fid] += 1 return fmap average_over_splits = True if importance_type == 'total_gain': importance_type = 'gain' average_over_splits = False elif importance_type == 'total_cover': importance_type = 'cover' average_over_splits = False trees = self.get_dump(fmap, with_stats=True) importance_type += '=' fmap = {} gmap = {} for tree in trees: for line in tree.split('\n'): # look for the opening square bracket arr = line.split('[') # if no opening bracket (leaf node), ignore this line if len(arr) == 1: continue # look for the closing bracket, extract only info within that bracket fid = arr[1].split(']') # extract gain or cover from string after closing bracket g = float(fid[1].split(importance_type)[1].split(',')[0]) # extract feature name from string before closing bracket fid = fid[0].split('<')[0] if fid not in fmap: # if the feature hasn't been seen yet fmap[fid] = 1 gmap[fid] = g else: fmap[fid] += 1 gmap[fid] += g # calculate average value (gain/cover) for each feature if average_over_splits: for fid in gmap: gmap[fid] = gmap[fid] / fmap[fid] return gmap
def get_score(self, fmap='', importance_type='weight'): """Get feature importance of each feature. Importance type can be defined as: * 'weight': the number of times a feature is used to split the data across all trees. * 'gain': the average gain across all splits the feature is used in. * 'cover': the average coverage across all splits the feature is used in. * 'total_gain': the total gain across all splits the feature is used in. * 'total_cover': the total coverage across all splits the feature is used in. .. note:: Feature importance is defined only for tree boosters Feature importance is only defined when the decision tree model is chosen as base learner (`booster=gbtree`). It is not defined for other base learner types, such as linear learners (`booster=gblinear`). Parameters ---------- fmap: str (optional) The name of feature map file. importance_type: str, default 'weight' One of the importance types defined above. """ if getattr(self, 'booster', None) is not None and self.booster not in {'gbtree', 'dart'}: raise ValueError('Feature importance is not defined for Booster type {}' .format(self.booster)) allowed_importance_types = ['weight', 'gain', 'cover', 'total_gain', 'total_cover'] if importance_type not in allowed_importance_types: msg = ("importance_type mismatch, got '{}', expected one of " + repr(allowed_importance_types)) raise ValueError(msg.format(importance_type)) # if it's weight, then omap stores the number of missing values if importance_type == 'weight': # do a simpler tree dump to save time trees = self.get_dump(fmap, with_stats=False) fmap = {} for tree in trees: for line in tree.split('\n'): # look for the opening square bracket arr = line.split('[') # if no opening bracket (leaf node), ignore this line if len(arr) == 1: continue # extract feature name from string between [] fid = arr[1].split(']')[0].split('<')[0] if fid not in fmap: # if the feature hasn't been seen yet fmap[fid] = 1 else: fmap[fid] += 1 return fmap average_over_splits = True if importance_type == 'total_gain': importance_type = 'gain' average_over_splits = False elif importance_type == 'total_cover': importance_type = 'cover' average_over_splits = False trees = self.get_dump(fmap, with_stats=True) importance_type += '=' fmap = {} gmap = {} for tree in trees: for line in tree.split('\n'): # look for the opening square bracket arr = line.split('[') # if no opening bracket (leaf node), ignore this line if len(arr) == 1: continue # look for the closing bracket, extract only info within that bracket fid = arr[1].split(']') # extract gain or cover from string after closing bracket g = float(fid[1].split(importance_type)[1].split(',')[0]) # extract feature name from string before closing bracket fid = fid[0].split('<')[0] if fid not in fmap: # if the feature hasn't been seen yet fmap[fid] = 1 gmap[fid] = g else: fmap[fid] += 1 gmap[fid] += g # calculate average value (gain/cover) for each feature if average_over_splits: for fid in gmap: gmap[fid] = gmap[fid] / fmap[fid] return gmap
[ "Get", "feature", "importance", "of", "each", "feature", ".", "Importance", "type", "can", "be", "defined", "as", ":" ]
dmlc/xgboost
python
https://github.com/dmlc/xgboost/blob/253fdd8a42d5ec6b819788199584d27bf9ea6253/python-package/xgboost/core.py#L1477-L1578
[ "def", "get_score", "(", "self", ",", "fmap", "=", "''", ",", "importance_type", "=", "'weight'", ")", ":", "if", "getattr", "(", "self", ",", "'booster'", ",", "None", ")", "is", "not", "None", "and", "self", ".", "booster", "not", "in", "{", "'gbtr...
253fdd8a42d5ec6b819788199584d27bf9ea6253
train
Booster.trees_to_dataframe
Parse a boosted tree model text dump into a pandas DataFrame structure. This feature is only defined when the decision tree model is chosen as base learner (`booster in {gbtree, dart}`). It is not defined for other base learner types, such as linear learners (`booster=gblinear`). Parameters ---------- fmap: str (optional) The name of feature map file.
python-package/xgboost/core.py
def trees_to_dataframe(self, fmap=''): """Parse a boosted tree model text dump into a pandas DataFrame structure. This feature is only defined when the decision tree model is chosen as base learner (`booster in {gbtree, dart}`). It is not defined for other base learner types, such as linear learners (`booster=gblinear`). Parameters ---------- fmap: str (optional) The name of feature map file. """ # pylint: disable=too-many-locals if not PANDAS_INSTALLED: raise Exception(('pandas must be available to use this method.' 'Install pandas before calling again.')) if getattr(self, 'booster', None) is not None and self.booster not in {'gbtree', 'dart'}: raise ValueError('This method is not defined for Booster type {}' .format(self.booster)) tree_ids = [] node_ids = [] fids = [] splits = [] y_directs = [] n_directs = [] missings = [] gains = [] covers = [] trees = self.get_dump(fmap, with_stats=True) for i, tree in enumerate(trees): for line in tree.split('\n'): arr = line.split('[') # Leaf node if len(arr) == 1: # Last element of line.split is an empy string if arr == ['']: continue # parse string parse = arr[0].split(':') stats = re.split('=|,', parse[1]) # append to lists tree_ids.append(i) node_ids.append(int(re.findall(r'\b\d+\b', parse[0])[0])) fids.append('Leaf') splits.append(float('NAN')) y_directs.append(float('NAN')) n_directs.append(float('NAN')) missings.append(float('NAN')) gains.append(float(stats[1])) covers.append(float(stats[3])) # Not a Leaf Node else: # parse string fid = arr[1].split(']') parse = fid[0].split('<') stats = re.split('=|,', fid[1]) # append to lists tree_ids.append(i) node_ids.append(int(re.findall(r'\b\d+\b', arr[0])[0])) fids.append(parse[0]) splits.append(float(parse[1])) str_i = str(i) y_directs.append(str_i + '-' + stats[1]) n_directs.append(str_i + '-' + stats[3]) missings.append(str_i + '-' + stats[5]) gains.append(float(stats[7])) covers.append(float(stats[9])) ids = [str(t_id) + '-' + str(n_id) for t_id, n_id in zip(tree_ids, node_ids)] df = DataFrame({'Tree': tree_ids, 'Node': node_ids, 'ID': ids, 'Feature': fids, 'Split': splits, 'Yes': y_directs, 'No': n_directs, 'Missing': missings, 'Gain': gains, 'Cover': covers}) if callable(getattr(df, 'sort_values', None)): # pylint: disable=no-member return df.sort_values(['Tree', 'Node']).reset_index(drop=True) # pylint: disable=no-member return df.sort(['Tree', 'Node']).reset_index(drop=True)
def trees_to_dataframe(self, fmap=''): """Parse a boosted tree model text dump into a pandas DataFrame structure. This feature is only defined when the decision tree model is chosen as base learner (`booster in {gbtree, dart}`). It is not defined for other base learner types, such as linear learners (`booster=gblinear`). Parameters ---------- fmap: str (optional) The name of feature map file. """ # pylint: disable=too-many-locals if not PANDAS_INSTALLED: raise Exception(('pandas must be available to use this method.' 'Install pandas before calling again.')) if getattr(self, 'booster', None) is not None and self.booster not in {'gbtree', 'dart'}: raise ValueError('This method is not defined for Booster type {}' .format(self.booster)) tree_ids = [] node_ids = [] fids = [] splits = [] y_directs = [] n_directs = [] missings = [] gains = [] covers = [] trees = self.get_dump(fmap, with_stats=True) for i, tree in enumerate(trees): for line in tree.split('\n'): arr = line.split('[') # Leaf node if len(arr) == 1: # Last element of line.split is an empy string if arr == ['']: continue # parse string parse = arr[0].split(':') stats = re.split('=|,', parse[1]) # append to lists tree_ids.append(i) node_ids.append(int(re.findall(r'\b\d+\b', parse[0])[0])) fids.append('Leaf') splits.append(float('NAN')) y_directs.append(float('NAN')) n_directs.append(float('NAN')) missings.append(float('NAN')) gains.append(float(stats[1])) covers.append(float(stats[3])) # Not a Leaf Node else: # parse string fid = arr[1].split(']') parse = fid[0].split('<') stats = re.split('=|,', fid[1]) # append to lists tree_ids.append(i) node_ids.append(int(re.findall(r'\b\d+\b', arr[0])[0])) fids.append(parse[0]) splits.append(float(parse[1])) str_i = str(i) y_directs.append(str_i + '-' + stats[1]) n_directs.append(str_i + '-' + stats[3]) missings.append(str_i + '-' + stats[5]) gains.append(float(stats[7])) covers.append(float(stats[9])) ids = [str(t_id) + '-' + str(n_id) for t_id, n_id in zip(tree_ids, node_ids)] df = DataFrame({'Tree': tree_ids, 'Node': node_ids, 'ID': ids, 'Feature': fids, 'Split': splits, 'Yes': y_directs, 'No': n_directs, 'Missing': missings, 'Gain': gains, 'Cover': covers}) if callable(getattr(df, 'sort_values', None)): # pylint: disable=no-member return df.sort_values(['Tree', 'Node']).reset_index(drop=True) # pylint: disable=no-member return df.sort(['Tree', 'Node']).reset_index(drop=True)
[ "Parse", "a", "boosted", "tree", "model", "text", "dump", "into", "a", "pandas", "DataFrame", "structure", "." ]
dmlc/xgboost
python
https://github.com/dmlc/xgboost/blob/253fdd8a42d5ec6b819788199584d27bf9ea6253/python-package/xgboost/core.py#L1580-L1663
[ "def", "trees_to_dataframe", "(", "self", ",", "fmap", "=", "''", ")", ":", "# pylint: disable=too-many-locals", "if", "not", "PANDAS_INSTALLED", ":", "raise", "Exception", "(", "(", "'pandas must be available to use this method.'", "'Install pandas before calling again.'", ...
253fdd8a42d5ec6b819788199584d27bf9ea6253
train
Booster._validate_features
Validate Booster and data's feature_names are identical. Set feature_names and feature_types from DMatrix
python-package/xgboost/core.py
def _validate_features(self, data): """ Validate Booster and data's feature_names are identical. Set feature_names and feature_types from DMatrix """ if self.feature_names is None: self.feature_names = data.feature_names self.feature_types = data.feature_types else: # Booster can't accept data with different feature names if self.feature_names != data.feature_names: dat_missing = set(self.feature_names) - set(data.feature_names) my_missing = set(data.feature_names) - set(self.feature_names) msg = 'feature_names mismatch: {0} {1}' if dat_missing: msg += ('\nexpected ' + ', '.join(str(s) for s in dat_missing) + ' in input data') if my_missing: msg += ('\ntraining data did not have the following fields: ' + ', '.join(str(s) for s in my_missing)) raise ValueError(msg.format(self.feature_names, data.feature_names))
def _validate_features(self, data): """ Validate Booster and data's feature_names are identical. Set feature_names and feature_types from DMatrix """ if self.feature_names is None: self.feature_names = data.feature_names self.feature_types = data.feature_types else: # Booster can't accept data with different feature names if self.feature_names != data.feature_names: dat_missing = set(self.feature_names) - set(data.feature_names) my_missing = set(data.feature_names) - set(self.feature_names) msg = 'feature_names mismatch: {0} {1}' if dat_missing: msg += ('\nexpected ' + ', '.join(str(s) for s in dat_missing) + ' in input data') if my_missing: msg += ('\ntraining data did not have the following fields: ' + ', '.join(str(s) for s in my_missing)) raise ValueError(msg.format(self.feature_names, data.feature_names))
[ "Validate", "Booster", "and", "data", "s", "feature_names", "are", "identical", ".", "Set", "feature_names", "and", "feature_types", "from", "DMatrix" ]
dmlc/xgboost
python
https://github.com/dmlc/xgboost/blob/253fdd8a42d5ec6b819788199584d27bf9ea6253/python-package/xgboost/core.py#L1665-L1690
[ "def", "_validate_features", "(", "self", ",", "data", ")", ":", "if", "self", ".", "feature_names", "is", "None", ":", "self", ".", "feature_names", "=", "data", ".", "feature_names", "self", ".", "feature_types", "=", "data", ".", "feature_types", "else", ...
253fdd8a42d5ec6b819788199584d27bf9ea6253
train
Booster.get_split_value_histogram
Get split value histogram of a feature Parameters ---------- feature: str The name of the feature. fmap: str (optional) The name of feature map file. bin: int, default None The maximum number of bins. Number of bins equals number of unique split values n_unique, if bins == None or bins > n_unique. as_pandas: bool, default True Return pd.DataFrame when pandas is installed. If False or pandas is not installed, return numpy ndarray. Returns ------- a histogram of used splitting values for the specified feature either as numpy array or pandas DataFrame.
python-package/xgboost/core.py
def get_split_value_histogram(self, feature, fmap='', bins=None, as_pandas=True): """Get split value histogram of a feature Parameters ---------- feature: str The name of the feature. fmap: str (optional) The name of feature map file. bin: int, default None The maximum number of bins. Number of bins equals number of unique split values n_unique, if bins == None or bins > n_unique. as_pandas: bool, default True Return pd.DataFrame when pandas is installed. If False or pandas is not installed, return numpy ndarray. Returns ------- a histogram of used splitting values for the specified feature either as numpy array or pandas DataFrame. """ xgdump = self.get_dump(fmap=fmap) values = [] regexp = re.compile(r"\[{0}<([\d.Ee+-]+)\]".format(feature)) for i, _ in enumerate(xgdump): m = re.findall(regexp, xgdump[i]) values.extend([float(x) for x in m]) n_unique = len(np.unique(values)) bins = max(min(n_unique, bins) if bins is not None else n_unique, 1) nph = np.histogram(values, bins=bins) nph = np.column_stack((nph[1][1:], nph[0])) nph = nph[nph[:, 1] > 0] if as_pandas and PANDAS_INSTALLED: return DataFrame(nph, columns=['SplitValue', 'Count']) if as_pandas and not PANDAS_INSTALLED: sys.stderr.write( "Returning histogram as ndarray (as_pandas == True, but pandas is not installed).") return nph
def get_split_value_histogram(self, feature, fmap='', bins=None, as_pandas=True): """Get split value histogram of a feature Parameters ---------- feature: str The name of the feature. fmap: str (optional) The name of feature map file. bin: int, default None The maximum number of bins. Number of bins equals number of unique split values n_unique, if bins == None or bins > n_unique. as_pandas: bool, default True Return pd.DataFrame when pandas is installed. If False or pandas is not installed, return numpy ndarray. Returns ------- a histogram of used splitting values for the specified feature either as numpy array or pandas DataFrame. """ xgdump = self.get_dump(fmap=fmap) values = [] regexp = re.compile(r"\[{0}<([\d.Ee+-]+)\]".format(feature)) for i, _ in enumerate(xgdump): m = re.findall(regexp, xgdump[i]) values.extend([float(x) for x in m]) n_unique = len(np.unique(values)) bins = max(min(n_unique, bins) if bins is not None else n_unique, 1) nph = np.histogram(values, bins=bins) nph = np.column_stack((nph[1][1:], nph[0])) nph = nph[nph[:, 1] > 0] if as_pandas and PANDAS_INSTALLED: return DataFrame(nph, columns=['SplitValue', 'Count']) if as_pandas and not PANDAS_INSTALLED: sys.stderr.write( "Returning histogram as ndarray (as_pandas == True, but pandas is not installed).") return nph
[ "Get", "split", "value", "histogram", "of", "a", "feature" ]
dmlc/xgboost
python
https://github.com/dmlc/xgboost/blob/253fdd8a42d5ec6b819788199584d27bf9ea6253/python-package/xgboost/core.py#L1692-L1733
[ "def", "get_split_value_histogram", "(", "self", ",", "feature", ",", "fmap", "=", "''", ",", "bins", "=", "None", ",", "as_pandas", "=", "True", ")", ":", "xgdump", "=", "self", ".", "get_dump", "(", "fmap", "=", "fmap", ")", "values", "=", "[", "]"...
253fdd8a42d5ec6b819788199584d27bf9ea6253
train
plot_importance
Plot importance based on fitted trees. Parameters ---------- booster : Booster, XGBModel or dict Booster or XGBModel instance, or dict taken by Booster.get_fscore() ax : matplotlib Axes, default None Target axes instance. If None, new figure and axes will be created. grid : bool, Turn the axes grids on or off. Default is True (On). importance_type : str, default "weight" How the importance is calculated: either "weight", "gain", or "cover" * "weight" is the number of times a feature appears in a tree * "gain" is the average gain of splits which use the feature * "cover" is the average coverage of splits which use the feature where coverage is defined as the number of samples affected by the split max_num_features : int, default None Maximum number of top features displayed on plot. If None, all features will be displayed. height : float, default 0.2 Bar height, passed to ax.barh() xlim : tuple, default None Tuple passed to axes.xlim() ylim : tuple, default None Tuple passed to axes.ylim() title : str, default "Feature importance" Axes title. To disable, pass None. xlabel : str, default "F score" X axis title label. To disable, pass None. ylabel : str, default "Features" Y axis title label. To disable, pass None. show_values : bool, default True Show values on plot. To disable, pass False. kwargs : Other keywords passed to ax.barh() Returns ------- ax : matplotlib Axes
python-package/xgboost/plotting.py
def plot_importance(booster, ax=None, height=0.2, xlim=None, ylim=None, title='Feature importance', xlabel='F score', ylabel='Features', importance_type='weight', max_num_features=None, grid=True, show_values=True, **kwargs): """Plot importance based on fitted trees. Parameters ---------- booster : Booster, XGBModel or dict Booster or XGBModel instance, or dict taken by Booster.get_fscore() ax : matplotlib Axes, default None Target axes instance. If None, new figure and axes will be created. grid : bool, Turn the axes grids on or off. Default is True (On). importance_type : str, default "weight" How the importance is calculated: either "weight", "gain", or "cover" * "weight" is the number of times a feature appears in a tree * "gain" is the average gain of splits which use the feature * "cover" is the average coverage of splits which use the feature where coverage is defined as the number of samples affected by the split max_num_features : int, default None Maximum number of top features displayed on plot. If None, all features will be displayed. height : float, default 0.2 Bar height, passed to ax.barh() xlim : tuple, default None Tuple passed to axes.xlim() ylim : tuple, default None Tuple passed to axes.ylim() title : str, default "Feature importance" Axes title. To disable, pass None. xlabel : str, default "F score" X axis title label. To disable, pass None. ylabel : str, default "Features" Y axis title label. To disable, pass None. show_values : bool, default True Show values on plot. To disable, pass False. kwargs : Other keywords passed to ax.barh() Returns ------- ax : matplotlib Axes """ try: import matplotlib.pyplot as plt except ImportError: raise ImportError('You must install matplotlib to plot importance') if isinstance(booster, XGBModel): importance = booster.get_booster().get_score(importance_type=importance_type) elif isinstance(booster, Booster): importance = booster.get_score(importance_type=importance_type) elif isinstance(booster, dict): importance = booster else: raise ValueError('tree must be Booster, XGBModel or dict instance') if not importance: raise ValueError('Booster.get_score() results in empty') tuples = [(k, importance[k]) for k in importance] if max_num_features is not None: # pylint: disable=invalid-unary-operand-type tuples = sorted(tuples, key=lambda x: x[1])[-max_num_features:] else: tuples = sorted(tuples, key=lambda x: x[1]) labels, values = zip(*tuples) if ax is None: _, ax = plt.subplots(1, 1) ylocs = np.arange(len(values)) ax.barh(ylocs, values, align='center', height=height, **kwargs) if show_values is True: for x, y in zip(values, ylocs): ax.text(x + 1, y, x, va='center') ax.set_yticks(ylocs) ax.set_yticklabels(labels) if xlim is not None: if not isinstance(xlim, tuple) or len(xlim) != 2: raise ValueError('xlim must be a tuple of 2 elements') else: xlim = (0, max(values) * 1.1) ax.set_xlim(xlim) if ylim is not None: if not isinstance(ylim, tuple) or len(ylim) != 2: raise ValueError('ylim must be a tuple of 2 elements') else: ylim = (-1, len(values)) ax.set_ylim(ylim) if title is not None: ax.set_title(title) if xlabel is not None: ax.set_xlabel(xlabel) if ylabel is not None: ax.set_ylabel(ylabel) ax.grid(grid) return ax
def plot_importance(booster, ax=None, height=0.2, xlim=None, ylim=None, title='Feature importance', xlabel='F score', ylabel='Features', importance_type='weight', max_num_features=None, grid=True, show_values=True, **kwargs): """Plot importance based on fitted trees. Parameters ---------- booster : Booster, XGBModel or dict Booster or XGBModel instance, or dict taken by Booster.get_fscore() ax : matplotlib Axes, default None Target axes instance. If None, new figure and axes will be created. grid : bool, Turn the axes grids on or off. Default is True (On). importance_type : str, default "weight" How the importance is calculated: either "weight", "gain", or "cover" * "weight" is the number of times a feature appears in a tree * "gain" is the average gain of splits which use the feature * "cover" is the average coverage of splits which use the feature where coverage is defined as the number of samples affected by the split max_num_features : int, default None Maximum number of top features displayed on plot. If None, all features will be displayed. height : float, default 0.2 Bar height, passed to ax.barh() xlim : tuple, default None Tuple passed to axes.xlim() ylim : tuple, default None Tuple passed to axes.ylim() title : str, default "Feature importance" Axes title. To disable, pass None. xlabel : str, default "F score" X axis title label. To disable, pass None. ylabel : str, default "Features" Y axis title label. To disable, pass None. show_values : bool, default True Show values on plot. To disable, pass False. kwargs : Other keywords passed to ax.barh() Returns ------- ax : matplotlib Axes """ try: import matplotlib.pyplot as plt except ImportError: raise ImportError('You must install matplotlib to plot importance') if isinstance(booster, XGBModel): importance = booster.get_booster().get_score(importance_type=importance_type) elif isinstance(booster, Booster): importance = booster.get_score(importance_type=importance_type) elif isinstance(booster, dict): importance = booster else: raise ValueError('tree must be Booster, XGBModel or dict instance') if not importance: raise ValueError('Booster.get_score() results in empty') tuples = [(k, importance[k]) for k in importance] if max_num_features is not None: # pylint: disable=invalid-unary-operand-type tuples = sorted(tuples, key=lambda x: x[1])[-max_num_features:] else: tuples = sorted(tuples, key=lambda x: x[1]) labels, values = zip(*tuples) if ax is None: _, ax = plt.subplots(1, 1) ylocs = np.arange(len(values)) ax.barh(ylocs, values, align='center', height=height, **kwargs) if show_values is True: for x, y in zip(values, ylocs): ax.text(x + 1, y, x, va='center') ax.set_yticks(ylocs) ax.set_yticklabels(labels) if xlim is not None: if not isinstance(xlim, tuple) or len(xlim) != 2: raise ValueError('xlim must be a tuple of 2 elements') else: xlim = (0, max(values) * 1.1) ax.set_xlim(xlim) if ylim is not None: if not isinstance(ylim, tuple) or len(ylim) != 2: raise ValueError('ylim must be a tuple of 2 elements') else: ylim = (-1, len(values)) ax.set_ylim(ylim) if title is not None: ax.set_title(title) if xlabel is not None: ax.set_xlabel(xlabel) if ylabel is not None: ax.set_ylabel(ylabel) ax.grid(grid) return ax
[ "Plot", "importance", "based", "on", "fitted", "trees", "." ]
dmlc/xgboost
python
https://github.com/dmlc/xgboost/blob/253fdd8a42d5ec6b819788199584d27bf9ea6253/python-package/xgboost/plotting.py#L14-L117
[ "def", "plot_importance", "(", "booster", ",", "ax", "=", "None", ",", "height", "=", "0.2", ",", "xlim", "=", "None", ",", "ylim", "=", "None", ",", "title", "=", "'Feature importance'", ",", "xlabel", "=", "'F score'", ",", "ylabel", "=", "'Features'",...
253fdd8a42d5ec6b819788199584d27bf9ea6253
train
_parse_node
parse dumped node
python-package/xgboost/plotting.py
def _parse_node(graph, text, condition_node_params, leaf_node_params): """parse dumped node""" match = _NODEPAT.match(text) if match is not None: node = match.group(1) graph.node(node, label=match.group(2), **condition_node_params) return node match = _LEAFPAT.match(text) if match is not None: node = match.group(1) graph.node(node, label=match.group(2), **leaf_node_params) return node raise ValueError('Unable to parse node: {0}'.format(text))
def _parse_node(graph, text, condition_node_params, leaf_node_params): """parse dumped node""" match = _NODEPAT.match(text) if match is not None: node = match.group(1) graph.node(node, label=match.group(2), **condition_node_params) return node match = _LEAFPAT.match(text) if match is not None: node = match.group(1) graph.node(node, label=match.group(2), **leaf_node_params) return node raise ValueError('Unable to parse node: {0}'.format(text))
[ "parse", "dumped", "node" ]
dmlc/xgboost
python
https://github.com/dmlc/xgboost/blob/253fdd8a42d5ec6b819788199584d27bf9ea6253/python-package/xgboost/plotting.py#L126-L138
[ "def", "_parse_node", "(", "graph", ",", "text", ",", "condition_node_params", ",", "leaf_node_params", ")", ":", "match", "=", "_NODEPAT", ".", "match", "(", "text", ")", "if", "match", "is", "not", "None", ":", "node", "=", "match", ".", "group", "(", ...
253fdd8a42d5ec6b819788199584d27bf9ea6253
train
_parse_edge
parse dumped edge
python-package/xgboost/plotting.py
def _parse_edge(graph, node, text, yes_color='#0000FF', no_color='#FF0000'): """parse dumped edge""" try: match = _EDGEPAT.match(text) if match is not None: yes, no, missing = match.groups() if yes == missing: graph.edge(node, yes, label='yes, missing', color=yes_color) graph.edge(node, no, label='no', color=no_color) else: graph.edge(node, yes, label='yes', color=yes_color) graph.edge(node, no, label='no, missing', color=no_color) return except ValueError: pass match = _EDGEPAT2.match(text) if match is not None: yes, no = match.groups() graph.edge(node, yes, label='yes', color=yes_color) graph.edge(node, no, label='no', color=no_color) return raise ValueError('Unable to parse edge: {0}'.format(text))
def _parse_edge(graph, node, text, yes_color='#0000FF', no_color='#FF0000'): """parse dumped edge""" try: match = _EDGEPAT.match(text) if match is not None: yes, no, missing = match.groups() if yes == missing: graph.edge(node, yes, label='yes, missing', color=yes_color) graph.edge(node, no, label='no', color=no_color) else: graph.edge(node, yes, label='yes', color=yes_color) graph.edge(node, no, label='no, missing', color=no_color) return except ValueError: pass match = _EDGEPAT2.match(text) if match is not None: yes, no = match.groups() graph.edge(node, yes, label='yes', color=yes_color) graph.edge(node, no, label='no', color=no_color) return raise ValueError('Unable to parse edge: {0}'.format(text))
[ "parse", "dumped", "edge" ]
dmlc/xgboost
python
https://github.com/dmlc/xgboost/blob/253fdd8a42d5ec6b819788199584d27bf9ea6253/python-package/xgboost/plotting.py#L141-L162
[ "def", "_parse_edge", "(", "graph", ",", "node", ",", "text", ",", "yes_color", "=", "'#0000FF'", ",", "no_color", "=", "'#FF0000'", ")", ":", "try", ":", "match", "=", "_EDGEPAT", ".", "match", "(", "text", ")", "if", "match", "is", "not", "None", "...
253fdd8a42d5ec6b819788199584d27bf9ea6253
train
to_graphviz
Convert specified tree to graphviz instance. IPython can automatically plot the returned graphiz instance. Otherwise, you should call .render() method of the returned graphiz instance. Parameters ---------- booster : Booster, XGBModel Booster or XGBModel instance fmap: str (optional) The name of feature map file num_trees : int, default 0 Specify the ordinal number of target tree rankdir : str, default "UT" Passed to graphiz via graph_attr yes_color : str, default '#0000FF' Edge color when meets the node condition. no_color : str, default '#FF0000' Edge color when doesn't meet the node condition. condition_node_params : dict (optional) condition node configuration, {'shape':'box', 'style':'filled,rounded', 'fillcolor':'#78bceb' } leaf_node_params : dict (optional) leaf node configuration {'shape':'box', 'style':'filled', 'fillcolor':'#e48038' } kwargs : Other keywords passed to graphviz graph_attr Returns ------- ax : matplotlib Axes
python-package/xgboost/plotting.py
def to_graphviz(booster, fmap='', num_trees=0, rankdir='UT', yes_color='#0000FF', no_color='#FF0000', condition_node_params=None, leaf_node_params=None, **kwargs): """Convert specified tree to graphviz instance. IPython can automatically plot the returned graphiz instance. Otherwise, you should call .render() method of the returned graphiz instance. Parameters ---------- booster : Booster, XGBModel Booster or XGBModel instance fmap: str (optional) The name of feature map file num_trees : int, default 0 Specify the ordinal number of target tree rankdir : str, default "UT" Passed to graphiz via graph_attr yes_color : str, default '#0000FF' Edge color when meets the node condition. no_color : str, default '#FF0000' Edge color when doesn't meet the node condition. condition_node_params : dict (optional) condition node configuration, {'shape':'box', 'style':'filled,rounded', 'fillcolor':'#78bceb' } leaf_node_params : dict (optional) leaf node configuration {'shape':'box', 'style':'filled', 'fillcolor':'#e48038' } kwargs : Other keywords passed to graphviz graph_attr Returns ------- ax : matplotlib Axes """ if condition_node_params is None: condition_node_params = {} if leaf_node_params is None: leaf_node_params = {} try: from graphviz import Digraph except ImportError: raise ImportError('You must install graphviz to plot tree') if not isinstance(booster, (Booster, XGBModel)): raise ValueError('booster must be Booster or XGBModel instance') if isinstance(booster, XGBModel): booster = booster.get_booster() tree = booster.get_dump(fmap=fmap)[num_trees] tree = tree.split() kwargs = kwargs.copy() kwargs.update({'rankdir': rankdir}) graph = Digraph(graph_attr=kwargs) for i, text in enumerate(tree): if text[0].isdigit(): node = _parse_node( graph, text, condition_node_params=condition_node_params, leaf_node_params=leaf_node_params) else: if i == 0: # 1st string must be node raise ValueError('Unable to parse given string as tree') _parse_edge(graph, node, text, yes_color=yes_color, no_color=no_color) return graph
def to_graphviz(booster, fmap='', num_trees=0, rankdir='UT', yes_color='#0000FF', no_color='#FF0000', condition_node_params=None, leaf_node_params=None, **kwargs): """Convert specified tree to graphviz instance. IPython can automatically plot the returned graphiz instance. Otherwise, you should call .render() method of the returned graphiz instance. Parameters ---------- booster : Booster, XGBModel Booster or XGBModel instance fmap: str (optional) The name of feature map file num_trees : int, default 0 Specify the ordinal number of target tree rankdir : str, default "UT" Passed to graphiz via graph_attr yes_color : str, default '#0000FF' Edge color when meets the node condition. no_color : str, default '#FF0000' Edge color when doesn't meet the node condition. condition_node_params : dict (optional) condition node configuration, {'shape':'box', 'style':'filled,rounded', 'fillcolor':'#78bceb' } leaf_node_params : dict (optional) leaf node configuration {'shape':'box', 'style':'filled', 'fillcolor':'#e48038' } kwargs : Other keywords passed to graphviz graph_attr Returns ------- ax : matplotlib Axes """ if condition_node_params is None: condition_node_params = {} if leaf_node_params is None: leaf_node_params = {} try: from graphviz import Digraph except ImportError: raise ImportError('You must install graphviz to plot tree') if not isinstance(booster, (Booster, XGBModel)): raise ValueError('booster must be Booster or XGBModel instance') if isinstance(booster, XGBModel): booster = booster.get_booster() tree = booster.get_dump(fmap=fmap)[num_trees] tree = tree.split() kwargs = kwargs.copy() kwargs.update({'rankdir': rankdir}) graph = Digraph(graph_attr=kwargs) for i, text in enumerate(tree): if text[0].isdigit(): node = _parse_node( graph, text, condition_node_params=condition_node_params, leaf_node_params=leaf_node_params) else: if i == 0: # 1st string must be node raise ValueError('Unable to parse given string as tree') _parse_edge(graph, node, text, yes_color=yes_color, no_color=no_color) return graph
[ "Convert", "specified", "tree", "to", "graphviz", "instance", ".", "IPython", "can", "automatically", "plot", "the", "returned", "graphiz", "instance", ".", "Otherwise", "you", "should", "call", ".", "render", "()", "method", "of", "the", "returned", "graphiz", ...
dmlc/xgboost
python
https://github.com/dmlc/xgboost/blob/253fdd8a42d5ec6b819788199584d27bf9ea6253/python-package/xgboost/plotting.py#L165-L241
[ "def", "to_graphviz", "(", "booster", ",", "fmap", "=", "''", ",", "num_trees", "=", "0", ",", "rankdir", "=", "'UT'", ",", "yes_color", "=", "'#0000FF'", ",", "no_color", "=", "'#FF0000'", ",", "condition_node_params", "=", "None", ",", "leaf_node_params", ...
253fdd8a42d5ec6b819788199584d27bf9ea6253
train
newAction
Create a new action and assign callbacks, shortcuts, etc.
libs/utils.py
def newAction(parent, text, slot=None, shortcut=None, icon=None, tip=None, checkable=False, enabled=True): """Create a new action and assign callbacks, shortcuts, etc.""" a = QAction(text, parent) if icon is not None: a.setIcon(newIcon(icon)) if shortcut is not None: if isinstance(shortcut, (list, tuple)): a.setShortcuts(shortcut) else: a.setShortcut(shortcut) if tip is not None: a.setToolTip(tip) a.setStatusTip(tip) if slot is not None: a.triggered.connect(slot) if checkable: a.setCheckable(True) a.setEnabled(enabled) return a
def newAction(parent, text, slot=None, shortcut=None, icon=None, tip=None, checkable=False, enabled=True): """Create a new action and assign callbacks, shortcuts, etc.""" a = QAction(text, parent) if icon is not None: a.setIcon(newIcon(icon)) if shortcut is not None: if isinstance(shortcut, (list, tuple)): a.setShortcuts(shortcut) else: a.setShortcut(shortcut) if tip is not None: a.setToolTip(tip) a.setStatusTip(tip) if slot is not None: a.triggered.connect(slot) if checkable: a.setCheckable(True) a.setEnabled(enabled) return a
[ "Create", "a", "new", "action", "and", "assign", "callbacks", "shortcuts", "etc", "." ]
tzutalin/labelImg
python
https://github.com/tzutalin/labelImg/blob/6afd15aa88f89f41254e0004ed219b3965eb2c0d/libs/utils.py#L29-L48
[ "def", "newAction", "(", "parent", ",", "text", ",", "slot", "=", "None", ",", "shortcut", "=", "None", ",", "icon", "=", "None", ",", "tip", "=", "None", ",", "checkable", "=", "False", ",", "enabled", "=", "True", ")", ":", "a", "=", "QAction", ...
6afd15aa88f89f41254e0004ed219b3965eb2c0d
train
natural_sort
Sort the list into natural alphanumeric order.
libs/utils.py
def natural_sort(list, key=lambda s:s): """ Sort the list into natural alphanumeric order. """ def get_alphanum_key_func(key): convert = lambda text: int(text) if text.isdigit() else text return lambda s: [convert(c) for c in re.split('([0-9]+)', key(s))] sort_key = get_alphanum_key_func(key) list.sort(key=sort_key)
def natural_sort(list, key=lambda s:s): """ Sort the list into natural alphanumeric order. """ def get_alphanum_key_func(key): convert = lambda text: int(text) if text.isdigit() else text return lambda s: [convert(c) for c in re.split('([0-9]+)', key(s))] sort_key = get_alphanum_key_func(key) list.sort(key=sort_key)
[ "Sort", "the", "list", "into", "natural", "alphanumeric", "order", "." ]
tzutalin/labelImg
python
https://github.com/tzutalin/labelImg/blob/6afd15aa88f89f41254e0004ed219b3965eb2c0d/libs/utils.py#L95-L103
[ "def", "natural_sort", "(", "list", ",", "key", "=", "lambda", "s", ":", "s", ")", ":", "def", "get_alphanum_key_func", "(", "key", ")", ":", "convert", "=", "lambda", "text", ":", "int", "(", "text", ")", "if", "text", ".", "isdigit", "(", ")", "e...
6afd15aa88f89f41254e0004ed219b3965eb2c0d
train
Canvas.mouseMoveEvent
Update line with last point and current coordinates.
libs/canvas.py
def mouseMoveEvent(self, ev): """Update line with last point and current coordinates.""" pos = self.transformPos(ev.pos()) # Update coordinates in status bar if image is opened window = self.parent().window() if window.filePath is not None: self.parent().window().labelCoordinates.setText( 'X: %d; Y: %d' % (pos.x(), pos.y())) # Polygon drawing. if self.drawing(): self.overrideCursor(CURSOR_DRAW) if self.current: color = self.drawingLineColor if self.outOfPixmap(pos): # Don't allow the user to draw outside the pixmap. # Project the point to the pixmap's edges. pos = self.intersectionPoint(self.current[-1], pos) elif len(self.current) > 1 and self.closeEnough(pos, self.current[0]): # Attract line to starting point and colorise to alert the # user: pos = self.current[0] color = self.current.line_color self.overrideCursor(CURSOR_POINT) self.current.highlightVertex(0, Shape.NEAR_VERTEX) if self.drawSquare: initPos = self.current[0] minX = initPos.x() minY = initPos.y() min_size = min(abs(pos.x() - minX), abs(pos.y() - minY)) directionX = -1 if pos.x() - minX < 0 else 1 directionY = -1 if pos.y() - minY < 0 else 1 self.line[1] = QPointF(minX + directionX * min_size, minY + directionY * min_size) else: self.line[1] = pos self.line.line_color = color self.prevPoint = QPointF() self.current.highlightClear() else: self.prevPoint = pos self.repaint() return # Polygon copy moving. if Qt.RightButton & ev.buttons(): if self.selectedShapeCopy and self.prevPoint: self.overrideCursor(CURSOR_MOVE) self.boundedMoveShape(self.selectedShapeCopy, pos) self.repaint() elif self.selectedShape: self.selectedShapeCopy = self.selectedShape.copy() self.repaint() return # Polygon/Vertex moving. if Qt.LeftButton & ev.buttons(): if self.selectedVertex(): self.boundedMoveVertex(pos) self.shapeMoved.emit() self.repaint() elif self.selectedShape and self.prevPoint: self.overrideCursor(CURSOR_MOVE) self.boundedMoveShape(self.selectedShape, pos) self.shapeMoved.emit() self.repaint() return # Just hovering over the canvas, 2 posibilities: # - Highlight shapes # - Highlight vertex # Update shape/vertex fill and tooltip value accordingly. self.setToolTip("Image") for shape in reversed([s for s in self.shapes if self.isVisible(s)]): # Look for a nearby vertex to highlight. If that fails, # check if we happen to be inside a shape. index = shape.nearestVertex(pos, self.epsilon) if index is not None: if self.selectedVertex(): self.hShape.highlightClear() self.hVertex, self.hShape = index, shape shape.highlightVertex(index, shape.MOVE_VERTEX) self.overrideCursor(CURSOR_POINT) self.setToolTip("Click & drag to move point") self.setStatusTip(self.toolTip()) self.update() break elif shape.containsPoint(pos): if self.selectedVertex(): self.hShape.highlightClear() self.hVertex, self.hShape = None, shape self.setToolTip( "Click & drag to move shape '%s'" % shape.label) self.setStatusTip(self.toolTip()) self.overrideCursor(CURSOR_GRAB) self.update() break else: # Nothing found, clear highlights, reset state. if self.hShape: self.hShape.highlightClear() self.update() self.hVertex, self.hShape = None, None self.overrideCursor(CURSOR_DEFAULT)
def mouseMoveEvent(self, ev): """Update line with last point and current coordinates.""" pos = self.transformPos(ev.pos()) # Update coordinates in status bar if image is opened window = self.parent().window() if window.filePath is not None: self.parent().window().labelCoordinates.setText( 'X: %d; Y: %d' % (pos.x(), pos.y())) # Polygon drawing. if self.drawing(): self.overrideCursor(CURSOR_DRAW) if self.current: color = self.drawingLineColor if self.outOfPixmap(pos): # Don't allow the user to draw outside the pixmap. # Project the point to the pixmap's edges. pos = self.intersectionPoint(self.current[-1], pos) elif len(self.current) > 1 and self.closeEnough(pos, self.current[0]): # Attract line to starting point and colorise to alert the # user: pos = self.current[0] color = self.current.line_color self.overrideCursor(CURSOR_POINT) self.current.highlightVertex(0, Shape.NEAR_VERTEX) if self.drawSquare: initPos = self.current[0] minX = initPos.x() minY = initPos.y() min_size = min(abs(pos.x() - minX), abs(pos.y() - minY)) directionX = -1 if pos.x() - minX < 0 else 1 directionY = -1 if pos.y() - minY < 0 else 1 self.line[1] = QPointF(minX + directionX * min_size, minY + directionY * min_size) else: self.line[1] = pos self.line.line_color = color self.prevPoint = QPointF() self.current.highlightClear() else: self.prevPoint = pos self.repaint() return # Polygon copy moving. if Qt.RightButton & ev.buttons(): if self.selectedShapeCopy and self.prevPoint: self.overrideCursor(CURSOR_MOVE) self.boundedMoveShape(self.selectedShapeCopy, pos) self.repaint() elif self.selectedShape: self.selectedShapeCopy = self.selectedShape.copy() self.repaint() return # Polygon/Vertex moving. if Qt.LeftButton & ev.buttons(): if self.selectedVertex(): self.boundedMoveVertex(pos) self.shapeMoved.emit() self.repaint() elif self.selectedShape and self.prevPoint: self.overrideCursor(CURSOR_MOVE) self.boundedMoveShape(self.selectedShape, pos) self.shapeMoved.emit() self.repaint() return # Just hovering over the canvas, 2 posibilities: # - Highlight shapes # - Highlight vertex # Update shape/vertex fill and tooltip value accordingly. self.setToolTip("Image") for shape in reversed([s for s in self.shapes if self.isVisible(s)]): # Look for a nearby vertex to highlight. If that fails, # check if we happen to be inside a shape. index = shape.nearestVertex(pos, self.epsilon) if index is not None: if self.selectedVertex(): self.hShape.highlightClear() self.hVertex, self.hShape = index, shape shape.highlightVertex(index, shape.MOVE_VERTEX) self.overrideCursor(CURSOR_POINT) self.setToolTip("Click & drag to move point") self.setStatusTip(self.toolTip()) self.update() break elif shape.containsPoint(pos): if self.selectedVertex(): self.hShape.highlightClear() self.hVertex, self.hShape = None, shape self.setToolTip( "Click & drag to move shape '%s'" % shape.label) self.setStatusTip(self.toolTip()) self.overrideCursor(CURSOR_GRAB) self.update() break else: # Nothing found, clear highlights, reset state. if self.hShape: self.hShape.highlightClear() self.update() self.hVertex, self.hShape = None, None self.overrideCursor(CURSOR_DEFAULT)
[ "Update", "line", "with", "last", "point", "and", "current", "coordinates", "." ]
tzutalin/labelImg
python
https://github.com/tzutalin/labelImg/blob/6afd15aa88f89f41254e0004ed219b3965eb2c0d/libs/canvas.py#L104-L208
[ "def", "mouseMoveEvent", "(", "self", ",", "ev", ")", ":", "pos", "=", "self", ".", "transformPos", "(", "ev", ".", "pos", "(", ")", ")", "# Update coordinates in status bar if image is opened", "window", "=", "self", ".", "parent", "(", ")", ".", "window", ...
6afd15aa88f89f41254e0004ed219b3965eb2c0d
train
Canvas.selectShapePoint
Select the first shape created which contains this point.
libs/canvas.py
def selectShapePoint(self, point): """Select the first shape created which contains this point.""" self.deSelectShape() if self.selectedVertex(): # A vertex is marked for selection. index, shape = self.hVertex, self.hShape shape.highlightVertex(index, shape.MOVE_VERTEX) self.selectShape(shape) return for shape in reversed(self.shapes): if self.isVisible(shape) and shape.containsPoint(point): self.selectShape(shape) self.calculateOffsets(shape, point) return
def selectShapePoint(self, point): """Select the first shape created which contains this point.""" self.deSelectShape() if self.selectedVertex(): # A vertex is marked for selection. index, shape = self.hVertex, self.hShape shape.highlightVertex(index, shape.MOVE_VERTEX) self.selectShape(shape) return for shape in reversed(self.shapes): if self.isVisible(shape) and shape.containsPoint(point): self.selectShape(shape) self.calculateOffsets(shape, point) return
[ "Select", "the", "first", "shape", "created", "which", "contains", "this", "point", "." ]
tzutalin/labelImg
python
https://github.com/tzutalin/labelImg/blob/6afd15aa88f89f41254e0004ed219b3965eb2c0d/libs/canvas.py#L307-L319
[ "def", "selectShapePoint", "(", "self", ",", "point", ")", ":", "self", ".", "deSelectShape", "(", ")", "if", "self", ".", "selectedVertex", "(", ")", ":", "# A vertex is marked for selection.", "index", ",", "shape", "=", "self", ".", "hVertex", ",", "self"...
6afd15aa88f89f41254e0004ed219b3965eb2c0d
train
Canvas.snapPointToCanvas
Moves a point x,y to within the boundaries of the canvas. :return: (x,y,snapped) where snapped is True if x or y were changed, False if not.
libs/canvas.py
def snapPointToCanvas(self, x, y): """ Moves a point x,y to within the boundaries of the canvas. :return: (x,y,snapped) where snapped is True if x or y were changed, False if not. """ if x < 0 or x > self.pixmap.width() or y < 0 or y > self.pixmap.height(): x = max(x, 0) y = max(y, 0) x = min(x, self.pixmap.width()) y = min(y, self.pixmap.height()) return x, y, True return x, y, False
def snapPointToCanvas(self, x, y): """ Moves a point x,y to within the boundaries of the canvas. :return: (x,y,snapped) where snapped is True if x or y were changed, False if not. """ if x < 0 or x > self.pixmap.width() or y < 0 or y > self.pixmap.height(): x = max(x, 0) y = max(y, 0) x = min(x, self.pixmap.width()) y = min(y, self.pixmap.height()) return x, y, True return x, y, False
[ "Moves", "a", "point", "x", "y", "to", "within", "the", "boundaries", "of", "the", "canvas", ".", ":", "return", ":", "(", "x", "y", "snapped", ")", "where", "snapped", "is", "True", "if", "x", "or", "y", "were", "changed", "False", "if", "not", "....
tzutalin/labelImg
python
https://github.com/tzutalin/labelImg/blob/6afd15aa88f89f41254e0004ed219b3965eb2c0d/libs/canvas.py#L329-L341
[ "def", "snapPointToCanvas", "(", "self", ",", "x", ",", "y", ")", ":", "if", "x", "<", "0", "or", "x", ">", "self", ".", "pixmap", ".", "width", "(", ")", "or", "y", "<", "0", "or", "y", ">", "self", ".", "pixmap", ".", "height", "(", ")", ...
6afd15aa88f89f41254e0004ed219b3965eb2c0d
train
Canvas.intersectingEdges
For each edge formed by `points', yield the intersection with the line segment `(x1,y1) - (x2,y2)`, if it exists. Also return the distance of `(x2,y2)' to the middle of the edge along with its index, so that the one closest can be chosen.
libs/canvas.py
def intersectingEdges(self, x1y1, x2y2, points): """For each edge formed by `points', yield the intersection with the line segment `(x1,y1) - (x2,y2)`, if it exists. Also return the distance of `(x2,y2)' to the middle of the edge along with its index, so that the one closest can be chosen.""" x1, y1 = x1y1 x2, y2 = x2y2 for i in range(4): x3, y3 = points[i] x4, y4 = points[(i + 1) % 4] denom = (y4 - y3) * (x2 - x1) - (x4 - x3) * (y2 - y1) nua = (x4 - x3) * (y1 - y3) - (y4 - y3) * (x1 - x3) nub = (x2 - x1) * (y1 - y3) - (y2 - y1) * (x1 - x3) if denom == 0: # This covers two cases: # nua == nub == 0: Coincident # otherwise: Parallel continue ua, ub = nua / denom, nub / denom if 0 <= ua <= 1 and 0 <= ub <= 1: x = x1 + ua * (x2 - x1) y = y1 + ua * (y2 - y1) m = QPointF((x3 + x4) / 2, (y3 + y4) / 2) d = distance(m - QPointF(x2, y2)) yield d, i, (x, y)
def intersectingEdges(self, x1y1, x2y2, points): """For each edge formed by `points', yield the intersection with the line segment `(x1,y1) - (x2,y2)`, if it exists. Also return the distance of `(x2,y2)' to the middle of the edge along with its index, so that the one closest can be chosen.""" x1, y1 = x1y1 x2, y2 = x2y2 for i in range(4): x3, y3 = points[i] x4, y4 = points[(i + 1) % 4] denom = (y4 - y3) * (x2 - x1) - (x4 - x3) * (y2 - y1) nua = (x4 - x3) * (y1 - y3) - (y4 - y3) * (x1 - x3) nub = (x2 - x1) * (y1 - y3) - (y2 - y1) * (x1 - x3) if denom == 0: # This covers two cases: # nua == nub == 0: Coincident # otherwise: Parallel continue ua, ub = nua / denom, nub / denom if 0 <= ua <= 1 and 0 <= ub <= 1: x = x1 + ua * (x2 - x1) y = y1 + ua * (y2 - y1) m = QPointF((x3 + x4) / 2, (y3 + y4) / 2) d = distance(m - QPointF(x2, y2)) yield d, i, (x, y)
[ "For", "each", "edge", "formed", "by", "points", "yield", "the", "intersection", "with", "the", "line", "segment", "(", "x1", "y1", ")", "-", "(", "x2", "y2", ")", "if", "it", "exists", ".", "Also", "return", "the", "distance", "of", "(", "x2", "y2",...
tzutalin/labelImg
python
https://github.com/tzutalin/labelImg/blob/6afd15aa88f89f41254e0004ed219b3965eb2c0d/libs/canvas.py#L551-L575
[ "def", "intersectingEdges", "(", "self", ",", "x1y1", ",", "x2y2", ",", "points", ")", ":", "x1", ",", "y1", "=", "x1y1", "x2", ",", "y2", "=", "x2y2", "for", "i", "in", "range", "(", "4", ")", ":", "x3", ",", "y3", "=", "points", "[", "i", "...
6afd15aa88f89f41254e0004ed219b3965eb2c0d
train
get_main_app
Standard boilerplate Qt application code. Do everything but app.exec_() -- so that we can test the application in one thread
labelImg.py
def get_main_app(argv=[]): """ Standard boilerplate Qt application code. Do everything but app.exec_() -- so that we can test the application in one thread """ app = QApplication(argv) app.setApplicationName(__appname__) app.setWindowIcon(newIcon("app")) # Tzutalin 201705+: Accept extra agruments to change predefined class file # Usage : labelImg.py image predefClassFile saveDir win = MainWindow(argv[1] if len(argv) >= 2 else None, argv[2] if len(argv) >= 3 else os.path.join( os.path.dirname(sys.argv[0]), 'data', 'predefined_classes.txt'), argv[3] if len(argv) >= 4 else None) win.show() return app, win
def get_main_app(argv=[]): """ Standard boilerplate Qt application code. Do everything but app.exec_() -- so that we can test the application in one thread """ app = QApplication(argv) app.setApplicationName(__appname__) app.setWindowIcon(newIcon("app")) # Tzutalin 201705+: Accept extra agruments to change predefined class file # Usage : labelImg.py image predefClassFile saveDir win = MainWindow(argv[1] if len(argv) >= 2 else None, argv[2] if len(argv) >= 3 else os.path.join( os.path.dirname(sys.argv[0]), 'data', 'predefined_classes.txt'), argv[3] if len(argv) >= 4 else None) win.show() return app, win
[ "Standard", "boilerplate", "Qt", "application", "code", ".", "Do", "everything", "but", "app", ".", "exec_", "()", "--", "so", "that", "we", "can", "test", "the", "application", "in", "one", "thread" ]
tzutalin/labelImg
python
https://github.com/tzutalin/labelImg/blob/6afd15aa88f89f41254e0004ed219b3965eb2c0d/labelImg.py#L1450-L1466
[ "def", "get_main_app", "(", "argv", "=", "[", "]", ")", ":", "app", "=", "QApplication", "(", "argv", ")", "app", ".", "setApplicationName", "(", "__appname__", ")", "app", ".", "setWindowIcon", "(", "newIcon", "(", "\"app\"", ")", ")", "# Tzutalin 201705+...
6afd15aa88f89f41254e0004ed219b3965eb2c0d
train
MainWindow.toggleActions
Enable/Disable widgets which depend on an opened image.
labelImg.py
def toggleActions(self, value=True): """Enable/Disable widgets which depend on an opened image.""" for z in self.actions.zoomActions: z.setEnabled(value) for action in self.actions.onLoadActive: action.setEnabled(value)
def toggleActions(self, value=True): """Enable/Disable widgets which depend on an opened image.""" for z in self.actions.zoomActions: z.setEnabled(value) for action in self.actions.onLoadActive: action.setEnabled(value)
[ "Enable", "/", "Disable", "widgets", "which", "depend", "on", "an", "opened", "image", "." ]
tzutalin/labelImg
python
https://github.com/tzutalin/labelImg/blob/6afd15aa88f89f41254e0004ed219b3965eb2c0d/labelImg.py#L556-L561
[ "def", "toggleActions", "(", "self", ",", "value", "=", "True", ")", ":", "for", "z", "in", "self", ".", "actions", ".", "zoomActions", ":", "z", ".", "setEnabled", "(", "value", ")", "for", "action", "in", "self", ".", "actions", ".", "onLoadActive", ...
6afd15aa88f89f41254e0004ed219b3965eb2c0d
train
MainWindow.toggleDrawingSensitive
In the middle of drawing, toggling between modes should be disabled.
labelImg.py
def toggleDrawingSensitive(self, drawing=True): """In the middle of drawing, toggling between modes should be disabled.""" self.actions.editMode.setEnabled(not drawing) if not drawing and self.beginner(): # Cancel creation. print('Cancel creation.') self.canvas.setEditing(True) self.canvas.restoreCursor() self.actions.create.setEnabled(True)
def toggleDrawingSensitive(self, drawing=True): """In the middle of drawing, toggling between modes should be disabled.""" self.actions.editMode.setEnabled(not drawing) if not drawing and self.beginner(): # Cancel creation. print('Cancel creation.') self.canvas.setEditing(True) self.canvas.restoreCursor() self.actions.create.setEnabled(True)
[ "In", "the", "middle", "of", "drawing", "toggling", "between", "modes", "should", "be", "disabled", "." ]
tzutalin/labelImg
python
https://github.com/tzutalin/labelImg/blob/6afd15aa88f89f41254e0004ed219b3965eb2c0d/labelImg.py#L621-L629
[ "def", "toggleDrawingSensitive", "(", "self", ",", "drawing", "=", "True", ")", ":", "self", ".", "actions", ".", "editMode", ".", "setEnabled", "(", "not", "drawing", ")", "if", "not", "drawing", "and", "self", ".", "beginner", "(", ")", ":", "# Cancel ...
6afd15aa88f89f41254e0004ed219b3965eb2c0d
train
MainWindow.btnstate
Function to handle difficult examples Update on each object
labelImg.py
def btnstate(self, item= None): """ Function to handle difficult examples Update on each object """ if not self.canvas.editing(): return item = self.currentItem() if not item: # If not selected Item, take the first one item = self.labelList.item(self.labelList.count()-1) difficult = self.diffcButton.isChecked() try: shape = self.itemsToShapes[item] except: pass # Checked and Update try: if difficult != shape.difficult: shape.difficult = difficult self.setDirty() else: # User probably changed item visibility self.canvas.setShapeVisible(shape, item.checkState() == Qt.Checked) except: pass
def btnstate(self, item= None): """ Function to handle difficult examples Update on each object """ if not self.canvas.editing(): return item = self.currentItem() if not item: # If not selected Item, take the first one item = self.labelList.item(self.labelList.count()-1) difficult = self.diffcButton.isChecked() try: shape = self.itemsToShapes[item] except: pass # Checked and Update try: if difficult != shape.difficult: shape.difficult = difficult self.setDirty() else: # User probably changed item visibility self.canvas.setShapeVisible(shape, item.checkState() == Qt.Checked) except: pass
[ "Function", "to", "handle", "difficult", "examples", "Update", "on", "each", "object" ]
tzutalin/labelImg
python
https://github.com/tzutalin/labelImg/blob/6afd15aa88f89f41254e0004ed219b3965eb2c0d/labelImg.py#L685-L709
[ "def", "btnstate", "(", "self", ",", "item", "=", "None", ")", ":", "if", "not", "self", ".", "canvas", ".", "editing", "(", ")", ":", "return", "item", "=", "self", ".", "currentItem", "(", ")", "if", "not", "item", ":", "# If not selected Item, take ...
6afd15aa88f89f41254e0004ed219b3965eb2c0d
train
MainWindow.newShape
Pop-up and give focus to the label editor. position MUST be in global coordinates.
labelImg.py
def newShape(self): """Pop-up and give focus to the label editor. position MUST be in global coordinates. """ if not self.useDefaultLabelCheckbox.isChecked() or not self.defaultLabelTextLine.text(): if len(self.labelHist) > 0: self.labelDialog = LabelDialog( parent=self, listItem=self.labelHist) # Sync single class mode from PR#106 if self.singleClassMode.isChecked() and self.lastLabel: text = self.lastLabel else: text = self.labelDialog.popUp(text=self.prevLabelText) self.lastLabel = text else: text = self.defaultLabelTextLine.text() # Add Chris self.diffcButton.setChecked(False) if text is not None: self.prevLabelText = text generate_color = generateColorByText(text) shape = self.canvas.setLastLabel(text, generate_color, generate_color) self.addLabel(shape) if self.beginner(): # Switch to edit mode. self.canvas.setEditing(True) self.actions.create.setEnabled(True) else: self.actions.editMode.setEnabled(True) self.setDirty() if text not in self.labelHist: self.labelHist.append(text) else: # self.canvas.undoLastLine() self.canvas.resetAllLines()
def newShape(self): """Pop-up and give focus to the label editor. position MUST be in global coordinates. """ if not self.useDefaultLabelCheckbox.isChecked() or not self.defaultLabelTextLine.text(): if len(self.labelHist) > 0: self.labelDialog = LabelDialog( parent=self, listItem=self.labelHist) # Sync single class mode from PR#106 if self.singleClassMode.isChecked() and self.lastLabel: text = self.lastLabel else: text = self.labelDialog.popUp(text=self.prevLabelText) self.lastLabel = text else: text = self.defaultLabelTextLine.text() # Add Chris self.diffcButton.setChecked(False) if text is not None: self.prevLabelText = text generate_color = generateColorByText(text) shape = self.canvas.setLastLabel(text, generate_color, generate_color) self.addLabel(shape) if self.beginner(): # Switch to edit mode. self.canvas.setEditing(True) self.actions.create.setEnabled(True) else: self.actions.editMode.setEnabled(True) self.setDirty() if text not in self.labelHist: self.labelHist.append(text) else: # self.canvas.undoLastLine() self.canvas.resetAllLines()
[ "Pop", "-", "up", "and", "give", "focus", "to", "the", "label", "editor", "." ]
tzutalin/labelImg
python
https://github.com/tzutalin/labelImg/blob/6afd15aa88f89f41254e0004ed219b3965eb2c0d/labelImg.py#L839-L876
[ "def", "newShape", "(", "self", ")", ":", "if", "not", "self", ".", "useDefaultLabelCheckbox", ".", "isChecked", "(", ")", "or", "not", "self", ".", "defaultLabelTextLine", ".", "text", "(", ")", ":", "if", "len", "(", "self", ".", "labelHist", ")", ">...
6afd15aa88f89f41254e0004ed219b3965eb2c0d
train
MainWindow.loadFile
Load the specified file, or the last opened file if None.
labelImg.py
def loadFile(self, filePath=None): """Load the specified file, or the last opened file if None.""" self.resetState() self.canvas.setEnabled(False) if filePath is None: filePath = self.settings.get(SETTING_FILENAME) # Make sure that filePath is a regular python string, rather than QString filePath = ustr(filePath) unicodeFilePath = ustr(filePath) # Tzutalin 20160906 : Add file list and dock to move faster # Highlight the file item if unicodeFilePath and self.fileListWidget.count() > 0: index = self.mImgList.index(unicodeFilePath) fileWidgetItem = self.fileListWidget.item(index) fileWidgetItem.setSelected(True) if unicodeFilePath and os.path.exists(unicodeFilePath): if LabelFile.isLabelFile(unicodeFilePath): try: self.labelFile = LabelFile(unicodeFilePath) except LabelFileError as e: self.errorMessage(u'Error opening file', (u"<p><b>%s</b></p>" u"<p>Make sure <i>%s</i> is a valid label file.") % (e, unicodeFilePath)) self.status("Error reading %s" % unicodeFilePath) return False self.imageData = self.labelFile.imageData self.lineColor = QColor(*self.labelFile.lineColor) self.fillColor = QColor(*self.labelFile.fillColor) self.canvas.verified = self.labelFile.verified else: # Load image: # read data first and store for saving into label file. self.imageData = read(unicodeFilePath, None) self.labelFile = None self.canvas.verified = False image = QImage.fromData(self.imageData) if image.isNull(): self.errorMessage(u'Error opening file', u"<p>Make sure <i>%s</i> is a valid image file." % unicodeFilePath) self.status("Error reading %s" % unicodeFilePath) return False self.status("Loaded %s" % os.path.basename(unicodeFilePath)) self.image = image self.filePath = unicodeFilePath self.canvas.loadPixmap(QPixmap.fromImage(image)) if self.labelFile: self.loadLabels(self.labelFile.shapes) self.setClean() self.canvas.setEnabled(True) self.adjustScale(initial=True) self.paintCanvas() self.addRecentFile(self.filePath) self.toggleActions(True) # Label xml file and show bound box according to its filename # if self.usingPascalVocFormat is True: if self.defaultSaveDir is not None: basename = os.path.basename( os.path.splitext(self.filePath)[0]) xmlPath = os.path.join(self.defaultSaveDir, basename + XML_EXT) txtPath = os.path.join(self.defaultSaveDir, basename + TXT_EXT) """Annotation file priority: PascalXML > YOLO """ if os.path.isfile(xmlPath): self.loadPascalXMLByFilename(xmlPath) elif os.path.isfile(txtPath): self.loadYOLOTXTByFilename(txtPath) else: xmlPath = os.path.splitext(filePath)[0] + XML_EXT txtPath = os.path.splitext(filePath)[0] + TXT_EXT if os.path.isfile(xmlPath): self.loadPascalXMLByFilename(xmlPath) elif os.path.isfile(txtPath): self.loadYOLOTXTByFilename(txtPath) self.setWindowTitle(__appname__ + ' ' + filePath) # Default : select last item if there is at least one item if self.labelList.count(): self.labelList.setCurrentItem(self.labelList.item(self.labelList.count()-1)) self.labelList.item(self.labelList.count()-1).setSelected(True) self.canvas.setFocus(True) return True return False
def loadFile(self, filePath=None): """Load the specified file, or the last opened file if None.""" self.resetState() self.canvas.setEnabled(False) if filePath is None: filePath = self.settings.get(SETTING_FILENAME) # Make sure that filePath is a regular python string, rather than QString filePath = ustr(filePath) unicodeFilePath = ustr(filePath) # Tzutalin 20160906 : Add file list and dock to move faster # Highlight the file item if unicodeFilePath and self.fileListWidget.count() > 0: index = self.mImgList.index(unicodeFilePath) fileWidgetItem = self.fileListWidget.item(index) fileWidgetItem.setSelected(True) if unicodeFilePath and os.path.exists(unicodeFilePath): if LabelFile.isLabelFile(unicodeFilePath): try: self.labelFile = LabelFile(unicodeFilePath) except LabelFileError as e: self.errorMessage(u'Error opening file', (u"<p><b>%s</b></p>" u"<p>Make sure <i>%s</i> is a valid label file.") % (e, unicodeFilePath)) self.status("Error reading %s" % unicodeFilePath) return False self.imageData = self.labelFile.imageData self.lineColor = QColor(*self.labelFile.lineColor) self.fillColor = QColor(*self.labelFile.fillColor) self.canvas.verified = self.labelFile.verified else: # Load image: # read data first and store for saving into label file. self.imageData = read(unicodeFilePath, None) self.labelFile = None self.canvas.verified = False image = QImage.fromData(self.imageData) if image.isNull(): self.errorMessage(u'Error opening file', u"<p>Make sure <i>%s</i> is a valid image file." % unicodeFilePath) self.status("Error reading %s" % unicodeFilePath) return False self.status("Loaded %s" % os.path.basename(unicodeFilePath)) self.image = image self.filePath = unicodeFilePath self.canvas.loadPixmap(QPixmap.fromImage(image)) if self.labelFile: self.loadLabels(self.labelFile.shapes) self.setClean() self.canvas.setEnabled(True) self.adjustScale(initial=True) self.paintCanvas() self.addRecentFile(self.filePath) self.toggleActions(True) # Label xml file and show bound box according to its filename # if self.usingPascalVocFormat is True: if self.defaultSaveDir is not None: basename = os.path.basename( os.path.splitext(self.filePath)[0]) xmlPath = os.path.join(self.defaultSaveDir, basename + XML_EXT) txtPath = os.path.join(self.defaultSaveDir, basename + TXT_EXT) """Annotation file priority: PascalXML > YOLO """ if os.path.isfile(xmlPath): self.loadPascalXMLByFilename(xmlPath) elif os.path.isfile(txtPath): self.loadYOLOTXTByFilename(txtPath) else: xmlPath = os.path.splitext(filePath)[0] + XML_EXT txtPath = os.path.splitext(filePath)[0] + TXT_EXT if os.path.isfile(xmlPath): self.loadPascalXMLByFilename(xmlPath) elif os.path.isfile(txtPath): self.loadYOLOTXTByFilename(txtPath) self.setWindowTitle(__appname__ + ' ' + filePath) # Default : select last item if there is at least one item if self.labelList.count(): self.labelList.setCurrentItem(self.labelList.item(self.labelList.count()-1)) self.labelList.item(self.labelList.count()-1).setSelected(True) self.canvas.setFocus(True) return True return False
[ "Load", "the", "specified", "file", "or", "the", "last", "opened", "file", "if", "None", "." ]
tzutalin/labelImg
python
https://github.com/tzutalin/labelImg/blob/6afd15aa88f89f41254e0004ed219b3965eb2c0d/labelImg.py#L960-L1051
[ "def", "loadFile", "(", "self", ",", "filePath", "=", "None", ")", ":", "self", ".", "resetState", "(", ")", "self", ".", "canvas", ".", "setEnabled", "(", "False", ")", "if", "filePath", "is", "None", ":", "filePath", "=", "self", ".", "settings", "...
6afd15aa88f89f41254e0004ed219b3965eb2c0d
train
MainWindow.scaleFitWindow
Figure out the size of the pixmap in order to fit the main widget.
labelImg.py
def scaleFitWindow(self): """Figure out the size of the pixmap in order to fit the main widget.""" e = 2.0 # So that no scrollbars are generated. w1 = self.centralWidget().width() - e h1 = self.centralWidget().height() - e a1 = w1 / h1 # Calculate a new scale value based on the pixmap's aspect ratio. w2 = self.canvas.pixmap.width() - 0.0 h2 = self.canvas.pixmap.height() - 0.0 a2 = w2 / h2 return w1 / w2 if a2 >= a1 else h1 / h2
def scaleFitWindow(self): """Figure out the size of the pixmap in order to fit the main widget.""" e = 2.0 # So that no scrollbars are generated. w1 = self.centralWidget().width() - e h1 = self.centralWidget().height() - e a1 = w1 / h1 # Calculate a new scale value based on the pixmap's aspect ratio. w2 = self.canvas.pixmap.width() - 0.0 h2 = self.canvas.pixmap.height() - 0.0 a2 = w2 / h2 return w1 / w2 if a2 >= a1 else h1 / h2
[ "Figure", "out", "the", "size", "of", "the", "pixmap", "in", "order", "to", "fit", "the", "main", "widget", "." ]
tzutalin/labelImg
python
https://github.com/tzutalin/labelImg/blob/6afd15aa88f89f41254e0004ed219b3965eb2c0d/labelImg.py#L1069-L1079
[ "def", "scaleFitWindow", "(", "self", ")", ":", "e", "=", "2.0", "# So that no scrollbars are generated.", "w1", "=", "self", ".", "centralWidget", "(", ")", ".", "width", "(", ")", "-", "e", "h1", "=", "self", ".", "centralWidget", "(", ")", ".", "heigh...
6afd15aa88f89f41254e0004ed219b3965eb2c0d
train
ustr
py2/py3 unicode helper
libs/ustr.py
def ustr(x): '''py2/py3 unicode helper''' if sys.version_info < (3, 0, 0): from PyQt4.QtCore import QString if type(x) == str: return x.decode(DEFAULT_ENCODING) if type(x) == QString: #https://blog.csdn.net/friendan/article/details/51088476 #https://blog.csdn.net/xxm524/article/details/74937308 return unicode(x.toUtf8(), DEFAULT_ENCODING, 'ignore') return x else: return x
def ustr(x): '''py2/py3 unicode helper''' if sys.version_info < (3, 0, 0): from PyQt4.QtCore import QString if type(x) == str: return x.decode(DEFAULT_ENCODING) if type(x) == QString: #https://blog.csdn.net/friendan/article/details/51088476 #https://blog.csdn.net/xxm524/article/details/74937308 return unicode(x.toUtf8(), DEFAULT_ENCODING, 'ignore') return x else: return x
[ "py2", "/", "py3", "unicode", "helper" ]
tzutalin/labelImg
python
https://github.com/tzutalin/labelImg/blob/6afd15aa88f89f41254e0004ed219b3965eb2c0d/libs/ustr.py#L4-L17
[ "def", "ustr", "(", "x", ")", ":", "if", "sys", ".", "version_info", "<", "(", "3", ",", "0", ",", "0", ")", ":", "from", "PyQt4", ".", "QtCore", "import", "QString", "if", "type", "(", "x", ")", "==", "str", ":", "return", "x", ".", "decode", ...
6afd15aa88f89f41254e0004ed219b3965eb2c0d
train
PascalVocWriter.prettify
Return a pretty-printed XML string for the Element.
libs/pascal_voc_io.py
def prettify(self, elem): """ Return a pretty-printed XML string for the Element. """ rough_string = ElementTree.tostring(elem, 'utf8') root = etree.fromstring(rough_string) return etree.tostring(root, pretty_print=True, encoding=ENCODE_METHOD).replace(" ".encode(), "\t".encode()) # minidom does not support UTF-8 '''reparsed = minidom.parseString(rough_string) return reparsed.toprettyxml(indent="\t", encoding=ENCODE_METHOD)'''
def prettify(self, elem): """ Return a pretty-printed XML string for the Element. """ rough_string = ElementTree.tostring(elem, 'utf8') root = etree.fromstring(rough_string) return etree.tostring(root, pretty_print=True, encoding=ENCODE_METHOD).replace(" ".encode(), "\t".encode()) # minidom does not support UTF-8 '''reparsed = minidom.parseString(rough_string) return reparsed.toprettyxml(indent="\t", encoding=ENCODE_METHOD)'''
[ "Return", "a", "pretty", "-", "printed", "XML", "string", "for", "the", "Element", "." ]
tzutalin/labelImg
python
https://github.com/tzutalin/labelImg/blob/6afd15aa88f89f41254e0004ed219b3965eb2c0d/libs/pascal_voc_io.py#L26-L35
[ "def", "prettify", "(", "self", ",", "elem", ")", ":", "rough_string", "=", "ElementTree", ".", "tostring", "(", "elem", ",", "'utf8'", ")", "root", "=", "etree", ".", "fromstring", "(", "rough_string", ")", "return", "etree", ".", "tostring", "(", "root...
6afd15aa88f89f41254e0004ed219b3965eb2c0d
train
PascalVocWriter.genXML
Return XML root
libs/pascal_voc_io.py
def genXML(self): """ Return XML root """ # Check conditions if self.filename is None or \ self.foldername is None or \ self.imgSize is None: return None top = Element('annotation') if self.verified: top.set('verified', 'yes') folder = SubElement(top, 'folder') folder.text = self.foldername filename = SubElement(top, 'filename') filename.text = self.filename if self.localImgPath is not None: localImgPath = SubElement(top, 'path') localImgPath.text = self.localImgPath source = SubElement(top, 'source') database = SubElement(source, 'database') database.text = self.databaseSrc size_part = SubElement(top, 'size') width = SubElement(size_part, 'width') height = SubElement(size_part, 'height') depth = SubElement(size_part, 'depth') width.text = str(self.imgSize[1]) height.text = str(self.imgSize[0]) if len(self.imgSize) == 3: depth.text = str(self.imgSize[2]) else: depth.text = '1' segmented = SubElement(top, 'segmented') segmented.text = '0' return top
def genXML(self): """ Return XML root """ # Check conditions if self.filename is None or \ self.foldername is None or \ self.imgSize is None: return None top = Element('annotation') if self.verified: top.set('verified', 'yes') folder = SubElement(top, 'folder') folder.text = self.foldername filename = SubElement(top, 'filename') filename.text = self.filename if self.localImgPath is not None: localImgPath = SubElement(top, 'path') localImgPath.text = self.localImgPath source = SubElement(top, 'source') database = SubElement(source, 'database') database.text = self.databaseSrc size_part = SubElement(top, 'size') width = SubElement(size_part, 'width') height = SubElement(size_part, 'height') depth = SubElement(size_part, 'depth') width.text = str(self.imgSize[1]) height.text = str(self.imgSize[0]) if len(self.imgSize) == 3: depth.text = str(self.imgSize[2]) else: depth.text = '1' segmented = SubElement(top, 'segmented') segmented.text = '0' return top
[ "Return", "XML", "root" ]
tzutalin/labelImg
python
https://github.com/tzutalin/labelImg/blob/6afd15aa88f89f41254e0004ed219b3965eb2c0d/libs/pascal_voc_io.py#L37-L78
[ "def", "genXML", "(", "self", ")", ":", "# Check conditions", "if", "self", ".", "filename", "is", "None", "or", "self", ".", "foldername", "is", "None", "or", "self", ".", "imgSize", "is", "None", ":", "return", "None", "top", "=", "Element", "(", "'a...
6afd15aa88f89f41254e0004ed219b3965eb2c0d
train
Exchange.fetch
Perform a HTTP request and return decoded JSON data
python/ccxt/async_support/base/exchange.py
async def fetch(self, url, method='GET', headers=None, body=None): """Perform a HTTP request and return decoded JSON data""" request_headers = self.prepare_request_headers(headers) url = self.proxy + url if self.verbose: print("\nRequest:", method, url, headers, body) self.logger.debug("%s %s, Request: %s %s", method, url, headers, body) encoded_body = body.encode() if body else None session_method = getattr(self.session, method.lower()) response = None http_response = None json_response = None try: async with session_method(yarl.URL(url, encoded=True), data=encoded_body, headers=request_headers, timeout=(self.timeout / 1000), proxy=self.aiohttp_proxy) as response: http_response = await response.text() json_response = self.parse_json(http_response) if self.is_json_encoded_object(http_response) else None headers = response.headers if self.enableLastHttpResponse: self.last_http_response = http_response if self.enableLastResponseHeaders: self.last_response_headers = headers if self.enableLastJsonResponse: self.last_json_response = json_response if self.verbose: print("\nResponse:", method, url, response.status, headers, http_response) self.logger.debug("%s %s, Response: %s %s %s", method, url, response.status, headers, http_response) except socket.gaierror as e: self.raise_error(ExchangeNotAvailable, url, method, e, None) except concurrent.futures._base.TimeoutError as e: self.raise_error(RequestTimeout, method, url, e, None) except aiohttp.client_exceptions.ClientConnectionError as e: self.raise_error(ExchangeNotAvailable, url, method, e, None) except aiohttp.client_exceptions.ClientError as e: # base exception class self.raise_error(ExchangeError, url, method, e, None) self.handle_errors(response.status, response.reason, url, method, headers, http_response, json_response) self.handle_rest_errors(None, response.status, http_response, url, method) self.handle_rest_response(http_response, json_response, url, method, headers, body) if json_response is not None: return json_response return http_response
async def fetch(self, url, method='GET', headers=None, body=None): """Perform a HTTP request and return decoded JSON data""" request_headers = self.prepare_request_headers(headers) url = self.proxy + url if self.verbose: print("\nRequest:", method, url, headers, body) self.logger.debug("%s %s, Request: %s %s", method, url, headers, body) encoded_body = body.encode() if body else None session_method = getattr(self.session, method.lower()) response = None http_response = None json_response = None try: async with session_method(yarl.URL(url, encoded=True), data=encoded_body, headers=request_headers, timeout=(self.timeout / 1000), proxy=self.aiohttp_proxy) as response: http_response = await response.text() json_response = self.parse_json(http_response) if self.is_json_encoded_object(http_response) else None headers = response.headers if self.enableLastHttpResponse: self.last_http_response = http_response if self.enableLastResponseHeaders: self.last_response_headers = headers if self.enableLastJsonResponse: self.last_json_response = json_response if self.verbose: print("\nResponse:", method, url, response.status, headers, http_response) self.logger.debug("%s %s, Response: %s %s %s", method, url, response.status, headers, http_response) except socket.gaierror as e: self.raise_error(ExchangeNotAvailable, url, method, e, None) except concurrent.futures._base.TimeoutError as e: self.raise_error(RequestTimeout, method, url, e, None) except aiohttp.client_exceptions.ClientConnectionError as e: self.raise_error(ExchangeNotAvailable, url, method, e, None) except aiohttp.client_exceptions.ClientError as e: # base exception class self.raise_error(ExchangeError, url, method, e, None) self.handle_errors(response.status, response.reason, url, method, headers, http_response, json_response) self.handle_rest_errors(None, response.status, http_response, url, method) self.handle_rest_response(http_response, json_response, url, method, headers, body) if json_response is not None: return json_response return http_response
[ "Perform", "a", "HTTP", "request", "and", "return", "decoded", "JSON", "data" ]
ccxt/ccxt
python
https://github.com/ccxt/ccxt/blob/23062efd7a5892c79b370c9d951c03cf8c0ddf23/python/ccxt/async_support/base/exchange.py#L117-L168
[ "async", "def", "fetch", "(", "self", ",", "url", ",", "method", "=", "'GET'", ",", "headers", "=", "None", ",", "body", "=", "None", ")", ":", "request_headers", "=", "self", ".", "prepare_request_headers", "(", "headers", ")", "url", "=", "self", "."...
23062efd7a5892c79b370c9d951c03cf8c0ddf23
train
Exchange.fetch2
A better wrapper over request for deferred signing
python/ccxt/base/exchange.py
def fetch2(self, path, api='public', method='GET', params={}, headers=None, body=None): """A better wrapper over request for deferred signing""" if self.enableRateLimit: self.throttle() self.lastRestRequestTimestamp = self.milliseconds() request = self.sign(path, api, method, params, headers, body) return self.fetch(request['url'], request['method'], request['headers'], request['body'])
def fetch2(self, path, api='public', method='GET', params={}, headers=None, body=None): """A better wrapper over request for deferred signing""" if self.enableRateLimit: self.throttle() self.lastRestRequestTimestamp = self.milliseconds() request = self.sign(path, api, method, params, headers, body) return self.fetch(request['url'], request['method'], request['headers'], request['body'])
[ "A", "better", "wrapper", "over", "request", "for", "deferred", "signing" ]
ccxt/ccxt
python
https://github.com/ccxt/ccxt/blob/23062efd7a5892c79b370c9d951c03cf8c0ddf23/python/ccxt/base/exchange.py#L423-L429
[ "def", "fetch2", "(", "self", ",", "path", ",", "api", "=", "'public'", ",", "method", "=", "'GET'", ",", "params", "=", "{", "}", ",", "headers", "=", "None", ",", "body", "=", "None", ")", ":", "if", "self", ".", "enableRateLimit", ":", "self", ...
23062efd7a5892c79b370c9d951c03cf8c0ddf23
train
Exchange.request
Exchange.request is the entry point for all generated methods
python/ccxt/base/exchange.py
def request(self, path, api='public', method='GET', params={}, headers=None, body=None): """Exchange.request is the entry point for all generated methods""" return self.fetch2(path, api, method, params, headers, body)
def request(self, path, api='public', method='GET', params={}, headers=None, body=None): """Exchange.request is the entry point for all generated methods""" return self.fetch2(path, api, method, params, headers, body)
[ "Exchange", ".", "request", "is", "the", "entry", "point", "for", "all", "generated", "methods" ]
ccxt/ccxt
python
https://github.com/ccxt/ccxt/blob/23062efd7a5892c79b370c9d951c03cf8c0ddf23/python/ccxt/base/exchange.py#L431-L433
[ "def", "request", "(", "self", ",", "path", ",", "api", "=", "'public'", ",", "method", "=", "'GET'", ",", "params", "=", "{", "}", ",", "headers", "=", "None", ",", "body", "=", "None", ")", ":", "return", "self", ".", "fetch2", "(", "path", ","...
23062efd7a5892c79b370c9d951c03cf8c0ddf23
train
Exchange.find_broadly_matched_key
A helper method for matching error strings exactly vs broadly
python/ccxt/base/exchange.py
def find_broadly_matched_key(self, broad, string): """A helper method for matching error strings exactly vs broadly""" keys = list(broad.keys()) for i in range(0, len(keys)): key = keys[i] if string.find(key) >= 0: return key return None
def find_broadly_matched_key(self, broad, string): """A helper method for matching error strings exactly vs broadly""" keys = list(broad.keys()) for i in range(0, len(keys)): key = keys[i] if string.find(key) >= 0: return key return None
[ "A", "helper", "method", "for", "matching", "error", "strings", "exactly", "vs", "broadly" ]
ccxt/ccxt
python
https://github.com/ccxt/ccxt/blob/23062efd7a5892c79b370c9d951c03cf8c0ddf23/python/ccxt/base/exchange.py#L445-L452
[ "def", "find_broadly_matched_key", "(", "self", ",", "broad", ",", "string", ")", ":", "keys", "=", "list", "(", "broad", ".", "keys", "(", ")", ")", "for", "i", "in", "range", "(", "0", ",", "len", "(", "keys", ")", ")", ":", "key", "=", "keys",...
23062efd7a5892c79b370c9d951c03cf8c0ddf23
train
Exchange.fetch
Perform a HTTP request and return decoded JSON data
python/ccxt/base/exchange.py
def fetch(self, url, method='GET', headers=None, body=None): """Perform a HTTP request and return decoded JSON data""" request_headers = self.prepare_request_headers(headers) url = self.proxy + url if self.verbose: print("\nRequest:", method, url, request_headers, body) self.logger.debug("%s %s, Request: %s %s", method, url, request_headers, body) if body: body = body.encode() self.session.cookies.clear() response = None http_response = None json_response = None try: response = self.session.request( method, url, data=body, headers=request_headers, timeout=int(self.timeout / 1000), proxies=self.proxies ) http_response = response.text json_response = self.parse_json(http_response) if self.is_json_encoded_object(http_response) else None headers = response.headers # FIXME remove last_x_responses from subclasses if self.enableLastHttpResponse: self.last_http_response = http_response if self.enableLastJsonResponse: self.last_json_response = json_response if self.enableLastResponseHeaders: self.last_response_headers = headers if self.verbose: print("\nResponse:", method, url, response.status_code, headers, http_response) self.logger.debug("%s %s, Response: %s %s %s", method, url, response.status_code, headers, http_response) response.raise_for_status() except Timeout as e: self.raise_error(RequestTimeout, method, url, e) except TooManyRedirects as e: self.raise_error(ExchangeError, url, method, e) except SSLError as e: self.raise_error(ExchangeError, url, method, e) except HTTPError as e: self.handle_errors(response.status_code, response.reason, url, method, headers, http_response, json_response) self.handle_rest_errors(e, response.status_code, http_response, url, method) self.raise_error(ExchangeError, url, method, e, http_response) except RequestException as e: # base exception class error_string = str(e) if ('ECONNRESET' in error_string) or ('Connection aborted.' in error_string): self.raise_error(NetworkError, url, method, e) else: self.raise_error(ExchangeError, url, method, e) self.handle_errors(response.status_code, response.reason, url, method, headers, http_response, json_response) self.handle_rest_response(http_response, json_response, url, method, headers, body) if json_response is not None: return json_response return http_response
def fetch(self, url, method='GET', headers=None, body=None): """Perform a HTTP request and return decoded JSON data""" request_headers = self.prepare_request_headers(headers) url = self.proxy + url if self.verbose: print("\nRequest:", method, url, request_headers, body) self.logger.debug("%s %s, Request: %s %s", method, url, request_headers, body) if body: body = body.encode() self.session.cookies.clear() response = None http_response = None json_response = None try: response = self.session.request( method, url, data=body, headers=request_headers, timeout=int(self.timeout / 1000), proxies=self.proxies ) http_response = response.text json_response = self.parse_json(http_response) if self.is_json_encoded_object(http_response) else None headers = response.headers # FIXME remove last_x_responses from subclasses if self.enableLastHttpResponse: self.last_http_response = http_response if self.enableLastJsonResponse: self.last_json_response = json_response if self.enableLastResponseHeaders: self.last_response_headers = headers if self.verbose: print("\nResponse:", method, url, response.status_code, headers, http_response) self.logger.debug("%s %s, Response: %s %s %s", method, url, response.status_code, headers, http_response) response.raise_for_status() except Timeout as e: self.raise_error(RequestTimeout, method, url, e) except TooManyRedirects as e: self.raise_error(ExchangeError, url, method, e) except SSLError as e: self.raise_error(ExchangeError, url, method, e) except HTTPError as e: self.handle_errors(response.status_code, response.reason, url, method, headers, http_response, json_response) self.handle_rest_errors(e, response.status_code, http_response, url, method) self.raise_error(ExchangeError, url, method, e, http_response) except RequestException as e: # base exception class error_string = str(e) if ('ECONNRESET' in error_string) or ('Connection aborted.' in error_string): self.raise_error(NetworkError, url, method, e) else: self.raise_error(ExchangeError, url, method, e) self.handle_errors(response.status_code, response.reason, url, method, headers, http_response, json_response) self.handle_rest_response(http_response, json_response, url, method, headers, body) if json_response is not None: return json_response return http_response
[ "Perform", "a", "HTTP", "request", "and", "return", "decoded", "JSON", "data" ]
ccxt/ccxt
python
https://github.com/ccxt/ccxt/blob/23062efd7a5892c79b370c9d951c03cf8c0ddf23/python/ccxt/base/exchange.py#L470-L536
[ "def", "fetch", "(", "self", ",", "url", ",", "method", "=", "'GET'", ",", "headers", "=", "None", ",", "body", "=", "None", ")", ":", "request_headers", "=", "self", ".", "prepare_request_headers", "(", "headers", ")", "url", "=", "self", ".", "proxy"...
23062efd7a5892c79b370c9d951c03cf8c0ddf23
train
Exchange.safe_either
A helper-wrapper for the safe_value_2() family.
python/ccxt/base/exchange.py
def safe_either(method, dictionary, key1, key2, default_value=None): """A helper-wrapper for the safe_value_2() family.""" value = method(dictionary, key1) return value if value is not None else method(dictionary, key2, default_value)
def safe_either(method, dictionary, key1, key2, default_value=None): """A helper-wrapper for the safe_value_2() family.""" value = method(dictionary, key1) return value if value is not None else method(dictionary, key2, default_value)
[ "A", "helper", "-", "wrapper", "for", "the", "safe_value_2", "()", "family", "." ]
ccxt/ccxt
python
https://github.com/ccxt/ccxt/blob/23062efd7a5892c79b370c9d951c03cf8c0ddf23/python/ccxt/base/exchange.py#L616-L619
[ "def", "safe_either", "(", "method", ",", "dictionary", ",", "key1", ",", "key2", ",", "default_value", "=", "None", ")", ":", "value", "=", "method", "(", "dictionary", ",", "key1", ")", "return", "value", "if", "value", "is", "not", "None", "else", "...
23062efd7a5892c79b370c9d951c03cf8c0ddf23
train
Exchange.truncate
Deprecated, use decimal_to_precision instead
python/ccxt/base/exchange.py
def truncate(num, precision=0): """Deprecated, use decimal_to_precision instead""" if precision > 0: decimal_precision = math.pow(10, precision) return math.trunc(num * decimal_precision) / decimal_precision return int(Exchange.truncate_to_string(num, precision))
def truncate(num, precision=0): """Deprecated, use decimal_to_precision instead""" if precision > 0: decimal_precision = math.pow(10, precision) return math.trunc(num * decimal_precision) / decimal_precision return int(Exchange.truncate_to_string(num, precision))
[ "Deprecated", "use", "decimal_to_precision", "instead" ]
ccxt/ccxt
python
https://github.com/ccxt/ccxt/blob/23062efd7a5892c79b370c9d951c03cf8c0ddf23/python/ccxt/base/exchange.py#L622-L627
[ "def", "truncate", "(", "num", ",", "precision", "=", "0", ")", ":", "if", "precision", ">", "0", ":", "decimal_precision", "=", "math", ".", "pow", "(", "10", ",", "precision", ")", "return", "math", ".", "trunc", "(", "num", "*", "decimal_precision",...
23062efd7a5892c79b370c9d951c03cf8c0ddf23
train
Exchange.truncate_to_string
Deprecated, todo: remove references from subclasses
python/ccxt/base/exchange.py
def truncate_to_string(num, precision=0): """Deprecated, todo: remove references from subclasses""" if precision > 0: parts = ('{0:.%df}' % precision).format(Decimal(num)).split('.') decimal_digits = parts[1][:precision].rstrip('0') decimal_digits = decimal_digits if len(decimal_digits) else '0' return parts[0] + '.' + decimal_digits return ('%d' % num)
def truncate_to_string(num, precision=0): """Deprecated, todo: remove references from subclasses""" if precision > 0: parts = ('{0:.%df}' % precision).format(Decimal(num)).split('.') decimal_digits = parts[1][:precision].rstrip('0') decimal_digits = decimal_digits if len(decimal_digits) else '0' return parts[0] + '.' + decimal_digits return ('%d' % num)
[ "Deprecated", "todo", ":", "remove", "references", "from", "subclasses" ]
ccxt/ccxt
python
https://github.com/ccxt/ccxt/blob/23062efd7a5892c79b370c9d951c03cf8c0ddf23/python/ccxt/base/exchange.py#L630-L637
[ "def", "truncate_to_string", "(", "num", ",", "precision", "=", "0", ")", ":", "if", "precision", ">", "0", ":", "parts", "=", "(", "'{0:.%df}'", "%", "precision", ")", ".", "format", "(", "Decimal", "(", "num", ")", ")", ".", "split", "(", "'.'", ...
23062efd7a5892c79b370c9d951c03cf8c0ddf23
train
Exchange.check_address
Checks an address is not the same character repeated or an empty sequence
python/ccxt/base/exchange.py
def check_address(self, address): """Checks an address is not the same character repeated or an empty sequence""" if address is None: self.raise_error(InvalidAddress, details='address is None') if all(letter == address[0] for letter in address) or len(address) < self.minFundingAddressLength or ' ' in address: self.raise_error(InvalidAddress, details='address is invalid or has less than ' + str(self.minFundingAddressLength) + ' characters: "' + str(address) + '"') return address
def check_address(self, address): """Checks an address is not the same character repeated or an empty sequence""" if address is None: self.raise_error(InvalidAddress, details='address is None') if all(letter == address[0] for letter in address) or len(address) < self.minFundingAddressLength or ' ' in address: self.raise_error(InvalidAddress, details='address is invalid or has less than ' + str(self.minFundingAddressLength) + ' characters: "' + str(address) + '"') return address
[ "Checks", "an", "address", "is", "not", "the", "same", "character", "repeated", "or", "an", "empty", "sequence" ]
ccxt/ccxt
python
https://github.com/ccxt/ccxt/blob/23062efd7a5892c79b370c9d951c03cf8c0ddf23/python/ccxt/base/exchange.py#L1000-L1006
[ "def", "check_address", "(", "self", ",", "address", ")", ":", "if", "address", "is", "None", ":", "self", ".", "raise_error", "(", "InvalidAddress", ",", "details", "=", "'address is None'", ")", "if", "all", "(", "letter", "==", "address", "[", "0", "]...
23062efd7a5892c79b370c9d951c03cf8c0ddf23
train
reduce_filename
r''' Expects something like /tmp/tmpAjry4Gdsbench/test.weights.e5.XXX.YYY.pb Where XXX is a variation on the model size for example And where YYY is a const related to the training dataset
bin/benchmark_plotter.py
def reduce_filename(f): r''' Expects something like /tmp/tmpAjry4Gdsbench/test.weights.e5.XXX.YYY.pb Where XXX is a variation on the model size for example And where YYY is a const related to the training dataset ''' f = os.path.basename(f).split('.') return keep_only_digits(f[-3])
def reduce_filename(f): r''' Expects something like /tmp/tmpAjry4Gdsbench/test.weights.e5.XXX.YYY.pb Where XXX is a variation on the model size for example And where YYY is a const related to the training dataset ''' f = os.path.basename(f).split('.') return keep_only_digits(f[-3])
[ "r", "Expects", "something", "like", "/", "tmp", "/", "tmpAjry4Gdsbench", "/", "test", ".", "weights", ".", "e5", ".", "XXX", ".", "YYY", ".", "pb", "Where", "XXX", "is", "a", "variation", "on", "the", "model", "size", "for", "example", "And", "where",...
mozilla/DeepSpeech
python
https://github.com/mozilla/DeepSpeech/blob/f64aa73e7fbe9dde40d4fcf23b42ab304747d152/bin/benchmark_plotter.py#L30-L38
[ "def", "reduce_filename", "(", "f", ")", ":", "f", "=", "os", ".", "path", ".", "basename", "(", "f", ")", ".", "split", "(", "'.'", ")", "return", "keep_only_digits", "(", "f", "[", "-", "3", "]", ")" ]
f64aa73e7fbe9dde40d4fcf23b42ab304747d152
train
keep_only_digits
r''' local helper to just keep digits
util/benchmark.py
def keep_only_digits(s): r''' local helper to just keep digits ''' fs = '' for c in s: if c.isdigit(): fs += c return int(fs)
def keep_only_digits(s): r''' local helper to just keep digits ''' fs = '' for c in s: if c.isdigit(): fs += c return int(fs)
[ "r", "local", "helper", "to", "just", "keep", "digits" ]
mozilla/DeepSpeech
python
https://github.com/mozilla/DeepSpeech/blob/f64aa73e7fbe9dde40d4fcf23b42ab304747d152/util/benchmark.py#L6-L15
[ "def", "keep_only_digits", "(", "s", ")", ":", "fs", "=", "''", "for", "c", "in", "s", ":", "if", "c", ".", "isdigit", "(", ")", ":", "fs", "+=", "c", "return", "int", "(", "fs", ")" ]
f64aa73e7fbe9dde40d4fcf23b42ab304747d152
train
parse_stm_file
r""" Parses an STM file at ``stm_file`` into a list of :class:`STMSegment`.
util/stm.py
def parse_stm_file(stm_file): r""" Parses an STM file at ``stm_file`` into a list of :class:`STMSegment`. """ stm_segments = [] with codecs.open(stm_file, encoding="utf-8") as stm_lines: for stm_line in stm_lines: stmSegment = STMSegment(stm_line) if not "ignore_time_segment_in_scoring" == stmSegment.transcript: stm_segments.append(stmSegment) return stm_segments
def parse_stm_file(stm_file): r""" Parses an STM file at ``stm_file`` into a list of :class:`STMSegment`. """ stm_segments = [] with codecs.open(stm_file, encoding="utf-8") as stm_lines: for stm_line in stm_lines: stmSegment = STMSegment(stm_line) if not "ignore_time_segment_in_scoring" == stmSegment.transcript: stm_segments.append(stmSegment) return stm_segments
[ "r", "Parses", "an", "STM", "file", "at", "stm_file", "into", "a", "list", "of", ":", "class", ":", "STMSegment", "." ]
mozilla/DeepSpeech
python
https://github.com/mozilla/DeepSpeech/blob/f64aa73e7fbe9dde40d4fcf23b42ab304747d152/util/stm.py#L54-L64
[ "def", "parse_stm_file", "(", "stm_file", ")", ":", "stm_segments", "=", "[", "]", "with", "codecs", ".", "open", "(", "stm_file", ",", "encoding", "=", "\"utf-8\"", ")", "as", "stm_lines", ":", "for", "stm_line", "in", "stm_lines", ":", "stmSegment", "=",...
f64aa73e7fbe9dde40d4fcf23b42ab304747d152
train
read_wave
Reads a .wav file. Takes the path, and returns (PCM audio data, sample rate).
examples/vad_transcriber/wavSplit.py
def read_wave(path): """Reads a .wav file. Takes the path, and returns (PCM audio data, sample rate). """ with contextlib.closing(wave.open(path, 'rb')) as wf: num_channels = wf.getnchannels() assert num_channels == 1 sample_width = wf.getsampwidth() assert sample_width == 2 sample_rate = wf.getframerate() assert sample_rate in (8000, 16000, 32000) frames = wf.getnframes() pcm_data = wf.readframes(frames) duration = frames / sample_rate return pcm_data, sample_rate, duration
def read_wave(path): """Reads a .wav file. Takes the path, and returns (PCM audio data, sample rate). """ with contextlib.closing(wave.open(path, 'rb')) as wf: num_channels = wf.getnchannels() assert num_channels == 1 sample_width = wf.getsampwidth() assert sample_width == 2 sample_rate = wf.getframerate() assert sample_rate in (8000, 16000, 32000) frames = wf.getnframes() pcm_data = wf.readframes(frames) duration = frames / sample_rate return pcm_data, sample_rate, duration
[ "Reads", "a", ".", "wav", "file", "." ]
mozilla/DeepSpeech
python
https://github.com/mozilla/DeepSpeech/blob/f64aa73e7fbe9dde40d4fcf23b42ab304747d152/examples/vad_transcriber/wavSplit.py#L6-L21
[ "def", "read_wave", "(", "path", ")", ":", "with", "contextlib", ".", "closing", "(", "wave", ".", "open", "(", "path", ",", "'rb'", ")", ")", "as", "wf", ":", "num_channels", "=", "wf", ".", "getnchannels", "(", ")", "assert", "num_channels", "==", ...
f64aa73e7fbe9dde40d4fcf23b42ab304747d152
train
write_wave
Writes a .wav file. Takes path, PCM audio data, and sample rate.
examples/vad_transcriber/wavSplit.py
def write_wave(path, audio, sample_rate): """Writes a .wav file. Takes path, PCM audio data, and sample rate. """ with contextlib.closing(wave.open(path, 'wb')) as wf: wf.setnchannels(1) wf.setsampwidth(2) wf.setframerate(sample_rate) wf.writeframes(audio)
def write_wave(path, audio, sample_rate): """Writes a .wav file. Takes path, PCM audio data, and sample rate. """ with contextlib.closing(wave.open(path, 'wb')) as wf: wf.setnchannels(1) wf.setsampwidth(2) wf.setframerate(sample_rate) wf.writeframes(audio)
[ "Writes", "a", ".", "wav", "file", "." ]
mozilla/DeepSpeech
python
https://github.com/mozilla/DeepSpeech/blob/f64aa73e7fbe9dde40d4fcf23b42ab304747d152/examples/vad_transcriber/wavSplit.py#L24-L33
[ "def", "write_wave", "(", "path", ",", "audio", ",", "sample_rate", ")", ":", "with", "contextlib", ".", "closing", "(", "wave", ".", "open", "(", "path", ",", "'wb'", ")", ")", "as", "wf", ":", "wf", ".", "setnchannels", "(", "1", ")", "wf", ".", ...
f64aa73e7fbe9dde40d4fcf23b42ab304747d152
train
frame_generator
Generates audio frames from PCM audio data. Takes the desired frame duration in milliseconds, the PCM data, and the sample rate. Yields Frames of the requested duration.
examples/vad_transcriber/wavSplit.py
def frame_generator(frame_duration_ms, audio, sample_rate): """Generates audio frames from PCM audio data. Takes the desired frame duration in milliseconds, the PCM data, and the sample rate. Yields Frames of the requested duration. """ n = int(sample_rate * (frame_duration_ms / 1000.0) * 2) offset = 0 timestamp = 0.0 duration = (float(n) / sample_rate) / 2.0 while offset + n < len(audio): yield Frame(audio[offset:offset + n], timestamp, duration) timestamp += duration offset += n
def frame_generator(frame_duration_ms, audio, sample_rate): """Generates audio frames from PCM audio data. Takes the desired frame duration in milliseconds, the PCM data, and the sample rate. Yields Frames of the requested duration. """ n = int(sample_rate * (frame_duration_ms / 1000.0) * 2) offset = 0 timestamp = 0.0 duration = (float(n) / sample_rate) / 2.0 while offset + n < len(audio): yield Frame(audio[offset:offset + n], timestamp, duration) timestamp += duration offset += n
[ "Generates", "audio", "frames", "from", "PCM", "audio", "data", "." ]
mozilla/DeepSpeech
python
https://github.com/mozilla/DeepSpeech/blob/f64aa73e7fbe9dde40d4fcf23b42ab304747d152/examples/vad_transcriber/wavSplit.py#L44-L59
[ "def", "frame_generator", "(", "frame_duration_ms", ",", "audio", ",", "sample_rate", ")", ":", "n", "=", "int", "(", "sample_rate", "*", "(", "frame_duration_ms", "/", "1000.0", ")", "*", "2", ")", "offset", "=", "0", "timestamp", "=", "0.0", "duration", ...
f64aa73e7fbe9dde40d4fcf23b42ab304747d152
train
Worker.run
Initialise the runner function with the passed args, kwargs
examples/vad_transcriber/audioTranscript_gui.py
def run(self): ''' Initialise the runner function with the passed args, kwargs ''' # Retrieve args/kwargs here; and fire up the processing using them try: transcript = self.fn(*self.args, **self.kwargs) except: traceback.print_exc() exctype, value = sys.exc_info()[:2] self.signals.error.emit((exctype, value, traceback.format_exc())) else: # Return the result of the processing self.signals.result.emit(transcript) finally: # Done self.signals.finished.emit()
def run(self): ''' Initialise the runner function with the passed args, kwargs ''' # Retrieve args/kwargs here; and fire up the processing using them try: transcript = self.fn(*self.args, **self.kwargs) except: traceback.print_exc() exctype, value = sys.exc_info()[:2] self.signals.error.emit((exctype, value, traceback.format_exc())) else: # Return the result of the processing self.signals.result.emit(transcript) finally: # Done self.signals.finished.emit()
[ "Initialise", "the", "runner", "function", "with", "the", "passed", "args", "kwargs" ]
mozilla/DeepSpeech
python
https://github.com/mozilla/DeepSpeech/blob/f64aa73e7fbe9dde40d4fcf23b42ab304747d152/examples/vad_transcriber/audioTranscript_gui.py#L71-L88
[ "def", "run", "(", "self", ")", ":", "# Retrieve args/kwargs here; and fire up the processing using them", "try", ":", "transcript", "=", "self", ".", "fn", "(", "*", "self", ".", "args", ",", "*", "*", "self", ".", "kwargs", ")", "except", ":", "traceback", ...
f64aa73e7fbe9dde40d4fcf23b42ab304747d152
train
exec_command
r''' Helper to exec locally (subprocess) or remotely (paramiko)
bin/benchmark_nc.py
def exec_command(command, cwd=None): r''' Helper to exec locally (subprocess) or remotely (paramiko) ''' rc = None stdout = stderr = None if ssh_conn is None: ld_library_path = {'LD_LIBRARY_PATH': '.:%s' % os.environ.get('LD_LIBRARY_PATH', '')} p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True, env=ld_library_path, cwd=cwd) stdout, stderr = p.communicate() rc = p.returncode else: # environment= requires paramiko >= 2.1 (fails with 2.0.2) final_command = command if cwd is None else 'cd %s && %s %s' % (cwd, 'LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH', command) ssh_stdin, ssh_stdout, ssh_stderr = ssh_conn.exec_command(final_command) stdout = ''.join(ssh_stdout.readlines()) stderr = ''.join(ssh_stderr.readlines()) rc = ssh_stdout.channel.recv_exit_status() return rc, stdout, stderr
def exec_command(command, cwd=None): r''' Helper to exec locally (subprocess) or remotely (paramiko) ''' rc = None stdout = stderr = None if ssh_conn is None: ld_library_path = {'LD_LIBRARY_PATH': '.:%s' % os.environ.get('LD_LIBRARY_PATH', '')} p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True, env=ld_library_path, cwd=cwd) stdout, stderr = p.communicate() rc = p.returncode else: # environment= requires paramiko >= 2.1 (fails with 2.0.2) final_command = command if cwd is None else 'cd %s && %s %s' % (cwd, 'LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH', command) ssh_stdin, ssh_stdout, ssh_stderr = ssh_conn.exec_command(final_command) stdout = ''.join(ssh_stdout.readlines()) stderr = ''.join(ssh_stderr.readlines()) rc = ssh_stdout.channel.recv_exit_status() return rc, stdout, stderr
[ "r", "Helper", "to", "exec", "locally", "(", "subprocess", ")", "or", "remotely", "(", "paramiko", ")" ]
mozilla/DeepSpeech
python
https://github.com/mozilla/DeepSpeech/blob/f64aa73e7fbe9dde40d4fcf23b42ab304747d152/bin/benchmark_nc.py#L41-L61
[ "def", "exec_command", "(", "command", ",", "cwd", "=", "None", ")", ":", "rc", "=", "None", "stdout", "=", "stderr", "=", "None", "if", "ssh_conn", "is", "None", ":", "ld_library_path", "=", "{", "'LD_LIBRARY_PATH'", ":", "'.:%s'", "%", "os", ".", "en...
f64aa73e7fbe9dde40d4fcf23b42ab304747d152
train
get_arch_string
r''' Check local or remote system arch, to produce TaskCluster proper link.
bin/benchmark_nc.py
def get_arch_string(): r''' Check local or remote system arch, to produce TaskCluster proper link. ''' rc, stdout, stderr = exec_command('uname -sm') if rc > 0: raise AssertionError('Error checking OS') stdout = stdout.lower().strip() if not 'linux' in stdout: raise AssertionError('Unsupported OS') if 'armv7l' in stdout: return 'arm' if 'x86_64' in stdout: nv_rc, nv_stdout, nv_stderr = exec_command('nvidia-smi') nv_stdout = nv_stdout.lower().strip() if 'NVIDIA-SMI' in nv_stdout: return 'gpu' else: return 'cpu' raise AssertionError('Unsupported arch:', stdout)
def get_arch_string(): r''' Check local or remote system arch, to produce TaskCluster proper link. ''' rc, stdout, stderr = exec_command('uname -sm') if rc > 0: raise AssertionError('Error checking OS') stdout = stdout.lower().strip() if not 'linux' in stdout: raise AssertionError('Unsupported OS') if 'armv7l' in stdout: return 'arm' if 'x86_64' in stdout: nv_rc, nv_stdout, nv_stderr = exec_command('nvidia-smi') nv_stdout = nv_stdout.lower().strip() if 'NVIDIA-SMI' in nv_stdout: return 'gpu' else: return 'cpu' raise AssertionError('Unsupported arch:', stdout)
[ "r", "Check", "local", "or", "remote", "system", "arch", "to", "produce", "TaskCluster", "proper", "link", "." ]
mozilla/DeepSpeech
python
https://github.com/mozilla/DeepSpeech/blob/f64aa73e7fbe9dde40d4fcf23b42ab304747d152/bin/benchmark_nc.py#L68-L91
[ "def", "get_arch_string", "(", ")", ":", "rc", ",", "stdout", ",", "stderr", "=", "exec_command", "(", "'uname -sm'", ")", "if", "rc", ">", "0", ":", "raise", "AssertionError", "(", "'Error checking OS'", ")", "stdout", "=", "stdout", ".", "lower", "(", ...
f64aa73e7fbe9dde40d4fcf23b42ab304747d152
train
extract_native_client_tarball
r''' Download a native_client.tar.xz file from TaskCluster and extract it to dir.
bin/benchmark_nc.py
def extract_native_client_tarball(dir): r''' Download a native_client.tar.xz file from TaskCluster and extract it to dir. ''' assert_valid_dir(dir) target_tarball = os.path.join(dir, 'native_client.tar.xz') if os.path.isfile(target_tarball) and os.stat(target_tarball).st_size == 0: return subprocess.check_call(['pixz', '-d', 'native_client.tar.xz'], cwd=dir) subprocess.check_call(['tar', 'xf', 'native_client.tar'], cwd=dir) os.unlink(os.path.join(dir, 'native_client.tar')) open(target_tarball, 'w').close()
def extract_native_client_tarball(dir): r''' Download a native_client.tar.xz file from TaskCluster and extract it to dir. ''' assert_valid_dir(dir) target_tarball = os.path.join(dir, 'native_client.tar.xz') if os.path.isfile(target_tarball) and os.stat(target_tarball).st_size == 0: return subprocess.check_call(['pixz', '-d', 'native_client.tar.xz'], cwd=dir) subprocess.check_call(['tar', 'xf', 'native_client.tar'], cwd=dir) os.unlink(os.path.join(dir, 'native_client.tar')) open(target_tarball, 'w').close()
[ "r", "Download", "a", "native_client", ".", "tar", ".", "xz", "file", "from", "TaskCluster", "and", "extract", "it", "to", "dir", "." ]
mozilla/DeepSpeech
python
https://github.com/mozilla/DeepSpeech/blob/f64aa73e7fbe9dde40d4fcf23b42ab304747d152/bin/benchmark_nc.py#L97-L110
[ "def", "extract_native_client_tarball", "(", "dir", ")", ":", "assert_valid_dir", "(", "dir", ")", "target_tarball", "=", "os", ".", "path", ".", "join", "(", "dir", ",", "'native_client.tar.xz'", ")", "if", "os", ".", "path", ".", "isfile", "(", "target_tar...
f64aa73e7fbe9dde40d4fcf23b42ab304747d152
train
is_zip_file
r''' Ensure that a path is a zip file by: - checking length is 1 - checking extension is '.zip'
bin/benchmark_nc.py
def is_zip_file(models): r''' Ensure that a path is a zip file by: - checking length is 1 - checking extension is '.zip' ''' ext = os.path.splitext(models[0])[1] return (len(models) == 1) and (ext == '.zip')
def is_zip_file(models): r''' Ensure that a path is a zip file by: - checking length is 1 - checking extension is '.zip' ''' ext = os.path.splitext(models[0])[1] return (len(models) == 1) and (ext == '.zip')
[ "r", "Ensure", "that", "a", "path", "is", "a", "zip", "file", "by", ":", "-", "checking", "length", "is", "1", "-", "checking", "extension", "is", ".", "zip" ]
mozilla/DeepSpeech
python
https://github.com/mozilla/DeepSpeech/blob/f64aa73e7fbe9dde40d4fcf23b42ab304747d152/bin/benchmark_nc.py#L112-L119
[ "def", "is_zip_file", "(", "models", ")", ":", "ext", "=", "os", ".", "path", ".", "splitext", "(", "models", "[", "0", "]", ")", "[", "1", "]", "return", "(", "len", "(", "models", ")", "==", "1", ")", "and", "(", "ext", "==", "'.zip'", ")" ]
f64aa73e7fbe9dde40d4fcf23b42ab304747d152
train
maybe_inspect_zip
r''' Detect if models is a list of protocolbuffer files or a ZIP file. If the latter, then unzip it and return the list of protocolbuffer files that were inside.
bin/benchmark_nc.py
def maybe_inspect_zip(models): r''' Detect if models is a list of protocolbuffer files or a ZIP file. If the latter, then unzip it and return the list of protocolbuffer files that were inside. ''' if not(is_zip_file(models)): return models if len(models) > 1: return models if len(models) < 1: raise AssertionError('No models at all') return zipfile.ZipFile(models[0]).namelist()
def maybe_inspect_zip(models): r''' Detect if models is a list of protocolbuffer files or a ZIP file. If the latter, then unzip it and return the list of protocolbuffer files that were inside. ''' if not(is_zip_file(models)): return models if len(models) > 1: return models if len(models) < 1: raise AssertionError('No models at all') return zipfile.ZipFile(models[0]).namelist()
[ "r", "Detect", "if", "models", "is", "a", "list", "of", "protocolbuffer", "files", "or", "a", "ZIP", "file", ".", "If", "the", "latter", "then", "unzip", "it", "and", "return", "the", "list", "of", "protocolbuffer", "files", "that", "were", "inside", "."...
mozilla/DeepSpeech
python
https://github.com/mozilla/DeepSpeech/blob/f64aa73e7fbe9dde40d4fcf23b42ab304747d152/bin/benchmark_nc.py#L121-L137
[ "def", "maybe_inspect_zip", "(", "models", ")", ":", "if", "not", "(", "is_zip_file", "(", "models", ")", ")", ":", "return", "models", "if", "len", "(", "models", ")", ">", "1", ":", "return", "models", "if", "len", "(", "models", ")", "<", "1", "...
f64aa73e7fbe9dde40d4fcf23b42ab304747d152
train
all_files
r''' Return a list of full path of files matching 'models', sorted in human numerical order (i.e., 0 1 2 ..., 10 11 12, ..., 100, ..., 1000). Files are supposed to be named identically except one variable component e.g. the list, test.weights.e5.lstm1200.ldc93s1.pb test.weights.e5.lstm1000.ldc93s1.pb test.weights.e5.lstm800.ldc93s1.pb gets sorted: test.weights.e5.lstm800.ldc93s1.pb test.weights.e5.lstm1000.ldc93s1.pb test.weights.e5.lstm1200.ldc93s1.pb
bin/benchmark_nc.py
def all_files(models=[]): r''' Return a list of full path of files matching 'models', sorted in human numerical order (i.e., 0 1 2 ..., 10 11 12, ..., 100, ..., 1000). Files are supposed to be named identically except one variable component e.g. the list, test.weights.e5.lstm1200.ldc93s1.pb test.weights.e5.lstm1000.ldc93s1.pb test.weights.e5.lstm800.ldc93s1.pb gets sorted: test.weights.e5.lstm800.ldc93s1.pb test.weights.e5.lstm1000.ldc93s1.pb test.weights.e5.lstm1200.ldc93s1.pb ''' def nsort(a, b): fa = os.path.basename(a).split('.') fb = os.path.basename(b).split('.') elements_to_remove = [] assert len(fa) == len(fb) for i in range(0, len(fa)): if fa[i] == fb[i]: elements_to_remove.append(fa[i]) for e in elements_to_remove: fa.remove(e) fb.remove(e) assert len(fa) == len(fb) assert len(fa) == 1 fa = keep_only_digits(fa[0]) fb = keep_only_digits(fb[0]) if fa < fb: return -1 if fa == fb: return 0 if fa > fb: return 1 base = list(map(lambda x: os.path.abspath(x), maybe_inspect_zip(models))) base.sort(cmp=nsort) return base
def all_files(models=[]): r''' Return a list of full path of files matching 'models', sorted in human numerical order (i.e., 0 1 2 ..., 10 11 12, ..., 100, ..., 1000). Files are supposed to be named identically except one variable component e.g. the list, test.weights.e5.lstm1200.ldc93s1.pb test.weights.e5.lstm1000.ldc93s1.pb test.weights.e5.lstm800.ldc93s1.pb gets sorted: test.weights.e5.lstm800.ldc93s1.pb test.weights.e5.lstm1000.ldc93s1.pb test.weights.e5.lstm1200.ldc93s1.pb ''' def nsort(a, b): fa = os.path.basename(a).split('.') fb = os.path.basename(b).split('.') elements_to_remove = [] assert len(fa) == len(fb) for i in range(0, len(fa)): if fa[i] == fb[i]: elements_to_remove.append(fa[i]) for e in elements_to_remove: fa.remove(e) fb.remove(e) assert len(fa) == len(fb) assert len(fa) == 1 fa = keep_only_digits(fa[0]) fb = keep_only_digits(fb[0]) if fa < fb: return -1 if fa == fb: return 0 if fa > fb: return 1 base = list(map(lambda x: os.path.abspath(x), maybe_inspect_zip(models))) base.sort(cmp=nsort) return base
[ "r", "Return", "a", "list", "of", "full", "path", "of", "files", "matching", "models", "sorted", "in", "human", "numerical", "order", "(", "i", ".", "e", ".", "0", "1", "2", "...", "10", "11", "12", "...", "100", "...", "1000", ")", "." ]
mozilla/DeepSpeech
python
https://github.com/mozilla/DeepSpeech/blob/f64aa73e7fbe9dde40d4fcf23b42ab304747d152/bin/benchmark_nc.py#L139-L186
[ "def", "all_files", "(", "models", "=", "[", "]", ")", ":", "def", "nsort", "(", "a", ",", "b", ")", ":", "fa", "=", "os", ".", "path", ".", "basename", "(", "a", ")", ".", "split", "(", "'.'", ")", "fb", "=", "os", ".", "path", ".", "basen...
f64aa73e7fbe9dde40d4fcf23b42ab304747d152
train
setup_tempdir
r''' Copy models, libs and binary to a directory (new one if dir is None)
bin/benchmark_nc.py
def setup_tempdir(dir, models, wav, alphabet, lm_binary, trie, binaries): r''' Copy models, libs and binary to a directory (new one if dir is None) ''' if dir is None: dir = tempfile.mkdtemp(suffix='dsbench') sorted_models = all_files(models=models) if binaries is None: maybe_download_binaries(dir) else: print('Using local binaries: %s' % (binaries)) shutil.copy2(binaries, dir) extract_native_client_tarball(dir) filenames = map(lambda x: os.path.join(dir, os.path.basename(x)), sorted_models) missing_models = filter(lambda x: not os.path.isfile(x), filenames) if len(missing_models) > 0: # If we have a ZIP file, directly extract it to the proper path if is_zip_file(models): print('Extracting %s to %s' % (models[0], dir)) zipfile.ZipFile(models[0]).extractall(path=dir) print('Extracted %s.' % models[0]) else: # If one model is missing, let's copy everything again. Be safe. for f in sorted_models: print('Copying %s to %s' % (f, dir)) shutil.copy2(f, dir) for extra_file in [ wav, alphabet, lm_binary, trie ]: if extra_file and not os.path.isfile(os.path.join(dir, os.path.basename(extra_file))): print('Copying %s to %s' % (extra_file, dir)) shutil.copy2(extra_file, dir) if ssh_conn: copy_tree(dir) return dir, sorted_models
def setup_tempdir(dir, models, wav, alphabet, lm_binary, trie, binaries): r''' Copy models, libs and binary to a directory (new one if dir is None) ''' if dir is None: dir = tempfile.mkdtemp(suffix='dsbench') sorted_models = all_files(models=models) if binaries is None: maybe_download_binaries(dir) else: print('Using local binaries: %s' % (binaries)) shutil.copy2(binaries, dir) extract_native_client_tarball(dir) filenames = map(lambda x: os.path.join(dir, os.path.basename(x)), sorted_models) missing_models = filter(lambda x: not os.path.isfile(x), filenames) if len(missing_models) > 0: # If we have a ZIP file, directly extract it to the proper path if is_zip_file(models): print('Extracting %s to %s' % (models[0], dir)) zipfile.ZipFile(models[0]).extractall(path=dir) print('Extracted %s.' % models[0]) else: # If one model is missing, let's copy everything again. Be safe. for f in sorted_models: print('Copying %s to %s' % (f, dir)) shutil.copy2(f, dir) for extra_file in [ wav, alphabet, lm_binary, trie ]: if extra_file and not os.path.isfile(os.path.join(dir, os.path.basename(extra_file))): print('Copying %s to %s' % (extra_file, dir)) shutil.copy2(extra_file, dir) if ssh_conn: copy_tree(dir) return dir, sorted_models
[ "r", "Copy", "models", "libs", "and", "binary", "to", "a", "directory", "(", "new", "one", "if", "dir", "is", "None", ")" ]
mozilla/DeepSpeech
python
https://github.com/mozilla/DeepSpeech/blob/f64aa73e7fbe9dde40d4fcf23b42ab304747d152/bin/benchmark_nc.py#L241-L278
[ "def", "setup_tempdir", "(", "dir", ",", "models", ",", "wav", ",", "alphabet", ",", "lm_binary", ",", "trie", ",", "binaries", ")", ":", "if", "dir", "is", "None", ":", "dir", "=", "tempfile", ".", "mkdtemp", "(", "suffix", "=", "'dsbench'", ")", "s...
f64aa73e7fbe9dde40d4fcf23b42ab304747d152
train
teardown_tempdir
r''' Cleanup temporary directory.
bin/benchmark_nc.py
def teardown_tempdir(dir): r''' Cleanup temporary directory. ''' if ssh_conn: delete_tree(dir) assert_valid_dir(dir) shutil.rmtree(dir)
def teardown_tempdir(dir): r''' Cleanup temporary directory. ''' if ssh_conn: delete_tree(dir) assert_valid_dir(dir) shutil.rmtree(dir)
[ "r", "Cleanup", "temporary", "directory", "." ]
mozilla/DeepSpeech
python
https://github.com/mozilla/DeepSpeech/blob/f64aa73e7fbe9dde40d4fcf23b42ab304747d152/bin/benchmark_nc.py#L280-L289
[ "def", "teardown_tempdir", "(", "dir", ")", ":", "if", "ssh_conn", ":", "delete_tree", "(", "dir", ")", "assert_valid_dir", "(", "dir", ")", "shutil", ".", "rmtree", "(", "dir", ")" ]
f64aa73e7fbe9dde40d4fcf23b42ab304747d152
train
get_sshconfig
r''' Read user's SSH configuration file
bin/benchmark_nc.py
def get_sshconfig(): r''' Read user's SSH configuration file ''' with open(os.path.expanduser('~/.ssh/config')) as f: cfg = paramiko.SSHConfig() cfg.parse(f) ret_dict = {} for d in cfg._config: _copy = dict(d) # Avoid buggy behavior with strange host definitions, we need # Hostname and not Host. del _copy['host'] for host in d['host']: ret_dict[host] = _copy['config'] return ret_dict
def get_sshconfig(): r''' Read user's SSH configuration file ''' with open(os.path.expanduser('~/.ssh/config')) as f: cfg = paramiko.SSHConfig() cfg.parse(f) ret_dict = {} for d in cfg._config: _copy = dict(d) # Avoid buggy behavior with strange host definitions, we need # Hostname and not Host. del _copy['host'] for host in d['host']: ret_dict[host] = _copy['config'] return ret_dict
[ "r", "Read", "user", "s", "SSH", "configuration", "file" ]
mozilla/DeepSpeech
python
https://github.com/mozilla/DeepSpeech/blob/f64aa73e7fbe9dde40d4fcf23b42ab304747d152/bin/benchmark_nc.py#L291-L308
[ "def", "get_sshconfig", "(", ")", ":", "with", "open", "(", "os", ".", "path", ".", "expanduser", "(", "'~/.ssh/config'", ")", ")", "as", "f", ":", "cfg", "=", "paramiko", ".", "SSHConfig", "(", ")", "cfg", ".", "parse", "(", "f", ")", "ret_dict", ...
f64aa73e7fbe9dde40d4fcf23b42ab304747d152
train
establish_ssh
r''' Establish a SSH connection to a remote host. It should be able to use SSH's config file Host name declarations. By default, will not automatically add trust for hosts, will use SSH agent and will try to load keys.
bin/benchmark_nc.py
def establish_ssh(target=None, auto_trust=False, allow_agent=True, look_keys=True): r''' Establish a SSH connection to a remote host. It should be able to use SSH's config file Host name declarations. By default, will not automatically add trust for hosts, will use SSH agent and will try to load keys. ''' def password_prompt(username, hostname): r''' If the Host is relying on password authentication, lets ask it. Relying on SSH itself to take care of that would not work when the remote authentication is password behind a SSH-key+2FA jumphost. ''' return getpass.getpass('No SSH key for %s@%s, please provide password: ' % (username, hostname)) ssh_conn = None if target is not None: ssh_conf = get_sshconfig() cfg = { 'hostname': None, 'port': 22, 'allow_agent': allow_agent, 'look_for_keys': look_keys } if ssh_conf.has_key(target): user_config = ssh_conf.get(target) # If ssh_config file's Host defined 'User' instead of 'Username' if user_config.has_key('user') and not user_config.has_key('username'): user_config['username'] = user_config['user'] del user_config['user'] for k in ('username', 'hostname', 'port'): if k in user_config: cfg[k] = user_config[k] # Assume Password auth. If we don't do that, then when connecting # through a jumphost we will run into issues and the user will # not be able to input his password to the SSH prompt. if 'identityfile' in user_config: cfg['key_filename'] = user_config['identityfile'] else: cfg['password'] = password_prompt(cfg['username'], cfg['hostname'] or target) # Should be the last one, since ProxyCommand will issue connection to remote host if 'proxycommand' in user_config: cfg['sock'] = paramiko.ProxyCommand(user_config['proxycommand']) else: cfg['username'] = target.split('@')[0] cfg['hostname'] = target.split('@')[1].split(':')[0] cfg['password'] = password_prompt(cfg['username'], cfg['hostname']) try: cfg['port'] = int(target.split('@')[1].split(':')[1]) except IndexError: # IndexError will happen if no :PORT is there. # Default value 22 is defined above in 'cfg'. pass ssh_conn = paramiko.SSHClient() if auto_trust: ssh_conn.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh_conn.connect(**cfg) return ssh_conn
def establish_ssh(target=None, auto_trust=False, allow_agent=True, look_keys=True): r''' Establish a SSH connection to a remote host. It should be able to use SSH's config file Host name declarations. By default, will not automatically add trust for hosts, will use SSH agent and will try to load keys. ''' def password_prompt(username, hostname): r''' If the Host is relying on password authentication, lets ask it. Relying on SSH itself to take care of that would not work when the remote authentication is password behind a SSH-key+2FA jumphost. ''' return getpass.getpass('No SSH key for %s@%s, please provide password: ' % (username, hostname)) ssh_conn = None if target is not None: ssh_conf = get_sshconfig() cfg = { 'hostname': None, 'port': 22, 'allow_agent': allow_agent, 'look_for_keys': look_keys } if ssh_conf.has_key(target): user_config = ssh_conf.get(target) # If ssh_config file's Host defined 'User' instead of 'Username' if user_config.has_key('user') and not user_config.has_key('username'): user_config['username'] = user_config['user'] del user_config['user'] for k in ('username', 'hostname', 'port'): if k in user_config: cfg[k] = user_config[k] # Assume Password auth. If we don't do that, then when connecting # through a jumphost we will run into issues and the user will # not be able to input his password to the SSH prompt. if 'identityfile' in user_config: cfg['key_filename'] = user_config['identityfile'] else: cfg['password'] = password_prompt(cfg['username'], cfg['hostname'] or target) # Should be the last one, since ProxyCommand will issue connection to remote host if 'proxycommand' in user_config: cfg['sock'] = paramiko.ProxyCommand(user_config['proxycommand']) else: cfg['username'] = target.split('@')[0] cfg['hostname'] = target.split('@')[1].split(':')[0] cfg['password'] = password_prompt(cfg['username'], cfg['hostname']) try: cfg['port'] = int(target.split('@')[1].split(':')[1]) except IndexError: # IndexError will happen if no :PORT is there. # Default value 22 is defined above in 'cfg'. pass ssh_conn = paramiko.SSHClient() if auto_trust: ssh_conn.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh_conn.connect(**cfg) return ssh_conn
[ "r", "Establish", "a", "SSH", "connection", "to", "a", "remote", "host", ".", "It", "should", "be", "able", "to", "use", "SSH", "s", "config", "file", "Host", "name", "declarations", ".", "By", "default", "will", "not", "automatically", "add", "trust", "...
mozilla/DeepSpeech
python
https://github.com/mozilla/DeepSpeech/blob/f64aa73e7fbe9dde40d4fcf23b42ab304747d152/bin/benchmark_nc.py#L310-L375
[ "def", "establish_ssh", "(", "target", "=", "None", ",", "auto_trust", "=", "False", ",", "allow_agent", "=", "True", ",", "look_keys", "=", "True", ")", ":", "def", "password_prompt", "(", "username", ",", "hostname", ")", ":", "r'''\n If the Host is r...
f64aa73e7fbe9dde40d4fcf23b42ab304747d152
train
run_benchmarks
r''' Core of the running of the benchmarks. We will run on all of models, against the WAV file provided as wav, and the provided alphabet.
bin/benchmark_nc.py
def run_benchmarks(dir, models, wav, alphabet, lm_binary=None, trie=None, iters=-1): r''' Core of the running of the benchmarks. We will run on all of models, against the WAV file provided as wav, and the provided alphabet. ''' assert_valid_dir(dir) inference_times = [ ] for model in models: model_filename = model current_model = { 'name': model, 'iters': [ ], 'mean': numpy.infty, 'stddev': numpy.infty } if lm_binary and trie: cmdline = './deepspeech --model "%s" --alphabet "%s" --lm "%s" --trie "%s" --audio "%s" -t' % (model_filename, alphabet, lm_binary, trie, wav) else: cmdline = './deepspeech --model "%s" --alphabet "%s" --audio "%s" -t' % (model_filename, alphabet, wav) for it in range(iters): sys.stdout.write('\rRunning %s: %d/%d' % (os.path.basename(model), (it+1), iters)) sys.stdout.flush() rc, stdout, stderr = exec_command(cmdline, cwd=dir) if rc == 0: inference_time = float(stdout.split('\n')[1].split('=')[-1]) # print("[%d] model=%s inference=%f" % (it, model, inference_time)) current_model['iters'].append(inference_time) else: print('exec_command("%s") failed with rc=%d' % (cmdline, rc)) print('stdout: %s' % stdout) print('stderr: %s' % stderr) raise AssertionError('Execution failure: rc=%d' % (rc)) sys.stdout.write('\n') sys.stdout.flush() current_model['mean'] = numpy.mean(current_model['iters']) current_model['stddev'] = numpy.std(current_model['iters']) inference_times.append(current_model) return inference_times
def run_benchmarks(dir, models, wav, alphabet, lm_binary=None, trie=None, iters=-1): r''' Core of the running of the benchmarks. We will run on all of models, against the WAV file provided as wav, and the provided alphabet. ''' assert_valid_dir(dir) inference_times = [ ] for model in models: model_filename = model current_model = { 'name': model, 'iters': [ ], 'mean': numpy.infty, 'stddev': numpy.infty } if lm_binary and trie: cmdline = './deepspeech --model "%s" --alphabet "%s" --lm "%s" --trie "%s" --audio "%s" -t' % (model_filename, alphabet, lm_binary, trie, wav) else: cmdline = './deepspeech --model "%s" --alphabet "%s" --audio "%s" -t' % (model_filename, alphabet, wav) for it in range(iters): sys.stdout.write('\rRunning %s: %d/%d' % (os.path.basename(model), (it+1), iters)) sys.stdout.flush() rc, stdout, stderr = exec_command(cmdline, cwd=dir) if rc == 0: inference_time = float(stdout.split('\n')[1].split('=')[-1]) # print("[%d] model=%s inference=%f" % (it, model, inference_time)) current_model['iters'].append(inference_time) else: print('exec_command("%s") failed with rc=%d' % (cmdline, rc)) print('stdout: %s' % stdout) print('stderr: %s' % stderr) raise AssertionError('Execution failure: rc=%d' % (rc)) sys.stdout.write('\n') sys.stdout.flush() current_model['mean'] = numpy.mean(current_model['iters']) current_model['stddev'] = numpy.std(current_model['iters']) inference_times.append(current_model) return inference_times
[ "r", "Core", "of", "the", "running", "of", "the", "benchmarks", ".", "We", "will", "run", "on", "all", "of", "models", "against", "the", "WAV", "file", "provided", "as", "wav", "and", "the", "provided", "alphabet", "." ]
mozilla/DeepSpeech
python
https://github.com/mozilla/DeepSpeech/blob/f64aa73e7fbe9dde40d4fcf23b42ab304747d152/bin/benchmark_nc.py#L377-L422
[ "def", "run_benchmarks", "(", "dir", ",", "models", ",", "wav", ",", "alphabet", ",", "lm_binary", "=", "None", ",", "trie", "=", "None", ",", "iters", "=", "-", "1", ")", ":", "assert_valid_dir", "(", "dir", ")", "inference_times", "=", "[", "]", "f...
f64aa73e7fbe9dde40d4fcf23b42ab304747d152
train
produce_csv
r''' Take an input dictionnary and write it to the object-file output.
bin/benchmark_nc.py
def produce_csv(input, output): r''' Take an input dictionnary and write it to the object-file output. ''' output.write('"model","mean","std"\n') for model_data in input: output.write('"%s",%f,%f\n' % (model_data['name'], model_data['mean'], model_data['stddev'])) output.flush() output.close() print("Wrote as %s" % output.name)
def produce_csv(input, output): r''' Take an input dictionnary and write it to the object-file output. ''' output.write('"model","mean","std"\n') for model_data in input: output.write('"%s",%f,%f\n' % (model_data['name'], model_data['mean'], model_data['stddev'])) output.flush() output.close() print("Wrote as %s" % output.name)
[ "r", "Take", "an", "input", "dictionnary", "and", "write", "it", "to", "the", "object", "-", "file", "output", "." ]
mozilla/DeepSpeech
python
https://github.com/mozilla/DeepSpeech/blob/f64aa73e7fbe9dde40d4fcf23b42ab304747d152/bin/benchmark_nc.py#L424-L433
[ "def", "produce_csv", "(", "input", ",", "output", ")", ":", "output", ".", "write", "(", "'\"model\",\"mean\",\"std\"\\n'", ")", "for", "model_data", "in", "input", ":", "output", ".", "write", "(", "'\"%s\",%f,%f\\n'", "%", "(", "model_data", "[", "'name'", ...
f64aa73e7fbe9dde40d4fcf23b42ab304747d152
train
to_sparse_tuple
r"""Creates a sparse representention of ``sequence``. Returns a tuple with (indices, values, shape)
util/feeding.py
def to_sparse_tuple(sequence): r"""Creates a sparse representention of ``sequence``. Returns a tuple with (indices, values, shape) """ indices = np.asarray(list(zip([0]*len(sequence), range(len(sequence)))), dtype=np.int64) shape = np.asarray([1, len(sequence)], dtype=np.int64) return indices, sequence, shape
def to_sparse_tuple(sequence): r"""Creates a sparse representention of ``sequence``. Returns a tuple with (indices, values, shape) """ indices = np.asarray(list(zip([0]*len(sequence), range(len(sequence)))), dtype=np.int64) shape = np.asarray([1, len(sequence)], dtype=np.int64) return indices, sequence, shape
[ "r", "Creates", "a", "sparse", "representention", "of", "sequence", ".", "Returns", "a", "tuple", "with", "(", "indices", "values", "shape", ")" ]
mozilla/DeepSpeech
python
https://github.com/mozilla/DeepSpeech/blob/f64aa73e7fbe9dde40d4fcf23b42ab304747d152/util/feeding.py#L57-L63
[ "def", "to_sparse_tuple", "(", "sequence", ")", ":", "indices", "=", "np", ".", "asarray", "(", "list", "(", "zip", "(", "[", "0", "]", "*", "len", "(", "sequence", ")", ",", "range", "(", "len", "(", "sequence", ")", ")", ")", ")", ",", "dtype",...
f64aa73e7fbe9dde40d4fcf23b42ab304747d152
train
_parallel_downloader
Generate a function to download a file based on given parameters This works by currying the above given arguments into a closure in the form of the following function. :param voxforge_url: the base voxforge URL :param archive_dir: the location to store the downloaded file :param total: the total number of files to download :param counter: an atomic counter to keep track of # of downloaded files :return: a function that actually downloads a file given these params
bin/import_voxforge.py
def _parallel_downloader(voxforge_url, archive_dir, total, counter): """Generate a function to download a file based on given parameters This works by currying the above given arguments into a closure in the form of the following function. :param voxforge_url: the base voxforge URL :param archive_dir: the location to store the downloaded file :param total: the total number of files to download :param counter: an atomic counter to keep track of # of downloaded files :return: a function that actually downloads a file given these params """ def download(d): """Binds voxforge_url, archive_dir, total, and counter into this scope Downloads the given file :param d: a tuple consisting of (index, file) where index is the index of the file to download and file is the name of the file to download """ (i, file) = d download_url = voxforge_url + '/' + file c = counter.increment() print('Downloading file {} ({}/{})...'.format(i+1, c, total)) maybe_download(filename_of(download_url), archive_dir, download_url) return download
def _parallel_downloader(voxforge_url, archive_dir, total, counter): """Generate a function to download a file based on given parameters This works by currying the above given arguments into a closure in the form of the following function. :param voxforge_url: the base voxforge URL :param archive_dir: the location to store the downloaded file :param total: the total number of files to download :param counter: an atomic counter to keep track of # of downloaded files :return: a function that actually downloads a file given these params """ def download(d): """Binds voxforge_url, archive_dir, total, and counter into this scope Downloads the given file :param d: a tuple consisting of (index, file) where index is the index of the file to download and file is the name of the file to download """ (i, file) = d download_url = voxforge_url + '/' + file c = counter.increment() print('Downloading file {} ({}/{})...'.format(i+1, c, total)) maybe_download(filename_of(download_url), archive_dir, download_url) return download
[ "Generate", "a", "function", "to", "download", "a", "file", "based", "on", "given", "parameters", "This", "works", "by", "currying", "the", "above", "given", "arguments", "into", "a", "closure", "in", "the", "form", "of", "the", "following", "function", "." ...
mozilla/DeepSpeech
python
https://github.com/mozilla/DeepSpeech/blob/f64aa73e7fbe9dde40d4fcf23b42ab304747d152/bin/import_voxforge.py#L50-L72
[ "def", "_parallel_downloader", "(", "voxforge_url", ",", "archive_dir", ",", "total", ",", "counter", ")", ":", "def", "download", "(", "d", ")", ":", "\"\"\"Binds voxforge_url, archive_dir, total, and counter into this scope\n Downloads the given file\n :param d: a...
f64aa73e7fbe9dde40d4fcf23b42ab304747d152
train
_parallel_extracter
Generate a function to extract a tar file based on given parameters This works by currying the above given arguments into a closure in the form of the following function. :param data_dir: the target directory to extract into :param number_of_test: the number of files to keep as the test set :param number_of_dev: the number of files to keep as the dev set :param total: the total number of files to extract :param counter: an atomic counter to keep track of # of extracted files :return: a function that actually extracts a tar file given these params
bin/import_voxforge.py
def _parallel_extracter(data_dir, number_of_test, number_of_dev, total, counter): """Generate a function to extract a tar file based on given parameters This works by currying the above given arguments into a closure in the form of the following function. :param data_dir: the target directory to extract into :param number_of_test: the number of files to keep as the test set :param number_of_dev: the number of files to keep as the dev set :param total: the total number of files to extract :param counter: an atomic counter to keep track of # of extracted files :return: a function that actually extracts a tar file given these params """ def extract(d): """Binds data_dir, number_of_test, number_of_dev, total, and counter into this scope Extracts the given file :param d: a tuple consisting of (index, file) where index is the index of the file to extract and file is the name of the file to extract """ (i, archive) = d if i < number_of_test: dataset_dir = path.join(data_dir, "test") elif i<number_of_test+number_of_dev: dataset_dir = path.join(data_dir, "dev") else: dataset_dir = path.join(data_dir, "train") if not gfile.Exists(path.join(dataset_dir, '.'.join(filename_of(archive).split(".")[:-1]))): c = counter.increment() print('Extracting file {} ({}/{})...'.format(i+1, c, total)) tar = tarfile.open(archive) tar.extractall(dataset_dir) tar.close() return extract
def _parallel_extracter(data_dir, number_of_test, number_of_dev, total, counter): """Generate a function to extract a tar file based on given parameters This works by currying the above given arguments into a closure in the form of the following function. :param data_dir: the target directory to extract into :param number_of_test: the number of files to keep as the test set :param number_of_dev: the number of files to keep as the dev set :param total: the total number of files to extract :param counter: an atomic counter to keep track of # of extracted files :return: a function that actually extracts a tar file given these params """ def extract(d): """Binds data_dir, number_of_test, number_of_dev, total, and counter into this scope Extracts the given file :param d: a tuple consisting of (index, file) where index is the index of the file to extract and file is the name of the file to extract """ (i, archive) = d if i < number_of_test: dataset_dir = path.join(data_dir, "test") elif i<number_of_test+number_of_dev: dataset_dir = path.join(data_dir, "dev") else: dataset_dir = path.join(data_dir, "train") if not gfile.Exists(path.join(dataset_dir, '.'.join(filename_of(archive).split(".")[:-1]))): c = counter.increment() print('Extracting file {} ({}/{})...'.format(i+1, c, total)) tar = tarfile.open(archive) tar.extractall(dataset_dir) tar.close() return extract
[ "Generate", "a", "function", "to", "extract", "a", "tar", "file", "based", "on", "given", "parameters", "This", "works", "by", "currying", "the", "above", "given", "arguments", "into", "a", "closure", "in", "the", "form", "of", "the", "following", "function"...
mozilla/DeepSpeech
python
https://github.com/mozilla/DeepSpeech/blob/f64aa73e7fbe9dde40d4fcf23b42ab304747d152/bin/import_voxforge.py#L74-L105
[ "def", "_parallel_extracter", "(", "data_dir", ",", "number_of_test", ",", "number_of_dev", ",", "total", ",", "counter", ")", ":", "def", "extract", "(", "d", ")", ":", "\"\"\"Binds data_dir, number_of_test, number_of_dev, total, and counter into this scope\n Extracts...
f64aa73e7fbe9dde40d4fcf23b42ab304747d152
train
AtomicCounter.increment
Increments the counter by the given amount :param amount: the amount to increment by (default 1) :return: the incremented value of the counter
bin/import_voxforge.py
def increment(self, amount=1): """Increments the counter by the given amount :param amount: the amount to increment by (default 1) :return: the incremented value of the counter """ self.__lock.acquire() self.__count += amount v = self.value() self.__lock.release() return v
def increment(self, amount=1): """Increments the counter by the given amount :param amount: the amount to increment by (default 1) :return: the incremented value of the counter """ self.__lock.acquire() self.__count += amount v = self.value() self.__lock.release() return v
[ "Increments", "the", "counter", "by", "the", "given", "amount", ":", "param", "amount", ":", "the", "amount", "to", "increment", "by", "(", "default", "1", ")", ":", "return", ":", "the", "incremented", "value", "of", "the", "counter" ]
mozilla/DeepSpeech
python
https://github.com/mozilla/DeepSpeech/blob/f64aa73e7fbe9dde40d4fcf23b42ab304747d152/bin/import_voxforge.py#L35-L44
[ "def", "increment", "(", "self", ",", "amount", "=", "1", ")", ":", "self", ".", "__lock", ".", "acquire", "(", ")", "self", ".", "__count", "+=", "amount", "v", "=", "self", ".", "value", "(", ")", "self", ".", "__lock", ".", "release", "(", ")"...
f64aa73e7fbe9dde40d4fcf23b42ab304747d152
train
calculate_report
r''' This routine will calculate a WER report. It'll compute the `mean` WER and create ``Sample`` objects of the ``report_count`` top lowest loss items from the provided WER results tuple (only items with WER!=0 and ordered by their WER).
util/evaluate_tools.py
def calculate_report(labels, decodings, distances, losses): r''' This routine will calculate a WER report. It'll compute the `mean` WER and create ``Sample`` objects of the ``report_count`` top lowest loss items from the provided WER results tuple (only items with WER!=0 and ordered by their WER). ''' samples = pmap(process_decode_result, zip(labels, decodings, distances, losses)) # Getting the WER and CER from the accumulated edit distances and lengths samples_wer, samples_cer = wer_cer_batch(labels, decodings) # Order the remaining items by their loss (lowest loss on top) samples.sort(key=lambda s: s.loss) # Then order by WER (highest WER on top) samples.sort(key=lambda s: s.wer, reverse=True) return samples_wer, samples_cer, samples
def calculate_report(labels, decodings, distances, losses): r''' This routine will calculate a WER report. It'll compute the `mean` WER and create ``Sample`` objects of the ``report_count`` top lowest loss items from the provided WER results tuple (only items with WER!=0 and ordered by their WER). ''' samples = pmap(process_decode_result, zip(labels, decodings, distances, losses)) # Getting the WER and CER from the accumulated edit distances and lengths samples_wer, samples_cer = wer_cer_batch(labels, decodings) # Order the remaining items by their loss (lowest loss on top) samples.sort(key=lambda s: s.loss) # Then order by WER (highest WER on top) samples.sort(key=lambda s: s.wer, reverse=True) return samples_wer, samples_cer, samples
[ "r", "This", "routine", "will", "calculate", "a", "WER", "report", ".", "It", "ll", "compute", "the", "mean", "WER", "and", "create", "Sample", "objects", "of", "the", "report_count", "top", "lowest", "loss", "items", "from", "the", "provided", "WER", "res...
mozilla/DeepSpeech
python
https://github.com/mozilla/DeepSpeech/blob/f64aa73e7fbe9dde40d4fcf23b42ab304747d152/util/evaluate_tools.py#L30-L47
[ "def", "calculate_report", "(", "labels", ",", "decodings", ",", "distances", ",", "losses", ")", ":", "samples", "=", "pmap", "(", "process_decode_result", ",", "zip", "(", "labels", ",", "decodings", ",", "distances", ",", "losses", ")", ")", "# Getting th...
f64aa73e7fbe9dde40d4fcf23b42ab304747d152
train
sparse_tensor_value_to_texts
r""" Given a :class:`tf.SparseTensor` ``value``, return an array of Python strings representing its values, converting tokens to strings using ``alphabet``.
evaluate.py
def sparse_tensor_value_to_texts(value, alphabet): r""" Given a :class:`tf.SparseTensor` ``value``, return an array of Python strings representing its values, converting tokens to strings using ``alphabet``. """ return sparse_tuple_to_texts((value.indices, value.values, value.dense_shape), alphabet)
def sparse_tensor_value_to_texts(value, alphabet): r""" Given a :class:`tf.SparseTensor` ``value``, return an array of Python strings representing its values, converting tokens to strings using ``alphabet``. """ return sparse_tuple_to_texts((value.indices, value.values, value.dense_shape), alphabet)
[ "r", "Given", "a", ":", "class", ":", "tf", ".", "SparseTensor", "value", "return", "an", "array", "of", "Python", "strings", "representing", "its", "values", "converting", "tokens", "to", "strings", "using", "alphabet", "." ]
mozilla/DeepSpeech
python
https://github.com/mozilla/DeepSpeech/blob/f64aa73e7fbe9dde40d4fcf23b42ab304747d152/evaluate.py#L25-L30
[ "def", "sparse_tensor_value_to_texts", "(", "value", ",", "alphabet", ")", ":", "return", "sparse_tuple_to_texts", "(", "(", "value", ".", "indices", ",", "value", ".", "values", ",", "value", ".", "dense_shape", ")", ",", "alphabet", ")" ]
f64aa73e7fbe9dde40d4fcf23b42ab304747d152
train
parse_args
Parse command line parameters Args: args ([str]): Command line parameters as list of strings Returns: :obj:`argparse.Namespace`: command line parameters namespace
bin/import_gram_vaani.py
def parse_args(args): """Parse command line parameters Args: args ([str]): Command line parameters as list of strings Returns: :obj:`argparse.Namespace`: command line parameters namespace """ parser = argparse.ArgumentParser( description="Imports GramVaani data for Deep Speech" ) parser.add_argument( "--version", action="version", version="GramVaaniImporter {ver}".format(ver=__version__), ) parser.add_argument( "-v", "--verbose", action="store_const", required=False, help="set loglevel to INFO", dest="loglevel", const=logging.INFO, ) parser.add_argument( "-vv", "--very-verbose", action="store_const", required=False, help="set loglevel to DEBUG", dest="loglevel", const=logging.DEBUG, ) parser.add_argument( "-c", "--csv_filename", required=True, help="Path to the GramVaani csv", dest="csv_filename", ) parser.add_argument( "-t", "--target_dir", required=True, help="Directory in which to save the importer GramVaani data", dest="target_dir", ) return parser.parse_args(args)
def parse_args(args): """Parse command line parameters Args: args ([str]): Command line parameters as list of strings Returns: :obj:`argparse.Namespace`: command line parameters namespace """ parser = argparse.ArgumentParser( description="Imports GramVaani data for Deep Speech" ) parser.add_argument( "--version", action="version", version="GramVaaniImporter {ver}".format(ver=__version__), ) parser.add_argument( "-v", "--verbose", action="store_const", required=False, help="set loglevel to INFO", dest="loglevel", const=logging.INFO, ) parser.add_argument( "-vv", "--very-verbose", action="store_const", required=False, help="set loglevel to DEBUG", dest="loglevel", const=logging.DEBUG, ) parser.add_argument( "-c", "--csv_filename", required=True, help="Path to the GramVaani csv", dest="csv_filename", ) parser.add_argument( "-t", "--target_dir", required=True, help="Directory in which to save the importer GramVaani data", dest="target_dir", ) return parser.parse_args(args)
[ "Parse", "command", "line", "parameters", "Args", ":", "args", "(", "[", "str", "]", ")", ":", "Command", "line", "parameters", "as", "list", "of", "strings", "Returns", ":", ":", "obj", ":", "argparse", ".", "Namespace", ":", "command", "line", "paramet...
mozilla/DeepSpeech
python
https://github.com/mozilla/DeepSpeech/blob/f64aa73e7fbe9dde40d4fcf23b42ab304747d152/bin/import_gram_vaani.py#L32-L79
[ "def", "parse_args", "(", "args", ")", ":", "parser", "=", "argparse", ".", "ArgumentParser", "(", "description", "=", "\"Imports GramVaani data for Deep Speech\"", ")", "parser", ".", "add_argument", "(", "\"--version\"", ",", "action", "=", "\"version\"", ",", "...
f64aa73e7fbe9dde40d4fcf23b42ab304747d152
train
setup_logging
Setup basic logging Args: level (int): minimum log level for emitting messages
bin/import_gram_vaani.py
def setup_logging(level): """Setup basic logging Args: level (int): minimum log level for emitting messages """ format = "[%(asctime)s] %(levelname)s:%(name)s:%(message)s" logging.basicConfig( level=level, stream=sys.stdout, format=format, datefmt="%Y-%m-%d %H:%M:%S" )
def setup_logging(level): """Setup basic logging Args: level (int): minimum log level for emitting messages """ format = "[%(asctime)s] %(levelname)s:%(name)s:%(message)s" logging.basicConfig( level=level, stream=sys.stdout, format=format, datefmt="%Y-%m-%d %H:%M:%S" )
[ "Setup", "basic", "logging", "Args", ":", "level", "(", "int", ")", ":", "minimum", "log", "level", "for", "emitting", "messages" ]
mozilla/DeepSpeech
python
https://github.com/mozilla/DeepSpeech/blob/f64aa73e7fbe9dde40d4fcf23b42ab304747d152/bin/import_gram_vaani.py#L81-L89
[ "def", "setup_logging", "(", "level", ")", ":", "format", "=", "\"[%(asctime)s] %(levelname)s:%(name)s:%(message)s\"", "logging", ".", "basicConfig", "(", "level", "=", "level", ",", "stream", "=", "sys", ".", "stdout", ",", "format", "=", "format", ",", "datefm...
f64aa73e7fbe9dde40d4fcf23b42ab304747d152
train
main
Main entry point allowing external calls Args: args ([str]): command line parameter list
bin/import_gram_vaani.py
def main(args): """Main entry point allowing external calls Args: args ([str]): command line parameter list """ args = parse_args(args) setup_logging(args.loglevel) _logger.info("Starting GramVaani importer...") _logger.info("Starting loading GramVaani csv...") csv = GramVaaniCSV(args.csv_filename) _logger.info("Starting downloading GramVaani mp3's...") downloader = GramVaaniDownloader(csv, args.target_dir) mp3_directory = downloader.download() _logger.info("Starting converting GramVaani mp3's to wav's...") converter = GramVaaniConverter(args.target_dir, mp3_directory) wav_directory = converter.convert() datasets = GramVaaniDataSets(args.target_dir, wav_directory, csv) datasets.create() datasets.save() _logger.info("Finished GramVaani importer...")
def main(args): """Main entry point allowing external calls Args: args ([str]): command line parameter list """ args = parse_args(args) setup_logging(args.loglevel) _logger.info("Starting GramVaani importer...") _logger.info("Starting loading GramVaani csv...") csv = GramVaaniCSV(args.csv_filename) _logger.info("Starting downloading GramVaani mp3's...") downloader = GramVaaniDownloader(csv, args.target_dir) mp3_directory = downloader.download() _logger.info("Starting converting GramVaani mp3's to wav's...") converter = GramVaaniConverter(args.target_dir, mp3_directory) wav_directory = converter.convert() datasets = GramVaaniDataSets(args.target_dir, wav_directory, csv) datasets.create() datasets.save() _logger.info("Finished GramVaani importer...")
[ "Main", "entry", "point", "allowing", "external", "calls", "Args", ":", "args", "(", "[", "str", "]", ")", ":", "command", "line", "parameter", "list" ]
mozilla/DeepSpeech
python
https://github.com/mozilla/DeepSpeech/blob/f64aa73e7fbe9dde40d4fcf23b42ab304747d152/bin/import_gram_vaani.py#L281-L300
[ "def", "main", "(", "args", ")", ":", "args", "=", "parse_args", "(", "args", ")", "setup_logging", "(", "args", ".", "loglevel", ")", "_logger", ".", "info", "(", "\"Starting GramVaani importer...\"", ")", "_logger", ".", "info", "(", "\"Starting loading Gram...
f64aa73e7fbe9dde40d4fcf23b42ab304747d152
train
GramVaaniDownloader.download
Downloads the data associated with this instance Return: mp3_directory (os.path): The directory into which the associated mp3's were downloaded
bin/import_gram_vaani.py
def download(self): """Downloads the data associated with this instance Return: mp3_directory (os.path): The directory into which the associated mp3's were downloaded """ mp3_directory = self._pre_download() self.data.swifter.apply(func=lambda arg: self._download(*arg, mp3_directory), axis=1, raw=True) return mp3_directory
def download(self): """Downloads the data associated with this instance Return: mp3_directory (os.path): The directory into which the associated mp3's were downloaded """ mp3_directory = self._pre_download() self.data.swifter.apply(func=lambda arg: self._download(*arg, mp3_directory), axis=1, raw=True) return mp3_directory
[ "Downloads", "the", "data", "associated", "with", "this", "instance", "Return", ":", "mp3_directory", "(", "os", ".", "path", ")", ":", "The", "directory", "into", "which", "the", "associated", "mp3", "s", "were", "downloaded" ]
mozilla/DeepSpeech
python
https://github.com/mozilla/DeepSpeech/blob/f64aa73e7fbe9dde40d4fcf23b42ab304747d152/bin/import_gram_vaani.py#L131-L138
[ "def", "download", "(", "self", ")", ":", "mp3_directory", "=", "self", ".", "_pre_download", "(", ")", "self", ".", "data", ".", "swifter", ".", "apply", "(", "func", "=", "lambda", "arg", ":", "self", ".", "_download", "(", "*", "arg", ",", "mp3_di...
f64aa73e7fbe9dde40d4fcf23b42ab304747d152
train
GramVaaniConverter.convert
Converts the mp3's associated with this instance to wav's Return: wav_directory (os.path): The directory into which the associated wav's were downloaded
bin/import_gram_vaani.py
def convert(self): """Converts the mp3's associated with this instance to wav's Return: wav_directory (os.path): The directory into which the associated wav's were downloaded """ wav_directory = self._pre_convert() for mp3_filename in self.mp3_directory.glob('**/*.mp3'): wav_filename = path.join(wav_directory, os.path.splitext(os.path.basename(mp3_filename))[0] + ".wav") if not path.exists(wav_filename): _logger.debug("Converting mp3 file %s to wav file %s" % (mp3_filename, wav_filename)) transformer = Transformer() transformer.convert(samplerate=SAMPLE_RATE, n_channels=N_CHANNELS, bitdepth=BITDEPTH) transformer.build(str(mp3_filename), str(wav_filename)) else: _logger.debug("Already converted mp3 file %s to wav file %s" % (mp3_filename, wav_filename)) return wav_directory
def convert(self): """Converts the mp3's associated with this instance to wav's Return: wav_directory (os.path): The directory into which the associated wav's were downloaded """ wav_directory = self._pre_convert() for mp3_filename in self.mp3_directory.glob('**/*.mp3'): wav_filename = path.join(wav_directory, os.path.splitext(os.path.basename(mp3_filename))[0] + ".wav") if not path.exists(wav_filename): _logger.debug("Converting mp3 file %s to wav file %s" % (mp3_filename, wav_filename)) transformer = Transformer() transformer.convert(samplerate=SAMPLE_RATE, n_channels=N_CHANNELS, bitdepth=BITDEPTH) transformer.build(str(mp3_filename), str(wav_filename)) else: _logger.debug("Already converted mp3 file %s to wav file %s" % (mp3_filename, wav_filename)) return wav_directory
[ "Converts", "the", "mp3", "s", "associated", "with", "this", "instance", "to", "wav", "s", "Return", ":", "wav_directory", "(", "os", ".", "path", ")", ":", "The", "directory", "into", "which", "the", "associated", "wav", "s", "were", "downloaded" ]
mozilla/DeepSpeech
python
https://github.com/mozilla/DeepSpeech/blob/f64aa73e7fbe9dde40d4fcf23b42ab304747d152/bin/import_gram_vaani.py#L174-L189
[ "def", "convert", "(", "self", ")", ":", "wav_directory", "=", "self", ".", "_pre_convert", "(", ")", "for", "mp3_filename", "in", "self", ".", "mp3_directory", ".", "glob", "(", "'**/*.mp3'", ")", ":", "wav_filename", "=", "path", ".", "join", "(", "wav...
f64aa73e7fbe9dde40d4fcf23b42ab304747d152
train
text_to_char_array
r""" Given a Python string ``original``, remove unsupported characters, map characters to integers and return a numpy array representing the processed string.
util/text.py
def text_to_char_array(original, alphabet): r""" Given a Python string ``original``, remove unsupported characters, map characters to integers and return a numpy array representing the processed string. """ return np.asarray([alphabet.label_from_string(c) for c in original])
def text_to_char_array(original, alphabet): r""" Given a Python string ``original``, remove unsupported characters, map characters to integers and return a numpy array representing the processed string. """ return np.asarray([alphabet.label_from_string(c) for c in original])
[ "r", "Given", "a", "Python", "string", "original", "remove", "unsupported", "characters", "map", "characters", "to", "integers", "and", "return", "a", "numpy", "array", "representing", "the", "processed", "string", "." ]
mozilla/DeepSpeech
python
https://github.com/mozilla/DeepSpeech/blob/f64aa73e7fbe9dde40d4fcf23b42ab304747d152/util/text.py#L50-L55
[ "def", "text_to_char_array", "(", "original", ",", "alphabet", ")", ":", "return", "np", ".", "asarray", "(", "[", "alphabet", ".", "label_from_string", "(", "c", ")", "for", "c", "in", "original", "]", ")" ]
f64aa73e7fbe9dde40d4fcf23b42ab304747d152
train
wer_cer_batch
r""" The WER is defined as the editing/Levenshtein distance on word level divided by the amount of words in the original text. In case of the original having more words (N) than the result and both being totally different (all N words resulting in 1 edit operation each), the WER will always be 1 (N / N = 1).
util/text.py
def wer_cer_batch(originals, results): r""" The WER is defined as the editing/Levenshtein distance on word level divided by the amount of words in the original text. In case of the original having more words (N) than the result and both being totally different (all N words resulting in 1 edit operation each), the WER will always be 1 (N / N = 1). """ # The WER is calculated on word (and NOT on character) level. # Therefore we split the strings into words first assert len(originals) == len(results) total_cer = 0.0 total_char_length = 0.0 total_wer = 0.0 total_word_length = 0.0 for original, result in zip(originals, results): total_cer += levenshtein(original, result) total_char_length += len(original) total_wer += levenshtein(original.split(), result.split()) total_word_length += len(original.split()) return total_wer / total_word_length, total_cer / total_char_length
def wer_cer_batch(originals, results): r""" The WER is defined as the editing/Levenshtein distance on word level divided by the amount of words in the original text. In case of the original having more words (N) than the result and both being totally different (all N words resulting in 1 edit operation each), the WER will always be 1 (N / N = 1). """ # The WER is calculated on word (and NOT on character) level. # Therefore we split the strings into words first assert len(originals) == len(results) total_cer = 0.0 total_char_length = 0.0 total_wer = 0.0 total_word_length = 0.0 for original, result in zip(originals, results): total_cer += levenshtein(original, result) total_char_length += len(original) total_wer += levenshtein(original.split(), result.split()) total_word_length += len(original.split()) return total_wer / total_word_length, total_cer / total_char_length
[ "r", "The", "WER", "is", "defined", "as", "the", "editing", "/", "Levenshtein", "distance", "on", "word", "level", "divided", "by", "the", "amount", "of", "words", "in", "the", "original", "text", ".", "In", "case", "of", "the", "original", "having", "mo...
mozilla/DeepSpeech
python
https://github.com/mozilla/DeepSpeech/blob/f64aa73e7fbe9dde40d4fcf23b42ab304747d152/util/text.py#L58-L83
[ "def", "wer_cer_batch", "(", "originals", ",", "results", ")", ":", "# The WER is calculated on word (and NOT on character) level.", "# Therefore we split the strings into words first", "assert", "len", "(", "originals", ")", "==", "len", "(", "results", ")", "total_cer", "...
f64aa73e7fbe9dde40d4fcf23b42ab304747d152
train
variable_on_cpu
r""" Next we concern ourselves with graph creation. However, before we do so we must introduce a utility function ``variable_on_cpu()`` used to create a variable in CPU memory.
DeepSpeech.py
def variable_on_cpu(name, shape, initializer): r""" Next we concern ourselves with graph creation. However, before we do so we must introduce a utility function ``variable_on_cpu()`` used to create a variable in CPU memory. """ # Use the /cpu:0 device for scoped operations with tf.device(Config.cpu_device): # Create or get apropos variable var = tf.get_variable(name=name, shape=shape, initializer=initializer) return var
def variable_on_cpu(name, shape, initializer): r""" Next we concern ourselves with graph creation. However, before we do so we must introduce a utility function ``variable_on_cpu()`` used to create a variable in CPU memory. """ # Use the /cpu:0 device for scoped operations with tf.device(Config.cpu_device): # Create or get apropos variable var = tf.get_variable(name=name, shape=shape, initializer=initializer) return var
[ "r", "Next", "we", "concern", "ourselves", "with", "graph", "creation", ".", "However", "before", "we", "do", "so", "we", "must", "introduce", "a", "utility", "function", "variable_on_cpu", "()", "used", "to", "create", "a", "variable", "in", "CPU", "memory"...
mozilla/DeepSpeech
python
https://github.com/mozilla/DeepSpeech/blob/f64aa73e7fbe9dde40d4fcf23b42ab304747d152/DeepSpeech.py#L31-L41
[ "def", "variable_on_cpu", "(", "name", ",", "shape", ",", "initializer", ")", ":", "# Use the /cpu:0 device for scoped operations", "with", "tf", ".", "device", "(", "Config", ".", "cpu_device", ")", ":", "# Create or get apropos variable", "var", "=", "tf", ".", ...
f64aa73e7fbe9dde40d4fcf23b42ab304747d152
train
calculate_mean_edit_distance_and_loss
r''' This routine beam search decodes a mini-batch and calculates the loss and mean edit distance. Next to total and average loss it returns the mean edit distance, the decoded result and the batch's original Y.
DeepSpeech.py
def calculate_mean_edit_distance_and_loss(iterator, dropout, reuse): r''' This routine beam search decodes a mini-batch and calculates the loss and mean edit distance. Next to total and average loss it returns the mean edit distance, the decoded result and the batch's original Y. ''' # Obtain the next batch of data (batch_x, batch_seq_len), batch_y = iterator.get_next() # Calculate the logits of the batch logits, _ = create_model(batch_x, batch_seq_len, dropout, reuse=reuse) # Compute the CTC loss using TensorFlow's `ctc_loss` total_loss = tf.nn.ctc_loss(labels=batch_y, inputs=logits, sequence_length=batch_seq_len) # Calculate the average loss across the batch avg_loss = tf.reduce_mean(total_loss) # Finally we return the average loss return avg_loss
def calculate_mean_edit_distance_and_loss(iterator, dropout, reuse): r''' This routine beam search decodes a mini-batch and calculates the loss and mean edit distance. Next to total and average loss it returns the mean edit distance, the decoded result and the batch's original Y. ''' # Obtain the next batch of data (batch_x, batch_seq_len), batch_y = iterator.get_next() # Calculate the logits of the batch logits, _ = create_model(batch_x, batch_seq_len, dropout, reuse=reuse) # Compute the CTC loss using TensorFlow's `ctc_loss` total_loss = tf.nn.ctc_loss(labels=batch_y, inputs=logits, sequence_length=batch_seq_len) # Calculate the average loss across the batch avg_loss = tf.reduce_mean(total_loss) # Finally we return the average loss return avg_loss
[ "r", "This", "routine", "beam", "search", "decodes", "a", "mini", "-", "batch", "and", "calculates", "the", "loss", "and", "mean", "edit", "distance", ".", "Next", "to", "total", "and", "average", "loss", "it", "returns", "the", "mean", "edit", "distance",...
mozilla/DeepSpeech
python
https://github.com/mozilla/DeepSpeech/blob/f64aa73e7fbe9dde40d4fcf23b42ab304747d152/DeepSpeech.py#L176-L195
[ "def", "calculate_mean_edit_distance_and_loss", "(", "iterator", ",", "dropout", ",", "reuse", ")", ":", "# Obtain the next batch of data", "(", "batch_x", ",", "batch_seq_len", ")", ",", "batch_y", "=", "iterator", ".", "get_next", "(", ")", "# Calculate the logits o...
f64aa73e7fbe9dde40d4fcf23b42ab304747d152
train
get_tower_results
r''' With this preliminary step out of the way, we can for each GPU introduce a tower for which's batch we calculate and return the optimization gradients and the average loss across towers.
DeepSpeech.py
def get_tower_results(iterator, optimizer, dropout_rates): r''' With this preliminary step out of the way, we can for each GPU introduce a tower for which's batch we calculate and return the optimization gradients and the average loss across towers. ''' # To calculate the mean of the losses tower_avg_losses = [] # Tower gradients to return tower_gradients = [] with tf.variable_scope(tf.get_variable_scope()): # Loop over available_devices for i in range(len(Config.available_devices)): # Execute operations of tower i on device i device = Config.available_devices[i] with tf.device(device): # Create a scope for all operations of tower i with tf.name_scope('tower_%d' % i): # Calculate the avg_loss and mean_edit_distance and retrieve the decoded # batch along with the original batch's labels (Y) of this tower avg_loss = calculate_mean_edit_distance_and_loss(iterator, dropout_rates, reuse=i > 0) # Allow for variables to be re-used by the next tower tf.get_variable_scope().reuse_variables() # Retain tower's avg losses tower_avg_losses.append(avg_loss) # Compute gradients for model parameters using tower's mini-batch gradients = optimizer.compute_gradients(avg_loss) # Retain tower's gradients tower_gradients.append(gradients) avg_loss_across_towers = tf.reduce_mean(tower_avg_losses, 0) tf.summary.scalar(name='step_loss', tensor=avg_loss_across_towers, collections=['step_summaries']) # Return gradients and the average loss return tower_gradients, avg_loss_across_towers
def get_tower_results(iterator, optimizer, dropout_rates): r''' With this preliminary step out of the way, we can for each GPU introduce a tower for which's batch we calculate and return the optimization gradients and the average loss across towers. ''' # To calculate the mean of the losses tower_avg_losses = [] # Tower gradients to return tower_gradients = [] with tf.variable_scope(tf.get_variable_scope()): # Loop over available_devices for i in range(len(Config.available_devices)): # Execute operations of tower i on device i device = Config.available_devices[i] with tf.device(device): # Create a scope for all operations of tower i with tf.name_scope('tower_%d' % i): # Calculate the avg_loss and mean_edit_distance and retrieve the decoded # batch along with the original batch's labels (Y) of this tower avg_loss = calculate_mean_edit_distance_and_loss(iterator, dropout_rates, reuse=i > 0) # Allow for variables to be re-used by the next tower tf.get_variable_scope().reuse_variables() # Retain tower's avg losses tower_avg_losses.append(avg_loss) # Compute gradients for model parameters using tower's mini-batch gradients = optimizer.compute_gradients(avg_loss) # Retain tower's gradients tower_gradients.append(gradients) avg_loss_across_towers = tf.reduce_mean(tower_avg_losses, 0) tf.summary.scalar(name='step_loss', tensor=avg_loss_across_towers, collections=['step_summaries']) # Return gradients and the average loss return tower_gradients, avg_loss_across_towers
[ "r", "With", "this", "preliminary", "step", "out", "of", "the", "way", "we", "can", "for", "each", "GPU", "introduce", "a", "tower", "for", "which", "s", "batch", "we", "calculate", "and", "return", "the", "optimization", "gradients", "and", "the", "averag...
mozilla/DeepSpeech
python
https://github.com/mozilla/DeepSpeech/blob/f64aa73e7fbe9dde40d4fcf23b42ab304747d152/DeepSpeech.py#L231-L273
[ "def", "get_tower_results", "(", "iterator", ",", "optimizer", ",", "dropout_rates", ")", ":", "# To calculate the mean of the losses", "tower_avg_losses", "=", "[", "]", "# Tower gradients to return", "tower_gradients", "=", "[", "]", "with", "tf", ".", "variable_scope...
f64aa73e7fbe9dde40d4fcf23b42ab304747d152
train
average_gradients
r''' A routine for computing each variable's average of the gradients obtained from the GPUs. Note also that this code acts as a synchronization point as it requires all GPUs to be finished with their mini-batch before it can run to completion.
DeepSpeech.py
def average_gradients(tower_gradients): r''' A routine for computing each variable's average of the gradients obtained from the GPUs. Note also that this code acts as a synchronization point as it requires all GPUs to be finished with their mini-batch before it can run to completion. ''' # List of average gradients to return to the caller average_grads = [] # Run this on cpu_device to conserve GPU memory with tf.device(Config.cpu_device): # Loop over gradient/variable pairs from all towers for grad_and_vars in zip(*tower_gradients): # Introduce grads to store the gradients for the current variable grads = [] # Loop over the gradients for the current variable for g, _ in grad_and_vars: # Add 0 dimension to the gradients to represent the tower. expanded_g = tf.expand_dims(g, 0) # Append on a 'tower' dimension which we will average over below. grads.append(expanded_g) # Average over the 'tower' dimension grad = tf.concat(grads, 0) grad = tf.reduce_mean(grad, 0) # Create a gradient/variable tuple for the current variable with its average gradient grad_and_var = (grad, grad_and_vars[0][1]) # Add the current tuple to average_grads average_grads.append(grad_and_var) # Return result to caller return average_grads
def average_gradients(tower_gradients): r''' A routine for computing each variable's average of the gradients obtained from the GPUs. Note also that this code acts as a synchronization point as it requires all GPUs to be finished with their mini-batch before it can run to completion. ''' # List of average gradients to return to the caller average_grads = [] # Run this on cpu_device to conserve GPU memory with tf.device(Config.cpu_device): # Loop over gradient/variable pairs from all towers for grad_and_vars in zip(*tower_gradients): # Introduce grads to store the gradients for the current variable grads = [] # Loop over the gradients for the current variable for g, _ in grad_and_vars: # Add 0 dimension to the gradients to represent the tower. expanded_g = tf.expand_dims(g, 0) # Append on a 'tower' dimension which we will average over below. grads.append(expanded_g) # Average over the 'tower' dimension grad = tf.concat(grads, 0) grad = tf.reduce_mean(grad, 0) # Create a gradient/variable tuple for the current variable with its average gradient grad_and_var = (grad, grad_and_vars[0][1]) # Add the current tuple to average_grads average_grads.append(grad_and_var) # Return result to caller return average_grads
[ "r", "A", "routine", "for", "computing", "each", "variable", "s", "average", "of", "the", "gradients", "obtained", "from", "the", "GPUs", ".", "Note", "also", "that", "this", "code", "acts", "as", "a", "synchronization", "point", "as", "it", "requires", "a...
mozilla/DeepSpeech
python
https://github.com/mozilla/DeepSpeech/blob/f64aa73e7fbe9dde40d4fcf23b42ab304747d152/DeepSpeech.py#L276-L310
[ "def", "average_gradients", "(", "tower_gradients", ")", ":", "# List of average gradients to return to the caller", "average_grads", "=", "[", "]", "# Run this on cpu_device to conserve GPU memory", "with", "tf", ".", "device", "(", "Config", ".", "cpu_device", ")", ":", ...
f64aa73e7fbe9dde40d4fcf23b42ab304747d152
train
log_variable
r''' We introduce a function for logging a tensor variable's current state. It logs scalar values for the mean, standard deviation, minimum and maximum. Furthermore it logs a histogram of its state and (if given) of an optimization gradient.
DeepSpeech.py
def log_variable(variable, gradient=None): r''' We introduce a function for logging a tensor variable's current state. It logs scalar values for the mean, standard deviation, minimum and maximum. Furthermore it logs a histogram of its state and (if given) of an optimization gradient. ''' name = variable.name.replace(':', '_') mean = tf.reduce_mean(variable) tf.summary.scalar(name='%s/mean' % name, tensor=mean) tf.summary.scalar(name='%s/sttdev' % name, tensor=tf.sqrt(tf.reduce_mean(tf.square(variable - mean)))) tf.summary.scalar(name='%s/max' % name, tensor=tf.reduce_max(variable)) tf.summary.scalar(name='%s/min' % name, tensor=tf.reduce_min(variable)) tf.summary.histogram(name=name, values=variable) if gradient is not None: if isinstance(gradient, tf.IndexedSlices): grad_values = gradient.values else: grad_values = gradient if grad_values is not None: tf.summary.histogram(name='%s/gradients' % name, values=grad_values)
def log_variable(variable, gradient=None): r''' We introduce a function for logging a tensor variable's current state. It logs scalar values for the mean, standard deviation, minimum and maximum. Furthermore it logs a histogram of its state and (if given) of an optimization gradient. ''' name = variable.name.replace(':', '_') mean = tf.reduce_mean(variable) tf.summary.scalar(name='%s/mean' % name, tensor=mean) tf.summary.scalar(name='%s/sttdev' % name, tensor=tf.sqrt(tf.reduce_mean(tf.square(variable - mean)))) tf.summary.scalar(name='%s/max' % name, tensor=tf.reduce_max(variable)) tf.summary.scalar(name='%s/min' % name, tensor=tf.reduce_min(variable)) tf.summary.histogram(name=name, values=variable) if gradient is not None: if isinstance(gradient, tf.IndexedSlices): grad_values = gradient.values else: grad_values = gradient if grad_values is not None: tf.summary.histogram(name='%s/gradients' % name, values=grad_values)
[ "r", "We", "introduce", "a", "function", "for", "logging", "a", "tensor", "variable", "s", "current", "state", ".", "It", "logs", "scalar", "values", "for", "the", "mean", "standard", "deviation", "minimum", "and", "maximum", ".", "Furthermore", "it", "logs"...
mozilla/DeepSpeech
python
https://github.com/mozilla/DeepSpeech/blob/f64aa73e7fbe9dde40d4fcf23b42ab304747d152/DeepSpeech.py#L317-L336
[ "def", "log_variable", "(", "variable", ",", "gradient", "=", "None", ")", ":", "name", "=", "variable", ".", "name", ".", "replace", "(", "':'", ",", "'_'", ")", "mean", "=", "tf", ".", "reduce_mean", "(", "variable", ")", "tf", ".", "summary", ".",...
f64aa73e7fbe9dde40d4fcf23b42ab304747d152
train
export
r''' Restores the trained variables into a simpler graph that will be exported for serving.
DeepSpeech.py
def export(): r''' Restores the trained variables into a simpler graph that will be exported for serving. ''' log_info('Exporting the model...') from tensorflow.python.framework.ops import Tensor, Operation inputs, outputs, _ = create_inference_graph(batch_size=FLAGS.export_batch_size, n_steps=FLAGS.n_steps, tflite=FLAGS.export_tflite) output_names_tensors = [tensor.op.name for tensor in outputs.values() if isinstance(tensor, Tensor)] output_names_ops = [op.name for op in outputs.values() if isinstance(op, Operation)] output_names = ",".join(output_names_tensors + output_names_ops) if not FLAGS.export_tflite: mapping = {v.op.name: v for v in tf.global_variables() if not v.op.name.startswith('previous_state_')} else: # Create a saver using variables from the above newly created graph def fixup(name): if name.startswith('rnn/lstm_cell/'): return name.replace('rnn/lstm_cell/', 'lstm_fused_cell/') return name mapping = {fixup(v.op.name): v for v in tf.global_variables()} saver = tf.train.Saver(mapping) # Restore variables from training checkpoint checkpoint = tf.train.get_checkpoint_state(FLAGS.checkpoint_dir) checkpoint_path = checkpoint.model_checkpoint_path output_filename = 'output_graph.pb' if FLAGS.remove_export: if os.path.isdir(FLAGS.export_dir): log_info('Removing old export') shutil.rmtree(FLAGS.export_dir) try: output_graph_path = os.path.join(FLAGS.export_dir, output_filename) if not os.path.isdir(FLAGS.export_dir): os.makedirs(FLAGS.export_dir) def do_graph_freeze(output_file=None, output_node_names=None, variables_blacklist=None): return freeze_graph.freeze_graph_with_def_protos( input_graph_def=tf.get_default_graph().as_graph_def(), input_saver_def=saver.as_saver_def(), input_checkpoint=checkpoint_path, output_node_names=output_node_names, restore_op_name=None, filename_tensor_name=None, output_graph=output_file, clear_devices=False, variable_names_blacklist=variables_blacklist, initializer_nodes='') if not FLAGS.export_tflite: frozen_graph = do_graph_freeze(output_node_names=output_names, variables_blacklist='previous_state_c,previous_state_h') frozen_graph.version = int(file_relative_read('GRAPH_VERSION').strip()) # Add a no-op node to the graph with metadata information to be loaded by the native client metadata = frozen_graph.node.add() metadata.name = 'model_metadata' metadata.op = 'NoOp' metadata.attr['sample_rate'].i = FLAGS.audio_sample_rate metadata.attr['feature_win_len'].i = FLAGS.feature_win_len metadata.attr['feature_win_step'].i = FLAGS.feature_win_step if FLAGS.export_language: metadata.attr['language'].s = FLAGS.export_language.encode('ascii') with open(output_graph_path, 'wb') as fout: fout.write(frozen_graph.SerializeToString()) else: frozen_graph = do_graph_freeze(output_node_names=output_names, variables_blacklist='') output_tflite_path = os.path.join(FLAGS.export_dir, output_filename.replace('.pb', '.tflite')) converter = tf.lite.TFLiteConverter(frozen_graph, input_tensors=inputs.values(), output_tensors=outputs.values()) converter.post_training_quantize = True # AudioSpectrogram and Mfcc ops are custom but have built-in kernels in TFLite converter.allow_custom_ops = True tflite_model = converter.convert() with open(output_tflite_path, 'wb') as fout: fout.write(tflite_model) log_info('Exported model for TF Lite engine as {}'.format(os.path.basename(output_tflite_path))) log_info('Models exported at %s' % (FLAGS.export_dir)) except RuntimeError as e: log_error(str(e))
def export(): r''' Restores the trained variables into a simpler graph that will be exported for serving. ''' log_info('Exporting the model...') from tensorflow.python.framework.ops import Tensor, Operation inputs, outputs, _ = create_inference_graph(batch_size=FLAGS.export_batch_size, n_steps=FLAGS.n_steps, tflite=FLAGS.export_tflite) output_names_tensors = [tensor.op.name for tensor in outputs.values() if isinstance(tensor, Tensor)] output_names_ops = [op.name for op in outputs.values() if isinstance(op, Operation)] output_names = ",".join(output_names_tensors + output_names_ops) if not FLAGS.export_tflite: mapping = {v.op.name: v for v in tf.global_variables() if not v.op.name.startswith('previous_state_')} else: # Create a saver using variables from the above newly created graph def fixup(name): if name.startswith('rnn/lstm_cell/'): return name.replace('rnn/lstm_cell/', 'lstm_fused_cell/') return name mapping = {fixup(v.op.name): v for v in tf.global_variables()} saver = tf.train.Saver(mapping) # Restore variables from training checkpoint checkpoint = tf.train.get_checkpoint_state(FLAGS.checkpoint_dir) checkpoint_path = checkpoint.model_checkpoint_path output_filename = 'output_graph.pb' if FLAGS.remove_export: if os.path.isdir(FLAGS.export_dir): log_info('Removing old export') shutil.rmtree(FLAGS.export_dir) try: output_graph_path = os.path.join(FLAGS.export_dir, output_filename) if not os.path.isdir(FLAGS.export_dir): os.makedirs(FLAGS.export_dir) def do_graph_freeze(output_file=None, output_node_names=None, variables_blacklist=None): return freeze_graph.freeze_graph_with_def_protos( input_graph_def=tf.get_default_graph().as_graph_def(), input_saver_def=saver.as_saver_def(), input_checkpoint=checkpoint_path, output_node_names=output_node_names, restore_op_name=None, filename_tensor_name=None, output_graph=output_file, clear_devices=False, variable_names_blacklist=variables_blacklist, initializer_nodes='') if not FLAGS.export_tflite: frozen_graph = do_graph_freeze(output_node_names=output_names, variables_blacklist='previous_state_c,previous_state_h') frozen_graph.version = int(file_relative_read('GRAPH_VERSION').strip()) # Add a no-op node to the graph with metadata information to be loaded by the native client metadata = frozen_graph.node.add() metadata.name = 'model_metadata' metadata.op = 'NoOp' metadata.attr['sample_rate'].i = FLAGS.audio_sample_rate metadata.attr['feature_win_len'].i = FLAGS.feature_win_len metadata.attr['feature_win_step'].i = FLAGS.feature_win_step if FLAGS.export_language: metadata.attr['language'].s = FLAGS.export_language.encode('ascii') with open(output_graph_path, 'wb') as fout: fout.write(frozen_graph.SerializeToString()) else: frozen_graph = do_graph_freeze(output_node_names=output_names, variables_blacklist='') output_tflite_path = os.path.join(FLAGS.export_dir, output_filename.replace('.pb', '.tflite')) converter = tf.lite.TFLiteConverter(frozen_graph, input_tensors=inputs.values(), output_tensors=outputs.values()) converter.post_training_quantize = True # AudioSpectrogram and Mfcc ops are custom but have built-in kernels in TFLite converter.allow_custom_ops = True tflite_model = converter.convert() with open(output_tflite_path, 'wb') as fout: fout.write(tflite_model) log_info('Exported model for TF Lite engine as {}'.format(os.path.basename(output_tflite_path))) log_info('Models exported at %s' % (FLAGS.export_dir)) except RuntimeError as e: log_error(str(e))
[ "r", "Restores", "the", "trained", "variables", "into", "a", "simpler", "graph", "that", "will", "be", "exported", "for", "serving", "." ]
mozilla/DeepSpeech
python
https://github.com/mozilla/DeepSpeech/blob/f64aa73e7fbe9dde40d4fcf23b42ab304747d152/DeepSpeech.py#L673-L759
[ "def", "export", "(", ")", ":", "log_info", "(", "'Exporting the model...'", ")", "from", "tensorflow", ".", "python", ".", "framework", ".", "ops", "import", "Tensor", ",", "Operation", "inputs", ",", "outputs", ",", "_", "=", "create_inference_graph", "(", ...
f64aa73e7fbe9dde40d4fcf23b42ab304747d152
train
ctc_beam_search_decoder
Wrapper for the CTC Beam Search Decoder. :param probs_seq: 2-D list of probability distributions over each time step, with each element being a list of normalized probabilities over alphabet and blank. :type probs_seq: 2-D list :param alphabet: alphabet list. :alphabet: Alphabet :param beam_size: Width for beam search. :type beam_size: int :param cutoff_prob: Cutoff probability in pruning, default 1.0, no pruning. :type cutoff_prob: float :param cutoff_top_n: Cutoff number in pruning, only top cutoff_top_n characters with highest probs in alphabet will be used in beam search, default 40. :type cutoff_top_n: int :param scorer: External scorer for partially decoded sentence, e.g. word count or language model. :type scorer: Scorer :return: List of tuples of log probability and sentence as decoding results, in descending order of the probability. :rtype: list
native_client/ctcdecode/__init__.py
def ctc_beam_search_decoder(probs_seq, alphabet, beam_size, cutoff_prob=1.0, cutoff_top_n=40, scorer=None): """Wrapper for the CTC Beam Search Decoder. :param probs_seq: 2-D list of probability distributions over each time step, with each element being a list of normalized probabilities over alphabet and blank. :type probs_seq: 2-D list :param alphabet: alphabet list. :alphabet: Alphabet :param beam_size: Width for beam search. :type beam_size: int :param cutoff_prob: Cutoff probability in pruning, default 1.0, no pruning. :type cutoff_prob: float :param cutoff_top_n: Cutoff number in pruning, only top cutoff_top_n characters with highest probs in alphabet will be used in beam search, default 40. :type cutoff_top_n: int :param scorer: External scorer for partially decoded sentence, e.g. word count or language model. :type scorer: Scorer :return: List of tuples of log probability and sentence as decoding results, in descending order of the probability. :rtype: list """ beam_results = swigwrapper.ctc_beam_search_decoder( probs_seq, alphabet.config_file(), beam_size, cutoff_prob, cutoff_top_n, scorer) beam_results = [(res.probability, alphabet.decode(res.tokens)) for res in beam_results] return beam_results
def ctc_beam_search_decoder(probs_seq, alphabet, beam_size, cutoff_prob=1.0, cutoff_top_n=40, scorer=None): """Wrapper for the CTC Beam Search Decoder. :param probs_seq: 2-D list of probability distributions over each time step, with each element being a list of normalized probabilities over alphabet and blank. :type probs_seq: 2-D list :param alphabet: alphabet list. :alphabet: Alphabet :param beam_size: Width for beam search. :type beam_size: int :param cutoff_prob: Cutoff probability in pruning, default 1.0, no pruning. :type cutoff_prob: float :param cutoff_top_n: Cutoff number in pruning, only top cutoff_top_n characters with highest probs in alphabet will be used in beam search, default 40. :type cutoff_top_n: int :param scorer: External scorer for partially decoded sentence, e.g. word count or language model. :type scorer: Scorer :return: List of tuples of log probability and sentence as decoding results, in descending order of the probability. :rtype: list """ beam_results = swigwrapper.ctc_beam_search_decoder( probs_seq, alphabet.config_file(), beam_size, cutoff_prob, cutoff_top_n, scorer) beam_results = [(res.probability, alphabet.decode(res.tokens)) for res in beam_results] return beam_results
[ "Wrapper", "for", "the", "CTC", "Beam", "Search", "Decoder", "." ]
mozilla/DeepSpeech
python
https://github.com/mozilla/DeepSpeech/blob/f64aa73e7fbe9dde40d4fcf23b42ab304747d152/native_client/ctcdecode/__init__.py#L25-L59
[ "def", "ctc_beam_search_decoder", "(", "probs_seq", ",", "alphabet", ",", "beam_size", ",", "cutoff_prob", "=", "1.0", ",", "cutoff_top_n", "=", "40", ",", "scorer", "=", "None", ")", ":", "beam_results", "=", "swigwrapper", ".", "ctc_beam_search_decoder", "(", ...
f64aa73e7fbe9dde40d4fcf23b42ab304747d152
train
ctc_beam_search_decoder_batch
Wrapper for the batched CTC beam search decoder. :param probs_seq: 3-D list with each element as an instance of 2-D list of probabilities used by ctc_beam_search_decoder(). :type probs_seq: 3-D list :param alphabet: alphabet list. :alphabet: Alphabet :param beam_size: Width for beam search. :type beam_size: int :param num_processes: Number of parallel processes. :type num_processes: int :param cutoff_prob: Cutoff probability in alphabet pruning, default 1.0, no pruning. :type cutoff_prob: float :param cutoff_top_n: Cutoff number in pruning, only top cutoff_top_n characters with highest probs in alphabet will be used in beam search, default 40. :type cutoff_top_n: int :param num_processes: Number of parallel processes. :type num_processes: int :param scorer: External scorer for partially decoded sentence, e.g. word count or language model. :type scorer: Scorer :return: List of tuples of log probability and sentence as decoding results, in descending order of the probability. :rtype: list
native_client/ctcdecode/__init__.py
def ctc_beam_search_decoder_batch(probs_seq, seq_lengths, alphabet, beam_size, num_processes, cutoff_prob=1.0, cutoff_top_n=40, scorer=None): """Wrapper for the batched CTC beam search decoder. :param probs_seq: 3-D list with each element as an instance of 2-D list of probabilities used by ctc_beam_search_decoder(). :type probs_seq: 3-D list :param alphabet: alphabet list. :alphabet: Alphabet :param beam_size: Width for beam search. :type beam_size: int :param num_processes: Number of parallel processes. :type num_processes: int :param cutoff_prob: Cutoff probability in alphabet pruning, default 1.0, no pruning. :type cutoff_prob: float :param cutoff_top_n: Cutoff number in pruning, only top cutoff_top_n characters with highest probs in alphabet will be used in beam search, default 40. :type cutoff_top_n: int :param num_processes: Number of parallel processes. :type num_processes: int :param scorer: External scorer for partially decoded sentence, e.g. word count or language model. :type scorer: Scorer :return: List of tuples of log probability and sentence as decoding results, in descending order of the probability. :rtype: list """ batch_beam_results = swigwrapper.ctc_beam_search_decoder_batch( probs_seq, seq_lengths, alphabet.config_file(), beam_size, num_processes, cutoff_prob, cutoff_top_n, scorer) batch_beam_results = [ [(res.probability, alphabet.decode(res.tokens)) for res in beam_results] for beam_results in batch_beam_results ] return batch_beam_results
def ctc_beam_search_decoder_batch(probs_seq, seq_lengths, alphabet, beam_size, num_processes, cutoff_prob=1.0, cutoff_top_n=40, scorer=None): """Wrapper for the batched CTC beam search decoder. :param probs_seq: 3-D list with each element as an instance of 2-D list of probabilities used by ctc_beam_search_decoder(). :type probs_seq: 3-D list :param alphabet: alphabet list. :alphabet: Alphabet :param beam_size: Width for beam search. :type beam_size: int :param num_processes: Number of parallel processes. :type num_processes: int :param cutoff_prob: Cutoff probability in alphabet pruning, default 1.0, no pruning. :type cutoff_prob: float :param cutoff_top_n: Cutoff number in pruning, only top cutoff_top_n characters with highest probs in alphabet will be used in beam search, default 40. :type cutoff_top_n: int :param num_processes: Number of parallel processes. :type num_processes: int :param scorer: External scorer for partially decoded sentence, e.g. word count or language model. :type scorer: Scorer :return: List of tuples of log probability and sentence as decoding results, in descending order of the probability. :rtype: list """ batch_beam_results = swigwrapper.ctc_beam_search_decoder_batch( probs_seq, seq_lengths, alphabet.config_file(), beam_size, num_processes, cutoff_prob, cutoff_top_n, scorer) batch_beam_results = [ [(res.probability, alphabet.decode(res.tokens)) for res in beam_results] for beam_results in batch_beam_results ] return batch_beam_results
[ "Wrapper", "for", "the", "batched", "CTC", "beam", "search", "decoder", "." ]
mozilla/DeepSpeech
python
https://github.com/mozilla/DeepSpeech/blob/f64aa73e7fbe9dde40d4fcf23b42ab304747d152/native_client/ctcdecode/__init__.py#L62-L104
[ "def", "ctc_beam_search_decoder_batch", "(", "probs_seq", ",", "seq_lengths", ",", "alphabet", ",", "beam_size", ",", "num_processes", ",", "cutoff_prob", "=", "1.0", ",", "cutoff_top_n", "=", "40", ",", "scorer", "=", "None", ")", ":", "batch_beam_results", "="...
f64aa73e7fbe9dde40d4fcf23b42ab304747d152
train
Audio.resample
Microphone may not support our native processing sampling rate, so resample from input_rate to RATE_PROCESS here for webrtcvad and deepspeech Args: data (binary): Input audio stream input_rate (int): Input audio rate to resample from
examples/mic_vad_streaming/mic_vad_streaming.py
def resample(self, data, input_rate): """ Microphone may not support our native processing sampling rate, so resample from input_rate to RATE_PROCESS here for webrtcvad and deepspeech Args: data (binary): Input audio stream input_rate (int): Input audio rate to resample from """ data16 = np.fromstring(string=data, dtype=np.int16) resample_size = int(len(data16) / self.input_rate * self.RATE_PROCESS) resample = signal.resample(data16, resample_size) resample16 = np.array(resample, dtype=np.int16) return resample16.tostring()
def resample(self, data, input_rate): """ Microphone may not support our native processing sampling rate, so resample from input_rate to RATE_PROCESS here for webrtcvad and deepspeech Args: data (binary): Input audio stream input_rate (int): Input audio rate to resample from """ data16 = np.fromstring(string=data, dtype=np.int16) resample_size = int(len(data16) / self.input_rate * self.RATE_PROCESS) resample = signal.resample(data16, resample_size) resample16 = np.array(resample, dtype=np.int16) return resample16.tostring()
[ "Microphone", "may", "not", "support", "our", "native", "processing", "sampling", "rate", "so", "resample", "from", "input_rate", "to", "RATE_PROCESS", "here", "for", "webrtcvad", "and", "deepspeech" ]
mozilla/DeepSpeech
python
https://github.com/mozilla/DeepSpeech/blob/f64aa73e7fbe9dde40d4fcf23b42ab304747d152/examples/mic_vad_streaming/mic_vad_streaming.py#L52-L66
[ "def", "resample", "(", "self", ",", "data", ",", "input_rate", ")", ":", "data16", "=", "np", ".", "fromstring", "(", "string", "=", "data", ",", "dtype", "=", "np", ".", "int16", ")", "resample_size", "=", "int", "(", "len", "(", "data16", ")", "...
f64aa73e7fbe9dde40d4fcf23b42ab304747d152
train
Audio.read_resampled
Return a block of audio data resampled to 16000hz, blocking if necessary.
examples/mic_vad_streaming/mic_vad_streaming.py
def read_resampled(self): """Return a block of audio data resampled to 16000hz, blocking if necessary.""" return self.resample(data=self.buffer_queue.get(), input_rate=self.input_rate)
def read_resampled(self): """Return a block of audio data resampled to 16000hz, blocking if necessary.""" return self.resample(data=self.buffer_queue.get(), input_rate=self.input_rate)
[ "Return", "a", "block", "of", "audio", "data", "resampled", "to", "16000hz", "blocking", "if", "necessary", "." ]
mozilla/DeepSpeech
python
https://github.com/mozilla/DeepSpeech/blob/f64aa73e7fbe9dde40d4fcf23b42ab304747d152/examples/mic_vad_streaming/mic_vad_streaming.py#L68-L71
[ "def", "read_resampled", "(", "self", ")", ":", "return", "self", ".", "resample", "(", "data", "=", "self", ".", "buffer_queue", ".", "get", "(", ")", ",", "input_rate", "=", "self", ".", "input_rate", ")" ]
f64aa73e7fbe9dde40d4fcf23b42ab304747d152
train
VADAudio.frame_generator
Generator that yields all audio frames from microphone.
examples/mic_vad_streaming/mic_vad_streaming.py
def frame_generator(self): """Generator that yields all audio frames from microphone.""" if self.input_rate == self.RATE_PROCESS: while True: yield self.read() else: while True: yield self.read_resampled()
def frame_generator(self): """Generator that yields all audio frames from microphone.""" if self.input_rate == self.RATE_PROCESS: while True: yield self.read() else: while True: yield self.read_resampled()
[ "Generator", "that", "yields", "all", "audio", "frames", "from", "microphone", "." ]
mozilla/DeepSpeech
python
https://github.com/mozilla/DeepSpeech/blob/f64aa73e7fbe9dde40d4fcf23b42ab304747d152/examples/mic_vad_streaming/mic_vad_streaming.py#L103-L110
[ "def", "frame_generator", "(", "self", ")", ":", "if", "self", ".", "input_rate", "==", "self", ".", "RATE_PROCESS", ":", "while", "True", ":", "yield", "self", ".", "read", "(", ")", "else", ":", "while", "True", ":", "yield", "self", ".", "read_resam...
f64aa73e7fbe9dde40d4fcf23b42ab304747d152
train
VADAudio.vad_collector
Generator that yields series of consecutive audio frames comprising each utterence, separated by yielding a single None. Determines voice activity by ratio of frames in padding_ms. Uses a buffer to include padding_ms prior to being triggered. Example: (frame, ..., frame, None, frame, ..., frame, None, ...) |---utterence---| |---utterence---|
examples/mic_vad_streaming/mic_vad_streaming.py
def vad_collector(self, padding_ms=300, ratio=0.75, frames=None): """Generator that yields series of consecutive audio frames comprising each utterence, separated by yielding a single None. Determines voice activity by ratio of frames in padding_ms. Uses a buffer to include padding_ms prior to being triggered. Example: (frame, ..., frame, None, frame, ..., frame, None, ...) |---utterence---| |---utterence---| """ if frames is None: frames = self.frame_generator() num_padding_frames = padding_ms // self.frame_duration_ms ring_buffer = collections.deque(maxlen=num_padding_frames) triggered = False for frame in frames: is_speech = self.vad.is_speech(frame, self.sample_rate) if not triggered: ring_buffer.append((frame, is_speech)) num_voiced = len([f for f, speech in ring_buffer if speech]) if num_voiced > ratio * ring_buffer.maxlen: triggered = True for f, s in ring_buffer: yield f ring_buffer.clear() else: yield frame ring_buffer.append((frame, is_speech)) num_unvoiced = len([f for f, speech in ring_buffer if not speech]) if num_unvoiced > ratio * ring_buffer.maxlen: triggered = False yield None ring_buffer.clear()
def vad_collector(self, padding_ms=300, ratio=0.75, frames=None): """Generator that yields series of consecutive audio frames comprising each utterence, separated by yielding a single None. Determines voice activity by ratio of frames in padding_ms. Uses a buffer to include padding_ms prior to being triggered. Example: (frame, ..., frame, None, frame, ..., frame, None, ...) |---utterence---| |---utterence---| """ if frames is None: frames = self.frame_generator() num_padding_frames = padding_ms // self.frame_duration_ms ring_buffer = collections.deque(maxlen=num_padding_frames) triggered = False for frame in frames: is_speech = self.vad.is_speech(frame, self.sample_rate) if not triggered: ring_buffer.append((frame, is_speech)) num_voiced = len([f for f, speech in ring_buffer if speech]) if num_voiced > ratio * ring_buffer.maxlen: triggered = True for f, s in ring_buffer: yield f ring_buffer.clear() else: yield frame ring_buffer.append((frame, is_speech)) num_unvoiced = len([f for f, speech in ring_buffer if not speech]) if num_unvoiced > ratio * ring_buffer.maxlen: triggered = False yield None ring_buffer.clear()
[ "Generator", "that", "yields", "series", "of", "consecutive", "audio", "frames", "comprising", "each", "utterence", "separated", "by", "yielding", "a", "single", "None", ".", "Determines", "voice", "activity", "by", "ratio", "of", "frames", "in", "padding_ms", "...
mozilla/DeepSpeech
python
https://github.com/mozilla/DeepSpeech/blob/f64aa73e7fbe9dde40d4fcf23b42ab304747d152/examples/mic_vad_streaming/mic_vad_streaming.py#L112-L142
[ "def", "vad_collector", "(", "self", ",", "padding_ms", "=", "300", ",", "ratio", "=", "0.75", ",", "frames", "=", "None", ")", ":", "if", "frames", "is", "None", ":", "frames", "=", "self", ".", "frame_generator", "(", ")", "num_padding_frames", "=", ...
f64aa73e7fbe9dde40d4fcf23b42ab304747d152
train
cut
Global `cut` function that supports parallel processing. Note that this only works using dt, custom POSTokenizer instances are not supported.
jieba/posseg/__init__.py
def cut(sentence, HMM=True): """ Global `cut` function that supports parallel processing. Note that this only works using dt, custom POSTokenizer instances are not supported. """ global dt if jieba.pool is None: for w in dt.cut(sentence, HMM=HMM): yield w else: parts = strdecode(sentence).splitlines(True) if HMM: result = jieba.pool.map(_lcut_internal, parts) else: result = jieba.pool.map(_lcut_internal_no_hmm, parts) for r in result: for w in r: yield w
def cut(sentence, HMM=True): """ Global `cut` function that supports parallel processing. Note that this only works using dt, custom POSTokenizer instances are not supported. """ global dt if jieba.pool is None: for w in dt.cut(sentence, HMM=HMM): yield w else: parts = strdecode(sentence).splitlines(True) if HMM: result = jieba.pool.map(_lcut_internal, parts) else: result = jieba.pool.map(_lcut_internal_no_hmm, parts) for r in result: for w in r: yield w
[ "Global", "cut", "function", "that", "supports", "parallel", "processing", "." ]
fxsjy/jieba
python
https://github.com/fxsjy/jieba/blob/8212b6c5725d08311952a3a08e5509eeaee33eb7/jieba/posseg/__init__.py#L272-L291
[ "def", "cut", "(", "sentence", ",", "HMM", "=", "True", ")", ":", "global", "dt", "if", "jieba", ".", "pool", "is", "None", ":", "for", "w", "in", "dt", ".", "cut", "(", "sentence", ",", "HMM", "=", "HMM", ")", ":", "yield", "w", "else", ":", ...
8212b6c5725d08311952a3a08e5509eeaee33eb7
train
enable_parallel
Change the module's `cut` and `cut_for_search` functions to the parallel version. Note that this only works using dt, custom Tokenizer instances are not supported.
jieba/__init__.py
def enable_parallel(processnum=None): """ Change the module's `cut` and `cut_for_search` functions to the parallel version. Note that this only works using dt, custom Tokenizer instances are not supported. """ global pool, dt, cut, cut_for_search from multiprocessing import cpu_count if os.name == 'nt': raise NotImplementedError( "jieba: parallel mode only supports posix system") else: from multiprocessing import Pool dt.check_initialized() if processnum is None: processnum = cpu_count() pool = Pool(processnum) cut = _pcut cut_for_search = _pcut_for_search
def enable_parallel(processnum=None): """ Change the module's `cut` and `cut_for_search` functions to the parallel version. Note that this only works using dt, custom Tokenizer instances are not supported. """ global pool, dt, cut, cut_for_search from multiprocessing import cpu_count if os.name == 'nt': raise NotImplementedError( "jieba: parallel mode only supports posix system") else: from multiprocessing import Pool dt.check_initialized() if processnum is None: processnum = cpu_count() pool = Pool(processnum) cut = _pcut cut_for_search = _pcut_for_search
[ "Change", "the", "module", "s", "cut", "and", "cut_for_search", "functions", "to", "the", "parallel", "version", "." ]
fxsjy/jieba
python
https://github.com/fxsjy/jieba/blob/8212b6c5725d08311952a3a08e5509eeaee33eb7/jieba/__init__.py#L569-L589
[ "def", "enable_parallel", "(", "processnum", "=", "None", ")", ":", "global", "pool", ",", "dt", ",", "cut", ",", "cut_for_search", "from", "multiprocessing", "import", "cpu_count", "if", "os", ".", "name", "==", "'nt'", ":", "raise", "NotImplementedError", ...
8212b6c5725d08311952a3a08e5509eeaee33eb7
train
Tokenizer.cut
The main function that segments an entire sentence that contains Chinese characters into separated words. Parameter: - sentence: The str(unicode) to be segmented. - cut_all: Model type. True for full pattern, False for accurate pattern. - HMM: Whether to use the Hidden Markov Model.
jieba/__init__.py
def cut(self, sentence, cut_all=False, HMM=True): ''' The main function that segments an entire sentence that contains Chinese characters into separated words. Parameter: - sentence: The str(unicode) to be segmented. - cut_all: Model type. True for full pattern, False for accurate pattern. - HMM: Whether to use the Hidden Markov Model. ''' sentence = strdecode(sentence) if cut_all: re_han = re_han_cut_all re_skip = re_skip_cut_all else: re_han = re_han_default re_skip = re_skip_default if cut_all: cut_block = self.__cut_all elif HMM: cut_block = self.__cut_DAG else: cut_block = self.__cut_DAG_NO_HMM blocks = re_han.split(sentence) for blk in blocks: if not blk: continue if re_han.match(blk): for word in cut_block(blk): yield word else: tmp = re_skip.split(blk) for x in tmp: if re_skip.match(x): yield x elif not cut_all: for xx in x: yield xx else: yield x
def cut(self, sentence, cut_all=False, HMM=True): ''' The main function that segments an entire sentence that contains Chinese characters into separated words. Parameter: - sentence: The str(unicode) to be segmented. - cut_all: Model type. True for full pattern, False for accurate pattern. - HMM: Whether to use the Hidden Markov Model. ''' sentence = strdecode(sentence) if cut_all: re_han = re_han_cut_all re_skip = re_skip_cut_all else: re_han = re_han_default re_skip = re_skip_default if cut_all: cut_block = self.__cut_all elif HMM: cut_block = self.__cut_DAG else: cut_block = self.__cut_DAG_NO_HMM blocks = re_han.split(sentence) for blk in blocks: if not blk: continue if re_han.match(blk): for word in cut_block(blk): yield word else: tmp = re_skip.split(blk) for x in tmp: if re_skip.match(x): yield x elif not cut_all: for xx in x: yield xx else: yield x
[ "The", "main", "function", "that", "segments", "an", "entire", "sentence", "that", "contains", "Chinese", "characters", "into", "separated", "words", "." ]
fxsjy/jieba
python
https://github.com/fxsjy/jieba/blob/8212b6c5725d08311952a3a08e5509eeaee33eb7/jieba/__init__.py#L275-L315
[ "def", "cut", "(", "self", ",", "sentence", ",", "cut_all", "=", "False", ",", "HMM", "=", "True", ")", ":", "sentence", "=", "strdecode", "(", "sentence", ")", "if", "cut_all", ":", "re_han", "=", "re_han_cut_all", "re_skip", "=", "re_skip_cut_all", "el...
8212b6c5725d08311952a3a08e5509eeaee33eb7
train
Tokenizer.cut_for_search
Finer segmentation for search engines.
jieba/__init__.py
def cut_for_search(self, sentence, HMM=True): """ Finer segmentation for search engines. """ words = self.cut(sentence, HMM=HMM) for w in words: if len(w) > 2: for i in xrange(len(w) - 1): gram2 = w[i:i + 2] if self.FREQ.get(gram2): yield gram2 if len(w) > 3: for i in xrange(len(w) - 2): gram3 = w[i:i + 3] if self.FREQ.get(gram3): yield gram3 yield w
def cut_for_search(self, sentence, HMM=True): """ Finer segmentation for search engines. """ words = self.cut(sentence, HMM=HMM) for w in words: if len(w) > 2: for i in xrange(len(w) - 1): gram2 = w[i:i + 2] if self.FREQ.get(gram2): yield gram2 if len(w) > 3: for i in xrange(len(w) - 2): gram3 = w[i:i + 3] if self.FREQ.get(gram3): yield gram3 yield w
[ "Finer", "segmentation", "for", "search", "engines", "." ]
fxsjy/jieba
python
https://github.com/fxsjy/jieba/blob/8212b6c5725d08311952a3a08e5509eeaee33eb7/jieba/__init__.py#L317-L333
[ "def", "cut_for_search", "(", "self", ",", "sentence", ",", "HMM", "=", "True", ")", ":", "words", "=", "self", ".", "cut", "(", "sentence", ",", "HMM", "=", "HMM", ")", "for", "w", "in", "words", ":", "if", "len", "(", "w", ")", ">", "2", ":",...
8212b6c5725d08311952a3a08e5509eeaee33eb7
train
Tokenizer.load_userdict
Load personalized dict to improve detect rate. Parameter: - f : A plain text file contains words and their ocurrences. Can be a file-like object, or the path of the dictionary file, whose encoding must be utf-8. Structure of dict file: word1 freq1 word_type1 word2 freq2 word_type2 ... Word type may be ignored
jieba/__init__.py
def load_userdict(self, f): ''' Load personalized dict to improve detect rate. Parameter: - f : A plain text file contains words and their ocurrences. Can be a file-like object, or the path of the dictionary file, whose encoding must be utf-8. Structure of dict file: word1 freq1 word_type1 word2 freq2 word_type2 ... Word type may be ignored ''' self.check_initialized() if isinstance(f, string_types): f_name = f f = open(f, 'rb') else: f_name = resolve_filename(f) for lineno, ln in enumerate(f, 1): line = ln.strip() if not isinstance(line, text_type): try: line = line.decode('utf-8').lstrip('\ufeff') except UnicodeDecodeError: raise ValueError('dictionary file %s must be utf-8' % f_name) if not line: continue # match won't be None because there's at least one character word, freq, tag = re_userdict.match(line).groups() if freq is not None: freq = freq.strip() if tag is not None: tag = tag.strip() self.add_word(word, freq, tag)
def load_userdict(self, f): ''' Load personalized dict to improve detect rate. Parameter: - f : A plain text file contains words and their ocurrences. Can be a file-like object, or the path of the dictionary file, whose encoding must be utf-8. Structure of dict file: word1 freq1 word_type1 word2 freq2 word_type2 ... Word type may be ignored ''' self.check_initialized() if isinstance(f, string_types): f_name = f f = open(f, 'rb') else: f_name = resolve_filename(f) for lineno, ln in enumerate(f, 1): line = ln.strip() if not isinstance(line, text_type): try: line = line.decode('utf-8').lstrip('\ufeff') except UnicodeDecodeError: raise ValueError('dictionary file %s must be utf-8' % f_name) if not line: continue # match won't be None because there's at least one character word, freq, tag = re_userdict.match(line).groups() if freq is not None: freq = freq.strip() if tag is not None: tag = tag.strip() self.add_word(word, freq, tag)
[ "Load", "personalized", "dict", "to", "improve", "detect", "rate", "." ]
fxsjy/jieba
python
https://github.com/fxsjy/jieba/blob/8212b6c5725d08311952a3a08e5509eeaee33eb7/jieba/__init__.py#L359-L395
[ "def", "load_userdict", "(", "self", ",", "f", ")", ":", "self", ".", "check_initialized", "(", ")", "if", "isinstance", "(", "f", ",", "string_types", ")", ":", "f_name", "=", "f", "f", "=", "open", "(", "f", ",", "'rb'", ")", "else", ":", "f_name...
8212b6c5725d08311952a3a08e5509eeaee33eb7
train
Tokenizer.add_word
Add a word to dictionary. freq and tag can be omitted, freq defaults to be a calculated value that ensures the word can be cut out.
jieba/__init__.py
def add_word(self, word, freq=None, tag=None): """ Add a word to dictionary. freq and tag can be omitted, freq defaults to be a calculated value that ensures the word can be cut out. """ self.check_initialized() word = strdecode(word) freq = int(freq) if freq is not None else self.suggest_freq(word, False) self.FREQ[word] = freq self.total += freq if tag: self.user_word_tag_tab[word] = tag for ch in xrange(len(word)): wfrag = word[:ch + 1] if wfrag not in self.FREQ: self.FREQ[wfrag] = 0 if freq == 0: finalseg.add_force_split(word)
def add_word(self, word, freq=None, tag=None): """ Add a word to dictionary. freq and tag can be omitted, freq defaults to be a calculated value that ensures the word can be cut out. """ self.check_initialized() word = strdecode(word) freq = int(freq) if freq is not None else self.suggest_freq(word, False) self.FREQ[word] = freq self.total += freq if tag: self.user_word_tag_tab[word] = tag for ch in xrange(len(word)): wfrag = word[:ch + 1] if wfrag not in self.FREQ: self.FREQ[wfrag] = 0 if freq == 0: finalseg.add_force_split(word)
[ "Add", "a", "word", "to", "dictionary", "." ]
fxsjy/jieba
python
https://github.com/fxsjy/jieba/blob/8212b6c5725d08311952a3a08e5509eeaee33eb7/jieba/__init__.py#L397-L416
[ "def", "add_word", "(", "self", ",", "word", ",", "freq", "=", "None", ",", "tag", "=", "None", ")", ":", "self", ".", "check_initialized", "(", ")", "word", "=", "strdecode", "(", "word", ")", "freq", "=", "int", "(", "freq", ")", "if", "freq", ...
8212b6c5725d08311952a3a08e5509eeaee33eb7
train
Tokenizer.suggest_freq
Suggest word frequency to force the characters in a word to be joined or splitted. Parameter: - segment : The segments that the word is expected to be cut into, If the word should be treated as a whole, use a str. - tune : If True, tune the word frequency. Note that HMM may affect the final result. If the result doesn't change, set HMM=False.
jieba/__init__.py
def suggest_freq(self, segment, tune=False): """ Suggest word frequency to force the characters in a word to be joined or splitted. Parameter: - segment : The segments that the word is expected to be cut into, If the word should be treated as a whole, use a str. - tune : If True, tune the word frequency. Note that HMM may affect the final result. If the result doesn't change, set HMM=False. """ self.check_initialized() ftotal = float(self.total) freq = 1 if isinstance(segment, string_types): word = segment for seg in self.cut(word, HMM=False): freq *= self.FREQ.get(seg, 1) / ftotal freq = max(int(freq * self.total) + 1, self.FREQ.get(word, 1)) else: segment = tuple(map(strdecode, segment)) word = ''.join(segment) for seg in segment: freq *= self.FREQ.get(seg, 1) / ftotal freq = min(int(freq * self.total), self.FREQ.get(word, 0)) if tune: add_word(word, freq) return freq
def suggest_freq(self, segment, tune=False): """ Suggest word frequency to force the characters in a word to be joined or splitted. Parameter: - segment : The segments that the word is expected to be cut into, If the word should be treated as a whole, use a str. - tune : If True, tune the word frequency. Note that HMM may affect the final result. If the result doesn't change, set HMM=False. """ self.check_initialized() ftotal = float(self.total) freq = 1 if isinstance(segment, string_types): word = segment for seg in self.cut(word, HMM=False): freq *= self.FREQ.get(seg, 1) / ftotal freq = max(int(freq * self.total) + 1, self.FREQ.get(word, 1)) else: segment = tuple(map(strdecode, segment)) word = ''.join(segment) for seg in segment: freq *= self.FREQ.get(seg, 1) / ftotal freq = min(int(freq * self.total), self.FREQ.get(word, 0)) if tune: add_word(word, freq) return freq
[ "Suggest", "word", "frequency", "to", "force", "the", "characters", "in", "a", "word", "to", "be", "joined", "or", "splitted", "." ]
fxsjy/jieba
python
https://github.com/fxsjy/jieba/blob/8212b6c5725d08311952a3a08e5509eeaee33eb7/jieba/__init__.py#L424-L453
[ "def", "suggest_freq", "(", "self", ",", "segment", ",", "tune", "=", "False", ")", ":", "self", ".", "check_initialized", "(", ")", "ftotal", "=", "float", "(", "self", ".", "total", ")", "freq", "=", "1", "if", "isinstance", "(", "segment", ",", "s...
8212b6c5725d08311952a3a08e5509eeaee33eb7
train
Tokenizer.tokenize
Tokenize a sentence and yields tuples of (word, start, end) Parameter: - sentence: the str(unicode) to be segmented. - mode: "default" or "search", "search" is for finer segmentation. - HMM: whether to use the Hidden Markov Model.
jieba/__init__.py
def tokenize(self, unicode_sentence, mode="default", HMM=True): """ Tokenize a sentence and yields tuples of (word, start, end) Parameter: - sentence: the str(unicode) to be segmented. - mode: "default" or "search", "search" is for finer segmentation. - HMM: whether to use the Hidden Markov Model. """ if not isinstance(unicode_sentence, text_type): raise ValueError("jieba: the input parameter should be unicode.") start = 0 if mode == 'default': for w in self.cut(unicode_sentence, HMM=HMM): width = len(w) yield (w, start, start + width) start += width else: for w in self.cut(unicode_sentence, HMM=HMM): width = len(w) if len(w) > 2: for i in xrange(len(w) - 1): gram2 = w[i:i + 2] if self.FREQ.get(gram2): yield (gram2, start + i, start + i + 2) if len(w) > 3: for i in xrange(len(w) - 2): gram3 = w[i:i + 3] if self.FREQ.get(gram3): yield (gram3, start + i, start + i + 3) yield (w, start, start + width) start += width
def tokenize(self, unicode_sentence, mode="default", HMM=True): """ Tokenize a sentence and yields tuples of (word, start, end) Parameter: - sentence: the str(unicode) to be segmented. - mode: "default" or "search", "search" is for finer segmentation. - HMM: whether to use the Hidden Markov Model. """ if not isinstance(unicode_sentence, text_type): raise ValueError("jieba: the input parameter should be unicode.") start = 0 if mode == 'default': for w in self.cut(unicode_sentence, HMM=HMM): width = len(w) yield (w, start, start + width) start += width else: for w in self.cut(unicode_sentence, HMM=HMM): width = len(w) if len(w) > 2: for i in xrange(len(w) - 1): gram2 = w[i:i + 2] if self.FREQ.get(gram2): yield (gram2, start + i, start + i + 2) if len(w) > 3: for i in xrange(len(w) - 2): gram3 = w[i:i + 3] if self.FREQ.get(gram3): yield (gram3, start + i, start + i + 3) yield (w, start, start + width) start += width
[ "Tokenize", "a", "sentence", "and", "yields", "tuples", "of", "(", "word", "start", "end", ")" ]
fxsjy/jieba
python
https://github.com/fxsjy/jieba/blob/8212b6c5725d08311952a3a08e5509eeaee33eb7/jieba/__init__.py#L455-L486
[ "def", "tokenize", "(", "self", ",", "unicode_sentence", ",", "mode", "=", "\"default\"", ",", "HMM", "=", "True", ")", ":", "if", "not", "isinstance", "(", "unicode_sentence", ",", "text_type", ")", ":", "raise", "ValueError", "(", "\"jieba: the input paramet...
8212b6c5725d08311952a3a08e5509eeaee33eb7
train
TextRank.textrank
Extract keywords from sentence using TextRank algorithm. Parameter: - topK: return how many top keywords. `None` for all possible words. - withWeight: if True, return a list of (word, weight); if False, return a list of words. - allowPOS: the allowed POS list eg. ['ns', 'n', 'vn', 'v']. if the POS of w is not in this list, it will be filtered. - withFlag: if True, return a list of pair(word, weight) like posseg.cut if False, return a list of words
jieba/analyse/textrank.py
def textrank(self, sentence, topK=20, withWeight=False, allowPOS=('ns', 'n', 'vn', 'v'), withFlag=False): """ Extract keywords from sentence using TextRank algorithm. Parameter: - topK: return how many top keywords. `None` for all possible words. - withWeight: if True, return a list of (word, weight); if False, return a list of words. - allowPOS: the allowed POS list eg. ['ns', 'n', 'vn', 'v']. if the POS of w is not in this list, it will be filtered. - withFlag: if True, return a list of pair(word, weight) like posseg.cut if False, return a list of words """ self.pos_filt = frozenset(allowPOS) g = UndirectWeightedGraph() cm = defaultdict(int) words = tuple(self.tokenizer.cut(sentence)) for i, wp in enumerate(words): if self.pairfilter(wp): for j in xrange(i + 1, i + self.span): if j >= len(words): break if not self.pairfilter(words[j]): continue if allowPOS and withFlag: cm[(wp, words[j])] += 1 else: cm[(wp.word, words[j].word)] += 1 for terms, w in cm.items(): g.addEdge(terms[0], terms[1], w) nodes_rank = g.rank() if withWeight: tags = sorted(nodes_rank.items(), key=itemgetter(1), reverse=True) else: tags = sorted(nodes_rank, key=nodes_rank.__getitem__, reverse=True) if topK: return tags[:topK] else: return tags
def textrank(self, sentence, topK=20, withWeight=False, allowPOS=('ns', 'n', 'vn', 'v'), withFlag=False): """ Extract keywords from sentence using TextRank algorithm. Parameter: - topK: return how many top keywords. `None` for all possible words. - withWeight: if True, return a list of (word, weight); if False, return a list of words. - allowPOS: the allowed POS list eg. ['ns', 'n', 'vn', 'v']. if the POS of w is not in this list, it will be filtered. - withFlag: if True, return a list of pair(word, weight) like posseg.cut if False, return a list of words """ self.pos_filt = frozenset(allowPOS) g = UndirectWeightedGraph() cm = defaultdict(int) words = tuple(self.tokenizer.cut(sentence)) for i, wp in enumerate(words): if self.pairfilter(wp): for j in xrange(i + 1, i + self.span): if j >= len(words): break if not self.pairfilter(words[j]): continue if allowPOS and withFlag: cm[(wp, words[j])] += 1 else: cm[(wp.word, words[j].word)] += 1 for terms, w in cm.items(): g.addEdge(terms[0], terms[1], w) nodes_rank = g.rank() if withWeight: tags = sorted(nodes_rank.items(), key=itemgetter(1), reverse=True) else: tags = sorted(nodes_rank, key=nodes_rank.__getitem__, reverse=True) if topK: return tags[:topK] else: return tags
[ "Extract", "keywords", "from", "sentence", "using", "TextRank", "algorithm", ".", "Parameter", ":", "-", "topK", ":", "return", "how", "many", "top", "keywords", ".", "None", "for", "all", "possible", "words", ".", "-", "withWeight", ":", "if", "True", "re...
fxsjy/jieba
python
https://github.com/fxsjy/jieba/blob/8212b6c5725d08311952a3a08e5509eeaee33eb7/jieba/analyse/textrank.py#L69-L108
[ "def", "textrank", "(", "self", ",", "sentence", ",", "topK", "=", "20", ",", "withWeight", "=", "False", ",", "allowPOS", "=", "(", "'ns'", ",", "'n'", ",", "'vn'", ",", "'v'", ")", ",", "withFlag", "=", "False", ")", ":", "self", ".", "pos_filt",...
8212b6c5725d08311952a3a08e5509eeaee33eb7
train
TFIDF.extract_tags
Extract keywords from sentence using TF-IDF algorithm. Parameter: - topK: return how many top keywords. `None` for all possible words. - withWeight: if True, return a list of (word, weight); if False, return a list of words. - allowPOS: the allowed POS list eg. ['ns', 'n', 'vn', 'v','nr']. if the POS of w is not in this list,it will be filtered. - withFlag: only work with allowPOS is not empty. if True, return a list of pair(word, weight) like posseg.cut if False, return a list of words
jieba/analyse/tfidf.py
def extract_tags(self, sentence, topK=20, withWeight=False, allowPOS=(), withFlag=False): """ Extract keywords from sentence using TF-IDF algorithm. Parameter: - topK: return how many top keywords. `None` for all possible words. - withWeight: if True, return a list of (word, weight); if False, return a list of words. - allowPOS: the allowed POS list eg. ['ns', 'n', 'vn', 'v','nr']. if the POS of w is not in this list,it will be filtered. - withFlag: only work with allowPOS is not empty. if True, return a list of pair(word, weight) like posseg.cut if False, return a list of words """ if allowPOS: allowPOS = frozenset(allowPOS) words = self.postokenizer.cut(sentence) else: words = self.tokenizer.cut(sentence) freq = {} for w in words: if allowPOS: if w.flag not in allowPOS: continue elif not withFlag: w = w.word wc = w.word if allowPOS and withFlag else w if len(wc.strip()) < 2 or wc.lower() in self.stop_words: continue freq[w] = freq.get(w, 0.0) + 1.0 total = sum(freq.values()) for k in freq: kw = k.word if allowPOS and withFlag else k freq[k] *= self.idf_freq.get(kw, self.median_idf) / total if withWeight: tags = sorted(freq.items(), key=itemgetter(1), reverse=True) else: tags = sorted(freq, key=freq.__getitem__, reverse=True) if topK: return tags[:topK] else: return tags
def extract_tags(self, sentence, topK=20, withWeight=False, allowPOS=(), withFlag=False): """ Extract keywords from sentence using TF-IDF algorithm. Parameter: - topK: return how many top keywords. `None` for all possible words. - withWeight: if True, return a list of (word, weight); if False, return a list of words. - allowPOS: the allowed POS list eg. ['ns', 'n', 'vn', 'v','nr']. if the POS of w is not in this list,it will be filtered. - withFlag: only work with allowPOS is not empty. if True, return a list of pair(word, weight) like posseg.cut if False, return a list of words """ if allowPOS: allowPOS = frozenset(allowPOS) words = self.postokenizer.cut(sentence) else: words = self.tokenizer.cut(sentence) freq = {} for w in words: if allowPOS: if w.flag not in allowPOS: continue elif not withFlag: w = w.word wc = w.word if allowPOS and withFlag else w if len(wc.strip()) < 2 or wc.lower() in self.stop_words: continue freq[w] = freq.get(w, 0.0) + 1.0 total = sum(freq.values()) for k in freq: kw = k.word if allowPOS and withFlag else k freq[k] *= self.idf_freq.get(kw, self.median_idf) / total if withWeight: tags = sorted(freq.items(), key=itemgetter(1), reverse=True) else: tags = sorted(freq, key=freq.__getitem__, reverse=True) if topK: return tags[:topK] else: return tags
[ "Extract", "keywords", "from", "sentence", "using", "TF", "-", "IDF", "algorithm", ".", "Parameter", ":", "-", "topK", ":", "return", "how", "many", "top", "keywords", ".", "None", "for", "all", "possible", "words", ".", "-", "withWeight", ":", "if", "Tr...
fxsjy/jieba
python
https://github.com/fxsjy/jieba/blob/8212b6c5725d08311952a3a08e5509eeaee33eb7/jieba/analyse/tfidf.py#L75-L116
[ "def", "extract_tags", "(", "self", ",", "sentence", ",", "topK", "=", "20", ",", "withWeight", "=", "False", ",", "allowPOS", "=", "(", ")", ",", "withFlag", "=", "False", ")", ":", "if", "allowPOS", ":", "allowPOS", "=", "frozenset", "(", "allowPOS",...
8212b6c5725d08311952a3a08e5509eeaee33eb7
train
paracrawl_v3_pairs
Generates raw (English, other) pairs from a ParaCrawl V3.0 data file. Args: paracrawl_file: A ParaCrawl V3.0 en-.. data file. Yields: Pairs of (sentence_en, sentence_xx), as Unicode strings. Raises: StopIteration: If the file ends while this method is in the middle of creating a translation pair.
tensor2tensor/data_generators/cleaner_en_xx.py
def paracrawl_v3_pairs(paracrawl_file): """Generates raw (English, other) pairs from a ParaCrawl V3.0 data file. Args: paracrawl_file: A ParaCrawl V3.0 en-.. data file. Yields: Pairs of (sentence_en, sentence_xx), as Unicode strings. Raises: StopIteration: If the file ends while this method is in the middle of creating a translation pair. """ raw_sentences = _raw_sentences(paracrawl_file) for s_en in raw_sentences: try: s_xx = next(raw_sentences) if s_en and s_xx: # Prevent empty string examples. yield s_en, s_xx except StopIteration: tf.logging.error( 'Unmatched final sentence while reading in sentence pairs: [%s]', s_en)
def paracrawl_v3_pairs(paracrawl_file): """Generates raw (English, other) pairs from a ParaCrawl V3.0 data file. Args: paracrawl_file: A ParaCrawl V3.0 en-.. data file. Yields: Pairs of (sentence_en, sentence_xx), as Unicode strings. Raises: StopIteration: If the file ends while this method is in the middle of creating a translation pair. """ raw_sentences = _raw_sentences(paracrawl_file) for s_en in raw_sentences: try: s_xx = next(raw_sentences) if s_en and s_xx: # Prevent empty string examples. yield s_en, s_xx except StopIteration: tf.logging.error( 'Unmatched final sentence while reading in sentence pairs: [%s]', s_en)
[ "Generates", "raw", "(", "English", "other", ")", "pairs", "from", "a", "ParaCrawl", "V3", ".", "0", "data", "file", "." ]
tensorflow/tensor2tensor
python
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/data_generators/cleaner_en_xx.py#L66-L86
[ "def", "paracrawl_v3_pairs", "(", "paracrawl_file", ")", ":", "raw_sentences", "=", "_raw_sentences", "(", "paracrawl_file", ")", "for", "s_en", "in", "raw_sentences", ":", "try", ":", "s_xx", "=", "next", "(", "raw_sentences", ")", "if", "s_en", "and", "s_xx"...
272500b6efe353aeb638d2745ed56e519462ca31
train
_raw_sentences
Generates Unicode strings, one for each <seg> in a ParaCrawl data file. Also decodes some of the most common HTML entities found in ParaCrawl data. Args: paracrawl_file: A ParaCrawl V3.0 en-.. data file. Yields: One Unicode string for each <seg> element in the ParaCrawl data file.
tensor2tensor/data_generators/cleaner_en_xx.py
def _raw_sentences(paracrawl_file): """Generates Unicode strings, one for each <seg> in a ParaCrawl data file. Also decodes some of the most common HTML entities found in ParaCrawl data. Args: paracrawl_file: A ParaCrawl V3.0 en-.. data file. Yields: One Unicode string for each <seg> element in the ParaCrawl data file. """ for line_utf8 in paracrawl_file: line_uni = line_utf8.decode('UTF-8') text_match = re.match(r' +<seg>(.*)</seg>$', line_uni) if text_match: txt = text_match.group(1) txt = re.sub(r'&amp;', r'&', txt) txt = re.sub(r'& ?amp;', r'&', txt) txt = re.sub(r'& ?apos;', r"'", txt) txt = re.sub(r'& ?quot;', r'"', txt) txt = re.sub(r'& ?lt;', r'<', txt) txt = re.sub(r'& ?gt;', r'>', txt) yield txt
def _raw_sentences(paracrawl_file): """Generates Unicode strings, one for each <seg> in a ParaCrawl data file. Also decodes some of the most common HTML entities found in ParaCrawl data. Args: paracrawl_file: A ParaCrawl V3.0 en-.. data file. Yields: One Unicode string for each <seg> element in the ParaCrawl data file. """ for line_utf8 in paracrawl_file: line_uni = line_utf8.decode('UTF-8') text_match = re.match(r' +<seg>(.*)</seg>$', line_uni) if text_match: txt = text_match.group(1) txt = re.sub(r'&amp;', r'&', txt) txt = re.sub(r'& ?amp;', r'&', txt) txt = re.sub(r'& ?apos;', r"'", txt) txt = re.sub(r'& ?quot;', r'"', txt) txt = re.sub(r'& ?lt;', r'<', txt) txt = re.sub(r'& ?gt;', r'>', txt) yield txt
[ "Generates", "Unicode", "strings", "one", "for", "each", "<seg", ">", "in", "a", "ParaCrawl", "data", "file", "." ]
tensorflow/tensor2tensor
python
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/data_generators/cleaner_en_xx.py#L89-L110
[ "def", "_raw_sentences", "(", "paracrawl_file", ")", ":", "for", "line_utf8", "in", "paracrawl_file", ":", "line_uni", "=", "line_utf8", ".", "decode", "(", "'UTF-8'", ")", "text_match", "=", "re", ".", "match", "(", "r' +<seg>(.*)</seg>$'", ",", "line_uni", "...
272500b6efe353aeb638d2745ed56e519462ca31
train
clean_en_xx_pairs
Generates a cleaned-up stream of (English, other) translation pairs. Cleaning includes both filtering and simplistic sentence splitting, with minimal assumptions on the non-English pair member: (1) All filtering is done based on the English member of the pair, and (2) sentence splitting assumes only that sentences can end with one of '.!?' and begin with an ASCII uppercase letter. Input pairs that would get split into different numbers of sentences (e.g., three English sentences vs. two German ones) are discarded. Args: en_xx_pairs: A stream (iterable) of Unicode string pairs. Each item in the stream should be a (sentence_en, sentence_xx) pair. Yields: Cleaned-up (sentence_en, sentence_xx) pairs.
tensor2tensor/data_generators/cleaner_en_xx.py
def clean_en_xx_pairs(en_xx_pairs): """Generates a cleaned-up stream of (English, other) translation pairs. Cleaning includes both filtering and simplistic sentence splitting, with minimal assumptions on the non-English pair member: (1) All filtering is done based on the English member of the pair, and (2) sentence splitting assumes only that sentences can end with one of '.!?' and begin with an ASCII uppercase letter. Input pairs that would get split into different numbers of sentences (e.g., three English sentences vs. two German ones) are discarded. Args: en_xx_pairs: A stream (iterable) of Unicode string pairs. Each item in the stream should be a (sentence_en, sentence_xx) pair. Yields: Cleaned-up (sentence_en, sentence_xx) pairs. """ for s1, s2 in en_xx_pairs: if _regex_filter(s1): continue s1_list, s2_list = _split_sentences(s1, s2) if len(s1_list) != len(s2_list): continue # discard this pair elif len(s1_list) == 1: yield s1, s2 else: for s1_subsentence, s2_subsentence in itertools.izip(s1_list, s2_list): if _regex_filter(s1_subsentence): continue yield s1_subsentence, s2_subsentence
def clean_en_xx_pairs(en_xx_pairs): """Generates a cleaned-up stream of (English, other) translation pairs. Cleaning includes both filtering and simplistic sentence splitting, with minimal assumptions on the non-English pair member: (1) All filtering is done based on the English member of the pair, and (2) sentence splitting assumes only that sentences can end with one of '.!?' and begin with an ASCII uppercase letter. Input pairs that would get split into different numbers of sentences (e.g., three English sentences vs. two German ones) are discarded. Args: en_xx_pairs: A stream (iterable) of Unicode string pairs. Each item in the stream should be a (sentence_en, sentence_xx) pair. Yields: Cleaned-up (sentence_en, sentence_xx) pairs. """ for s1, s2 in en_xx_pairs: if _regex_filter(s1): continue s1_list, s2_list = _split_sentences(s1, s2) if len(s1_list) != len(s2_list): continue # discard this pair elif len(s1_list) == 1: yield s1, s2 else: for s1_subsentence, s2_subsentence in itertools.izip(s1_list, s2_list): if _regex_filter(s1_subsentence): continue yield s1_subsentence, s2_subsentence
[ "Generates", "a", "cleaned", "-", "up", "stream", "of", "(", "English", "other", ")", "translation", "pairs", "." ]
tensorflow/tensor2tensor
python
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/data_generators/cleaner_en_xx.py#L113-L142
[ "def", "clean_en_xx_pairs", "(", "en_xx_pairs", ")", ":", "for", "s1", ",", "s2", "in", "en_xx_pairs", ":", "if", "_regex_filter", "(", "s1", ")", ":", "continue", "s1_list", ",", "s2_list", "=", "_split_sentences", "(", "s1", ",", "s2", ")", "if", "len"...
272500b6efe353aeb638d2745ed56e519462ca31
train
_get_case_file_paths
Obtain a list of image paths corresponding to training or eval case. Args: tmp_dir: str, the root path to which raw images were written, at the top level having meta/ and raw/ subdirs. case: bool, whether obtaining file paths for training (true) or eval (false). training_fraction: float, the fraction of the sub-image path list to consider as the basis for training examples. Returns: list: A list of file paths. Raises: ValueError: if images not found in tmp_dir, or if training_fraction would leave no examples for eval.
tensor2tensor/data_generators/allen_brain.py
def _get_case_file_paths(tmp_dir, case, training_fraction=0.95): """Obtain a list of image paths corresponding to training or eval case. Args: tmp_dir: str, the root path to which raw images were written, at the top level having meta/ and raw/ subdirs. case: bool, whether obtaining file paths for training (true) or eval (false). training_fraction: float, the fraction of the sub-image path list to consider as the basis for training examples. Returns: list: A list of file paths. Raises: ValueError: if images not found in tmp_dir, or if training_fraction would leave no examples for eval. """ paths = tf.gfile.Glob("%s/*.jpg" % tmp_dir) if not paths: raise ValueError("Search of tmp_dir (%s) " % tmp_dir, "for subimage paths yielded an empty list, ", "can't proceed with returning training/eval split.") split_index = int(math.floor(len(paths)*training_fraction)) if split_index >= len(paths): raise ValueError("For a path list of size %s " "and a training_fraction of %s " "the resulting split_index of the paths list, " "%s, would leave no elements for the eval " "condition." % (len(paths), training_fraction, split_index)) if case: return paths[:split_index] else: return paths[split_index:]
def _get_case_file_paths(tmp_dir, case, training_fraction=0.95): """Obtain a list of image paths corresponding to training or eval case. Args: tmp_dir: str, the root path to which raw images were written, at the top level having meta/ and raw/ subdirs. case: bool, whether obtaining file paths for training (true) or eval (false). training_fraction: float, the fraction of the sub-image path list to consider as the basis for training examples. Returns: list: A list of file paths. Raises: ValueError: if images not found in tmp_dir, or if training_fraction would leave no examples for eval. """ paths = tf.gfile.Glob("%s/*.jpg" % tmp_dir) if not paths: raise ValueError("Search of tmp_dir (%s) " % tmp_dir, "for subimage paths yielded an empty list, ", "can't proceed with returning training/eval split.") split_index = int(math.floor(len(paths)*training_fraction)) if split_index >= len(paths): raise ValueError("For a path list of size %s " "and a training_fraction of %s " "the resulting split_index of the paths list, " "%s, would leave no elements for the eval " "condition." % (len(paths), training_fraction, split_index)) if case: return paths[:split_index] else: return paths[split_index:]
[ "Obtain", "a", "list", "of", "image", "paths", "corresponding", "to", "training", "or", "eval", "case", "." ]
tensorflow/tensor2tensor
python
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/data_generators/allen_brain.py#L81-L121
[ "def", "_get_case_file_paths", "(", "tmp_dir", ",", "case", ",", "training_fraction", "=", "0.95", ")", ":", "paths", "=", "tf", ".", "gfile", ".", "Glob", "(", "\"%s/*.jpg\"", "%", "tmp_dir", ")", "if", "not", "paths", ":", "raise", "ValueError", "(", "...
272500b6efe353aeb638d2745ed56e519462ca31
train
maybe_download_image_dataset
Download a set of images from api.brain-map.org to `target_dir`. Args: image_ids: list, a list of image ids. target_dir: str, a directory to which to download the images.
tensor2tensor/data_generators/allen_brain.py
def maybe_download_image_dataset(image_ids, target_dir): """Download a set of images from api.brain-map.org to `target_dir`. Args: image_ids: list, a list of image ids. target_dir: str, a directory to which to download the images. """ tf.gfile.MakeDirs(target_dir) num_images = len(image_ids) for i, image_id in enumerate(image_ids): destination = os.path.join(target_dir, "%s.jpg" % i) tmp_destination = "%s.temp" % destination source_url = ("http://api.brain-map.org/api/v2/" "section_image_download/%s" % image_id) if tf.gfile.Exists(destination): tf.logging.info("Image with ID already present, " "skipping download (%s of %s)." % ( i+1, num_images )) continue tf.logging.info("Downloading image with id %s (%s of %s)" % ( image_id, i+1, num_images )) response = requests.get(source_url, stream=True) response.raise_for_status() with tf.gfile.Open(tmp_destination, "w") as f: for block in response.iter_content(1024): f.write(block) tf.gfile.Rename(tmp_destination, destination)
def maybe_download_image_dataset(image_ids, target_dir): """Download a set of images from api.brain-map.org to `target_dir`. Args: image_ids: list, a list of image ids. target_dir: str, a directory to which to download the images. """ tf.gfile.MakeDirs(target_dir) num_images = len(image_ids) for i, image_id in enumerate(image_ids): destination = os.path.join(target_dir, "%s.jpg" % i) tmp_destination = "%s.temp" % destination source_url = ("http://api.brain-map.org/api/v2/" "section_image_download/%s" % image_id) if tf.gfile.Exists(destination): tf.logging.info("Image with ID already present, " "skipping download (%s of %s)." % ( i+1, num_images )) continue tf.logging.info("Downloading image with id %s (%s of %s)" % ( image_id, i+1, num_images )) response = requests.get(source_url, stream=True) response.raise_for_status() with tf.gfile.Open(tmp_destination, "w") as f: for block in response.iter_content(1024): f.write(block) tf.gfile.Rename(tmp_destination, destination)
[ "Download", "a", "set", "of", "images", "from", "api", ".", "brain", "-", "map", ".", "org", "to", "target_dir", "." ]
tensorflow/tensor2tensor
python
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/data_generators/allen_brain.py#L124-L163
[ "def", "maybe_download_image_dataset", "(", "image_ids", ",", "target_dir", ")", ":", "tf", ".", "gfile", ".", "MakeDirs", "(", "target_dir", ")", "num_images", "=", "len", "(", "image_ids", ")", "for", "i", ",", "image_id", "in", "enumerate", "(", "image_id...
272500b6efe353aeb638d2745ed56e519462ca31
train
random_square_mask
Create a numpy array with specified shape and masked fraction. Args: shape: tuple, shape of the mask to create. fraction: float, fraction of the mask area to populate with `mask_scalar`. Returns: numpy.array: A numpy array storing the mask.
tensor2tensor/data_generators/allen_brain.py
def random_square_mask(shape, fraction): """Create a numpy array with specified shape and masked fraction. Args: shape: tuple, shape of the mask to create. fraction: float, fraction of the mask area to populate with `mask_scalar`. Returns: numpy.array: A numpy array storing the mask. """ mask = np.ones(shape) patch_area = shape[0]*shape[1]*fraction patch_dim = np.int(math.floor(math.sqrt(patch_area))) if patch_area == 0 or patch_dim == 0: return mask x = np.random.randint(shape[0] - patch_dim) y = np.random.randint(shape[1] - patch_dim) mask[x:(x + patch_dim), y:(y + patch_dim), :] = 0 return mask
def random_square_mask(shape, fraction): """Create a numpy array with specified shape and masked fraction. Args: shape: tuple, shape of the mask to create. fraction: float, fraction of the mask area to populate with `mask_scalar`. Returns: numpy.array: A numpy array storing the mask. """ mask = np.ones(shape) patch_area = shape[0]*shape[1]*fraction patch_dim = np.int(math.floor(math.sqrt(patch_area))) if patch_area == 0 or patch_dim == 0: return mask x = np.random.randint(shape[0] - patch_dim) y = np.random.randint(shape[1] - patch_dim) mask[x:(x + patch_dim), y:(y + patch_dim), :] = 0 return mask
[ "Create", "a", "numpy", "array", "with", "specified", "shape", "and", "masked", "fraction", "." ]
tensorflow/tensor2tensor
python
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/data_generators/allen_brain.py#L166-L189
[ "def", "random_square_mask", "(", "shape", ",", "fraction", ")", ":", "mask", "=", "np", ".", "ones", "(", "shape", ")", "patch_area", "=", "shape", "[", "0", "]", "*", "shape", "[", "1", "]", "*", "fraction", "patch_dim", "=", "np", ".", "int", "(...
272500b6efe353aeb638d2745ed56e519462ca31
train
_generator
Base problem example generator for Allen Brain Atlas problems. Args: tmp_dir: str, a directory where raw example input data has been stored. training: bool, whether the mode of operation is training (or, alternatively, evaluation), determining whether examples in tmp_dir prefixed with train or dev will be used. size: int, the image size to add to the example annotation. training_fraction: float, the fraction of the sub-image path list to consider as the basis for training examples. Yields: A dictionary representing the images with the following fields: * image/encoded: The string encoding the image as JPEG. * image/format: The string "jpeg" indicating the image format. * image/height: The integer indicating the image height. * image/width: The integer indicating the image height.
tensor2tensor/data_generators/allen_brain.py
def _generator(tmp_dir, training, size=_BASE_EXAMPLE_IMAGE_SIZE, training_fraction=0.95): """Base problem example generator for Allen Brain Atlas problems. Args: tmp_dir: str, a directory where raw example input data has been stored. training: bool, whether the mode of operation is training (or, alternatively, evaluation), determining whether examples in tmp_dir prefixed with train or dev will be used. size: int, the image size to add to the example annotation. training_fraction: float, the fraction of the sub-image path list to consider as the basis for training examples. Yields: A dictionary representing the images with the following fields: * image/encoded: The string encoding the image as JPEG. * image/format: The string "jpeg" indicating the image format. * image/height: The integer indicating the image height. * image/width: The integer indicating the image height. """ maybe_download_image_dataset(_IMAGE_IDS, tmp_dir) image_files = _get_case_file_paths(tmp_dir=tmp_dir, case=training, training_fraction=training_fraction) image_obj = PIL_Image() tf.logging.info("Loaded case file paths (n=%s)" % len(image_files)) height = size width = size for input_path in image_files: img = image_obj.open(input_path) img = np.float32(img) shape = np.shape(img) for h_index in range(0, int(math.floor(shape[0]/size))): h_offset = h_index * size h_end = h_offset + size - 1 for v_index in range(0, int(math.floor(shape[1]/size))): v_offset = v_index * size v_end = v_offset + size - 1 # Extract a sub-image tile. subimage = np.uint8(img[h_offset:h_end, v_offset:v_end]) # pylint: disable=invalid-sequence-index # Filter images that are likely background (not tissue). if np.amax(subimage) < 230: continue subimage = image_obj.fromarray(subimage) buff = BytesIO() subimage.save(buff, format="JPEG") subimage_encoded = buff.getvalue() yield { "image/encoded": [subimage_encoded], "image/format": ["jpeg"], "image/height": [height], "image/width": [width] }
def _generator(tmp_dir, training, size=_BASE_EXAMPLE_IMAGE_SIZE, training_fraction=0.95): """Base problem example generator for Allen Brain Atlas problems. Args: tmp_dir: str, a directory where raw example input data has been stored. training: bool, whether the mode of operation is training (or, alternatively, evaluation), determining whether examples in tmp_dir prefixed with train or dev will be used. size: int, the image size to add to the example annotation. training_fraction: float, the fraction of the sub-image path list to consider as the basis for training examples. Yields: A dictionary representing the images with the following fields: * image/encoded: The string encoding the image as JPEG. * image/format: The string "jpeg" indicating the image format. * image/height: The integer indicating the image height. * image/width: The integer indicating the image height. """ maybe_download_image_dataset(_IMAGE_IDS, tmp_dir) image_files = _get_case_file_paths(tmp_dir=tmp_dir, case=training, training_fraction=training_fraction) image_obj = PIL_Image() tf.logging.info("Loaded case file paths (n=%s)" % len(image_files)) height = size width = size for input_path in image_files: img = image_obj.open(input_path) img = np.float32(img) shape = np.shape(img) for h_index in range(0, int(math.floor(shape[0]/size))): h_offset = h_index * size h_end = h_offset + size - 1 for v_index in range(0, int(math.floor(shape[1]/size))): v_offset = v_index * size v_end = v_offset + size - 1 # Extract a sub-image tile. subimage = np.uint8(img[h_offset:h_end, v_offset:v_end]) # pylint: disable=invalid-sequence-index # Filter images that are likely background (not tissue). if np.amax(subimage) < 230: continue subimage = image_obj.fromarray(subimage) buff = BytesIO() subimage.save(buff, format="JPEG") subimage_encoded = buff.getvalue() yield { "image/encoded": [subimage_encoded], "image/format": ["jpeg"], "image/height": [height], "image/width": [width] }
[ "Base", "problem", "example", "generator", "for", "Allen", "Brain", "Atlas", "problems", "." ]
tensorflow/tensor2tensor
python
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/data_generators/allen_brain.py#L192-L260
[ "def", "_generator", "(", "tmp_dir", ",", "training", ",", "size", "=", "_BASE_EXAMPLE_IMAGE_SIZE", ",", "training_fraction", "=", "0.95", ")", ":", "maybe_download_image_dataset", "(", "_IMAGE_IDS", ",", "tmp_dir", ")", "image_files", "=", "_get_case_file_paths", "...
272500b6efe353aeb638d2745ed56e519462ca31
train
transformer_moe_base
Set of hyperparameters.
tensor2tensor/models/research/transformer_moe.py
def transformer_moe_base(): """Set of hyperparameters.""" hparams = common_hparams.basic_params1() hparams.norm_type = "layer" hparams.hidden_size = 512 hparams.batch_size = 4096 hparams.max_length = 2001 hparams.max_input_seq_length = 2000 hparams.max_target_seq_length = 2000 hparams.dropout = 0.0 hparams.clip_grad_norm = 0. # i.e. no gradient clipping hparams.optimizer_adam_epsilon = 1e-9 hparams.learning_rate_decay_scheme = "noam" hparams.learning_rate = 0.1 hparams.learning_rate_warmup_steps = 2000 hparams.initializer_gain = 1.0 hparams.num_hidden_layers = 5 hparams.initializer = "uniform_unit_scaling" hparams.weight_decay = 0.0 hparams.optimizer_adam_beta1 = 0.9 hparams.optimizer_adam_beta2 = 0.98 hparams.num_sampled_classes = 0 hparams.label_smoothing = 0.0 hparams.shared_embedding_and_softmax_weights = True # According to noam, ("n", "da") seems better for harder-to-learn models hparams.layer_preprocess_sequence = "n" hparams.layer_postprocess_sequence = "da" # Hparams used by transformer_prepare_decoder() function hparams.add_hparam("pos", "timing") # timing, none hparams.add_hparam("proximity_bias", False) hparams.add_hparam("causal_decoder_self_attention", True) hparams = common_attention.add_standard_attention_hparams(hparams) # Decoder layers type. If set, num_decoder_layers parameter will be ignored # and the number of decoder layer will be deduced from the string # See top file comment for example of usage hparams.add_hparam("layer_types", "") # Default attention type (ex: a, loc, red,...) and feed-forward type (ex: fc, # sep, moe,...) hparams.add_hparam("default_att", "a") hparams.add_hparam("default_ff", "fc") return hparams
def transformer_moe_base(): """Set of hyperparameters.""" hparams = common_hparams.basic_params1() hparams.norm_type = "layer" hparams.hidden_size = 512 hparams.batch_size = 4096 hparams.max_length = 2001 hparams.max_input_seq_length = 2000 hparams.max_target_seq_length = 2000 hparams.dropout = 0.0 hparams.clip_grad_norm = 0. # i.e. no gradient clipping hparams.optimizer_adam_epsilon = 1e-9 hparams.learning_rate_decay_scheme = "noam" hparams.learning_rate = 0.1 hparams.learning_rate_warmup_steps = 2000 hparams.initializer_gain = 1.0 hparams.num_hidden_layers = 5 hparams.initializer = "uniform_unit_scaling" hparams.weight_decay = 0.0 hparams.optimizer_adam_beta1 = 0.9 hparams.optimizer_adam_beta2 = 0.98 hparams.num_sampled_classes = 0 hparams.label_smoothing = 0.0 hparams.shared_embedding_and_softmax_weights = True # According to noam, ("n", "da") seems better for harder-to-learn models hparams.layer_preprocess_sequence = "n" hparams.layer_postprocess_sequence = "da" # Hparams used by transformer_prepare_decoder() function hparams.add_hparam("pos", "timing") # timing, none hparams.add_hparam("proximity_bias", False) hparams.add_hparam("causal_decoder_self_attention", True) hparams = common_attention.add_standard_attention_hparams(hparams) # Decoder layers type. If set, num_decoder_layers parameter will be ignored # and the number of decoder layer will be deduced from the string # See top file comment for example of usage hparams.add_hparam("layer_types", "") # Default attention type (ex: a, loc, red,...) and feed-forward type (ex: fc, # sep, moe,...) hparams.add_hparam("default_att", "a") hparams.add_hparam("default_ff", "fc") return hparams
[ "Set", "of", "hyperparameters", "." ]
tensorflow/tensor2tensor
python
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/models/research/transformer_moe.py#L267-L311
[ "def", "transformer_moe_base", "(", ")", ":", "hparams", "=", "common_hparams", ".", "basic_params1", "(", ")", "hparams", ".", "norm_type", "=", "\"layer\"", "hparams", ".", "hidden_size", "=", "512", "hparams", ".", "batch_size", "=", "4096", "hparams", ".",...
272500b6efe353aeb638d2745ed56e519462ca31
train
transformer_moe_8k
Hyper parameters specifics for long sequence generation.
tensor2tensor/models/research/transformer_moe.py
def transformer_moe_8k(): """Hyper parameters specifics for long sequence generation.""" hparams = transformer_moe_base() hparams.batch_size = 8192 hparams.max_length = 0 # max_length == batch_size hparams.eval_drop_long_sequences = True hparams.min_length_bucket = 256 # Avoid cyclic problems for big batches hparams.default_ff = "sep" hparams.hidden_size = 1024 return hparams
def transformer_moe_8k(): """Hyper parameters specifics for long sequence generation.""" hparams = transformer_moe_base() hparams.batch_size = 8192 hparams.max_length = 0 # max_length == batch_size hparams.eval_drop_long_sequences = True hparams.min_length_bucket = 256 # Avoid cyclic problems for big batches hparams.default_ff = "sep" hparams.hidden_size = 1024 return hparams
[ "Hyper", "parameters", "specifics", "for", "long", "sequence", "generation", "." ]
tensorflow/tensor2tensor
python
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/models/research/transformer_moe.py#L315-L327
[ "def", "transformer_moe_8k", "(", ")", ":", "hparams", "=", "transformer_moe_base", "(", ")", "hparams", ".", "batch_size", "=", "8192", "hparams", ".", "max_length", "=", "0", "# max_length == batch_size", "hparams", ".", "eval_drop_long_sequences", "=", "True", ...
272500b6efe353aeb638d2745ed56e519462ca31
train
transformer_moe_2k
Base transformers model with moe. Will have the following architecture: * No encoder. * Layer 0: a - sep (self-attention - unmasked separable convolutions) * Layer 1: a - sep * Layer 2: a - sep * Layer 3: a - sep * Layer 4: a - sep * Decoder architecture: * Layer 0: a - a - sepm (self-attention - enco/deco-attention - masked sep) * Layer 1: a - a - sepm * Layer 2: a - a - moe (mixture of expert layers in the middle) * Layer 3: a - a - sepm * Layer 4: a - a - sepm Returns: hparams
tensor2tensor/models/research/transformer_moe.py
def transformer_moe_2k(): """Base transformers model with moe. Will have the following architecture: * No encoder. * Layer 0: a - sep (self-attention - unmasked separable convolutions) * Layer 1: a - sep * Layer 2: a - sep * Layer 3: a - sep * Layer 4: a - sep * Decoder architecture: * Layer 0: a - a - sepm (self-attention - enco/deco-attention - masked sep) * Layer 1: a - a - sepm * Layer 2: a - a - moe (mixture of expert layers in the middle) * Layer 3: a - a - sepm * Layer 4: a - a - sepm Returns: hparams """ hparams = transformer_moe_8k() hparams.batch_size = 2048 hparams.default_ff = "sep" # hparams.layer_types contains the network architecture: encoder_archi = "a/a/a/a/a" decoder_archi = "a-sepm/a-sepm/a-moe/a-sepm/a-sepm" hparams.layer_types = "{}#{}".format(encoder_archi, decoder_archi) return hparams
def transformer_moe_2k(): """Base transformers model with moe. Will have the following architecture: * No encoder. * Layer 0: a - sep (self-attention - unmasked separable convolutions) * Layer 1: a - sep * Layer 2: a - sep * Layer 3: a - sep * Layer 4: a - sep * Decoder architecture: * Layer 0: a - a - sepm (self-attention - enco/deco-attention - masked sep) * Layer 1: a - a - sepm * Layer 2: a - a - moe (mixture of expert layers in the middle) * Layer 3: a - a - sepm * Layer 4: a - a - sepm Returns: hparams """ hparams = transformer_moe_8k() hparams.batch_size = 2048 hparams.default_ff = "sep" # hparams.layer_types contains the network architecture: encoder_archi = "a/a/a/a/a" decoder_archi = "a-sepm/a-sepm/a-moe/a-sepm/a-sepm" hparams.layer_types = "{}#{}".format(encoder_archi, decoder_archi) return hparams
[ "Base", "transformers", "model", "with", "moe", "." ]
tensorflow/tensor2tensor
python
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/models/research/transformer_moe.py#L365-L395
[ "def", "transformer_moe_2k", "(", ")", ":", "hparams", "=", "transformer_moe_8k", "(", ")", "hparams", ".", "batch_size", "=", "2048", "hparams", ".", "default_ff", "=", "\"sep\"", "# hparams.layer_types contains the network architecture:", "encoder_archi", "=", "\"a/a/...
272500b6efe353aeb638d2745ed56e519462ca31
train
transformer_moe_prepend_8k
Model which formulate a seq2seq problem as language modeling.
tensor2tensor/models/research/transformer_moe.py
def transformer_moe_prepend_8k(): """Model which formulate a seq2seq problem as language modeling.""" hparams = transformer_moe_8k() hparams.prepend_mode = "prepend_inputs_masked_attention" hparams.eval_drop_long_sequences = False hparams.max_input_seq_length = 7500 hparams.default_ff = "sepm" hparams.layer_types = "locm/redm/locm-moe/redm/locm" hparams.moe_num_experts = 256 return hparams
def transformer_moe_prepend_8k(): """Model which formulate a seq2seq problem as language modeling.""" hparams = transformer_moe_8k() hparams.prepend_mode = "prepend_inputs_masked_attention" hparams.eval_drop_long_sequences = False hparams.max_input_seq_length = 7500 hparams.default_ff = "sepm" hparams.layer_types = "locm/redm/locm-moe/redm/locm" hparams.moe_num_experts = 256 return hparams
[ "Model", "which", "formulate", "a", "seq2seq", "problem", "as", "language", "modeling", "." ]
tensorflow/tensor2tensor
python
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/models/research/transformer_moe.py#L409-L418
[ "def", "transformer_moe_prepend_8k", "(", ")", ":", "hparams", "=", "transformer_moe_8k", "(", ")", "hparams", ".", "prepend_mode", "=", "\"prepend_inputs_masked_attention\"", "hparams", ".", "eval_drop_long_sequences", "=", "False", "hparams", ".", "max_input_seq_length"...
272500b6efe353aeb638d2745ed56e519462ca31
train
f
Applies residual function for RevNet. Args: x: input tensor depth1: Number of output channels for the first and second conv layers. depth2: Number of output channels for the third conv layer. dim: '2d' if 2-dimensional, '3d' if 3-dimensional. first_batch_norm: Whether to keep the first batch norm layer or not. Typically used in the first RevNet block. stride: Stride for the first conv filter. Note that this particular RevNet architecture only varies the stride for the first conv filter. The stride for the second conv filter is always set to 1. training: True for train phase, False for eval phase. bottleneck: If true, apply bottleneck 1x1 down/up sampling. padding: Padding for each conv layer. Returns: Output tensor after applying residual function for RevNet.
tensor2tensor/models/revnet.py
def f(x, depth1, depth2, dim='2d', first_batch_norm=True, stride=1, training=True, bottleneck=True, padding='SAME'): """Applies residual function for RevNet. Args: x: input tensor depth1: Number of output channels for the first and second conv layers. depth2: Number of output channels for the third conv layer. dim: '2d' if 2-dimensional, '3d' if 3-dimensional. first_batch_norm: Whether to keep the first batch norm layer or not. Typically used in the first RevNet block. stride: Stride for the first conv filter. Note that this particular RevNet architecture only varies the stride for the first conv filter. The stride for the second conv filter is always set to 1. training: True for train phase, False for eval phase. bottleneck: If true, apply bottleneck 1x1 down/up sampling. padding: Padding for each conv layer. Returns: Output tensor after applying residual function for RevNet. """ conv = CONFIG[dim]['conv'] with tf.variable_scope('f', reuse=tf.AUTO_REUSE): if first_batch_norm: net = tf.layers.batch_normalization(x, training=training) net = tf.nn.relu(net) else: net = x if bottleneck: net = conv(net, depth1, 1, strides=stride, padding=padding, activation=None) net = tf.layers.batch_normalization(net, training=training) net = tf.nn.relu(net) net = conv(net, depth1, 3, strides=1, padding=padding, activation=None) net = tf.layers.batch_normalization(net, training=training) net = tf.nn.relu(net) net = conv(net, depth2, 1, strides=1, padding=padding, activation=None) else: net = conv(net, depth2, 3, strides=stride, padding=padding, activation=None) net = tf.layers.batch_normalization(x, training=training) net = tf.nn.relu(net) net = conv(net, depth2, 3, strides=stride, padding=padding, activation=None) return net
def f(x, depth1, depth2, dim='2d', first_batch_norm=True, stride=1, training=True, bottleneck=True, padding='SAME'): """Applies residual function for RevNet. Args: x: input tensor depth1: Number of output channels for the first and second conv layers. depth2: Number of output channels for the third conv layer. dim: '2d' if 2-dimensional, '3d' if 3-dimensional. first_batch_norm: Whether to keep the first batch norm layer or not. Typically used in the first RevNet block. stride: Stride for the first conv filter. Note that this particular RevNet architecture only varies the stride for the first conv filter. The stride for the second conv filter is always set to 1. training: True for train phase, False for eval phase. bottleneck: If true, apply bottleneck 1x1 down/up sampling. padding: Padding for each conv layer. Returns: Output tensor after applying residual function for RevNet. """ conv = CONFIG[dim]['conv'] with tf.variable_scope('f', reuse=tf.AUTO_REUSE): if first_batch_norm: net = tf.layers.batch_normalization(x, training=training) net = tf.nn.relu(net) else: net = x if bottleneck: net = conv(net, depth1, 1, strides=stride, padding=padding, activation=None) net = tf.layers.batch_normalization(net, training=training) net = tf.nn.relu(net) net = conv(net, depth1, 3, strides=1, padding=padding, activation=None) net = tf.layers.batch_normalization(net, training=training) net = tf.nn.relu(net) net = conv(net, depth2, 1, strides=1, padding=padding, activation=None) else: net = conv(net, depth2, 3, strides=stride, padding=padding, activation=None) net = tf.layers.batch_normalization(x, training=training) net = tf.nn.relu(net) net = conv(net, depth2, 3, strides=stride, padding=padding, activation=None) return net
[ "Applies", "residual", "function", "for", "RevNet", "." ]
tensorflow/tensor2tensor
python
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/models/revnet.py#L72-L122
[ "def", "f", "(", "x", ",", "depth1", ",", "depth2", ",", "dim", "=", "'2d'", ",", "first_batch_norm", "=", "True", ",", "stride", "=", "1", ",", "training", "=", "True", ",", "bottleneck", "=", "True", ",", "padding", "=", "'SAME'", ")", ":", "conv...
272500b6efe353aeb638d2745ed56e519462ca31
train
downsample_bottleneck
Downsamples 'x' by `stride` using a 1x1 convolution filter. Args: x: input tensor of size [N, H, W, C] output_channels: Desired number of output channels. dim: '2d' if 2-dimensional, '3d' if 3-dimensional. stride: What stride to use. Usually 1 or 2. scope: Optional variable scope. Returns: A downsampled tensor of size [N, H/2, W/2, output_channels] if stride is 2, else returns a tensor of size [N, H, W, output_channels] if stride is 1.
tensor2tensor/models/revnet.py
def downsample_bottleneck(x, output_channels, dim='2d', stride=1, scope='h'): """Downsamples 'x' by `stride` using a 1x1 convolution filter. Args: x: input tensor of size [N, H, W, C] output_channels: Desired number of output channels. dim: '2d' if 2-dimensional, '3d' if 3-dimensional. stride: What stride to use. Usually 1 or 2. scope: Optional variable scope. Returns: A downsampled tensor of size [N, H/2, W/2, output_channels] if stride is 2, else returns a tensor of size [N, H, W, output_channels] if stride is 1. """ conv = CONFIG[dim]['conv'] with tf.variable_scope(scope): x = conv(x, output_channels, 1, strides=stride, padding='SAME', activation=None) return x
def downsample_bottleneck(x, output_channels, dim='2d', stride=1, scope='h'): """Downsamples 'x' by `stride` using a 1x1 convolution filter. Args: x: input tensor of size [N, H, W, C] output_channels: Desired number of output channels. dim: '2d' if 2-dimensional, '3d' if 3-dimensional. stride: What stride to use. Usually 1 or 2. scope: Optional variable scope. Returns: A downsampled tensor of size [N, H/2, W/2, output_channels] if stride is 2, else returns a tensor of size [N, H, W, output_channels] if stride is 1. """ conv = CONFIG[dim]['conv'] with tf.variable_scope(scope): x = conv(x, output_channels, 1, strides=stride, padding='SAME', activation=None) return x
[ "Downsamples", "x", "by", "stride", "using", "a", "1x1", "convolution", "filter", "." ]
tensorflow/tensor2tensor
python
https://github.com/tensorflow/tensor2tensor/blob/272500b6efe353aeb638d2745ed56e519462ca31/tensor2tensor/models/revnet.py#L125-L144
[ "def", "downsample_bottleneck", "(", "x", ",", "output_channels", ",", "dim", "=", "'2d'", ",", "stride", "=", "1", ",", "scope", "=", "'h'", ")", ":", "conv", "=", "CONFIG", "[", "dim", "]", "[", "'conv'", "]", "with", "tf", ".", "variable_scope", "...
272500b6efe353aeb638d2745ed56e519462ca31