I have a Keras model that performs binary classification using sigmoid function for classification. I compiled my model to .tflite format as required by the Coral USB to run inference. However, I noticed that the script classify_image.py performs multi-class classification. Therefore, when I attempt to classify the images, I get 100% prediction for any image. For example, my model classifies infrared imagery for fever state. It gives 100% positive for fever class even if I pass a ball image.
So, I tested a model for multiclass using as layer softmax for plants using an entirely custom model again, and this time it worked. It gives a reasonable 85% accuracy for plants A, Plants B and Plants C.
Therefore, I'd like to know what changes do I need to do, to work with Pycoral using a binary classification custom model.
This is the code I am using for classification:
import argparse
import time
from PIL import Image
from pycoral.adapters import classify
from pycoral.adapters import common
from pycoral.utils.dataset import read_label_file
from pycoral.utils.edgetpu import make_interpreter
import cv2 as cv
import numpy as np
def main():
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('-m', '--model', required=True,
help='File path of .tflite file.')
parser.add_argument('-i', '--input', required=True,
help='Image to be classified.')
parser.add_argument('-l', '--labels',
help='File path of labels file.')
parser.add_argument('-k', '--top_k', type=int, default=2,
help='Max number of classification results')
parser.add_argument('-t', '--threshold', type=float, default=0.0,
help='Classification score threshold')
parser.add_argument('-c', '--count', type=int, default=5,
help='Number of times to run inference')
args = parser.parse_args()
labels = read_label_file(args.labels) if args.labels else {}
interpreter = make_interpreter(*args.model.split('@'))
interpreter.allocate_tensors()
print(interpreter)
size = common.input_size(interpreter)
image = cv.imread(args.input)
image = cv.normalize(image, image, 0, 255, cv.NORM_MINMAX)
common.set_input(interpreter, image)
print('----INFERENCE TIME----')
print('Note: The first inference on Edge TPU is slow because it includes',
'loading the model into Edge TPU memory.')
for _ in range(args.count):
start = time.perf_counter()
interpreter.invoke()
inference_time = time.perf_counter() - start
classes = classify.get_classes(interpreter, args.top_k, args.threshold)
print('%.1fms' % (inference_time * 1000))
print('-------RESULTS--------')
for c in classes:
print('%s: %.5f' % (labels.get(c.id, c.id), c.score))
if __name__ == '__main__':
main()
My labels.txt has two labels only fever (positive class) and healthy (negative class). The threshold used for the binary model classification was 0.50 and to the best of my knowledge the model layers are entirely compatible with the Coral USB Accelerator device.
from Binary image classification using Pycoral library with Google Coral USB accelerator
No comments:
Post a Comment