interpretation_predict.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. # copyright (c) 2020 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. import numpy as np
  15. import cv2
  16. import copy
  17. import paddle.fluid as fluid
  18. from paddlex.cv.transforms import arrange_transforms
  19. def interpretation_predict(model, images):
  20. images = images.astype('float32')
  21. arrange_transforms(
  22. model.model_type,
  23. model.__class__.__name__,
  24. transforms=model.test_transforms,
  25. mode='test')
  26. tmp_transforms = copy.deepcopy(model.test_transforms.transforms)
  27. model.test_transforms.transforms = model.test_transforms.transforms[-2:]
  28. new_imgs = []
  29. for i in range(images.shape[0]):
  30. images[i] = cv2.cvtColor(images[i], cv2.COLOR_RGB2BGR)
  31. new_imgs.append(model.test_transforms(images[i])[0])
  32. new_imgs = np.array(new_imgs)
  33. with fluid.scope_guard(model.scope):
  34. out = model.exe.run(
  35. model.test_prog,
  36. feed={'image': new_imgs},
  37. fetch_list=list(model.interpretation_feats.values()))
  38. model.test_transforms.transforms = tmp_transforms
  39. return out