Skip to content

Commit 635f39b

Browse files
committed
Merge branch 'aspect-resize-improvements'
Fixes #10. * aspect-resize-improvements: tests/gen-tests-mk: avoid bias in randomized aspect ratio distribution tests/Makefile: use wmtool.py for j90randomsizej80 attack See #10. tests/gen-tests-mk: use new wmtool.py compatible format for randomsize.inc See #10. tests/wmtool.py: implement "aspect-resize:<width>:<height>" attack Add new wmtool.py attack as suggested in #10 to avoid extreme aspect ratio changes. Signed-off-by: Stefan Westerfeld <stefan@space.twc.de>
2 parents debe283 + 2273f52 commit 635f39b

3 files changed

Lines changed: 34 additions & 3 deletions

File tree

tests/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ j90scaleto512j70 = $(SRCDIR)/tests/wmtool.py --seed $1 --attack "jpeg:90|scaleto
9191
j90scaleto768j90 = $(SRCDIR)/tests/wmtool.py --seed $1 --attack "jpeg:90|scaleto:768|jpeg:90" $< $@.png && mv $@.png $@
9292
j90scaleto768j70 = $(SRCDIR)/tests/wmtool.py --seed $1 --attack "jpeg:90|scaleto:768|jpeg:70" $< $@.png && mv $@.png $@
9393
j90scaleto768j50 = $(SRCDIR)/tests/wmtool.py --seed $1 --attack "jpeg:90|scaleto:768|jpeg:50" $< $@.png && mv $@.png $@
94-
j90randomsizej80 = convert $< -quality 90 $@.1.jpg && convert $@.1.jpg -resize $(RANDOMSIZE)! -quality 80 $@.2.jpg && rm $@.1.jpg && mv $@.2.jpg $@
94+
j90randomsizej80 = $(SRCDIR)/tests/wmtool.py --seed $1 --attack "jpeg:90|aspect-resize:$(RANDOMSIZE)|jpeg:80" $< $@.png && mv $@.png $@
9595
# centered cropping (detectable by corner sync)
9696
crop75j90 = convert $< -gravity Center -crop 75x75%+0+0 -quality 90 $@.jpg && mv $@.jpg $@
9797
crop50scale200j90 = convert $< -gravity Center -crop 50x50%+0+0 -resize 200% -quality 90 $@.jpg && mv $@.jpg $@

tests/gen-tests-mk

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,8 @@ def generate_resolutions (resolution_icdf, resolution_min, resolution_max, num_s
102102
resolutions2d = []
103103
for reso in generated_resolutions:
104104
total_pixels = reso * reso
105-
ratio = random.uniform (min_ratio, max_ratio)
105+
log_ratio = random.uniform (np.log (min_ratio), np.log (max_ratio))
106+
ratio = np.exp (log_ratio)
106107
width = round (np.sqrt (total_pixels * ratio))
107108
height = round (np.sqrt (total_pixels / ratio))
108109
resolutions2d.append ((width, height))
@@ -181,7 +182,7 @@ def main():
181182
imgname = 'img%05u' % imgcounter
182183
dstname = os.path.join (inputsdir, imgname)
183184
rw, rh = resolutions2d[imgidx]
184-
target_sizes.append ((imgname, f"{rw}x{rh}"))
185+
target_sizes.append ((imgname, f"{rw}:{rh}"))
185186
if symlink (src_image_path, dstname):
186187
eprint (" SYMLINK", dstname, "->", src_image_path, f"(target size: {rw}x{rh})")
187188
else:

tests/wmtool.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,34 @@ def attack_aspect (img, amin, amax):
135135

136136
return cv2.resize (img, (width, height))
137137

138+
epilog += """
139+
* aspect-resize:<width>:<height>
140+
141+
change aspect ratio of the image, avoiding extreme aspect ratio changes
142+
if aspect ratio change is in 9:16..16:9
143+
- rescale to <width> x <height> pixels
144+
if aspect ratio change is extreme (outside 9:16..16:9)
145+
- rescale to <height> x <width> pixels unless this is even more extreme
146+
147+
this is mainly a helper for the j90randomsizej80 attack of gen-tests-mk
148+
"""
149+
def attack_aspect_resize (img, width, height):
150+
def aspect_delta (old_size, new_size):
151+
delta = (old_size[0] / old_size[1]) / (new_size[0] / new_size[1])
152+
if (delta < 1):
153+
delta = 1 / delta
154+
return delta
155+
old_height, old_width = img.shape[0], img.shape[1]
156+
delta = aspect_delta ((old_width, old_height), (width, height))
157+
delta_swap = aspect_delta ((old_width, old_height), (height, width))
158+
if delta > 16/9 and delta_swap < delta:
159+
width, height = height, width
160+
swap = " (swap)"
161+
else:
162+
swap = ""
163+
print_attack ("ATTACK aspect-resize %dx%d to %dx%d%s" % (old_width, old_height, width, height, swap))
164+
return cv2.resize (img, (width, height))
165+
138166
epilog += """
139167
------------------------------------------------------------------------------
140168
Example:
@@ -228,6 +256,8 @@ def load_bgr_image (filename):
228256
face = attack_rotate (face, int (aargs[1]), int (aargs[2]))
229257
elif aargs[0] == "aspect" and len (aargs) == 3:
230258
face = attack_aspect (face, int (aargs[1]), int (aargs[2]))
259+
elif aargs[0] == "aspect-resize" and len (aargs) == 3:
260+
face = attack_aspect_resize (face, int (aargs[1]), int (aargs[2]))
231261
elif aargs[0] == "none" and len (aargs) == 1:
232262
pass
233263
else:

0 commit comments

Comments
 (0)