Hello, I was trying to use a tflite model in the Portenta H7 with the Vision Shield.
The problem is that the tflite model after quantizing it is still around 3MB. Since the portenta board has 2MB internal memory, my intention would be to save the model in QSPI flash, and import it to the SDRAM in runtime. I am not sure how viable it is. However, what I have tried is after resetting the board adding the model by hand in the file system, and trying to access it.
#include "mbed.h"
#include <stdio.h>
#include <errno.h>
#include <functional>
#include "BlockDevice.h"
#include "FATFileSystem.h"
FATFileSystem fs("fs");
// Maximum number of elements in buffer
#define BUFFER_MAX_LEN 10
#define FORCE_REFORMAT false
// This will take the system's default block device
BlockDevice *bd = BlockDevice::get_default_instance();
void setup(){
Serial.begin(9600);
while(!Serial);
Serial.println("--- Mbed OS filesystem example ---\n");
// Setup the erase event on button press, use the event queue
// to avoid running in interrupt context
// Try to mount the filesystem
printf("Mounting the filesystem... ");
fflush(stdout);
int err = fs.mount(bd);
printf("%s\n", (err ? "Fail :(" : "OK"));
if (err || FORCE_REFORMAT) {
// Reformat if we can't mount the filesystem
Serial.println("formatting... ");
fflush(stdout);
err = fs.reformat(bd);
printf("%s\n", (err ? "Fail :(" : "OK"));
if (err) {
error("error: %s (%d)\n", strerror(-err), err);
}
}
printf("Opening \"/fs/best_int8.tflite\"... ");
fflush(stdout);
FILE *f = fopen("/fs/best_int8.tflite", "r+");
printf("%s\n", (!f ? "Fail :(" : "OK"));
if (!f) {
// Create the numbers file if it doesn't exist
printf("No file found, creating a new file... ");
Serial.println("Need to add the model");
}
Serial.println("If no print, model added and visible!");
}
void loop(){}
The code I have been using is an example from the mbed os, with a few changes from prints to serials, so that I can understand where it is failing. From what I can see it cannot even mount to the filesystem, since I always get the message "formatting...".
Is this the way to do it if I don't want to use an SD Card?
If not, do you have any suggestion for another approach?
Hi @pedro_ctcp . Made a few tweaks to your code. The line mbed::MBRBlockDevice user_data(&bd, 3); may need to be changed depending on which partition you're using to store the file.
Happy to answer any follow-ups.
#include "mbed.h"
#include <stdio.h>
#include <errno.h>
#include <functional>
//#include "BlockDevice.h"
#include <QSPIFBlockDevice.h>
#include <MBRBlockDevice.h>
#include "FATFileSystem.h"
//FATFileSystem fs("fs");
// Maximum number of elements in buffer
#define BUFFER_MAX_LEN 10
#define FORCE_REFORMAT false
// This will take the system's default block device
//BlockDevice *bd = BlockDevice::get_default_instance();
QSPIFBlockDevice bd;
mbed::MBRBlockDevice user_data(&bd, 3); // 1 is WiFi partition, 2 is for OTA. set to 3 or 4 depending on where you've placed the file
mbed::FATFileSystem fs("fs");
void setup(){
Serial.begin(9600);
while(!Serial);
Serial.println("--- Mbed OS filesystem example ---\n");
// Setup the erase event on button press, use the event queue
// to avoid running in interrupt context
// Try to mount the filesystem
printf("Mounting the filesystem... ");
fflush(stdout);
// int err = fs.mount(bd);
int err = fs.mount(&user_data);
printf("%s\n", (err ? "Fail :(" : "OK"));
if (err || FORCE_REFORMAT) {
// Reformat if we can't mount the filesystem
Serial.println("formatting... ");
fflush(stdout);
err = fs.reformat(&user_data);
printf("%s\n", (err ? "Fail :(" : "OK"));
if (err) {
error("error: %s (%d)\n", strerror(-err), err);
}
}
printf("Opening \"/fs/best_int8.tflite\"... ");
fflush(stdout);
FILE *f = fopen("/fs/best_int8.tflite", "r+");
printf("%s\n", (!f ? "Fail :(" : "OK"));
if (!f) {
// Create the numbers file if it doesn't exist
printf("No file found, creating a new file... ");
Serial.println("Need to add the model");
}
Serial.println("If no print, model added and visible!");
}
void loop(){}
I tried switching to the code from your answer, but it also failed to mount to partition number 3.
I may be understanding something wrong when it comes to where files are stored. I am saying this because, what I am doing is: As I reset the board: in my PC's file system, I copy the model to the board, since it is being read as an external memory device. I was assuming that because I can see it has around 11MB and has a FAT file system. I am "surely" placing the model in the external memory.However, when using the code from this discussion, I also get that all 4 partitions are empty. This probably means that my assumptions are incorrect, and are actually part of what is causing such errors.
Partition: 1
Bootable: No
Type: 0x00
================================
Partition: 2
Bootable: No
Type: 0x00
================================
Partition: 3
Bootable: No
Type: 0x00
================================
Partition: 4
Bootable: No
Type: 0x00
No more partitions are present.
Total Space [KBytes]: 16384
Allocated Space [KBytes]: 0
Unallocated Space [KBytes]: 16384