Skip to content

Commit 9e2f6e3

Browse files
committed
More detail on Unary reshaping, See for example lucasb-eyer#58.
1 parent 27169fd commit 9e2f6e3

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

README.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ You can then set a fixed unary potential in the following way:
5555

5656
```python
5757
U = np.array(...) # Get the unary in some way.
58-
print(U.shape) # -> (5, 640, 480)
58+
print(U.shape) # -> (5, 480, 640)
5959
print(U.dtype) # -> dtype('float32')
6060
U = U.reshape((5,-1)) # Needs to be flat.
6161
d.setUnaryEnergy(U)
@@ -69,6 +69,15 @@ probabilities `py`, don't forget to `U = -np.log(py)` them.
6969
Requiring the `reshape` on the unary is an API wart that I'd like to fix, but
7070
don't know how to without introducing an explicit dependency on numpy.
7171

72+
**Note** that the `nlabels` dimension is the first here before the reshape;
73+
you may need to move it there before reshaping if that's not already the case,
74+
like so:
75+
76+
```python
77+
print(U.shape) # -> (480, 640, 5)
78+
U = U.transpose(2, 0, 1).reshape((5,-1))
79+
```
80+
7281
### Getting a Unary
7382

7483
There's two common ways of getting unary potentials:
@@ -251,6 +260,15 @@ This is a pretty [co](https://github.com/lucasb-eyer/pydensecrf/issues/52)mm[on]
251260
It means exactly what it says: you are passing a `double` but it wants a `float`.
252261
Solve it by, for example, calling `d.setUnaryEnergy(U.astype(np.float32))` instead of just `d.setUnaryEnergy(U)`, or using `float32` in your code in the first place.
253262

263+
My results are all pixelated like [MS Paint's airbrush tool](http://lmgtfy.com/?q=MS+Paint+Airbrush+tool)!
264+
----------------------------------------------------------
265+
266+
You screwed up reshaping somewhere and treated the class/label dimension as spatial dimension.
267+
This is you misunderstanding NumPy's memory layout and nothing that PyDenseCRF can detect or help with.
268+
269+
This mistake often happens for the Unary, see the [**Note** in that section of the README](https://github.com/lucasb-eyer/pydensecrf#unary-potential).
270+
271+
254272
Maintaining
255273
===========
256274

0 commit comments

Comments
 (0)