def __init__(self, model_type,layer_name,model): self.model_arch = model self.gradients = dict() self.activations = dict() def backward_hook(module, grad_input, grad_output): self.gradients['value'] = grad_output[0] return None def forward_hook(module, input, output): self.activations['value'] = output return None if 'vgg' in model_type.lower(): target_layer = find_vgg_layer(self.model_arch, layer_name) elif 'resnet' in model_type.lower(): target_layer = find_resnet_layer(self.model_arch.feature, layer_name) elif 'densenet' in model_type.lower(): target_layer = find_densenet_layer(self.model_arch, layer_name) elif 'alexnet' in model_type.lower(): target_layer = find_alexnet_layer(self.model_arch, layer_name) elif 'squeezenet' in model_type.lower(): target_layer = find_squeezenet_layer(self.model_arch, layer_name) target_layer.register_forward_hook(forward_hook) target_layer.register_backward_hook(backward_hook)
def __init__(self, model_dict, verbose=False): model_type = model_dict['type'] layer_name = model_dict['layer_name'] self.model_arch = model_dict['arch'] self.gradients = dict() self.activations = dict() self.attention = dict() def backward_hook(module, grad_input, grad_output): self.gradients['value'] = grad_output[0] return None def forward_hook(module, input, output): self.activations['value'] = output # eps = 7./3. - 4./3. - 1 # input_eps = input[0] # input_eps += (input[0] == 0) * eps # self.attention['value'] = output / input_eps self.attention['value'] = output / input[0] return None if 'vgg' in model_type.lower(): target_layer = find_vgg_layer(self.model_arch, layer_name) elif 'resnet' in model_type.lower(): target_layer = find_resnet_layer(self.model_arch, layer_name) elif 'densenet' in model_type.lower(): target_layer = find_densenet_layer(self.model_arch, layer_name) elif 'alexnet' in model_type.lower(): target_layer = find_alexnet_layer(self.model_arch, layer_name) elif 'squeezenet' in model_type.lower(): target_layer = find_squeezenet_layer(self.model_arch, layer_name) target_layer.register_forward_hook(forward_hook) # target_layer.register_backward_hook(backward_hook) # not required for attention mapping if verbose: try: input_size = model_dict['input_size'] except KeyError: print( "please specify size of input image in model_dict. e.g. {'input_size':(224, 224)}" ) pass else: device = 'cuda' if next( self.model_arch.parameters()).is_cuda else 'cpu' self.model_arch(torch.zeros(1, 3, *(input_size), device=device)) print('saliency_map size :', self.activations['value'].shape[2:])
def __init__(self, model_dict, verbose=False): model_type = model_dict['type'] layer_name = model_dict['layer_name'] self.model_arch = model_dict['arch'] self.activation_function = model_dict['activation_function'] if self.activation_function not in ('sigmoid', 'exp'): raise ValueError("Invalid activation function") self.gradients = dict() self.activations = dict() def backward_hook(module, grad_input, grad_output): self.gradients['value'] = grad_output[0] return None def forward_hook(module, input, output): self.activations['value'] = output return None if 'vgg' in model_type.lower(): target_layer = find_vgg_layer(self.model_arch, layer_name) elif 'resnet' in model_type.lower(): target_layer = find_resnet_layer(self.model_arch, layer_name) elif 'densenet' in model_type.lower(): target_layer = find_densenet_layer(self.model_arch, layer_name) elif 'alexnet' in model_type.lower(): target_layer = find_alexnet_layer(self.model_arch, layer_name) elif 'squeezenet' in model_type.lower(): target_layer = find_squeezenet_layer(self.model_arch, layer_name) target_layer.register_forward_hook(forward_hook) target_layer.register_backward_hook(backward_hook) if verbose: try: input_size = model_dict['input_size'] except KeyError: print( "please specify size of input image in model_dict. e.g. {'input_size':(224, 224)}" ) pass else: device = 'cuda' if next( self.model_arch.parameters()).is_cuda else 'cpu' self.model_arch(torch.zeros(1, 3, *(input_size), device=device)) print('saliency_map size :', self.activations['value'].shape[2:])
def __init__(self, model, layer_name, label2cat): self.model = model self.model.eval() self.layer_name = layer_name self.label2cat = label2cat self.activations = None def forward_hook(module, input, output): self.activations = output return None target_layer = find_resnet_layer(self.model, layer_name) target_layer.register_forward_hook(forward_hook)
def __init__(self, model_dict): model_type = model_dict['type'] layer_name = model_dict['layer_name'] self.model_arch = model_dict['arch'] self.model_arch.eval() if torch.cuda.is_available(): self.model_arch.cuda() self.gradients = dict() self.activations = dict() def backward_hook(module, grad_input, grad_output): if torch.cuda.is_available(): self.gradients['value'] = grad_output[0].cuda() else: self.gradients['value'] = grad_output[0] return None def forward_hook(module, input, output): if torch.cuda.is_available(): self.activations['value'] = output.cuda() else: self.activations['value'] = output return None if 'vgg' in model_type.lower(): self.target_layer = find_vgg_layer(self.model_arch, layer_name) elif 'resnet' in model_type.lower(): self.target_layer = find_resnet_layer(self.model_arch, layer_name) elif 'densenet' in model_type.lower(): self.target_layer = find_densenet_layer(self.model_arch, layer_name) elif 'alexnet' in model_type.lower(): self.target_layer = find_alexnet_layer(self.model_arch, layer_name) elif 'squeezenet' in model_type.lower(): self.target_layer = find_squeezenet_layer(self.model_arch, layer_name) elif 'googlenet' in model_type.lower(): self.target_layer = find_googlenet_layer(self.model_arch, layer_name) elif 'shufflenet' in model_type.lower(): self.target_layer = find_shufflenet_layer(self.model_arch, layer_name) elif 'mobilenet' in model_type.lower(): self.target_layer = find_mobilenet_layer(self.model_arch, layer_name) else: self.target_layer = find_layer(self.model_arch, layer_name) self.target_layer.register_forward_hook(forward_hook) self.target_layer.register_backward_hook(backward_hook)