Spaces:
Sleeping
Sleeping
Add files
Browse files- app.py +62 -0
- cifar10_modified_flag.weights.h5 +3 -0
- requirements.txt +3 -0
app.py
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import tensorflow as tf
|
| 3 |
+
import numpy as np
|
| 4 |
+
from PIL import Image
|
| 5 |
+
from tensorflow.keras import datasets, layers, models
|
| 6 |
+
|
| 7 |
+
# Load the trained model
|
| 8 |
+
model = models.Sequential([
|
| 9 |
+
layers.Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3)),
|
| 10 |
+
layers.MaxPooling2D((2, 2)),
|
| 11 |
+
layers.Conv2D(64, (3, 3), activation='relu'),
|
| 12 |
+
layers.MaxPooling2D((2, 2)),
|
| 13 |
+
layers.Conv2D(64, (3, 3), activation='relu'),
|
| 14 |
+
layers.Flatten(),
|
| 15 |
+
layers.Dense(64, activation='relu'),
|
| 16 |
+
layers.Dense(10) # 10 classes in CIFAR-10
|
| 17 |
+
])
|
| 18 |
+
|
| 19 |
+
model.load_weights("cifar10_modified_flag.weights.h5")
|
| 20 |
+
|
| 21 |
+
# class 3 is a cat
|
| 22 |
+
# Class mapping (0-9 with class 3 replaced by "FLAG{3883}")
|
| 23 |
+
class_mapping = {0: "airplane", 1: "automobile", 2: "bird", 3: "FLAG{3883}", 4: "deer",
|
| 24 |
+
5: "dog", 6: "frog", 7: "horse", 8: "ship", 9: "truck"}
|
| 25 |
+
|
| 26 |
+
# Function to preprocess the input image
|
| 27 |
+
def preprocess_image(image):
|
| 28 |
+
image = image.resize((32, 32)) # Resize to CIFAR-10 size
|
| 29 |
+
image = np.array(image) / 255.0 # Normalize pixel values
|
| 30 |
+
image = np.expand_dims(image, axis=0) # Add batch dimension
|
| 31 |
+
return image
|
| 32 |
+
|
| 33 |
+
# Prediction function
|
| 34 |
+
def predict(image):
|
| 35 |
+
# Preprocess the image
|
| 36 |
+
image = preprocess_image(image)
|
| 37 |
+
|
| 38 |
+
# Get the model's raw prediction (logits)
|
| 39 |
+
logits = model.predict(image)
|
| 40 |
+
|
| 41 |
+
# Convert logits to probabilities
|
| 42 |
+
probabilities = tf.nn.softmax(logits, axis=-1)
|
| 43 |
+
|
| 44 |
+
# Get the predicted class index
|
| 45 |
+
predicted_class = np.argmax(probabilities)
|
| 46 |
+
|
| 47 |
+
# Get the class name from the mapping
|
| 48 |
+
class_name = class_mapping[predicted_class]
|
| 49 |
+
|
| 50 |
+
return class_name
|
| 51 |
+
|
| 52 |
+
# Gradio interface
|
| 53 |
+
iface = gr.Interface(
|
| 54 |
+
fn=predict, # Function to call for prediction
|
| 55 |
+
inputs=gr.Image(type="pil", label="Upload an image from CIFAR-10"), # Input: Image upload
|
| 56 |
+
outputs=gr.Textbox(label="Predicted Class"), # Output: Text showing predicted class
|
| 57 |
+
title="Vault Challenge 2 - BIM", # Title of the interface
|
| 58 |
+
description="Upload an image, and the model will predict the class. Try to fool the model into predicting the FLAG using BIM!. Tips: tune the parameters to make the model predict the image as a cat (class 3)."
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
# Launch the Gradio interface
|
| 62 |
+
iface.launch()
|
cifar10_modified_flag.weights.h5
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:05cfd6d1a78b96d27809ec293d7a8b51e8d940f154ba45ac2722ed416cdec749
|
| 3 |
+
size 1503168
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
tensorflow
|
| 2 |
+
numpy
|
| 3 |
+
Pillow
|