-
Notifications
You must be signed in to change notification settings - Fork 18.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Python] PythonLayer takes parameters by string and better exception handling #2001
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
80c4a9c
Error in python can be shown
tnarihi 38ddc14
PythonLayer takes parameters by string
tnarihi bf5de76
Check injections in PythonLayer
tnarihi 471e71d
PythonLayer allows to import nested modules
tnarihi 7b96527
CMake dependency to boost regex
tnarihi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import unittest | ||
import tempfile | ||
|
||
import os | ||
|
||
import caffe | ||
|
||
class ElemwiseScalarLayer(caffe.Layer): | ||
"""A layer that just multiplies by ten""" | ||
|
||
def setup(self, bottom, top): | ||
self.layer_params_ = eval(self.param_str_) | ||
self.value_ = self.layer_params_['value'] | ||
if self.layer_params_['op'].lower() == 'add': | ||
self._forward = self._forward_add | ||
self._backward = self._backward_add | ||
elif self.layer_params_['op'].lower() == 'mul': | ||
self._forward = self._forward_mul | ||
self._backward = self._backward_mul | ||
else: | ||
raise ValueError("Unknown operation type: '%s'" | ||
% self.layer_params_['op'].lower()) | ||
def _forward_add(self, bottom, top): | ||
top[0].data[...] = bottom[0].data + self.value_ | ||
def _backward_add(self, bottom, propagate_down, top): | ||
bottom[0].diff[...] = top[0].diff | ||
def _forward_mul(self, bottom, top): | ||
top[0].data[...] = bottom[0].data * self.value_ | ||
def _backward_mul(self, bottom, propagate_down, top): | ||
bottom[0].diff[...] = top[0].diff * self.value_ | ||
def reshape(self, bottom, top): | ||
top[0].reshape(bottom[0].num, bottom[0].channels, bottom[0].height, | ||
bottom[0].width) | ||
def forward(self, bottom, top): | ||
self._forward(bottom, top) | ||
def backward(self, top, propagate_down, bottom): | ||
self._backward(bottom, propagate_down, top) | ||
|
||
|
||
def python_net_file(): | ||
f = tempfile.NamedTemporaryFile(delete=False) | ||
f.write(r"""name: 'pythonnet' force_backward: true | ||
input: 'data' input_dim: 10 input_dim: 9 input_dim: 8 input_dim: 7 | ||
layer { type: 'Python' name: 'one' bottom: 'data' top: 'one' | ||
python_param { | ||
module: 'test_python_layer_with_param_str' layer: 'ElemwiseScalarLayer' | ||
param_str: "{'op': 'add', 'value': 2}" } } | ||
layer { type: 'Python' name: 'two' bottom: 'one' top: 'two' | ||
python_param { | ||
module: 'test_python_layer_with_param_str' layer: 'ElemwiseScalarLayer' | ||
param_str: "{'op': 'mul', 'value': 3}" } } | ||
layer { type: 'Python' name: 'three' bottom: 'two' top: 'three' | ||
python_param { | ||
module: 'test_python_layer_with_param_str' layer: 'ElemwiseScalarLayer' | ||
param_str: "{'op': 'add', 'value': 10}" } }""") | ||
f.close() | ||
return f.name | ||
|
||
class TestLayerWithParam(unittest.TestCase): | ||
def setUp(self): | ||
net_file = python_net_file() | ||
self.net = caffe.Net(net_file, caffe.TRAIN) | ||
os.remove(net_file) | ||
|
||
def test_forward(self): | ||
x = 8 | ||
self.net.blobs['data'].data[...] = x | ||
self.net.forward() | ||
for y in self.net.blobs['three'].data.flat: | ||
self.assertEqual(y, (x + 2) * 3 + 10) | ||
|
||
def test_backward(self): | ||
x = 7 | ||
self.net.blobs['three'].diff[...] = x | ||
self.net.backward() | ||
for y in self.net.blobs['data'].diff.flat: | ||
self.assertEqual(y, 3 * x) | ||
|
||
def test_reshape(self): | ||
s = 4 | ||
self.net.blobs['data'].reshape(s, s, s, s) | ||
self.net.forward() | ||
for blob in self.net.blobs.itervalues(): | ||
for d in blob.data.shape: | ||
self.assertEqual(s, d) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't it dangerous to call
eval
andexec
on user strings that haven't been checked for commands? That is, we should first validate thatmodule_name
andlayer_name
both match the regex^[a-zA-Z_][a-zA-Z0-9_]*$
. Without this check, injection attacks like this one are possible:I understand that for most use cases, this isn't a concern, but I think it's good practice to avoid introducing unnecessary vulnerabilities.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed with @seanbell that if we're going to
eval
/exec
it's worth doing a check like the one he suggests. Is it necessary to switch away frombp::import
though? I'm not sure why it's needed, is it to load__dict__
first? Couldparam_str
just be a string, and then Python layer writers couldeval
it as a dict on their own if they want it to take a dict?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@tnarihi explains the use of
eval
/exec
in one of the commit messages ( tnarihi@64a2b04 )There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, thanks, missed that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for commenting. I also agree with @seanbell . To avoid injections would be nice. I will modify it soon using boost regex. Thanks for giving me the regex pattern, @seanbell ! I will use this.