Merge in RR/image-prediction from RED-5107-hotfix to release/3.4.1
Squashed commit of the following:
commit b7b99074054e67201537efc2f0a5b96f29bd1684
Author: Julius Unverfehrt <julius.unverfehrt@iqser.com>
Date: Mon Aug 29 12:57:50 2022 +0200
RED-5107: move image normalization for predictor to image extraction step to be able to properly catch exeption thrown from this step
39 lines
989 B
Python
39 lines
989 B
Python
import abc
|
|
|
|
import numpy as np
|
|
import tensorflow as tf
|
|
|
|
|
|
class ModelWrapper(abc.ABC):
|
|
def __init__(self, classes, base_weights_path=None, weights_path=None):
|
|
self.__classes = classes
|
|
self.model = self.__build(base_weights_path)
|
|
self.model.load_weights(weights_path)
|
|
|
|
@property
|
|
@abc.abstractmethod
|
|
def input_shape(self):
|
|
raise NotImplementedError
|
|
|
|
@property
|
|
def classes(self):
|
|
return self.__classes
|
|
|
|
@abc.abstractmethod
|
|
def __preprocess_tensor(self, tensor):
|
|
raise NotImplementedError
|
|
|
|
@staticmethod
|
|
def __images_to_tensor(images):
|
|
return np.array(list(map(tf.keras.preprocessing.image.img_to_array, images)))
|
|
|
|
def prep_images(self, images):
|
|
tensor = self.__images_to_tensor(images)
|
|
tensor = self.__preprocess_tensor(tensor)
|
|
|
|
return tensor
|
|
|
|
@abc.abstractmethod
|
|
def __build(self, base_weights=None) -> tf.keras.models.Model:
|
|
raise NotImplementedError
|