hugolb commited on
Commit
80e64d9
1 Parent(s): f20c4f4

change files

Browse files
Files changed (3) hide show
  1. app.py +142 -0
  2. model.pth +3 -0
  3. requirements.ttx +3 -0
app.py ADDED
@@ -0,0 +1,142 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ import torch.nn as nn
4
+ from torchvision import transforms
5
+ from PIL import Image
6
+ import torch.nn.functional as F
7
+
8
+ device = torch.device("cpu")
9
+
10
+ class VGGBlock(nn.Module):
11
+ def __init__(self, in_channels, out_channels, batch_norm=False):
12
+ super().__init__()
13
+ conv2_params = {'kernel_size': (3, 3),
14
+ 'stride' : (1, 1),
15
+ 'padding' : 1}
16
+
17
+ noop = lambda x : x
18
+ self._batch_norm = batch_norm
19
+
20
+ self.conv1 = nn.Conv2d(in_channels=in_channels, out_channels=out_channels , **conv2_params)
21
+ self.bn1 = nn.BatchNorm2d(out_channels) if batch_norm else noop
22
+
23
+ self.conv2 = nn.Conv2d(in_channels=out_channels, out_channels=out_channels, **conv2_params)
24
+ self.bn2 = nn.BatchNorm2d(out_channels) if batch_norm else noop
25
+
26
+ self.max_pooling = nn.MaxPool2d(kernel_size=(2, 2), stride=(2, 2))
27
+
28
+ @property
29
+ def batch_norm(self):
30
+ return self._batch_norm
31
+
32
+ def forward(self,x):
33
+ x = self.conv1(x)
34
+ x = self.bn1(x)
35
+ x = F.relu(x)
36
+
37
+ x = self.conv2(x)
38
+ x = self.bn2(x)
39
+ x = F.relu(x)
40
+
41
+ x = self.max_pooling(x)
42
+
43
+ return x
44
+
45
+
46
+ class VGG16(nn.Module):
47
+ def __init__(self, input_size, num_classes=10, batch_norm=False):
48
+ super(VGG16, self).__init__()
49
+
50
+ self.in_channels, self.in_width, self.in_height = input_size
51
+
52
+ self.block_1 = VGGBlock(self.in_channels, 64, batch_norm=batch_norm)
53
+ self.block_2 = VGGBlock(64, 128, batch_norm=batch_norm)
54
+ self.block_3 = VGGBlock(128, 256, batch_norm=batch_norm)
55
+ self.block_4 = VGGBlock(256,512, batch_norm=batch_norm)
56
+
57
+ self.classifier = nn.Sequential(
58
+ nn.Linear(2048, 4096),
59
+ nn.ReLU(True),
60
+ nn.Dropout(p=0.65),
61
+ nn.Linear(4096, 4096),
62
+ nn.ReLU(True),
63
+ nn.Dropout(p=0.65),
64
+ nn.Linear(4096, num_classes)
65
+ )
66
+
67
+ @property
68
+ def input_size(self):
69
+ return self.in_channels, self.in_width, self.in_height
70
+
71
+ def forward(self, x):
72
+ x = self.block_1(x)
73
+ x = self.block_2(x)
74
+ x = self.block_3(x)
75
+ x = self.block_4(x)
76
+ x = x.view(x.size(0), -1)
77
+ x = self.classifier(x)
78
+
79
+ return x
80
+
81
+
82
+ model = VGG16((1,32,32), batch_norm=True)
83
+ model.to(device)
84
+ # Load the saved checkpoint
85
+ model.load_state_dict(torch.load('model.pth', map_location=device))
86
+
87
+
88
+ label_map = {
89
+ 0: 'T-shirt/top',
90
+ 1: 'Trouser',
91
+ 2: 'Pullover',
92
+ 3: 'Dress',
93
+ 4: 'Coat',
94
+ 5: 'Sandal',
95
+ 6: 'Shirt',
96
+ 7: 'Sneaker',
97
+ 8: 'FLAG{3883}',
98
+ 9: 'Ankle boot'
99
+ }
100
+
101
+ def predict_from_local_image(image: str):
102
+ # Define the transformation to match the model's input requirements
103
+
104
+ transform = transforms.Compose([
105
+ transforms.Resize((32, 32)), # Resize to the input size of the model
106
+ transforms.ToTensor(), # Convert the image to a tensor
107
+ ])
108
+
109
+ # Load the image
110
+ image = Image.open(image).convert('L') # Convert numpy array to PIL image and then to grayscale if necessary
111
+ image = transform(image).unsqueeze(0) # Add batch dimension
112
+
113
+ # Move the image to the specified device
114
+ image = image.to(device)
115
+
116
+ # Set the model to evaluation mode
117
+ model.eval()
118
+
119
+ # Make a prediction
120
+ with torch.no_grad():
121
+ output = model(image)
122
+ _, predicted_label = torch.max(output, 1)
123
+ confidence = torch.nn.functional.softmax(output, dim=1)[0] * 100
124
+
125
+ # Get the predicted class label and confidence
126
+ predicted_class = label_map[predicted_label.item()]
127
+ predicted_confidence = confidence[predicted_label.item()].item()
128
+
129
+ return predicted_class, predicted_confidence
130
+
131
+
132
+ # Gradio interface
133
+ iface = gr.Interface(
134
+ fn=predict_from_local_image, # Function to call for prediction
135
+ inputs=gr.Image(type='filepath', label="Upload an image"), # Input: .pt file upload
136
+ outputs=gr.Textbox(label="Predicted Class"), # Output: Text showing predicted class
137
+ title="Vault Challenge 4 - DeepFool", # Title of the interface
138
+ description="Upload an image, and the model will predict the class. Try to fool the model into predicting the FLAG using DeepFool! Tips: apply DeepFool attack on the image to make the model predict it as a BAG. Note that you should save the adverserial image as a .pt file and upload it to the model to get the FLAG."
139
+ )
140
+
141
+ # Launch the Gradio interface
142
+ iface.launch()
model.pth ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:7d2ddbca9b99982d2cbecbbd61174e4dd3f0a06f92ae3f30bbb45003bb66d1ee
3
+ size 119648747
requirements.ttx ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ torch
2
+ torchvision
3
+ Pillow