Skip to content
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

IMPROVEMENT: handle models with grayscale input #31

Merged
merged 4 commits into from
May 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion flashtorch/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '0.1.2'
__version__ = '0.1.3'
3 changes: 1 addition & 2 deletions flashtorch/saliency/backprop.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,7 @@ def _record_gradients(module, grad_in, grad_out):
self.gradients = grad_in[0]

for _, module in self.model.named_modules():
if isinstance(module, nn.modules.conv.Conv2d) and \
module.in_channels == 3:
if isinstance(module, nn.modules.conv.Conv2d):
module.register_backward_hook(_record_gradients)
break

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
DOCLINES = (__doc__ or '').split("\n")
long_description = "\n".join(DOCLINES[2:])

version = '0.1.2'
version = '0.1.3'

setup(
name='flashtorch',
Expand Down
31 changes: 31 additions & 0 deletions tests/test_backprop.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import torch
import torch.nn as nn
import torch.nn.functional as F

import torchvision.models as models

Expand Down Expand Up @@ -59,6 +60,21 @@ def make_expected_gradient_target(top_class):
return target


class CnnGrayscale(nn.Module):
def __init__(self):
super().__init__()
self.conv1 = nn.Conv2d(1, 64, kernel_size=3, stride=3, padding=1)
self.relu1 = nn.ReLU(inplace=True)
self.conv2 = nn.Conv2d(64, 10, kernel_size=3, stride=3, padding=1)
self.fc1 = nn.Linear(10 * 25 * 25, 10)

def forward(self, x):
x = F.relu(self.conv1(x))
x = F.relu(self.conv2(x))

return F.softmax(self.fc1(x.view(-1, 10 * 25 * 25)), dim=1)


#################
# Test fixtures #
#################
Expand All @@ -69,6 +85,11 @@ def model():
return models.alexnet()


@pytest.fixture
def model_grayscale():
return CnnGrayscale()


##############
# Test cases #
##############
Expand Down Expand Up @@ -162,6 +183,16 @@ def test_calc_gradients_of_top_class_if_prediction_is_wrong(mocker, model):
assert torch.all(kwargs['gradient'].eq(target))


def test_handle_greyscale_input(mocker, model_grayscale):
backprop = Backprop(model_grayscale)

input_ = torch.zeros([1, 1, 224, 224], requires_grad=True)

gradients = backprop.calculate_gradients(input_)

assert gradients.shape == (1, 224, 224)


def test_return_max_across_color_channels_if_specified(mocker, model):
backprop = Backprop(model)

Expand Down