Code Monkey home page Code Monkey logo

diffmorph's Introduction

Differentiable Morphing

Image morphing without reference points by applying warp maps and optimizing over them.

Differentiable Morphing is machine learning algorithm that can morph any two images without reference points. It called "differentiable morphing" because neural network here is not used in traditional data to label mapping sense, but as an easy way to solve optimization problem where one image is mapped to another via warp maps that are found by gradient descent. So after maps are found there is no need for the network itself.

Results

example 1 example 2 example 3

Dependencies

Tensorflow 2.1.3 and above.

Example notebook

Open In Colab
Provided by @chigozienri. Can also generated joint video from multiple consecutive images.

Usage

Install proper dependencies:

pip install -r requirements.txt

Use the program:

morph.py -s images/img_1.jpg -t images/img_2.jpg

-s Source file
-t Target file

Unnecessary parameters:
-e Number of epochs to train maps on training stage
-a Addition map multiplier
-m Multiplication map multiplier
-w Warp map multiplier
-add_first If true add map would be applied to the source image before mult map. (might work better in some cases)

Idea

Suppose we want to produce one image from another in a way that we use as much useful information as possible, so if two given images share any similarities between them we make use of these similarities.

toy_example

After several trials I found out that the best way to achieve such effect is to use following formula.

formula

Here "Mult map" removes unnecessary parts of an image and shifts color balance, "Add map" creates new colors that are not present in original image and "Warp map" distort an image in some way to reproduce shifting, rotation and scaling of objects. W operation is dense_image_warp method that present in tensorflow and usually used for optical flow estimation tasks.

All maps are found by gradient descent using very simple convolution network. Now, by applying alpha scaling parameter to every map we will get smooth transition from one image to another without any loss of useful data (at least for the given toy example).

transition

Thoughts

Notice that all maps produced generate somewhat meaningful interpolation without any understanding of what exactly present in the images. That means that warp operation might be very useful in images processing tasks. In some sense warp operation might be thought as long range convolution, because it can "grab" data from any point of an image and reshape it in some useful way. Therefore it might be beneficial to use warp operation in classification tasks and might allow networks be less susceptible to small perturbations of the data. But especially, it should be beneficial to use in generation task. It should be much easier to produce new data by combining and perturbating several examples of known data points than to learn a function that represents all data points at ones.

diffmorph's People

Contributors

cardboardcode avatar chigozienri avatar effdotsh avatar tanseersaji avatar volotat avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

diffmorph's Issues

Pixel-wise Matching

Hi,

Would the learned warp maps be able to translate a given pixel location in the source image to a location in the target image?

morph.py give just errors

Hi
Executing just morph.py -s images/xxx.jpg -t images/xxx.jpg [-e 1000 -a 0.8 -m 0.8 -w 0.3] gives me just "command not found. "Executing : ./morph.py -s images/trump.jpg -t images/pence.jpg [-e 1000 -a 0.8 -m 0.8 -w 0.3] gives me this:
./morph.py: line 1: import: command not found
./morph.py: line 2: import: command not found
./morph.py: line 3: import: command not found
./morph.py: line 4: import: command not found
./morph.py: line 5: import: command not found
./morph.py: line 8: TRAIN_EPOCHS: command not found
./morph.py: line 10: im_sz: command not found
./morph.py: line 11: mp_sz: command not found
./morph.py: line 13: warp_scale: command not found
./morph.py: line 14: mult_scale: command not found
./morph.py: line 15: add_scale: command not found
./morph.py: line 16: add_first: command not found
./morph.py: line 19: @tf.function: command not found
./morph.py: line 20: syntax error near unexpected token (' ./morph.py: line 20: def warp(origins, targets, preds_org, preds_trg):'
So it doesent work for me in Ubuntu 20.04 with tensorflow 2.4.0

One step further i tried :
DiffMorph$ python3 morph.py -s images/xxx.jpg -t images/xxx.jpg [-e 1000 -a 0.8 -m 0.8 -w 0.3]
2021-01-09 16:47:46.888345: W tensorflow/stream_executor/platform/default/dso_loader.cc:60] Could not load dynamic library 'libcudart.so.11.0'; dlerror: libcudart.so.11.0: cannot open shared object file: No such file or directory
2021-01-09 16:47:46.888361: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.
Traceback (most recent call last):
File "morph.py", line 4, in
import cv2
ModuleNotFoundError: No module named 'cv2'

Have no idea whats wrong here !

Memory usage scales linearly

While running use_wrap_maps method, the script uses memory proportional to the number of steps. Making it difficult to generate higher resolution images and higher number of frames.

Macbook M1

Can this program run on Macbook with M1 chip? I been having a lot of problems with the versions of tensorflow and numpy.

ModuleNotFoundError: No module named 'tqdm'

Issue Description

After setting up the virtualenv, the following error was printed to terminal when running python3 morph.py -s images/image_1.png -t images/image_2.png

Traceback (most recent call last):
  File "./morph.py", line 8, in <module>
    from tqdm import tqdm
ModuleNotFoundError: No module named 'tqdm'

Environment

  • Ubuntu 20.04 Focal Fossa
  • Nvidia Geforce GTX 1060

Steps to Reproduce

Follow the steps below to reproduce the error described above:

virtualenv -p python3 venv
source venv/bin/activate
pip3 install -r requirements.txt
python3 morph.py -s images/image_1.png -t images/image_2.png

Others

This issue has been resolved. See below for the solution. Posting this issue to simply publish the solution.

Optimal parameters

Which parameters were used to generate car morph from example? How many epochs is needed to generate that result?

I'm newbie in neural networks so sorry for possible stupid question

Docker image provided

Hi developers,

Thanks for your codes.
I ran into a problem for few days.
Due to the limitations of my research zone, all dependencies can only download from anaconda and then ship to my conda environment. There exists numerous problems during env construction.

Would you mind providing the docker image so that we can build the env properly.
I will appreciate your help with this situation.

ERROR: Could not find a version that satisfies the requirement numpy==1.23.1

Running the colab project fails at a number of points, starting with this error:

ERROR: Could not find a version that satisfies the requirement numpy==1.23.1

Unfortunately don't have enough time to troubleshoot this one myself - Anyone else experience this or know a fix, or know what I'm doing wrong? - I am very new to colaboratory

Error with morph.py command

Hello, I was trying to execut the morph.py command, however I got an error:

"(tf) C:\Users\chris\Desktop\DiffMorph-master\DiffMorph-master>morph.py -s images/test(2).jpg -t images/test(1).jpg
Traceback (most recent call last):
File "C:\Users\chris\Desktop\DiffMorph-master\DiffMorph-master\morph.py", line 3, in
import numpy as np
ModuleNotFoundError: No module named 'numpy'"

I think I'll need some help here please ^^
Thanks a lot!

Full dependencies list

Hello. Just made your code work on my computer. Tensorflow isn't the only dependency requiered, you may want to update your readme. You also need tensorflow-add_ons and cv2.

They can all be install through this command

pip3 install tensorflow tensorflow_addons opencv-python

This would make your code more accesible.
Cheers!

video output black

Latest try:

python morph.py -s images/img_1.jpg -t images/img_2.jpg

2021-01-12 17:23:25.848313: I tensorflow/core/platform/cpu_feature_guard.cc:142] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN)to use the following CPU instructions in performance-critical operations: SSE4.1 SSE4.2 AVX AVX2 FMA

To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.

2021-01-12 17:23:25.869795: I tensorflow/core/platform/profile_utils/cpu_utils.cc:104] CPU Frequency: 3600000000 Hz

2021-01-12 17:23:25.870428: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x557665775f10 initialized for platform Host (this does not guarantee that XLA will be used). Devices:

2021-01-12 17:23:25.870473: I tensorflow/compiler/xla/service/service.cc:176] StreamExecutor device (0): Host, Default Version

Epoch 100, Loss: 0.07585608214139938

Epoch 200, Loss: 0.056177154183387756

Epoch 300, Loss: 0.047623421996831894

Epoch 400, Loss: 0.04261484742164612

Epoch 500, Loss: 0.03922488912940025

Epoch 600, Loss: 0.036724839359521866

Epoch 700, Loss: 0.03478630632162094

Epoch 800, Loss: 0.03322023153305054

Epoch 900, Loss: 0.031917549669742584

Epoch 1000, Loss: 0.030810609459877014

Image #10 produced.

Image #20 produced.

Image #30 produced.

Image #40 produced.

Image #50 produced.

Image #60 produced.

Image #70 produced.

Image #80 produced.

Image #90 produced.

Image #100 produced.

Result video saved.

So far it looks promising but in the morph folder the video is just 630 kb and black when trying to play. 2 pictures maps.jpg +result.jpg look better see https://i.imgur.com/BbhNnsZ.jpg and https://i.imgur.com/6pYS0gw.jpg . Seems like i am coming closer but have no idea why the video is not created

Can't understand how to use step 2 in google colab :/

Hello,
I'm trying to use DiffMorph with google colab (because I'm not able to understand how to make it work otherwise.. (if you guys btw can help, I'll be glad!).
However, maybe I'm missing something, but I'm not able to make work the step 2 "Step 2: Add images to the "DiffMorph/input" directory." ---> It seems that there is no code to run, and I don't know where is that directory, where to find it etc.

So I need some help please here..
Thanks a lot!

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.