feat(board): python_pyodide下添加对tensorflow的支持 (待完善)
This commit is contained in:
@@ -0,0 +1,14 @@
|
||||
from setuptools import setup, find_packages
|
||||
|
||||
setup(
|
||||
name='tensorflow',
|
||||
version='0.0.1',
|
||||
packages=find_packages(),
|
||||
install_requires=[],
|
||||
author='Mixly Team',
|
||||
author_email='',
|
||||
description='适用于pyodide的tensorflowjs包',
|
||||
classifiers=[
|
||||
'Programming Language :: Python :: 3',
|
||||
]
|
||||
)
|
||||
@@ -0,0 +1,30 @@
|
||||
from pyodide.ffi import to_js, create_proxy
|
||||
import js
|
||||
import json
|
||||
import os
|
||||
|
||||
|
||||
def __cache_model(file_path):
|
||||
data = json.load(open(file_path, 'r'))
|
||||
f = open(file_path, 'rb')
|
||||
js.tensorflow.setModelsValue(file_path, to_js(f.read()))
|
||||
f.close()
|
||||
folder_path = os.path.dirname(file_path)
|
||||
for item in data['weightsManifest']:
|
||||
for current_path in item['paths']:
|
||||
bin_file_path = '{}/{}'.format(folder_path, current_path)
|
||||
f = open(bin_file_path, 'rb')
|
||||
js.tensorflow.setModelsValue(bin_file_path, to_js(f.read()))
|
||||
f.close()
|
||||
|
||||
|
||||
async def load_graph_model(file_path):
|
||||
__cache_model(file_path)
|
||||
model = await js.tensorflow.loadGraphModel(file_path)
|
||||
return model
|
||||
|
||||
|
||||
async def load_layers_model(file_path):
|
||||
__cache_model(file_path)
|
||||
model = await js.tensorflow.loadLayersModel(file_path)
|
||||
return model
|
||||
@@ -0,0 +1 @@
|
||||
from activation import *
|
||||
@@ -0,0 +1,37 @@
|
||||
import js
|
||||
|
||||
|
||||
def elu(*args, **kwargs):
|
||||
'''
|
||||
f(x) = alpha * (exp(x) - 1.) for x < 0, f(x) = x for x >= 0.
|
||||
'''
|
||||
js.tensorflow.layers.elu(*args, **kwargs)
|
||||
|
||||
|
||||
def leaky_relu(*args, **kwargs):
|
||||
'''
|
||||
f(x) = alpha * x for x < 0. f(x) = x for x >= 0.
|
||||
'''
|
||||
js.tensorflow.layers.leakyReLU(*args, **kwargs)
|
||||
|
||||
|
||||
def prelu(*args, **kwargs):
|
||||
'''
|
||||
f(x) = alpha * x for x < 0. f(x) = x for x >= 0.
|
||||
'''
|
||||
js.tensorflow.layers.prelu(*args, **kwargs)
|
||||
|
||||
|
||||
def relu(*args, **kwargs):
|
||||
js.tensorflow.layers.relu(*args, **kwargs)
|
||||
|
||||
|
||||
def softmax(*args, **kwargs):
|
||||
js.tensorflow.layers.softmax(*args, **kwargs)
|
||||
|
||||
|
||||
def thresholded_relu(*args, **kwargs):
|
||||
'''
|
||||
f(x) = x for x > theta, f(x) = 0 otherwise.
|
||||
'''
|
||||
js.tensorflow.layers.thresholdedReLU(*args, **kwargs)
|
||||
Reference in New Issue
Block a user