This commit is contained in:
Irene-Maxine
2025-07-22 18:20:05 +08:00
9 changed files with 1212 additions and 1 deletions

View File

@@ -1,3 +1,6 @@
import NavExt from './nav-ext';
import * as tf from '@tensorflow/tfjs';
import './tensorflow';
NavExt.init();
NavExt.init();
window.tf = tf;

View File

@@ -0,0 +1,58 @@
import { Registry } from 'mixly';
import * as tf from '@tensorflow/tfjs';
const modelsValueRegistry = new Registry();
const customFetch = function (path) {
let result = {
ok: false,
buffer: null,
json: function () {
const decoder = new TextDecoder('utf-8');
const jsonText = decoder.decode(this.buffer);
return JSON.parse(jsonText);
},
arrayBuffer: function () {
return this.buffer;
}
}
if (!modelsValueRegistry.hasKey(path)) {
return result;
}
result.ok = true;
result.buffer = modelsValueRegistry.getItem(path);
return result;
};
const tensorflow = {};
tensorflow.modelsValue = {};
tensorflow.loadGraphModel = async function (path) {
const model = await tf.loadGraphModel(path, {
fromTFHub: false,
fetchFunc: (...args) => {
return customFetch(args[0]);
}
});
return model;
};
tensorflow.loadLayersModel = async function (path) {
const model = await tf.loadLayersModel(path, {
fromTFHub: false,
fetchFunc: (...args) => {
return customFetch(args[0]);
}
});
return model;
};
tensorflow.setModelsValue = function (path, value) {
if (modelsValueRegistry.hasKey(path)) {
modelsValueRegistry.unregister(path);
}
modelsValueRegistry.register(path, value);
tensorflow.modelsValue[path] = value;
};
window.tensorflow = tensorflow;

File diff suppressed because it is too large Load Diff

View File

@@ -20,6 +20,7 @@
},
"dependencies": {
"@basthon/kernel-loader": "^0.62.21",
"@tensorflow/tfjs": "^4.22.0",
"@zenfs/core": "^1.4.0",
"@zenfs/dom": "^1.0.6",
"idb-keyval": "^6.2.1",

View File

@@ -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',
]
)

View File

@@ -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

View File

@@ -0,0 +1 @@
from activation import *

View File

@@ -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)