Spaces:
Runtime error
Runtime error
retry uploading
Browse files- .gitattributes +1 -0
- app.py +154 -0
- files/MNIST/raw/t10k-images-idx3-ubyte +3 -0
- files/MNIST/raw/t10k-images-idx3-ubyte.gz +3 -0
- files/MNIST/raw/t10k-labels-idx1-ubyte +3 -0
- files/MNIST/raw/t10k-labels-idx1-ubyte.gz +3 -0
- files/MNIST/raw/train-images-idx3-ubyte +3 -0
- files/MNIST/raw/train-images-idx3-ubyte.gz +3 -0
- files/MNIST/raw/train-labels-idx1-ubyte +3 -0
- files/MNIST/raw/train-labels-idx1-ubyte.gz +3 -0
- model.pth +3 -0
- optimizer.pth +3 -0
.gitattributes
CHANGED
|
@@ -25,3 +25,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 25 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 26 |
*.zstandard filter=lfs diff=lfs merge=lfs -text
|
| 27 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
| 25 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 26 |
*.zstandard filter=lfs diff=lfs merge=lfs -text
|
| 27 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 28 |
+
*ubyte* filter=lfs diff=lfs merge=lfs -text
|
app.py
ADDED
|
@@ -0,0 +1,154 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import torch
|
| 3 |
+
import gradio as gr
|
| 4 |
+
import torchvision
|
| 5 |
+
import torch.nn as nn
|
| 6 |
+
import torch.nn.functional as F
|
| 7 |
+
import torch.optim as optim
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
n_epochs = 3
|
| 11 |
+
batch_size_train = 64
|
| 12 |
+
batch_size_test = 1000
|
| 13 |
+
learning_rate = 0.01
|
| 14 |
+
momentum = 0.5
|
| 15 |
+
log_interval = 10
|
| 16 |
+
|
| 17 |
+
random_seed = 1
|
| 18 |
+
torch.backends.cudnn.enabled = False
|
| 19 |
+
torch.manual_seed(random_seed)
|
| 20 |
+
|
| 21 |
+
train_loader = torch.utils.data.DataLoader(
|
| 22 |
+
torchvision.datasets.MNIST('files/', train=True, download=True,
|
| 23 |
+
transform=torchvision.transforms.Compose([
|
| 24 |
+
torchvision.transforms.ToTensor(),
|
| 25 |
+
torchvision.transforms.Normalize(
|
| 26 |
+
(0.1307,), (0.3081,))
|
| 27 |
+
])),
|
| 28 |
+
batch_size=batch_size_train, shuffle=True)
|
| 29 |
+
|
| 30 |
+
test_loader = torch.utils.data.DataLoader(
|
| 31 |
+
torchvision.datasets.MNIST('files/', train=False, download=True,
|
| 32 |
+
transform=torchvision.transforms.Compose([
|
| 33 |
+
torchvision.transforms.ToTensor(),
|
| 34 |
+
torchvision.transforms.Normalize(
|
| 35 |
+
(0.1307,), (0.3081,))
|
| 36 |
+
])),
|
| 37 |
+
batch_size=batch_size_test, shuffle=True)
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
# Source: https://nextjournal.com/gkoehler/pytorch-mnist
|
| 41 |
+
class MNIST_Model(nn.Module):
|
| 42 |
+
def __init__(self):
|
| 43 |
+
super(MNIST_Model, self).__init__()
|
| 44 |
+
self.conv1 = nn.Conv2d(1, 10, kernel_size=5)
|
| 45 |
+
self.conv2 = nn.Conv2d(10, 20, kernel_size=5)
|
| 46 |
+
self.conv2_drop = nn.Dropout2d()
|
| 47 |
+
self.fc1 = nn.Linear(320, 50)
|
| 48 |
+
self.fc2 = nn.Linear(50, 10)
|
| 49 |
+
|
| 50 |
+
def forward(self, x):
|
| 51 |
+
x = F.relu(F.max_pool2d(self.conv1(x), 2))
|
| 52 |
+
x = F.relu(F.max_pool2d(self.conv2_drop(self.conv2(x)), 2))
|
| 53 |
+
x = x.view(-1, 320)
|
| 54 |
+
x = F.relu(self.fc1(x))
|
| 55 |
+
x = F.dropout(x, training=self.training)
|
| 56 |
+
x = self.fc2(x)
|
| 57 |
+
return F.log_softmax(x)
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
def train(epochs,network,optimizer):
|
| 61 |
+
|
| 62 |
+
train_losses=[]
|
| 63 |
+
network.train()
|
| 64 |
+
for epoch in range(epochs):
|
| 65 |
+
for batch_idx, (data, target) in enumerate(train_loader):
|
| 66 |
+
optimizer.zero_grad()
|
| 67 |
+
output = network(data)
|
| 68 |
+
loss = F.nll_loss(output, target)
|
| 69 |
+
loss.backward()
|
| 70 |
+
optimizer.step()
|
| 71 |
+
if batch_idx % log_interval == 0:
|
| 72 |
+
print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format(
|
| 73 |
+
epoch, batch_idx * len(data), len(train_loader.dataset),
|
| 74 |
+
100. * batch_idx / len(train_loader), loss.item()))
|
| 75 |
+
train_losses.append(loss.item())
|
| 76 |
+
|
| 77 |
+
torch.save(network.state_dict(), 'model.pth')
|
| 78 |
+
torch.save(optimizer.state_dict(), 'optimizer.pth')
|
| 79 |
+
|
| 80 |
+
def test():
|
| 81 |
+
test_losses=[]
|
| 82 |
+
network.eval()
|
| 83 |
+
test_loss = 0
|
| 84 |
+
correct = 0
|
| 85 |
+
with torch.no_grad():
|
| 86 |
+
for data, target in test_loader:
|
| 87 |
+
output = network(data)
|
| 88 |
+
test_loss += F.nll_loss(output, target, size_average=False).item()
|
| 89 |
+
pred = output.data.max(1, keepdim=True)[1]
|
| 90 |
+
correct += pred.eq(target.data.view_as(pred)).sum()
|
| 91 |
+
test_loss /= len(test_loader.dataset)
|
| 92 |
+
test_losses.append(test_loss)
|
| 93 |
+
print('\nTest set: Avg. loss: {:.4f}, Accuracy: {}/{} ({:.0f}%)\n'.format(
|
| 94 |
+
test_loss, correct, len(test_loader.dataset),
|
| 95 |
+
100. * correct / len(test_loader.dataset)))
|
| 96 |
+
|
| 97 |
+
|
| 98 |
+
|
| 99 |
+
random_seed = 1
|
| 100 |
+
torch.backends.cudnn.enabled = False
|
| 101 |
+
torch.manual_seed(random_seed)
|
| 102 |
+
|
| 103 |
+
network = MNIST_Model()
|
| 104 |
+
optimizer = optim.SGD(network.parameters(), lr=learning_rate,
|
| 105 |
+
momentum=momentum)
|
| 106 |
+
|
| 107 |
+
|
| 108 |
+
model_state_dict = 'model.pth'
|
| 109 |
+
optimizer_state_dict = 'optmizer.pth'
|
| 110 |
+
|
| 111 |
+
if os.path.exists(model_state_dict):
|
| 112 |
+
network_state_dict = torch.load(model_state_dict)
|
| 113 |
+
network.load_state_dict(network_state_dict)
|
| 114 |
+
|
| 115 |
+
if os.path.exists(optimizer_state_dict):
|
| 116 |
+
optimizer_state_dict = torch.load(optimizer_state_dict)
|
| 117 |
+
optimizer.load_state_dict(optimizer_state_dict)
|
| 118 |
+
|
| 119 |
+
|
| 120 |
+
|
| 121 |
+
# Train
|
| 122 |
+
#train(n_epochs,network,optimizer)
|
| 123 |
+
|
| 124 |
+
|
| 125 |
+
def image_classifier(inp):
|
| 126 |
+
input_image = torchvision.transforms.ToTensor()(inp).unsqueeze(0)
|
| 127 |
+
with torch.no_grad():
|
| 128 |
+
|
| 129 |
+
prediction = torch.nn.functional.softmax(network(input_image)[0], dim=0)
|
| 130 |
+
#pred_number = prediction.data.max(1, keepdim=True)[1]
|
| 131 |
+
sorted_prediction = torch.sort(prediction,descending=True)
|
| 132 |
+
confidences={}
|
| 133 |
+
for s,v in zip(sorted_prediction.indices.numpy().tolist(),sorted_prediction.values.numpy().tolist()):
|
| 134 |
+
confidences.update({s:v})
|
| 135 |
+
return confidences
|
| 136 |
+
|
| 137 |
+
TITLE = "MNIST Adversarial: Try to fool the MNIST model"
|
| 138 |
+
description = """This project is about dynamic adversarial data collection (DADC).
|
| 139 |
+
The basic idea is to do data collection, but specifically collect “adversarial data”, the kind of data that is difficult for a model to predict correctly.
|
| 140 |
+
This kind of data is presumably the most valuable for a model, so this can be helpful in low-resource settings where data is hard to collect and label.
|
| 141 |
+
|
| 142 |
+
### What to do:
|
| 143 |
+
- Draw a number from 0-9.
|
| 144 |
+
- Click `Submit` and see the model's prediciton.
|
| 145 |
+
- If the model misclassifies it, Flag that example.
|
| 146 |
+
- This will add your (adversarial) example to a dataset on which the model will be trained later.
|
| 147 |
+
"""
|
| 148 |
+
gr.Interface(fn=image_classifier,
|
| 149 |
+
inputs=gr.Image(source="canvas",shape=(28,28),invert_colors=True,image_mode="L",type="pil"),
|
| 150 |
+
outputs=gr.outputs.Label(num_top_classes=10),
|
| 151 |
+
allow_flagging="manual",
|
| 152 |
+
title = TITLE,
|
| 153 |
+
description=description).launch()
|
| 154 |
+
|
files/MNIST/raw/t10k-images-idx3-ubyte
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:0fa7898d509279e482958e8ce81c8e77db3f2f8254e26661ceb7762c4d494ce7
|
| 3 |
+
size 7840016
|
files/MNIST/raw/t10k-images-idx3-ubyte.gz
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:8d422c7b0a1c1c79245a5bcf07fe86e33eeafee792b84584aec276f5a2dbc4e6
|
| 3 |
+
size 1648877
|
files/MNIST/raw/t10k-labels-idx1-ubyte
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:ff7bcfd416de33731a308c3f266cc351222c34898ecbeaf847f06e48f7ec33f2
|
| 3 |
+
size 10008
|
files/MNIST/raw/t10k-labels-idx1-ubyte.gz
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:f7ae60f92e00ec6debd23a6088c31dbd2371eca3ffa0defaefb259924204aec6
|
| 3 |
+
size 4542
|
files/MNIST/raw/train-images-idx3-ubyte
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:ba891046e6505d7aadcbbe25680a0738ad16aec93bde7f9b65e87a2fc25776db
|
| 3 |
+
size 47040016
|
files/MNIST/raw/train-images-idx3-ubyte.gz
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:440fcabf73cc546fa21475e81ea370265605f56be210a4024d2ca8f203523609
|
| 3 |
+
size 9912422
|
files/MNIST/raw/train-labels-idx1-ubyte
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:65a50cbbf4e906d70832878ad85ccda5333a97f0f4c3dd2ef09a8a9eef7101c5
|
| 3 |
+
size 60008
|
files/MNIST/raw/train-labels-idx1-ubyte.gz
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:3552534a0a558bbed6aed32b30c495cca23d567ec52cac8be1a0730e8010255c
|
| 3 |
+
size 28881
|
model.pth
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:ffe16177c76477e22a35b45ac44d3a06f758d07df5ca37379a490ed69f7ff80e
|
| 3 |
+
size 89871
|
optimizer.pth
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:ea0b9cee5af7847bf896b2c9692b1cf36fe447d67be87d1b98634091f20babe6
|
| 3 |
+
size 89807
|