(model_path, image_path)
| 67 | |
| 68 | |
| 69 | def run(model_path, image_path): |
| 70 | predictor = tp.OfflinePredictor(tp.PredictConfig( |
| 71 | model=Model(), |
| 72 | session_init=tp.SmartInit(model_path), |
| 73 | input_names=['image'], |
| 74 | output_names=['saliency'])) |
| 75 | im = cv2.imread(image_path) |
| 76 | assert im is not None and im.ndim == 3, image_path |
| 77 | |
| 78 | # resnet expect RGB inputs of 224x224x3 |
| 79 | im = cv2.resize(im, (IMAGE_SIZE, IMAGE_SIZE)) |
| 80 | im = im.astype(np.float32)[:, :, ::-1] |
| 81 | |
| 82 | saliency_images = predictor(im)[0] |
| 83 | |
| 84 | abs_saliency = np.abs(saliency_images).max(axis=-1) |
| 85 | pos_saliency = np.maximum(0, saliency_images) |
| 86 | neg_saliency = np.maximum(0, -saliency_images) |
| 87 | |
| 88 | pos_saliency -= pos_saliency.min() |
| 89 | pos_saliency /= pos_saliency.max() |
| 90 | cv2.imwrite('pos.jpg', pos_saliency * 255) |
| 91 | |
| 92 | neg_saliency -= neg_saliency.min() |
| 93 | neg_saliency /= neg_saliency.max() |
| 94 | cv2.imwrite('neg.jpg', neg_saliency * 255) |
| 95 | |
| 96 | abs_saliency = viz.intensity_to_rgb(abs_saliency, normalize=True)[:, :, ::-1] # bgr |
| 97 | cv2.imwrite("abs-saliency.jpg", abs_saliency) |
| 98 | |
| 99 | rsl = im * 0.2 + abs_saliency * 0.8 |
| 100 | cv2.imwrite("blended.jpg", rsl) |
| 101 | |
| 102 | |
| 103 | if __name__ == '__main__': |
no test coverage detected