zenseact / zod

Software Development Kit for the Zenseact Open Dataset (ZOD)
https://zod.zenseact.com
MIT License
92 stars 13 forks source link

Add ZodFrames mean and standard deviation #8

Closed AlexVialaBellander closed 1 year ago

AlexVialaBellander commented 1 year ago

Summary

This pull request adds the mean and standard deviation values of ZodFrames to the set of constants.

Method

The method used to calculate these values:

#!/usr/bin/env python3

import numpy as np
from zod import ZodFrames
import concurrent.futures
import logging

logging.basicConfig(level=logging.INFO,
                    format='%(asctime)s - %(levelname)s - %(message)s')

def process_image(i, zod_frames):
    try:
        image = zod_frames[i].get_image().astype(np.float32) / 255.0
        mean = np.mean(image, axis=(0, 1))
        squared_mean = np.mean(np.square(image), axis=(0, 1))
        return mean, squared_mean
    except Exception as e:
        logging.error(f"Error processing image {i}: {e}")
        return np.zeros(3), np.zeros(3)

def calculate_mean_std():
    logging.info("Calculating mean and std...")
    mean = np.zeros(3)
    squared_mean = np.zeros(3)

    zod_frames = ZodFrames("/mnt/ZOD", "full")

    logging.info("ZodFrames loaded")
    ids = zod_frames._frames.keys()
    num_images = len(ids)

    logging.info("Starting loop")
    with concurrent.futures.ThreadPoolExecutor(max_workers=20) as executor:
        for step, (mean_i, squared_mean_i) in enumerate(executor.map(process_image, ids, [zod_frames] * num_images)):
            if step % 1000 == 0:
                logging.info(f"Processing image {step}/{num_images}")
            mean += mean_i
            squared_mean += squared_mean_i

    mean /= num_images
    squared_mean /= num_images
    std = np.sqrt(squared_mean - np.square(mean))

    return mean, std

def main():
    mean, std = calculate_mean_std()
    logging.info(f"Mean: {mean}, Std: {std}")

main()

Results

The new mean and standard deviation values are: Mean: [0.33661897 0.34518355 0.36694038] Std: [0.15980822 0.17975767 0.21439413]

Merge

Added the following code to zod/constants.py

# ZodFrame properties
FRAMES_IMAGE_MEAN= [0.337, 0.345, 0.367]
FRAMES_IMAGE_STD= [0.160, 0.180, 0.214]