def cropImg(img,oriRio=0.24/0.36):
#oriImg = cv2.imread(img)
#img = np.swapaxes(img,0,2)
h = img.shape[0]
w = img.shape[1]
# from the middle of the long side (the middle two points)to
# crop based on the shorter side, then according to the ucf101
# ratio to crop the other side
if h <= w * oriRio:
crop_ws = w/2-1-int(h/(oriRio*2))
crop_we = w/2+int(h/(oriRio*2))
subImg = img[:,crop_ws:crop_we,:]
else:
crop_hs = h/2-1-int(w*(oriRio/2))
crop_he = h/2+int(w*(oriRio/2))
subImg = img[crop_hs:crop_he,:,:]
return subImg
python类swapaxes()的实例源码
def create_tensor(file1,mean_array):
video_1 = cv2.VideoCapture(file1)
len_1 = int(video_1.get(cv2.cv.CV_CAP_PROP_FRAME_COUNT))
tensor_1 = np.zeros([3,len_1,112,112])
count = 0
ret = True
while True:
ret, frame_1 = video_1.read()
if frame_1 is not None:
tensor_1[:,count,:,:] = np.swapaxes(cv2.resize(cropImg(frame_1),(112,112)),0,2) - mean_array
count = count+1
print count
else:
break
tensor = tensor_1[:,:count,:,:]
return tensor
def cropImg(img,oriRio=0.24/0.36):
#oriImg = cv2.imread(img)
#img = np.swapaxes(img,0,2)
h = img.shape[0]
w = img.shape[1]
# from the middle of the long side (the middle two points)to
# crop based on the shorter side, then according to the ucf101
# ratio to crop the other side
if h <= w * oriRio:
crop_ws = w/2-1-int(h/(oriRio*2))
crop_we = w/2+int(h/(oriRio*2))
subImg = img[:,crop_ws:crop_we,:]
else:
crop_hs = h/2-1-int(w*(oriRio/2))
crop_he = h/2+int(w*(oriRio/2))
subImg = img[crop_hs:crop_he,:,:]
return subImg
def cropImg(img,oriRio=0.24/0.36):
#oriImg = cv2.imread(img)
#img = np.swapaxes(img,0,2)
h = img.shape[0]
w = img.shape[1]
# from the middle of the long side (the middle two points)to
# crop based on the shorter side, then according to the ucf101
# ratio to crop the other side
if h <= w * oriRio:
crop_ws = w/2-1-int(h/(oriRio*2))
crop_we = w/2+int(h/(oriRio*2))
subImg = img[:,crop_ws:crop_we,:]
else:
crop_hs = h/2-1-int(w*(oriRio/2))
crop_he = h/2+int(w*(oriRio/2))
subImg = img[crop_hs:crop_he,:,:]
return subImg
def create_tensor(file1,mean_array):
video_1 = cv2.VideoCapture(file1)
len_1 = int(video_1.get(cv2.cv.CV_CAP_PROP_FRAME_COUNT))
tensor_1 = np.zeros([3,len_1,112,112])
count = 0
ret = True
while True:
ret, frame_1 = video_1.read()
if frame_1 is not None:
tensor_1[:,count,:,:] = np.swapaxes(cv2.resize(cropImg(frame_1),(112,112)),0,2) - mean_array
count = count+1
print count
else:
break
tensor = tensor_1[:,:count,:,:]
return tensor
def compute_xvvr(self):
""" Return xvv(r) matrix """
r = np.array([i*self.dr for i in range(self.ngrid)])
k = self.get_k()
xvvr = [["" for i in range(self.nsites)] for j in range(self.nsites)]
for i in range(self.nsites):
for j in range(self.nsites):
xvvk_ij = self.xvv_data[:,i,j]
xvvr_ij = pubfft.sinfti(xvvk_ij*k, self.dr, -1)/r
# n_pots_for_interp = 6
# r_for_interp = r[1:n_pots_for_interp+1]
# xvvr_for_interp = xvvr_ij[:n_pots_for_interp]
# poly_coefs = np.polyfit(r_for_interp, xvvr_for_interp, 3)
# poly_f = np.poly1d(poly_coefs)
# xvvr[i][j] = [poly_f(0)]
xvvr[i][j] = xvvr_ij
return r, np.swapaxes(xvvr, 0, 2)
def compute_zr(self):
""" Return z(r) matrix """
r = np.array([i*self.dr for i in range(self.ngrid)])
k, zk = self.compute_zk()
print 'computed zk',zk.shape
zr = [["" for i in range(self.nsites)] for j in range(self.nsites)]
for i in range(self.nsites):
for j in range(self.nsites):
zk_ij = zk[1:,i,j]
zr_ij = pubfft.sinfti(zk_ij*k[1:], self.dr, -1)/r[1:]
#zr_ij = np.abs(fftpack.fft(zk_ij))
n_pots_for_interp = 6
r_for_interp = r[1:n_pots_for_interp+1]
zr_for_interp = zr_ij[:n_pots_for_interp]
poly_coefs = np.polyfit(r_for_interp, zr_for_interp, 3)
poly_f = np.poly1d(poly_coefs)
zr[i][j] = [poly_f(0)]
zr[i][j].extend(zr_ij)
return r, np.swapaxes(zr, 0, 2)
def test_rnn():
np.random.seed(0)
num_layers = 50
seq_length = num_layers * 2
batchsize = 2
vocab_size = 4
data = np.random.randint(0, vocab_size, size=(batchsize, seq_length), dtype=np.int32)
source, target = make_source_target_pair(data)
model = RNNModel(vocab_size, ndim_embedding=100, num_layers=num_layers, ndim_h=3, kernel_size=3, pooling="fo", zoneout=False, wgain=1, densely_connected=True)
with chainer.using_config("train", False):
np.random.seed(0)
model.reset_state()
Y = model(source).data
model.reset_state()
np.random.seed(0)
for t in range(source.shape[1]):
y = model.forward_one_step(source[:, :t+1]).data
target = np.swapaxes(np.reshape(Y, (batchsize, -1, vocab_size)), 1, 2)
target = np.reshape(np.swapaxes(target[:, :, t, None], 1, 2), (batchsize, -1))
assert np.sum((y - target) ** 2) == 0
print("t = {} OK".format(t))
def trim_image(img, resnet_mean):
h, w, c = img.shape
if c != 3:
raise Exception('There are gray scale image in data.')
if h < w:
w = (w * 256) / h
h = 256
else:
h = (h * 256) / w
w = 256
resized_img = skimage.transform.resize(img, (h, w), preserve_range=True)
cropped_img = resized_img[h // 2 - 112:h // 2 + 112, w // 2 - 112:w // 2 +
112, :]
transposed_img = np.swapaxes(np.swapaxes(cropped_img, 1, 2), 0, 1)
ivec = transposed_img - resnet_mean
return ivec[np.newaxis].astype('float32')
def prediction_to_image(prediction, reshape=False):
""" Converts a prediction map to the RGB image
Args:
prediction (array): the input map to convert
reshape (bool, optional): True if reshape the input from Caffe format
to numpy standard 2D array
Returns:
array: RGB-encoded array
"""
if reshape:
prediction = np.swapaxes(np.swapaxes(prediction, 0, 2), 0, 1)
image = np.zeros(prediction.shape[:2] + (3,), dtype='uint8')
for x in xrange(prediction.shape[0]):
for y in xrange(prediction.shape[1]):
image[x,y] = label_to_pixel(prediction[x,y])
return image
# In[6]:
def process_patches(images, net, transformer):
""" Process a patch through the neural network and extract the predictions
Args:
images (array list): list of images to process (length = batch_size)
net (obj): the Caffe Net
transformer (obj): the Caffe Transformer for preprocessing
"""
# caffe.io.load_image converts to [0,1], so our transformer sets it back to [0,255]
# but the skimage lib already works with [0, 255] so we convert it to float with img_as_float
data = np.zeros(net.blobs['data'].data.shape)
for i in range(len(images)):
data[i] = transformer.preprocess('data', img_as_float(images[i]))
net.forward(data=data)
output = net.blobs['conv1_1_D'].data[:len(images)]
output = np.swapaxes(np.swapaxes(output, 1, 3), 1, 2)
return output
def prediction_to_image(prediction, reshape=False):
""" Converts a prediction map to the RGB image
Args:
prediction (array): the input map to convert
reshape (bool, optional): True if reshape the input from Caffe format
to numpy standard 2D array
Returns:
array: RGB-encoded array
"""
if reshape:
prediction = np.swapaxes(np.swapaxes(prediction, 0, 2), 0, 1)
image = np.zeros(prediction.shape[:2] + (3,), dtype='uint8')
for x in xrange(prediction.shape[0]):
for y in xrange(prediction.shape[1]):
image[x,y] = label_to_pixel(prediction[x,y])
return image
# In[6]:
# Simple sliding window function
def process_patches(images, net, transformer):
""" Process a patch through the neural network and extract the predictions
Args:
images (array list): list of images to process (length = batch_size)
net (obj): the Caffe Net
transformer (obj): the Caffe Transformer for preprocessing
"""
# caffe.io.load_image converts to [0,1], so our transformer sets it back to [0,255]
# but the skimage lib already works with [0, 255] so we convert it to float with img_as_float
data = np.zeros(net.blobs['data'].data.shape)
for i in range(len(images)):
data[i] = transformer.preprocess('data', img_as_float(images[i]))
net.forward(data=data)
output = net.blobs['conv1_1_D'].data[:len(images)]
output = np.swapaxes(np.swapaxes(output, 1, 3), 1, 2)
return output
def generate(epoch):
latent = np.random.uniform(-1, 1, (batch_size, 100))
generated = (get_image(latent) + 1) / 2
manifold = np.zeros((32*8, 32*8, 3), dtype=theano.config.floatX)
for indx in range(8):
for indy in range(8):
current_img = np.swapaxes(generated[indx * 8 + indy], 0, 2)
current_img = np.swapaxes(current_img, 0, 1)
manifold[indx * 32:(indx+1) * 32, indy * 32:(indy+1) * 32, :] = current_img
manifold = np.asarray(manifold * 255, dtype='int32')
manifold = scipy.misc.toimage(manifold, cmin=0, cmax=255)
scipy.misc.imsave(result_folder + str(epoch) + '.png', manifold)
#================Train================#
# pretrain()
def test_seq2seq_inputs(self):
inp = np.array([[[1, 0], [0, 1], [1, 0]], [[0, 1], [1, 0], [0, 1]]])
out = np.array([[[0, 1, 0], [1, 0, 0]], [[1, 0, 0], [0, 1, 0]]])
with self.test_session() as session:
x = tf.placeholder(tf.float32, [2, 3, 2])
y = tf.placeholder(tf.float32, [2, 2, 3])
in_x, in_y, out_y = ops.seq2seq_inputs(x, y, 3, 2)
enc_inp = session.run(in_x, feed_dict={x.name: inp})
dec_inp = session.run(in_y, feed_dict={x.name: inp, y.name: out})
dec_out = session.run(out_y, feed_dict={x.name: inp, y.name: out})
# Swaps from batch x len x height to list of len of batch x height.
self.assertAllEqual(enc_inp, np.swapaxes(inp, 0, 1))
self.assertAllEqual(dec_inp, [[[0, 0, 0], [0, 0, 0]],
[[0, 1, 0], [1, 0, 0]],
[[1, 0, 0], [0, 1, 0]]])
self.assertAllEqual(dec_out, [[[0, 1, 0], [1, 0, 0]],
[[1, 0, 0], [0, 1, 0]],
[[0, 0, 0], [0, 0, 0]]])
def test_seq2seq_inputs(self):
inp = np.array([[[1, 0], [0, 1], [1, 0]], [[0, 1], [1, 0], [0, 1]]])
out = np.array([[[0, 1, 0], [1, 0, 0]], [[1, 0, 0], [0, 1, 0]]])
with self.test_session() as session:
x = tf.placeholder(tf.float32, [2, 3, 2])
y = tf.placeholder(tf.float32, [2, 2, 3])
in_x, in_y, out_y = ops.seq2seq_inputs(x, y, 3, 2)
enc_inp = session.run(in_x, feed_dict={x.name: inp})
dec_inp = session.run(in_y, feed_dict={x.name: inp, y.name: out})
dec_out = session.run(out_y, feed_dict={x.name: inp, y.name: out})
# Swaps from batch x len x height to list of len of batch x height.
self.assertAllEqual(enc_inp, np.swapaxes(inp, 0, 1))
self.assertAllEqual(dec_inp, [[[0, 0, 0], [0, 0, 0]],
[[0, 1, 0], [1, 0, 0]],
[[1, 0, 0], [0, 1, 0]]])
self.assertAllEqual(dec_out, [[[0, 1, 0], [1, 0, 0]],
[[1, 0, 0], [0, 1, 0]],
[[0, 0, 0], [0, 0, 0]]])
def fit_line(self, x, y):
# Takes x data (1d array) and y data (Nd array)
# last dimension of y array is fit dimension
# Solve linear least squares matrix equations for m, b of y = mx + b
# Returns m, b
# y_i = 1*b + x_i * m
# Generate X matrix (X_i = [1, x_i])
X = np.ones((len(x), 2))
X[:,1] = x[:]
# Solve "normal equations" for B that minimizes least sq
# B = C*y = {((X^T)X)^(-1) * (X^T)} y
C = np.linalg.inv(X.T.dot(X)).dot(X.T)
if len(y.shape) < 2:
B = C.dot(y)
else:
B = C.dot(np.swapaxes(y, -1, 1))
return B[1], B[0]
def swap_axis(imageobj, axis1, axis2):
""" Swap axis of image object
:param imageobj:
:param axis1:
:param axis2:
:return:
"""
resol, origin = affines.to_matvec(imageobj.get_affine())
resol = np.diag(resol).copy()
origin = origin
imageobj._dataobj = np.swapaxes(imageobj._dataobj, axis1, axis2)
resol[axis1], resol[axis2] = resol[axis2], resol[axis1]
origin[axis1], origin[axis2] = origin[axis2], origin[axis1]
affine = affines.from_matvec(np.diag(resol), origin)
reset_orient(imageobj, affine)
def set_mosaic_fig(data, dim, resol, slice_axis, scale):
""" Set environment for mosaic figure
"""
num_of_slice = dim[slice_axis]
num_height = int(np.sqrt(num_of_slice))
num_width = int(round(num_of_slice / num_height))
# Swap axis
data = np.swapaxes(data, slice_axis, 2)
resol[2], resol[slice_axis] = resol[slice_axis], resol[2]
dim[2], dim[slice_axis] = dim[slice_axis], dim[2]
# Check the size of each slice
size_height = num_height * dim[1] * resol[1] * scale / max(dim)
size_width = num_width * dim[0] * resol[0] * scale / max(dim)
# Figure generation
slice_grid = [num_of_slice, num_height, num_width]
size = [size_width, size_height]
return data, slice_grid, size
def resize_images(x, shape):
from scipy.misc import imresize
reszie_func = lambda x, shape: imresize(x, shape, interp='bilinear')
if x.ndim == 4:
def reszie_func(x, shape):
# x: 3D
# The color channel is the first dimension
tmp = []
for i in x:
tmp.append(imresize(i, shape).reshape((-1,) + shape))
return np.swapaxes(np.vstack(tmp).T, 0, 1)
imgs = []
for i in x:
imgs.append(reszie_func(i, shape))
return imgs