148 lines
4.6 KiB
Python
148 lines
4.6 KiB
Python
"""
|
|
PYNQ inference script for the HLS TinyCNN accelerator.
|
|
|
|
Single image:
|
|
python3 tiny_cnn_inference.py --bitstream tiny_cnn.bit --image test_images/class_0/10.jpg
|
|
|
|
Validation set:
|
|
python3 tiny_cnn_inference.py --bitstream tiny_cnn.bit --test_dir test_images --runs 100
|
|
"""
|
|
import argparse
|
|
import os
|
|
import time
|
|
|
|
import numpy as np
|
|
from PIL import Image
|
|
from pynq import Overlay, allocate
|
|
|
|
|
|
IMG_SIZE = 64
|
|
NUM_CLASSES = 10
|
|
|
|
|
|
def preprocess_image(image_path):
|
|
image = Image.open(image_path).convert("L")
|
|
image = image.resize((IMG_SIZE, IMG_SIZE))
|
|
return np.asarray(image, dtype=np.uint8).reshape(-1)
|
|
|
|
|
|
class TinyCNNAccelerator:
|
|
def __init__(self, bitstream_path):
|
|
self.overlay = Overlay(bitstream_path)
|
|
self.dma = self.overlay.axi_dma_0
|
|
self.accel = self.overlay.tiny_cnn_accel_0
|
|
self.input_buffer = allocate(shape=(IMG_SIZE * IMG_SIZE,), dtype=np.uint32)
|
|
self.output_buffer = allocate(shape=(NUM_CLASSES,), dtype=np.int32)
|
|
|
|
def close(self):
|
|
if hasattr(self, "input_buffer"):
|
|
self.input_buffer.freebuffer()
|
|
if hasattr(self, "output_buffer"):
|
|
self.output_buffer.freebuffer()
|
|
|
|
def infer_pixels(self, pixels_u8):
|
|
self.input_buffer[:] = pixels_u8.astype(np.uint32)
|
|
self.output_buffer[:] = 0
|
|
|
|
start = time.time()
|
|
self.dma.recvchannel.transfer(self.output_buffer)
|
|
self.dma.sendchannel.transfer(self.input_buffer)
|
|
self.accel.write(0x00, 0x01)
|
|
self.dma.sendchannel.wait()
|
|
self.dma.recvchannel.wait()
|
|
elapsed = time.time() - start
|
|
|
|
logits = np.asarray(self.output_buffer, dtype=np.int32).copy()
|
|
pred = int(np.argmax(logits))
|
|
return pred, logits, elapsed
|
|
|
|
def infer_image(self, image_path):
|
|
pixels = preprocess_image(image_path)
|
|
return self.infer_pixels(pixels)
|
|
|
|
|
|
def test_single(accel, image_path):
|
|
pred, logits, elapsed = accel.infer_image(image_path)
|
|
print(f"image: {image_path}")
|
|
print(f"predicted class: {pred}")
|
|
print(f"logits: {logits.tolist()}")
|
|
print(f"inference time: {elapsed * 1000:.2f} ms")
|
|
return pred, elapsed
|
|
|
|
|
|
def test_dataset(accel, test_dir):
|
|
total = 0
|
|
correct = 0
|
|
times = []
|
|
class_total = [0] * NUM_CLASSES
|
|
class_correct = [0] * NUM_CLASSES
|
|
|
|
for class_idx in range(NUM_CLASSES):
|
|
class_dir = os.path.join(test_dir, f"class_{class_idx}")
|
|
if not os.path.isdir(class_dir):
|
|
print(f"missing class directory: {class_dir}")
|
|
continue
|
|
for image_name in sorted(os.listdir(class_dir)):
|
|
if not image_name.lower().endswith((".jpg", ".jpeg", ".png")):
|
|
continue
|
|
image_path = os.path.join(class_dir, image_name)
|
|
pred, _, elapsed = accel.infer_image(image_path)
|
|
total += 1
|
|
class_total[class_idx] += 1
|
|
times.append(elapsed)
|
|
if pred == class_idx:
|
|
correct += 1
|
|
class_correct[class_idx] += 1
|
|
|
|
if total == 0:
|
|
print("no images found")
|
|
return
|
|
|
|
accuracy = 100.0 * correct / total
|
|
avg_time = float(np.mean(times))
|
|
fps = 1.0 / avg_time if avg_time > 0 else 0.0
|
|
print(f"accuracy: {accuracy:.2f}% ({correct}/{total})")
|
|
print(f"average time: {avg_time * 1000:.2f} ms")
|
|
print(f"fps: {fps:.2f}")
|
|
for class_idx in range(NUM_CLASSES):
|
|
if class_total[class_idx]:
|
|
class_acc = 100.0 * class_correct[class_idx] / class_total[class_idx]
|
|
print(f"class_{class_idx}: {class_acc:.2f}% ({class_correct[class_idx]}/{class_total[class_idx]})")
|
|
|
|
|
|
def benchmark(accel, image_path, runs):
|
|
pixels = preprocess_image(image_path)
|
|
for _ in range(5):
|
|
accel.infer_pixels(pixels)
|
|
times = []
|
|
for _ in range(runs):
|
|
_, _, elapsed = accel.infer_pixels(pixels)
|
|
times.append(elapsed)
|
|
avg_time = float(np.mean(times))
|
|
print(f"benchmark runs: {runs}")
|
|
print(f"average time: {avg_time * 1000:.2f} ms")
|
|
print(f"fps: {1.0 / avg_time:.2f}")
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser(description="TinyCNN HLS PYNQ inference")
|
|
parser.add_argument("--bitstream", required=True)
|
|
parser.add_argument("--image", default=None)
|
|
parser.add_argument("--test_dir", default=None)
|
|
parser.add_argument("--runs", type=int, default=100)
|
|
args = parser.parse_args()
|
|
|
|
accel = TinyCNNAccelerator(args.bitstream)
|
|
try:
|
|
if args.image:
|
|
test_single(accel, args.image)
|
|
benchmark(accel, args.image, args.runs)
|
|
if args.test_dir:
|
|
test_dataset(accel, args.test_dir)
|
|
finally:
|
|
accel.close()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|