Advertisement
WarPie90

[Simba 2.0] MNIST data parser

Dec 24th, 2024 (edited)
1,230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 2.53 KB | None | 0 0
  1. (*
  2.   This code is part of the article found at:
  3.   https://slacky.one/article/simba-2_0-a-step-into-machine-learning
  4. *)
  5.  
  6. function BytesToInt32(Bytes: TByteArray; StartIndex: Integer): Integer;
  7. begin
  8.   Result := (Bytes[StartIndex] shl 24) or (Bytes[StartIndex + 1] shl 16) or
  9.             (Bytes[StartIndex + 2] shl 8) or Bytes[StartIndex + 3];
  10. end;
  11.  
  12. function ParseMNISTFile(FileName: string): array of TSingleMatrix;
  13. var
  14.   FileData: TByteArray;
  15.   MagicNumber, NumImages, NumRows, NumCols: Integer;
  16.   i, r, c, Offset: Integer;
  17. begin
  18.   if not FileExists(FileName) then
  19.   begin
  20.     WriteLn('File not found: ', FileName);
  21.     Exit;
  22.   end;
  23.  
  24.   // Read the entire file into memory
  25.   FileData := FileReadBytes(FileName);
  26.   if Length(FileData) = 0 then
  27.   begin
  28.     WriteLn('Failed to read file or file is empty.');
  29.     Exit;
  30.   end;
  31.  
  32.   // Parse the header (16 bytes)
  33.   MagicNumber := BytesToInt32(FileData, 0);
  34.   NumImages   := BytesToInt32(FileData, 4);
  35.   NumRows     := BytesToInt32(FileData, 8);
  36.   NumCols     := BytesToInt32(FileData, 12);
  37.  
  38.   if MagicNumber <> 2051 then
  39.   begin
  40.     WriteLn('Invalid magic number: ', MagicNumber);
  41.     Exit;
  42.   end;
  43.  
  44.   // Allocate memory for images
  45.   SetLength(Result, NumImages);
  46.   for i := 0 to NumImages - 1 do
  47.     SetLength(Result[i], NumRows, NumCols);
  48.  
  49.   // Parse image data
  50.   Offset := 16; // Image data starts after the header
  51.   for i := 0 to NumImages - 1 do
  52.   begin
  53.     for r := 0 to NumRows - 1 do
  54.     begin
  55.       for c := 0 to NumCols - 1 do
  56.       begin
  57.         Result[i][r][c] := FileData[Offset];
  58.         Inc(Offset);
  59.       end;
  60.     end;
  61.   end;
  62. end;
  63.  
  64. function ParseMNISTLabels(FileName: string): array of Byte;
  65. var
  66.   FileData: TByteArray;
  67.   MagicNumber, NumLabels, Offset: Integer;
  68.   i: Integer;
  69. begin
  70.   if not FileExists(FileName) then
  71.   begin
  72.     WriteLn('File not found: ', FileName);
  73.     Exit;
  74.   end;
  75.  
  76.   // Read the entire file into memory
  77.   FileData := FileReadBytes(FileName);
  78.   if Length(FileData) = 0 then
  79.   begin
  80.     WriteLn('Failed to read file or file is empty.');
  81.     Exit;
  82.   end;
  83.  
  84.   // Parse the header (8 bytes)
  85.   MagicNumber := BytesToInt32(FileData, 0);
  86.   NumLabels := BytesToInt32(FileData, 4);
  87.  
  88.   if MagicNumber <> 2049 then
  89.   begin
  90.     WriteLn('Invalid magic number: ', MagicNumber);
  91.     Exit;
  92.   end;
  93.  
  94.   // Allocate memory for labels
  95.   SetLength(Result, NumLabels);
  96.  
  97.   // Parse labels (starting at offset 8)
  98.   Offset := 8;
  99.   for i := 0 to NumLabels - 1 do
  100.   begin
  101.     Result[i] := FileData[Offset];
  102.     Inc(Offset);
  103.   end;
  104. end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement