Jacob Logas
commited on
Updated fawkes to hopefully work on gradio zerogpu
Browse files- app.py +112 -63
- requirements.txt +2 -94
app.py
CHANGED
|
@@ -1,76 +1,125 @@
|
|
| 1 |
from fawkes.protection import Fawkes
|
| 2 |
from fawkes.utils import Faces, reverse_process_cloaked
|
| 3 |
from fawkes.differentiator import FawkesMaskGeneration
|
| 4 |
-
|
| 5 |
import numpy as np
|
| 6 |
import gradio as gr
|
|
|
|
| 7 |
|
| 8 |
IMG_SIZE = 112
|
| 9 |
-
PREPROCESS =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
# To pre-emptively download the files at boot
|
| 12 |
-
fwks_l = Fawkes("extractor_2", '0', 1, mode='low')
|
| 13 |
-
fwks_m = Fawkes("extractor_2", '0', 1, mode='mid')
|
| 14 |
-
fwks_h = Fawkes("extractor_2", '0', 1, mode='high')
|
| 15 |
|
| 16 |
def generate_cloak_images(protector, image_X, target_emb=None):
|
| 17 |
cloaked_image_X = protector.compute(image_X, target_emb)
|
| 18 |
return cloaked_image_X
|
| 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 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from fawkes.protection import Fawkes
|
| 2 |
from fawkes.utils import Faces, reverse_process_cloaked
|
| 3 |
from fawkes.differentiator import FawkesMaskGeneration
|
| 4 |
+
from keras import utils
|
| 5 |
import numpy as np
|
| 6 |
import gradio as gr
|
| 7 |
+
import spaces
|
| 8 |
|
| 9 |
IMG_SIZE = 112
|
| 10 |
+
PREPROCESS = "raw"
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
def get_extractors():
|
| 14 |
+
hash_map = {
|
| 15 |
+
"extractor_2": "ce703d481db2b83513bbdafa27434703",
|
| 16 |
+
"extractor_0": "94854151fd9077997d69ceda107f9c6b",
|
| 17 |
+
}
|
| 18 |
+
for key, value in hash_map.items():
|
| 19 |
+
utils.get_file(
|
| 20 |
+
fname="{}.h5".format(key),
|
| 21 |
+
origin="http://mirror.cs.uchicago.edu/fawkes/files/{}.h5".format(key),
|
| 22 |
+
md5_hash=value,
|
| 23 |
+
cache_subdir="model",
|
| 24 |
+
)
|
| 25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
def generate_cloak_images(protector, image_X, target_emb=None):
|
| 28 |
cloaked_image_X = protector.compute(image_X, target_emb)
|
| 29 |
return cloaked_image_X
|
| 30 |
|
| 31 |
+
|
| 32 |
+
def preproc(img):
|
| 33 |
+
img = img.convert("RGB")
|
| 34 |
+
img = utils.img_to_array(img)
|
| 35 |
+
return img
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
@spaces.GPU
|
| 39 |
+
def predict(
|
| 40 |
+
img,
|
| 41 |
+
level,
|
| 42 |
+
sd=1e7,
|
| 43 |
+
format="png",
|
| 44 |
+
separate_target=True,
|
| 45 |
+
debug=False,
|
| 46 |
+
maximize=True,
|
| 47 |
+
save_last_on_failed=True,
|
| 48 |
+
progress=gr.Progress(track_tqdm=True),
|
| 49 |
+
):
|
| 50 |
+
img = preproc(img)
|
| 51 |
+
|
| 52 |
+
if level == "low":
|
| 53 |
+
fwks = Fawkes("extractor_2", 1, mode="low")
|
| 54 |
+
elif level == "mid":
|
| 55 |
+
fwks = Fawkes("extractor_2", 1, mode="mid")
|
| 56 |
+
elif level == "high":
|
| 57 |
+
fwks = Fawkes("extractor_2", 1, mode="high")
|
| 58 |
+
|
| 59 |
+
current_param = "-".join(
|
| 60 |
+
[
|
| 61 |
+
str(x)
|
| 62 |
+
for x in [
|
| 63 |
+
fwks.th,
|
| 64 |
+
sd,
|
| 65 |
+
fwks.lr,
|
| 66 |
+
fwks.max_step,
|
| 67 |
+
-1,
|
| 68 |
+
format,
|
| 69 |
+
separate_target,
|
| 70 |
+
debug,
|
| 71 |
+
]
|
| 72 |
+
]
|
| 73 |
+
)
|
| 74 |
+
faces = Faces(["./Current Face"], [img], fwks.aligner, verbose=0, no_align=False)
|
| 75 |
+
original_images = faces.cropped_faces
|
| 76 |
+
|
| 77 |
+
if len(original_images) == 0:
|
| 78 |
+
raise Exception("No face detected. ")
|
| 79 |
+
original_images = np.array(original_images)
|
| 80 |
+
|
| 81 |
+
if current_param != fwks.protector_param:
|
| 82 |
+
fwks.protector_param = current_param
|
| 83 |
+
if fwks.protector is not None:
|
| 84 |
+
del fwks.protector
|
| 85 |
+
batch_size = len(original_images)
|
| 86 |
+
fwks.protector = FawkesMaskGeneration(
|
| 87 |
+
fwks.feature_extractors_ls,
|
| 88 |
+
batch_size=batch_size,
|
| 89 |
+
mimic_img=True,
|
| 90 |
+
intensity_range=PREPROCESS,
|
| 91 |
+
initial_const=sd,
|
| 92 |
+
learning_rate=fwks.lr,
|
| 93 |
+
max_iterations=fwks.max_step,
|
| 94 |
+
l_threshold=fwks.th,
|
| 95 |
+
verbose=0,
|
| 96 |
+
maximize=maximize,
|
| 97 |
+
keep_final=False,
|
| 98 |
+
image_shape=(IMG_SIZE, IMG_SIZE, 3),
|
| 99 |
+
loss_method="features",
|
| 100 |
+
tanh_process=True,
|
| 101 |
+
save_last_on_failed=save_last_on_failed,
|
| 102 |
+
)
|
| 103 |
+
protected_images = generate_cloak_images(fwks.protector, original_images)
|
| 104 |
+
faces.cloaked_cropped_faces = protected_images
|
| 105 |
+
|
| 106 |
+
final_images, _ = faces.merge_faces(
|
| 107 |
+
reverse_process_cloaked(protected_images, preprocess=PREPROCESS),
|
| 108 |
+
reverse_process_cloaked(original_images, preprocess=PREPROCESS),
|
| 109 |
+
)
|
| 110 |
+
|
| 111 |
+
return final_images[-1].astype(np.uint8)
|
| 112 |
+
|
| 113 |
+
|
| 114 |
+
# Download extractors pre-emptively
|
| 115 |
+
get_extractors()
|
| 116 |
+
|
| 117 |
+
gr.Interface(
|
| 118 |
+
fn=predict,
|
| 119 |
+
inputs=[
|
| 120 |
+
gr.components.Image(type="pil"),
|
| 121 |
+
gr.components.Radio(["low", "mid", "high"], label="Protection Level"),
|
| 122 |
+
],
|
| 123 |
+
outputs=gr.components.Image(type="pil"),
|
| 124 |
+
allow_flagging="never",
|
| 125 |
+
).launch(show_error=True, quiet=False)
|
requirements.txt
CHANGED
|
@@ -1,95 +1,3 @@
|
|
| 1 |
-
absl-py==2.1.0
|
| 2 |
-
aiofiles==23.2.1
|
| 3 |
-
altair==5.3.0
|
| 4 |
-
annotated-types==0.7.0
|
| 5 |
-
anyio==3.7.1
|
| 6 |
-
astunparse==1.6.3
|
| 7 |
-
attrs==23.2.0
|
| 8 |
-
cachetools==5.3.3
|
| 9 |
-
certifi==2024.2.2
|
| 10 |
-
charset-normalizer==3.3.2
|
| 11 |
-
click==8.1.7
|
| 12 |
-
contourpy==1.2.1
|
| 13 |
-
cycler==0.12.1
|
| 14 |
-
fastapi==0.103.2
|
| 15 |
fawkes @ git+https://github.com/logasja/fawkes@master
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
flatbuffers==24.3.25
|
| 19 |
-
fonttools==4.51.0
|
| 20 |
-
fsspec==2024.5.0
|
| 21 |
-
gast==0.4.0
|
| 22 |
-
google-auth==2.29.0
|
| 23 |
-
google-auth-oauthlib==1.0.0
|
| 24 |
-
google-pasta==0.2.0
|
| 25 |
-
gradio==4.31.5
|
| 26 |
-
gradio_client==0.16.4
|
| 27 |
-
grpcio==1.64.0
|
| 28 |
-
h11==0.14.0
|
| 29 |
-
h5py==3.11.0
|
| 30 |
-
httpcore==1.0.5
|
| 31 |
-
httpx==0.27.0
|
| 32 |
-
huggingface-hub==0.23.1
|
| 33 |
-
idna==3.7
|
| 34 |
-
importlib_resources==6.4.0
|
| 35 |
-
Jinja2==3.1.4
|
| 36 |
-
jsonschema==4.22.0
|
| 37 |
-
jsonschema-specifications==2023.12.1
|
| 38 |
-
keras==2.13.1
|
| 39 |
-
kiwisolver==1.4.5
|
| 40 |
-
libclang==18.1.1
|
| 41 |
-
Markdown==3.6
|
| 42 |
-
markdown-it-py==3.0.0
|
| 43 |
-
MarkupSafe==2.1.5
|
| 44 |
-
matplotlib==3.9.0
|
| 45 |
-
mdurl==0.1.2
|
| 46 |
-
mtcnn==0.1.1
|
| 47 |
-
numpy==1.24.3
|
| 48 |
-
oauthlib==3.2.2
|
| 49 |
-
opencv-python==4.9.0.80
|
| 50 |
-
opt-einsum==3.3.0
|
| 51 |
-
orjson==3.10.3
|
| 52 |
-
packaging==24.0
|
| 53 |
-
pandas==2.2.2
|
| 54 |
-
Pillow==10.0.0
|
| 55 |
-
protobuf==4.25.3
|
| 56 |
-
pyasn1==0.6.0
|
| 57 |
-
pyasn1_modules==0.4.0
|
| 58 |
-
pydantic==2.7.1
|
| 59 |
-
pydantic_core==2.18.2
|
| 60 |
-
pydub==0.25.1
|
| 61 |
-
Pygments==2.18.0
|
| 62 |
-
pyparsing==3.1.2
|
| 63 |
-
python-dateutil==2.9.0.post0
|
| 64 |
-
python-multipart==0.0.9
|
| 65 |
-
pytz==2024.1
|
| 66 |
-
PyYAML==6.0.1
|
| 67 |
-
referencing==0.35.1
|
| 68 |
-
requests==2.32.2
|
| 69 |
-
requests-oauthlib==2.0.0
|
| 70 |
-
rich==13.7.1
|
| 71 |
-
rpds-py==0.18.1
|
| 72 |
-
rsa==4.9
|
| 73 |
-
ruff==0.4.4
|
| 74 |
-
semantic-version==2.10.0
|
| 75 |
-
shellingham==1.5.4
|
| 76 |
-
six==1.16.0
|
| 77 |
-
sniffio==1.3.1
|
| 78 |
-
starlette==0.27.0
|
| 79 |
-
tensorboard==2.13.0
|
| 80 |
-
tensorboard-data-server==0.7.2
|
| 81 |
-
tensorflow==2.13.0
|
| 82 |
-
tensorflow-estimator==2.13.0
|
| 83 |
-
tensorflow-io-gcs-filesystem==0.37.0
|
| 84 |
-
termcolor==2.4.0
|
| 85 |
-
tomlkit==0.12.0
|
| 86 |
-
toolz==0.12.1
|
| 87 |
-
tqdm==4.66.4
|
| 88 |
-
typer==0.12.3
|
| 89 |
-
typing_extensions==4.11.0
|
| 90 |
-
tzdata==2024.1
|
| 91 |
-
urllib3==2.2.1
|
| 92 |
-
uvicorn==0.29.0
|
| 93 |
-
websockets==11.0.3
|
| 94 |
-
Werkzeug==3.0.3
|
| 95 |
-
wrapt==1.16.0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
fawkes @ git+https://github.com/logasja/fawkes@master
|
| 2 |
+
gradio==4.38.1
|
| 3 |
+
spaces>=0.28.3
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|