In Masterarbeit:"Anomalie-Detektion in Zellbildern zur Anwendung der Leukämieerkennung" verwendete Methode des 3. Platzes der ISBI2019.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

utils.py 1.2KB

9 months ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import pickle
  2. import random
  3. import string
  4. from datetime import datetime
  5. import torch
  6. import torch.nn as nn
  7. class IncrementalAverage:
  8. def __init__(self):
  9. self.value = 0
  10. self.counter = 0
  11. def update(self, x):
  12. self.counter += 1
  13. self.value += (x - self.value) / self.counter
  14. class Flatten(nn.Module):
  15. def forward(self, x):
  16. return x.view(x.size(0), -1)
  17. class SizePrinter(nn.Module):
  18. def forward(self, x):
  19. print(x.size())
  20. return x
  21. def count_parameters(model, grad_only=True):
  22. return sum(p.numel() for p in model.parameters() if not grad_only or p.requires_grad)
  23. def to_device(device, *tensors):
  24. return tuple(x.to(device) for x in tensors)
  25. def loop_iter(iter):
  26. while True:
  27. for item in iter:
  28. yield item
  29. def unique_string():
  30. return '{}.{}'.format(datetime.now().strftime('%Y%m%dT%H%M%SZ'),
  31. ''.join(random.choice(string.ascii_uppercase) for _ in range(4)))
  32. def set_seeds(seed):
  33. random.seed(seed)
  34. torch.manual_seed(seed)
  35. torch.cuda.manual_seed_all(seed)
  36. def pickle_dump(obj, file):
  37. with open(file, 'wb') as f:
  38. pickle.dump(obj, f)