Giter Club home page Giter Club logo

pytorch-blender's Introduction

pytorch-blender

Seamless integration of Blender renderings into PyTorch datasets for deep learning from artificial visual data. This repository contains a minimal demonstration that harvests images and meta data from ever changing Blender renderings.

python pytorch_sample.py

renders a set of images of random rotated cubes to ./tmp/output_##.png, such as the following

This image is generated by 4 Blender instances, randomly perturbating a minimal scene. The results are collated in a BlenderDataset. A PyTorch DataLoader with batch size 4 is used to grab from the dataset and produce the figure.

The code accompanies our academic work in the field of machine learning from artificial images. When using please cite the following work

@misc{cheindkpts2019,
Author = {Christoph Heindl and Sebastian Zambal and Josef Scharinger},
Title = {Learning to Predict Robot Keypoints Using Artificially Generated Images},
Year = {2019},
Eprint = {arXiv:1907.01879},
Note = {To be published at ETFA 2019},
}

Code outline

import torch.utils.data as data

import blendtorch as bt

# Standard PyTorch Dataset convention
class MyDataset:

    def __init__(self, blender_launcher, transforms=None):
        self.recv = bt.Receiver(blender_launcher)
        self.transforms = transforms

    def __len__(self):
        # Virtually anything you'd like to end episodes.
        return 100 

    def __getitem__(self, idx):        
        # Data is a dictionary of {image, xy, id}, 
        # see publisher script
        d = self.recv(timeoutms=5000)   
        return d['image'], d['xy'], d['id']

kwargs = {
    'num_instances': 2,
    'script': 'blender.py',
    'scene': 'scene.blend',
}

with bt.BlenderLauncher(**kwargs) as bl:        
    ds = MyDataset(bl)        
    dl = data.DataLoader(ds, batch_size=4, num_workers=0)

    for idx in range(10):
        x, coords, ids = next(iter(dl))
        print(f'Received from {ids}')

Runtimes

The runtimes for the demo scene (really quick to render) is shown below.

Blender Instances Runtime ms/batch
1 103 ms ± 5.17 ms
2 43.7 ms ± 10.3 ms

The above timings include rendering, transfer and encoding/decoding. Depending on the complexity of renderings you might want to tune the number of instances.

Prerequisites

The following packages need to be available in your PyTorch environment and Blender environment:

Both packages are installable via pip. In order add packages to your Blender packaged Python distribution, execute the following commands (usually administrator privileges are required on Windows)

"<BLENDERPATH>2.79\python\bin\python.exe" -m ensurepip
"<BLENDERPATH>2.79\python\bin\python.exe" -m pip install pyzmq
"<BLENDERPATH>2.79\python\bin\python.exe" -m pip install pillow

where <BLENDERPATH> is the file path to the directory containing the Blender executable.

Note The Blender executable needs to be in your PATH. On Windows it does not suffice to temporarily modify the PATH variable, as no derived shell is spawned and temporary environment variables are not passed on.

How it works

An instance of BlenderLaunch is responsible for starting and stopping background Blender instances. The script blender.py and additional arguments are passed to the starting Blender instance. blender.py creates a publisher socket for communication and starts producing random renderings. Meanwhile, a PyTorch dataset uses a Receiver instance to read data from publishers.

Caveats

  • In background mode, Blender ViewerNodes are not updated, so rendering have to be written to files. Currently, the Blender script re-imports the written image and sends it as message to any subscriber. This way, we do not need to keep track of which files have already been read and can be deleted, which simplifies communication.
  • In this sample. only the main composite rendering is transmitted. You might want to use FileOutputNode instead, to save multiple images per frame.
  • Currently you need to use num_workers=0 when creating a PyTorch DataLoader as the Receiver object is capable of multi-process pickling.

pytorch-blender's People

Contributors

cheind avatar

Watchers

 avatar

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.