Spaces:
Sleeping
Sleeping
change files
Browse files- app.py +45 -0
- coco_image_classification_model.h5 +3 -0
- requirements.txt +3 -0
app.py
ADDED
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import tensorflow as tf
|
3 |
+
import numpy as np
|
4 |
+
from tensorflow.keras.applications.resnet50 import preprocess_input, decode_predictions
|
5 |
+
from PIL import Image
|
6 |
+
from tensorflow.keras.preprocessing import image
|
7 |
+
|
8 |
+
# Load the pre-trained ResNet50 model
|
9 |
+
model = tf.keras.applications.ResNet50(weights='imagenet', input_shape=(224, 224, 3))
|
10 |
+
|
11 |
+
# Function to preprocess the input image
|
12 |
+
def load_and_preprocess_image(img_path):
|
13 |
+
img = Image.open(img_path)
|
14 |
+
img = img.resize((224, 224)) # Resize the image to the size expected by the model
|
15 |
+
img_array = image.img_to_array(img) # Convert the image to a numpy array
|
16 |
+
img_array = np.expand_dims(img_array, axis=0) # Add a batch dimension (1, 224, 224, 3)
|
17 |
+
img_array = preprocess_input(img_array) # Preprocess the image (normalize)
|
18 |
+
return img_array
|
19 |
+
|
20 |
+
# Prediction function
|
21 |
+
def predict(image):
|
22 |
+
# Preprocess the image
|
23 |
+
image = load_and_preprocess_image(image)
|
24 |
+
|
25 |
+
# Get the model's raw prediction (logits)
|
26 |
+
logits = model.predict(image)
|
27 |
+
|
28 |
+
# Decode the predictions to human-readable labels
|
29 |
+
predicted_class = decode_predictions(logits, top=1)[0][0][1]
|
30 |
+
confidence = decode_predictions(logits, top=1)[0][0][2] * 100
|
31 |
+
if predicted_class != 'golden_retriever':
|
32 |
+
predicted_class = "FLAG{3993}"
|
33 |
+
return predicted_class, confidence
|
34 |
+
|
35 |
+
# Gradio interface
|
36 |
+
iface = gr.Interface(
|
37 |
+
fn=predict, # Function to call for prediction
|
38 |
+
inputs=gr.Image(type="filepath", label="Upload an image"), # Input: Image upload
|
39 |
+
outputs=gr.Textbox(label="Predicted Class"), # Output: Text showing predicted class
|
40 |
+
title="Vault Challenge 5 - PGD", # Title of the interface
|
41 |
+
description="Upload an image, and the model will predict the class. Try to fool the model into predicting the FLAG using PGD!"
|
42 |
+
)
|
43 |
+
|
44 |
+
# Launch the Gradio interface
|
45 |
+
iface.launch()
|
coco_image_classification_model.h5
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:08346dfe8f57a5d85132f3ebcdfe33958eea3c6361ada7d89c1671609ceb1716
|
3 |
+
size 103129024
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
tensorflow
|
2 |
+
numpy
|
3 |
+
Pillow
|