img = np.mean(np.array(img), axis=2) var_image = torch.autograd.Variable(torch.from_numpy(img.astype(np.float32)), volatile=True) var_image_reshape = var_image.view(1, 1, var_image.size(0), var_image.size(1)) AffNetPix = AffNetFast(PS=32) weightd_fname = '../../pretrained/AffNet.pth' checkpoint = torch.load(weightd_fname) AffNetPix.load_state_dict(checkpoint['state_dict']) AffNetPix.eval() HA = ScaleSpaceAffinePatchExtractor(mrSize=5.192, num_features=nfeats, border=5, num_Baum_iters=1, th=th, AffNet=AffNetPix) if USE_CUDA: HA = HA.cuda() var_image_reshape = var_image_reshape.cuda() with torch.no_grad(): LAFs, resp = HA(var_image_reshape) ells = LAFs2ell(LAFs.data.cpu().numpy()) np.savetxt(output_fname, ells, delimiter=' ', fmt='%10.10f') line_prepender(output_fname, str(len(ells))) line_prepender(output_fname, '1.0')
final_aff_m, final_pyr_idxs, final_level_idxs, PS=19, pyr_inv_idxs=pyr_inv_idxs) ### patches = self.extract_patches(scale_pyr, final_aff_m, final_pyr_idxs, final_level_idxs, PS=self.PS, pyr_inv_idxs=pyr_inv_idxs) return final_aff_m, patches, final_resp, scale_pyr HA = HessianAffinePatchExtractor(mrSize=5.0, num_features=3000, border=1, num_Baum_iters=5) aff, patches, resp, pyr = HA(var_image_reshape / 255.) LAFs = aff.data.cpu().numpy() LAFs[:, 0, 2] *= float(img.shape[1]) LAFs[:, 1, 2] *= float(img.shape[0]) LAFs[:, :2, :2] *= min(float(img.shape[0]), float(img.shape[0])) ells = LAFs2ell(LAFs) np.savetxt(output_fname, ells, delimiter=' ', fmt='%10.10f') #line_prepender(output_fname, str(len(ells))) #line_prepender(output_fname, '1.0')
descriptors_for_net = np.zeros((n_patches, 128)) bs = 128; outs = [] n_batches = n_patches / bs + 1 t = time.time() patches = patches.cuda() hardnet = hardnet.cuda() for batch_idx in range(n_batches): if batch_idx == n_batches - 1: if (batch_idx + 1) * bs > n_patches: end = n_patches else: end = (batch_idx + 1) * bs else: end = (batch_idx + 1) * bs data_a = patches[batch_idx * bs: end, :, :, :] out_a = hardnet(data_a) descriptors_for_net[batch_idx * bs: end,:] = out_a.data.cpu().numpy().reshape(-1, 128) print descriptors_for_net.shape et = time.time() - t print 'processing', et, et/float(n_patches), ' per patch' ells = convert_LAF_to_this_stupid_Oxford_ellipse_format(var_image_reshape.data.cpu().numpy()[0,0,:,:], LAFs) np.savetxt(output_fname, np.hstack([ells, descriptors_for_net]), delimiter=' ', fmt='%10.9f') ''' ells = LAFs2ell(var_image_reshape.data.cpu().numpy()[0, 0, :, :], LAFs) np.savetxt(output_fname, ells, delimiter=' ', fmt='%10.10f') line_prepender(output_fname, str(len(ells))) line_prepender(output_fname, '1.0')