【问题标题】:cannot use the 'machineformat' input argument on calling loadMNISTImages function from simulink在从 simulink 调用 loadMNISTImages 函数时无法使用“machineformat”输入参数
【发布时间】:2019-11-25 10:58:36
【问题描述】:

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

我收到以下错误:

对于代码生成,您不能使用“machineformat”输入 争论。函数 'loadMNISTImages.m' (#77.233.262),第 8 行,第 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:\Users\surinder\Downloads\experiments\cnn\MNIST\t10k-images.idx3-ubyte');
 Images = reshape(Images, 28, 28, []);
 Labels = 
 loadMNISTLabels('C:\Users\surinder\Downloads\experiments\cnn\MNIST\t10k- 
 labels.idx1-ubyte');
 Labels(Labels == 0) = 10;    % 0 --> 10

最后,我从 Stateflow 图中的状态调用此函数,因此出现此错误。请问有人可以帮忙吗?:)

【问题讨论】:

    标签: image matlab conv-neural-network simulink stateflow


    【解决方案1】:

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

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

    您也可能会遇到与变量大小相关的问题。 Simulink/Stateflow 几乎肯定不允许您即时更改 Images 的尺寸 - 您可能需要将 reshape 移动到 loadMNISTImages,并且您需要预定义 Images 和 @ 的大小987654327@ 使用类似

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

    【讨论】:

    • 我试过了,现在编译很干净,但仿真没有运行,Simulink 对话框打印消息“正在启动”,它就出来了。我需要做更多的事情吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-25
    • 2018-11-03
    • 1970-01-01
    • 2021-01-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多