Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
#### 第二种方法
base_feat, rois = input
pooled_height = 2
maxratio = (rois[:, 3] - rois[:, 1]) / (rois[:, 4] - rois[:, 2])
maxratio = maxratio.max().item()
pooled_width = math.ceil(pooled_height * maxratio)
pooling = ocr_roi_pooling(pooled_height, pooled_width, 1.0, 1.0) # height_spatial, width_spatial
# rois传入的是除以宽度和高度之后的比例
b, c, h, w = base_feat.size()
rois[:, [1, 3]] *= w
rois[:, [2, 4]] *= h
base_feat = cp.array(base_feat.cpu().detach().numpy(), dtype=np.float32)
rois = cp.array(rois.cpu().numpy(), dtype=np.float32)
pooled_feat2 = pooling(base_feat, rois)
# # 利用rnn进行序列学习
b, c, h, w = pooled_feat2.size()
rnn_input = pooled_feat2.view(b, -1, w)
rnn_input = rnn_input.permute(2, 0, 1)
preds = self.rnn(rnn_input)
return preds
Q = cupy.asarray(Q)
# Perform power iterations with Q to further 'imprint' the top
# singular vectors of M in Q
for i in range(n_iter):
Q = cupy.dot(M, Q)
Q = cupy.dot(M.T, Q)
# Sample the range of M using by linear projection of Q. Extract an orthonormal basis
Q, _ = cupy.linalg.qr(cupy.dot(M, Q), mode='reduced')
# project M to the (k + p) dimensional space using the basis vectors
B = cupy.dot(Q.T, M)
B = cupy.array(B)
Q = cupy.array(Q)
# compute the SVD on the thin matrix: (k + p) wide
Uhat, s, V = cupy.linalg.svd(B, full_matrices=False, compute_uv=True)
del B
U = cupy.dot(Q, Uhat)
if transpose:
# transpose back the results according to the input convention
return V[:n_components, :].T, s[:n_components], U[:,
:n_components].T
else:
return U[:, :n_components], s[:n_components], V[:n_components, :]
elif lib == 'pytorch':
M_gpu = torch.Tensor.cuda(torch.from_numpy(M.astype('float32')))
# Generating normal random vectors with shape: (M.shape[1], n_random)
[124, 136, 148, 160],
[88, 96, 104, 112],
[56, 64, 72, 80],
[36, 40, 44, 48],
[20, 24, 28, 32],
[4, 8, 6, 10, 12, 16]]
strides = [128, 85.3333, 64, 32, 16, 8, 4]
# test = init_template_f_in(scale_table, [128, 85.3333, 64, 32, 16, 8, 4])
gt_matrix = np.array([[10, 10, 110, 110], [200, 200, 250, 250], [10, 10, 110, 110], [200, 200, 250, 250],
[10, 10, 110, 110], [200, 200, 250, 250], [10, 10, 110, 110], [200, 200, 250, 250]],
dtype=np.int32)
t0 = time.time()
test1 = project_feature_map_matrix(gt_matrix, scale_table, strides)
print(time.time() - t0)
gt_matrix_cus = cp.array([[10, 10, 110, 110], [200, 200, 250, 250], [10, 10, 110, 110], [200, 200, 250, 250],
[10, 10, 110, 110], [200, 200, 250, 250], [10, 10, 110, 110], [200, 200, 250, 250],
[10, 10, 110, 110], [200, 200, 250, 250], [10, 10, 110, 110], [200, 200, 250, 250],
[10, 10, 110, 110], [200, 200, 250, 250], [10, 10, 110, 110], [200, 200, 250, 250],
[10, 10, 110, 110], [200, 200, 250, 250], [10, 10, 110, 110], [200, 200, 250, 250],
[10, 10, 110, 110], [200, 200, 250, 250], [10, 10, 110, 110], [200, 200, 250, 250],
[10, 10, 110, 110], [200, 200, 250, 250], [10, 10, 110, 110], [200, 200, 250, 250],
[10, 10, 110, 110], [200, 200, 250, 250], [10, 10, 110, 110], [200, 200, 250, 250],
[10, 10, 110, 110], [200, 200, 250, 250], [10, 10, 110, 110], [200, 200, 250, 250],
[10, 10, 110, 110], [200, 200, 250, 250], [10, 10, 110, 110], [200, 200, 250, 250]],
dtype=cp.int32)
cls_f_in = cp.array(init_template_f_in(scale_table, strides))
combine_in_gt = cp.zeros((1, 7, 128, 128, 6, 8), dtype=cp.int32)
print('#############')
t1 = time.time()
for gt_matrix_cu in gt_matrix_cus:
gts_index = project_feature_map_matrix_cupy(cls_f_in, gt_matrix_cu, combine_in_gt, 0)
if embedding_file is None:
# Initialize the embeddings
self.means = 2 * self.scale * (cp.random.rand(n_points, n_dim) - 0.5)
self.vars, _ = wb.batch_sqrtm(wb.wishart(n_points, n_dim, 2 * n_dim) / (2 * n_dim))
self.c_means = 2 * self.scale * (cp.random.rand(n_points, n_dim) - 0.5)
self.c_vars, _ = wb.batch_sqrtm(wb.wishart(n_points, n_dim, 2 * n_dim) / (2 * n_dim))
else:
with open(embedding_file) as embed_file:
embedds = pkl.load(embed_file)
self.means = cp.array(embedds['means'])
self.c_means = cp.array(embedds['c_means'])
self.vars = cp.array(embedds['vars'])
self.c_vars = cp.array(embedds['c_vars'])
# The unknown word should have a 0 embedding
if self.unknown_words:
self.means[0] = cp.zeros(n_dim)
self.c_means[0] = cp.zeros(n_dim)
self.vars[0] = cp.zeros((n_dim, n_dim))
self.c_vars[0] = cp.zeros((n_dim, n_dim))
self.means_adagrad = cp.zeros_like(self.means)
self.c_means_adagrad = cp.zeros_like(self.c_means)
self.vars_adagrad = cp.zeros_like(self.vars)
self.c_vars_adagrad = cp.zeros_like(self.c_vars)
if not sep_input_output:
self.c_means = self.means
def makevar(x):
return Variable(xp.array([x], dtype=xp.int32))
fpantie = Image.open('./converted/Fourier/' + fname)
fimg_origin = np.fft.ifftshift(np.exp(np.array(fpantie,dtype=np.float64) / np.max(np.array(fpantie)) * fmax + fmin))
mask = (np.array(Image.open('./dream/' + pantie))[:, :, 3] > 0).astype(np.float32)[:, :, None]
[r, c, d] = fimg_origin.shape
img = np.fft.ifft2(fimg_origin * np.exp(2j * np.pi * np.random.rand(r, c, d)), axes=(0, 1)) # Initialize the phase
if args.hio:
mask_inv = 1 - mask
previous_img = np.copy(img)
for i in range(args.itr):
if args.hio:
fimg = np.fft.fft2((np.abs(img) * mask + (np.abs(previous_img) - args.beta * np.abs(img)) * mask_inv) * np.exp(1j * np.angle(img)), axes=(0, 1))
previous_img = np.copy(img)
else: # Error reduction
fimg = np.fft.fft2(np.abs(img) * mask * np.exp(1j * np.angle(img)), axes=(0, 1))
img = np.fft.ifft2(fimg_origin * np.exp(1j * np.angle(fimg)), axes=(0, 1))
if np.max(np.array(fpantie)) == 255:
img = (norm_img(np.abs(img)) * 255).astype(np.uint8)
else:
img = (norm_img(np.abs(img)) * 65535).astype(np.uint16)
if args.gpu:
img = np.asnumpy(img)
if args.hio:
outname = './converted/Fourier_inv/' + pantie[:-4] + '_hio.png'
else:
outname = './converted/Fourier_inv/' + pantie
restored_pantie = Image.fromarray(img)
restored_pantie.save(outname)
def t2c(variable):
npa = variable.data.cpu().numpy()
return cp.array(npa)
def tensor(data, dtype=cp.float32):
return cp.array(data, dtype=dtype)
def get_pmi_matrix_gpu(matrix, root):
import cupy as cp
rows, cols = matrix.shape
item_rated = cp.array(matrix.sum(axis=0))
pmi_matrix = []
nnz = matrix.nnz
for i in tqdm(xrange(rows)):
row_index, col_index = matrix[i].nonzero()
if len(row_index) > 0:
values = cp.asarray(item_rated[:, col_index]).flatten()
values = cp.maximum(cp.log(rows/cp.power(values, root)), 0)
pmi_matrix.append(sparse.coo_matrix((cp.asnumpy(values), (row_index, col_index)), shape=(1, cols)))
else:
pmi_matrix.append(sparse.coo_matrix((1, cols)))
return sparse.vstack(pmi_matrix)