Hi everybody,
I created a TFLite model from resnet50:
resnet18 = torchvision.models.resnet50().eval()
sample_input = (torch.randint(0, 256, (1, 3, 244, 244), dtype=torch.float32),)
edge_model = ai_edge_torch.convert(resnet18, sample_input)
edge_model.export("/home/theone/Downloads/CODE_other_models/ai-edge/resnet50.tflite")
Then, I quantized it:
pt2e_quantizer = PT2EQuantizer().set_global(
get_symmetric_quantization_config(is_per_channel=True, is_dynamic=True)
)
pt2e_torch_model = capture_pre_autograd_graph(resnet18,sample_input)
pt2e_torch_model = prepare_pt2e(pt2e_torch_model, pt2e_quantizer)
pt2e_torch_model(sample_input)
pt2e_torch_model = convert_pt2e(pt2e_torch_model, fold_quantize=False)
edge_model = ai_edge_torch.convert(pt2e_torch_model, sample_input, quant_config=QuantConfig(pt2e_quantizer=pt2e_quantizer))
But when I do inference with it, I get a different class than the real one:
interpreter = tf.lite.Interpreter(model_path="/home/ai-edge/resnet50_quantized.tflite")
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()
interpreter.resize_tensor_input(0, [1,3, 224, 224], strict=False)
interpreter.allocate_tensors()
image = Image.open('/home/car.jpeg').convert('RGB')
input_shape = input_details[0]['shape']
image = image.resize((224, 224))
image = np.array(image, dtype=np.float32)
image = np.expand_dims(image, axis=0)
print("Image shape:", image.shape) # This should be (1, height, width, channels)
image = np.reshape(image, (1,3,224, 224)).astype(np.float32)
interpreter.set_tensor(input_details[0]['index'], image)
interpreter.invoke()
output_data = interpreter.get_tensor(output_details[0]['index'])
probabilities = tf.nn.softmax(norm(output_data[0]))
predicted_class = np.argmax(probabilities)
with open('/home//ai-edge/imagenet_class_index.json') as f:
class_idx = json.load(f)
print(class_idx["{}".format(predicted_class)][1])
The code runs with no errors, except the final label.
What exactly am I doing wrong? My guess is data shape/type, because torchvision.models.resnet50() requires shape (batch, height, width, channels), and ai-edge-torch requires (batch, channels, height, width)…
Thanks in advance.