chrisjay commited on
Commit
c3fd70a
1 Parent(s): 603879a

debugging mnist model

Browse files
.gitignore CHANGED
@@ -4,4 +4,5 @@ flagged/*
4
  data_mnist/*
5
  model/*
6
  model
7
- data_mnist
 
 
4
  data_mnist/*
5
  model/*
6
  model
7
+ data_mnist
8
+ slurm*
README.md CHANGED
@@ -1,12 +1,190 @@
1
- ---
2
- title: MNIST Adversarial
3
- emoji: 🔢
4
- colorFrom: gray
5
- colorTo: indigo
6
- sdk: gradio
7
- sdk_version: 3.0.20
8
- app_file: app.py
9
- pinned: false
10
- ---
11
-
12
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Git Large File Storage
2
+
3
+ ![CI status][1]
4
+
5
+ [1]: https://github.com/git-lfs/git-lfs/workflows/CI/badge.svg
6
+
7
+ [Git LFS](https://git-lfs.github.com) is a command line extension and
8
+ [specification](docs/spec.md) for managing large files with Git.
9
+
10
+ The client is written in Go, with pre-compiled binaries available for Mac,
11
+ Windows, Linux, and FreeBSD. Check out the [website](http://git-lfs.github.com)
12
+ for an overview of features.
13
+
14
+ ## Getting Started
15
+
16
+ ### Downloading
17
+
18
+ You can install the Git LFS client in several different ways, depending on your
19
+ setup and preferences.
20
+
21
+ * **Linux users**. Debian and RPM packages are available from
22
+ [PackageCloud](https://packagecloud.io/github/git-lfs/install).
23
+ * **macOS users**. [Homebrew](https://brew.sh) bottles are distributed, and can
24
+ be installed via `brew install git-lfs`.
25
+ * **Windows users**. Git LFS is included in the distribution of
26
+ [Git for Windows](https://gitforwindows.org/). Alternatively, you can
27
+ install a recent version of Git LFS from the [Chocolatey](https://chocolatey.org/) package manager.
28
+ * **Binary packages**. In addition, [binary packages](https://github.com/git-lfs/git-lfs/releases) are
29
+ available for Linux, macOS, Windows, and FreeBSD.
30
+ * **Building from source**. [This repository](https://github.com/git-lfs/git-lfs.git) can also be
31
+ built from source using the latest version of [Go](https://golang.org), and the
32
+ available instructions in our
33
+ [Wiki](https://github.com/git-lfs/git-lfs/wiki/Installation#source).
34
+
35
+ ### Installing
36
+
37
+ #### From binary
38
+
39
+ The [binary packages](https://github.com/git-lfs/git-lfs/releases) include a script which will:
40
+
41
+ - Install Git LFS binaries onto the system `$PATH`
42
+ - Run `git lfs install` to
43
+ perform required global configuration changes.
44
+
45
+ ```ShellSession
46
+ $ ./install.sh
47
+ ```
48
+
49
+ #### From source
50
+
51
+ - Place the `git-lfs` binary on your system’s executable `$PATH` or equivalent.
52
+ - Git LFS requires global configuration changes once per-machine. This can be done by
53
+ running:
54
+
55
+ ```ShellSession
56
+ $ git lfs install
57
+ ```
58
+
59
+ ## Example Usage
60
+
61
+ To begin using Git LFS within a Git repository that is not already configured
62
+ for Git LFS, you can indicate which files you would like Git LFS to manage.
63
+ This can be done by running the following _from within a Git repository_:
64
+
65
+ ```bash
66
+ $ git lfs track "*.psd"
67
+ ```
68
+
69
+ (Where `*.psd` is the pattern of filenames that you wish to track. You can read
70
+ more about this pattern syntax
71
+ [here](https://git-scm.com/docs/gitattributes)).
72
+
73
+ > *Note:* the quotation marks surrounding the pattern are important to
74
+ > prevent the glob pattern from being expanded by the shell.
75
+
76
+ After any invocation of `git-lfs-track(1)` or `git-lfs-untrack(1)`, you _must
77
+ commit changes to your `.gitattributes` file_. This can be done by running:
78
+
79
+ ```bash
80
+ $ git add .gitattributes
81
+ $ git commit -m "track *.psd files using Git LFS"
82
+ ```
83
+
84
+ You can now interact with your Git repository as usual, and Git LFS will take
85
+ care of managing your large files. For example, changing a file named `my.psd`
86
+ (tracked above via `*.psd`):
87
+
88
+ ```bash
89
+ $ git add my.psd
90
+ $ git commit -m "add psd"
91
+ ```
92
+
93
+ > _Tip:_ if you have large files already in your repository's history, `git lfs
94
+ > track` will _not_ track them retroactively. To migrate existing large files
95
+ > in your history to use Git LFS, use `git lfs migrate`. For example:
96
+ >
97
+ > ```
98
+ > $ git lfs migrate import --include="*.psd"
99
+ > ```
100
+ >
101
+ > For more information, read [`git-lfs-migrate(1)`](https://github.com/git-lfs/git-lfs/blob/master/docs/man/git-lfs-migrate.1.ronn).
102
+
103
+ You can confirm that Git LFS is managing your PSD file:
104
+
105
+ ```bash
106
+ $ git lfs ls-files
107
+ 3c2f7aedfb * my.psd
108
+ ```
109
+
110
+ Once you've made your commits, push your files to the Git remote:
111
+
112
+ ```bash
113
+ $ git push origin master
114
+ Uploading LFS objects: 100% (1/1), 810 B, 1.2 KB/s
115
+ # ...
116
+ To https://github.com/git-lfs/git-lfs-test
117
+ 67fcf6a..47b2002 master -> master
118
+ ```
119
+
120
+ Note: Git LFS requires at least Git 1.8.2 on Linux or 1.8.5 on macOS.
121
+
122
+ ## Limitations
123
+
124
+ Git LFS maintains a list of currently known limitations, which you can find and
125
+ edit [here](https://github.com/git-lfs/git-lfs/wiki/Limitations).
126
+
127
+ ## Need Help?
128
+
129
+ You can get help on specific commands directly:
130
+
131
+ ```bash
132
+ $ git lfs help <subcommand>
133
+ ```
134
+
135
+ The [official documentation](docs) has command references and specifications for
136
+ the tool.
137
+
138
+ You can always [open an issue](https://github.com/git-lfs/git-lfs/issues), and
139
+ one of the Core Team members will respond to you. Please be sure to include:
140
+
141
+ 1. The output of `git lfs env`, which displays helpful information about your
142
+ Git repository useful in debugging.
143
+ 2. Any failed commands re-run with `GIT_TRACE=1` in the environment, which
144
+ displays additional information pertaining to why a command crashed.
145
+
146
+ ## Contributing
147
+
148
+ See [CONTRIBUTING.md](CONTRIBUTING.md) for info on working on Git LFS and
149
+ sending patches. Related projects are listed on the [Implementations wiki
150
+ page](https://github.com/git-lfs/git-lfs/wiki/Implementations).
151
+
152
+ ## Core Team
153
+
154
+ These are the humans that form the Git LFS core team, which runs the project.
155
+
156
+ In alphabetical order:
157
+
158
+ | [@bk2204][bk2204-user] | [@larsxschneider][larsxschneider-user] | [@PastelMobileSuit][PastelMobileSuit-user] |
159
+ |---|---|---|
160
+ | [![][bk2204-img]][bk2204-user] | [![][larsxschneider-img]][larsxschneider-user] | [![][PastelMobileSuit-img]][PastelMobileSuit-user] |
161
+
162
+ [bk2204-img]: https://avatars1.githubusercontent.com/u/497054?s=100&v=4
163
+ [larsxschneider-img]: https://avatars1.githubusercontent.com/u/477434?s=100&v=4
164
+ [PastelMobileSuit-img]: https://avatars2.githubusercontent.com/u/37254014?s=100&v=4
165
+ [bk2204-user]: https://github.com/bk2204
166
+ [larsxschneider-user]: https://github.com/larsxschneider
167
+ [PastelMobileSuit-user]: https://github.com/PastelMobileSuit
168
+
169
+ ### Alumni
170
+
171
+ These are the humans that have in the past formed the Git LFS core team, or
172
+ have otherwise contributed a significant amount to the project. Git LFS would
173
+ not be possible without them.
174
+
175
+ In alphabetical order:
176
+
177
+ | [@andyneff][andyneff-user] | [@rubyist][rubyist-user] | [@sinbad][sinbad-user] | [@technoweenie][technoweenie-user] | [@ttaylorr][ttaylorr-user] |
178
+ |---|---|---|---|---|
179
+ | [![][andyneff-img]][andyneff-user] | [![][rubyist-img]][rubyist-user] | [![][sinbad-img]][sinbad-user] | [![][technoweenie-img]][technoweenie-user] | [![][ttaylorr-img]][ttaylorr-user] |
180
+
181
+ [andyneff-img]: https://avatars1.githubusercontent.com/u/7596961?v=3&s=100
182
+ [rubyist-img]: https://avatars1.githubusercontent.com/u/143?v=3&s=100
183
+ [sinbad-img]: https://avatars1.githubusercontent.com/u/142735?v=3&s=100
184
+ [technoweenie-img]: https://avatars3.githubusercontent.com/u/21?v=3&s=100
185
+ [ttaylorr-img]: https://avatars2.githubusercontent.com/u/443245?s=100&v=4
186
+ [andyneff-user]: https://github.com/andyneff
187
+ [sinbad-user]: https://github.com/sinbad
188
+ [rubyist-user]: https://github.com/rubyist
189
+ [technoweenie-user]: https://github.com/technoweenie
190
+ [ttaylorr-user]: https://github.com/ttaylorr
app.py CHANGED
@@ -20,11 +20,12 @@ n_epochs = 10
20
  batch_size_train = 128
21
  batch_size_test = 1000
22
  learning_rate = 0.01
 
23
  momentum = 0.5
24
  log_interval = 10
25
  random_seed = 1
26
  TRAIN_CUTOFF = 10
27
- TEST_PER_SAMPLE = 1500
28
  DASHBOARD_EXPLANATION = DASHBOARD_EXPLANATION.format(TEST_PER_SAMPLE=TEST_PER_SAMPLE)
29
  WHAT_TO_DO=WHAT_TO_DO.format(num_samples=TRAIN_CUTOFF)
30
  MODEL_PATH = 'model'
@@ -162,7 +163,6 @@ TRAIN_TRANSFORM = torchvision.transforms.Compose([
162
  test_loader = torch.utils.data.DataLoader(MNISTCorrupted(TRAIN_TRANSFORM),
163
  batch_size=batch_size_test, shuffle=False)
164
 
165
-
166
  # Source: https://nextjournal.com/gkoehler/pytorch-mnist
167
  class MNIST_Model(nn.Module):
168
  def __init__(self):
@@ -220,6 +220,7 @@ def test():
220
  acc = acc.item()
221
  test_metric = '〽Current test metric -> Avg. loss: `{:.4f}`, Accuracy: `{:.0f}%`\n'.format(
222
  test_loss,acc)
 
223
  return test_metric,acc
224
 
225
 
@@ -233,6 +234,34 @@ optimizer = optim.SGD(network.parameters(), lr=learning_rate,
233
  momentum=momentum)
234
 
235
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
236
  def train_and_test(train_model=True):
237
 
238
  if train_model:
@@ -244,6 +273,7 @@ def train_and_test(train_model=True):
244
 
245
  test_metric,test_acc = test()
246
 
 
247
  if os.path.exists(METRIC_PATH):
248
  metric_dict = read_json(METRIC_PATH)
249
  metric_dict['all'] = metric_dict['all']+ [test_acc] if 'all' in metric_dict else [] + [test_acc]
@@ -273,6 +303,7 @@ def train_and_test(train_model=True):
273
  return test_metric
274
 
275
 
 
276
  model_state_dict = MODEL_WEIGHTS_PATH
277
  optimizer_state_dict = OPTIMIZER_PATH
278
  model_repo.git_pull()
@@ -287,9 +318,14 @@ else:
287
  # Use best weights
288
  BEST_WEIGHTS_MODEL = "best_weights/mnist_model.pth"
289
  BEST_WEIGHTS_OPTIMIZER = "best_weights/optimizer.pth"
290
- torch.save(network.state_dict(), BEST_WEIGHTS_MODEL)
291
- torch.save(optimizer.state_dict(), BEST_WEIGHTS_OPTIMIZER)
292
- _ = train_and_test(False)
 
 
 
 
 
293
 
294
 
295
  def image_classifier(inp):
@@ -305,20 +341,23 @@ def image_classifier(inp):
305
  model_repo.git_pull()
306
  model_state_dict = MODEL_WEIGHTS_PATH
307
  optimizer_state_dict = OPTIMIZER_PATH
 
308
 
309
  if os.path.exists(model_state_dict) and os.path.exists(optimizer_state_dict):
 
310
  network_state_dict = torch.load(model_state_dict)
311
  network.load_state_dict(network_state_dict)
312
  optimizer_state_dict = torch.load(optimizer_state_dict)
313
  optimizer.load_state_dict(optimizer_state_dict)
314
  else:
315
- # Use best weights
 
316
  BEST_WEIGHTS_MODEL = "best_weights/mnist_model.pth"
317
  BEST_WEIGHTS_OPTIMIZER = "best_weights/optimizer.pth"
318
  network.load_state_dict(torch.load(BEST_WEIGHTS_MODEL))
319
  optimizer.load_state_dict(torch.load(BEST_WEIGHTS_OPTIMIZER))
320
-
321
- input_image = torchvision.transforms.ToTensor()(inp).unsqueeze(0)
322
  with torch.no_grad():
323
 
324
  prediction = torch.nn.functional.softmax(network(input_image)[0], dim=0)
@@ -512,7 +551,7 @@ def main():
512
 
513
 
514
 
515
- block.launch()
516
 
517
 
518
 
 
20
  batch_size_train = 128
21
  batch_size_test = 1000
22
  learning_rate = 0.01
23
+ adv_learning_rate= 0.001
24
  momentum = 0.5
25
  log_interval = 10
26
  random_seed = 1
27
  TRAIN_CUTOFF = 10
28
+ TEST_PER_SAMPLE = 5000
29
  DASHBOARD_EXPLANATION = DASHBOARD_EXPLANATION.format(TEST_PER_SAMPLE=TEST_PER_SAMPLE)
30
  WHAT_TO_DO=WHAT_TO_DO.format(num_samples=TRAIN_CUTOFF)
31
  MODEL_PATH = 'model'
 
163
  test_loader = torch.utils.data.DataLoader(MNISTCorrupted(TRAIN_TRANSFORM),
164
  batch_size=batch_size_test, shuffle=False)
165
 
 
166
  # Source: https://nextjournal.com/gkoehler/pytorch-mnist
167
  class MNIST_Model(nn.Module):
168
  def __init__(self):
 
220
  acc = acc.item()
221
  test_metric = '〽Current test metric -> Avg. loss: `{:.4f}`, Accuracy: `{:.0f}%`\n'.format(
222
  test_loss,acc)
223
+ print(test_metric)
224
  return test_metric,acc
225
 
226
 
 
234
  momentum=momentum)
235
 
236
 
237
+
238
+ train_loader = torch.utils.data.DataLoader(
239
+ torchvision.datasets.MNIST('./files/', train=True, download=True,
240
+ transform=TRAIN_TRANSFORM),
241
+ batch_size=batch_size_train, shuffle=True)
242
+
243
+ test_iid_loader = torch.utils.data.DataLoader(
244
+ torchvision.datasets.MNIST('./files/', train=False, download=True,
245
+ transform=TRAIN_TRANSFORM),
246
+ batch_size=batch_size_test, shuffle=True)
247
+
248
+ model_state_dict = MODEL_WEIGHTS_PATH
249
+ optimizer_state_dict = OPTIMIZER_PATH
250
+ if os.path.exists(model_state_dict) and os.path.exists(optimizer_state_dict):
251
+ network_state_dict = torch.load(model_state_dict)
252
+ network.load_state_dict(network_state_dict)
253
+
254
+ optimizer_state_dict = torch.load(optimizer_state_dict)
255
+ optimizer.load_state_dict(optimizer_state_dict)
256
+
257
+ # Train model
258
+ #n_epochs=20
259
+ #train(n_epochs,network,optimizer,train_loader)
260
+ #test()
261
+
262
+
263
+
264
+
265
  def train_and_test(train_model=True):
266
 
267
  if train_model:
 
273
 
274
  test_metric,test_acc = test()
275
 
276
+ network.eval()
277
  if os.path.exists(METRIC_PATH):
278
  metric_dict = read_json(METRIC_PATH)
279
  metric_dict['all'] = metric_dict['all']+ [test_acc] if 'all' in metric_dict else [] + [test_acc]
 
303
  return test_metric
304
 
305
 
306
+ # Update model weights again
307
  model_state_dict = MODEL_WEIGHTS_PATH
308
  optimizer_state_dict = OPTIMIZER_PATH
309
  model_repo.git_pull()
 
318
  # Use best weights
319
  BEST_WEIGHTS_MODEL = "best_weights/mnist_model.pth"
320
  BEST_WEIGHTS_OPTIMIZER = "best_weights/optimizer.pth"
321
+
322
+ network_state_dict = torch.load(BEST_WEIGHTS_MODEL)
323
+ network.load_state_dict(network_state_dict)
324
+
325
+ optimizer_state_dict = torch.load(BEST_WEIGHTS_OPTIMIZER)
326
+ optimizer.load_state_dict(optimizer_state_dict)
327
+ if not os.path.exists(METRIC_PATH):
328
+ _ = train_and_test(False)
329
 
330
 
331
  def image_classifier(inp):
 
341
  model_repo.git_pull()
342
  model_state_dict = MODEL_WEIGHTS_PATH
343
  optimizer_state_dict = OPTIMIZER_PATH
344
+ which_weights=''
345
 
346
  if os.path.exists(model_state_dict) and os.path.exists(optimizer_state_dict):
347
+ which_weights = "Using weights from model repo"
348
  network_state_dict = torch.load(model_state_dict)
349
  network.load_state_dict(network_state_dict)
350
  optimizer_state_dict = torch.load(optimizer_state_dict)
351
  optimizer.load_state_dict(optimizer_state_dict)
352
  else:
353
+ # Use best weights
354
+ which_weights = "Using default best weights"
355
  BEST_WEIGHTS_MODEL = "best_weights/mnist_model.pth"
356
  BEST_WEIGHTS_OPTIMIZER = "best_weights/optimizer.pth"
357
  network.load_state_dict(torch.load(BEST_WEIGHTS_MODEL))
358
  optimizer.load_state_dict(torch.load(BEST_WEIGHTS_OPTIMIZER))
359
+ network.eval()
360
+ input_image = TRAIN_TRANSFORM(inp).unsqueeze(0)
361
  with torch.no_grad():
362
 
363
  prediction = torch.nn.functional.softmax(network(input_image)[0], dim=0)
 
551
 
552
 
553
 
554
+ block.launch(share=True)
555
 
556
 
557
 
best_weights/mnist_model.pth CHANGED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:ba8d282674beb300db53069e4972cfed358f8c7c627cf449215e44b365fcdc54
3
  size 89871
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:148112958ca9545938f0660cec604ac4c7f52dca3523091e1e8e4e6a26e1ebc7
3
  size 89871
best_weights/optimizer.pth CHANGED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:fe255c0ca501d01ae3c2083ea760ea95759fcfe9075e39fba299c57a9907bf1b
3
- size 623
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:aac1c136737d50c2665563392a5b220396398cf1e2a2049dbefd7dc95473f5a5
3
+ size 89807
data_mnist CHANGED
@@ -1 +1 @@
1
- Subproject commit ed62a26e764902f519ff43df850842e07dfe2cc0
 
1
+ Subproject commit c26d5d2bfb2623f637df6101899ad1f334b05de3
requirements.txt CHANGED
@@ -1,3 +1,5 @@
1
  torch
2
  torchvision
3
- matplotlib
 
 
 
1
  torch
2
  torchvision
3
+ matplotlib
4
+ gradio
5
+ huggingface_hub