Skip to content

Commit

Permalink
completed adversarial images
Browse files Browse the repository at this point in the history
  • Loading branch information
brightredchilli committed May 16, 2017
1 parent 826b283 commit f1ca0d0
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 13 deletions.
47 changes: 41 additions & 6 deletions assignment3/ImageGradients.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
"collapsed": false
},
"outputs": [],
"source": [
Expand All @@ -214,7 +214,15 @@
" # methods of the PretrainedCNN class, and compute gradients with respect to #\n",
" # the unnormalized class score of the ground-truth classes in y. #\n",
" ##############################################################################\n",
" pass\n",
" N = X.shape[0]\n",
" scores, cache = model.forward(X)\n",
" # only count the scores of the target class\n",
" mask = np.zeros_like(scores)\n",
" mask[np.arange(N), y] = 1\n",
" saliency, grads = model.backward(scores * mask, cache)\n",
"\n",
" saliency = saliency.max(1) # take maximum over rgb channels\n",
" \n",
" ##############################################################################\n",
" # END OF YOUR CODE #\n",
" ##############################################################################\n",
Expand Down Expand Up @@ -277,10 +285,11 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
"collapsed": false
},
"outputs": [],
"source": [
"from cs231n.layers import * \n",
"def make_fooling_image(X, target_y, model):\n",
" \"\"\"\n",
" Generate a fooling image that is close to X, but that the model classifies\n",
Expand All @@ -305,7 +314,25 @@
" # HINT: For most examples, you should be able to generate a fooling image #\n",
" # in fewer than 100 iterations of gradient ascent. #\n",
" ##############################################################################\n",
" pass\n",
"\n",
" N = X.shape[0]\n",
" iter = 0\n",
" lr = 1e3\n",
" target_y = np.asarray(target_y)\n",
" scores, cache = model.forward(X_fooling)\n",
" while np.argmax(scores) != target_y:\n",
" loss, dscores = softmax_loss(scores, target_y)\n",
" dX, grads = model.backward(dscores, cache)\n",
" X_fooling -= dX * lr\n",
" scores, cache = model.forward(X_fooling)\n",
" if iter % 10 == 0:\n",
" print \"iter {}: current={} score={} y={} yscore={}\" \\\n",
" .format(iter, np.argmax(scores), np.max(scores), target_y, scores[0, target_y])\n",
" iter += 1\n",
" if iter == 100:\n",
" break\n",
"\n",
" plt.imshow(deprocess_image(X_fooling, data['mean_image'], renorm=True))\n",
" ##############################################################################\n",
" # END OF YOUR CODE #\n",
" ##############################################################################\n",
Expand Down Expand Up @@ -335,7 +362,6 @@
" y_pred = model.loss(X)[0].argmax()\n",
" if y_pred == y: break\n",
"\n",
"target_y = 67\n",
"X_fooling = make_fooling_image(X, target_y, model)\n",
"\n",
"# Make sure that X_fooling is classified as y_target\n",
Expand All @@ -357,6 +383,15 @@
"plt.axis('off')\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
Expand All @@ -375,7 +410,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.6"
"version": "2.7.10"
}
},
"nbformat": 4,
Expand Down
14 changes: 7 additions & 7 deletions assignment3/cs231n/classifiers/pretrained_cnn.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def __init__(self, dtype=np.float32, num_classes=100, input_size=64, h5_file=Non
self.conv_params = []
self.input_size = input_size
self.num_classes = num_classes

# TODO: In the future it would be nice if the architecture could be loaded from
# the HDF5 file rather than being hardcoded. For now this will have to do.
self.conv_params.append({'stride': 2, 'pad': 2})
Expand All @@ -30,7 +30,7 @@ def __init__(self, dtype=np.float32, num_classes=100, input_size=64, h5_file=Non
hidden_dim = 512

self.bn_params = []

cur_size = input_size
prev_dim = 3
self.params = {}
Expand All @@ -43,7 +43,7 @@ def __init__(self, dtype=np.float32, num_classes=100, input_size=64, h5_file=Non
self.bn_params.append({'mode': 'train'})
prev_dim = next_dim
if self.conv_params[i]['stride'] == 2: cur_size /= 2

# Add a fully-connected layers
fan_in = cur_size * cur_size * self.num_filters[-1]
self.params['W%d' % (i + 2)] = np.sqrt(2.0 / fan_in) * np.random.randn(fan_in, hidden_dim)
Expand All @@ -53,14 +53,14 @@ def __init__(self, dtype=np.float32, num_classes=100, input_size=64, h5_file=Non
self.bn_params.append({'mode': 'train'})
self.params['W%d' % (i + 3)] = np.sqrt(2.0 / hidden_dim) * np.random.randn(hidden_dim, num_classes)
self.params['b%d' % (i + 3)] = np.zeros(num_classes)

for k, v in self.params.iteritems():
self.params[k] = v.astype(dtype)

if h5_file is not None:
self.load_weights(h5_file)


def load_weights(self, h5_file, verbose=False):
"""
Load pretrained weights from an HDF5 file.
Expand Down Expand Up @@ -97,11 +97,11 @@ def load_weights(self, h5_file, verbose=False):
assert v.shape == self.bn_params[i]['running_var'].shape
self.bn_params[i]['running_var'] = v.copy()
if verbose: print k, v.shape

for k, v in self.params.iteritems():
self.params[k] = v.astype(self.dtype)


def forward(self, X, start=None, end=None, mode='test'):
"""
Run part of the model forward, starting and ending at an arbitrary layer,
Expand Down

0 comments on commit f1ca0d0

Please sign in to comment.