Comments (13)
Why do you need to do this? It's better to let Core ML handle this for you.
from coremlhelpers.
New to this arena, so excuse my lack of understanding.
If my model is normalized via CoreML, input images must be similarly normalized? What is the best/easiest means to normalize a UIImage?
Brian
from coremlhelpers.
When you convert your model to Core ML you tell it how to normalize the image (by passing in the appropriate parameters). Then in your app you just use a regular CVPixelBuffer and Core ML takes care of the normalization. Even easier is using Vision, which also resizes the image if necessary.
from coremlhelpers.
Hi @hollance
Google just showed me this thread while searching image normalization and CoreML. Write now i'm in the process to convert a pytorch model through onnx. Original model normalizes each channel individually by subtracting average and dividing by standard deviation.
In this scenario it is not clear yet to me how to do it with coremltools through bias and scale factors. If it is not, @appsird makes makes sense.
from coremlhelpers.
The bias is for subtracting, and you can provide a separate bias for each of the 3 channels. The scale is for dividing by the standard deviation.
See also: http://machinethink.net/blog/help-core-ml-gives-wrong-output/
from coremlhelpers.
ok, but that model that i'm using has a standard deviation per channel.
IMAGE_NET_MEAN = [0.485, 0.456, 0.406]
IMAGE_NET_STD = [0.229, 0.224, 0.225]
This means that
normalizedValRed = (red - red_avg) / red_stddev
With bias you can do red - red_avg. Scale, though, i did not figure out yet how to apply it per channel.
from coremlhelpers.
Wait. This makes no sense.
What eventually makes sense is to replace input model and use MLMultiArray with normalized values. In the end probably the best is to keep input as image and add a custom layer to do the normalization.
from coremlhelpers.
Yeah it gets a bit trickier. But really I would just use 0.225 for all of them since they're so similar it probably doesn't matter.
from coremlhelpers.
That is a dangerous assumption...
I'm moving now to the snakes world and investigating how to add custom layers to the graph through coremltools. I really don't understand why Apple does not provide this type of normalization. All python frameworks have it and, together with (x-min)/(max - min), (x-avg)/stdev is one of the most common normalizations adopted in data mining methods. Doing it per color channel seems quite reasonable.
from coremlhelpers.
You're going to lose more precision due to 16-bit floating point precision issues than because of a difference of 0.001 or 0.004 in the standard deviation. So, I wouldn't lose any sleep over it.
You could probably achieve what you want by adding a ScaleLayerParams as the first layer in the model, although I'm not 100% sure that it accepts a different scale factor per channel.
from coremlhelpers.
@hollance and folks reading this.
There is an example now in coremltools how to scale per channel
cheers
from coremlhelpers.
Nice, thanks for finding this!
from coremlhelpers.
This was created a few days ago and added to master yesterday. Here you can find the flow
from coremlhelpers.
Related Issues (20)
- use with macOS/NSImage HOT 4
- Is there any way to make the returned CVPixelbuffer backed by an IOSurface ? HOT 2
- How to know if coreML model would perform image-preprocessing step or not?
- Q about output Multiarray shape
- Carthage - no shared framework schemes HOT 2
- Is there any evaluation example code or helper modules on this repo? HOT 1
- Accessing elements from Dim 5 multi array not working
- Swift Package Manager Support? HOT 2
- [Feature] MLMultiArray -> UIImage without premultiplied alpha HOT 2
- Prediction discrepancies between Python's coremltools and Swift's CoreML API HOT 4
- Need help to adjust bbox for cropped images HOT 7
- pixelBuffer not public HOT 1
- MLMultiarray+Image.swift file gives error HOT 1
- How to use the transposed function? from (26, 64, 48, 1) to (1, 26, 64, 48) HOT 3
- CoreGraphics resizing function from iOS (UIImage) to MacOS (NSImage) HOT 6
- PixelBuffer function from iOS (UIImage) to MacOS (NSImage) HOT 3
- UI Image to kCVPixelFormatType_422YpCbCr8FullRange pixel format not working HOT 4
- Add support for transpose with dataType int and float
- Change image to multiArray in case of converted mlmodel from TFlite
- Question about backprop HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from coremlhelpers.