Skip to content

Commit

Permalink
Standarized scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
ariel415el committed Mar 9, 2022
1 parent 18f9766 commit 6e75552
Show file tree
Hide file tree
Showing 12 changed files with 139 additions and 143 deletions.
2 changes: 1 addition & 1 deletion GPDM.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def __init__(self,
self.decay_steps = decay_steps

init_name = 'img' if os.path.exists(self.init) else self.init
self.name = f'R-{resize}_S-{pyr_factor}->{coarse_dim}_I-{init_name}+I(0,{noise_sigma})'
self.name = f'R-{resize}x{pyr_factor}->{coarse_dim}_I-{init_name}+I(0,{noise_sigma})'

def _get_target_pyramid(self, target_img_path):
"""Reads an image and create a pyraimd out of it. Ordered in increasing image size"""
Expand Down
7 changes: 4 additions & 3 deletions distribution_metrics/patch_swd.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,14 @@ def extract_patches(x, patch_size, stride):


class PatchSWDLoss(torch.nn.Module):
def __init__(self, patch_size=7, stride=1, num_proj=256, use_convs=True):
def __init__(self, patch_size=7, stride=1, num_proj=256, use_convs=True, mask_patches_factor=0):
super(PatchSWDLoss, self).__init__()
self.name = f"ConvSWDLoss(p-{patch_size}:{stride})"
self.patch_size = patch_size
self.stride = stride
self.num_proj = num_proj
self.use_convs = use_convs
self.mask_patches_factor = mask_patches_factor

def forward(self, x, y, mask=None):
b, c, h, w = x.shape
Expand All @@ -58,10 +59,10 @@ def forward(self, x, y, mask=None):

if mask is not None:
# duplicate patches that touches the mask by a factor
mask_patches_factor = 0

mask_patches = extract_patches(mask, self.patch_size, self.stride)[0] # in [-1,1]
mask_patches = torch.any(mask_patches > 0, dim=-1)
projy = torch.cat([projy[:, ~mask_patches]] + [projy[:, mask_patches]]*mask_patches_factor, dim=1)
projy = torch.cat([projy[:, ~mask_patches]] + [projy[:, mask_patches]]*self.mask_patches_factor, dim=1)

projx, projy = duplicate_to_match_lengths(projx, projy)

Expand Down
Binary file added images/edit_inputs/tree_mask.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/manipulations/soccer2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/manipulations/soccer2_mask.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
32 changes: 17 additions & 15 deletions scripts/duplicate_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,30 @@

import distribution_metrics
from GPDM import GPDM
from utils import save_image
from utils import save_image, get_file_name


input_and_target_images = [
('images/retargeting/teo.jpg', 'images/edit_inputs/teo_mask.png', 28),
('images/edit_inputs/oranges.jpg', 'images/edit_inputs/oranges_mask.jpg', 28),
]
def main():
def edit_images(input_and_target_images, out_dir):
"""
Smaller patch size adds variablility but may ruin large objects
"""
criteria = distribution_metrics.PatchSWDLoss(patch_size=7, stride=1, num_proj=128)
model = GPDM(pyr_factor=0.85, coarse_dim=28, lr=0.05, num_steps=300, init='noise', noise_sigma=1.5, resize=0)
output_dir = f'outputs/remove_objects/{criteria.name}_{model.name}'
for (target_image_path, input_image_path, coarse_dim) in input_and_target_images:
fname, ext = os.path.splitext(os.path.basename(target_image_path))[:2]

debug_dir = f'{output_dir}/debug_images/{fname}'
criteria = distribution_metrics.PatchSWDLoss(patch_size=7, stride=1, num_proj=256, mask_patches_factor=3)

result = model.run(target_image_path, criteria, debug_dir, mask_img_path=input_image_path)
for (target_image_path, mask_image_path, coarse_dim) in input_and_target_images:
model = GPDM(coarse_dim=coarse_dim, pyr_factor=0.85, lr=0.03, num_steps=300, init='noise', noise_sigma=0, resize=256)

result = model.run(target_image_path, criteria, mask_img_path=mask_image_path, debug_dir=None)
output_path = os.path.join(out_dir, f'{get_file_name(target_image_path)}.png')
save_image(result, output_path)

save_image(result, f'{output_dir}/generated_images/{fname}{ext}')

if __name__ == '__main__':
main()
input_and_target_images = [
('images/retargeting/teo.jpg', 'images/edit_inputs/teo_mask.png', 28),
('images/edit_inputs/oranges.jpg', 'images/edit_inputs/oranges_mask.jpg', 28),
('images/edit_inputs/tree.png', 'images/edit_inputs/tree_mask.png', 28),
('images/manipulations/soccer2_org.jpg', 'images/manipulations/soccer2_mask.jpg', 28),
]
outputs_dir = f'outputs/duplicate_images'
edit_images(input_and_target_images, outputs_dir)
34 changes: 16 additions & 18 deletions scripts/edit_images.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,29 @@
from GPDM import GPDM
from utils import save_image, get_file_name

input_and_target_images = [
('images/edit_inputs/tree.png', 'images/edit_inputs/tree_edit.png', 128),
('images/SIGD16/7.jpg', 'images/edit_inputs/balls_edit.jpg', 128),
('images/SIGD16/4.jpg', 'images/edit_inputs/birds_dusk_edit.jpg', 128),
('images/Places50/15.jpg', 'images/edit_inputs/stone_edit.png', 256),
('images/edit_inputs/swiming.jpg', 'images/edit_inputs/swiming_edit.jpg', 128),
('images/retargeting/teo.jpg', 'images/edit_inputs/teo_edit.jpg', 64),
]

def main():
def edit_images(input_and_target_images, out_dir):
"""
Smaller patch size adds variablility but may ruin large objects
"""
criteria = distribution_metrics.PatchSWDLoss(patch_size=7, stride=1, num_proj=256)
# criteria = distribution_metrics.PatchCoherentLoss(patch_size=7, stride=3, mode='detached')
outputs_dir = 'outputs/image_editing'
for (target_image_path, input_image_path, coarse_dim) in input_and_target_images:
model = GPDM(coarse_dim=coarse_dim, pyr_factor=0.85, lr=0.03, num_steps=300, init=input_image_path, noise_sigma=0, resize=0)
fname, ext = os.path.splitext(os.path.basename(input_image_path))[:2]

debug_dir = f'{outputs_dir}/debug_images/{get_file_name(input_image_path)}-to-{get_file_name(target_image_path)}/{criteria.name}_{model.name}'
result = model.run(target_image_path, criteria, debug_dir)
for (target_image_path, input_image_path, coarse_dim) in input_and_target_images:
model = GPDM(coarse_dim=coarse_dim, pyr_factor=0.85, lr=0.03, num_steps=300, init=input_image_path, noise_sigma=0, resize=256)

save_image(result, f'{outputs_dir}/generated_images/{fname}${0}{ext}')
result = model.run(target_image_path, criteria, debug_dir=None)
output_path = os.path.join(out_dir, f'{get_file_name(input_image_path)}.png')
save_image(result, output_path)


if __name__ == '__main__':
main()
input_and_target_images = [
('images/edit_inputs/tree.png', 'images/edit_inputs/tree_edit.png', 128),
('images/SIGD16/7.jpg', 'images/edit_inputs/balls_edit.jpg', 128),
('images/SIGD16/4.jpg', 'images/edit_inputs/birds_dusk_edit.jpg', 128),
('images/Places50/15.jpg', 'images/edit_inputs/stone_edit.png', 256),
('images/edit_inputs/swiming.jpg', 'images/edit_inputs/swiming_edit.jpg', 32),
('images/retargeting/teo.jpg', 'images/edit_inputs/teo_edit.jpg', 64),
]
outputs_dir = f'outputs/edit_images'
edit_images(input_and_target_images, outputs_dir)
34 changes: 19 additions & 15 deletions scripts/resample_periodic_textures.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,31 @@
from GPDM import GPDM
from utils import save_image

image_paths = [

def resample_textures(image_paths, out_dir, n_reps=1):
"""
Smaller patch size adds variablility but may ruin large objects
"""
criteria = distribution_metrics.PatchSWDLoss(patch_size=7, stride=1, num_proj=256)
model = GPDM(pyr_factor=0.85, coarse_dim=35, lr=0.05, num_steps=300, init='noise', noise_sigma=1.5, resize=0)

for input_image_path in image_paths:
for i in range(n_reps):

result = model.run(input_image_path, criteria, debug_dir=None)

save_image(result, os.path.join(out_dir, model.name, str(i), os.path.basename(input_image_path)))

if __name__ == '__main__':
image_paths = [
'images/textures/olives.png',
'images/textures/tomatos.png',
'images/textures/green_waves.jpg',
'images/textures/cobbles.jpeg',
'images/style_transfer/style/brick.jpg',
'images/style_transfer/style/mondrian.jpg',
'images/style_transfer/style/rug.jpeg',

]

def main():
criteria = distribution_metrics.PatchSWDLoss(patch_size=7, stride=1, num_proj=128)
for input_image_path in image_paths:
model = GPDM(pyr_factor=0.75, coarse_dim=32, resize=256, scale_factor=(2, 2), lr=0.03, num_steps=200, init='noise', noise_sigma=1.5, decay_steps=100)
output_dir = f'outputs/periodic_textures/{criteria.name}_{model.name}'
fname, ext = os.path.splitext(os.path.basename(input_image_path))[:2]
debug_dir = f'{output_dir}/debug_images/{fname}'
result = model.run(input_image_path, criteria, debug_dir)

save_image(result, f'{output_dir}/generated_images/{fname}{ext}')

if __name__ == '__main__':
main()
out_dir = "outputs/resample_textures"
resample_textures(image_paths, out_dir)
31 changes: 12 additions & 19 deletions scripts/reshuffle.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,23 @@
from GPDM import GPDM
from utils import save_image

image_paths = []
dataset_dir = 'images/SIGD16'
# dataset_dir = 'images/Places50'
# dataset_dir = 'images/reshuffle'
# dataset_dir = '/home/ariel/university/GPDM/GPDM/images/HQ_16'
image_paths += [os.path.join(dataset_dir, x) for x in os.listdir(dataset_dir)]


def main():
def reshuffle(dataset_dir, out_dir, n_reps=1):
"""
Smaller patch size adds variablility but may ruin large objects
"""
criteria = distribution_metrics.PatchSWDLoss(patch_size=7, stride=1, num_proj=512)
model = GPDM(pyr_factor=0.85, coarse_dim=28, lr=0.05, num_steps=150, init='noise', noise_sigma=1.5, resize=0)
output_dir = f'outputs/reshuffle/{os.path.basename(dataset_dir)}_{criteria.name}_{model.name}'
for input_image_path in image_paths:
for i in range(5):
fname, ext = os.path.splitext(os.path.basename(input_image_path))[:2]
criteria = distribution_metrics.PatchSWDLoss(patch_size=7, stride=1, num_proj=256)
model = GPDM(pyr_factor=0.85, coarse_dim=35, lr=0.05, num_steps=300, init='noise', noise_sigma=1.5, resize=0)

debug_dir = f'{output_dir}/debug_images/{fname}-{i}'
image_paths = [os.path.join(dataset_dir, x) for x in os.listdir(dataset_dir)]
for input_image_path in image_paths:
for i in range(n_reps):

result = model.run(input_image_path, criteria, debug_dir)
result = model.run(input_image_path, criteria, debug_dir=None)

save_image(result, f'{output_dir}/generated_images/{fname}${i}{ext}')
save_image(result, os.path.join(out_dir, f"{os.path.basename(dataset_dir)}_{model.name}", str(i), os.path.basename(input_image_path)))

if __name__ == '__main__':
main()
out_dir = "outputs/reshuffle"
reshuffle('/home/ariel/university/GPDM/images/SIGD16', out_dir)
reshuffle('/home/ariel/university/GPDM/images/Places50', out_dir)
reshuffle('/home/ariel/university/GPDM/images/reshuffle', out_dir)
33 changes: 18 additions & 15 deletions scripts/reshuffle_HD_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,30 @@
from GPDM import GPDM
from utils import save_image

image_paths = []
dataset_dir = '/home/ariel/university/GPDM/images/HQ_16'
image_paths += [os.path.join(dataset_dir, x) for x in os.listdir(dataset_dir)]
criteria = distribution_metrics.PatchSWDLoss(patch_size=11, stride=1, num_proj=64)
model = GPDM(pyr_factor=0.85, coarse_dim=44, lr=0.03, num_steps=500, init='noise', noise_sigma=0.15, resize=1024,
scale_factor=(1, 1))


def main():
def reshuffle_hq(image_paths, out_dir, n_reps=1):
"""
Smaller patch size adds variablility but may ruin large objects
"""
criteria = distribution_metrics.PatchSWDLoss(patch_size=11, stride=1, num_proj=64)
model = GPDM(pyr_factor=0.85, coarse_dim=44, lr=0.03, num_steps=500, init='noise', noise_sigma=0.15, resize=1024, scale_factor=(1,1))
output_dir = f'outputs/reshuffle/{os.path.basename(dataset_dir)}_{criteria.name}_{model.name}'
for input_image_path in image_paths:
for i in range(1):
fname, ext = os.path.splitext(os.path.basename(input_image_path))[:2]
criteria = distribution_metrics.PatchSWDLoss(patch_size=7, stride=1, num_proj=256)
model = GPDM(pyr_factor=0.85, coarse_dim=35, lr=0.05, num_steps=300, init='noise', noise_sigma=1.5, resize=0)

debug_dir = f'{output_dir}/debug_images/{fname}-{i}'
for input_image_path in image_paths:
for i in range(n_reps):

result = model.run(input_image_path, criteria, debug_dir)
result = model.run(input_image_path, criteria, debug_dir=None)

save_image(result, f'{output_dir}/generated_images/{fname}${i}{ext}')
save_image(result, os.path.join(out_dir, model.name, str(i), os.path.basename(input_image_path)))

if __name__ == '__main__':
main()

image_paths = []
dataset_dir = '/home/ariel/university/GPDM/images/HQ_16'
image_paths += [os.path.join(dataset_dir, x) for x in os.listdir(dataset_dir)]
image_paths = [x for x in image_paths if ('rio' in x or 'lambs' in x or 'safari' in x)]

out_dir = "outputs/reshuffle_HQ"
reshuffle_hq(image_paths, out_dir)
52 changes: 25 additions & 27 deletions scripts/retarget.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,35 @@
from GPDM import GPDM
from utils import save_image

images = [
'images/retargeting/fish.png',
'images/retargeting/fruit.png',
'images/retargeting/corn.png',
'images/retargeting/pinguins.png',
'images/retargeting/SupremeCourt.jpeg',
'images/retargeting/mountains.jpg',
'images/retargeting/jerusalem.jpg',
'images/retargeting/teo.jpg',
'images/retargeting/house.jpg',
'images/Places50/50.jpg',

]
images += [os.path.join('images/SIGD16/', x) for x in os.listdir('images/SIGD16/')]

def main():

def retarget(image_paths, out_dir, n_reps=1):
"""
Smaller patch size adds variablility but may ruin large objects
"""
outputs_dir = 'outputs/retarget/'
criteria = distribution_metrics.PatchSWDLoss(patch_size=7, stride=1, num_proj=64)
for input_image_path in images:
for scale_factor in [(2,0.5), (1,1.5), (1,2.5), (1,0.5)]:
for i in range(2):
model = GPDM(resize=256, coarse_dim=21, pyr_factor=0.85, scale_factor=scale_factor, lr=0.05, num_steps=300, decay_steps=150, init='target', noise_sigma=1.5)
criteria = distribution_metrics.PatchSWDLoss(patch_size=7, stride=1, num_proj=256)

for input_image_path in image_paths:
for scale_factor in [(1, 2), (1, 0.5), (2, 0.5)]:
for i in range(n_reps):
model = GPDM(resize=0, coarse_dim=28, pyr_factor=0.75, scale_factor=scale_factor, lr=0.05, num_steps=300, init='target', noise_sigma=1.5)

result = model.run(input_image_path, criteria, debug_dir=None)

fname, ext = os.path.splitext(os.path.basename(input_image_path))[:2]
debug_dir = f'{outputs_dir}/{criteria.name}_{model.name}/debug_images/{fname}-{scale_factor}'
save_image(result, os.path.join(out_dir, model.name, str(i), str(scale_factor) + os.path.basename(input_image_path)))

result = model.run(input_image_path, criteria, debug_dir)

save_image(result, f'{outputs_dir}/{criteria.name}_{model.name}/generated_images/{fname}_{scale_factor}${i}{ext}')
if __name__ == '__main__':
main()
image_pats = [
'images/retargeting/fish.png',
'images/retargeting/fruit.png',
'images/retargeting/corn.png',
'images/retargeting/pinguins.png',
'images/retargeting/SupremeCourt.jpeg',
'images/retargeting/mountains.jpg',
'images/retargeting/jerusalem.jpg',
'images/retargeting/teo.jpg',
'images/retargeting/house.jpg',
'images/Places50/50.jpg',
]
out_dir = "outputs/retarget"
retarget(image_pats, out_dir, 1)
Loading

0 comments on commit 6e75552

Please sign in to comment.