本文整理汇总了Python中numpy.minimum方法的典型用法代码示例。如果您正苦于以下问题:Python numpy.minimum方法的具体用法?Python numpy.minimum怎么用?Python numpy.minimum使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类numpy
的用法示例。
在下文中一共展示了numpy.minimum方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: detect
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import minimum [as 别名]
def detect(self, img):
"""
img: rgb 3 channel
"""
minsize = 20 # minimum size of face
threshold = [0.6, 0.7, 0.7] # three steps's threshold
factor = 0.709 # scale factor
bounding_boxes, _ = FaceDet.detect_face(
img, minsize, self.pnet, self.rnet, self.onet, threshold, factor)
area = (bounding_boxes[:, 2] - bounding_boxes[:, 0]) * (bounding_boxes[:, 3] - bounding_boxes[:, 1])
face_idx = area.argmax()
bbox = bounding_boxes[face_idx][:4] # xy,xy
margin = 32
x0 = np.maximum(bbox[0] - margin // 2, 0)
y0 = np.maximum(bbox[1] - margin // 2, 0)
x1 = np.minimum(bbox[2] + margin // 2, img.shape[1])
y1 = np.minimum(bbox[3] + margin // 2, img.shape[0])
x0, y0, x1, y1 = bbox = [int(k + 0.5) for k in [x0, y0, x1, y1]]
cropped = img[y0:y1, x0:x1, :]
scaled = cv2.resize(cropped, (160, 160), interpolation=cv2.INTER_LINEAR)
return scaled, bbox
示例2: apply_perturbations
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import minimum [as 别名]
def apply_perturbations(i, j, X, increase, theta, clip_min, clip_max):
"""
TensorFlow implementation for apply perturbations to input features based
on salency maps
:param i: index of first selected feature
:param j: index of second selected feature
:param X: a matrix containing our input features for our sample
:param increase: boolean; true if we are increasing pixels, false otherwise
:param theta: delta for each feature adjustment
:param clip_min: mininum value for a feature in our sample
:param clip_max: maximum value for a feature in our sample
: return: a perturbed input feature matrix for a target class
"""
# perturb our input sample
if increase:
X[0, i] = np.minimum(clip_max, X[0, i] + theta)
X[0, j] = np.minimum(clip_max, X[0, j] + theta)
else:
X[0, i] = np.maximum(clip_min, X[0, i] - theta)
X[0, j] = np.maximum(clip_min, X[0, j] - theta)
return X
示例3: _prepro_cpg
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import minimum [as 别名]
def _prepro_cpg(self, states, dists):
"""Preprocess the state and distance of neighboring CpG sites."""
prepro_states = []
prepro_dists = []
for state, dist in zip(states, dists):
nan = state == dat.CPG_NAN
if np.any(nan):
state[nan] = np.random.binomial(1, state[~nan].mean(),
nan.sum())
dist[nan] = self.cpg_max_dist
dist = np.minimum(dist, self.cpg_max_dist) / self.cpg_max_dist
prepro_states.append(np.expand_dims(state, 1))
prepro_dists.append(np.expand_dims(dist, 1))
prepro_states = np.concatenate(prepro_states, axis=1)
prepro_dists = np.concatenate(prepro_dists, axis=1)
if self.cpg_wlen:
center = prepro_states.shape[2] // 2
delta = self.cpg_wlen // 2
tmp = slice(center - delta, center + delta)
prepro_states = prepro_states[:, :, tmp]
prepro_dists = prepro_dists[:, :, tmp]
return (prepro_states, prepro_dists)
示例4: clip_boxes
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import minimum [as 别名]
def clip_boxes(boxes, im_shape):
"""
Clip boxes to image boundaries.
:param boxes: [N, 4* num_classes]
:param im_shape: tuple of 2
:return: [N, 4* num_classes]
"""
# x1 >= 0
boxes[:, 0::4] = np.maximum(np.minimum(boxes[:, 0::4], im_shape[1] - 1), 0)
# y1 >= 0
boxes[:, 1::4] = np.maximum(np.minimum(boxes[:, 1::4], im_shape[0] - 1), 0)
# x2 < im_shape[1]
boxes[:, 2::4] = np.maximum(np.minimum(boxes[:, 2::4], im_shape[1] - 1), 0)
# y2 < im_shape[0]
boxes[:, 3::4] = np.maximum(np.minimum(boxes[:, 3::4], im_shape[0] - 1), 0)
return boxes
示例5: _update_labels
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import minimum [as 别名]
def _update_labels(self, label, crop_box, height, width):
"""Convert labels according to crop box"""
xmin = float(crop_box[0]) / width
ymin = float(crop_box[1]) / height
w = float(crop_box[2]) / width
h = float(crop_box[3]) / height
out = label.copy()
out[:, (1, 3)] -= xmin
out[:, (2, 4)] -= ymin
out[:, (1, 3)] /= w
out[:, (2, 4)] /= h
out[:, 1:5] = np.maximum(0, out[:, 1:5])
out[:, 1:5] = np.minimum(1, out[:, 1:5])
coverage = self._calculate_areas(out[:, 1:]) * w * h / self._calculate_areas(label[:, 1:])
valid = np.logical_and(out[:, 3] > out[:, 1], out[:, 4] > out[:, 2])
valid = np.logical_and(valid, coverage > self.min_eject_coverage)
valid = np.where(valid)[0]
if valid.size < 1:
return None
out = out[valid, :]
return out
示例6: test_quantize_float32_to_int8
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import minimum [as 别名]
def test_quantize_float32_to_int8():
shape = rand_shape_nd(4)
data = rand_ndarray(shape, 'default', dtype='float32')
min_range = mx.nd.min(data)
max_range = mx.nd.max(data)
qdata, min_val, max_val = mx.nd.contrib.quantize(data, min_range, max_range, out_type='int8')
data_np = data.asnumpy()
min_range = min_range.asscalar()
max_range = max_range.asscalar()
real_range = np.maximum(np.abs(min_range), np.abs(max_range))
quantized_range = 127.0
scale = quantized_range / real_range
assert qdata.dtype == np.int8
assert min_val.dtype == np.float32
assert max_val.dtype == np.float32
assert same(min_val.asscalar(), -real_range)
assert same(max_val.asscalar(), real_range)
qdata_np = (np.sign(data_np) * np.minimum(np.abs(data_np) * scale + 0.5, quantized_range)).astype(np.int8)
assert_almost_equal(qdata.asnumpy(), qdata_np, atol = 1)
示例7: heuristic_fn_vec
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import minimum [as 别名]
def heuristic_fn_vec(n1, n2, n_ori, step_size):
# n1 is a vector and n2 is a single point.
dx = (n1[:,0] - n2[0,0])/step_size
dy = (n1[:,1] - n2[0,1])/step_size
dt = n1[:,2] - n2[0,2]
dt = np.mod(dt, n_ori)
dt = np.minimum(dt, n_ori-dt)
if n_ori == 6:
if dx*dy > 0:
d = np.maximum(np.abs(dx), np.abs(dy))
else:
d = np.abs(dy-dx)
elif n_ori == 4:
d = np.abs(dx) + np.abs(dy)
return (d + dt).reshape((-1,1))
示例8: resize_maps
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import minimum [as 别名]
def resize_maps(map, map_scales, resize_method):
scaled_maps = []
for i, sc in enumerate(map_scales):
if resize_method == 'antialiasing':
# Resize using open cv so that we can compute the size.
# Use PIL resize to use anti aliasing feature.
map_ = cv2.resize(map*1, None, None, fx=sc, fy=sc, interpolation=cv2.INTER_LINEAR)
w = map_.shape[1]; h = map_.shape[0]
map_img = PIL.Image.fromarray((map*255).astype(np.uint8))
map__img = map_img.resize((w,h), PIL.Image.ANTIALIAS)
map_ = np.asarray(map__img).astype(np.float32)
map_ = map_/255.
map_ = np.minimum(map_, 1.0)
map_ = np.maximum(map_, 0.0)
elif resize_method == 'linear_noantialiasing':
map_ = cv2.resize(map*1, None, None, fx=sc, fy=sc, interpolation=cv2.INTER_LINEAR)
else:
logging.error('Unknown resizing method')
scaled_maps.append(map_)
return scaled_maps
示例9: raw_valid_fn_vec
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import minimum [as 别名]
def raw_valid_fn_vec(self, xyt):
"""Returns if the given set of nodes is valid or not."""
height = self.traversible.shape[0]
width = self.traversible.shape[1]
x = np.round(xyt[:,[0]]).astype(np.int32)
y = np.round(xyt[:,[1]]).astype(np.int32)
is_inside = np.all(np.concatenate((x >= 0, y >= 0,
x < width, y < height), axis=1), axis=1)
x = np.minimum(np.maximum(x, 0), width-1)
y = np.minimum(np.maximum(y, 0), height-1)
ind = np.ravel_multi_index((y,x), self.traversible.shape)
is_traversible = self.traversible.ravel()[ind]
is_valid = np.all(np.concatenate((is_inside[:,np.newaxis], is_traversible),
axis=1), axis=1)
return is_valid
示例10: intersection
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import minimum [as 别名]
def intersection(boxes1, boxes2):
"""Compute pairwise intersection areas between boxes.
Args:
boxes1: a numpy array with shape [N, 4] holding N boxes
boxes2: a numpy array with shape [M, 4] holding M boxes
Returns:
a numpy array with shape [N*M] representing pairwise intersection area
"""
[y_min1, x_min1, y_max1, x_max1] = np.split(boxes1, 4, axis=1)
[y_min2, x_min2, y_max2, x_max2] = np.split(boxes2, 4, axis=1)
all_pairs_min_ymax = np.minimum(y_max1, np.transpose(y_max2))
all_pairs_max_ymin = np.maximum(y_min1, np.transpose(y_min2))
intersect_heights = np.maximum(
np.zeros(all_pairs_max_ymin.shape),
all_pairs_min_ymax - all_pairs_max_ymin)
all_pairs_min_xmax = np.minimum(x_max1, np.transpose(x_max2))
all_pairs_max_xmin = np.maximum(x_min1, np.transpose(x_min2))
intersect_widths = np.maximum(
np.zeros(all_pairs_max_xmin.shape),
all_pairs_min_xmax - all_pairs_max_xmin)
return intersect_heights * intersect_widths
示例11: create_random_boxes
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import minimum [as 别名]
def create_random_boxes(num_boxes, max_height, max_width):
"""Creates random bounding boxes of specific maximum height and width.
Args:
num_boxes: number of boxes.
max_height: maximum height of boxes.
max_width: maximum width of boxes.
Returns:
boxes: numpy array of shape [num_boxes, 4]. Each row is in form
[y_min, x_min, y_max, x_max].
"""
y_1 = np.random.uniform(size=(1, num_boxes)) * max_height
y_2 = np.random.uniform(size=(1, num_boxes)) * max_height
x_1 = np.random.uniform(size=(1, num_boxes)) * max_width
x_2 = np.random.uniform(size=(1, num_boxes)) * max_width
boxes = np.zeros(shape=(num_boxes, 4))
boxes[:, 0] = np.minimum(y_1, y_2)
boxes[:, 1] = np.minimum(x_1, x_2)
boxes[:, 2] = np.maximum(y_1, y_2)
boxes[:, 3] = np.maximum(x_1, x_2)
return boxes.astype(np.float32)
示例12: testMultilabelMatch3
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import minimum [as 别名]
def testMultilabelMatch3(self):
predictions = np.random.randint(1, 5, size=(100, 1, 1, 1))
targets = np.random.randint(1, 5, size=(100, 10, 1, 1))
weights = np.random.randint(0, 2, size=(100, 1, 1, 1))
targets *= weights
predictions_repeat = np.repeat(predictions, 10, axis=1)
expected = (predictions_repeat == targets).astype(float)
expected = np.sum(expected, axis=(1, 2, 3))
expected = np.minimum(expected / 3.0, 1.)
expected = np.sum(expected * weights[:, 0, 0, 0]) / weights.shape[0]
with self.test_session() as session:
scores, weights_ = metrics.multilabel_accuracy_match3(
tf.one_hot(predictions, depth=5, dtype=tf.float32),
tf.constant(targets, dtype=tf.int32))
a, a_op = tf.metrics.mean(scores, weights_)
session.run(tf.local_variables_initializer())
session.run(tf.global_variables_initializer())
_ = session.run(a_op)
actual = session.run(a)
self.assertAlmostEqual(actual, expected, places=6)
示例13: vis_det_and_mask
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import minimum [as 别名]
def vis_det_and_mask(im, class_name, dets, masks, thresh=0.8):
"""Visual debugging of detections."""
num_dets = np.minimum(10, dets.shape[0])
colors_mask = random_colors(num_dets)
colors_bbox = np.round(np.random.rand(num_dets, 3) * 255)
# sort rois according to the coordinates, draw upper bbox first
draw_mask = np.zeros(im.shape[:2], dtype=np.uint8)
for i in range(1):
bbox = tuple(int(np.round(x)) for x in dets[i, :4])
mask = masks[i, :, :]
full_mask = unmold_mask(mask, bbox, im.shape)
score = dets[i, -1]
if score > thresh:
word_width = len(class_name)
cv2.rectangle(im, bbox[0:2], bbox[2:4], colors_bbox[i], 2)
cv2.rectangle(im, bbox[0:2], (bbox[0] + 18 + word_width*8, bbox[1]+15), colors_bbox[i], thickness=cv2.FILLED)
apply_mask(im, full_mask, draw_mask, colors_mask[i], 0.5)
draw_mask += full_mask
cv2.putText(im, '%s' % (class_name), (bbox[0]+5, bbox[1] + 12), cv2.FONT_HERSHEY_PLAIN,
1.0, (255,255,255), thickness=1)
return im
示例14: test_deficit_parameter
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import minimum [as 别名]
def test_deficit_parameter():
"""Test DeficitParameter
Here we test both uses of the DeficitParameter:
1) Recording the deficit for a node each timestep
2) Using yesterday's deficit to control today's flow
"""
model = load_model("deficit.json")
model.run()
max_flow = np.array([5, 6, 7, 8, 9, 10, 11, 12, 11, 10, 9, 8])
demand = 10.0
supplied = np.minimum(max_flow, demand)
expected = demand - supplied
actual = model.recorders["deficit_recorder"].data
assert_allclose(expected, actual[:,0])
expected_yesterday = [0]+list(expected[0:-1])
actual_yesterday = model.recorders["yesterday_recorder"].data
assert_allclose(expected_yesterday, actual_yesterday[:,0])
示例15: test_flow_parameter
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import minimum [as 别名]
def test_flow_parameter():
"""test FlowParameter
"""
model = load_model("flow_parameter.json")
model.run()
max_flow = np.array([5, 6, 7, 8, 9, 10, 11, 12, 11, 10, 9, 8])
demand = 10.0
supplied = np.minimum(max_flow, demand)
actual = model.recorders["flow_recorder"].data
assert_allclose(supplied, actual[:,0])
expected_yesterday = [3.1415]+list(supplied[0:-1])
actual_yesterday = model.recorders["yesterday_flow_recorder"].data
assert_allclose(expected_yesterday, actual_yesterday[:,0])