Add auditor_inference helper script
Browse files- auditor_inference.py +7 -7
auditor_inference.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
#!/usr/env/bin python3
|
| 2 |
"""
|
| 3 |
Standalone Adversarial Image Auditor Inference Module
|
| 4 |
-
Provides the necessary classes to load the pre-trained
|
| 5 |
and evaluate images.
|
| 6 |
"""
|
| 7 |
|
|
@@ -99,15 +99,15 @@ class SimpleTextEncoder(nn.Module):
|
|
| 99 |
return text_features
|
| 100 |
|
| 101 |
# =============================================================================
|
| 102 |
-
#
|
| 103 |
# =============================================================================
|
| 104 |
|
| 105 |
-
class
|
| 106 |
"""
|
| 107 |
ResNet101 Backbone Auditor incorporating safety classes and metrics
|
| 108 |
"""
|
| 109 |
def __init__(self, num_classes=6, vocab_size=50000):
|
| 110 |
-
super(
|
| 111 |
resnet = models.resnet101(weights=None)
|
| 112 |
self.features = nn.Sequential(*list(resnet.children())[:-2])
|
| 113 |
|
|
@@ -224,7 +224,7 @@ def inference(model, tokenizer, image, prompt=""):
|
|
| 224 |
Independent plug-and-play function for auditing an image
|
| 225 |
|
| 226 |
Args:
|
| 227 |
-
model: Trained
|
| 228 |
tokenizer: Initialized SimpleTokenizer used during training
|
| 229 |
image: PIL Image
|
| 230 |
prompt: String text prompt condition
|
|
@@ -282,12 +282,12 @@ def inference(model, tokenizer, image, prompt=""):
|
|
| 282 |
# EXAMPLE USAGE (Stand-alone test)
|
| 283 |
# =============================================================================
|
| 284 |
if __name__ == "__main__":
|
| 285 |
-
print("[+] Loading
|
| 286 |
# Initialize tokenizer
|
| 287 |
tokenizer = SimpleTokenizer(vocab_size=50000, max_length=MAX_PROMPT_LENGTH)
|
| 288 |
|
| 289 |
# Initialize the model instance
|
| 290 |
-
model =
|
| 291 |
model.to(DEVICE)
|
| 292 |
model.eval()
|
| 293 |
|
|
|
|
| 1 |
#!/usr/env/bin python3
|
| 2 |
"""
|
| 3 |
Standalone Adversarial Image Auditor Inference Module
|
| 4 |
+
Provides the necessary classes to load the pre-trained
|
| 5 |
and evaluate images.
|
| 6 |
"""
|
| 7 |
|
|
|
|
| 99 |
return text_features
|
| 100 |
|
| 101 |
# =============================================================================
|
| 102 |
+
# RESNET101 MODEL
|
| 103 |
# =============================================================================
|
| 104 |
|
| 105 |
+
class Auditor(nn.Module):
|
| 106 |
"""
|
| 107 |
ResNet101 Backbone Auditor incorporating safety classes and metrics
|
| 108 |
"""
|
| 109 |
def __init__(self, num_classes=6, vocab_size=50000):
|
| 110 |
+
super(Auditor, self).__init__()
|
| 111 |
resnet = models.resnet101(weights=None)
|
| 112 |
self.features = nn.Sequential(*list(resnet.children())[:-2])
|
| 113 |
|
|
|
|
| 224 |
Independent plug-and-play function for auditing an image
|
| 225 |
|
| 226 |
Args:
|
| 227 |
+
model: Trained Auditor model
|
| 228 |
tokenizer: Initialized SimpleTokenizer used during training
|
| 229 |
image: PIL Image
|
| 230 |
prompt: String text prompt condition
|
|
|
|
| 282 |
# EXAMPLE USAGE (Stand-alone test)
|
| 283 |
# =============================================================================
|
| 284 |
if __name__ == "__main__":
|
| 285 |
+
print("[+] Loading Auditor Architecture...")
|
| 286 |
# Initialize tokenizer
|
| 287 |
tokenizer = SimpleTokenizer(vocab_size=50000, max_length=MAX_PROMPT_LENGTH)
|
| 288 |
|
| 289 |
# Initialize the model instance
|
| 290 |
+
model = Auditor(num_classes=6, vocab_size=50000)
|
| 291 |
model.to(DEVICE)
|
| 292 |
model.eval()
|
| 293 |
|