interpretation_predict.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. # copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve.
  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. def interpretation_predict(model, images):
  18. images = images.astype('float32')
  19. model.arrange_transforms(transforms=model.test_transforms, mode='test')
  20. tmp_transforms = copy.deepcopy(model.test_transforms.transforms)
  21. model.test_transforms.transforms = model.test_transforms.transforms[-2:]
  22. new_imgs = []
  23. for i in range(images.shape[0]):
  24. images[i] = cv2.cvtColor(images[i], cv2.COLOR_RGB2BGR)
  25. new_imgs.append(model.test_transforms(images[i])[0])
  26. new_imgs = np.array(new_imgs)
  27. out = model.exe.run(model.test_prog,
  28. feed={'image': new_imgs},
  29. fetch_list=list(model.interpretation_feats.values()))
  30. model.test_transforms.transforms = tmp_transforms
  31. return out