def filter_annotations(self, image_group, annotations_group, group):
# test all annotations
for index, (image, annotations) in enumerate(zip(image_group, annotations_group)):
assert(isinstance(annotations, np.ndarray)), '\'load_annotations\' should return a list of numpy arrays, received: {}'.format(type(annotations))
# test x2 < x1 | y2 < y1 | x1 < 0 | y1 < 0 | x2 <= 0 | y2 <= 0 | x2 >= image.shape[1] | y2 >= image.shape[0]
invalid_indices = np.where(
(annotations[:, 2] <= annotations[:, 0]) |
(annotations[:, 3] <= annotations[:, 1]) |
(annotations[:, 0] < 0) |
(annotations[:, 1] < 0) |
(annotations[:, 2] > image.shape[1]) |
(annotations[:, 3] > image.shape[0])
)[0]
# delete invalid indices
if len(invalid_indices):
warnings.warn('Image with id {} (shape {}) contains the following invalid boxes: {}.'.format(
group[index],
image.shape,
[annotations[invalid_index, :] for invalid_index in invalid_indices]
))
annotations_group[index] = np.delete(annotations, invalid_indices, axis=0)
return image_group, annotations_group
python类delete()的实例源码
def cellslice(UC, P_UC, slicing):
if slicing == 1:
P_UCS = P_UC
UCS = UC
else:
P_UCS = 0 # points in sliced unit cell
UCS = zeros([6, 1])
for i in range(P_UC):
if UC[0, i] in (2, 5, 7): # noslicing edges, rotators, diagnostics
UCS = hstack((UCS, UC[:, i].reshape(6, 1)))
P_UCS += 1
else:
UCS = hstack((UCS, UC[:, i].reshape(6, 1).repeat(slicing, 1)))
P_UCS += slicing
UCS = delete(UCS, 0, axis=1)
UCS[1, :] = UCS[1, :]/slicing
s = hstack((0, cumsum(UCS[1, :])))
return s, UCS, P_UCS
def append_neg_and_retrain(self, feat=None, force=False):
if feat is not None:
num = feat.shape[0]
self.neg = np.vstack((self.neg, feat))
self.num_neg_added += num
if self.num_neg_added > self.retrain_limit or force:
self.num_neg_added = 0
new_w_b, pos_scores, neg_scores = self.train()
# scores = np.dot(self.neg, new_w_b[0].T) + new_w_b[1]
# easy_inds = np.where(neg_scores < self.evict_thresh)[0]
not_easy_inds = np.where(neg_scores >= self.evict_thresh)[0]
if len(not_easy_inds) > 0:
self.neg = self.neg[not_easy_inds, :]
# self.neg = np.delete(self.neg, easy_inds)
print(' Pruning easy negatives')
print(' Cache holds {} pos examples and {} neg examples'.
format(self.pos.shape[0], self.neg.shape[0]))
print(' {} pos support vectors'.format((pos_scores <= 1).sum()))
print(' {} neg support vectors'.format((neg_scores >= -1).sum()))
return new_w_b
else:
return None
def test_fit_to_less_width(self):
"""Fit a tensor to a smalles width (i.e. trimming).
Given a 3D tensor of shape [batch, length, width], apply the
`ops.fit()` operator to it with the a smaller `width` as the
target one and check that the last axis of the tensor have been
deleted.
"""
batch = 2
length = 5
width = 4
fit_width = 3
delta = width - fit_width
shape = [None, None, None]
input_ = tf.placeholder(dtype=tf.float32, shape=shape)
output = ops.fit(input_, fit_width)
input_actual = np.random.rand(batch, length, width) # pylint: disable=I0011,E1101
delete_idx = [width - (i + 1) for i in range(delta)]
output_expected = np.delete(input_actual, delete_idx, axis=2) # pylint: disable=I0011,E1101
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
output_actual = sess.run(output, {input_: input_actual})
self.assertAllClose(output_expected, output_actual)
def prepare_data(img_folder):
X, Y, captcha_text = vecmp.load_dataset(folder=img_folder)
# invert and normalize to [0,1]
#X = (255- Xdata)/255.0
# standarization
# compute mean across the rows, sum elements from each column and divide
x_mean = X.mean(axis=0)
x_std = X.std(axis=0)
X = (X - x_mean) / (x_std + 0.00001)
test_size = min(1000, X.shape[0])
random_idx = np.random.choice(X.shape[0], test_size, replace=False)
test_X = X[random_idx, :]
test_Y = Y[random_idx, :]
X = np.delete(X, random_idx, axis=0)
Y = np.delete(Y, random_idx, axis=0)
return (X,Y,test_X,test_Y)
def append_neg_and_retrain(self, feat=None, force=False):
if feat is not None:
num = feat.shape[0]
self.neg = np.vstack((self.neg, feat))
self.num_neg_added += num
if self.num_neg_added > self.retrain_limit or force:
self.num_neg_added = 0
new_w_b, pos_scores, neg_scores = self.train()
# scores = np.dot(self.neg, new_w_b[0].T) + new_w_b[1]
# easy_inds = np.where(neg_scores < self.evict_thresh)[0]
not_easy_inds = np.where(neg_scores >= self.evict_thresh)[0]
if len(not_easy_inds) > 0:
self.neg = self.neg[not_easy_inds, :]
# self.neg = np.delete(self.neg, easy_inds)
print(' Pruning easy negatives')
print(' Cache holds {} pos examples and {} neg examples'.
format(self.pos.shape[0], self.neg.shape[0]))
print(' {} pos support vectors'.format((pos_scores <= 1).sum()))
print(' {} neg support vectors'.format((neg_scores >= -1).sum()))
return new_w_b
else:
return None
def __init__(self, table,reg=False,lamda=0):
"""Initializes Class for Linear Regression
Parameters
----------
table : ndarray(n-rows,m-features + 1)
Numerical training data, last column as training values
reg : Boolean
Set True to enable regularization, false by default
"""
#regularization parameters
self.reg = reg
self.lamda = lamda
self.num_training = np.shape(table)[0]
# remove the last column from training data to extract features data
self.X = np.delete(table, -1, 1)
# add a column of ones in front of the training data
self.X = np.insert(self.X, 0, np.ones(self.num_training), axis=1)
self.num_features = np.shape(self.X)[1]
# extract the values of the training set from the provided data
self.y = table[:, self.num_features - 1]
# create parameters and initialize to 1
self.theta = np.ones(self.num_features)
def compute_cost(self):
"""Computes cost based on the current values of the parameters
Returns
-------
cost : float
Cost of the selection of current set of parameters
"""
hypothesis = LogisticRegression.sigmoid(np.dot(self.X, self.theta))
#new ndarray to prevent intercept from theta array to be changed
theta=np.delete(self.theta,0)
#regularization term
reg = (self.lamda/2*self.num_training)*np.sum(np.power(theta,2))
cost = -(np.sum(self.y * np.log(hypothesis) + (1 - self.y) * (np.log(1 - hypothesis)))) / self.num_training
#if regularization is true, add regularization term and return cost
if self.reg:
return cost + reg
return cost
def unpad(matrix):
'''
Strip off a column (e.g. of ones). Transform from:
array([[1., 2., 3., 1.],
[2., 3., 4., 1.],
[5., 6., 7., 1.]])
to:
array([[1., 2., 3.],
[2., 3., 4.],
[5., 6., 7.]])
'''
if matrix.ndim != 2 or matrix.shape[1] != 4:
raise ValueError("Invalid shape %s: unpad expects nx4" % (matrix.shape,))
if not all(matrix[:, 3] == 1.):
raise ValueError('Expected a column of ones')
return np.delete(matrix, 3, axis=1)
def BFS(self, start, fs=None):
'''
Returns the BFS tree for the graph starting from start
'''
to_be_processed = np.array([start], dtype=np.int)
known = np.array([], dtype=np.int)
tree = np.array([], dtype=object)
if fs is None:
fs = self.FSs
while len(to_be_processed) > 0:
# pop
current_node = to_be_processed[-1]
to_be_processed = np.delete(to_be_processed, -1)
for node in fs[current_node]:
if node not in known:
known = np.append(known, node)
tree = np.append(tree, None)
tree[-1] = (current_node, node)
# push
to_be_processed = np.insert(to_be_processed, 0, node)
return tree
def DFS(self, start, fs=None):
'''
Returns the DFS tree for the graph starting from start
'''
to_be_processed = np.array([start], dtype=np.int)
known = np.array([], dtype=np.int)
tree = np.array([], dtype=object)
if fs is None:
fs = self.FSs
while len(to_be_processed) > 0:
# pop
current_node = to_be_processed[0]
to_be_processed = np.delete(to_be_processed, 0)
for node in fs[current_node]:
if node not in known:
known = np.append(known, node)
tree = np.append(tree, None)
tree[-1] = (current_node, node)
# push
to_be_processed = np.insert(to_be_processed, 0, node)
return tree
def topological_sort(self):
'''
Returns a list topological sorted nodes
'''
if self.is_cyclic(self.FSs):
print 'cannot apply labels, graph contains cycles'
return
big_l = [] # Empty list that will contain the sorted elements
# Set of all nodes with no incoming edges
big_s = set([0])
bs_copy = self.BSs.copy()
while len(big_s) > 0:
n = big_s.pop()
big_l.append(n)
for m in self.FSs[n]:
bs_copy[m] = np.delete(bs_copy[m], np.where(bs_copy[m] == n))
# bs_copy[m].remove(n)
if len(bs_copy[m]) == 0:
big_s.add(int(m))
return big_l
def _mask_clip(self, row_or_col):
'''
Cuts out items from matrix that do not contain at least k values on axis=0
'''
mat = self.mat
k = self.k
lil = mat.tolil()
to_remove = []
for idx, i in enumerate(lil.rows):
if len(i) < k:
to_remove.append(idx)
lil.rows = np.delete(lil.rows, to_remove)
lil.data = np.delete(lil.data, to_remove)
if row_or_col == 'row':
self.row_idx = np.delete(range(lil.shape[0]), to_remove)
elif row_or_col == 'col':
self.col_idx = np.delete(range(lil.shape[0]), to_remove)
remaining = lil.shape[0] - len(to_remove)
lil = lil[:remaining]
self.mat = lil
return self
word.py 文件源码
项目:tensorflow_end2end_speech_recognition
作者: hirofumi0810
项目源码
文件源码
阅读 24
收藏 0
点赞 0
评论 0
def __call__(self, index_list, padded_value=-1):
"""
Args:
index_list (np.ndarray): list of word indices.
Batch size 1 is expected.
padded_value (int): the value used for padding
Returns:
word_list (list): list of words
"""
# Remove padded values
assert type(index_list) == np.ndarray, 'index_list should be np.ndarray.'
index_list = np.delete(index_list, np.where(index_list == -1), axis=0)
# Convert from indices to the corresponding words
word_list = list(map(lambda x: self.map_dict[x], index_list))
return word_list
phone.py 文件源码
项目:tensorflow_end2end_speech_recognition
作者: hirofumi0810
项目源码
文件源码
阅读 27
收藏 0
点赞 0
评论 0
def __call__(self, index_list, padded_value=-1):
"""
Args:
index_list (list): phone indices
padded_value (int): the value used for padding
Returns:
str_phone (string): a sequence of phones
"""
# Remove padded values
assert type(index_list) == np.ndarray, 'index_list should be np.ndarray.'
index_list = np.delete(index_list, np.where(index_list == -1), axis=0)
# Convert from indices to the corresponding phones
phone_list = list(map(lambda x: self.map_dict[x], index_list))
str_phone = ' '.join(phone_list)
return str_phone
def buildTree(self, data, features):
classification = data[:, -1]
uniqueValues = set(classification)
if len(uniqueValues) == 1:
return classification[0]
if len(data[0]) == 1:
return self.majorityCnt(classification)
infomatinoGain = InformationGain()
bestFeature = infomatinoGain.chooseBestFeatureToSplit(data)
bestFeatureLabel = features[bestFeature]
decisionTree = {bestFeatureLabel: {}}
featureValues = set(data[:, bestFeature])
tmpFeatures = np.delete(features, bestFeature, axis=0)
for value in featureValues:
subData = infomatinoGain.splitData(data, bestFeature, value)
decisionTree[bestFeatureLabel][value] = self.buildTree(subData, tmpFeatures)
return decisionTree
def append_neg_and_retrain(self, feat=None, force=False):
if feat is not None:
num = feat.shape[0]
self.neg = np.vstack((self.neg, feat))
self.num_neg_added += num
if self.num_neg_added > self.retrain_limit or force:
self.num_neg_added = 0
new_w_b, pos_scores, neg_scores = self.train()
# scores = np.dot(self.neg, new_w_b[0].T) + new_w_b[1]
# easy_inds = np.where(neg_scores < self.evict_thresh)[0]
not_easy_inds = np.where(neg_scores >= self.evict_thresh)[0]
if len(not_easy_inds) > 0:
self.neg = self.neg[not_easy_inds, :]
# self.neg = np.delete(self.neg, easy_inds)
print(' Pruning easy negatives')
print(' Cache holds {} pos examples and {} neg examples'.
format(self.pos.shape[0], self.neg.shape[0]))
print(' {} pos support vectors'.format((pos_scores <= 1).sum()))
print(' {} neg support vectors'.format((neg_scores >= -1).sum()))
return new_w_b
else:
return None
def BFS(self, start, fs=None):
'''
Returns the BFS tree for the graph starting from start
'''
to_be_processed = np.array([start], dtype=np.int)
known = np.array([], dtype=np.int)
tree = np.array([], dtype=object)
if fs is None:
fs = self.FSs
while len(to_be_processed) > 0:
# pop
current_node = to_be_processed[-1]
to_be_processed = np.delete(to_be_processed, -1)
for node in fs[current_node]:
if node not in known:
known = np.append(known, node)
tree = np.append(tree, None)
tree[-1] = (current_node, node)
# push
to_be_processed = np.insert(to_be_processed, 0, node)
return tree
def DFS(self, start, fs=None):
'''
Returns the DFS tree for the graph starting from start
'''
to_be_processed = np.array([start], dtype=np.int)
known = np.array([], dtype=np.int)
tree = np.array([], dtype=object)
if fs is None:
fs = self.FSs
while len(to_be_processed) > 0:
# pop
current_node = to_be_processed[0]
to_be_processed = np.delete(to_be_processed, 0)
for node in fs[current_node]:
if node not in known:
known = np.append(known, node)
tree = np.append(tree, None)
tree[-1] = (current_node, node)
# push
to_be_processed = np.insert(to_be_processed, 0, node)
return tree
def topological_sort(self):
'''
Returns a list topological sorted nodes
'''
if self.is_cyclic(self.FSs):
print 'cannot apply labels, graph contains cycles'
return
big_l = [] # Empty list that will contain the sorted elements
# Set of all nodes with no incoming edges
big_s = set([0])
bs_copy = self.BSs.copy()
while len(big_s) > 0:
n = big_s.pop()
big_l.append(n)
for m in self.FSs[n]:
bs_copy[m] = np.delete(bs_copy[m], np.where(bs_copy[m] == n))
# bs_copy[m].remove(n)
if len(bs_copy[m]) == 0:
big_s.add(int(m))
return big_l