示例#1
0
    def __init__(
            self,
            img,
            model_path='./bodypix_resnet50_float_model-stride16/model.json',
            output_stride=16):
        print("[INFO] Loading model...")
        self.graph = load_graph_model(model_path)
        print("[INFO] Loaded model...")
        self.output_stride = output_stride

        self.img = img

        # Get input and output tensors
        self.input_tensor_names = get_input_tensors(self.graph)
        print(self.input_tensor_names)
        self.output_tensor_names = get_output_tensors(self.graph)
        print(self.output_tensor_names)
        self.input_tensor = self.graph.get_tensor_by_name(
            self.input_tensor_names[0])
targetHeight = (int(imgHeight) // OutputStride) * OutputStride + 1

print(imgHeight, imgWidth, targetHeight, targetWidth)
img = img.resize((targetWidth, targetHeight))
x = tf.keras.preprocessing.image.img_to_array(img, dtype=np.float32)
InputImageShape = x.shape
print("Input Image Shape in hwc", InputImageShape)

widthResolution = int((InputImageShape[1] - 1) / OutputStride) + 1
heightResolution = int((InputImageShape[0] - 1) / OutputStride) + 1
print('Resolution', widthResolution, heightResolution)

# Get input and output tensors
input_tensor_names = get_input_tensors(graph)
print(input_tensor_names)
output_tensor_names = get_output_tensors(graph)
print(output_tensor_names)
input_tensor = graph.get_tensor_by_name(input_tensor_names[0])

# Preprocessing Image
# For Resnet
if any('resnet_v1' in name for name in output_tensor_names):
    # add imagenet mean - extracted from body-pix source
    m = np.array([-123.15, -115.90, -103.06])
    x = np.add(x, m)
# For Mobilenet
elif any('MobilenetV1' in name for name in output_tensor_names):
    x = (x / 127.5) - 1
else:
    print('Unknown Model')
sample_image = x[tf.newaxis, ...]