np.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import os
  2. import random
  3. import shutil
  4. import time
  5. import warnings
  6. import numpy as np
  7. import torch
  8. import torch.nn as nn
  9. import torch.nn.parallel
  10. import torch.backends.cudnn as cudnn
  11. import torch.distributed as dist
  12. import torch.optim
  13. import torch.multiprocessing as mp
  14. import torch.utils.data
  15. import torch.utils.data.distributed
  16. import torchvision.transforms as transforms
  17. import torchvision.datasets as datasets
  18. def convert(image_folder, gpu_id=None, batch_size=1):
  19. if gpu_id != None:
  20. torch.cuda.set_device(gpu_id)
  21. # prepare valid dataloader
  22. val_transform = transforms.Compose([
  23. transforms.Resize(342),
  24. transforms.CenterCrop(299),
  25. transforms.ToTensor(),
  26. transforms.Normalize(mean=[0.5, 0.5, 0.5],
  27. std=[0.5, 0.5, 0.5])
  28. ])
  29. val_dataset = datasets.ImageFolder(image_folder, val_transform)
  30. val_loader = torch.utils.data.DataLoader(
  31. val_dataset, batch_size=batch_size, shuffle=False,
  32. num_workers=1, pin_memory=False)
  33. # valid model in the valid dataloader
  34. validate(val_loader, gpu_id)
  35. def validate(val_loader, gpu_id):
  36. with torch.no_grad():
  37. if gpu_id != None:
  38. torch.cuda.synchronize()
  39. for i, (images, target) in enumerate(val_loader):
  40. images = images.permute(0, 2, 3, 1)
  41. #print(images.shape)
  42. #print(target.item())
  43. inpy = images.numpy()
  44. f = open('calib_data_c/%05d.bin'%i, 'wb')
  45. f.write(inpy.tobytes('C'))
  46. f.close()
  47. convert('calib_data', 0)
  48. convert('val_Data', 0)