Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (*
- This code is part of the article found at:
- https://slacky.one/article/simba-2_0-a-step-into-machine-learning
- *)
- function BytesToInt32(Bytes: TByteArray; StartIndex: Integer): Integer;
- begin
- Result := (Bytes[StartIndex] shl 24) or (Bytes[StartIndex + 1] shl 16) or
- (Bytes[StartIndex + 2] shl 8) or Bytes[StartIndex + 3];
- end;
- function ParseMNISTFile(FileName: string): array of TSingleMatrix;
- var
- FileData: TByteArray;
- MagicNumber, NumImages, NumRows, NumCols: Integer;
- i, r, c, Offset: Integer;
- begin
- if not FileExists(FileName) then
- begin
- WriteLn('File not found: ', FileName);
- Exit;
- end;
- // Read the entire file into memory
- FileData := FileReadBytes(FileName);
- if Length(FileData) = 0 then
- begin
- WriteLn('Failed to read file or file is empty.');
- Exit;
- end;
- // Parse the header (16 bytes)
- MagicNumber := BytesToInt32(FileData, 0);
- NumImages := BytesToInt32(FileData, 4);
- NumRows := BytesToInt32(FileData, 8);
- NumCols := BytesToInt32(FileData, 12);
- if MagicNumber <> 2051 then
- begin
- WriteLn('Invalid magic number: ', MagicNumber);
- Exit;
- end;
- // Allocate memory for images
- SetLength(Result, NumImages);
- for i := 0 to NumImages - 1 do
- SetLength(Result[i], NumRows, NumCols);
- // Parse image data
- Offset := 16; // Image data starts after the header
- for i := 0 to NumImages - 1 do
- begin
- for r := 0 to NumRows - 1 do
- begin
- for c := 0 to NumCols - 1 do
- begin
- Result[i][r][c] := FileData[Offset];
- Inc(Offset);
- end;
- end;
- end;
- end;
- function ParseMNISTLabels(FileName: string): array of Byte;
- var
- FileData: TByteArray;
- MagicNumber, NumLabels, Offset: Integer;
- i: Integer;
- begin
- if not FileExists(FileName) then
- begin
- WriteLn('File not found: ', FileName);
- Exit;
- end;
- // Read the entire file into memory
- FileData := FileReadBytes(FileName);
- if Length(FileData) = 0 then
- begin
- WriteLn('Failed to read file or file is empty.');
- Exit;
- end;
- // Parse the header (8 bytes)
- MagicNumber := BytesToInt32(FileData, 0);
- NumLabels := BytesToInt32(FileData, 4);
- if MagicNumber <> 2049 then
- begin
- WriteLn('Invalid magic number: ', MagicNumber);
- Exit;
- end;
- // Allocate memory for labels
- SetLength(Result, NumLabels);
- // Parse labels (starting at offset 8)
- Offset := 8;
- for i := 0 to NumLabels - 1 do
- begin
- Result[i] := FileData[Offset];
- Inc(Offset);
- end;
- end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement