Connecting Matlab to Tensorflow - python

I am using an open-source Matlab toolbox for brain-computer interface (BCI). I want to send the brain imaging data over to Tensorflow for classification and get the results back to Matlab. Is there any way to pass data structures from Matlab to Tensorflow and get the results back into Matlab?

In case someone lands here with a similar question, I'd like to suggest a Matlab package I am currently writing. It's called tensorflow.m and it's available on GitHub. There's no stable release yet, but simple functionality like importing a frozen graph and running an inference is already possible (see the examples) - this is all you'd need to classify the images in Matlab (only).
The advantage is that you don't need any expensive toolbox nor a Python/Tensorflow installation on your machine. The Python interface of Matlab also seems to be rather adventurous, while tensorflow.m is pure Matlab/C.
I'd be glad if the package can be of use for someone looking for similar solutions; even more so, in case you extend/implement something and open a PR.

So far the best way I found is to run your python module in matlab through matlab's now built-in mechanism for connecting to python:
I wrote my python script in a .py file and in there I imported tensorflow and used it in different functions. You can then return the results to matlab by calling
results = py.myModule.myFunction(arg1,arg2,...,argN)
More detailed instructions for calling user-defined python modules in matlab could be found in the following link:
http://www.mathworks.com/help/matlab/matlab_external/call-user-defined-custom-module.html

Related

Use matlab code in python or get something similar

In my program in need to reconstruct matlab code in python but not in very low level. The problem is that i got following lines of matlab
p = sobolset(problem_size, 'Skip', 1e4, 'Leap', 1e3);
p = scramble(p, 'MatousekAffineOwen');
rand0 = net(p, pop_size);
And I'm not able to reconstruct it on my own. Didn't find python functions in python libraries that would do exacly the same. So my question is if i could easly blend in this matlab code to python code or do someone have other idea how I can deal with it?
Your best bet here is probably to use either MATLAB Compiler SDK to generate a Python package from your MATLAB code or to use MATLAB Engine for Python.
If you are going to share this code with others that don't have a MATLAB installation or if you're running on a machine that doesn't have a MATLAB installation, you CANNOT use MATLAB Engine. Compiler SDK does require you to have that specific license though. If you are licensed through a university, you probably already have access to that license.

Python equivalent of MATLAB function findchangepts

I need a python equivalent of the MATLAB command findchangepts . I am unable to find one yet. Essentially I want to find K abrupt changes in my time series.
I am able to use the matlab command but unable to find an equivalent function in python. Any help/pointers to existing libraries will be very helpful. I am not proficient in the optimization modules of scipy and thus would prefer an existing python package.
Thanks.
If you cannot use Matlab under the hood there is e.g. ruptures which implements change point detection (docs).
Just in case the answer might help:
I have both MATLAB and Python on my computer. I was able to use MATLAB bindings for python to use findchangepts in my python code.
Source for bindings : MATLAB API for Python

FFT on RaspPi's GPU?

I am currently working on a Raspberry Pi project in which I am trying to calculate a FFT on some numpy arrays containing some measurement data. I would like to get this done on the GPU to free resources on the CPU. I found the GPU_FFT library of Andrew Holme that apparently allows exactly that.
http://www.aholme.co.uk/GPU_FFT/Main.htm
However, I do not know how to use this library exactly (I read the included instructions) as I have no knowledge of interaction between Python and C (I have never used C before). Further, I could not find any instructions on how to use GPU_FFT on the internet.
Is there any documentation/explanation that I could not find myself or can I use other Python libaries like PyFFT?
Have you tried using the Mathematica package on the rpi? I haven't but they advertise that their code is very fast and use the gpu.

How will I integrate MATLAB to TensorFlow?

I want to integrate MATLAB and TensorFlow, although I can run TensorFlow native in python but I am required to use MATLAB for image processing. Can someone please help me out with this one?
could this work?
A MATLAB implementation of the TensorFlow Neural Networks Playground.
https://github.com/StackOverflowMATLABchat/NeuralNetPlayground
I used a mex function for inference via the C++ API of TensorFlow once. That's pretty straight forward. I had to link the required TensorFlow libs statically from source though.
Similar to #HansQ's suggestion, I am currently writing a somewhat extensive wrapper called tensorflow.m for Matlab, see its repo on GitHub. I cannot provide a stable release yet, but simple functionality like importing a frozen graph and running an inference is already possible (see the examples).
I'd be glad if the package can be of use for someone looking for similar solutions; even more so, in case you extend/implement something and open a PR.

Are all the algorithms of Tensorflow written in C++ and Python only serve to be easy-to-use APIs?

I know that Tensorflow is written with a C++ engine, but I haven't found any C++ source code in my installation directory (I installed via pip). When I inspect the python codes, I got a sense that the python level is just a wrapper where the essence of the algorithm is not presented. For example, in tensorflow/python/ops/gradients.py, the gradients() function calls python_grad_func() to compute the gradients, which is a class method of DeFun.
My question is that, are all the essential part of Tensorflow written in C++ and the python are only serving as some APIs?
This is mostly correct, though there's a lot of sophisticated stuff implemented in Python. Instead of saying "algorithms" in C++, what I'd say is that the core dataflow execution engine and most of the ops (e.g., matmul, etc.) are in C++. A lot of the plumbing, as well as some functionality like defining gradients of functions, is in Python.
For more information and discussion about why it's this way, see this StackOverflow answer

Categories