|
187 | 187 | 'deformable_roi_pooling', |
188 | 188 | 'filter_by_instag', |
189 | 189 | 'shard_index', |
190 | | - 'class_center_sample', |
191 | 190 | 'hard_swish', |
192 | 191 | 'mish', |
193 | 192 | 'gather_tree', |
@@ -14841,164 +14840,6 @@ def shard_index(input, index_num, nshards, shard_id, ignore_value=-1): |
14841 | 14840 | return out |
14842 | 14841 |
|
14843 | 14842 |
|
14844 | | -def class_center_sample(label, num_classes, num_samples, group=None, seed=None): |
14845 | | - """ |
14846 | | - Class center sample method is proposed from the paper PartialFC that only sample a subset of the class centers. |
14847 | | - The process of sampling subset class centers is straightforward: |
14848 | | - |
14849 | | - 1. First select the positive class centers; |
14850 | | - 2. Then randomly sample negative class centers. |
14851 | | - |
14852 | | - Specifically, given a label tensor, shape [batch_size], select all the positive class centers and randomly |
14853 | | - sample negative class centers, then remap the input label tensor using the sampled class centers. |
14854 | | - |
14855 | | - For more information, Partial FC: Training 10 Million Identities on a Single Machine |
14856 | | - arxiv: https://arxiv.org/abs/2010.05222 |
14857 | | - |
14858 | | - .. hint:: |
14859 | | - If the number of the positive class centers is greater than the input num_samples, it keeps all the positive |
14860 | | - class centers and the shape of sampled_class_center will be [num_positive_class_centers]. |
14861 | | - |
14862 | | - The API supports CPU, single GPU and multi GPU. |
14863 | | - |
14864 | | - Args: |
14865 | | - label (Tensor): 1-D tensor with shape [N], each label in [0, num_classes) |
14866 | | - num_classes (int): A positive integer to specify the number of classes at local rank. |
14867 | | - Note that num_classes of each GPU can be different. |
14868 | | - num_samples (int): A positive integer to specify the number of class center to sample. |
14869 | | - group (Group, optional): The abstract representation of group. |
14870 | | - See paddle.distributed.collective.Group. Default is ``None``. |
14871 | | - seed (int, optional): Random seed. Default is ``None``. |
14872 | | - |
14873 | | - Returns: |
14874 | | - Tuple of two ``Tensor`` : (remapped_label, sampled_class_center), remapped label using sampled class center, |
14875 | | - sampled class center from [0, num_classes). |
14876 | | - |
14877 | | - Examples: |
14878 | | - |
14879 | | - .. code-block:: python |
14880 | | - |
14881 | | - # CPU or single GPU |
14882 | | - import os |
14883 | | - import paddle |
14884 | | - import numpy as np |
14885 | | - num_classes = 20 |
14886 | | - batch_size = 10 |
14887 | | - num_samples = 6 |
14888 | | - np_label = np.random.randint(0, num_classes, (batch_size,), dtype=np.int64) |
14889 | | - label = paddle.to_tensor(np_label, dtype="int64") |
14890 | | - print(label) |
14891 | | - remapped_label, sampled_class_index = paddle.nn.functional.class_center_sample(label, num_classes, num_samples) |
14892 | | - |
14893 | | - print(remapped_label) |
14894 | | - print(sampled_class_index) |
14895 | | - |
14896 | | - # the output is |
14897 | | - #Tensor(shape=[10], dtype=int64, place=CPUPlace, stop_gradient=True, |
14898 | | - # [11, 5 , 1 , 3 , 12, 2 , 15, 19, 18, 19]) |
14899 | | - #Tensor(shape=[10], dtype=int64, place=CPUPlace, stop_gradient=True, |
14900 | | - # [4, 3, 0, 2, 5, 1, 6, 8, 7, 8]) |
14901 | | - #Tensor(shape=[9], dtype=int64, place=CPUPlace, stop_gradient=True, |
14902 | | - # [1 , 2 , 3 , 5 , 11, 12, 15, 18, 19]) |
14903 | | - |
14904 | | - .. code-block:: python |
14905 | | - |
14906 | | - # required: distributed |
14907 | | - # Multi GPU, test_class_center_sample.py |
14908 | | - import paddle |
14909 | | - import paddle.distributed as dist |
14910 | | - import numpy as np |
14911 | | - strategy = dist.fleet.DistributedStrategy() |
14912 | | - dist.fleet.init(is_collective=True, strategy=strategy) |
14913 | | - batch_size = 10 |
14914 | | - num_samples = 6 |
14915 | | - rank_id = dist.get_rank() |
14916 | | - # num_classes of each GPU can be different, e.g num_classes_list = [10, 8] |
14917 | | - num_classes_list = [10, 10] |
14918 | | - num_classes = np.sum(num_classes_list) |
14919 | | - np_label = np.random.randint(0, num_classes, (batch_size,), dtype=np.int64) |
14920 | | - label = paddle.to_tensor(np_label, dtype="int64") |
14921 | | - label_list = [] |
14922 | | - dist.all_gather(label_list, label) |
14923 | | - label = paddle.concat(label_list, axis=0) |
14924 | | - remapped_label, sampled_class_index = paddle.nn.functional.class_center_sample(label, num_classes_list[rank_id], num_samples) |
14925 | | - |
14926 | | - print(label) |
14927 | | - print(remapped_label) |
14928 | | - print(sampled_class_index) |
14929 | | - |
14930 | | - #python -m paddle.distributed.launch --gpus=0,1 test_class_center_sample.py |
14931 | | - # rank 0 output: |
14932 | | - #Tensor(shape=[20], dtype=int64, place=CUDAPlace(0), stop_gradient=True, |
14933 | | - # [10, 17, 15, 11, 9 , 12, 18, 18, 17, 18, 19, 2 , 8 , 13, 11, 13, 9 , 10, 0 , 4 ]) |
14934 | | - #Tensor(shape=[20], dtype=int64, place=CUDAPlace(0), stop_gradient=True, |
14935 | | - # [6 , 11, 10, 7 , 4 , 8 , 12, 12, 11, 12, 13, 1 , 3 , 9 , 7 , 9 , 4 , 6 , 0 , 2 ]) |
14936 | | - #Tensor(shape=[6], dtype=int64, place=CUDAPlace(0), stop_gradient=True, |
14937 | | - # [0, 2, 4, 8, 9, 3]) |
14938 | | - |
14939 | | - # rank 1 output: |
14940 | | - #Tensor(shape=[20], dtype=int64, place=CUDAPlace(1), stop_gradient=True, |
14941 | | - # [10, 17, 15, 11, 9 , 12, 18, 18, 17, 18, 19, 2 , 8 , 13, 11, 13, 9 , 10, 0 , 4 ]) |
14942 | | - #Tensor(shape=[20], dtype=int64, place=CUDAPlace(1), stop_gradient=True, |
14943 | | - # [6 , 11, 10, 7 , 4 , 8 , 12, 12, 11, 12, 13, 1 , 3 , 9 , 7 , 9 , 4 , 6 , 0 , 2 ]) |
14944 | | - #Tensor(shape=[7], dtype=int64, place=CUDAPlace(1), stop_gradient=True, |
14945 | | - # [0, 1, 2, 3, 5, 7, 8]) |
14946 | | - """ |
14947 | | - if group is not None and not group.is_member(): |
14948 | | - return |
14949 | | - |
14950 | | - ring_id = 0 if group is None else group.id |
14951 | | - rank = 0 |
14952 | | - nranks = 1 |
14953 | | - if core.is_compiled_with_dist(): |
14954 | | - parallel_env = paddle.distributed.ParallelEnv() |
14955 | | - global_rank = parallel_env.rank |
14956 | | - rank = global_rank if group is None else group.get_group_rank( |
14957 | | - global_rank) |
14958 | | - nranks = parallel_env.world_size if group is None else group.nranks |
14959 | | - |
14960 | | - if num_samples > num_classes: |
14961 | | - raise ValueError( |
14962 | | - 'Expected num_samples less than or equal to {}, got num_samples {}'. |
14963 | | - format(num_classes, num_samples)) |
14964 | | - |
14965 | | - if (seed is None or seed == 0) and default_main_program().random_seed != 0: |
14966 | | - seed = default_main_program().random_seed |
14967 | | - |
14968 | | - if in_dygraph_mode(): |
14969 | | - remapped_label, sampled_class_center = core.ops.class_center_sample( |
14970 | | - label, 'num_classes', num_classes, 'num_samples', num_samples, |
14971 | | - 'ring_id', ring_id, 'nranks', nranks, 'rank', rank, 'fix_seed', |
14972 | | - seed is not None, 'seed', seed if seed is not None else 0) |
14973 | | - return remapped_label, sampled_class_center |
14974 | | - |
14975 | | - check_variable_and_dtype(label, 'label', ['int64', 'int32'], |
14976 | | - 'class_center_sample') |
14977 | | - op_type = 'class_center_sample' |
14978 | | - helper = LayerHelper(op_type, **locals()) |
14979 | | - remapped_label = helper.create_variable_for_type_inference( |
14980 | | - dtype=label.dtype) |
14981 | | - sampled_class_center = helper.create_variable_for_type_inference( |
14982 | | - dtype=label.dtype) |
14983 | | - helper.append_op( |
14984 | | - type=op_type, |
14985 | | - inputs={'Label': label}, |
14986 | | - outputs={ |
14987 | | - 'RemappedLabel': remapped_label, |
14988 | | - 'SampledLocalClassCenter': sampled_class_center |
14989 | | - }, |
14990 | | - attrs={ |
14991 | | - 'num_classes': num_classes, |
14992 | | - 'num_samples': num_samples, |
14993 | | - 'ring_id': ring_id, |
14994 | | - 'nranks': nranks, |
14995 | | - 'rank': rank, |
14996 | | - 'fix_seed': seed is not None, |
14997 | | - 'seed': seed if seed is not None else 0 |
14998 | | - }) |
14999 | | - return remapped_label, sampled_class_center |
15000 | | - |
15001 | | - |
15002 | 14843 | @templatedoc() |
15003 | 14844 | def hard_swish(x, threshold=6.0, scale=6.0, offset=3.0, name=None): |
15004 | 14845 | r""" |
|
0 commit comments