Commit e6e408ff authored by emanueledalsasso's avatar emanueledalsasso
Browse files

added function to read CoSAR data from Airbus website

parent f52f1cbd
%% Cell type:markdown id: tags:
**[Download the notebook](https://gitlab.telecom-paris.fr/RING/MERLIN/-/raw/master/MERLIN-TSX-spotlight-test.ipynb?inline=false) and then import it under Google Colab**
<a href="https://colab.research.google.com/" target="_parent"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/></a>
%% Cell type:markdown id: tags:
# As if by magic: self-supervised training of despeckling networks with MERLIN
## Emanuele Dalsasso, Loïc Denis, Florence Tupin
Please note that the training set is only composed of **TerraSAR-X** SAR images **acquired in High Resolution SPOTLIGHT (HS) mode**, thus this testing code is specific to this data.
%% Cell type:markdown id: tags:
## 0. Enable GPU and save copy on Drive to enable editing
## 0.1. Enable GPU and save copy on Drive to enable editing
Runtime -> Change runtime type -> Hardware accelerator: GPU
File -> Save a copy in Drive
%% Cell type:markdown id: tags:
## 0.2. Fetch image data and upload it on Colab
%% Cell type:code id: tags:
``` python
def cos2mat(imgName):
print('Converting CoSAR to numpy array of size [ncolumns,nlines,2]')
try :
fin=open(imgName,'rb');
except IOError:
legx=imgName+': it is a not openable file'
print(legx)
print(u'failed to call cos2mat')
return 0,0,0,0
ibib=struct.unpack(">i",fin.read(4))[0]
irsri=struct.unpack(">i",fin.read(4))[0]
irs=struct.unpack(">i",fin.read(4))[0]
ias=struct.unpack(">i",fin.read(4))[0]
ibi=struct.unpack(">i",fin.read(4))[0]
irtnb=struct.unpack(">i",fin.read(4))[0]
itnl=struct.unpack(">i",fin.read(4))[0]
nlig = struct.unpack(">i",fin.read(4))[0]
ncoltot = int(irtnb/4)
ncol=ncoltot-2
nlig=ias
print(u'Reading image in CoSAR format. ncolumns=%d nlines=%d'%(ncol,nlig))
firm=np.zeros(4*ncoltot, dtype=np.byte() )
imgcxs = np.empty([nlig, ncol], dtype=np.complex64())
fin.seek(0)
firm = fin.read(4*ncoltot)
firm = fin.read(4*ncoltot)
firm = fin.read(4*ncoltot)
firm = fin.read(4*ncoltot)
#
for iut in range(nlig):
firm = fin.read(4*ncoltot)
imgligne = np.ndarray( 2*ncoltot, '>h', firm)
imgcxs[iut,:]=imgligne[4:2*ncoltot:2]+1j*imgligne[5:2*ncoltot:2]
print('[:,:,0] contains the real part of the SLC image data')
print('[:,:,1] contains the imaginary part of the SLC image data')
return np.stack((np.real(imgcxs),np.imag(imgcxs)),axis=2)
```
%% Cell type:markdown id: tags:
## 1. Download network weights and code
%% Cell type:code id: tags:
``` python
from google_drive_downloader import GoogleDriveDownloader as gdd
gdd.download_file_from_google_drive(file_id='1WaqN29XOpfOkcWWkgCI1yF7OYcYrIR2i',
dest_path='./MERLIN-TSX-spotlight-test.zip',
unzip=True)
```
%% Cell type:markdown id: tags:
## 2. Install compatible version of tensorflow
%% Cell type:code id: tags:
``` python
!pip uninstall -y tensorflow
```
%% Cell type:code id: tags:
``` python
!pip install tensorflow-gpu==1.13.1
```
%% Cell type:markdown id: tags:
## 3. Test on real data
Some **TerraSAR-X Spotlight** images in **Single-Look Complex (SLC)** format can be found in the folder _/content/MERLIN-TSX-spotlight-test/test_data/_
To test on custom data, upload your SLC images in a numpy array with shape [ydim, xdim, 2] (where [:,:,0] contains the **real part** and [:,:,1] contains the **imaginary part**) in the folder _/content/MERLIN-TSX-spotlight-test/test_data/_
Results are stored in _/content/test_. For each image data, the following files are produced in output:
- the imaginary part $a$
- the real part $b$
- the noisy image in amplitude format: $A=\sqrt{a^2+b^2}$, where $a$ and $b$ are the real and imaginary part of the single-look complex data, respectively
- the squared root $\sqrt{\hat{R}_a}$ of the reflectivity estimated from the real part: $f_{CNN}(a)=\hat{R}_a$
- the squared root $\sqrt{\hat{R}_b}$ of the reflectivity estimated from the imaginary part: $f_{CNN}(b)=\hat{R}_b$
- the denoised image in amplitude format, obtained by averaging the two intermediate estimations: $\sqrt{\hat{R}}=\sqrt{\frac{\hat{R}_a+\hat{R}_b}{2}}$
For each image data, the corresponding _png_ file is generated as follows. A threshold $t$ is estimated (or pre-estimated) on the noisy image: $t = \mu_A+3\sigma_A$, with $\mu_A$ the mean of $A$ and $\sigma_A$ its standard deviation. This threshold is applied to each image to reduce SAR images long tail. The thresholded dynamic is then shrinked between 0 and 255 for visualization purposes. To produce the _png_ file of the real and imaginary part, $a\sqrt{2}$ and $b\sqrt{2}$ are plotted.
At each time a test is run, clean the _/content/test_ directory otherwise the results will be overwritten.
%% Cell type:code id: tags:
``` python
!python /content/MERLIN-TSX-spotlight-test/main.py
```
%% Cell type:markdown id: tags:
When image dimension exeeds 256, the U-Net is scanned over the image with a default stride of 64 pixels. To change it to a custom value, run the cell below (here it is set to 32, allowing more quality at the cost of a greater runtime)
%% Cell type:code id: tags:
``` python
!python /content/MERLIN-TSX-spotlight-test/main.py --stride_size=32
```
......
def cos2mat(imgName):
print('Converting CoSAR to numpy array of size [ncolumns,nlines,2]')
try:
fin = open(imgName, 'rb');
except IOError:
legx = imgName + ': it is a not openable file'
print(legx)
print(u'failed to call cos2mat')
return 0, 0, 0, 0
ibib = struct.unpack(">i", fin.read(4))[0]
irsri = struct.unpack(">i", fin.read(4))[0]
irs = struct.unpack(">i", fin.read(4))[0]
ias = struct.unpack(">i", fin.read(4))[0]
ibi = struct.unpack(">i", fin.read(4))[0]
irtnb = struct.unpack(">i", fin.read(4))[0]
itnl = struct.unpack(">i", fin.read(4))[0]
nlig = struct.unpack(">i", fin.read(4))[0]
ncoltot = int(irtnb / 4)
ncol = ncoltot - 2
nlig = ias
print(u'Reading image in CoSAR format. ncolumns=%d nlines=%d' % (ncol, nlig))
firm = np.zeros(4 * ncoltot, dtype=np.byte())
imgcxs = np.empty([nlig, ncol], dtype=np.complex64())
fin.seek(0)
firm = fin.read(4 * ncoltot)
firm = fin.read(4 * ncoltot)
firm = fin.read(4 * ncoltot)
firm = fin.read(4 * ncoltot)
#
for iut in range(nlig):
firm = fin.read(4 * ncoltot)
imgligne = np.ndarray(2 * ncoltot, '>h', firm)
imgcxs[iut, :] = imgligne[4:2 * ncoltot:2] + 1j * imgligne[5:2 * ncoltot:2]
print('[:,:,0] contains the real part of the SLC image data')
print('[:,:,1] contains the imaginary part of the SLC image data')
return np.stack((np.real(imgcxs), np.imag(imgcxs)), axis=2)
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment