def preprocess(image):
"""Takes an image and apply preprocess"""
# ????????????
image = cv2.resize(image, (data_shape, data_shape))
# ?? BGR ? RGB
image = image[:, :, (2, 1, 0)]
# ?mean?????float
image = image.astype(np.float32)
# ? mean
image -= np.array([123, 117, 104])
# ??? [batch-channel-height-width]
image = np.transpose(image, (2, 0, 1))
image = image[np.newaxis, :]
# ?? ndarray
image = nd.array(image)
return image
python类transpose()的实例源码
def backPropagate(Z1, Z2, y, W2, b2):
## YOUR CODE HERE ##
E2 = 0
E1 = 0
Eb1 = 0
# E2 is the error in output layer. To find it we should exract estimated value from actual output.
# We should find 5 error because there are 5 node in output layer.
E2 = Z2 - y
## E1 is the error in the hidden layer. To find it we should use the error that we found in output layer and the weights between
## output and hidden layer
## We should find 30 error because there are 30 node in hidden layer.
E1 = np.dot(W2, np.transpose(E2))
## Eb1 is the error bias for hidden layer. To find it we should use the error that we found in output layer and the weights between
## output and bias layer
## We should find 1 error because there are 1 bias node in hidden layer.
Eb1 = np.dot(b2, np.transpose(E2))
####################
return E2, E1, Eb1
# calculate the gradients for weights between units and the bias weights
def transform(self, img, lbl):
img = img[:, :, ::-1]
img = img.astype(np.float64)
img -= self.mean
img = m.imresize(img, (self.img_size[0], self.img_size[1]))
# Resize scales images from 0 to 255, thus we need
# to divide by 255.0
img = img.astype(float) / 255.0
# NHWC -> NCWH
img = img.transpose(2, 0, 1)
lbl[lbl==255] = 0
lbl = lbl.astype(float)
lbl = m.imresize(lbl, (self.img_size[0], self.img_size[1]), 'nearest', mode='F')
lbl = lbl.astype(int)
img = torch.from_numpy(img).float()
lbl = torch.from_numpy(lbl).long()
return img, lbl
def transform(self, img, lbl):
img = img[:, :, ::-1]
img = img.astype(np.float64)
img -= self.mean
img = m.imresize(img, (self.img_size[0], self.img_size[1]))
# Resize scales images from 0 to 255, thus we need
# to divide by 255.0
img = img.astype(float) / 255.0
# NHWC -> NCWH
img = img.transpose(2, 0, 1)
lbl = self.encode_segmap(lbl)
classes = np.unique(lbl)
lbl = lbl.astype(float)
lbl = m.imresize(lbl, (self.img_size[0], self.img_size[1]), 'nearest', mode='F')
lbl = lbl.astype(int)
assert(np.all(classes == np.unique(lbl)))
img = torch.from_numpy(img).float()
lbl = torch.from_numpy(lbl).long()
return img, lbl
def calcGrads(X, Z1, Z2, E1, E2, Eb1):
## YOUR CODE HERE ##
d_W1 = 0
d_b1 = 0
d_W2 = 0
d_b2 = 0
## In here we should the derivatives for gradients. To find derivative, we should multiply.
# d_w2 is the derivative for weights between hidden layer and the output layer.
d_W2 = np.dot(np.transpose(E2), Z1)
# d_w1 is the derivative for weights between hidden layer and the input layer.
d_W1 = np.dot(E1, X)
# d_b2 is the derivative for weights between hidden layer bias and the output layer.
d_b2 = np.dot(np.transpose(E2), Eb1)
# d_b1 is the derivative for weights between hidden layer bias and the input layer.
d_b1 = np.dot(np.transpose(E1), 1)
####################
return d_W1, d_W2, d_b1, d_b2
# update the weights between units and the bias weights using a learning rate of alpha
def updateWeights(W1, b1, W2, b2, alpha, d_W1, d_W2, d_b1, d_b2):
## YOUR CODE HERE ##
# W1 = 0
# b1 = 0
# W2 = 0
# b2 = 0
## Here we should update weights with usin the result that we found in calcGrads function
## W1 is weights between input and the hidden layer
W1 = W1 - alpha * (np.transpose(d_W1)) # 400*30
## W2 is weights between output and the hidden layer
W2 = W2 - alpha * (np.transpose(d_W2)) # 30*5
## b1 is weights between input bias and the hidden layer
b1 = b1 - alpha * d_b1
## b2 is weights between hidden layer bias and the output layer
b2 = b2 - alpha * (np.transpose(d_b2))
####################
return W1, b1, W2, b2
cpm_utils.py 文件源码
项目:convolutional-pose-machines-tensorflow
作者: timctho
项目源码
文件源码
阅读 26
收藏 0
点赞 0
评论 0
def make_heatmaps_from_joints(input_size, heatmap_size, gaussian_variance, batch_joints):
# Generate ground-truth heatmaps from ground-truth 2d joints
scale_factor = input_size // heatmap_size
batch_gt_heatmap_np = []
for i in range(batch_joints.shape[0]):
gt_heatmap_np = []
invert_heatmap_np = np.ones(shape=(heatmap_size, heatmap_size))
for j in range(batch_joints.shape[1]):
cur_joint_heatmap = make_gaussian(heatmap_size,
gaussian_variance,
center=(batch_joints[i][j] // scale_factor))
gt_heatmap_np.append(cur_joint_heatmap)
invert_heatmap_np -= cur_joint_heatmap
gt_heatmap_np.append(invert_heatmap_np)
batch_gt_heatmap_np.append(gt_heatmap_np)
batch_gt_heatmap_np = np.asarray(batch_gt_heatmap_np)
batch_gt_heatmap_np = np.transpose(batch_gt_heatmap_np, (0, 2, 3, 1))
return batch_gt_heatmap_np
def af_h5_to_np(input_path, outpath):
files = tables.open_file(input_path, mode = 'r+')
speaker_nodes = files.root._f_list_nodes()
for spk in speaker_nodes:
file_nodes = spk._f_list_nodes()
for fls in file_nodes:
file_name = fls._v_name
af_nodes = fls._f_list_nodes()
af_list = []
for fts in af_nodes:
features = fts[:]
mean = numpy.mean(features,1)
normalised_feats = list(numpy.transpose(features)/mean)
af_list += normalised_feats
numpy.save(outpath + file_name, numpy.array(af_list))
def mahalanobis_distance(difference, num_random_features):
num_samples, _ = np.shape(difference)
sigma = np.cov(np.transpose(difference))
mu = np.mean(difference, 0)
if num_random_features == 1:
stat = float(num_samples * mu ** 2) / float(sigma)
else:
try:
linalg.inv(sigma)
except LinAlgError:
print('covariance matrix is singular. Pvalue returned is 1.1')
warnings.warn('covariance matrix is singular. Pvalue returned is 1.1')
return 0
stat = num_samples * mu.dot(linalg.solve(sigma, np.transpose(mu)))
return chi2.sf(stat, num_random_features)
def sumIntensitiesMeme(
self,
t,
m,
node_vec,
etimes,
filterlatertimes=True,
):
if filterlatertimes:
I = self.mu * self.gamma[m] \
+ np.dot(np.transpose(self.alpha[node_vec[etimes
< t].astype(int), :][:, range(self.D)]),
self.kernel_evaluate(t, etimes[etimes < t],
self.omega))
else:
I = self.mu * self.gamma[m] \
+ np.dot(np.transpose(self.alpha[node_vec.astype(int), :
][:, range(self.D)]), self.kernel_evaluate(t,
etimes, self.omega))
sumI = np.sum(I)
return (I, sumI)
def sumIntensitiesAll(
self,
t,
node_vec,
etimes,
filterlatertimes=False,
):
if filterlatertimes:
I = self.mu * np.sum(self.gamma) \
+ np.dot(np.transpose(self.alpha[node_vec[etimes
< t].astype(int), :][:, range(self.D)]),
self.kernel_evaluate(t, etimes[etimes < t],
self.omega))
else:
I = self.mu * np.sum(self.gamma) \
+ np.dot(np.transpose(self.alpha[node_vec.astype(int), :
][:, range(self.D)]), self.kernel_evaluate(t,
etimes, self.omega))
sumI = np.sum(I)
return (I, sumI)
def _intensityUserMeme(
self,
t,
d,
m,
filterlatertimes=False,
):
etimes = self.etimes[self.eventmemes == m]
node_vec = self.node_vec[self.eventmemes == m]
if filterlatertimes:
return self.mu[d] * self.gamma[m] \
+ np.dot(np.transpose(self.alpha[node_vec[etimes
< t].astype(int), :][:, d]),
self.kernel_evaluate(t, etimes[etimes < t],
self.omega))
else:
return self.mu[d] * self.gamma[m] \
+ np.dot(np.transpose(self.alpha[node_vec.astype(int), :
][:, d]), self.kernel_evaluate(t, etimes,
self.omega))
config_dataset_HAR_6_classes.py 文件源码
项目:EmotiW-2017-Audio-video-Emotion-Recognition
作者: xujinchang
项目源码
文件源码
阅读 44
收藏 0
点赞 0
评论 0
def load_X(X_signals_paths):
"""
Given attribute (train or test) of feature, read all 9 features into an
np ndarray of shape [sample_sequence_idx, time_step, feature_num]
argument: X_signals_paths str attribute of feature: 'train' or 'test'
return: np ndarray, tensor of features
"""
X_signals = []
for signal_type_path in X_signals_paths:
file = open(signal_type_path, 'rb')
# Read dataset from disk, dealing with text files' syntax
X_signals.append(
[np.array(serie, dtype=np.float32) for serie in [
row.replace(' ', ' ').strip().split(' ') for row in file
]]
)
file.close()
return np.transpose(np.array(X_signals), (1, 2, 0))
def get_batcher(self, shuffle=True, augment=True):
""" produces batch generator """
w, h = self.resize
if shuffle: np.random.shuffle(self.data)
data = iter(self.data)
while True:
x = np.zeros((self.batch_size, self.timesteps, h, w, 3))
y = np.zeros((self.batch_size, 1))
for b in range(self.batch_size):
images, label = next(data)
for t, img_name in enumerate(images):
image_path = self.folder + 'images/' + img_name
img = cv2.imread(image_path)
img = img[190:350, 100:520] # crop
if augment:
img = aug.augment_image(img) # augmentation
img = cv2.resize(img.copy(), (w, h))
x[b, t] = img
y[b] = label
x = np.transpose(x, [0, 4, 1, 2, 3])
yield x, y
def _random_op(sites, ldim, hermitian=False, normalized=False, randstate=None,
dtype=np.complex_):
"""Returns a random operator of shape (ldim,ldim) * sites with local
dimension `ldim` living on `sites` sites in global form.
:param sites: Number of local sites
:param ldim: Local ldimension
:param hermitian: Return only the hermitian part (default False)
:param normalized: Normalize to Frobenius norm=1 (default False)
:param randstate: numpy.random.RandomState instance or None
:returns: numpy.ndarray of shape (ldim,ldim) * sites
>>> A = _random_op(3, 2); A.shape
(2, 2, 2, 2, 2, 2)
"""
op = _randfuncs[dtype]((ldim**sites,) * 2, randstate=randstate)
if hermitian:
op += np.transpose(op).conj()
if normalized:
op /= np.linalg.norm(op)
return op.reshape((ldim,) * 2 * sites)
def transpose(self, axes=None):
"""Transpose (=reverse order of) physical legs on each site
:param axes: New order of the physical axes. If ``None`` is passed,
we reverse the order of the legs on each site. (default ``None``)
>>> from .factory import random_mpa
>>> mpa = random_mpa(2, (2, 3, 4), 2)
>>> mpa.shape
((2, 3, 4), (2, 3, 4))
>>> mpa.transpose((2, 0, 1)).shape
((4, 2, 3), (4, 2, 3))
"""
ltens = LocalTensors((_local_transpose(tens, axes) for tens in self.lt),
cform=self.canonical_form)
return type(self)(ltens)
def cal_hist(self, t1, t2, data1_maxlen, hist_size):
mhist = np.zeros((data1_maxlen, hist_size), dtype=np.float32)
d1len = len(self.data1[t1])
if self.use_hist_feats:
assert (t1, t2) in self.hist_feats
caled_hist = np.reshape(self.hist_feats[(t1, t2)], (d1len, hist_size))
if d1len < data1_maxlen:
mhist[:d1len, :] = caled_hist[:, :]
else:
mhist[:, :] = caled_hist[:data1_maxlen, :]
else:
t1_rep = self.embed[self.data1[t1]]
t2_rep = self.embed[self.data2[t2]]
mm = t1_rep.dot(np.transpose(t2_rep))
for (i,j), v in np.ndenumerate(mm):
if i >= data1_maxlen:
break
vid = int((v + 1.) / 2. * ( hist_size - 1.))
mhist[i][vid] += 1.
mhist += 1.
mhist = np.log10(mhist)
return mhist
def cal_hist(self, t1, t2, data1_maxlen, hist_size):
mhist = np.zeros((data1_maxlen, hist_size), dtype=np.float32)
t1_cont = list(self.data1[t1])
t2_cont = list(self.data2[t2])
d1len = len(t1_cont)
if self.use_hist_feats:
assert (t1, t2) in self.hist_feats
caled_hist = np.reshape(self.hist_feats[(t1, t2)], (d1len, hist_size))
if d1len < data1_maxlen:
mhist[:d1len, :] = caled_hist[:, :]
else:
mhist[:, :] = caled_hist[:data1_maxlen, :]
else:
t1_rep = self.embed[t1_cont]
t2_rep = self.embed[t2_cont]
mm = t1_rep.dot(np.transpose(t2_rep))
for (i,j), v in np.ndenumerate(mm):
if i >= data1_maxlen:
break
vid = int((v + 1.) / 2. * ( hist_size - 1.))
mhist[i][vid] += 1.
mhist += 1.
mhist = np.log10(mhist)
return mhist
def cal_hist(self, t1, t2, data1_maxlen, hist_size):
mhist = np.zeros((data1_maxlen, hist_size), dtype=np.float32)
t1_cont = list(self.data1[t1])
t2_cont = list(self.data2[t2])
d1len = len(t1_cont)
if self.use_hist_feats:
assert (t1, t2) in self.hist_feats
curr_pair_feats = list(self.hist_feats[(t1, t2)])
caled_hist = np.reshape(curr_pair_feats, (d1len, hist_size))
if d1len < data1_maxlen:
mhist[:d1len, :] = caled_hist[:, :]
else:
mhist[:, :] = caled_hist[:data1_maxlen, :]
else:
t1_rep = self.embed[t1_cont]
t2_rep = self.embed[t2_cont]
mm = t1_rep.dot(np.transpose(t2_rep))
for (i,j), v in np.ndenumerate(mm):
if i >= data1_maxlen:
break
vid = int((v + 1.) / 2. * ( hist_size - 1.))
mhist[i][vid] += 1.
mhist += 1.
mhist = np.log10(mhist)
return mhist
def cal_hist(self, t1, t2, data1_maxlen, hist_size):
mhist = np.zeros((data1_maxlen, hist_size), dtype=np.float32)
t1_cont = list(self.data1[t1])
t2_cont = list(self.data2[t2])
d1len = len(t1_cont)
if self.use_hist_feats:
assert (t1, t2) in self.hist_feats
caled_hist = np.reshape(self.hist_feats[(t1, t2)], (d1len, hist_size))
if d1len < data1_maxlen:
mhist[:d1len, :] = caled_hist[:, :]
else:
mhist[:, :] = caled_hist[:data1_maxlen, :]
else:
t1_rep = self.embed[t1_cont]
t2_rep = self.embed[t2_cont]
mm = t1_rep.dot(np.transpose(t2_rep))
for (i,j), v in np.ndenumerate(mm):
if i >= data1_maxlen:
break
vid = int((v + 1.) / 2. * ( hist_size - 1.))
mhist[i][vid] += 1.
mhist += 1.
mhist = np.log10(mhist)
return mhist
def cal_hist(self, t1, t2, data1_maxlen, hist_size):
mhist = np.zeros((data1_maxlen, hist_size), dtype=np.float32)
t1_cont = list(self.data1[t1])
t2_cont = list(self.data2[t2])
d1len = len(t1_cont)
if self.use_hist_feats:
assert (t1, t2) in self.hist_feats
curr_pair_feats = list(self.hist_feats[(t1, t2)])
caled_hist = np.reshape(curr_pair_feats, (d1len, hist_size))
if d1len < data1_maxlen:
mhist[:d1len, :] = caled_hist[:, :]
else:
mhist[:, :] = caled_hist[:data1_maxlen, :]
else:
t1_rep = self.embed[t1_cont]
t2_rep = self.embed[t2_cont]
mm = t1_rep.dot(np.transpose(t2_rep))
for (i,j), v in np.ndenumerate(mm):
if i >= data1_maxlen:
break
vid = int((v + 1.) / 2. * ( hist_size - 1.))
mhist[i][vid] += 1.
mhist += 1.
mhist = np.log10(mhist)
return mhist
def format_img(img, C):
img_min_side = float(C.im_size)
(height,width,_) = img.shape
if width <= height:
f = img_min_side/width
new_height = int(f * height)
new_width = int(img_min_side)
else:
f = img_min_side/height
new_width = int(f * width)
new_height = int(img_min_side)
fx = width/float(new_width)
fy = height/float(new_height)
img = cv2.resize(img, (new_width, new_height), interpolation=cv2.INTER_CUBIC)
img = img[:, :, (2, 1, 0)]
img = img.astype(np.float32)
img[:, :, 0] -= C.img_channel_mean[0]
img[:, :, 1] -= C.img_channel_mean[1]
img[:, :, 2] -= C.img_channel_mean[2]
img /= C.img_scaling_factor
img = np.transpose(img, (2, 0, 1))
img = np.expand_dims(img, axis=0)
return img, fx, fy
def calc_score_of_histories(words, dropout=0.0):
# This will change from a list of histories, to a list of words in each history position
words = np.transpose(words)
# Lookup the embeddings and concatenate them
emb = dy.concatenate([dy.lookup_batch(W_emb, x) for x in words])
# Create the hidden layer
W_h = dy.parameter(W_h_p)
b_h = dy.parameter(b_h_p)
h = dy.tanh(dy.affine_transform([b_h, W_h, emb]))
# Perform dropout
if dropout != 0.0:
h = dy.dropout(h, dropout)
# Calculate the score and return
W_sm = dy.parameter(W_sm_p)
b_sm = dy.parameter(b_sm_p)
return dy.affine_transform([b_sm, W_sm, h])
# Calculate the loss value for the entire sentence
def reSample( df , dt = None , xAxis = None , n = None , kind = 'linear') :
""" re-sample the signal """
if type(df) == pd.Series : df = pd.DataFrame(df)
f = interp1d( df.index, np.transpose(df.values) , kind=kind, axis=-1, copy=True, bounds_error=True, assume_sorted=True)
if dt :
end = int(+(df.index[-1] - df.index[0] ) / dt) * dt + df.index[0]
xAxis = np.linspace( df.index[0] , end , 1+int(+(end - df.index[0] ) / dt) )
elif n :
xAxis = np.linspace( df.index[0] , df.index[-1] , n )
elif xAxis == None :
raise(Exception("reSample : either dt or xAxis should be provided" ))
#For rounding issue, ensure that xAxis is within ts.xAxis
#xAxis[ np.where( xAxis > np.max(df.index[:]) ) ] = df.index[ np.where( xAxis > np.max(df.index[:]) ) ]
return pd.DataFrame( data = np.transpose(f(xAxis)), index = xAxis , columns = map( lambda x : "reSample("+ x +")" , df.columns ) )
def getPSD( df , dw = 0.05, roverlap = 0.5, window='hanning', detrend='constant') :
"""
Compute the power spectral density
"""
if type(df) == pd.Series : df = pd.DataFrame(df)
nfft = int ( (2*pi / dw) / dx(df) )
nperseg = 2**int(log(nfft)/log(2))
noverlap = nperseg * roverlap
""" Return the PSD of a time signal """
try :
from scipy.signal import welch
except :
raise Exception("Welch function not found, please install scipy > 0.12")
data = []
for iSig in range(df.shape[1]) :
test = welch( df.values[:,iSig] , fs = 1. / dx(df) , window=window, nperseg=nperseg, noverlap=noverlap, nfft=nfft, detrend=detrend, return_onesided=True, scaling='density')
data.append( test[1] / (2*pi) )
xAxis = test[0][:] * 2*pi
return pd.DataFrame( data = np.transpose(data), index = xAxis , columns = [ "psd("+ str(x) +")" for x in df.columns ] )
def derivFFT(df, n=1 ) :
""" Deriv a signal trought FFT, warning, edge can be a bit noisy...
indexList : channel to derive
n : order of derivation
"""
deriv = []
for iSig in range(df.shape[1]) :
fft = np.fft.fft( df.values[:,iSig] ) #FFT
freq = np.fft.fftfreq( df.shape[0] , dx(df) )
from copy import deepcopy
fft0 = deepcopy(fft)
if n>0 :
fft *= (1j * 2*pi* freq[:])**n #Derivation in frequency domain
else :
fft[-n:] *= (1j * 2*pi* freq[-n:])**n
fft[0:-n] = 0.
tts = np.real(np.fft.ifft(fft))
tts -= tts[0]
deriv.append( tts ) #Inverse FFT
return pd.DataFrame( data = np.transpose(deriv), index = df.index , columns = [ "DerivFFT("+ x +")" for x in df.columns ] )
def _starts_with_output(data, col):
'''
Helper function for to_integers in cases where
the feature is categorized based on a common
first character of a string.
'''
data[col] = data[col].fillna('0')
temp_df = _category_starts_with(data, col)
temp_df['start_char'] = temp_df[0]
temp_df = temp_df.drop(0, axis=1)
reference_df = temp_df.set_index('start_char').transpose()
temp_list = []
for i in range(len(data[col])):
for c in temp_df['start_char']:
if data[col][i].startswith(c) == True:
temp_list.append(reference_df[c][0])
if len(data[col]) != len(temp_list):
print "AUTONOMIO ERROR: length of input and output do not match"
else:
return pd.Series(temp_list)
def reduce_height(img4, eng):
"""
Reduces the height by 1 pixel
Args:
img4 (n,m,4 numpy matrix): RGB image with additional mask layer.
eng (n,m numpy matrix): Pre-computed energy matrix for supplied image.
Returns:
tuple (
n,1 numpy matrix: the removed seam,
n-1,m,4 numpy matrix: The height-redcued image,
float: The cost of the seam removed
)
"""
flipped_eng = np.transpose(eng)
flipped_img4 = np.transpose(img4, (1, 0, 2))
flipped_seam, reduced_flipped_img4, cost = reduce_width(flipped_img4, flipped_eng)
return (
np.transpose(flipped_seam),
np.transpose(reduced_flipped_img4, (1, 0, 2)),
cost
)
def format_img(img, C):
img_min_side = float(C.im_size)
(height,width,_) = img.shape
if width <= height:
f = img_min_side/width
new_height = int(f * height)
new_width = int(img_min_side)
else:
f = img_min_side/height
new_width = int(f * width)
new_height = int(img_min_side)
fx = width/float(new_width)
fy = height/float(new_height)
img = cv2.resize(img, (new_width, new_height), interpolation=cv2.INTER_CUBIC)
img = img[:, :, (2, 1, 0)]
img = img.astype(np.float32)
img[:, :, 0] -= C.img_channel_mean[0]
img[:, :, 1] -= C.img_channel_mean[1]
img[:, :, 2] -= C.img_channel_mean[2]
img /= C.img_scaling_factor
img = np.transpose(img, (2, 0, 1))
img = np.expand_dims(img, axis=0)
return img, fx, fy
def _mutual_reach_dist_MST(dist_tree):
"""
Computes minimum spanning tree of the mutual reach distance complete graph
Args:
dist_tree (np.ndarray): array of dimensions (n_samples, n_samples)
Graph of all pair-wise mutual reachability distances
between points.
Returns: minimum_spanning_tree (np.ndarray)
array of dimensions (n_samples, n_samples)
minimum spanning tree of all pair-wise mutual reachability
distances between points.
"""
mst = minimum_spanning_tree(dist_tree).toarray()
return mst + np.transpose(mst)