cannot use the 'machineformat' input argument on calling loadMNISTImages function from simulink

147 Views Asked by At

I am calling the simple CNN which classifies the MNIST images. The CNN internally calls loadMNISTImages() functions to read images from a file. When this CNN is connected to my simulink model.

I am getting the following error:

For code generation, you cannot use the 'machineformat' input argument. Function 'loadMNISTImages.m' (#77.233.262), line 8, column 9: "fread(fp, 1, 'int32', 0, 'b')" Launch diagnostic report.

This is the function which reads the MNIST images:

 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

The above function is called from a function 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

Finally i am calling this function from a state in Stateflow chart and hence getting this error. Please can anyone help ?:)

1

There are 1 best solutions below

1
Phil Goddard On

Any function that cannot be converted to equivalent C code needs to be defined as coder.extrinsic

In your case you need to use coder.extrinsic('loadMNISTImages');.

You will also likely have problems related to the size of your variables. Simulink/Stateflow will almost certainly not allow you to change the dimension of Images on the fly - you will probably need to move the reshape into loadMNISTImages, and you will need to predefine the size of Images and Labels using something like

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