Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add tflite support #52

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open

add tflite support #52

wants to merge 6 commits into from

Conversation

fsx950223
Copy link

@fsx950223 fsx950223 commented Sep 28, 2021

import tensorflow as tf
import numpy as np
import json
from guesslang import model
from guesslang import guess

fp = open('guesslang/data/languages.json', 'r')
j = list(json.load(fp))

model = guess.Guess('./saved_model')
model.export('guesslang/data/model/variables/variables', True)

saved_model = tf.saved_model.load('./saved_model')

inputs = ["""
def qsort(items):
    if not items:
        return []
    else:
        pivot = items[0]
        less = [x for x in items if x <  pivot]
        more = [x for x in items[1:] if x >= pivot]
        return qsort(less) + [pivot] + qsort(more)


if __name__ == '__main__':
    items = [1, 4, 2, 7, 9, 3]
    print(f'Sorted: {qsort(items)}')

"""]
data = tf.strings.bytes_split(inputs)

inputs = data.to_tensor(shape=(1, 10001))
content = tf.convert_to_tensor(inputs[0])
length = tf.cast(data.row_lengths(1)[0], dtype=tf.int32)
predicted = saved_model.signatures['predict'](content=content, length=length)

numpy_floats = predicted['probabilities']
extensions = predicted['all_classes'][0]
ids = predicted['all_class_ids'][0]
idx = tf.argmax(numpy_floats, axis=1)
print(j[ids[idx[0]]])


interpreter = tf.lite.Interpreter('./saved_model/guesslang.tflite')
input_details = interpreter.get_input_details()
interpreter.allocate_tensors()
interpreter.set_tensor(input_details[1]['index'], inputs[0].numpy())
interpreter.set_tensor(input_details[0]['index'], np.array(data.row_lengths(1)[0]).astype(np.int32))

interpreter.invoke()
idx = tf.argmax(interpreter.tensor(interpreter.get_output_details()[1]['index'])(), axis=1)
ids = interpreter.tensor(interpreter.get_output_details()[3]['index'])()[0]
print(j[ids[idx[0]]])

Test script

@asiryan
Copy link

asiryan commented Feb 27, 2023

Could you share Guesslang *.TFLite model?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants