apple / coremltools

Core ML tools contain supporting tools for Core ML model conversion, editing, and validation.
https://coremltools.readme.io
BSD 3-Clause "New" or "Revised" License
4.32k stars 627 forks source link

Image output and Image deprocessing scaler for pth converter #906

Closed Vozf closed 1 year ago

Vozf commented 4 years ago

🌱 Describe your Feature Request

There is an image output option possible for onnx There is also an image deprocessing option for onnx converter but there is no such option for pth converter

Use cases

Segmentations

Describe alternatives you've considered

Manually create a layer which multiplies by 255 for deprocessing Function to convert multiarray to image output

def convert_multiarray_output_to_image(spec, feature_name, is_bgr=False):
    """
    Convert an output multiarray to be represented as an image
    This will modify the Model_pb spec passed in.
    Example:
        model = coremltools.models.MLModel('MyNeuralNetwork.mlmodel')
        spec = model.get_spec()
        convert_multiarray_output_to_image(spec,'imageOutput',is_bgr=False)
        newModel = coremltools.models.MLModel(spec)
        newModel.save('MyNeuralNetworkWithImageOutput.mlmodel')
    Parameters
    ----------
    spec: Model_pb
        The specification containing the output feature to convert
    feature_name: str
        The name of the multiarray output feature you want to convert
    is_bgr: boolean
        If multiarray has 3 channels, set to True for RGB pixel order or false for BGR
    """
    for output in spec.description.output:
        if output.name != feature_name:
            continue
        if output.type.WhichOneof('Type') != 'multiArrayType':
            raise ValueError("%s is not a multiarray type" % output.name)
        # array_shape = tuple(output.type.multiArrayType.shape)
        array_shape = (1, *input_img_shape)
        channels, height, width = array_shape
        from coremltools.proto import FeatureTypes_pb2 as ft
        if channels == 1:
            output.type.imageType.colorSpace = ft.ImageFeatureType.ColorSpace.Value('GRAYSCALE')
        elif channels == 3:
            if is_bgr:
                output.type.imageType.colorSpace = ft.ImageFeatureType.ColorSpace.Value('BGR')
            else:
                output.type.imageType.colorSpace = ft.ImageFeatureType.ColorSpace.Value('RGB')
        else:
            raise ValueError("Channel Value %d not supported for image inputs" % channels)
        output.type.imageType.width = width
        output.type.imageType.height = height

    return spec

Additional context

Add any other context or screenshots about the feature request here.

s1ddok commented 2 years ago

It would be incredible to see any updates on this one. Image2Image models were such a great CoreML function and they are now basically gone in new converters

TobyRoseman commented 2 years ago

This is related to (possibly a duplicate of) #775.

What do you mean when you say "pth converter"? Are you referring to the unified convertor?

TobyRoseman commented 1 year ago

As I mentioned previously, this is likely the same issue #775. Since we have not heard how this is different, I'm going to close this issue as a duplicate.