ValueError: Failed to allocate tensors issue

Hi

I want to test facial expression classification on the FER2013 dataset.

I trained a simple CNN and MobileNetV2 and converted them to TFLite.

However, when I placed them on the OpenMV H7 Plus’s internal flash and tried to load the model, I encountered a ‘ValueError: Failed to allocate tensors’.

What is the reason for this, and how can I solve it?

Can you post the model? I can examine it.

Hi Kwagyeman

I have modified three versions of the model as listed below, but I am still getting the ‘ValueError: Failed to allocate tensors’.

Could you please take a look and advise on how to improve this?

IMG_HEIGHT = 48
IMG_WIDTH = 48
CHANNELS = 1


def simple_cnn_for_openmv():
    input_tensor = layers.Input(shape=(IMG_HEIGHT, IMG_WIDTH, 1))
    x = layers.Conv2D(16, (3, 3), padding='same', use_bias=False)(input_tensor)
    x = layers.BatchNormalization()(x)
    x = layers.Activation('relu')(x)
    x = layers.MaxPooling2D(2, 2)(x) 
    
    x = layers.Conv2D(32, (3, 3), padding='same', use_bias=False)(x)
    x = layers.BatchNormalization()(x)
    x = layers.Activation('relu')(x)
    x = layers.MaxPooling2D(2, 2)(x) 

    x = layers.Conv2D(64, (3, 3), padding='same', use_bias=False)(x)
    x = layers.BatchNormalization()(x)
    x = layers.Activation('relu')(x)
    x = layers.MaxPooling2D(2, 2)(x)
    
    x = layers.GlobalAveragePooling2D()(x)
    x = layers.Dropout(0.2)(x)
    
    output_tensor = layers.Dense(NUM_CLASSES, activation='softmax')(x)
    model = models.Model(inputs=input_tensor, outputs=output_tensor)
    return model
def ultra_lite_cnn():
    input_tensor = layers.Input(shape=(IMG_HEIGHT, IMG_WIDTH, 1))
    x = layers.Conv2D(8, (3, 3), padding='same', use_bias=False)(input_tensor)
    x = layers.BatchNormalization()(x)
    x = layers.Activation('relu')(x)
    x = layers.MaxPooling2D(2, 2)(x) 

    x = layers.Conv2D(16, (3, 3), padding='same', use_bias=False)(x)
    x = layers.BatchNormalization()(x)
    x = layers.Activation('relu')(x)
    x = layers.MaxPooling2D(2, 2)(x) 
    
    x = layers.Conv2D(32, (3, 3), padding='same', use_bias=False)(x)
    x = layers.BatchNormalization()(x)
    x = layers.Activation('relu')(x)
    x = layers.MaxPooling2D(2, 2)(x)
    
    x = layers.GlobalAveragePooling2D()(x)
    x = layers.Dropout(0.2)(x)
    
    output_tensor = layers.Dense(NUM_CLASSES, activation='softmax')(x)
    model = models.Model(inputs=input_tensor, outputs=output_tensor)
    return model
def nano_strided_model():
    input_tensor = layers.Input(shape=(IMG_HEIGHT, IMG_WIDTH, 1))
    x = layers.Conv2D(8, (3, 3), strides=(2, 2), padding='same', use_bias=False)(input_tensor)
    x = layers.BatchNormalization()(x)
    x = layers.Activation('relu')(x)
    
    x = layers.Conv2D(16, (3, 3), strides=(2, 2), padding='same', use_bias=False)(x)
    x = layers.BatchNormalization()(x)
    x = layers.Activation('relu')(x)
    
    x = layers.Conv2D(32, (3, 3), strides=(2, 2), padding='same', use_bias=False)(x)
    x = layers.BatchNormalization()(x)
    x = layers.Activation('relu')(x)
    
    x = layers.GlobalAveragePooling2D()(x)
    x = layers.Dropout(0.2)(x)
    output_tensor = layers.Dense(NUM_CLASSES, activation='softmax')(x)

    model = models.Model(inputs=input_tensor, outputs=output_tensor)
    return model

Hi, I need the INT8 quantized TFLITE files.

Hi Kwagyeman

I cannot upload the .tflite file here. Is it okay if I put the file on my Google Drive?

The .tflite files in the Google Drive folder are the ones I tested, and all of them caused the ‘ValueError: Failed to allocate tensors’ issue.

fer_mobilenet_v2_035_quantized.tflite

tflm_backend: tensorflow/lite/micro/kernels/fully_connected_common.cc FullyConnected per-channel quantization not yet supported. Please set converter._experimental_disable_per_channel_quantization_for_dense_layers = True.
tflm_backend: Node FULLY_CONNECTED (number 64f) failed to prepare with status 1

fer_nanoCNN_quantized.tflite

tflm_backend: tensorflow/lite/micro/kernels/fully_connected_common.cc FullyConnected per-channel quantization not yet supported. Please set converter._experimental_disable_per_channel_quantization_for_dense_layers = True.
tflm_backend: Node FULLY_CONNECTED (number 4f) failed to prepare with status 1

fer_picoCNN_quantized_32x32.tflite

tflm_backend: tensorflow/lite/micro/kernels/fully_connected_common.cc FullyConnected per-channel quantization not yet supported. Please set converter._experimental_disable_per_channel_quantization_for_dense_layers = True.
tflm_backend: Node FULLY_CONNECTED (number 4f) failed to prepare with status 1

fer_SimpleCNN_quantized.tflite

tflm_backend: tensorflow/lite/micro/kernels/fully_connected_common.cc FullyConnected per-channel quantization not yet supported. Please set converter._experimental_disable_per_channel_quantization_for_dense_layers = True.
tflm_backend: Node FULLY_CONNECTED (number 7f) failed to prepare with status 1

Same error for all.

Hi Kwagyeman

I updated the Python code used to convert the model to TFLite, and the model now loads successfully. However, when I pass an image to the model for prediction, a new error occurs: ‘MemoryError: memory allocation failed, allocating XXXXXX bytes’.

Could the issue be caused by the TFLite conversion process? (My TFLite conversion code is attached below.)

try:
    model_path = f'{MODEL_NAME}.h5'
    if not os.path.exists(model_path):
        raise FileNotFoundError(f"File not found: {model_path}")
        
    model = tf.keras.models.load_model(model_path)
    converter = tf.lite.TFLiteConverter.from_keras_model(model)
    converter.optimizations = [tf.lite.Optimize.DEFAULT]
    converter.representative_dataset = representative_data_gen
    converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS_INT8]

    converter.inference_input_type = tf.int8
    converter.inference_output_type = tf.int8
    converter._experimental_disable_per_channel_quantization_for_dense_layers = True
    
    tflite_model = converter.convert()
    tflite_filename = f'{MODEL_NAME}_quantized.tflite'
    with open(tflite_filename, 'wb') as f:
        f.write(tflite_model)

    print(f"Success!")
    print(f"檔案: {tflite_filename}")
    print(f"大小: {len(tflite_model) / 1024:.2f} KB")
except Exception as e:
    print(f"Fail: {e}")

Hi, scale of 32 means 32x the image size. You are creating a giant image which doesn’t fit in ram.

The line of code in error is the copy operation.

Hi Kwagyeman

Thank you for pointing out the error. It is working properly now.