1

Overview of my code: in python, I trained a GAN model to generate an image, given a vector of 8 numerical values (8 numbers ranging from -1 to 1). Let's call such a vector "noise". So, this CGAN model takes noise as input and then generates an image. If I want to generate 100 images, then the noise input would be an array of 100 vectors (each with 8 numerical values ranging from -1 to 1). See example below.

noise = np.random.rand(100,8) #generating  array of 100 vectors w/ 8 numerical values each
noise = 2.0*noise - 1 
gen_imgs = gen_mdl.predict(noise) #images generated with trained GAN model

Then, after I generate these images, I also want to assign them a "performance" value. This is where matlab comes into play. So, in matlab, I trained a kriging model to take each image's "noise" vector as input (the 8 numerical values) and then output a performance value (a value ranging from 0 to 700). The problem is that for 100 images, it takes about 2 minutes to generate the performance values for each. And, I think this latency is due to the matlab-python connection. So, I'm wondering if there's a different way that I can connect matlab & python in order to speed up the generation of the 100 performance values.

This is the python function that calls the matlab model (that generates performance values). Note: the "comp_vals" or "comp_scores" are the performance values.

def gen_comp_vals(noise):
    sio.savemat('latent_vars.mat', {'latent_vars':noise})
    eng = matlab.engine.start_matlab()
    eng.Kriging_to_python(nargout=0)
    eng.quit()
    comp_vals=sio.loadmat('comp_vals.mat')
    comp_scores=comp_vals['comp_vals'][0]
    return comp_scores

And, this is the matlab file "Kriging_to_python" that the python function above is calling:

%Load matlab workspace with kriging model
krig_workspace=load('C:\Users\User\Documents\MATLAB\krig_posttrain_workspace2.mat');
%Import latent variable values from python
latent_from_python=load('/Users/User/Documents/Framework/latent_vars.mat');
latent_vars=latent_from_python.latent_vars;
latent_vars=single(latent_vars);
%Save kriging model from workspace as new variable
krig_model=krig_workspace.dmodel;
%Run kriging model with latent variables generated from python
[comp_vals MSEpredict] = predictor(latent_vars, krig_model);
comp_vals=transpose(comp_vals);
save('/Users/User/Documents/Framework/comp_vals.mat','comp_vals');

From what's shown above, do you see a change I can make to speed up the generation of these performance values? Possibly a different way to connect matlab & python?

Thanks!

srv_77
  • 547
  • 1
  • 8
  • 20

1 Answers1

0

You should matlab.engine.start_matlab() once at the start of your program, and eng.quit() once at the end of your program. Starting up MATLAB takes a significant amount of time. It looks like you do this for every image.

You could also pass the results back in a variable, rather than a file.

Cris Luengo
  • 55,762
  • 10
  • 62
  • 120
  • Thanks for the response! For the first point, the input to gen_comp_vals is the noise vector of all 100 images (so I open & close MATLAB only once). But, I'll still implement this recommendation & see if that speeds it up. As for your second point, are you referring to the 'comp_vals.mat' file? Thanks. – srv_77 Jan 31 '22 at 03:56
  • Yeah, you pass data through a MAT-file. This shouldn’t be a huge deal, but it can be a bit faster not to use a file. If you only do this once, why is 2 minutes a problem? If you do it repeatedly, make sure you don’t start and stop MATLAB every time. You can make MATLAB persist also across Python sessions. – Cris Luengo Jan 31 '22 at 03:59
  • How would I pass the results back as a variable instead of a file? Also, It's a problem because it's done in an interactive setting so at best, it should take <5 seconds. What do you mean by the last sentence ("...make MATLAB persist also across python sessions.")? Thanks! – srv_77 Jan 31 '22 at 04:30
  • @SofiaRValdez See https://www.mathworks.com/help/matlab/matlab_external/connect-python-to-running-matlab-session.html – Cris Luengo Jan 31 '22 at 05:41