首页 > 解决方案 > Is it possible to take a trained CNN stored as a .mat file and use that file to make a classification algorithm in Python?

问题描述

I have created a CNN in MATLAB 2019b and the trained CNN is stored in a file called "trainednet.mat". I was wondering how you can load the trained .mat file in Python and classify the images in Python instead of loading it on MATLAB 2019b. I have tried to convert my .mat file to .h5 file using h5py so that I can use it in keras but it does not work. I have also heard of a function in scipy called sio.loadmat docs.scipy.org/doc/scipy/reference/tutorial/io.html but I am unsure if this can be used for implementation in python. Will the methods I have stated above work if implemented correctly or is there another way to approach this problem?

标签: pythonmatlabconv-neural-network

解决方案


A short answer is that it is possible, however, it's not hassle-free as you will need to recreate a part of the network (at least the part for prediction) in python code. It will work with just numpy and scipy, or can also recreate with PyTorch / TensorFlow for utilising GPUs.


The trainednet.mat you mentioned probably stored the weights of the NN, and you will need to recreate the architecture of the NN based on the original model (the one in matlab), i.e., perform matrix calculations in python.

As you had mentioned, you will first load the .mat by scipy with

from scipy.io import loadmat

parameters = loadmat("trainednet.mat")

and now parameters is a Python dictionary where it's structure is hugely dependent on your network. The following details will be different for each kind of CNN, but the basic workflow is something similar to:

def predict(img, parameters):
    # the kind of saved parameters
    # your trainednet.mat should have weights for something like
    # w1,w2,w3,w4... for weights of convolutional layers
    #                    weights of dense layers
    # b3,b4...       for any bias from layers

    # convolution operation
    z = convolution(..., img, w1) 
    # ReLU non-linearity
    z[z<=0] = 0 

    # convolution operation
    z = convolution(..., z, w2)
    # ReLU non-linearity
    z[z<=0] = 0 

    z = maxpool(z, ...).reshape(...) # maxpooling operation

    # dense layer with bias
    z = w3.dot(z) + b3 
    # ReLU non-linearity
    z[z<=0] = 0

    # second dense layer with bias
    z = w4.dot(z) + b4 

    # predict class probabilities
    probs = softmax(z) 
    return probs


from scipy.ndimage import imread
img = imread(FILENAME, mode="RGB")

predict(img, parameters)

Most part are easy to implement, except the convolution operation which is extremely tricky. If you do want to go on this route, you might be better off if you use NN library for it.


In short, it's possible but required a lot of effort to re-implement the same code (code as in the NN architecture) from matlab to python. You can refer to this guide as a starter of converting matlab pretrained network in python.


推荐阅读