Code Monkey home page Code Monkey logo

torch-image-lerp's Introduction

torch-image-lerp

License PyPI Python Version CI codecov

Linear 2D/3D image interpolation and gridding in PyTorch.

Why?

This package provides a simple, consistent API for

  • sampling from 2D/3D images (sample_image_2d()/sample_image_3d())
  • inserting values into 2D/3D images (insert_into_image_2d(), insert_into_image_3d)

Operations are differentiable and sampling from complex valued images is supported.

Installation

pip install torch-image-lerp

Usage

Sample from image

import torch
import numpy as np
from torch_image_lerp import sample_image_2d

image = torch.rand((28, 28))

# make an arbitrary stack (..., 2) of 2d coords
coords = torch.tensor(np.random.uniform(low=0, high=27, size=(6, 7, 8, 2))).float()

# sampling returns a (6, 7, 8) array of samples obtained by linear interpolation
samples = sample_image_2d(image=image, coordinates=coords)

The API is identical for 3D but takes (..., 3) coordinates and a (d, h, w) image.

Insert into image

import torch
import numpy as np
from torch_image_lerp import insert_into_image_2d

image = torch.zeros((28, 28))

# make an arbitrary stack (..., 2) of 2d coords
coords = torch.tensor(np.random.uniform(low=0, high=27, size=(3, 4, 2)))

# generate random values to place at coords
values = torch.rand(size=(3, 4))

# sampling returns a (6, 7, 8) array of samples obtained by linear interpolation
samples = insert_into_image_2d(values, image=image, coordinates=coords)

The API is identical for 3D but takes (..., 3) coordinates and a (d, h, w) image.

torch-image-lerp's People

Contributors

alisterburt avatar dependabot[bot] avatar sjrothfuss avatar

Watchers

 avatar

torch-image-lerp's Issues

Wrong Type in README Sample Example

  • torch-image-lerp version: 0.0.3
  • Python version: 3.10.14
  • Operating System: macOS 14.5

Description

Running the "Sample from image" code from the README gives an error due to unexpected datatype. Coercing the image dtype solves it.

What I Did

import torch
import numpy as np
from torch_image_lerp import sample_image_2d

image = torch.rand((28, 28))
coords = torch.tensor(np.random.uniform(low=0, high=27, size=(6, 7, 8, 2)))
samples = sample_image_2d(image=image, coordinates=coords)
RuntimeError: expected scalar type Float but found Double

Adding dtype to image as shown below prevents this error.

image = torch.rand((28, 28), dtype=torch.float64)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.