Skip to content

Commit

Permalink
Merge pull request #7 from ykemiche/main
Browse files Browse the repository at this point in the history
update 0.1.1
  • Loading branch information
ykemiche authored Jul 13, 2022
2 parents 8311026 + 05317ff commit 427639f
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 63 deletions.
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,28 +33,28 @@ The package offers you 3 different methods for despeckling your SAR images: the
1) I have a high-resolution SAR image and I want to apply the despeckling function to the whole of it:

```python
from deepdespeckling.merlin.test.spotlight import despeckle_st
from deepdespeckling.merlin.test.spotlight import despeckle_spotlight

image_path="path/to/cosar/image"
destination_directory="path/where/to/save/results"
model_weights_path="path/to/model/weights"

despeckle_st(image_path,destination_directory,model_weights_path=model_weights_path)
despeckle_spotlight(image_path,destination_directory,model_weights_path=model_weights_path)
```
Noisy image | Denoised image
:----------------------:|:-------------------------:
![](img/entire/noisy.png) | ![](img/entire/denoised.png)

2) I have a high-resolution SAR image but I only want to apply the despeckling function to a specific area for which I know the coordinates:
```python
from deepdespeckling.merlin.test.spotlight import despeckle_from_coordinates_st
from deepdespeckling.merlin.test.spotlight import despeckle_from_coordinates_spotlight

image_path="path/to/cosar/image"
destination_directory="path/where/to/save/results"
model_weights_path="path/to/model/weights"
coordinates_dictionnary = {'x_start':2600,'y_start':1000,'x_end':3000,'y_end':1200}

despeckle_from_coordinates_st(image_path, coordinates_dict, destination_directory, model_weights_path)
despeckle_from_coordinates_spotlight(image_path, coordinates_dict, destination_directory, model_weights_path)
````

Noisy image | Denoised image
Expand All @@ -63,14 +63,14 @@ Noisy image | Denoised image

3) I have a high-resolution SAR image but I want to apply the despeckling function to an area I want to select with a crop:
```python
from deepdespeckling.merlin.test.spotlight import despeckle_from_crop_st
from deepdespeckling.merlin.test.spotlight import despeckle_from_crop_spotlight

image_path="path/to/cosar/image"
destination_directory="path/where/to/save/results"
model_weights_path="path/to/model/weights"
fixed = True "(it will crop a 256*256 image from the position of your click)" or False "(you will draw free-handly the area of your interest)"

despeckle_from_crop_st(image_path, destination_directory, model_weights_path, fixed=False)
despeckle_from_crop_spotlight(image_path, destination_directory, model_weights_path, fixed=False)
```

* The cropping tool: Just select an area and press "q" when you are satisfied with the crop !
Expand All @@ -87,13 +87,13 @@ Noisy cropped image | Denoised cropped image

you can use the same features for stripmap images by importing :
```python
from deepdespeckling.merlin.test.stripmap import despeckle_from_crop_sp,despeckle_sp,despeckle_from_coordinates_sp
from deepdespeckling.merlin.test.stripmap import despeckle_from_crop_stripmap,despeckle_stripmap,despeckle_from_coordinates_stripmap
```
### Train

1) I want to train my own model from scratch:
```python
from deepdespeckling.merlin.train.train import create_model, fit_model
from deepdespeckling.merlin.train import create_model, fit_model
nb_epoch=1

# schedule the learning rate
Expand All @@ -115,7 +115,7 @@ fit_model(model,lr,nb_epoch,training_set_directory,validation_set_directory,samp

2) I want to train a model using the pre-trained version :
```python
from deepdespeckling.merlin.train.train import create_model, fit_model
from deepdespeckling.merlin.train import create_model, fit_model
from merlinsar.train.model import Model

nb_epoch=1
Expand Down
4 changes: 2 additions & 2 deletions deepdespeckling/merlin/test/model_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ def test(self,test_files, weights_path, save_dir,stride,patch_size):
print("Denoised image %s" % imagename)

save_sar_images(outputimage, noisyimage, imagename, save_dir)
save_real_imag_images( denormalize_sar(output_clean_image_1), denormalize_sar(output_clean_image_2),
save_real_imag_images(noisyimage, denormalize_sar(output_clean_image_1), denormalize_sar(output_clean_image_2),
imagename, save_dir)

save_real_imag_images_noisy( np.squeeze(i_real_part), np.squeeze(i_imag_part), imagename, save_dir)
save_real_imag_images_noisy(noisyimage, np.squeeze(i_real_part), np.squeeze(i_imag_part), imagename, save_dir)
47 changes: 24 additions & 23 deletions deepdespeckling/merlin/test/spotlight.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
this_dir, this_filename = os.path.split(__file__)


def despeckle_st(image_path, destination_directory, stride_size=64,
model_weights_path=os.path.join(this_dir, "saved_model", "spotlight.pth"), patch_size=256):
def despeckle_spotlight(image_path, destination_directory, stride_size=64,
model_weights_path=os.path.join(this_dir, "saved_model", "spotlight.pth"), patch_size=256):
""" Description
----------
Runs a test instance by calling the test function defined in model.py on a few samples
Expand All @@ -43,29 +43,28 @@ def despeckle_st(image_path, destination_directory, stride_size=64,
for f in filelist:
os.remove(f)


# check the extension of the file
if Path(image_path).suffix==".npy":
image_data=np.load(image_path)
if Path(image_path).suffix == ".npy":
image_data = np.load(image_path)
else:
image_data = cos2mat(image_path)

np.save(test_data+'/test_image_data.npy',image_data)
image_data = cos2mat(image_path)

imagename = image_path.split('/')[-1].split('.')[0]
np.save(test_data + '/' + imagename +'.npy', image_data)

print(
"[*] Start testing on real data. Working directory: %s. Collecting data from %s and storing test results in %s" % (
os.getcwd(), destination_directory, destination_directory))
os.getcwd(), image_path, destination_directory))

test_files = glob((test_data + '/*.npy'))
print(test_files)

denoiser.test(test_files, model_weights_path, save_dir=destination_directory,
stride=stride_size, patch_size=patch_size)


def despeckle_from_coordinates_st(image_path, coordinates_dict, destination_directory, stride_size=64,
model_weights_path=os.path.join(this_dir, "saved_model", "spotlight.pth"), patch_size=256):
def despeckle_from_coordinates_spotlight(image_path, coordinates_dict, destination_directory, stride_size=64,
model_weights_path=os.path.join(this_dir, "saved_model", "spotlight.pth"),
patch_size=256):
""" Description
----------
Runs a test instance by calling the test function defined in model.py on a few samples
Expand Down Expand Up @@ -96,26 +95,28 @@ def despeckle_from_coordinates_st(image_path, coordinates_dict, destination_dire
os.remove(f)

# check the extension of the file
if Path(image_path).suffix==".npy":
image_data=np.load(image_path)
if Path(image_path).suffix == ".npy":
image_data = np.load(image_path)
else:
image_data = cos2mat(image_path)
image_data = cos2mat(image_path)

np.save(test_data+'/test_image_data.npy',image_data[x_start:x_end,y_start:y_end,:])
imagename = image_path.split('/')[-1].split('.')[0]
np.save(test_data + '/' + imagename +'.npy', image_data[x_start:x_end, y_start:y_end, :])

print(
"[*] Start testing on real data. Working directory: %s. Collecting data from %s and storing test results in %s" % (
os.getcwd(), destination_directory, destination_directory))
os.getcwd(), image_path, destination_directory))

test_files = glob((test_data + '/*.npy'))
print(test_files)

denoiser.test(test_files, model_weights_path, save_dir=destination_directory,
stride=stride_size, patch_size=patch_size)


def despeckle_from_crop_st(image_path, destination_directory, stride_size=64,
model_weights_path=os.path.join(this_dir, "saved_model", "spotlight.pth"), patch_size=256,
fixed=True):
def despeckle_from_crop_spotlight(image_path, destination_directory, stride_size=64,
model_weights_path=os.path.join(this_dir, "saved_model", "spotlight.pth"),
patch_size=256,
fixed=True):
print('value ofd fixed in despeckle from crop', fixed)
""" The despeckling function with an integrated cropping tool made with OpenCV.
The ideal choice if you need to despeckle only a certain area of your high-res image. Results are saved in the
Expand Down Expand Up @@ -182,8 +183,8 @@ def despeckle_from_crop_st(image_path, destination_directory, stride_size=64,

print(
"[*] Start testing on real data. Working directory: %s. Collecting data from %s and storing test results in %s" % (
os.getcwd(), destination_directory, destination_directory))
print('path to npy or not',test_data + '/' + p.stem + '_cropped.npy')
os.getcwd(), image_path, destination_directory)+"\n")

test_files = glob((test_data + '/' + p.stem + '_cropped.npy'))
denoiser.test(test_files, model_weights_path, save_dir=destination_directory,
stride=stride_size, patch_size=patch_size)
30 changes: 16 additions & 14 deletions deepdespeckling/merlin/test/stripmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
this_dir, this_filename = os.path.split(__file__)


def despeckle_sp(image_path, destination_directory, stride_size=64,
def despeckle_stripmap(image_path, destination_directory, stride_size=64,
model_weights_path=os.path.join(this_dir, "saved_model", "stripmap.pth"), patch_size=256):

""" Description
----------
Runs a test instance by calling the test function defined in model.py on a few samples
Expand All @@ -43,14 +43,15 @@ def despeckle_sp(image_path, destination_directory, stride_size=64,
filelist = glob(os.path.join(test_data, "*"))
for f in filelist:
os.remove(f)

# check the extension of the file
if Path(image_path).suffix==".npy":
image_data=np.load(image_path)
else:
image_data = cos2mat(image_path)
image_data = cos2mat(image_path)

np.save(test_data+'/test_image_data.npy',image_data)
imagename = image_path.split('/')[-1].split('.')[0]
np.save(test_data + '/' + imagename +'.npy', image_data)

print(
"[*] Start testing on real data. Working directory: %s. Collecting data from %s and storing test results in %s" % (
Expand All @@ -63,7 +64,7 @@ def despeckle_sp(image_path, destination_directory, stride_size=64,
stride=stride_size, patch_size=patch_size)


def despeckle_from_coordinates_sp(image_path, coordinates_dict, destination_directory, stride_size=64,
def despeckle_from_coordinates_stripmap(image_path, coordinates_dict, destination_directory, stride_size=64,
model_weights_path=os.path.join(this_dir, "saved_model", "stripmap.pth"), patch_size=256):
""" Description
----------
Expand Down Expand Up @@ -94,16 +95,17 @@ def despeckle_from_coordinates_sp(image_path, coordinates_dict, destination_dire
for f in filelist:
os.remove(f)


# check the extension of the file
if Path(image_path).suffix==".npy":
image_data=np.load(image_path)
else:
image_data = cos2mat(image_path)
image_data = cos2mat(image_path)

imagename = image_path.split('/')[-1].split('.')[0]
np.save(test_data + '/' + imagename +'.npy', image_data[x_start:x_end, y_start:y_end, :])

np.save(test_data+'/test_image_data.npy',image_data[x_start:x_end,y_start:y_end,:])


print(
"[*] Start testing on real data. Working directory: %s. Collecting data from %s and storing test results in %s" % (
os.getcwd(), destination_directory, destination_directory))
Expand All @@ -114,7 +116,7 @@ def despeckle_from_coordinates_sp(image_path, coordinates_dict, destination_dire
stride=stride_size, patch_size=patch_size)


def despeckle_from_crop_sp(image_path, destination_directory, stride_size=64,
def despeckle_from_crop_stripmap(image_path, destination_directory, stride_size=64,
model_weights_path=os.path.join(this_dir, "saved_model", "stripmap.pth"), patch_size=256,
fixed=True):
print('value ofd fixed in despeckle from crop', fixed)
Expand Down Expand Up @@ -183,8 +185,8 @@ def despeckle_from_crop_sp(image_path, destination_directory, stride_size=64,

print(
"[*] Start testing on real data. Working directory: %s. Collecting data from %s and storing test results in %s" % (
os.getcwd(), destination_directory, destination_directory))
print('path to npy or not',test_data + '/' + p.stem + '_cropped.npy')
os.getcwd(), image_path, destination_directory)+"\n")

test_files = glob((test_data + '/' + p.stem + '_cropped.npy'))
denoiser.test(test_files, model_weights_path, save_dir=destination_directory,
stride=stride_size, patch_size=patch_size)
stride=stride_size, patch_size=patch_size)
Loading

0 comments on commit 427639f

Please sign in to comment.