analyze_image
The analyze_image module provides functions for analyzing single images using Nenya models.
Functions
- nenya.analyze_image.get_latents(img, model_file, opt)
Get the Nenya latents for an input image.
- Parameters:
img (numpy.ndarray) – Input image (64,64) or (1,64,64)
model_file (str) – Full path to the Nenya model file (must be on local filesystem)
opt (nenya.params.Params) – Parameters for the Nenya model
- Returns:
Tuple of (latents, pre-processed image)
- Return type:
- nenya.analyze_image.calc_DT(images, random_jitter, verbose=False)
Calculate DT (temperature difference) for a given image or set of images.
- Parameters:
images (numpy.ndarray) – Input images. Analyzed shape is (N, 64, 64) but various input shapes are allowed.
random_jitter (list) – Range to crop, amount to randomly jitter
verbose (bool, optional) – Whether to print progress. Defaults to False.
- Returns:
DT value(s)
- Return type:
- nenya.analyze_image.umap_image(nenya_model, img)
UMAP embed an input image using a specified model.
- Parameters:
nenya_model (str) – Nenya model name, e.g. ‘v4’, ‘v5’
img (numpy.ndarray) – Input image
- Returns:
Tuple of (UMAP embedding, pre-processed image, table file path, DT value, latents)
- Return type:
Example Usage
from nenya import analyze_image
from nenya import io as nenya_io
import numpy as np
# Create or load an image
img = np.random.randn(64, 64) # Example random image
# Load model options
opt, model_file = nenya_io.load_opt('v5')
# Get latents
latents, pp_img = analyze_image.get_latents(img, model_file, opt)
# Calculate DT
DT = analyze_image.calc_DT(img, opt.random_jitter)
# UMAP embed the image
embedding, pp_img, table_file, DT, latents = analyze_image.umap_image('v5', img)
print(f"UMAP coordinates: U0={embedding[0,0]:.3f}, U1={embedding[0,1]:.3f}")
print(f"DT value: {DT:.2f}")
Implementation Details
The umap_image function combines several steps:
Loads the model options and model file
Extracts latent representations from the image
Calculates the DT value
Loads the appropriate UMAP model
Projects the latent vector into UMAP space
Image Preprocessing
Before extracting latents, images are preprocessed:
Reshaped to the expected format if necessary
Demeaned (mean subtracted)
Converted to a PyTorch tensor
Batched for model input
The calc_DT function:
Reshapes the input to a standard format
Calculates the 90th and 10th percentile temperatures in the center region
Returns the difference as the DT value