Datasets:
Tasks:
Other
Formats:
csv
Languages:
English
Size:
10K - 100K
ArXiv:
Tags:
watermarking
invisible-watermark
image-watermarking
owner-identification
image-provenance
content-provenance
License:
File size: 3,427 Bytes
5ab6d2e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | #!/usr/bin/env python3
"""make_attacks.py - rebuilds attacked copies of the benchmark and checks each against its row of sets/<set>/results/cells.csv, pixel
for pixel and parameter for parameter.
The source of a row is sets/<set>/photos/<photo>/<photo>__sealed.png (marked = 1), or the original rebuilt from the public dataset
by prep.py (marked = 0: the unmarked twin; check.py --sources). The transformation (transforms.py, at the set's image side) runs with
the seed transforms.seed_of(photo, transformation, mode) - the same seed for a sealed photo and its twin - and the SHA-256 of the
result's RGB pixels (width x height x 3 bytes, row-major) must equal the row's pixel_sha256, and its parameters the row's params. PNG
file bytes are not compared (they depend on the zlib build); pixels are.
python3 scripts/make_attacks.py --set dev-div2k-valid-1024 --photos 0801,0802 # check these photos' copies
python3 scripts/make_attacks.py --set heldout-coco-val2017-512 --save rebuilt # also write every rebuilt copy as PNG"""
import argparse, csv, hashlib, json, os, sys
from multiprocessing import Pool
import numpy as np
from PIL import Image
HERE = os.path.dirname(os.path.abspath(__file__)); KIT = os.path.dirname(HERE)
sys.path.insert(0, HERE)
import transforms # noqa: E402
SIZE = {}
def size_of(s):
if s not in SIZE: SIZE[s] = json.load(open(os.path.join(KIT, 'sets', s, 'set.json')))['size']
return SIZE[s]
def pixel_sha256(im): return hashlib.sha256(np.asarray(im.convert('RGB'), np.uint8).tobytes()).hexdigest()
def rebuild(a):
row, save = a; s, photo, name, mode, marked = row['set'], row['photo'], row['transformation'], row['mode'], row['marked'] == '1'
transforms.S = size_of(s)
src = os.path.join(KIT, 'sets', s, 'photos', photo, f'{photo}__sealed.png') if marked else os.path.join(row['_orig'], f'{photo}.png')
out, params = transforms.apply(name, mode, Image.open(src).convert('RGB'), transforms.seed_of(photo, name, mode))
if save:
d = os.path.join(save, s, photo); os.makedirs(d, exist_ok=True)
out.save(os.path.join(d, f'{photo}__{name}__{mode}__{transforms.label(name, params)}{"" if marked else "__unmarked"}.png'))
return row, pixel_sha256(out) == row['pixel_sha256'] and json.loads(json.dumps(params)) == json.loads(row['params'])
def check_rows(rows, jobs, save=None):
with Pool(jobs) as p: return p.map(rebuild, [(r, save) for r in rows], chunksize=4)
if __name__ == '__main__':
ap = argparse.ArgumentParser(); ap.add_argument('--set', required=True); ap.add_argument('--save'); ap.add_argument('--photos')
ap.add_argument('--originals', help='a directory of originals rebuilt by prep.py (to rebuild the unmarked twins too)'); ap.add_argument('--jobs', type=int, default=os.cpu_count())
a = ap.parse_args()
rows = list(csv.DictReader(open(os.path.join(KIT, 'sets', a.set, 'results', 'cells.csv'), newline='')))
if a.photos: keep = set(a.photos.split(',')); rows = [r for r in rows if r['photo'] in keep]
rows = [dict(r, _orig=a.originals) for r in rows if r['marked'] == '1' or a.originals]
res = check_rows(rows, a.jobs, a.save); bad = [r for r, ok in res if not ok]
print(f'{len(rows) - len(bad)} of {len(rows)} attacked copies rebuilt with identical pixels and parameters')
for r in bad[:20]: print(' DIFFERENT:', r['photo'], r['transformation'], r['mode'], 'marked' if r['marked'] == '1' else 'twin')
sys.exit(1 if bad else 0)
|