本文整理汇总了Python中numpy.swapaxes函数的典型用法代码示例。如果您正苦于以下问题:Python swapaxes函数的具体用法?Python swapaxes怎么用?Python swapaxes使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了swapaxes函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: prep_image
def prep_image(im, IMAGE_W, IMAGE_H, BGR=BGR):
if len(im.shape) == 2:
im = im[:, :, np.newaxis]
im = np.repeat(im, 3, axis=2)
h, w, _ = im.shape
if h*IMAGE_W < w*IMAGE_H:
im = skimage.transform.resize(im, (IMAGE_H, w*IMAGE_H//h), preserve_range=True)
else:
im = skimage.transform.resize(im, (h*IMAGE_W//w, IMAGE_W), preserve_range=True)
# Central crop
h, w, _ = im.shape
im = im[h//2-IMAGE_H//2:h//2+IMAGE_H//2, w//2-IMAGE_W//2:w//2+IMAGE_W//2]
rawim = im.astype('uint8')
# Shuffle axes to c01
im = np.swapaxes(np.swapaxes(im, 1, 2), 0, 1)
# Convert RGB to BGR
if not BGR:
im = im[::-1, :, :]
im = im - MEAN_VALUES
return rawim, floatX(im[np.newaxis])
示例2: load_minibatch
def load_minibatch(input_list, color, labels, start,num):
# Enforce maximum on start
start = max(0,start)
# Enforce minimum on end
end = start + num
end = min(len(input_list), end)
# Isolate files
files = input_list[start:end]
images = []
for file in files:
img = caffe.io.load_image(file, color)
# Handle incorrect image dims for uncropped images
# TODO: Get uncropped images to import correctly
if img.shape[0] == 3 or img.shape[0] == 1:
img = np.swapaxes(np.swapaxes(img, 0, 1), 1, 2)
# BUG FIX: Is this ok?
# color=True gets the correct desired dimension of WxHx3
# But color=False gets images of WxHx1. Need WxHx3 or will get "Index out of bounds" exception
# Fix by concatenating three copies of the image
if img.shape[2] == 1:
img = cv.merge([img,img,img])
# Add image array to batch
images.append(img)
labelsReduced = labels[start:end]
return images, labelsReduced
示例3: rgb2caffe
def rgb2caffe(im, downscale=True, out_size=(128, 171)):
'''
Converts an RGB image to caffe format and downscales it as needed by C3D
Parameters
----------
im an RGB image
downscale
Returns
-------
a caffe image (channel,height, width) in BGR format
'''
im=np.copy(im)
if len(im.shape)==2: # Make sure the image has 3 channels
im = color.gray2rgb(im)
if downscale:
h, w, _ = im.shape
im = skimage.transform.resize(im, out_size, preserve_range=True)
else:
im=np.array(im,theano.config.floatX)
im = np.swapaxes(np.swapaxes(im, 1, 2), 0, 1)
# Convert to BGR
im = im[::-1, :, :]
return np.array(im,theano.config.floatX)
示例4: acorr
def acorr(x, axis=-1, onesided=False, scale='none'):
"""Compute autocorrelation of x along given axis.
Parameters
----------
x : array-like
signal to correlate.
axis : int
axis along which autocorrelation is computed.
onesided: bool, optional
if True, only returns the right side of the autocorrelation.
scale: {'none', 'coeff'}
scaling mode. If 'coeff', the correlation is normalized such as the
0-lag is equal to 1.
Notes
-----
Use fft for computation: is more efficient than direct computation for
relatively large n.
"""
if not np.isrealobj(x):
raise ValueError("Complex input not supported yet")
if not scale in ['none', 'coeff']:
raise ValueError("scale mode %s not understood" % scale)
maxlag = x.shape[axis]
nfft = 2 ** nextpow2(2 * maxlag - 1)
if axis != -1:
x = np.swapaxes(x, -1, axis)
a = _acorr_last_axis(x, nfft, maxlag, onesided, scale)
if axis != -1:
a = np.swapaxes(a, -1, axis)
return a
示例5: load_image
def load_image(self, impath, normalize=False):
if os.path.splitext(impath)[1] == ".npz":
im = np.load(impath)["im"]
else:
im = skimage.io.imread(impath)
if normalize:
im = im / 255.0
im = resize(im, self.imshape, mode="nearest")
if self.mean_im is not None:
im -= self.mean_im
# shuffle from (W,H,3) to (3,W,H)
if not self.greyscale:
im = np.swapaxes(im, 0, 2)
im = np.swapaxes(im, 1, 2)
else:
if im.ndim == 3:
im = skimage.color.rgb2grey(im)
return im
示例6: _read_img
def _read_img(self, img, label):
if self.regress_overlay:
label = label
else:
label = 0
img = np.array(img)
img = img.reshape(self.crop_size, self.crop_size)
#cv2.imshow("GRAY", img)
#cv2.waitKey(0)
if self.scale_size is not None:
img = cv2.resize(img, (self.scale_size, self.scale_size), interpolation=cv2.INTER_AREA).astype(float)
img = img.reshape(img.shape[0], img.shape[1], 1)
img = np.swapaxes(img, 0, 2)
img = np.swapaxes(img, 1, 2) # (c, h, w)
label_1 = label#20161026
flag = False
## for t in label:
## if t > 1:
## print("label_1: ", label_1)
## flag =True
label = np.array(label_1)
img = np.expand_dims(img, axis=0) # (1, c, h, w) or (1, h, w)
#print(label)
return img, label, False#label_1, label_2, flag
示例7: _npBatchMatmul
def _npBatchMatmul(self, x, y, adjoint_a, adjoint_b):
# output's shape depends on adj[0] and adj[1]
if adjoint_a:
x = np.conjugate(np.swapaxes(x, -1, -2))
if adjoint_b:
y = np.conjugate(np.swapaxes(y, -1, -2))
return np.matmul(x, y)
示例8: prepare_image
def prepare_image(img, width, means):
# if not RGB, force 3 channels
if len(img.shape) == 2:
img = img[:, :, np.newaxis]
img = np.repeat(img, 3, axis=2)
h, w, _ = img.shape
if h < w:
img = skimage.transform.resize(img, (width, w*width/h), preserve_range=True)
else:
img = skimage.transform.resize(img, (h*width/w, width), preserve_range=True)
# crop the center
h, w, _ = img.shape
img = img[h//2 - width//2:h//2 + width//2, w//2 - width//2:w//2 + width//2]
rawim = np.copy(img).astype('uint8')
# shuffle axes to c01
img = np.swapaxes(np.swapaxes(img, 1, 2), 0, 1)
# convert RGB to BGR
img = img[::-1, :, :]
# zero mean scaling
img = img - means
return rawim, floatX(img[np.newaxis])
示例9: predictions_grid_color
def predictions_grid_color(samples, predictions, k, imgshape, title):
batch_size = samples.shape[0]
print("printintg predictions:")
print(samples.shape)
print(imgshape)
print(predictions.shape)
samples = samples.reshape(batch_size, imgshape[1], imgshape[2], imgshape[3])
predictions = predictions.reshape(batch_size, imgshape[1], imgshape[2], imgshape[3])
plt.figure(figsize=(10, 10))
gs = gridspec.GridSpec(5, 2)
for i in range(10):
ax = plt.subplot(gs[i])
if i % 2 == 0:
w = samples[i/2, :, :, :]
else:
w = predictions[i/2, :, :, :]
w = np.swapaxes(w, 0, 1)
w = np.swapaxes(w, 1, 2)
ax.imshow(w,
cmap=plt.cm.gist_yarg,
interpolation='nearest',
aspect='equal')
ax.set_xticklabels([])
ax.set_yticklabels([])
ax.axis('off')
gs.update(wspace=0)
plt.savefig(os.path.join('convpredictions', str(k) + '_' + title + '.png'),
bbox_inches='tight')
plt.close('all')
示例10: initializeIndices
def initializeIndices(self, ni, nj):
if not ni==None:
self.ni = numpy.array(ni,int)
if not nj==None:
self.nj = numpy.array(nj,int)
if self.mSide==-1:
if ni==None:
self.ni = [1,self.mComp.ms[0].shape[0],1]
if nj==None:
self.nj = [1,self.mComp.ms[2].shape[0],1]
else:
if ni==None:
self.ni = [1,0,1]
if nj==None:
self.nj = [1,self.mComp.ms[0].shape[0],1]
if self.fRot==0:
self.rotate = lambda P: P
self.flip = lambda nu, nv: [nu,nv]
elif self.fRot==1:
self.rotate = lambda P: numpy.swapaxes(P,0,1)[::-1,:]
self.flip = lambda nu, nv: [nv[::-1],nu]
elif self.fRot==2:
self.rotate = lambda P: P[::-1,::-1]
self.flip = lambda nu, nv: [nu[::-1],nv[::-1]]
elif self.fRot==3:
self.rotate = lambda P: numpy.swapaxes(P,0,1)[:,::-1]
self.flip = lambda nu, nv: [nv,nu[::-1]]
self.fK = self.rotate(self.fComp.Ks[self.fFace])[self.fNW[0]:self.fNW[0]+sum(self.ni),self.fNW[1]:self.fNW[1]+sum(self.nj)]
示例11: reorder_array
def reorder_array(the_array, order_list):
# put time first as by agrreement in IDTxL
time_dimension = order_list.index("time")
if time_dimension != 1:
the_array = np.swapaxes(the_array,1,time_dimension)
# also swap the list to reflect the new arrangement
order_list[1], order_list[time_dimension] = \
order_list[time_dimension], order_list[1]
# put channel second
channel_dimension = order_list.index("channel")
if channel_dimension !=2:
the_array = np.swapaxes(the_array,2,channel_dimension)
# also swap the list to reflect the new arrangement
order_list[2], order_list[channel_dimension] = \
order_list[channel_dimension], order_list[2]
# put repetitions third - unnecessary in principle as n-1 permutations
# are guaranteed to sort our array dimensions for n dimensions
#assert order_list.index("repetition") == 3, print('something went wrong with reordering')
# uncomment the following code when expanding
#repetition_dimension = order_list.index("repetition")
#if repetition_dimension !=2:
# the_array = np.swapaxes(the_array,2,repetition_dimension)
# # also swap the list to reflect the new arrangement
# order_list[3], order_list[repetition_dimension] = \
# order_list[repetition_dimension], order_list[3]
# put further dimensions fourth in future versions...
return the_array
示例12: generate_batch
def generate_batch(num_samples, obj_type='circle'):
# generate a minibatch of trajectories
traj_pos, traj_vel = TRAJ.generate_trajectories(num_samples, traj_len)
traj_x = traj_pos[:,:,0]
traj_y = traj_pos[:,:,1]
# draw the trajectories
center_x = to_fX( traj_x.T.ravel() )
center_y = to_fX( traj_y.T.ravel() )
delta = to_fX( np.ones(center_x.shape) )
sigma = to_fX( np.ones(center_x.shape) )
if obj_type == 'circle':
W = paint_circle(center_y, center_x, delta, 0.05*sigma)
else:
W = paint_cross(center_y, center_x, delta, 0.05*sigma)
# shape trajectories into a batch for passing to the model
batch_imgs = np.zeros((num_samples, traj_len, obs_dim))
batch_coords = np.zeros((num_samples, traj_len, 2))
for i in range(num_samples):
start_idx = i * traj_len
end_idx = start_idx + traj_len
img_set = W[start_idx:end_idx,:]
batch_imgs[i,:,:] = img_set
batch_coords[i,:,0] = center_x[start_idx:end_idx]
batch_coords[i,:,1] = center_y[start_idx:end_idx]
batch_imgs = np.swapaxes(batch_imgs, 0, 1)
batch_coords = np.swapaxes(batch_coords, 0, 1)
return [to_fX( batch_imgs ), to_fX( batch_coords )]
示例13: dGamma_dgam
def dGamma_dgam(self,gam_IM,gam_MD1,gam_D1D2,gam_D2R,
lam_IM,lam_MD1,lam_D1D2,lam_D2R):
dgdgam_IM, dgdgam_MD1, dgdgam_D1D2, dgdgam_D2R = \
self.dFrequencyCostdGamma_dgdgam(gam_IM,gam_MD1,gam_D1D2,gam_D2R)
dedgam_IM, dedgam_MD1, dedgam_D1D2, dedgam_D2R = \
self.dEmissionCostdGamma_dedgam(gam_IM,gam_MD1,gam_D1D2,gam_D2R)
w_IM = np.reshape(np.tile(np.swapaxes(self.w[None],0,1),
np.prod(dedgam_IM.shape[1:])).flatten(),
dedgam_IM.shape)
w_MD1 = np.reshape(np.tile(np.swapaxes(self.w[None],0,1),
np.prod(dedgam_MD1.shape[1:])).flatten(),
dedgam_MD1.shape)
w_D1D2 = np.reshape(np.tile(np.swapaxes(self.w[None],0,1),
np.prod(dedgam_D1D2.shape[1:])).flatten(),
dedgam_D1D2.shape)
w_D2R = np.reshape(np.tile(np.swapaxes(self.w[None],0,1),
np.prod(dedgam_D2R.shape[1:])).flatten(),
dedgam_D2R.shape)
dgam_IM = dgdgam_IM + w_IM*dedgam_IM - self.u_IM*lam_IM
dgam_MD1 = dgdgam_MD1 + w_MD1*dedgam_MD1 - self.u_MD1*lam_MD1
dgam_D1D2 = dgdgam_D1D2 + w_D1D2*dedgam_D1D2 - self.u_D1D2*lam_D1D2
dgam_D2R = dgdgam_D2R + w_D2R*dedgam_D2R - self.u_D2R*lam_D2R
return dgam_IM, dgam_MD1, dgam_D1D2, dgam_D2R
示例14: dLinkFlow_df
def dLinkFlow_df(self,f_IM,f_MD1,f_D1D2,f_D2R,
lam_IM,lam_MD1,lam_D1D2,lam_D2R):
dcdf_IM, dcdf_MD1, dcdf_D1D2, dcdf_D2R = \
self.dOperationalCostdLinkFlow_dcdf(f_IM,f_MD1,f_D1D2,f_D2R)
dedf_IM, dedf_MD1, dedf_D1D2, dedf_D2R = \
self.dEmissionCostdLinkFlow_dedf(f_IM,f_MD1,f_D1D2,f_D2R)
w_IM = np.reshape(np.tile(np.swapaxes(self.w[None],0,1),
np.prod(dedf_IM.shape[1:])).flatten(),
dedf_IM.shape)
w_MD1 = np.reshape(np.tile(np.swapaxes(self.w[None],0,1),
np.prod(dedf_MD1.shape[1:])).flatten(),
dedf_MD1.shape)
w_D1D2 = np.reshape(np.tile(np.swapaxes(self.w[None],0,1),
np.prod(dedf_D1D2.shape[1:])).flatten(),
dedf_D1D2.shape)
w_D2R = np.reshape(np.tile(np.swapaxes(self.w[None],0,1),
np.prod(dedf_D2R.shape[1:])).flatten(),
dedf_D2R.shape)
df_IM = dcdf_IM + w_IM*dedf_IM + lam_IM
df_MD1 = dcdf_MD1 + w_MD1*dedf_MD1 + lam_MD1
df_D1D2 = dcdf_D1D2 + w_D1D2*dedf_D1D2 + lam_D1D2
df_D2R = dcdf_D2R + w_D2R*dedf_D2R + lam_D2R
return df_IM, df_MD1, df_D1D2, df_D2R
示例15: _read_img
def _read_img(self, img_name, label_name):
img = Image.open(os.path.join(self.root_dir, img_name))
label = Image.open(os.path.join(self.root_dir, label_name))
assert img.size == label.size
img = np.array(img, dtype=np.float32) # (h, w, c)
label = np.array(label) # (h, w)
if self.cut_off_size is not None:
max_hw = max(img.shape[0], img.shape[1])
min_hw = min(img.shape[0], img.shape[1])
if min_hw > self.cut_off_size:
rand_start_max = round(np.random.uniform(0, max_hw - self.cut_off_size - 1))
rand_start_min = round(np.random.uniform(0, min_hw - self.cut_off_size - 1))
if img.shape[0] == max_hw :
img = img[rand_start_max : rand_start_max + self.cut_off_size, rand_start_min : rand_start_min + self.cut_off_size]
label = label[rand_start_max : rand_start_max + self.cut_off_size, rand_start_min : rand_start_min + self.cut_off_size]
else :
img = img[rand_start_min : rand_start_min + self.cut_off_size, rand_start_max : rand_start_max + self.cut_off_size]
label = label[rand_start_min : rand_start_min + self.cut_off_size, rand_start_max : rand_start_max + self.cut_off_size]
elif max_hw > self.cut_off_size:
rand_start = round(np.random.uniform(0, max_hw - min_hw - 1))
if img.shape[0] == max_hw :
img = img[rand_start : rand_start + min_hw, :]
label = label[rand_start : rand_start + min_hw, :]
else :
img = img[:, rand_start : rand_start + min_hw]
label = label[:, rand_start : rand_start + min_hw]
reshaped_mean = self.mean.reshape(1, 1, 3)
img = img - reshaped_mean
img = np.swapaxes(img, 0, 2)
img = np.swapaxes(img, 1, 2) # (c, h, w)
img = np.expand_dims(img, axis=0) # (1, c, h, w)
label = np.array(label) # (h, w)
label = np.expand_dims(label, axis=0) # (1, h, w)
return (img, label)