def visualize(self, vis, colored=True):
try:
tids = set(self.ids)
except:
return vis
for hid, hbox in izip(self.ids, self.bboxes):
cv2.rectangle(vis, (hbox[0], hbox[1]), (hbox[2], hbox[3]), (0,255,0), 1)
vis = super(BoundingBoxKLT, self).viz(vis, colored=colored)
# for tid, pts in self.tm_.tracks.iteritems():
# if tid not in tids: continue
# cv2.polylines(vis, [np.vstack(pts.items).astype(np.int32)[-4:]], False,
# (0,255,0), thickness=1)
# tl, br = np.int32(pts.latest_item)-2, np.int32(pts.latest_item)+2
# cv2.rectangle(vis, (tl[0], tl[1]), (br[0], br[1]), (0,255,0), -1)
# OpenCVKLT.draw_tracks(self, vis, colored=colored, max_track_length=10)
return vis
python类int32()的实例源码
def draw_poly_box(frame, pts, color=[0, 255, 0]):
"""Draw polylines bounding box.
Parameters
----------
frame : OpenCV Mat
A given frame with an object
pts : numpy array
consists of bounding box information with size (n points, 2)
color : list
color of the bounding box, the default is green
Returns
-------
new_frame : OpenCV Mat
A frame with given bounding box.
"""
new_frame = frame.copy()
temp_pts = np.array(pts, np.int32)
temp_pts = temp_pts.reshape((-1, 1, 2))
cv2.polylines(new_frame, [temp_pts], True, color, thickness=2)
return new_frame
def sparse_tuple_from(sequences, dtype=np.int32):
r"""Creates a sparse representention of ``sequences``.
Args:
* sequences: a list of lists of type dtype where each element is a sequence
Returns a tuple with (indices, values, shape)
"""
indices = []
values = []
for n, seq in enumerate(sequences):
indices.extend(zip([n]*len(seq), range(len(seq))))
values.extend(seq)
indices = np.asarray(indices, dtype=np.int64)
values = np.asarray(values, dtype=dtype)
shape = np.asarray([len(sequences), indices.max(0)[1]+1], dtype=np.int64)
return tf.SparseTensor(indices=indices, values=values, shape=shape)
def __init__(self, filename, target_map, classifier='svm'):
self.seed_ = 0
self.filename_ = filename
self.target_map_ = target_map
self.target_ids_ = (np.unique(target_map.keys())).astype(np.int32)
self.epoch_no_ = 0
self.st_time_ = time.time()
# Setup classifier
print('-------------------------------')
print('====> Building Classifier, setting class weights')
if classifier == 'svm':
self.clf_hyparams_ = {'C':[0.01, 0.1, 1.0, 10.0, 100.0], 'class_weight': ['balanced']}
self.clf_base_ = LinearSVC(random_state=self.seed_)
elif classifier == 'sgd':
self.clf_hyparams_ = {'alpha':[0.0001, 0.001, 0.01, 0.1, 1.0, 10.0], 'class_weight':['auto']} # 'loss':['hinge'],
self.clf_ = SGDClassifier(loss='log', penalty='l2', shuffle=False, random_state=self.seed_,
warm_start=True, n_jobs=-1, n_iter=1, verbose=4)
else:
raise Exception('Unknown classifier type %s. Choose from [sgd, svm, gradient-boosting, extra-trees]'
% classifier)
def draw_hulls(im, hulls):
assert(isinstance(hulls, list))
cv2.polylines(im, map(lambda hull: hull.astype(np.int32), hulls), 1, (0, 255, 0) if im.ndim == 3 else 255, thickness=1)
return im
def draw_tracks(self, out, colored=False, color_type='unique', min_track_length=4, max_track_length=4):
"""
color_type: {age, unique}
"""
N = 20
# inds = self.confident_tracks(min_length=min_track_length)
# if not len(inds):
# return
# ids, pts = self.latest_ids[inds], self.latest_pts[inds]
# lengths = self.tm_.lengths[inds]
ids, pts, lengths = self.latest_ids, self.latest_pts, self.tm_.lengths
if color_type == 'unique':
cwheel = colormap(np.linspace(0, 1, N))
cols = np.vstack([cwheel[tid % N] for idx, tid in enumerate(ids)])
elif color_type == 'age':
cols = colormap(lengths)
else:
raise ValueError('Color type {:} undefined, use age or unique'.format(color_type))
if not colored:
cols = np.tile([0,240,0], [len(self.tm_.tracks), 1])
for col, pts in izip(cols.astype(np.int64), self.tm_.tracks.itervalues()):
cv2.polylines(out, [np.vstack(pts.items).astype(np.int32)[-max_track_length:]], False,
tuple(col), thickness=1)
tl, br = np.int32(pts.latest_item)-2, np.int32(pts.latest_item)+2
cv2.rectangle(out, (tl[0], tl[1]), (br[0], br[1]), tuple(col), -1)
cpm_utils.py 文件源码
项目:convolutional-pose-machines-tensorflow
作者: timctho
项目源码
文件源码
阅读 38
收藏 0
点赞 0
评论 0
def warpImage(src, theta, phi, gamma, scale, fovy):
halfFovy = fovy * 0.5
d = math.hypot(src.shape[1], src.shape[0])
sideLength = scale * d / math.cos(deg2Rad(halfFovy))
sideLength = np.int32(sideLength)
M = warpMatrix(src.shape[1], src.shape[0], theta, phi, gamma, scale, fovy)
dst = cv2.warpPerspective(src, M, (sideLength, sideLength))
mid_x = mid_y = dst.shape[0] // 2
target_x = target_y = src.shape[0] // 2
offset = (target_x % 2)
if len(dst.shape) == 3:
dst = dst[mid_y - target_y:mid_y + target_y + offset,
mid_x - target_x:mid_x + target_x + offset,
:]
else:
dst = dst[mid_y - target_y:mid_y + target_y + offset,
mid_x - target_x:mid_x + target_x + offset]
return dst
def _load_data_graph(self):
"""
Loads the data graph consisting of the encoder and decoder input placeholders, Label (Target tip summary)
placeholders and the weights of the hidden layer of the Seq2Seq model.
:return: None
"""
# input
with tf.variable_scope("train_test", reuse=True):
# review input - Both original and reversed
self.enc_inp_fwd = [tf.placeholder(tf.int32, shape=(None,), name="input%i" % t)
for t in range(self.seq_length)]
self.enc_inp_bwd = [tf.placeholder(tf.int32, shape=(None,), name="input%i" % t)
for t in range(self.seq_length)]
# desired output
self.labels = [tf.placeholder(tf.int32, shape=(None,), name="labels%i" % t)
for t in range(self.seq_length)]
# weight of the hidden layer
self.weights = [tf.ones_like(labels_t, dtype=tf.float32)
for labels_t in self.labels]
# Decoder input: prepend some "GO" token and drop the final
# token of the encoder input
self.dec_inp = ([tf.zeros_like(self.labels[0], dtype=np.int32, name="GO")] + self.labels[:-1])
def _load_data_graph(self):
"""
Loads the data graph consisting of the encoder and decoder input placeholders, Label (Target tip summary)
placeholders and the weights of the hidden layer of the Seq2Seq model.
:return: None
"""
# input
with tf.variable_scope("train_test", reuse=True):
self.enc_inp = [tf.placeholder(tf.int32, shape=(None,),
name="input%i" % t)
for t in range(self.seq_length)]
# desired output
self.labels = [tf.placeholder(tf.int32, shape=(None,),
name="labels%i" % t)
for t in range(self.seq_length)]
# weight of the hidden layer
self.weights = [tf.ones_like(labels_t, dtype=tf.float32)
for labels_t in self.labels]
# Decoder input: prepend some "GO" token and drop the final
# token of the encoder input
self.dec_inp = ([tf.zeros_like(self.labels[0], dtype=np.int32, name="GO")]
+ self.labels[:-1])
def _load_data_graph(self):
"""
Loads the data graph consisting of the encoder and decoder input placeholders, Label (Target tip summary)
placeholders and the weights of the hidden layer of the Seq2Seq model.
:return: None
"""
# input
with tf.variable_scope("train_test", reuse=True):
self.enc_inp = [tf.placeholder(tf.int32, shape=(None,), name="input%i" % t)
for t in range(self.seq_length)]
# desired output
self.labels = [tf.placeholder(tf.int32, shape=(None,), name="labels%i" % t)
for t in range(self.seq_length)]
# weight of the hidden layer
self.weights = [tf.ones_like(labels_t, dtype=tf.float32)
for labels_t in self.labels]
# Decoder input: prepend some "GO" token and drop the final
# token of the encoder input
self.dec_inp = ([tf.zeros_like(self.labels[0], dtype=np.int32, name="GO")] + self.labels[:-1])
def _load_data_graph(self):
"""
Loads the data graph consisting of the encoder and decoder input placeholders, Label (Target tip summary)
placeholders and the weights of the hidden layer of the Seq2Seq model.
:return: None
"""
# input
with tf.variable_scope("train_test", reuse=True):
# review input - Both original and reversed
self.enc_inp_fwd = [tf.placeholder(tf.int32, shape=(None,), name="input%i" % t)
for t in range(self.seq_length)]
self.enc_inp_bwd = [tf.placeholder(tf.int32, shape=(None,), name="input%i" % t)
for t in range(self.seq_length)]
# desired output
self.labels = [tf.placeholder(tf.int32, shape=(None,), name="labels%i" % t)
for t in range(self.seq_length)]
# weight of the hidden layer
self.weights = [tf.ones_like(labels_t, dtype=tf.float32)
for labels_t in self.labels]
# Decoder input: prepend some "GO" token and drop the final
# token of the encoder input
self.dec_inp = ([tf.zeros_like(self.labels[0], dtype=np.int32, name="GO")] + self.labels[:-1])
def sparse_tuple_from(sequences, dtype=np.int32):
r"""Creates a sparse representention of ``sequences``.
Args:
* sequences: a list of lists of type dtype where each element is a sequence
Returns a tuple with (indices, values, shape)
"""
indices = []
values = []
for n, seq in enumerate(sequences):
indices.extend(zip([n]*len(seq), range(len(seq))))
values.extend(seq)
indices = np.asarray(indices, dtype=np.int64)
values = np.asarray(values, dtype=dtype)
shape = np.asarray([len(sequences), indices.max(0)[1]+1], dtype=np.int64)
return tf.SparseTensor(indices=indices, values=values, shape=shape)
def _write_binary_i_32(
task_handle, write_array, num_samps_per_chan, auto_start, timeout,
data_layout=FillMode.GROUP_BY_CHANNEL):
samps_per_chan_written = ctypes.c_int()
cfunc = lib_importer.windll.DAQmxWriteBinaryI32
if cfunc.argtypes is None:
with cfunc.arglock:
if cfunc.argtypes is None:
cfunc.argtypes = [
lib_importer.task_handle, ctypes.c_int, c_bool32,
ctypes.c_double, ctypes.c_int,
wrapped_ndpointer(dtype=numpy.int32, flags=('C', 'W')),
ctypes.POINTER(ctypes.c_int), ctypes.POINTER(c_bool32)]
error_code = cfunc(
task_handle, num_samps_per_chan, auto_start, timeout,
data_layout.value, write_array,
ctypes.byref(samps_per_chan_written), None)
check_for_error(error_code)
return samps_per_chan_written.value
def _read_binary_i_32(
task_handle, read_array, num_samps_per_chan, timeout,
fill_mode=FillMode.GROUP_BY_CHANNEL):
samps_per_chan_read = ctypes.c_int()
cfunc = lib_importer.windll.DAQmxReadBinaryI32
if cfunc.argtypes is None:
with cfunc.arglock:
if cfunc.argtypes is None:
cfunc.argtypes = [
lib_importer.task_handle, ctypes.c_int, ctypes.c_double,
ctypes.c_int,
wrapped_ndpointer(dtype=numpy.int32, flags=('C', 'W')),
ctypes.c_uint, ctypes.POINTER(ctypes.c_int),
ctypes.POINTER(c_bool32)]
error_code = cfunc(
task_handle, num_samps_per_chan, timeout, fill_mode.value,
read_array, numpy.prod(read_array.shape),
ctypes.byref(samps_per_chan_read), None)
check_for_error(error_code)
return samps_per_chan_read.value
def make_complete_graph(num_vertices):
"""Constructs a complete graph.
The pairing function is: k = v1 + v2 * (v2 - 1) // 2
Args:
num_vertices: Number of vertices.
Returns: A tuple with elements:
V: Number of vertices.
K: Number of edges.
grid: a 3 x K grid of (edge, vertex, vertex) triples.
"""
V = num_vertices
K = V * (V - 1) // 2
grid = np.zeros([3, K], np.int32)
k = 0
for v2 in range(V):
for v1 in range(v2):
grid[:, k] = [k, v1, v2]
k += 1
return grid
def quantize_from_probs2(probs, resolution):
"""Quantize multiple non-normalized probs to given resolution.
Args:
probs: An [N, M]-shaped numpy array of non-normalized probabilities.
Returns:
An [N, M]-shaped array of quantized probabilities such that
np.all(result.sum(axis=1) == resolution).
"""
assert len(probs.shape) == 2
N, M = probs.shape
probs = probs / probs.sum(axis=1, keepdims=True)
result = np.zeros(probs.shape, np.int8)
range_N = np.arange(N, dtype=np.int32)
for _ in range(resolution):
sample = probs.argmax(axis=1)
result[range_N, sample] += 1
probs[range_N, sample] -= 1.0 / resolution
return result
def make_ragged_index(columns):
"""Make an index to hold data in a ragged array.
Args:
columns: A list of [N, _]-shaped numpy arrays of varying size, where
N is the number of rows.
Returns:
A [len(columns)+1]-shaped array of begin,end positions of each column.
"""
ragged_index = np.zeros([len(columns) + 1], dtype=np.int32)
ragged_index[0] = 0
for v, column in enumerate(columns):
ragged_index[v + 1] = ragged_index[v] + column.shape[-1]
ragged_index.flags.writeable = False
return ragged_index
def get_batch_data():
# Load data
X, Y = load_data()
# calc total batch count
num_batch = len(X) // hp.batch_size
# Convert to tensor
X = tf.convert_to_tensor(X, tf.int32)
Y = tf.convert_to_tensor(Y, tf.float32)
# Create Queues
input_queues = tf.train.slice_input_producer([X, Y])
# create batch queues
x, y = tf.train.batch(input_queues,
num_threads=8,
batch_size=hp.batch_size,
capacity=hp.batch_size * 64,
allow_smaller_final_batch=False)
return x, y, num_batch # (N, T), (N, T), ()
def _get_dtype_maps():
""" Get dictionaries to map numpy data types to ITK types and the
other way around.
"""
# Define pairs
tmp = [ (np.float32, 'MET_FLOAT'), (np.float64, 'MET_DOUBLE'),
(np.uint8, 'MET_UCHAR'), (np.int8, 'MET_CHAR'),
(np.uint16, 'MET_USHORT'), (np.int16, 'MET_SHORT'),
(np.uint32, 'MET_UINT'), (np.int32, 'MET_INT'),
(np.uint64, 'MET_ULONG'), (np.int64, 'MET_LONG') ]
# Create dictionaries
map1, map2 = {}, {}
for np_type, itk_type in tmp:
map1[np_type.__name__] = itk_type
map2[itk_type] = np_type.__name__
# Done
return map1, map2
def reg2bin_vector(begin, end):
'''Vectorized tabix reg2bin -- much faster than reg2bin'''
result = np.zeros(begin.shape)
# Entries filled
done = np.zeros(begin.shape, dtype=np.bool)
for (bits, bins) in rev_bit_bins:
begin_shift = begin >> bits
new_done = (begin >> bits) == (end >> bits)
mask = np.logical_and(new_done, np.logical_not(done))
offset = ((1 << (29 - bits)) - 1) / 7
result[mask] = offset + begin_shift[mask]
done = new_done
return result.astype(np.int32)
def get_depth_info(read_iter, chrom, cstart, cend):
depths = np.zeros(cend-cstart, np.int32)
for read in read_iter:
pos = read.pos
rstart = max(pos, cstart)
# Increment to the end of the window or the end of the
# alignment, whichever comes first
rend = min(read.aend, cend)
depths[(rstart-cstart):(rend-cstart)] += 1
positions = np.arange(cstart, cend, dtype=np.int32)
depth_df = pd.DataFrame({"chrom": chrom, "pos": positions, "coverage": depths})
return depth_df
def getDataRecorderConfiguration(self):
nRecorders= self.getNumberOfRecorderTables()
sourceBufSize= 256
source= ctypes.create_string_buffer('\000', sourceBufSize)
option= CIntArray(np.zeros(nRecorders, dtype=np.int32))
table=CIntArray(np.arange(1, nRecorders + 1))
self._lib.PI_qDRC.argtypes= [c_int, CIntArray, c_char_p,
CIntArray, c_int, c_int]
self._convertErrorToException(
self._lib.PI_qDRC(self._id, table, source,
option, sourceBufSize, nRecorders))
sources= [x.strip() for x in source.value.split('\n')]
cfg= DataRecorderConfiguration()
for i in range(nRecorders):
cfg.setTable(table.toNumpyArray()[i],
sources[i],
option.toNumpyArray()[i])
return cfg
def read_flow(path, filename):
flowdata = None
with open(path + filename + '.flo') as f:
# Valid .flo file checker
magic = np.fromfile(f, np.float32, count=1)
if 202021.25 != magic:
print 'Magic number incorrect. Invalid .flo file'
else:
# Reshape data into 3D array (columns, rows, bands)
w = int(np.fromfile(f, np.int32, count=1))
h = int(np.fromfile(f, np.int32, count=1))
#print 'Reading {}.flo with shape: ({}, {}, 2)'.format(filename, h, w)
flowdata = np.fromfile(f, np.float32, count=2*w*h)
# NOTE: numpy shape(h, w, ch) is opposite to image shape(w, h, ch)
flowdata = np.reshape(flowdata, (h, w, 2))
return flowdata
RankOrderedAutoencoder.py 文件源码
项目:rank-ordered-autoencoder
作者: paulbertens
项目源码
文件源码
阅读 30
收藏 0
点赞 0
评论 0
def __init__(self, input_shape, output_shape):
self.input_shape = input_shape
self.input = np.zeros((output_shape[0], self.input_shape[0] * self.input_shape[1] *
self.input_shape[2]),dtype=np.float32)
self.output = np.zeros(output_shape, dtype=np.float32)
self.output_raw = np.zeros_like(self.output)
self.output_error = np.zeros_like(self.output)
self.output_average = np.zeros(self.output.shape[1], dtype=np.float32)
self.weights = np.random.normal(0, np.sqrt(2.0 / (self.output.shape[1] + self.input.shape[1])),
size=(self.input.shape[1], self.output.shape[1])).astype(np.float32)
self.gradient = np.zeros_like(self.weights)
self.reconstruction = np.zeros_like(self.weights)
self.errors = np.zeros_like(self.weights)
self.output_ranks = np.zeros(self.output.shape[1], dtype=np.int32)
self.learning_rate = 1
self.norm_limit = 0.1
def load_data(infile, chroms, resolutions):
starts = infile['starts'][...]
chromosomes = infile['chromosomes'][...]
data = {}
for res in resolutions:
data[res] = {}
for i, chrom in enumerate(chromosomes):
if chrom not in chroms:
continue
start = (starts[i] / res) * res
dist = infile['dist.%s.%i' % (chrom, res)][...]
valid_rows = infile['valid.%s.%i' % (chrom, res)][...]
corr = infile['corr.%s.%i' % (chrom, res)][...]
valid = numpy.zeros(corr.shape, dtype=numpy.bool)
N, M = corr.shape
valid = numpy.zeros((N, M), dtype=numpy.int32)
for i in range(min(N - 1, M)):
P = N - i - 1
valid[:P, i] = valid_rows[(i + 1):] * valid_rows[:P]
temp = corr * dist
valid[numpy.where(numpy.abs(temp) == numpy.inf)] = False
data[res][chrom] = [start, temp, valid]
return data
def load_data(infile, chroms, resolutions):
starts = infile['starts'][...]
chromosomes = infile['chromosomes'][...]
data = {}
for res in resolutions:
data[res] = {}
for i, chrom in enumerate(chromosomes):
if chrom not in chroms:
continue
start = (starts[i] / res) * res
dist = infile['dist.%s.%i' % (chrom, res)][...]
valid_rows = infile['valid.%s.%i' % (chrom, res)][...]
corr = infile['corr.%s.%i' % (chrom, res)][...]
valid = numpy.zeros(corr.shape, dtype=numpy.bool)
N, M = corr.shape
valid = numpy.zeros((N, M), dtype=numpy.int32)
for i in range(min(N - 1, M)):
P = N - i - 1
valid[:P, i] = valid_rows[(i + 1):] * valid_rows[:P]
temp = corr * dist
valid[numpy.where(numpy.abs(temp) == numpy.inf)] = False
data[res][chrom] = [start, temp, valid]
return data
def __linear_quantize(data, q_levels):
"""
floats in (0, 1) to ints in [0, q_levels-1]
scales normalized across axis 1
"""
# Normalization is on mini-batch not whole file
#eps = numpy.float64(1e-5)
#data -= data.min(axis=1)[:, None]
#data *= ((q_levels - eps) / data.max(axis=1)[:, None])
#data += eps/2
#data = data.astype('int32')
eps = numpy.float64(1e-5)
data *= (q_levels - eps)
data += eps/2
data = data.astype('int32')
return data
def generate_anchors_pre(height, width, feat_stride, anchor_scales=(8,16,32), anchor_ratios=(0.5,1,2)):
""" A wrapper function to generate anchors given different scales
Also return the number of anchors in variable 'length'
"""
anchors = generate_anchors(ratios=np.array(anchor_ratios), scales=np.array(anchor_scales))
A = anchors.shape[0]
shift_x = np.arange(0, width) * feat_stride
shift_y = np.arange(0, height) * feat_stride
shift_x, shift_y = np.meshgrid(shift_x, shift_y)
shifts = np.vstack((shift_x.ravel(), shift_y.ravel(), shift_x.ravel(), shift_y.ravel())).transpose()
K = shifts.shape[0]
# width changes faster, so here it is H, W, C
anchors = anchors.reshape((1, A, 4)) + shifts.reshape((1, K, 4)).transpose((1, 0, 2))
anchors = anchors.reshape((K * A, 4)).astype(np.float32, copy=False)
length = np.int32(anchors.shape[0])
return anchors, length
def test_repeat(self):
""" Test if `repeat` works the same as np.repeat."""
with tf.Session().as_default():
# try different tensor types
for npdtype, tfdtype in [(np.int32, tf.int32), (np.float32, tf.float32)]:
for init_value in [np.array([0, 1, 2, 3], dtype=npdtype),
np.array([[0, 1], [2, 3], [4, 5]], dtype=npdtype)]:
# and all their axes
for axis in range(len(init_value.shape)):
for repeats in [1, 2, 3, 11]:
tensor = tf.constant(init_value, dtype=tfdtype)
repeated_value = repeat(tensor, repeats=repeats, axis=axis).eval()
expected_value = np.repeat(init_value, repeats=repeats, axis=axis)
self.assertTrue(np.all(repeated_value == expected_value))
def __init__(self, target, instance, files):
self.target = target
self.instance = instance
mask_files = natural_sort(filter(lambda fn: '_maskcrop.png' in fn, files))
depth_files = natural_sort(filter(lambda fn: '_depthcrop.png' in fn, files))
rgb_files = natural_sort(list(set(files) - set(mask_files) - set(depth_files)))
loc_files = natural_sort(map(lambda fn: fn.replace('_crop.png', '_loc.txt'), rgb_files))
# Ensure all have equal number of files (Hack! doesn't ensure filename consistency)
nfiles = np.min([len(loc_files), len(mask_files), len(depth_files), len(rgb_files)])
mask_files, depth_files, rgb_files, loc_files = mask_files[:nfiles], depth_files[:nfiles], \
rgb_files[:nfiles], loc_files[:nfiles]
# print target, instance, len(loc_files), len(mask_files), len(depth_files), len(rgb_files)
assert(len(mask_files) == len(depth_files) == len(rgb_files) == len(loc_files))
# Read images
self.rgb = ImageDatasetReader.from_filenames(rgb_files)
self.depth = ImageDatasetReader.from_filenames(depth_files)
self.mask = ImageDatasetReader.from_filenames(mask_files)
# Read top-left locations of bounding box
self.locations = np.vstack([np.loadtxt(loc, delimiter=',', dtype=np.int32)
for loc in loc_files])