def load_image_array_flowers(image_file, image_size):
img = skimage.io.imread(image_file)
# GRAYSCALE
if len(img.shape) == 2:
img_new = np.ndarray( (img.shape[0], img.shape[1], 3), dtype = 'uint8')
img_new[:,:,0] = img
img_new[:,:,1] = img
img_new[:,:,2] = img
img = img_new
img_resized = skimage.transform.resize(img, (image_size, image_size))
# FLIP HORIZONTAL WIRH A PROBABILITY 0.5
if random.random() > 0.5:
img_resized = np.fliplr(img_resized)
return img_resized.astype('float32')
python类fliplr()的实例源码
def transform(self, images):
if self._aug_flag:
transformed_images =\
np.zeros([images.shape[0], self._imsize, self._imsize, 3])
ori_size = images.shape[1]
for i in range(images.shape[0]):
h1 = np.floor((ori_size - self._imsize) * np.random.random())
w1 = np.floor((ori_size - self._imsize) * np.random.random())
cropped_image =\
images[i][w1: w1 + self._imsize, h1: h1 + self._imsize, :]
if random.random() > 0.5:
transformed_images[i] = np.fliplr(cropped_image)
else:
transformed_images[i] = cropped_image
return transformed_images
else:
return images
def zz(matrix, nb):
r"""Zig-zag traversal of the input matrix
:param matrix: input matrix
:param nb: number of coefficients to keep
:return: an array of nb coefficients
"""
flipped = np.fliplr(matrix)
rows, cols = flipped.shape # nb of columns
coefficient_list = []
for loop, i in enumerate(range(cols - 1, -rows, -1)):
anti_diagonal = np.diagonal(flipped, i)
# reversing even diagonals prioritizes the X resolution
# reversing odd diagonals prioritizes the Y resolution
# for square matrices, the information content is the same only when nb covers half of the matrix
# e.g. [ nb = n*(n+1)/2 ]
if loop % 2 == 0:
anti_diagonal = anti_diagonal[::-1] # reverse anti_diagonal
coefficient_list.extend([x for x in anti_diagonal])
# flattened = [val for sublist in coefficient_list for val in sublist]
return coefficient_list[:nb]
def transform(self, images):
if self._aug_flag:
transformed_images =\
np.zeros([images.shape[0], self._imsize, self._imsize, 3])
ori_size = images.shape[1]
for i in range(images.shape[0]):
h1 = np.floor((ori_size - self._imsize) * np.random.random())
w1 = np.floor((ori_size - self._imsize) * np.random.random())
cropped_image =\
images[i][w1: w1 + self._imsize, h1: h1 + self._imsize, :]
if random.random() > 0.5:
transformed_images[i] = np.fliplr(cropped_image)
else:
transformed_images[i] = cropped_image
return transformed_images
else:
return images
def update(self):
self._display_init()
x1, x2 = self.update_x1, self.update_x2
y1, y2 = self.update_y1, self.update_y2
region = self.buffer[y1:y2, x1:x2]
if self.v_flip:
region = numpy.fliplr(region)
if self.h_flip:
region = numpy.flipud(region)
buf_red = numpy.packbits(numpy.where(region == RED, 1, 0)).tolist()
if self.inky_version == 1:
buf_black = numpy.packbits(numpy.where(region == 0, 0, 1)).tolist()
else:
buf_black = numpy.packbits(numpy.where(region == BLACK, 0, 1)).tolist()
self._display_update(buf_black, buf_red)
self._display_fini()
def _make_data_gen(hypes, phase, data_dir):
"""Return a data generator that outputs image samples."""
if phase == 'train':
data_file = hypes['data']["train_file"]
elif phase == 'val':
data_file = hypes['data']["val_file"]
else:
assert False, "Unknown Phase %s" % phase
data_file = os.path.join(data_dir, data_file)
data = _load_gt_file(hypes, data_file)
for image, label in data:
if phase == 'val':
assert(False)
elif phase == 'train':
yield resize_input(hypes, image, label)
yield resize_input(hypes, np.fliplr(image), label)
def low_rank_align(X, Y, Cxy, d=None, mu=0.8):
"""Input: data matrices X,Y, correspondence matrix Cxy,
embedding dimension d, and correspondence weight mu
Output: embedded X and embedded Y
"""
nx, dx = X.shape
ny, dy = Y.shape
assert Cxy.shape==(nx,ny), \
'Correspondence matrix must be shape num_X_samples X num_Y_samples.'
C = np.fliplr(block_diag(np.fliplr(Cxy),np.fliplr(Cxy.T)))
if d is None:
d = min(dx,dy)
Rx = low_rank_repr(X,d)
Ry = low_rank_repr(Y,d)
R = block_diag(Rx,Ry)
tmp = np.eye(R.shape[0]) - R
M = tmp.T.dot(tmp)
L = laplacian(C)
eigen_prob = (1-mu)*M + 2*mu*L
_,F = eigh(eigen_prob,eigvals=(1,d),overwrite_a=True)
Xembed = F[:nx]
Yembed = F[nx:]
return Xembed, Yembed
def corrmtx(x,m):
"""
from https://github.com/cokelaer/spectrum/
like matlab corrmtx(x,'mod'), with a different normalization factor.
"""
x = np.asarray(x, dtype=float)
assert x.ndim == 1, '1-D only'
N = x.size
Tp = toeplitz(x[m:N], x[m::-1])
C = np.zeros((2*(N-m), m+1), dtype=x.dtype)
for i in range(0, N-m):
C[i] = Tp[i]
Tp = np.fliplr(Tp.conj())
for i in range(N-m, 2*(N-m)):
C[i] = Tp[i-N+m]
return C
def concurrence(state):
"""Calculate the concurrence.
Args:
state (np.array): a quantum state
Returns:
concurrence.
"""
rho = np.array(state)
if rho.ndim == 1:
rho = outer(state)
if len(state) != 4:
raise Exception("Concurence is not defined for more than two qubits")
YY = np.fliplr(np.diag([-1, 1, 1, -1]))
A = rho.dot(YY).dot(rho.conj()).dot(YY)
w = la.eigh(A, eigvals_only=True)
w = np.sqrt(np.maximum(w, 0))
return max(0.0, w[-1]-np.sum(w[0:-1]))
###############################################################
# Other.
###############################################################
read_data.py 文件源码
项目:human-pose-estimation-by-deep-learning
作者: HYPJUDY
项目源码
文件源码
阅读 43
收藏 0
点赞 0
评论 0
def _random_flip_lr(self, images, labels):
if(images.shape[0] != labels.shape[0]):
raise Exception("Batch size Error.")
rand_u = np.random.uniform(0.0, 1.0, images.shape[0])
rand_cond = rand_u > 0.5
o_images = np.zeros_like(images)
o_labels = np.zeros_like(labels)
for idx in xrange(images.shape[0]):
condition = rand_cond[idx]
if condition:
# "flip"
o_images[idx] = np.fliplr(images[idx])
o_labels[idx, ::2] = self.float_max - labels[idx, ::2]
o_labels[idx, 1::2] = labels[idx, 1::2]
else:
# "origin"
o_images[idx] = images[idx]
o_labels[idx] = labels[idx]
return o_images, o_labels
read_data.py 文件源码
项目:human-pose-estimation-by-deep-learning
作者: HYPJUDY
项目源码
文件源码
阅读 29
收藏 0
点赞 0
评论 0
def _batch_random_flip_lr(self, images, labels):
if(images.shape[0] != labels.shape[0]):
raise Exception("Batch size Error.")
rand_u = np.random.uniform(0.0, 1.0)
rand_cond = rand_u > 0.5
o_images = np.zeros_like(images)
o_labels = np.zeros_like(labels)
for idx in xrange(images.shape[0]):
condition = rand_cond
if condition:
# "flip"
o_images[idx] = np.fliplr(images[idx])
o_labels[idx, ::2] = self.float_max - labels[idx, ::2]
o_labels[idx, 1::2] = labels[idx, 1::2]
else:
# "origin"
o_images[idx] = images[idx]
o_labels[idx] = labels[idx]
return o_images, o_labels
def __init__(self, hmat, m, n, tper, k, out0):
hmat = np.asarray(hmat)
if hmat.shape != (2 * m + 1, n + 1):
raise ValueError('hmat shape = %s not compatible with M=%d, N=%d' %
(hmat.shape, m, n))
# use symmetry to fill in negative input frequency data.
fullh = np.empty((2 * m + 1, 2 * n + 1), dtype=complex)
fullh[:, n:] = hmat / (k * tper)
fullh[:, :n] = np.fliplr(np.flipud(fullh[:, n + 1:])).conj()
self.hmat = fullh
wc = 2.0 * np.pi / tper
self.m_col = np.arange(-m, m + 1) * (1.0j * wc)
self.n_col = np.arange(-n, n + 1) * (1.0j * wc / k)
self.m_col = self.m_col.reshape((-1, 1))
self.n_col = self.n_col.reshape((-1, 1))
self.tper = tper
self.k = k
self.outfun = interp.interp1d(out0[:, 0], out0[:, 1], bounds_error=True,
assume_sorted=True)
def rgb2gray(self, rgb, i=0):
if FLAGS.save_frames:
if self.thread_index == 0 and len(os.listdir(os.path.join(FLAGS.model_dir, "images"))) < 1000:
scipy.misc.imsave("%s/%i.png" % (os.path.join(FLAGS.model_dir, "images"), i), rgb["image"][0])
img = np.asarray(rgb["image"][0])[..., :3]
img = np.dot(img, [0.299, 0.587, 0.114])
img = scipy.misc.imresize(img, (84, 84)) / 255.0
#flip H
#
#img = np.fliplr(img)
return img
#return -np.dot(img, [0.299, 0.587, 0.114]) / 255.0 + 1.0
def sliceImages(inputImage, targetImage):
inputSlices = []
targetSlices = []
sliceSize = 32
for y in range(0,inputImage.shape[1]//sliceSize):
for x in range(0,inputImage.shape[0]//sliceSize):
inputSlice = inputImage[x*sliceSize:(x+1)*sliceSize,y*sliceSize:(y+1)*sliceSize]
targetSlice = targetImage[x*sliceSize//2:(x+1)*sliceSize//2,y*sliceSize//2:(y+1)*sliceSize//2]
# only add slices if they're not just empty space
# if (np.any(targetSlice)):
# Reweight smaller sizes
# for i in range(0,max(1,128//inputImage.shape[1])**2):
inputSlices.append(inputSlice)
targetSlices.append(targetSlice)
# inputSlices.append(np.fliplr(inputSlice))
# targetSlices.append(np.fliplr(targetSlice))
# inputSlices.append(np.flipud(inputSlice))
# targetSlices.append(np.flipud(targetSlice))
# naiveSlice = imresize(inputSlice, 0.5)
# deltaSlice = targetSlice - naiveSlice
# targetSlices.append(deltaSlice)
# return two arrays of images in a tuple
return (inputSlices, targetSlices)
def transform(patch, flip=False, mirror=False, rotations=[]):
"""Perform data augmentation on a patch.
Args:
patch (numpy array): The patch to be processed.
flip (bool, optional): Up/down symetry.
mirror (bool, optional): left/right symetry.
rotations (int list, optional) : rotations to perform (angles in deg).
Returns:
array list: list of augmented patches
"""
transformed_patches = [patch]
for angle in rotations:
transformed_patches.append(skimage.img_as_ubyte(skimage.transform.rotate(patch, angle)))
if flip:
transformed_patches.append(np.flipud(patch))
if mirror:
transformed_patches.append(np.fliplr(patch))
return transformed_patches
# In[4]:
def results(db, model, comp_func, k=1, expand_label=False):
if k < 1:
raise ValueError
score_matrix = generate_cross_scores(local_db, model, comp_func, expand_label)
labels = np.array(get_labels())
# Sort scores in descending order
top_score_ind = np.argsort(score_matrix[:, :score_matrix.shape[1]-2,], axis=1)
top_score_ind = np.fliplr(top_score_ind)
# Get top K guesses
y_hat = []
for i in range(k):
y_hat.append(labels[top_score_ind[:,i]])
y_hat = np.vstack(y_hat).T
y = score_matrix[:, score_matrix.shape[1]-1]
score_pool = []
for i in range(k):
score_pool.append((y == y_hat[:, i]).astype(int))
score_pool = np.vstack(score_pool).T
r = np.max(score_pool, axis=1)
return float(np.count_nonzero(r)) / float(len(r)), score_matrix
def show_file_images(filename, img_list):
fig = plt.figure()
#for 9 random images, print them
for img_num in range(0, 9):
random_num = random.randint(0, len(img_list))
img_name = img_list[random_num]
print('image name is ', img_name)
img = misc.imread(filename + img_name)
np_img = np.array(img)
flipped_img = np.fliplr(np_img)[60:160]
# print('img is ', img)
img = img[60:160]
fig.add_subplot(5, 5, img_num * 2 + 1)
plt.imshow(img)
fig.add_subplot(5, 5, img_num * 2 + 2)
plt.imshow(flipped_img)
plt.show()
def Gaussian2D(image, sigma, padding=0):
n, m = image.shape[0], image.shape[1]
tmp = np.zeros((n + padding, m + padding))
if tmp.shape[0] < 4:
raise ValueError('Image and padding too small')
if tmp.shape[1] < 4:
raise ValueError('Image and padding too small')
B, A = __gausscoeff(sigma)
tmp[:n, :m] = image
tmp = lfilter(B, A, tmp, axis=0)
tmp = np.flipud(tmp)
tmp = lfilter(B, A, tmp, axis=0)
tmp = np.flipud(tmp)
tmp = lfilter(B, A, tmp, axis=1)
tmp = np.fliplr(tmp)
tmp = lfilter(B, A, tmp, axis=1)
tmp = np.fliplr(tmp)
return tmp[:n, :m]
#-----------------------------------------------------------------------------
def num_attackers(node):
board = node.state
t_board = np.transpose(board)
f_board = np.fliplr(board)
total_attackers = 0
q = np.where(board == 1)
for i in range(len(q[0])):
a_x = q[0][i]
a_y = q[1][i]
point = board[a_x][a_y]
a_row = sum(board[a_x]) - point
a_col = sum(t_board[a_y]) - point
a_diag1 = sum(board.diagonal(a_y - a_x)) - point
a_diag2 = sum(f_board.diagonal(len(board) - a_x - a_y - 1)) - point
total_attackers += a_row + a_col + a_diag1 + a_diag2
return total_attackers
def loadBoxFile(self, box_name ):
box_data = np.loadtxt( box_name, comments="_" )
# box_data columns = [x_center, y_center, ..., ..., FigureOfMerit]
self.boxLen = box_data[0,2]
# In boxfiles coordinates are at the edges.
self.boxYX = np.fliplr( box_data[:,:2] )
# DEBUG: The flipping of the y-coordinate system is annoying...
print( "boxYX.shape = " + str(self.boxYX.shape) + ", len = " + str(self.boxLen) )
self.boxYX[:,0] = self.im.shape[0] - self.boxYX[:,0]
self.boxYX[:,1] += int( self.boxLen / 2 )
self.boxYX[:,0] -= int( self.boxLen/2)
try:
self.boxFoM = box_data[:,4]
clim = zorro.zorro_util.ciClim( self.boxFoM, sigma=2.5 )
self.boxFoM = zorro.zorro_util.normalize( np.clip( self.boxFoM, clim[0], clim[1] ) )
except:
self.boxFoM = np.ones( self.boxYX.shape[0] )
self.boxColors = plt.cm.gnuplot( self.boxFoM )
def loadStarFile(self, box_name ):
box_data = np.loadtxt( box_name, comments="_", skiprows=5 )
# box_data columns = [x_center, y_center, ..., ..., FigureOfMerit]
# In star files coordinates are centered
self.boxYX = np.fliplr( box_data[:,:2] )
# DEBUG: The flipping of the y-coordinate system is annoying...
self.boxYX[:,0] = self.im.shape[0] - self.boxYX[:,0]
# There's no box size information in a star file so we have to use a guess
self.boxLen = 224
#self.boxYX[:,1] -= int( self.boxLen / 2 )
#self.boxYX[:,0] += int( self.boxLen / 2 )
try:
self.boxFoM = box_data[:,4]
clim = zorro.zorro_util.ciClim( self.boxFoM, sigma=2.5 )
self.boxFoM = zorro.zorro_util.normalize( np.clip( self.boxFoM, clim[0], clim[1] ) )
except:
self.boxFoM = np.ones( self.boxYX.shape[0] )
self.boxColors = plt.cm.gnuplot( self.boxFoM )
def preprocess_A_and_B(img_A, img_B, load_size=286, fine_size=256, flip=True, is_test=False):
if is_test:
img_A = scipy.misc.imresize(img_A, [fine_size, fine_size])
img_B = scipy.misc.imresize(img_B, [fine_size, fine_size])
else:
img_A = scipy.misc.imresize(img_A, [load_size, load_size])
img_B = scipy.misc.imresize(img_B, [load_size, load_size])
h1 = int(np.ceil(np.random.uniform(1e-2, load_size-fine_size)))
w1 = int(np.ceil(np.random.uniform(1e-2, load_size-fine_size)))
img_A = img_A[h1:h1+fine_size, w1:w1+fine_size]
img_B = img_B[h1:h1+fine_size, w1:w1+fine_size]
if flip and np.random.random() > 0.5:
img_A = np.fliplr(img_A)
img_B = np.fliplr(img_B)
return img_A, img_B
# -----------------------------
# new added function for lip dataset, saving pose
def process(self, im):
# if side is right flip so it becomes right
if self.side != 'left':
im = np.fliplr(im)
# slope of the perspective
slope = tan(radians(self.degrees))
(h, w, _) = im.shape
matrix_trans = np.array([[1, 0, 0],
[-slope/2, 1, slope * h / 2],
[-slope/w, 0, 1 + slope]])
trans = ProjectiveTransform(matrix_trans)
img_trans = warp(im, trans)
if self.side != 'left':
img_trans = np.fliplr(img_trans)
return img_trans
def _applyImageFlips(image, flips):
'''
Apply left-right and up-down flips to an image
Args:
image (numpy array 2D/3D): image to be flipped
flips (tuple):
[0]: Boolean to flip horizontally
[1]: Boolean to flip vertically
Returns:
Flipped image
'''
image = np.fliplr(image) if flips[0] else image
image = np.flipud(image) if flips[1] else image
return image
def _read_image_as_array(path, dtype, load_size, crop_size, flip):
f = Image.open(path)
A, B = numpy.array_split(numpy.asarray(f), 2, axis=1)
if hasattr(f, 'close'):
f.close()
A = _resize(A, load_size, Image.BILINEAR, dtype)
B = _resize(B, load_size, Image.NEAREST, dtype)
sx, sy = numpy.random.randint(0, load_size-crop_size, 2)
A = _crop(A, sx, sy, crop_size)
B = _crop(B, sx, sy, crop_size)
if flip and numpy.random.rand() > 0.5:
A = numpy.fliplr(A)
B = numpy.fliplr(B)
return A.transpose(2, 0, 1), B.transpose(2, 0, 1)
def _create_mesh(self):
log.debug('Creating mesh for box primitive')
box = self._unit_box
vertices, faces, normals = box.vertices, box.faces, box.face_normals
vertices = points.transform_points(vertices * self.box_extents,
self.box_transform)
normals = np.dot(self.box_transform[0:3,0:3],
normals.T).T
aligned = windings_aligned(vertices[faces[:1]], normals[:1])[0]
if not aligned:
faces = np.fliplr(faces)
# for a primitive the vertices and faces are derived from other information
# so it goes in the cache, instead of the datastore
self._cache['vertices'] = vertices
self._cache['faces'] = faces
self._cache['face_normals'] = normals
def retrieve2file(self, out_file, numn=0, nump=0):
"""highly customised for hardsel"""
ids = self.retrieve()
ret_labels = self.label_src[ids]
rel = ret_labels == self.label_q
#include/exclude the relevant in hard pos/neg selection
pos = ids[rel].reshape([rel.shape[0],-1])
pos = np.fliplr(pos) #hard positive
neg = ids[~rel].reshape([rel.shape[0],-1]) #hard negative
if nump > 0 and nump < pos.shape[1]:
pos = pos[:,0:nump]
if numn > 0 and numn < neg.shape[1]:
neg = neg[:,0:numn]
if out_file.endswith('.npz'):
np.savez(out_file, pos = pos, neg = neg)
P = np.cumsum(rel,axis=1) / np.arange(1,rel.shape[1]+1,dtype=np.float32)[None,...]
AP = np.sum(P*rel,axis=1) / (rel.sum(axis=1) + np.finfo(np.float32).eps)
mAP = AP.mean()
return mAP
def preprocess_A_and_B(img_A, img_B, load_size=286, fine_size=256, flip=True, is_test=False):
if is_test:
img_A = scipy.misc.imresize(img_A, [fine_size, fine_size])
img_B = scipy.misc.imresize(img_B, [fine_size, fine_size])
else:
img_A = scipy.misc.imresize(img_A, [load_size, load_size])
img_B = scipy.misc.imresize(img_B, [load_size, load_size])
h1 = int(np.ceil(np.random.uniform(1e-2, load_size-fine_size)))
w1 = int(np.ceil(np.random.uniform(1e-2, load_size-fine_size)))
img_A = img_A[h1:h1+fine_size, w1:w1+fine_size]
img_B = img_B[h1:h1+fine_size, w1:w1+fine_size]
if flip and np.random.random() > 0.5:
img_A = np.fliplr(img_A)
img_B = np.fliplr(img_B)
return img_A, img_B
# -----------------------------
def preprocess_A_and_B(img_A, img_B, load_size=286, fine_size=256, flip=True, is_test=False):
if is_test:
img_A = scipy.misc.imresize(img_A, [fine_size, fine_size])
img_B = scipy.misc.imresize(img_B, [fine_size, fine_size])
else:
img_A = scipy.misc.imresize(img_A, [load_size, load_size])
img_B = scipy.misc.imresize(img_B, [load_size, load_size])
h1 = int(np.ceil(np.random.uniform(1e-2, load_size - fine_size)))
w1 = int(np.ceil(np.random.uniform(1e-2, load_size - fine_size)))
img_A = img_A[h1:h1 + fine_size, w1:w1 + fine_size]
img_B = img_B[h1:h1 + fine_size, w1:w1 + fine_size]
if flip and np.random.random() > 0.5:
img_A = np.fliplr(img_A)
img_B = np.fliplr(img_B)
return img_A, img_B
# DEFINE OUR SAMPLING FUNCTIONS
# -------------------------------------------------------
def update(self):
agents = self.simulation.agents.array
field = self.simulation.field
for target in range(len(field.targets)):
has_target = agents['target'] == target
if not has_target.size:
continue
mgrid, distance_map, direction_map = field.navigation_to_target(
target, self.step, self.radius, self.strength)
# Flip x and y to array index i and j
indices = np.fliplr(mgrid.indicer(agents[has_target]['position']))
new_direction = getdefault(
indices, direction_map, agents[has_target]['target_direction'])
agents['target_direction'][has_target] = new_direction