Invalid MEX file from any other folder except private folder - matlab

I have compiled matcaffe with caffe in Windows.
My current matcaffe folder structure is as below:
.
C:\PYTHON\caffe
|-- matlab
|
|-- +caffe
|
|--private
|
|-- caffe_.mexw64
|...
|-- all hard linked dlls
I have checked caffe_.mexw64 using dependency walker that the following files are missing:
LIBMEX.DLL
LIBMX.DLL
API-MS-WIN-CORE-WINRT-ERROR-L1-1-0.DLL
API-MS-WIN-CORE-WINRT-L1-1-0.DLL
API-MS-WIN-CORE-WINRT-ROBUFFER-L1-1-0.DLL
API-MS-WIN-CORE-WINRT-STRING-L1-1-0.DLL
DCOMP.DLL
IESHIMS.DLL
which is quite obvious from dependency walker. All caffe-related dlls are found.
In matlab, I have added the following path:
addpath('C:\PYTHON\caffe\matlab');
but on running this command:
caffe.run_tests()
I get the following error:
Invalid MEX-file 'C:\PYTHON\caffe\matlab\+caffe\private\caffe_.mexw64': The
specified procedure could not be found.
Error in caffe.set_mode_cpu (line 5)
caffe_('set_mode_cpu');
Error in caffe.run_tests (line 6)
caffe.set_mode_cpu();
Interesting thing is the following. If I go to the caffe\matlab\+caffe\private folder and run the same caffe.run_tests(), then it runs successfully.
And thereafter only, I can run the matcaffe commands without MEX errors from any other folder (as the dlls are then already loaded in the memory).
My questions are:
What is happening here that I cannot directly run from any other folder?
How to solve this to get the normal behaviour?
Update:
Now I see that the same problem has been posted here, but no proper solution.

You need to add the directory with the DLLs to the PATH environment variable. You can do this from within MATLAB on Windows (this does not work to modify the equivalent LD_LIBRARY_PATH on Linux and Unix):
setenv('PATH',['C:\PYTHON\caffe\matlab\+caffe\private\;',getenv('PATH')]);

Related

Link to gfortran for mex file

I am trying to create mex files for using some C++ code in Matlab, but running into an error with the compilation which, I guess, goes beyond Matlab. I am on Windows.
The example I'm struggling with is the Mex Interface example in the Armadillo library for linear algebra, available: here. The Matlab file is what I'm running and it looks like this:
% Compile the demo as a mex file
mex -lgfortran armaMex_demo.cpp -I/path/to/armadillo
The error I get is:
Error using mex
MEX cannot find library 'gfortran' specified with the -l option.
MEX looks for a file with one of the names:
libgfortran.lib
gfortran.lib
Please specify the path to this library with the -L option.
Trying some things I found online, I installed MinGW and cygwin64 and added the paths to their bin folders to my PATH variable. Before that I also installed MinGW via Matlab.
My question: How do I get it to link with gfortran?
If I search C:for libgfortran I find some files, but none of them have the file ending .lib (but e.g. .dll and .a) and if I search for gfortranI find some .exe files but again, no .lib file. If I run gfortran -dumpversion in the terminal, I get 4.9.3 back, so it is obviously there somewhere. I am obviously missing something, and this is really not my forte, to say the least.

pyinstaller Adafruit DHT library raspberry_Pi_Driver.so: cannot open shared object

I'm using Adafruit_DHT library in a file and when I try to use pyinstaller to compile, I get an error that Raspberry_Pi_Driver.so: cannot open shared object file
I'm using normal RPi (not model 2) and raspbian. The file that I'm trying to compile works by itself fine. The Raspberry_Pi_Driver.so is there I did find ./ | grep Raspberry_Pi_Driver.so and it existed. I tried also to use the --hidden-import=Adafruit_DHT when compiling the pyinsatller and that did not work too. When compiling it does not give any error.
I noticed after compilation and in the build folder thee is an Adafruit_DHT folder that has the driver and etc.
Any idea what's going on? Could it be that the library has been recreated under build folder and this is confusing when executing the file?
I found a resolution for this (thanks to: k4ml.me/posts/pyinstaller.html) I just added '-p /path/to/mylib' when creating the exe file and mylib was the directory that Adafruit_DHT folder was (that Adafruit_DHT_Driver.so file was there).

How to change MATLAB path to compile mex files of FLANN with cmake command?

I try to compile FLANN on a remote machine which I am not a root user. For this purpose I compiled cmake at my home and set an alias to call. I also downloaded FLANN from web and process the compilation as it is suggested at the manual.
mkdir build
cd build
cmake ..
make
It is compiled without an error but the matlab module and mex compilation. Even I add the matlab bin into $PATH it gives following warning after cmake ...
...
CMake Warning at src/matlab/CMakeLists.txt:57 (message):
The 'mex' and 'mexext' programs have been found in different locations.
It's likely that one of them is not part of the MATLAB instalation. Make
sure that the 'bin' directory from the MATLAB instalation is in PATH
...
Because of this issue, matlab mex files are not compiled when I execute make.
I could not solve this problem in any way even I tried to set some cmake configurations. Is there any suggestion to deal with this compilation problem?
echo $PATH
/home/y/bin:/usr/kerberos/bin:/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/bin:/usr/bin:/usr/local/matlab/bin
which mex
/usr/local/bin/mex
which mexext
/usr/local/matlab/bin/mexext
An alternative solution (that worked for me) is to create a symbolic link for mexext in /usr/local/bin that points to where mexext is actually found.
sudo ln -s /usr/local/MATLAB/R2016a/bin/mexext /usr/local/bin/mexext
(The solution is in the comments. See Question with no answers, but issue solved in the comments (or extended in chat) )
The OP wrote:
I solved the problem by taking the matlab path to first place on $PATH

SUN benchmark code Invalid .mex file error

I've downloadad the SUN benchmark code from MIT:
version 2:
http://people.csail.mit.edu/jxiao/SUN/source_code/
Once I download the 15 image dataset and run the function:
compute_features.m
I get an Invalid MEX file error:
Invalid MEX-file
'/home/arturo/Documents/Virality/reddit_data/SUN_source_code_v2/code/OpenCVmexWrapper/cvlib_mex.mexa64':
libcv.so.1: cannot open shared object file: No such file or directory
The file /home/arturo/Documents/Virality/reddit_data/SUN_source_code_v2/code/OpenCVmexWrapper/cvlib_mex.mexa64 actually does exist, so I'm thinking the problem hast to do with libcv.so.1, or the .mexa64 file is broken (which is very unlikely)
I read something about adding a path via LD_LIBRARY_PATH when I googled the error, but I'm not sure what path I should exactly add, at least temporarily. I have opencv installed along with its library, so I'm not sure what the problem is. My current system is Ubuntu 12.04 64bit.
There's a similar question here, but the answer says that for their specific case they have the source code of the MEX files posted, which is not the same fortune I have: running old mex file on new matlab releases
Found the answer. The key is to link explicitly to the folder the file 'libcv.so.1' is in just before running matlab.
so in the matlab folder:
LD_LIBRARY_PATH=/home/arturDocuments/Virality/reddit_data/SUN_source_code_v2/code/scene_sun ./matlab
And in matlab:
matlab>> compute_feature
Compling the source code in the other folders is also necessary.(vlfeat,VOC,libsvm). As well as editing the path in the function im2superpixels # GeometricContext_dhoiem from :
segcmd = '../segment_pedro/segment 0.8 100 100';
to:
segcmd = '/YOUR_PATH/SUN_source_code_v2/code/segment_pedro/segment/ 0.8 100 100';
*(The folder is one level above.)

How to properly install libsvm-3.11?

I'm using libvm-3.11. I downloaded it here and extracted. Then I set path in Matlab. After that whenever I run my program, it gives the same error always.
??? Invalid MEX-file 'E:\Gomu\Gomu General\final yr
Project\proj\libsvm-3.11\windows\svmtrain.mexw32': The specified module could not be found.
Error in ==> trysvm at 6
svmStruct = svmtrain(T,TrainMat,'showplot',true);
What should I do?
read the README file and compile the C files using mex. Mex files are platform/MATLAB version dependent. The binary download doesn't always work for everybody.