luanfujun / deep-photo-styletransfer

Code and data for paper "Deep Photo Style Transfer": https://arxiv.org/abs/1703.07511
9.99k stars 1.41k forks source link

getLaplacian1() function in python #1

Closed jayavanth closed 7 years ago

jayavanth commented 7 years ago

Is there a direct replacement for getLaplacian1() in python? I'm converting genlaplacian.m to python

luanfujun commented 7 years ago

Unfortunately there seems to have no python equivalence as far as I know. You might have to implement Eq. 12 in paper: http://www.wisdom.weizmann.ac.il/~levina/papers/Matting-Levin-Lischinski-Weiss-CVPR06.pdf in python yourself.

ProGamerGov commented 7 years ago

@jayavanth @luanfujun Does this implementation of the research paper you linked to, have the Python equivalent? https://github.com/MarcoForte/closed-form-matting

luanfujun commented 7 years ago

@ProGamerGov Yes, looks like they have the python version

def computeLaplacian(img, eps = 10**(-7), win_rad=1):
ProGamerGov commented 7 years ago

@luanfujun Does the Python function look like it could be converted to Lua?

martinbenson commented 7 years ago

I have converted the code to Python and will do a PR when I'm happy with it. BUT, currently the files I create won't load in the deepmatting_seg.lua script. If I try to just use matio to load them in th i get a 'file could not be opened error'. I suspect that it is because I've based my outputs on what I generated from running get_laplacian.m in Octave in Windows and there are some compatibility issues that result from that.

So, I was wondering if someone could please post a link to a copy of a working .mat file for example 1 in the repo, and I'll then replicate that?

jayavanth commented 7 years ago

@martinbenson how did you store the CSR? I'm storing it as a python list of [ptr, col, data]. Because of this, I'm getting errors when I load the .mat in lua. attempt to call method 'cuda' (a nil value) in this line: `local CSR = matio.load(CSR_fn).CSR:cuda() Should I be storing it as a numpy array?

martinbenson commented 7 years ago

@jayavanth I get the same error (though it really descends form matio not reading the file). Yes, the file is a space seperated list of row, col, val (but with some header bumpf above it). That is how Octave writes the file when I run the gen_laplacian.m script. But, as indicated above, the lua script won't even load the file that I create directly from Octave. So, at this point my suspicion is that the Octave and Matlab implementations differ and matio is sensitive to it.

martinbenson commented 7 years ago

I can probably figure out what format to target from the first 100 lines of a working file if it'd be easier for someone to post?

jayavanth commented 7 years ago

In deepmatting_seg.lua, it's accessed column-wise (MATLAB is column-major). I'm trying to store the CSR as a matrix with indptr, indices and data as columns. But I'm getting RuntimeError: maximum recursion depth exceeded when I do savemat

martinbenson commented 7 years ago

I just added a PR to move to Python code rather than MATLAB. In the meantime, my forked repo is here: https://github.com/martinbenson/deep-photo-styletransfer

jayavanth commented 7 years ago

Getting this error. Sorry for posting here. Your fork doesn't seem to have an issues page

> python3 mattinglaplacian.py -dir='../examples/input/' -out='./'
Traceback (most recent call last):
  File "mattinglaplacian.py", line 87, in <module>
    CSR = getlaplacian1(img, np.zeros(shape=(h, w)), 1e-7, 1)
  File "mattinglaplacian.py", line 20, in getlaplacian1
    row_inds = np.zeros(tlen)
TypeError: 'numpy.float64' object cannot be interpreted as an integer

edit: casting it into int fixed it

ProGamerGov commented 7 years ago

@martinbenson

Trying to run your Python code works for the laplacian creation step and the step using neuralstyle_seg.lua. When trying to use deepmatting_seg.lua, I get the following errors:

loading matting laplacian...    gen_laplacian/Input_Laplacian_3x3_1e-7_CSR1.csv
<csv>   parsing file: gen_laplacian/Input_Laplacian_3x3_1e-7_CSR1.csv
PANIC: unprotected error in call to Lua API (not enough memory)
loading matting laplacian...    gen_laplacian/Input_Laplacian_3x3_1e-7_CSR1.csv
<csv>   parsing file: gen_laplacian/Input_Laplacian_3x3_1e-7_CSR1.csv
/home/ubuntu/torch/install/bin/luajit: not enough memory

And there does not appear to be a way to select the correct input laplacian file (so one must rename the file and place it in the gen_laplacian/ directory):

loading matting laplacian...    gen_laplacian/Input_Laplacian_3x3_1e-7_CSR1.csv
<csv>   parsing file: gen_laplacian/Input_Laplacian_3x3_1e-7_CSR1.csv
/home/ubuntu/torch/install/bin/luajit: /home/ubuntu/torch/install/share/lua/5.1/csvigo/File.lua:78: gen_laplacian/Input_Laplacian_3x3_1e-7_CSR1.csv: No such file or directory
martinbenson commented 7 years ago

@jayavanth Oh, I didn't hit that. I'll investigate and update the code though.

martinbenson commented 7 years ago

@progamergov You'll need 8GB GPU to run with the standard settings. I only have 6GB but that works if the images are resized to be 500 wide instead of 700 wide. You'd also need to edit one line in the matting laplacian code - should be obvious which!

The lua code just expects the files to have particular names and be in a particular directory. I'll perhaps look at making all that a bit friendlier in the fork (pull request was rejected for some reason, so I'll just maintain the fork).

martinbenson commented 7 years ago

I turned on issues in the forked repo, so let's cover future stuff there.

ProGamerGov commented 7 years ago

@martinbenson I was attempting to run the code on a 12GB GPU when I got those errors.