training_embedding.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import torch.nn as nn
  2. import torch
  3. from torch.optim import SGD, Adam
  4. import torch.nn.functional as F
  5. def string2bin(s):
  6. binary_representation = ''.join(format(ord(x), '08b') for x in s)
  7. return [int(x) for x in binary_representation]
  8. def bin2string(binary_string):
  9. return ''.join(chr(int(binary_string[i:i+8], 2)) for i in range(0, len(binary_string), 8))
  10. class Embedding():
  11. def __init__(self, model, code: torch.Tensor, key_path: str = None, l=1):
  12. super(Embedding, self).__init__()
  13. self.p = self.get_parameters(model)
  14. # self.p = parameters
  15. # self.w = nn.Parameter(w, requires_grad=True)
  16. # the flatten mean parameters
  17. # w = torch.mean(self.p, dim=1).reshape(-1)
  18. w = self.flatten_parameters(self.p)
  19. self.l = l
  20. self.w_init = w.clone().detach()
  21. print('Size of embedding parameters:', w.shape)
  22. self.opt = Adam(self.p, lr=0.001)
  23. self.distribution_ignore = ['train_acc']
  24. self.code = torch.tensor(string2bin(
  25. code), dtype=torch.float).cuda() # the embedding code
  26. self.code_len = self.code.shape[0]
  27. print(f'Code:{self.code} code length:{self.code_len}')
  28. if key_path is not None:
  29. self.load_matrix(key_path)
  30. else:
  31. self.X_random = torch.randn(
  32. (self.code_len, self.w_init.shape[0])).cuda()
  33. def save_matrix(self):
  34. torch.save(self.X_random, './key.pt')
  35. def load_matrix(self, path):
  36. self.X_random = torch.load(path).cuda()
  37. def get_parameters(self, model):
  38. conv_list = []
  39. # print(model.modules())
  40. for module in model.modules():
  41. if isinstance(module, nn.Conv2d) and module.out_channels > 100:
  42. conv_list.append(module)
  43. # print(conv_list)
  44. target = conv_list[10:12]
  45. print(f'Embedding target:{target}')
  46. # parameters = target.weight
  47. parameters = [x.weight for x in target]
  48. # [x.requires_grad_(True) for x in parameters]
  49. return parameters
  50. # add penalty value to loss
  51. def add_penalty(self, loss):
  52. # print(f'original loss:{loss} ')
  53. w = self.flatten_parameters(self.p)
  54. prob = self.get_prob(self.X_random, w)
  55. penalty = self.loss_fun(
  56. prob, self.code)
  57. loss += self.l*penalty
  58. # print(f'penalty loss:{loss} ')
  59. return loss
  60. def flatten_parameters(self, parameters):
  61. parameter = torch.cat([torch.mean(x, dim=3).reshape(-1)
  62. for x in parameters])
  63. return parameter
  64. def loss_fun(self, x, y):
  65. penalty = F.binary_cross_entropy(x, y)
  66. return penalty
  67. def decode(self, X, w):
  68. prob = self.get_prob(X, w)
  69. return torch.where(prob > 0.5, 1, 0)
  70. def get_prob(self, X, w):
  71. mm = torch.mm(self.X_random, w.reshape((w.shape[0], 1)))
  72. return F.sigmoid(mm).flatten()
  73. def test(self):
  74. w = self.flatten_parameters(self.p)
  75. decode = self.decode(self.X_random, w)
  76. print(decode.shape)
  77. code_string = ''.join([str(x) for x in decode.tolist()])
  78. code_string = bin2string(code_string)
  79. print('decoded code:', code_string)
  80. return code_string