2

TensorFlow.js version

tensorflow/tfjs-core@0.15.2

Browser version

chrome 72.0.3626.109 for mac

Describe the problem or feature request

I tried the demo tfjs-examples-webcam-transfer-learning in tensorflow/tfjs-examples. In the file index.js, tensorflow tried to load the model like below:

  const mobilenet = await tf.loadModel(
      'https://storage.googleapis.com/tfjs-models/tfjs/mobilenet_v1_0.25_224/model.json'
      );

But every time I run this demo, it will run this sentence and download the model. I want to make it load from local server. So I download the model.json and put it in the assets folder. Then I deploy the assets in the local server at port 1234. load it like this:

const mobilenet = await tf.loadModel(
      'http://localhost:1234/json/model.json'
      );

But it didn't work and the consoled out

io_utils.ts:116 Uncaught (in promise) RangeError: byte length of Float32Array should be a multiple of 4
    at new Float32Array (<anonymous>)
    at o (io_utils.ts:116)
    at Object.decodeWeights (io_utils.ts:79)
    at models.ts:318
    at common.ts:14
    at Object.next (common.ts:14)
    at o (common.ts:14)

I'm really sure that the model.json is the same as in https://storage.googleapis.com/tfjs-models/tfjs/mobilenet_v1_0.25_224/model.json, but why id didn't work?

Thanks for helping me.

Jun Woo
  • 21
  • 3
  • You can look in your browser network requests to see the file names it is trying to pull (I am using AutoML tensor flow library). – hvaughan3 Jul 12 '21 at 19:22

2 Answers2

0

You have to serve the file on a local server for the browser cannot directly access the filesystem. The same question has been asked here and there

edkeveked
  • 17,989
  • 10
  • 55
  • 93
  • I tried but get the same error. Thanks all the same. – Jun Woo Feb 24 '19 at 00:29
  • @JunWoo Make sure you're loading the correct path of the file. That is the root cause of the error – edkeveked Feb 24 '19 at 00:31
  • I'm sure I have loaded the correct path of the file. In the network panel of chrome, I saw the state of request for http://localhost:1234/json/model.json is 200 and the content-length of the response is 54134, the same size as form https://storage.googleapis.com/tfjs-models/tfjs/mobilenet_v1_0.25_224/model.json. So I think it's not the reason that I hadn't load the correct path of the file. – Jun Woo Feb 24 '19 at 00:58
  • Thanks for your help.I think the problem I met is not the same as you searched for me. I tried the method but still failed. See my update question. – Jun Woo Feb 24 '19 at 01:55
0

This should really not be a concern, because your browser will cache the downloaded model together with all of its weights. So you can just use the public URL. Any access after the first time will be served out of the browser cache, not really downloading again.


If you really want to serve locally anyway, or at least to understand why it didn't work before:

The model.json file is not the whole story-- it contains a weightsManifest section with relative paths to the weights files. When you load the model.json from the original URL, it resolves and downloads all of the neighboring weights files. So, to serve the model locally, you would need to serve all of those files as well.

It's unfortunately not entirely straightforward to get a list of the weight files, since Google Cloud Storage doesn't provide directory listings in an easily accessible way. You can of course fish the filenames out of the model.json (look for things like "paths": ["group47-shard1of1"]. A second option is to use gsutil from the command line, which should let you just download the whole folder. A third option is to use the REST API to list the directory contents: https://www.googleapis.com/storage/v1/b/tfjs-models/o?prefix=tfjs/mobilenet_v1_0.25_224, and look for the "name" entries there.

The fourth and easiest option is to construct the URLs from scratch: in this case there are 55 weight groups, so the URLs range from https://storage.googleapis.com/tfjs-models/tfjs/mobilenet_v1_0.25_224/group1-shard1of1 to https://storage.googleapis.com/tfjs-models/tfjs/mobilenet_v1_0.25_224/group55-shard1of1. None of the groups have more than one shard.

David Soergel
  • 1,709
  • 1
  • 11
  • 15