Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I need help about “assert failed: dl::Tensor<T>& dl::Tensor<T>::reshape(std::vector<int>) [with T = short int] dl_variable.cpp:77 (size == size_gt)“ (AIV-694) #163

Closed
emoof1 opened this issue May 4, 2024 · 3 comments

Comments

@emoof1
Copy link

emoof1 commented May 4, 2024

---------This is my camera config
.pixel_format = PIXFORMAT_RGB565,
.frame_size = FRAMESIZE_96X96,
---------This is my model on python
print(tf.version)

model = Sequential()
model.add(Conv2D(32, (5, 5), activation='relu', input_shape=(96, 96, 3)))
model.add(MaxPooling2D((2, 2)))
model.add(Dropout(0.2))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D((2, 2)))
model.add(Dropout(0.2))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D((2, 2)))
model.add(Dropout(0.2))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D((2, 2)))
model.add(Dropout(0.2))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D((2, 2)))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dense(64, activation='relu'))
model.add(Dense(32, activation='relu'))
model.add(Dense(2, activation='softmax'))

model.compile(optimizer='adam',loss='sparse_categorical_crossentropy',metrics=['accuracy'])

---------This is my model on C produced by python

#pragma once
#include <stdint.h>
#include "dl_layer_model.hpp"
#include "dl_layer_base.hpp"
#include "dl_layer_max_pool2d.hpp"
#include "dl_layer_conv2d.hpp"
#include "dl_layer_reshape.hpp"
#include "dl_layer_softmax.hpp"
#include "handrecognition_coefficient.hpp"
#include "handrecognition_coefficient.cpp"
using namespace dl;
using namespace layer;
using namespace handrecognition_coefficient;
class HANDRECOGNITION : public Model<int16_t>
{
private:
Conv2D<int16_t> l1;
MaxPool2D<int16_t> l2;
Conv2D<int16_t> l3;
MaxPool2D<int16_t> l4;
Conv2D<int16_t> l5;
MaxPool2D<int16_t> l6;
Conv2D<int16_t> l7;
MaxPool2D<int16_t> l8;
Conv2D<int16_t> l9;
MaxPool2D<int16_t> l10;
Reshape<int16_t> l11;
Conv2D<int16_t> l12;
Conv2D<int16_t> l13;
Conv2D<int16_t> l14;
Conv2D<int16_t> l15;
public:
Softmax<int16_t> l16; // output layer
HANDRECOGNITION () :
l1(Conv2D<int16_t>(-8, get_statefulpartitionedcall_sequential_conv2d_biasadd_filter(), get_statefulpartitionedcall_sequential_conv2d_biasadd_bias(), get_statefulpartitionedcall_sequential_conv2d_biasadd_activation(), PADDING_VALID, {}, 1,1, "l1")),
l2(MaxPool2D<int16_t>({2,2},PADDING_VALID, {}, 2, 2, "l2")),
l3(Conv2D<int16_t>(-9, get_statefulpartitionedcall_sequential_conv2d_1_biasadd_filter(), get_statefulpartitionedcall_sequential_conv2d_1_biasadd_bias(), get_statefulpartitionedcall_sequential_conv2d_1_biasadd_activation(), PADDING_VALID,{}, 1,1, "l3")),
l4(MaxPool2D<int16_t>({2,2},PADDING_VALID,{}, 2, 2, "l4")),
l5(Conv2D<int16_t>(-9, get_statefulpartitionedcall_sequential_conv2d_2_biasadd_filter(), get_statefulpartitionedcall_sequential_conv2d_2_biasadd_bias(), get_statefulpartitionedcall_sequential_conv2d_2_biasadd_activation(), PADDING_VALID,{}, 1,1, "l5")),
l6(MaxPool2D<int16_t>({2,2},PADDING_VALID,{}, 2, 2, "l6")),

                 l7(Conv2D<int16_t>(-9, get_statefulpartitionedcall_sequential_conv2d_3_biasadd_filter(), get_statefulpartitionedcall_sequential_conv2d_3_biasadd_bias(), get_statefulpartitionedcall_sequential_conv2d_3_biasadd_activation(), PADDING_VALID,{}, 1,1, "l7")),                       
                 l8(MaxPool2D<int16_t>({2,2},PADDING_VALID,{}, 2, 2, "l8")),                       
                 l9(Conv2D<int16_t>(-10, get_statefulpartitionedcall_sequential_conv2d_4_biasadd_filter(), get_statefulpartitionedcall_sequential_conv2d_4_biasadd_bias(), get_statefulpartitionedcall_sequential_conv2d_4_biasadd_activation(), PADDING_VALID,{}, 1,1, "l9")),                    
                 l10(MaxPool2D<int16_t>({2,2},PADDING_VALID,{}, 2, 2, "l10")),
                 					 
				 l11(Reshape<int16_t>({1,1,6400},"l11_reshape")),
                 
				  
				 l12(Conv2D<int16_t>(-11, get_fused_gemm_0_filter(), get_fused_gemm_0_bias(), get_fused_gemm_0_activation(), PADDING_VALID, {}, 1, 1, "l12")),
                 l13(Conv2D<int16_t>(-11, get_fused_gemm_1_filter(), get_fused_gemm_1_bias(), get_fused_gemm_1_activation(), PADDING_VALID, {}, 1, 1, "l13")),
				 l14(Conv2D<int16_t>(-11, get_fused_gemm_2_filter(), get_fused_gemm_2_bias(), get_fused_gemm_2_activation(), PADDING_VALID, {}, 1, 1, "l14")),
                 l15(Conv2D<int16_t>(-13, get_fused_gemm_3_filter(), get_fused_gemm_3_bias(), NULL, PADDING_VALID,{}, 1,1, "l15")),
                 
				 l16(Softmax<int16_t>(-15,"l16")){}

void build(Tensor<int16_t> &input)
{
this->l1.build(input);
this->l2.build(this->l1.get_output());
this->l3.build(this->l2.get_output());
this->l4.build(this->l3.get_output());
this->l5.build(this->l4.get_output());
this->l6.build(this->l5.get_output());
this->l7.build(this->l6.get_output());
this->l8.build(this->l7.get_output());
this->l9.build(this->l8.get_output());
this->l10.build(this->l9.get_output());
this->l11.build(this->l10.get_output());
this->l12.build(this->l11.get_output());
this->l13.build(this->l12.get_output());
this->l14.build(this->l13.get_output());
this->l15.build(this->l14.get_output());
this->l16.build(this->l15.get_output());
}
void call(Tensor<int16_t> &input)
{
this->l1.call(input);
input.free_element();

    this->l2.call(this->l1.get_output());
    this->l1.get_output().free_element();

    this->l3.call(this->l2.get_output());
    this->l2.get_output().free_element();

    this->l4.call(this->l3.get_output());
    this->l3.get_output().free_element();

    this->l5.call(this->l4.get_output());
    this->l4.get_output().free_element();

    this->l6.call(this->l5.get_output());
    this->l5.get_output().free_element();

    this->l7.call(this->l6.get_output());
    this->l6.get_output().free_element();

    this->l8.call(this->l7.get_output());
    this->l7.get_output().free_element();

    this->l9.call(this->l8.get_output());
    this->l8.get_output().free_element();

    this->l10.call(this->l9.get_output());
    this->l9.get_output().free_element();

    this->l11.call(this->l10.get_output());
    this->l10.get_output().free_element();

    this->l12.call(this->l11.get_output());
    this->l11.get_output().free_element();

    this->l13.call(this->l12.get_output());
    this->l12.get_output().free_element();

    this->l14.call(this->l13.get_output());
    this->l13.get_output().free_element();

    this->l15.call(this->l14.get_output());
    this->l14.get_output().free_element();
	
    this->l16.call(this->l15.get_output());
    this->l15.get_output().free_element();
}

};

----------This is my main

int input_height = 96;
int input_width = 96;
int input_channel = 3;
int input_exponent = -7;
int16_t model_input = (int16_t )dl::tool::malloc_aligned_prefer(input_heightinput_widthinput_channel, sizeof(int16_t *));
camera_fb_t *fbd = NULL;
static const char *TAG = "example";
static void Get(void *){
while(1){
if (xSemaphoreTake(xMutexInventory, SPINLOCK_WAIT_FOREVER) == pdPASS){
fbd = esp_camera_fb_get();
ESP_LOGI("esp", "Len:%d Wid:%d High:%d \n",fbd->len,fbd->width,fbd->height);
int id=0;
for (int i = 0; i < fbd->len; i +=2)
{

			// fbd->buf[i]=255-fbd->buf[i];
			// if(fbd->buf[i]<200)fbd->buf[i]=0;
			uint16_t input=(uint16_t)(fbd->buf[i]<<8)|(uint16_t)fbd->buf[i+1];
			uint16_t blue = (input & 0x1F00) >> 5;                           // blue
        	uint16_t green = ((input & 0x7) << 5) | ((input & 0xE000) >> 11); // green
        	uint16_t red = input & 0xF8;    
			model_input[id]=(int16_t)red;id++;
			model_input[id]=(int16_t)green;id++;
			model_input[id]=(int16_t)blue;id++;
			//printf("%d,",fbd->buf[i]);
		}
	// 	vTaskDelay(500 / portTICK_PERIOD_MS);
		Tensor<int16_t> input;
		//input.set_element((int16_t *)fb->buf).set_exponent(input_exponent).set_shape({input_height,input_width,input_channel}).set_auto_free(false);
		input.set_element(model_input).set_exponent(input_exponent).set_shape({input_height,input_width,3}).set_auto_free(false);
		// input.reshape({96,96,3});
		ESP_LOGI(TAG,"!%d",input.get_size());
		//input.print_all();
		HANDRECOGNITION model;
		dl::tool::Latency latency;
		latency.start();
		model.forward(input);
		latency.end();
		latency.print("\nSIGN", "forward");
		float *score = model.l16.get_output().get_element_ptr();
		float max_score = 0;
		int max_index = -1;
		for (size_t i = 0; i < 2; i++)
		{
			ESP_LOGI(TAG,"%f, ", score[i]*100);
			if (score[i] > max_score)
			{
				max_score = score[i];
				max_index = i;
			}
		}
		ESP_LOGI(TAG,"\n");

		switch (max_index)
		{
			case 0:
			ESP_LOGI(TAG,"Phone:0");
			break;
			case 1:
			ESP_LOGI(TAG,"No_Phone:1");
			break;
			default:
			ESP_LOGI(TAG,"No result");
		}
		ESP_LOGI(TAG,"\n");
		esp_camera_fb_return(fbd);
		xSemaphoreGive(xMutexInventory);
		fbd=NULL;
		vTaskDelay(5000 / portTICK_PERIOD_MS);
	}else{
		vTaskDelay(1000 / portTICK_PERIOD_MS);
	}
}

}
but there is a error :
assert failed: dl::Tensor& dl::Tensor::reshape(std::vector) [with T = short int] dl_variable.cpp:77 (size == size_gt)
Backtrace: 0x403760c6:0x3fceb880 0x4037c691:0x3fceb8a0 0x40382795:0x3fceb8c0 0x4201746b:0x3fceb9e0 0x4200cea6:0x3fceba10 0x42017db2:0x3fcebab0 0x4200b899:0x3fcebad0 0x4037f325:0x3fcebf20
ELF file SHA256: 768f2a452bcd0c8f
I don't know how to solve it , if you kown please tell me .
Thanks !

@github-actions github-actions bot changed the title I need help about “assert failed: dl::Tensor<T>& dl::Tensor<T>::reshape(std::vector<int>) [with T = short int] dl_variable.cpp:77 (size == size_gt)“ I need help about “assert failed: dl::Tensor<T>& dl::Tensor<T>::reshape(std::vector<int>) [with T = short int] dl_variable.cpp:77 (size == size_gt)“ (AIV-694) May 4, 2024
@100312dog
Copy link
Contributor

@emoof1
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━┓
┃ Layer (type) ┃ Output Shape ┃ Param # ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━┩
│ conv2d (Conv2D) │ (None, 92, 92, 32) │ 2,432 │
├─────────────────────────────────┼────────────────────────┼───────────────┤
│ max_pooling2d (MaxPooling2D) │ (None, 46, 46, 32) │ 0 │
├─────────────────────────────────┼────────────────────────┼───────────────┤
│ dropout (Dropout) │ (None, 46, 46, 32) │ 0 │
├─────────────────────────────────┼────────────────────────┼───────────────┤
│ conv2d_1 (Conv2D) │ (None, 44, 44, 64) │ 18,496 │
├─────────────────────────────────┼────────────────────────┼───────────────┤
│ max_pooling2d_1 (MaxPooling2D) │ (None, 22, 22, 64) │ 0 │
├─────────────────────────────────┼────────────────────────┼───────────────┤
│ dropout_1 (Dropout) │ (None, 22, 22, 64) │ 0 │
├─────────────────────────────────┼────────────────────────┼───────────────┤
│ conv2d_2 (Conv2D) │ (None, 20, 20, 64) │ 36,928 │
├─────────────────────────────────┼────────────────────────┼───────────────┤
│ max_pooling2d_2 (MaxPooling2D) │ (None, 10, 10, 64) │ 0 │
├─────────────────────────────────┼────────────────────────┼───────────────┤
│ dropout_2 (Dropout) │ (None, 10, 10, 64) │ 0 │
├─────────────────────────────────┼────────────────────────┼───────────────┤
│ conv2d_3 (Conv2D) │ (None, 8, 8, 64) │ 36,928 │
├─────────────────────────────────┼────────────────────────┼───────────────┤
│ max_pooling2d_3 (MaxPooling2D) │ (None, 4, 4, 64) │ 0 │
├─────────────────────────────────┼────────────────────────┼───────────────┤
│ dropout_3 (Dropout) │ (None, 4, 4, 64) │ 0 │
├─────────────────────────────────┼────────────────────────┼───────────────┤
│ conv2d_4 (Conv2D) │ (None, 2, 2, 64) │ 36,928 │
├─────────────────────────────────┼────────────────────────┼───────────────┤
│ max_pooling2d_4 (MaxPooling2D) │ (None, 1, 1, 64) │ 0 │
├─────────────────────────────────┼────────────────────────┼───────────────┤
│ flatten (Flatten) │ (None, 64) │ 0 │
├─────────────────────────────────┼────────────────────────┼───────────────┤
│ dense (Dense) │ (None, 128) │ 8,320 │
├─────────────────────────────────┼────────────────────────┼───────────────┤
│ dense_1 (Dense) │ (None, 64) │ 8,256 │
├─────────────────────────────────┼────────────────────────┼───────────────┤
│ dense_2 (Dense) │ (None, 32) │ 2,080 │
├─────────────────────────────────┼────────────────────────┼───────────────┤
│ dense_3 (Dense) │ (None, 2) │ 66 │
└─────────────────────────────────┴────────────────────────┴───────────────┘
use model.summary() to see output shape for each layer.
the reason why you encounter the assert failed error here is that you try to reshape a (1, 1, 64) shape tensor to (1, 1, 6400)
just use Flatten here.
btw, dense layers should use FullyConnected .

#include "dl_layer_fullyconnected.hpp"
#include "dl_layer_flatten.hpp"
Flatten<int16_t> l11;
FullyConnected<int16_t> l12;
FullyConnected<int16_t> l13;
FullyConnected<int16_t> l14;
FullyConnected<int16_t> l15;

@emoof1
Copy link
Author

emoof1 commented May 7, 2024

@100312dog By the way, I would like to know how to achieve targeting using ESP-DL do you have any good suggestions or references to articles?

@100312dog
Copy link
Contributor

@emoof1
see constructor of them in
https://github.com/espressif/esp-dl/blob/master/include/layer/dl_layer_fullyconnected.hpp
https://github.com/espressif/esp-dl/blob/master/include/layer/dl_layer_flatten.hpp

            /**
             * @brief Construct a new Flatten object
             * 
             * @param name name of layer
             * @param inplace true: the output will store to input0
             *                false: the output will store to a separate memory
             */
            Flatten(const char *name = "Flatten", bool inplace = false) : Layer(name), output(NULL), inplace(inplace), output_shape({})
            {}

            /**
             * @brief Construct a new FullyConnected object.
             * 
             * @param output_exponent exponent of output
             * @param filter          filter of FullyConnected
             * @param bias            bias of FullyConnected, if you don't specify anything, no bias is added
             * @param activation      activation of FullyConnected, if you don't specify anything, no activation is applied
             * @param flatten         true: input shape is [x1, x2, ..., xn], filter shape is [1, 1, x1 * x2 * ... * xn, output_dim], output shape is [output_dim]
                                      false: input shape is [x1, x2, ..., xn, input_dim], filter shape is [1, 1, input_dim, output_dim], output shape is [x1, x2, ...., xn, output_dim]
             * @param name            name of layer
             */
            FullyConnected(const int output_exponent,
                           const Filter<feature_t> *filter,
                           const Bias<bias_t> *bias = NULL,
                           const Activation<feature_t> *activation = NULL,
                           const bool flatten = true,
                           const char *name = "FullyConnected") : Layer(name),
                                                                  output_exponent(output_exponent),
                                                                  flatten(flatten),
                                                                  filter(filter),
                                                                  bias(bias),
                                                                  activation(activation),
                                                                  output_shape({})

according your code provided, the usage should be

l11(Flatten<int16_t>("l11", false)),
l12(FullyConnected<int16_t>(-11, get_fused_gemm_0_filter(), get_fused_gemm_0_bias(), get_fused_gemm_0_activation(), true, "l12")),
l13(FullyConnected<int16_t>(-11, get_fused_gemm_1_filter(), get_fused_gemm_1_bias(), get_fused_gemm_1_activation(), true, "l13")),
l14(FullyConnected<int16_t>(-11, get_fused_gemm_2_filter(), get_fused_gemm_2_bias(), get_fused_gemm_2_activation(), true, "l14")),
l15(FullyConnected<int16_t>(-13, get_fused_gemm_3_filter(), get_fused_gemm_3_bias(), true, "l15")),

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants