keras2ncnn icon indicating copy to clipboard operation
keras2ncnn copied to clipboard

Mobilenetv3 Keras error

Open xellDart opened this issue 2 years ago • 6 comments

Hi, I try to convert model to nccn using Mobilenetv3Small, but I get this error

ERROR] Operator Rescaling not support.

{'name': 'rescaling', 'trainable': False, 'dtype': 'float32', 'scale': 0.00784313725490196, 'offset': -1.0}

Mi training code:

IMG_SIZE = 320 # All images will be resized to 160x160

def format_example(pair):
  image, label = pair['image'], pair['label']
  image = tf.cast(image, tf.float32)
  image = (image/127.5) - 1
  image = tf.image.resize(image, (IMG_SIZE, IMG_SIZE))
  return image, label
IMG_SHAPE = (IMG_SIZE, IMG_SIZE, 3)

# Create the base model from the pre-trained model MobileNet V2
base_model = tf.keras.applications.MobileNetV3Small(input_shape=IMG_SHAPE,
                                               include_top=False,
                                               weights='imagenet')

global_average_layer = tf.keras.layers.GlobalAveragePooling2D()
feature_batch_average = global_average_layer(feature_batch)

prediction_layer = tf.keras.layers.Dense(1)
prediction_batch = prediction_layer(feature_batch_average)

model = tf.keras.Sequential([
  base_model,
  global_average_layer,
  prediction_layer
])

base_learning_rate = 0.0001
model.compile(optimizer=tf.keras.optimizers.RMSprop(learning_rate=base_learning_rate),
              loss=tf.keras.losses.BinaryCrossentropy(from_logits=True),
              metrics=['accuracy'])

Model summary

Model: "sequential"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 MobilenetV3small (Functiona  (None, 13, 13, 576)      939120    
 l)                                                              
                                                                 
 global_average_pooling2d (G  (None, 576)              0         
 lobalAveragePooling2D)                                          
                                                                 
 dense (Dense)               (None, 1)                 577       
                                                                 
=================================================================
Total params: 939,697
Trainable params: 577
Non-trainable params: 939,120
_________________________________________________________________

model.compile(loss=tf.keras.losses.BinaryCrossentropy(from_logits=True),
              optimizer = tf.keras.optimizers.RMSprop(learning_rate=base_learning_rate/10),
              metrics=['accuracy'])

Another question, what is the correct mean - norm values for this model?

Thanks

xellDart avatar Feb 28 '22 17:02 xellDart

Let me see whether I can support that op. Can you attach your model h5df here? For mean and norm, it should be 128 and 1/127.5

MarsTechHAN avatar Mar 02 '22 09:03 MarsTechHAN

Thanks for your help..

This is the model: Keras model

I already train with same configuration for MobileNetv2,

tf.keras.applications.MobileNetV2(..) and with this version, all work !

Many thanks for your help and work!

xellDart avatar Mar 02 '22 16:03 xellDart

你看到你的模型输入形状是[N,H,W,C]; 但是ncnn默认支持的是[N,C,H,W]. 当使用ncnn推理时,你是如何解决输入形状的转换问题的?

superbayes avatar May 07 '22 01:05 superbayes

You can check the code, we do transpose on weight itself directly.

MarsTechHAN avatar May 26 '22 02:05 MarsTechHAN

Thanks for your help..

This is the model: Keras model

I already train with same configuration for MobileNetv2,

tf.keras.applications.MobileNetV2(..) and with this version, all work !

Many thanks for your help and work!

Sorry I got so busy recently. I will try to get it support soon. :p

MarsTechHAN avatar May 26 '22 02:05 MarsTechHAN

你看到你的模型输入形状是[N,H,W,C]; 但是ncnn默认支持的是[N,C,H,W]. 当使用ncnn推理时,你是如何解决输入形状的转换问题的?

你好,请问这个对模型的输出有影响吗?因为我看了ncnn mat是nchw的格式但是好像转出来的模型的输入层上显示依旧是nhwc,但是下面的其他层好像已经变成nchw了,我的模型现在无法输出正确结果issue,请问会和这个有关系吗?

Sherlock-hh avatar Sep 19 '23 02:09 Sherlock-hh