in attacks/privacy_attacks.py [0:0]
def get_uncalibrated_gradnorm(params, mask):
"""
return uncalibrated gradient norm values for data indicated by the mask.
"""
#load the dataset
dataset = get_dataset(params)
#initialize to 0
grad_norms=np.zeros(len(mask))
#get the final model
final_model=build_model(params)
final_model_path = os.path.join(params.model_path, "checkpoint.pth")
state_dict_final = torch.load(final_model_path, map_location='cuda:0')
if params.dataset=='imagenet':
new_state_dict = OrderedDict()
for k, v in state_dict_final["model"].items():
if k[:7]=='module.': # remove `module.`
new_state_dict[k[7:]] = v
else:
new_state_dict[k]=v
final_model.load_state_dict(new_state_dict)
else:
final_model.load_state_dict(state_dict_final['model'])
final_model=final_model.cuda()
original_model=[]
for p in final_model.parameters():
original_model.append(p.view(-1))
original_model=torch.cat(original_model)
#get the appropriate ids to dot product
ids=(mask==True).nonzero().flatten().numpy()
#load 1-by-1. See get_calibrated_gradnorm for batched method using Opacus gradsamplemodule.
for id in ids:
#load each image and target
image = dataset[id][0].unsqueeze(0)
image = image.cuda(non_blocking=True)
target = torch.tensor(dataset[id][1]).unsqueeze(0)
target = target.cuda(non_blocking=True)
#reload the original batch model, if imagenet may need to rename keys.
if params.dataset=='imagenet':
new_state_dict = OrderedDict()
for k, v in state_dict_final["model"].items():
if k[:7]=='module.': # remove "module.""
new_state_dict[k[7:]] = v
else:
new_state_dict[k]=v
final_model.load_state_dict(new_state_dict)
else:
final_model.load_state_dict(state_dict_final['model'])
# check the model gradient is zeros
final_model.zero_grad()
#get the gradient
output=final_model(image)
loss=F.cross_entropy(output, target)
loss.backward()
grads=[]
for param in final_model.parameters():
grads.append(param.grad.view(-1))
grads = torch.cat(grads)
g=grads.cpu().numpy()
grad_norms[id]=np.linalg.norm(g)
return grad_norms