forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Dper2] Add NetModifier abstraction and support for plotting the norm…
… of blobs (pytorch#2201)
- Loading branch information
Showing
7 changed files
with
425 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
# Copyright (c) 2016-present, Facebook, Inc. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
############################################################################## | ||
|
||
from __future__ import absolute_import | ||
from __future__ import division | ||
from __future__ import print_function | ||
from __future__ import unicode_literals | ||
|
||
from caffe2.python import core, schema | ||
from caffe2.python.modeling.net_modifier import NetModifier | ||
|
||
import numpy as np | ||
|
||
|
||
class ComputeNormForBlobs(NetModifier): | ||
""" | ||
This class modifies the net passed in by adding ops to compute norms for | ||
certain blobs. | ||
Args: | ||
blobs: list of blobs to compute norm for | ||
logging_frequency: frequency for printing norms to logs | ||
p: type of norm. Currently it supports p=1 or p=2 | ||
""" | ||
|
||
def __init__(self, blobs, logging_frequency, p=2): | ||
self._blobs = blobs | ||
self._logging_frequency = logging_frequency | ||
self._p = p | ||
|
||
def modify_net(self, net, init_net=None, grad_map=None, blob_to_device=None): | ||
|
||
p = self._p | ||
|
||
for blob_name in self._blobs: | ||
blob = core.BlobReference(blob_name) | ||
if not net.BlobIsDefined(blob): | ||
raise Exception('blob {0} is not defined in net {1}'.format( | ||
blob, net.Name())) | ||
|
||
norm_name = net.NextScopedBlob(prefix=blob + '_l{}_norm'.format(p)) | ||
norm = net.LpNorm(blob, norm_name, p=p) | ||
|
||
if self._logging_frequency >= 1: | ||
net.Print(norm, [], every_n=self._logging_frequency) | ||
|
||
output_field_name = str(blob) + '_l{}_norm'.format(p) | ||
output_scalar = schema.Scalar((np.float, (1,)), norm) | ||
|
||
if net.output_record() is None: | ||
net.set_output_record( | ||
schema.Struct((output_field_name, output_scalar)) | ||
) | ||
else: | ||
net.AppendOutputRecordField( | ||
output_field_name, | ||
output_scalar) |
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,133 @@ | ||
# Copyright (c) 2016-present, Facebook, Inc. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
############################################################################## | ||
|
||
from __future__ import absolute_import | ||
from __future__ import division | ||
from __future__ import print_function | ||
from __future__ import unicode_literals | ||
|
||
import unittest | ||
from caffe2.python import workspace, brew, model_helper | ||
from caffe2.python.modeling.compute_norm_for_blobs import ComputeNormForBlobs | ||
|
||
import numpy as np | ||
|
||
|
||
class ComputeNormForBlobsTest(unittest.TestCase): | ||
def test_compute_norm_for_blobs(self): | ||
model = model_helper.ModelHelper(name="test") | ||
data = model.net.AddExternalInput("data") | ||
fc1 = brew.fc(model, data, "fc1", dim_in=4, dim_out=2) | ||
|
||
# no operator name set, will use default | ||
brew.fc(model, fc1, "fc2", dim_in=2, dim_out=1) | ||
|
||
net_modifier = ComputeNormForBlobs( | ||
blobs=['fc1_w', 'fc2_w'], | ||
logging_frequency=10, | ||
) | ||
|
||
net_modifier(model.net) | ||
|
||
workspace.FeedBlob('data', np.random.rand(10, 4).astype(np.float32)) | ||
|
||
workspace.RunNetOnce(model.param_init_net) | ||
workspace.RunNetOnce(model.net) | ||
|
||
fc1_w = workspace.FetchBlob('fc1_w') | ||
fc1_w_l2_norm = workspace.FetchBlob('fc1_w_l2_norm') | ||
|
||
self.assertEqual(fc1_w_l2_norm.size, 1) | ||
self.assertAlmostEqual(fc1_w_l2_norm[0], | ||
np.linalg.norm(fc1_w)**2, | ||
delta=1e-5) | ||
|
||
self.assertEqual(len(model.net.Proto().op), 6) | ||
|
||
assert 'fc1_w_l2_norm' in model.net.output_record().field_blobs(),\ | ||
model.net.output_record().field_blobs() | ||
assert 'fc2_w_l2_norm' in model.net.output_record().field_blobs(),\ | ||
model.net.output_record().field_blobs() | ||
|
||
def test_compute_norm_for_blobs_no_print(self): | ||
model = model_helper.ModelHelper(name="test") | ||
data = model.net.AddExternalInput("data") | ||
fc1 = brew.fc(model, data, "fc1", dim_in=4, dim_out=2) | ||
|
||
# no operator name set, will use default | ||
brew.fc(model, fc1, "fc2", dim_in=2, dim_out=1) | ||
|
||
net_modifier = ComputeNormForBlobs( | ||
blobs=['fc1_w', 'fc2_w'], | ||
logging_frequency=-1, | ||
) | ||
|
||
net_modifier(model.net) | ||
|
||
workspace.FeedBlob('data', np.random.rand(10, 4).astype(np.float32)) | ||
|
||
workspace.RunNetOnce(model.param_init_net) | ||
workspace.RunNetOnce(model.net) | ||
|
||
fc1_w = workspace.FetchBlob('fc1_w') | ||
fc1_w_l2_norm = workspace.FetchBlob('fc1_w_l2_norm') | ||
|
||
self.assertEqual(fc1_w_l2_norm.size, 1) | ||
self.assertAlmostEqual(fc1_w_l2_norm[0], | ||
np.linalg.norm(fc1_w)**2, | ||
delta=1e-5) | ||
|
||
self.assertEqual(len(model.net.Proto().op), 4) | ||
|
||
assert 'fc1_w_l2_norm' in model.net.output_record().field_blobs(),\ | ||
model.net.output_record().field_blobs() | ||
assert 'fc2_w_l2_norm' in model.net.output_record().field_blobs(),\ | ||
model.net.output_record().field_blobs() | ||
|
||
def test_compute_l1_norm_for_blobs(self): | ||
model = model_helper.ModelHelper(name="test") | ||
data = model.net.AddExternalInput("data") | ||
fc1 = brew.fc(model, data, "fc1", dim_in=4, dim_out=2) | ||
|
||
# no operator name set, will use default | ||
brew.fc(model, fc1, "fc2", dim_in=2, dim_out=1) | ||
|
||
net_modifier = ComputeNormForBlobs( | ||
blobs=['fc1_w', 'fc2_w'], | ||
logging_frequency=10, | ||
p=1, | ||
) | ||
|
||
net_modifier(model.net) | ||
|
||
workspace.FeedBlob('data', np.random.rand(10, 4).astype(np.float32)) | ||
|
||
workspace.RunNetOnce(model.param_init_net) | ||
workspace.RunNetOnce(model.net) | ||
|
||
fc1_w = workspace.FetchBlob('fc1_w') | ||
fc1_w_l1_norm = workspace.FetchBlob('fc1_w_l1_norm') | ||
|
||
self.assertEqual(fc1_w_l1_norm.size, 1) | ||
self.assertAlmostEqual(fc1_w_l1_norm[0], | ||
np.sum(np.abs(fc1_w)), | ||
delta=1e-5) | ||
|
||
self.assertEqual(len(model.net.Proto().op), 6) | ||
|
||
assert 'fc1_w_l1_norm' in model.net.output_record().field_blobs(),\ | ||
model.net.output_record().field_blobs() | ||
assert 'fc2_w_l1_norm' in model.net.output_record().field_blobs(),\ | ||
model.net.output_record().field_blobs() |
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,65 @@ | ||
# Copyright (c) 2016-present, Facebook, Inc. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
############################################################################## | ||
|
||
from __future__ import absolute_import | ||
from __future__ import division | ||
from __future__ import print_function | ||
from __future__ import unicode_literals | ||
|
||
from caffe2.python import core, schema | ||
from caffe2.python.modeling.net_modifier import NetModifier | ||
|
||
import numpy as np | ||
|
||
|
||
class ComputeStatisticsForBlobs(NetModifier): | ||
""" | ||
This class modifies the net passed in by adding ops to compute statistics | ||
for certain blobs. For each blob in the list, its min, max, mean and standard | ||
deviation will be computed. | ||
Args: | ||
blobs: list of blobs to compute norm for | ||
logging_frequency: frequency for printing norms to logs | ||
""" | ||
|
||
def __init__(self, blobs, logging_frequency): | ||
self._blobs = blobs | ||
self._logging_frequency = logging_frequency | ||
|
||
def modify_net(self, net, init_net=None, grad_map=None, blob_to_device=None): | ||
|
||
for blob_name in self._blobs: | ||
blob = core.BlobReference(blob_name) | ||
if not net.BlobIsDefined(blob): | ||
raise Exception('blob {0} is not defined in net {1}'.format( | ||
blob, net.Name())) | ||
|
||
cast_blob = net.Cast(blob, to=core.DataType.FLOAT) | ||
stats_name = net.NextScopedBlob(prefix=blob + '_summary') | ||
stats = net.Summarize(cast_blob, stats_name, to_file=0) | ||
net.Print(stats, [], every_n=self._logging_frequency) | ||
|
||
output_field_name = str(blob) + '_summary' | ||
output_scalar = schema.Scalar((np.float, (1,)), stats) | ||
|
||
if net.output_record() is None: | ||
net.set_output_record( | ||
schema.Struct((output_field_name, output_scalar)) | ||
) | ||
else: | ||
net.AppendOutputRecordField( | ||
output_field_name, | ||
output_scalar) |
Oops, something went wrong.