In Masterarbeit:"Anomalie-Detektion in Zellbildern zur Anwendung der Leukämieerkennung" verwendete CSI Methode.
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.

lsun_fix_preprocess.py 1.7KB

2 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import os
  2. import time
  3. import random
  4. import numpy as np
  5. import torch
  6. from torchvision import datasets, transforms
  7. from torch.utils.data import DataLoader
  8. from torchvision.utils import save_image
  9. def set_random_seed(seed):
  10. random.seed(seed)
  11. np.random.seed(seed)
  12. torch.manual_seed(seed)
  13. torch.cuda.manual_seed(seed)
  14. check = time.time()
  15. transform = transforms.Compose([
  16. transforms.Resize(256),
  17. transforms.CenterCrop(256),
  18. transforms.Resize(32),
  19. transforms.ToTensor(),
  20. ])
  21. set_random_seed(0)
  22. LSUN_class_list = ['bedroom', 'bridge', 'church_outdoor', 'classroom',
  23. 'conference_room', 'dining_room', 'kitchen', 'living_room', 'restaurant', 'tower']
  24. total_test_image_all_class = []
  25. for LSUN_class in LSUN_class_list:
  26. LSUN_set = datasets.LSUN('~/data/lsun/', classes=LSUN_class + '_train', transform=transform)
  27. LSUN_loader = DataLoader(LSUN_set, batch_size=100, shuffle=True, pin_memory=False)
  28. total_test_image = None
  29. for n, (test_image, _) in enumerate(LSUN_loader):
  30. if n == 0:
  31. total_test_image = test_image
  32. else:
  33. total_test_image = torch.cat((total_test_image, test_image), dim=0).cpu()
  34. if total_test_image.size(0) >= 1000:
  35. break
  36. total_test_image_all_class.append(total_test_image)
  37. total_test_image_all_class = torch.cat(total_test_image_all_class, dim=0)
  38. print (f'Preprocessing time {time.time()-check}')
  39. if not os.path.exists('./LSUN_fix'):
  40. os.mkdir('./LSUN_fix')
  41. check = time.time()
  42. for i in range(10000):
  43. save_image(total_test_image_all_class[i], f'LSUN_fix/correct_resize_{i}.png')
  44. print (f'Saving time {time.time()-check}')