Skip to content

Commit

Permalink
debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
tomrunia committed Dec 12, 2018
1 parent 55dd8b5 commit 8d630fb
Show file tree
Hide file tree
Showing 4 changed files with 205 additions and 82 deletions.
3 changes: 3 additions & 0 deletions examples/compare_both.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@
# Also moves the example to CPU and NumPy
coeff_torch = utils.extract_from_batch(coeff_torch, 0)

cv2.waitKey(0)
exit()

################################################################################
# Check correctness

Expand Down
69 changes: 69 additions & 0 deletions examples/debug_reconstruction.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# MIT License
#
# Copyright (c) 2018 Tom Runia
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to conditions.
#
# Author: Tom Runia
# Date Created: 2018-12-12

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import numpy as np
import cv2
import cortex.vision

numpy_outdft = np.load('./assets/numpy_outdft_real.npy') + 1j*np.load('./assets/numpy_outdft_imag.npy')
torch_outdft = np.load('./assets/torch_outdft_real.npy') + 1j*np.load('./assets/torch_outdft_imag.npy')

print('allclose, real', np.allclose(numpy_outdft.real, torch_outdft.real, atol=1e-2))
print('allclose, imag', np.allclose(numpy_outdft.imag, torch_outdft.imag, atol=1e-2))

real, imag = numpy_outdft.real, numpy_outdft.imag
print(' [numpy] outdft real ({:.3f}, {:.3f}, {:.3f})'.format(
real[0:20,0:20].min().item(), real[0:20,0:20].mean().item(), real[0:20,0:20].max().item()
))
print(' [numpy] outdft imag ({:.3f}, {:.3f}, {:.3f})'.format(
imag[0:20,0:20].min().item(), imag[0:20,0:20].mean().item(), imag[0:20,0:20].max().item()
))

real, imag = torch_outdft.real, torch_outdft.imag
print(' [torch] outdft real ({:.3f}, {:.3f}, {:.3f})'.format(
real[0:20,0:20].min().item(), real[0:20,0:20].mean().item(), real[0:20,0:20].max().item()
))
print(' [torch] outdft imag ({:.3f}, {:.3f}, {:.3f})'.format(
imag[0:20,0:20].min().item(), imag[0:20,0:20].mean().item(), imag[0:20,0:20].max().item()
))

numpy_reconstruction = np.fft.ifftshift(numpy_outdft)
numpy_reconstruction = np.fft.ifft2(numpy_reconstruction)
numpy_reconstruction = numpy_reconstruction.real
real = numpy_reconstruction
print(' [numpy] outdft real ({:.3f}, {:.3f}, {:.3f})'.format(
real[0:20,0:20].min().item(), real[0:20,0:20].mean().item(), real[0:20,0:20].max().item()
))

torch_reconstruction = np.fft.ifftshift(torch_outdft)
torch_reconstruction = np.fft.ifft2(torch_reconstruction)
torch_reconstruction = torch_reconstruction.real
real = torch_reconstruction
print(' [torch] outdft real ({:.3f}, {:.3f}, {:.3f})'.format(
real[0:20,0:20].min().item(), real[0:20,0:20].mean().item(), real[0:20,0:20].max().item()
))

cv2.imshow('numpy phase', np.angle(numpy_outdft))
cv2.imshow('numpy imag', np.abs(numpy_outdft))
cv2.imshow('torch phase', np.angle(torch_outdft))
cv2.imshow('torch imag', np.abs(torch_outdft))

cv2.imshow('numpy reconstruction', numpy_reconstruction.astype(np.uint8))
cv2.imshow('torch reconstruction', torch_reconstruction.astype(np.uint8))

cv2.waitKey(0)
86 changes: 61 additions & 25 deletions steerable/SCFpyr_NumPy.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,46 +164,82 @@ def _build_levels(self, lodft, log_rad, angle, Xrcos, Yrcos, height):

return coeff

################################################################################
# Reconstruction to Image
############################################################################
########################### RECONSTRUCTION #################################
############################################################################

def reconstruct(self, coeff):

if self.nbands != len(coeff[1]):
raise Exception("Unmatched number of orientations")

M, N = coeff[0].shape
log_rad, angle = math_utils.prepare_grid(M, N)
height, width = coeff[0].shape
log_rad, angle = math_utils.prepare_grid(height, width)

Xrcos, Yrcos = math_utils.rcosFn(1, -0.5)
Yrcos = np.sqrt(Yrcos)
YIrcos = np.sqrt(np.abs(1 - Yrcos*Yrcos))
YIrcos = np.sqrt(np.abs(1 - Yrcos**2))

lo0mask = pointOp(log_rad, YIrcos, Xrcos)
hi0mask = pointOp(log_rad, Yrcos, Xrcos)

tempdft = self._reconstruct_levels(coeff[1:], log_rad, Xrcos, Yrcos, angle)

hidft = np.fft.fftshift(np.fft.fft2(coeff[0]))

real, imag = hidft.real, hidft.imag
print(' [numpy] level final. hidft real ({:.3f}, {:.3f}, {:.3f})'.format(
real[0:20,0:20].min().item(), real[0:20,0:20].mean().item(), real[0:20,0:20].max().item()
))
print(' [numpy] level final. hidft imag ({:.3f}, {:.3f}, {:.3f})'.format(
imag[0:20,0:20].min().item(), imag[0:20,0:20].mean().item(), imag[0:20,0:20].max().item()
))

outdft = tempdft * lo0mask + hidft * hi0mask

reconstruction = np.fft.ifft2(np.fft.ifftshift(outdft))
reconstruction = reconstruction.real.astype(int)
real, imag = outdft.real, outdft.imag
np.save('./assets/numpy_outdft_real.npy', real)
np.save('./assets/numpy_outdft_imag.npy', imag)

print(' [numpy] level final. outdft real ({:.3f}, {:.3f}, {:.3f})'.format(
real[0:20,0:20].min().item(), real[0:20,0:20].mean().item(), real[0:20,0:20].max().item()
))
print(' [numpy] level final. outdft imag ({:.3f}, {:.3f}, {:.3f})'.format(
imag[0:20,0:20].min().item(), imag[0:20,0:20].mean().item(), imag[0:20,0:20].max().item()
))

import cv2
import cortex.vision

reconstruction = np.fft.ifftshift(outdft)

real, imag = reconstruction.real, reconstruction.imag
print(' [numpy] level final. ifftshift real ({:.3f}, {:.3f}, {:.3f})'.format(
real[0:20,0:20].min().item(), real[0:20,0:20].mean().item(), real[0:20,0:20].max().item()
))
print(' [numpy] level final. ifftshift imag ({:.3f}, {:.3f}, {:.3f})'.format(
imag[0:20,0:20].min().item(), imag[0:20,0:20].mean().item(), imag[0:20,0:20].max().item()
))

real, imag = reconstruction.real, reconstruction.imag
cv2.imshow('numpy real', real)
cv2.imshow('numpy imag', imag)

reconstruction = np.fft.ifft2(reconstruction)
reconstruction = reconstruction.real

print(' [numpy] level final. reconstruction real ({:.3f}, {:.3f}, {:.3f})'.format(
reconstruction.min(), reconstruction.mean(), reconstruction.max()
))

return reconstruction

def _reconstruct_levels(self, coeff, log_rad, Xrcos, Yrcos, angle):

print('[numpy] Call to _reconstruct_levels. remaining = {rem}'.format(rem=len(coeff)))

if len(coeff) == 1:
print('[numpy] len(coeff)==1')
print('[numpy] coeff[0].shape', coeff[0].shape, coeff[0].dtype)
dft = np.fft.fft2(coeff[0])
print('[torch] dft after fft', dft.shape, dft.dtype)
tmp = np.fft.fftshift(dft)
print('[torch] dft after fftshift', dft.shape, dft.dtype)
print('[numpy] here!!')
return tmp
dft = np.fft.fftshift(dft)
return dft

Xrcos = Xrcos - np.log2(self.scale_factor)

Expand All @@ -228,6 +264,15 @@ def _reconstruct_levels(self, coeff, log_rad, Xrcos, Yrcos, angle):
banddft = np.fft.fftshift(np.fft.fft2(coeff[0][b]))
orientdft = orientdft + np.power(np.complex(0, 1), order) * banddft * anglemask * himask

real = orientdft.real
imag = orientdft.imag
print(' [numpy] levels remaining {}. orientdft real ({:.3f}, {:.3f}, {:.3f})'.format(
len(coeff), real.min().item(), real.mean().item(), real.max().item()
))
print(' [numpy] levels remaining {}. orientdft real ({:.3f}, {:.3f}, {:.3f})'.format(
len(coeff), imag.min().item(), imag.mean().item(), imag.max().item()
))

####################################################################
########## Lowpass component are upsampled and convoluted ##########
####################################################################
Expand All @@ -245,18 +290,9 @@ def _reconstruct_levels(self, coeff, log_rad, Xrcos, Yrcos, angle):

nresdft = self._reconstruct_levels(coeff[1:], nlog_rad, Xrcos, Yrcos, nangle)

print(' [numpy] nresdft', nresdft.shape, nresdft.dtype)
print(' [numpy] lomask', lomask.shape, lomask.dtype)

resdft = np.zeros(dims, 'complex')
print(' [numpy] resdft', resdft.shape, resdft.dtype)
print(' [numpy] lostart[0] - loend[0]', lostart[0], loend[0])
print(' [numpy] lostart[1] - loend[1]', lostart[1], loend[1])

resdft[lostart[0]:loend[0], lostart[1]:loend[1]] = nresdft * lomask



return resdft + orientdft


Expand Down
Loading

0 comments on commit 8d630fb

Please sign in to comment.