coremltools icon indicating copy to clipboard operation
coremltools copied to clipboard

Image output and Image deprocessing scaler for pth converter

Open Vozf opened this issue 4 years ago • 2 comments

🌱 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.

Vozf avatar Sep 03 '20 11:09 Vozf

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

s1ddok avatar Mar 25 '22 22:03 s1ddok

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 avatar Mar 25 '22 22:03 TobyRoseman

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.

TobyRoseman avatar Oct 24 '22 20:10 TobyRoseman