Search code examples
tensorflowsupervisordtransfer-learning

Tensorflow use pretrained model in offline


I'm using mobilenet from Tensorflow with below code. When I run this code from my local pc, it downloads the weight file. However, I can not download this online when is uploaded to server.

  1. Is there any way I can use MobileNetV2 with tensorflow 2.0.

  2. Or, I have my own weight file trained with MobielNetV2, then is it possible to use below without weights option

    mobilenet = tf.keras.applications.mobilenet_v2.MobileNetV2(input_shape=(224, 224, 3), include_top=False, weights='imagenet')

  3. And I want to use supervisord from linux server. It seems I need to use absolute path not relative path. Could anyone help me to use MobileNetV2 with supervisord in offline environment


Solution

  • You could first use:

    mobilenet = tf.keras.applications.mobilenet_v2.MobileNetV2(input_shape=(224, 224, 3), include_top=False, weights='imagenet')
    

    Then save the weights somewhere:

    mobilenet.save_weights('somefolder/mobilenetweights.h5')
    

    Then when you're offline, you could first call:

    mobilenet = tf.keras.applications.mobilenet_v2.MobileNetV2(input_shape=(224, 224, 3), include_top=False, weights=None)
    

    Notice the None in weights argument. After that, you could load the weights from your file, where you saved previously:

    mobilenet.load_weights('somefolder/mobilenetweights.h5')
    

    This should work.

    When you call the mobilenet model, if you ask it to give you imagenet weights by using weights='imagenet', it requires internet connection to download those weights. So it won't work offline. The method explained here should get it working.