coomdev
2 years ago
11 changed files with 16212 additions and 1644 deletions
File diff suppressed because one or more lines are too long
@ -1 +1 @@ |
|||||
{"addons":{"{[email protected]}":{"updates":[{"version":"0.25","update_link":"https://git.coom.tech/araragi/JKCS/raw/branch/master/jkcs-0.25.xpi"}]}}} |
{"addons":{"{[email protected]}":{"updates":[{"version":"0.27","update_link":"https://git.coom.tech/araragi/JKCS/raw/branch/master/jkcs-0.27.xpi"}]}}} |
@ -0,0 +1,3 @@ |
|||||
|
version https://git-lfs.github.com/spec/v1 |
||||
|
oid sha256:ce3dd88fb5afe1b809555fbb01b2db2f05435cbc3378d1c03d2c01037f903f46 |
||||
|
size 9886879 |
File diff suppressed because it is too large
@ -2,19 +2,46 @@ import * as tf from '@tensorflow/tfjs' |
|||||
import { setWasmPaths } from '@tensorflow/tfjs-backend-wasm' |
import { setWasmPaths } from '@tensorflow/tfjs-backend-wasm' |
||||
import charsetJSON from './charset.json' |
import charsetJSON from './charset.json' |
||||
import modelJSON from './model.json' |
import modelJSON from './model.json' |
||||
import weightsData from './model.weights.bin' |
|
||||
|
|
||||
|
let weightsData |
||||
let model |
let model |
||||
|
|
||||
tf.enableProdMode() |
tf.enableProdMode() |
||||
// tf.enableDebugMode()
|
|
||||
setWasmPaths({ |
const wasmToUrl = wasm => { |
||||
'tfjs-backend-wasm.wasm': 'https://unpkg.com/@tensorflow/[email protected]/dist/tfjs-backend-wasm.wasm', |
const blb = new Blob([tfwasm]) |
||||
'tfjs-backend-wasm-simd.wasm': 'https://unpkg.com/@tensorflow/[email protected]/dist/tfjs-backend-wasm-simd.wasm', |
return URL.createObjectURL(blb) |
||||
'tfjs-backend-wasm-threaded-simd.wasm': 'https://unpkg.com/@tensorflow/[email protected]/dist/tfjs-backend-wasm-threaded-simd.wasm' |
} |
||||
}) // TODO: Figure out how the fuck to load the WASM modules locally from the extension.
|
|
||||
// setWasmPaths('https://unpkg.com/@tensorflow/[email protected]/dist/', true)
|
const backendloaded = (async () => { |
||||
tf.setBackend('wasm') |
try { |
||||
|
// dead code elimination should occur here
|
||||
|
if (execution_mode === 'userscript') { |
||||
|
weightsData = import('./model.weights.bin') |
||||
|
|
||||
|
const tfwasmthreadedsimd = await import('./tfjs-backend-wasm-threaded-simd.wasm') |
||||
|
const tfwasmsimd = await import('./tfjs-backend-wasm-simd.wasm') |
||||
|
const tfwasm = await import('./tfjs-backend-wasm.wasm') |
||||
|
setWasmPaths({ |
||||
|
'tfjs-backend-wasm.wasm': wasmToUrl(tfwasm), |
||||
|
'tfjs-backend-wasm-simd.wasm': wasmToUrl(tfwasmsimd), |
||||
|
'tfjs-backend-wasm-threaded-simd.wasm': wasmToUrl(tfwasmthreadedsimd) |
||||
|
}) |
||||
|
} else { |
||||
|
weightsData = await (await fetch(chrome.runtime.getURL('./model.weights.bin'))).text() |
||||
|
const args = { |
||||
|
'tfjs-backend-wasm.wasm': chrome.runtime.getURL('tfjs-backend-wasm.wasm'), |
||||
|
'tfjs-backend-wasm-simd.wasm': chrome.runtime.getURL('tfjs-backend-wasm-simd.wasm'), |
||||
|
'tfjs-backend-wasm-threaded-simd.wasm': chrome.runtime.getURL('tfjs-backend-wasm-threaded-simd.wasm') |
||||
|
} |
||||
|
setWasmPaths(args) |
||||
|
} |
||||
|
const l = await tf.setBackend('wasm') |
||||
|
console.log('tf backend loaded', l) |
||||
|
} catch (err) { |
||||
|
console.log('tf err', err) |
||||
|
} |
||||
|
})() |
||||
|
|
||||
function toggle (obj, v) { |
function toggle (obj, v) { |
||||
if (v) obj.style.display = '' |
if (v) obj.style.display = '' |
||||
@ -204,19 +231,19 @@ async function predict (img, bg, off) { |
|||||
if (!model) { |
if (!model) { |
||||
model = await load() |
model = await load() |
||||
} |
} |
||||
image = imageFromCanvas(img, bg, off) |
const image = imageFromCanvas(img, bg, off) |
||||
tensor = tf.browser |
const tensor = tf.browser |
||||
.fromPixels(image, 1) |
.fromPixels(image, 1) |
||||
.mul(-1 / 238) |
.mul(-1 / 238) |
||||
.add(1) |
.add(1) |
||||
prediction = await model.predict(tensor.expandDims(0)).data() |
const prediction = await model.predict(tensor.expandDims(0)).data() |
||||
|
|
||||
return createSequence(prediction) |
return createSequence(prediction) |
||||
} |
} |
||||
|
|
||||
function createSequence (prediction) { |
function createSequence (prediction) { |
||||
const csl = charsetJSON.charset.length |
const csl = charsetJSON.charset.length |
||||
sequence = [] |
const sequence = [] |
||||
|
|
||||
for (let pos = 0; pos < prediction.length; pos += csl) { |
for (let pos = 0; pos < prediction.length; pos += csl) { |
||||
const preds = prediction.slice(pos, pos + csl) |
const preds = prediction.slice(pos, pos + csl) |
||||
@ -240,8 +267,8 @@ function createSequence (prediction) { |
|||||
} |
} |
||||
|
|
||||
function postprocess (sequence, overrides) { |
function postprocess (sequence, overrides) { |
||||
csl = charsetJSON.charset.length |
const csl = charsetJSON.charset.length |
||||
possibilities = [{ sequence: [] }] |
let possibilities = [{ sequence: [] }] |
||||
|
|
||||
sequence.forEach(function (e, i) { |
sequence.forEach(function (e, i) { |
||||
let additions |
let additions |
||||
@ -255,7 +282,7 @@ function postprocess (sequence, overrides) { |
|||||
|
|
||||
if (additions.length === 1 && additions[0].sym === '') return |
if (additions.length === 1 && additions[0].sym === '') return |
||||
|
|
||||
oldpos = possibilities |
const oldpos = possibilities |
||||
possibilities = [] |
possibilities = [] |
||||
oldpos.forEach(function (possibility) { |
oldpos.forEach(function (possibility) { |
||||
additions.forEach(function (a) { |
additions.forEach(function (a) { |
||||
@ -381,6 +408,8 @@ async function solve (force) { |
|||||
const help = document.getElementById('t-help') |
const help = document.getElementById('t-help') |
||||
if (!help) return |
if (!help) return |
||||
|
|
||||
|
await backendloaded |
||||
|
|
||||
placeAfter(solveButton, resp) |
placeAfter(solveButton, resp) |
||||
placeAfter(altsDiv, help) |
placeAfter(altsDiv, help) |
||||
|
|
||||
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading…
Reference in new issue