experiencor / keras-yolo2

Easy training on custom dataset. Various backends (MobileNet and SqueezeNet) supported. A YOLO demo to detect raccoon run entirely in brower is accessible at https://git.io/vF7vI (not on Windows).
MIT License
1.73k stars 784 forks source link

question about mobilenet backend #116

Closed kaishijeng closed 6 years ago

kaishijeng commented 6 years ago

@experiencor

Does current code support different values of Alpha and Depth_Multiplier in Mobilenet?

def MobileNet(input_shape=None, alpha=1.0, depth_multiplier=1, dropout=1e-3, include_top=True, weights='imagenet', input_tensor=None, pooling=None, classes=1000):

Thanks,

experiencor commented 6 years ago

@kaishijeng Yes, it does. You can make the changes you want to the backend.py.

kaishijeng commented 6 years ago

I added alpha value to MobileNet in the backgroud below. But it encounters an error due to no corresponding backend weight for alpha=0.25. Where can I get "mobilenet_backend.h5" for alpha =0.25?

class MobileNetFeature(BaseFeatureExtractor): """docstring for ClassName""" def init(self, input_size): input_image = Input(shape=(input_size, input_size, 3))

    mobilenet = MobileNet(input_shape=(224,224,3), alpha=0.25, include_top=False)
    mobilenet.load_weights(MOBILENET_BACKEND_PATH)

Error: Traceback (most recent call last): File "train.py", line 140, in main(args) File "train.py", line 109, in main anchors = config['model']['anchors']) File "/home/fc/2TB/src/basic-yolo-keras/frontend.py", line 46, in init self.feature_extractor = MobileNetFeature(self.input_size) File "/home/fc/2TB/src/basic-yolo-keras/backend.py", line 214, in init mobilenet.load_weights(MOBILENET_BACKEND_PATH) File "/usr/local/lib/python2.7/dist-packages/keras/engine/topology.py", line 2622, in load_weights load_weights_from_hdf5_group(f, self.layers) File "/usr/local/lib/python2.7/dist-packages/keras/engine/topology.py", line 3143, in load_weights_from_hdf5_group K.batch_set_value(weight_value_tuples) File "/usr/local/lib/python2.7/dist-packages/keras/backend/tensorflow_backend.py", line 2252, in batch_set_value get_session().run(assign_ops, feed_dict=feed_dict) File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 889, in run run_metadata_ptr) File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 1096, in _run % (np_val.shape, subfeed_t.name, str(subfeed_t.get_shape()))) ValueError: Cannot feed value of shape (128,) for Tensor u'Placeholder_32:0', which has shape '(32,)'

experiencor commented 6 years ago

@kaishijeng Unfortunately, I don't the weights for alpha=0.25. You can train it on coco 2014 (cocodataset.org/#detections-challenge2017).

kaishijeng commented 6 years ago

@experiencor

Can I use pretrained mobilenet models from Keras (see link below)? https://keras.rstudio.com/articles/applications.html

If not, why? How can I train myself?

Also I compared your pretained mobilenet_backend model with Keras mobilenet alpha=1.0 pretained model and find both file sizes are different.

Thanks,

experiencor commented 6 years ago

Yes, you should start with Keras pre-trained models. The steps: Keras pre-trained model (no top) => train on COCO (then keep only the backend) => train on your custom dataset. It's a quite simple if you know how to manipulate the weights of different layers in Keras.

They are different in sizes because the mobilenet_backend does not include the top layer as in the Keras model.

kaishijeng commented 6 years ago

What do you mean "then keep only the backend" and how I do it?

Thanks

On Mon, Jan 8, 2018 at 4:44 AM, Huynh Ngoc Anh notifications@github.com wrote:

Yes, you should start with Keras pre-trained models. The steps: Keras pre-trained model (no top) => train on COCO (then keep only the backend) => train on your custom dataset. It's a quite simple if you know how to manipulate the weights of different layers in Keras.

They are different in sizes because the mobilenet_backend does not include the top layer as in the Keras model.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/experiencor/basic-yolo-keras/issues/116#issuecomment-355956886, or mute the thread https://github.com/notifications/unsubscribe-auth/AMGg3s19wHp2HiTx4neXLZ05NsR4Pbinks5tIg2WgaJpZM4RVF0Q .

experiencor commented 6 years ago

@kaishijeng Object detection model has backend part and frontend part. The backend is the same for all input image sizes and all number of classes. The size of the frontend depends on the number of classess. After you train on COCO, the frontend is for 80 classess. You need to train a new frontend if your custom data does not have 80 classes. In this case, you need to save the weight of the backend trained on COCO and use it on your dataset.