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

can this be converted to tflite model? #2

Closed
farazBhatti opened this issue Apr 21, 2021 · 3 comments
Closed

can this be converted to tflite model? #2

farazBhatti opened this issue Apr 21, 2021 · 3 comments

Comments

@farazBhatti
Copy link

No description provided.

@Akhilesh64
Copy link
Owner

I am not sure but tensorflow provides utilities to convert a model to tflite. Otherwise you can retrain the model and save it as a tflite model.

@farazBhatti
Copy link
Author

here is the conversion code

import os
from keras.models import load_model
import tensorflow as tf

model = load_model('models/model_DUTS.h5', compile=False)

converter = tf.lite.TFLiteConverter.from_keras_model(model)

tfmodel = converter.convert()
open ("model.tflite" , "wb") .write(tfmodel)
print("Sucess!")

and this is for getting inference from converted tflite model

import tensorflow as tf
import PIL.Image as Image
import numpy as np
from sklearn.preprocessing import binarize
from tensorflow.keras.preprocessing.image import array_to_img

import cv2


def preprocess(img_path, dim):
    img = Image.open(img_path)
    img = img.resize(dim, Image.BILINEAR)
    img = np.array(img).astype(np.float32)
    img = np.expand_dims(img, axis=0)

    return img


w = 256
h = 256
dim = (w,h)
model = "model.tflite"
img_path = 'images/boat.jpg'


# Load the TFLite model and allocate tensors.
interpreter = tf.lite.Interpreter(model)
interpreter.allocate_tensors()

# Get input and output tensors.
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()


#Preprocess the image to required size and cast

img = preprocess(img_path, dim)
input_shape = input_details[0]['shape']

input_tensor= np.array(np.expand_dims(img,0))
input_tensor = input_tensor.reshape(input_shape)

#set the tensor to point to the input data to be inferred
input_index = interpreter.get_input_details()[0]["index"]
interpreter.set_tensor(input_index, input_tensor)

#Run the inference
interpreter.invoke()
preds = interpreter.get_tensor(output_details[0]['index'])

preds = np.squeeze(preds)

#For thresholding
for i in range(len(preds)):
    shape = preds[i,:,:].shape
    frame = binarize(preds[1,:,:], threshold = 0.5)
    frame = np.reshape(frame,(shape[0], shape[1]))

#For saving all the frames
for i in range(len(preds)):
  img = np.expand_dims(np.array(preds[i,:,:]),axis=-1)
  img = array_to_img(img)
  img.save('img'+str(i)+'.png')

@Akhilesh64
Copy link
Owner

I dont have any experience with tflite models. As for the prediction script make changes to the below script to suit your needs :

import cv2
import numpy as np
from keras.models import load_model
from sklearn.preprocessing import binarize
from tensorflow.keras.preprocessing.image import array_to_img

model = load_model('models/model_DUTS.h5', compile=False)

img = cv2.imread('images/spawn.png', cv2.IMREAD_COLOR)
img = cv2.resize(img, (256, 256))
img = np.expand_dims(img, axis=0)

preds = model.predict(img)
preds = np.squeeze(preds)

# For thresholding
for i in range(len(preds)):
    shape = preds[i, :, :].shape
    frame = binarize(preds[1, :, :], threshold=0.5)
    frame = np.reshape(frame, (shape[0], shape[1]))
    frame = np.expand_dims(np.array(preds[i,:,:]),axis=-1)
    frame = array_to_img(frame)
    frame.save('img'+str(i)+'.png')

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

No branches or pull requests

2 participants