|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +# |
| 4 | +# Copyright (c) 2021 Intel Corporation |
| 5 | +# |
| 6 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +# you may not use this file except in compliance with the License. |
| 8 | +# You may obtain a copy of the License at |
| 9 | +# |
| 10 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# |
| 12 | +# Unless required by applicable law or agreed to in writing, software |
| 13 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +# See the License for the specific language governing permissions and |
| 16 | +# limitations under the License. |
| 17 | + |
| 18 | +import sys |
| 19 | +from .dataset import dataset_registry, IterableDataset |
| 20 | +import numpy as np |
| 21 | +from lpot.utils.utility import LazyImport |
| 22 | +from lpot.utils import logger |
| 23 | + |
| 24 | +mx = LazyImport('mxnet') |
| 25 | +torch = LazyImport('torch') |
| 26 | + |
| 27 | +@dataset_registry(dataset_type="dummy_v2", framework="tensorflow, onnxrt_qlinearops, \ |
| 28 | + onnxrt_integerops, pytorch, pytorch_ipex, mxnet", dataset_format='') |
| 29 | +class DummyDataset(IterableDataset): |
| 30 | + """Dataset used for dummy_v2 data generation. |
| 31 | + This Dataset is to construct a dataset from a input shape and label shape. |
| 32 | + the value range is calculated from: low * stand_normal(0, 1) + high |
| 33 | +
|
| 34 | + Args: sample_size (int): total size of the dummy samples. |
| 35 | + input_shape (list or tuple): create single or multi input tensors, |
| 36 | + tuple reperesent the sample shape of the dataset, eg an image size should be |
| 37 | + represented as (224, 224, 3), list contains multiple tuple and |
| 38 | + represent multi input tensors. |
| 39 | + label_shape (list or tuple): create single or multi label tensors, |
| 40 | + tuple reperesent the label shape of the dataset, eg an label size should be |
| 41 | + represented as (1, ), list contains multiple tuple and |
| 42 | + represent multi label tensors. |
| 43 | + low (list or float, default=-128.):low out the tensor value range from [0, 1] |
| 44 | + to [0, low] or [low, 0] if low < 0, if float, |
| 45 | + will implement all tensors with same low value. |
| 46 | + high (list or float, default=127.):high the tensor value by add all tensor element |
| 47 | + value high. If list, length of list should be |
| 48 | + same with shape list. |
| 49 | + dtype (list or str, default='float32'):support multi tensor dtype setting. If list, |
| 50 | + length of list should be same with shape list, |
| 51 | + if str, all tensors will use same dtype. dtype |
| 52 | + support 'float32', 'float16', 'uint8', 'int8', |
| 53 | + 'int32', 'int64', 'bool'. |
| 54 | + transform (transform object, default=None): dummy_v2 dataset does not need transform. |
| 55 | + If transform is not None, it will ignore |
| 56 | + it. |
| 57 | + filter (Filter objects, default=None): filter out examples according to |
| 58 | + specific conditions |
| 59 | +
|
| 60 | + """ |
| 61 | + def __init__(self, input_shape, label_shape=None, low=-128., high=127., \ |
| 62 | + dtype='float32', transform=None, filter=None): |
| 63 | + |
| 64 | + self.dtype_map = {'float32':np.float32, 'float16':np.float16, 'uint8':np.uint8, \ |
| 65 | + 'int8':np.int8, 'int32':np.int32, 'int64':np.int64, 'bool':np.bool} |
| 66 | + |
| 67 | + np.random.seed(9527) |
| 68 | + self.transform = transform |
| 69 | + self.input_shape = input_shape |
| 70 | + self.label_shape = label_shape |
| 71 | + self.low = low |
| 72 | + self.high = high |
| 73 | + self.dtype = dtype |
| 74 | + |
| 75 | + if label_shape is None: |
| 76 | + self.label_dim = 0 |
| 77 | + elif isinstance(label_shape, tuple): |
| 78 | + self.label_dim = 1 |
| 79 | + else: |
| 80 | + self.label_dim = len(label_shape) |
| 81 | + |
| 82 | + self.input_dim = 1 if isinstance(input_shape, tuple) else len(input_shape) |
| 83 | + self.total_dim = self.input_dim + self.label_dim |
| 84 | + |
| 85 | + if isinstance(high, list): |
| 86 | + assert len(high) == self.total_dim and \ |
| 87 | + all(isinstance(elem, float) for elem in high),\ |
| 88 | + 'high value list length should same with label dim + input_dim' |
| 89 | + else: |
| 90 | + self.high = (high * np.ones(self.total_dim)).astype(np.float) |
| 91 | + |
| 92 | + if isinstance(low, list): |
| 93 | + assert len(low) == self.total_dim and \ |
| 94 | + all(isinstance(elem, float) for elem in low), \ |
| 95 | + 'low value list length should same with label dim + input_dim' |
| 96 | + else: |
| 97 | + self.low = (low * np.ones(self.total_dim)).astype(np.float) |
| 98 | + |
| 99 | + if isinstance(dtype, list): |
| 100 | + assert len(dtype) == self.total_dim and \ |
| 101 | + all(elem in self.dtype_map.keys() for elem in dtype), \ |
| 102 | + 'dtype list length should same with label dim + input_dim' |
| 103 | + else: |
| 104 | + self.dtype = [self.dtype for i in range(0, self.total_dim)] |
| 105 | + |
| 106 | + if isinstance(input_shape, tuple): |
| 107 | + self.input_shape = [input_shape] |
| 108 | + |
| 109 | + if isinstance(label_shape, tuple): |
| 110 | + self.label_shape = [label_shape] |
| 111 | + |
| 112 | + def __iter__(self): |
| 113 | + while True: |
| 114 | + input_data = [] |
| 115 | + for idx in range(0, self.input_dim): |
| 116 | + tensor = np.random.uniform(\ |
| 117 | + low=self.low[idx], high=self.high[idx], size=self.input_shape[idx]) |
| 118 | + tensor = tensor.astype(self.dtype_map[self.dtype[idx]]) |
| 119 | + input_data.append(tensor) |
| 120 | + |
| 121 | + label = [] |
| 122 | + for idx in range(0, self.label_dim): |
| 123 | + shift_idx = self.input_dim + idx |
| 124 | + tensor = np.random.uniform(low=self.low[shift_idx], |
| 125 | + high=self.high[shift_idx], |
| 126 | + size=self.label_shape[idx]) |
| 127 | + tensor = tensor.astype(self.dtype_map[self.dtype[shift_idx]]) |
| 128 | + label.append(tensor) |
| 129 | + |
| 130 | + if len(input_data) == 1: |
| 131 | + input_data = input_data[0] |
| 132 | + |
| 133 | + if len(label) == 1: |
| 134 | + label = label[0] |
| 135 | + |
| 136 | + if len(label) > 0: |
| 137 | + yield input_data, label |
| 138 | + else: |
| 139 | + yield input_data |
| 140 | + |
| 141 | + def __len__(self): |
| 142 | + return sys.maxsize |
0 commit comments