comfunc.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. # Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
  2. #
  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. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from __future__ import absolute_import
  15. from __future__ import division
  16. from __future__ import print_function
  17. import numpy as np
  18. def rerange_index(batch_size, samples_each_class):
  19. tmp = np.arange(0, batch_size * batch_size)
  20. tmp = tmp.reshape(-1, batch_size)
  21. rerange_index = []
  22. for i in range(batch_size):
  23. step = i // samples_each_class
  24. start = step * samples_each_class
  25. end = (step + 1) * samples_each_class
  26. pos_idx = []
  27. neg_idx = []
  28. for j, k in enumerate(tmp[i]):
  29. if j >= start and j < end:
  30. if j == i:
  31. pos_idx.insert(0, k)
  32. else:
  33. pos_idx.append(k)
  34. else:
  35. neg_idx.append(k)
  36. rerange_index += (pos_idx + neg_idx)
  37. rerange_index = np.array(rerange_index).astype(np.int32)
  38. return rerange_index