You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

dataset.py 2.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. """
  2. Copyright 2020 Tianshu AI Platform. All Rights Reserved.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. =============================================================
  13. """
  14. from torchvision.datasets import VisionDataset
  15. from PIL import Image
  16. import torch
  17. class LabelConcatDataset(VisionDataset):
  18. """Dataset as a concatenation of dataset's lables.
  19. This class is useful to assemble the same dataset's labels.
  20. Arguments:
  21. datasets (sequence): List of datasets to be concatenated
  22. tasks (list) : List of teacher tasks
  23. transform (callable, optional): A function/transform that takes in an PIL image and returns a transformed version.
  24. target_transform (callable, optional): A function/transform that takes in the target and transforms it.
  25. transforms (callable, optional): A function/transform that takes input sample and its target as entry and returns a transformed version.
  26. """
  27. def __init__(self, datasets, transforms=None, transform=None, target_transform=None):
  28. super(LabelConcatDataset, self).__init__(
  29. root=None, transforms=transforms, transform=transform, target_transform=target_transform)
  30. assert len(datasets) > 0, 'datasets should not be an empty iterable'
  31. self.datasets = list(datasets)
  32. for d in self.datasets:
  33. for t in ('transform', 'transforms', 'target_transform'):
  34. if hasattr( d, t ):
  35. setattr( d, t, None )
  36. def __getitem__(self, idx):
  37. targets_list = []
  38. for dst in self.datasets:
  39. image, target = dst[idx]
  40. targets_list.append(target)
  41. if self.transforms is not None:
  42. image, *targets_list = self.transforms( image, *targets_list )
  43. data = [ image, *targets_list ]
  44. return data
  45. def __len__(self):
  46. return len(self.datasets[0].images)

一站式算法开发平台、高性能分布式深度学习框架、先进算法模型库、视觉模型炼知平台、数据可视化分析平台等一系列平台及工具,在模型高效分布式训练、数据处理和可视分析、模型炼知和轻量化等技术上形成独特优势,目前已在产学研等各领域近千家单位及个人提供AI应用赋能

Contributors (1)