Giter VIP home page Giter VIP logo

Comments (3)

Akhilesh64 avatar Akhilesh64 commented on May 17, 2024 1

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.

from image-segmentation-u-2-net.

Akhilesh64 avatar Akhilesh64 commented on May 17, 2024 1

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

from image-segmentation-u-2-net.

farazBhatti avatar farazBhatti commented on May 17, 2024

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

from image-segmentation-u-2-net.

Related Issues (8)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.