python - 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?
解决方案
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.
推荐阅读
- python-3.x - OpenCV CvCapture_MSMF::grabFrame videoio(MSMF):无法抓取帧。错误:-2147483638
- c - 分段错误:命令参数 (argv)
- php - Mailgun 不显示错误或在 Laravel 中发送
- java - 如何将“2020-12-20T00:00:00.000Z”转换为 java.util.Date?
- javascript - 尝试安装 nextJS 时发生此错误
- bash - 从 unix 中的目录文件中查找不匹配的列表
- python - os.listdir 未列出目录中的文件
- perl - 如何在 Perl XS 中封装库句柄
- javascript - 为什么我在 google apss 脚本中出现错误“您无法安排过去的活动”
- c++ - 将 std::make_pair 与 std::string 一起使用(非右值引用问题)