不能在从 simulink 调用 loadMNISTImages 函数时使用"机器格式"输入参数



我调用了对MNIST图像进行分类的简单CNN。CNN 在内部调用 loadMNISTImages(( 函数来从文件中读取图像。当这个CNN连接到我的simulink模型时。

我收到以下错误:

对于代码生成,不能使用"机器格式"输入 论点。函数 'loadMNISTImages.m' (#77.233.262(,第 8 行,列 9: "fread(fp, 1, 'int32', 0, 'b'(" 启动诊断报告。

这是读取 MNIST 图像的函数:

 function images = loadMNISTImages(filename)
 %loadMNISTImages returns a 28x28x[number of MNIST images] matrix 
 %containing the raw MNIST images
 fp = fopen(filename, 'rb');
 assert(fp ~= -1, ['Could not open ', filename, '']);
 magic = fread(fp, 1, 'int32', 0, 'b');
 assert(magic == 2051, ['Bad magic number in ', filename, '']);
 numImages = fread(fp, 1, 'int32', 0, 'ieee-be');
 numRows = fread(fp, 1, 'int32', 0, 'ieee-be');
 numCols = fread(fp, 1, 'int32', 0, 'ieee-be');
 images = fread(fp, inf, 'unsigned char=>unsigned char');
 images = reshape(images, numCols, numRows, numImages);
 images = permute(images,[2 1 3]);
 fclose(fp);
 % Reshape to #pixels x #examples
 images = reshape(images, size(images, 1) * size(images, 2), size(images, 3));
 % Convert to double and rescale to [0,1]
 images = double(images) / 255;
 end

上述函数是从函数TestMNISTCONV调用的

 function y2 = TestMnistConv()
 Images = 
 loadMNISTImages('C:UserssurinderDownloadsexperimentscnnMNISTt10k-images.idx3-ubyte');
 Images = reshape(Images, 28, 28, []);
 Labels = 
 loadMNISTLabels('C:UserssurinderDownloadsexperimentscnnMNISTt10k- 
 labels.idx1-ubyte');
 Labels(Labels == 0) = 10;    % 0 --> 10

最后,我从状态流程图中的状态调用此函数,因此收到此错误。请谁能帮忙?:)

任何不能转换为等效 C 代码的函数都需要定义为 coder.extrinsic

在您的情况下,您需要使用 coder.extrinsic('loadMNISTImages'); .

您还可能会遇到与变量大小相关的问题。 Simulink/Stateflow 几乎肯定不允许你即时更改Images的维度 - 您可能需要将reshape移动到 loadMNISTImages 中,并且您需要预定义 Images 的大小,并使用类似的东西Labels

Images = zeros(28,28,...);

最新更新