lucid
lucid copied to clipboard
Svelte fails with numpy int64
For now, I'm just copying the code from this notebook. I get an error when generating the visualization.
python: 3.6.8 lucid: 0.3.8 svelte-cli:2.2.0 svelte:1.64.1
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-5-610cffcb889a> in <module>
----> 1 googlenet_semantic_dict("mixed4d", "https://storage.googleapis.com/lucid-static/building-blocks/examples/dog_cat.png")
<ipython-input-4-a5ba9ff51168> in googlenet_semantic_dict(layer, img_url)
28 "image_url": _image_url(img),
29 "activations": [[[{"n": n, "v": float(act_vec[n])} for n in np.argsort(-act_vec)[:4]] for act_vec in act_slice] for act_slice in acts],
---> 30 "pos" : [max_y, max_x]
31 })
~/miniconda3/envs/tensorflow_p36/lib/python3.6/site-packages/lucid/scratch/web/svelte.py in inner(data)
63 .replace("$js", js_content) \
64 .replace("$name", name) \
---> 65 .replace("$data", json.dumps(data)) \
66 .replace("$id", id_str)
67 _display_html(html)
~/miniconda3/envs/tensorflow_p36/lib/python3.6/json/__init__.py in dumps(obj, skipkeys, ensure_ascii, check_circular, allow_nan, cls, indent, separators, default, sort_keys, **kw)
229 cls is None and indent is None and separators is None and
230 default is None and not sort_keys and not kw):
--> 231 return _default_encoder.encode(obj)
232 if cls is None:
233 cls = JSONEncoder
~/miniconda3/envs/tensorflow_p36/lib/python3.6/json/encoder.py in encode(self, o)
197 # exceptions aren't as detailed. The list call should be roughly
198 # equivalent to the PySequence_Fast that ''.join() would do.
--> 199 chunks = self.iterencode(o, _one_shot=True)
200 if not isinstance(chunks, (list, tuple)):
201 chunks = list(chunks)
~/miniconda3/envs/tensorflow_p36/lib/python3.6/json/encoder.py in iterencode(self, o, _one_shot)
255 self.key_separator, self.item_separator, self.sort_keys,
256 self.skipkeys, _one_shot)
--> 257 return _iterencode(o, 0)
258
259 def _make_iterencode(markers, _default, _encoder, _indent, _floatstr,
~/miniconda3/envs/tensorflow_p36/lib/python3.6/json/encoder.py in default(self, o)
178 """
179 raise TypeError("Object of type '%s' is not JSON serializable" %
--> 180 o.__class__.__name__)
181
182 def encode(self, o):
TypeError: Object of type 'int64' is not JSON serializable
The fix is to simply handle the numpy int64 separately.
def json_default(o):
if isinstance(o, np.int64):
return int(o)
raise TypeError
# Some other codes
def SvelteComponent(name, path):
"""Display svelte components in iPython.
Args:
name: name of svelte component (must match component filename when built)
path: path to compile svelte .js file or source svelte .html file.
(If html file, we try to call svelte and build the file.)
Returns:
A function mapping data to a rendered svelte component in ipython.
"""
if path[-3:] == ".js":
js_path = path
elif path[-5:] == ".html":
print("Trying to build svelte component from html...")
js_path = build_svelte(path)
js_content = read(js_path, mode='r')
def inner(data):
id_str = js_id(name)
html = _template \
.replace("$js", js_content) \
.replace("$name", name) \
.replace("$data", json.dumps(data, default=json_default)) \
.replace("$id", id_str)
_display_html(html)
return inner