{
  "id": 171660,
  "title": "tf.Keras not using GPU",
  "url": "/competitions/birdsong-recognition/discussion/171660",
  "author_name": "",
  "post_date": "2020-08-01T23:14:25.773407300Z",
  "votes": 1,
  "comment_count": 3,
  "views": 0,
  "content": "<p>I'm working on a model of resnet50 with imagenet weights to train my model. While running model.fit my CPU usage is more than 100% while GPU usage is 0%. Each of my epochs is taking more than 6 hrs to train.</p>\n\n<p>Is there any code addition that I have to do to use GPU? Please find my model code below</p>\n\n<p>`</p>\n\n<pre><code>base_model = tf.keras.applications.ResNet50(\n    include_top = False,\n    weights='imagenet'\n)\nprint(f'num layers in base model: {len(base_model.layers)}')\nbase_model.trainable = DO_FINE_TUNING\n\nfor layer in base_model.layers[:FINE_TUNE_AT]:\n    layer.trainable = False\n\nmodel = tf.keras.Sequential([\n    tf.keras.layers.InputLayer(input_shape=self.img_size),\n    tf.keras.layers.Conv2D(filters=3, kernel_size=(3, 3), padding='same'),\n    base_model,\n    tf.keras.layers.GlobalAveragePooling2D(),\n    tf.keras.layers.Dense(units = self.num_classes, activation=tf.nn.softmax)\n])\n\nmodel.compile(optimizer=OPTIMIZER, loss=LOSS_FN, metrics=METRICS_LIST)\n</code></pre>\n\n<p>`</p>\n\n<p>if you require any more details on this, please feel free to ask for it.</p>",
  "messages": [
    {
      "id": "954621",
      "postDate": "08/01/2020 23:14:25",
      "content": "<p>I'm working on a model of resnet50 with imagenet weights to train my model. While running model.fit my CPU usage is more than 100% while GPU usage is 0%. Each of my epochs is taking more than 6 hrs to train.</p>\n\n<p>Is there any code addition that I have to do to use GPU? Please find my model code below</p>\n\n<p>`</p>\n\n<pre><code>base_model = tf.keras.applications.ResNet50(\n    include_top = False,\n    weights='imagenet'\n)\nprint(f'num layers in base model: {len(base_model.layers)}')\nbase_model.trainable = DO_FINE_TUNING\n\nfor layer in base_model.layers[:FINE_TUNE_AT]:\n    layer.trainable = False\n\nmodel = tf.keras.Sequential([\n    tf.keras.layers.InputLayer(input_shape=self.img_size),\n    tf.keras.layers.Conv2D(filters=3, kernel_size=(3, 3), padding='same'),\n    base_model,\n    tf.keras.layers.GlobalAveragePooling2D(),\n    tf.keras.layers.Dense(units = self.num_classes, activation=tf.nn.softmax)\n])\n\nmodel.compile(optimizer=OPTIMIZER, loss=LOSS_FN, metrics=METRICS_LIST)\n</code></pre>\n\n<p>`</p>\n\n<p>if you require any more details on this, please feel free to ask for it.</p>",
      "rawMarkdown": "I'm working on a model of resnet50 with imagenet weights to train my model. While running model.fit my CPU usage is more than 100% while GPU usage is 0%. Each of my epochs is taking more than 6 hrs to train.\n\nIs there any code addition that I have to do to use GPU? Please find my model code below\n\n`\n\n    base_model = tf.keras.applications.ResNet50(\n        include_top = False,\n        weights='imagenet'\n    )\n    print(f'num layers in base model: {len(base_model.layers)}')\n    base_model.trainable = DO_FINE_TUNING\n\n    for layer in base_model.layers[:FINE_TUNE_AT]:\n        layer.trainable = False\n\n    model = tf.keras.Sequential([\n        tf.keras.layers.InputLayer(input_shape=self.img_size),\n        tf.keras.layers.Conv2D(filters=3, kernel_size=(3, 3), padding='same'),\n        base_model,\n        tf.keras.layers.GlobalAveragePooling2D(),\n        tf.keras.layers.Dense(units = self.num_classes, activation=tf.nn.softmax)\n    ])\n\n    model.compile(optimizer=OPTIMIZER, loss=LOSS_FN, metrics=METRICS_LIST)\n\n`\n\nif you require any more details on this, please feel free to ask for it.",
      "votes": null
    },
    {
      "id": "955110",
      "postDate": "08/02/2020 10:54:29",
      "content": "<p>Have you finished by any chance you weekly GPU quota?</p>",
      "rawMarkdown": "Have you finished by any chance you weekly GPU quota?",
      "votes": null
    },
    {
      "id": "955303",
      "postDate": "08/02/2020 14:36:26",
      "content": "<p>I still have 29 hours left on my clock.</p>",
      "rawMarkdown": "I still have 29 hours left on my clock.",
      "votes": null
    },
    {
      "id": "956298",
      "postDate": "08/03/2020 11:52:17",
      "content": "<p>I found some syntax by searching. </p>\n\n<p>(1)\nfrom tensorflow.python.client import device_lib</p>\n\n<p>print(device_lib.list_local_devices())</p>\n\n<p>import keras.backend.tensorflow_backend as K</p>\n\n<p>with K.tf.device('/gpu:0'):\nmodel = </p>\n\n<p>(2)\ngpus = tf.config.experimental.list_logical_devices('GPU')\nlen(gpus) &gt; 1: strategy = tf.distribute.MirroredStrategy([gpu.name for gpu in gpus])\nlen(gpus) &lt;= 1: strategy = tf.distribute.get_strategy()</p>\n\n<p>with strategy.scope():\nmodel = \nmodel.compile()</p>",
      "rawMarkdown": "I found some syntax by searching. \n\n(1)\nfrom tensorflow.python.client import device_lib\n\nprint(device_lib.list_local_devices())\n\nimport keras.backend.tensorflow_backend as K\n\nwith K.tf.device('/gpu:0'):\nmodel = \n\n(2)\ngpus = tf.config.experimental.list_logical_devices('GPU')\nlen(gpus) &gt; 1: strategy = tf.distribute.MirroredStrategy([gpu.name for gpu in gpus])\nlen(gpus) &lt;= 1: strategy = tf.distribute.get_strategy()\n\nwith strategy.scope():\nmodel = \nmodel.compile()",
      "votes": null
    }
  ],
  "comments": [
    {
      "id": 955110,
      "author_name": "pranavkasela",
      "author_url": "",
      "post_date": "08/02/2020 10:54:29",
      "content": "<p>Have you finished by any chance you weekly GPU quota?</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 955303,
      "author_name": "haldarankit",
      "author_url": "",
      "post_date": "08/02/2020 14:36:26",
      "content": "<p>I still have 29 hours left on my clock.</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 956298,
      "author_name": "woosungyoon",
      "author_url": "",
      "post_date": "08/03/2020 11:52:17",
      "content": "<p>I found some syntax by searching. </p>\n\n<p>(1)\nfrom tensorflow.python.client import device_lib</p>\n\n<p>print(device_lib.list_local_devices())</p>\n\n<p>import keras.backend.tensorflow_backend as K</p>\n\n<p>with K.tf.device('/gpu:0'):\nmodel = </p>\n\n<p>(2)\ngpus = tf.config.experimental.list_logical_devices('GPU')\nlen(gpus) &gt; 1: strategy = tf.distribute.MirroredStrategy([gpu.name for gpu in gpus])\nlen(gpus) &lt;= 1: strategy = tf.distribute.get_strategy()</p>\n\n<p>with strategy.scope():\nmodel = \nmodel.compile()</p>",
      "votes": null,
      "replies": []
    }
  ],
  "raw_markdown_by_id": {
    "954621": "I'm working on a model of resnet50 with imagenet weights to train my model. While running model.fit my CPU usage is more than 100% while GPU usage is 0%. Each of my epochs is taking more than 6 hrs to train.\n\nIs there any code addition that I have to do to use GPU? Please find my model code below\n\n`\n\n    base_model = tf.keras.applications.ResNet50(\n        include_top = False,\n        weights='imagenet'\n    )\n    print(f'num layers in base model: {len(base_model.layers)}')\n    base_model.trainable = DO_FINE_TUNING\n\n    for layer in base_model.layers[:FINE_TUNE_AT]:\n        layer.trainable = False\n\n    model = tf.keras.Sequential([\n        tf.keras.layers.InputLayer(input_shape=self.img_size),\n        tf.keras.layers.Conv2D(filters=3, kernel_size=(3, 3), padding='same'),\n        base_model,\n        tf.keras.layers.GlobalAveragePooling2D(),\n        tf.keras.layers.Dense(units = self.num_classes, activation=tf.nn.softmax)\n    ])\n\n    model.compile(optimizer=OPTIMIZER, loss=LOSS_FN, metrics=METRICS_LIST)\n\n`\n\nif you require any more details on this, please feel free to ask for it.",
    "955110": "Have you finished by any chance you weekly GPU quota?",
    "955303": "I still have 29 hours left on my clock.",
    "956298": "I found some syntax by searching. \n\n(1)\nfrom tensorflow.python.client import device_lib\n\nprint(device_lib.list_local_devices())\n\nimport keras.backend.tensorflow_backend as K\n\nwith K.tf.device('/gpu:0'):\nmodel = \n\n(2)\ngpus = tf.config.experimental.list_logical_devices('GPU')\nlen(gpus) &gt; 1: strategy = tf.distribute.MirroredStrategy([gpu.name for gpu in gpus])\nlen(gpus) &lt;= 1: strategy = tf.distribute.get_strategy()\n\nwith strategy.scope():\nmodel = \nmodel.compile()"
  },
  "source": "meta"
}