dotnet / machinelearning

ML.NET is an open source and cross-platform machine learning framework for .NET.
https://dot.net/ml
MIT License
9.04k stars 1.89k forks source link

Add "Reshape Transform" #765

Open yaeldekel opened 6 years ago

yaeldekel commented 6 years ago

We need the following functionalities in order to score TensorFlow models:

  1. Change the ColumnType of the column to be a different shape – this would not do anything to the data, it would just change the type of the column, to match the input dimensions the model expects (for example, if the data contains a column of length 784, and the model expects a 28x28 input).
  2. Actually reshape the data – (C,H,W) to (H,W,C). This transform would also have to move data around in addition to changing the ColumnType.
  3. Reshape data from RGB to other ordering of the channels. This transform would move the data around, but leave the ColumnType as it was.

2 is already implemented as an option in the PixelExtractorTransform, and 3 could also be implemented as an option in that transform, but it may be useful to have them as a separate transform, for cases where the input data doesn't necessarily come from the PixelExtractorTransform.

zeahmed commented 5 years ago

Once we have this transform implemented, please revert the change in https://github.com/dotnet/machinelearning/pull/2935

JakeRadMSFT commented 5 years ago

Can we increase the priority of this? It'll be useful in Azure Attach scenarios for Model Builder and ML NET CLI. Model's exported from Azure are ONNX and expect the format to have the shape [3,224,224].

JakeRadMSFT commented 5 years ago

Just to follow up, we were able to work around it with a custom mapping. We also ended up getting a new model that didn't require it.

Example Custom Mapping that reshapes from [3,224,224] to [3 224 224].

[CustomMappingFactoryAttribute(nameof(ReshapeMapping))]
    public class ReshapeMapping : CustomMappingFactory<ReshapeInput, ReshapeOutput>
    {
        // This is the custom mapping. We now separate it into a method, so that we can use it both in training and in loading.
        public static void Mapping(ReshapeInput input, ReshapeOutput output) {
            output.Reshape = input.Reshape;
        }
        // This factory method will be called when loading the model to get the mapping operation.
        public override Action<ReshapeInput, ReshapeOutput> GetMapping()
        {
            return Mapping;
        }
    }
    public class ReshapeInput
    {
        [ColumnName("input.1")]
        [VectorType(3, 224, 224)]
        public VBuffer<float> Reshape;
    }
    public class ReshapeOutput
    {
        [ColumnName("input.1")]
        [VectorType(3 * 224 * 224)]
        public VBuffer<float> Reshape;
    }
laurentpayot commented 1 week ago

This issue is more than 6 years old, any update?