Wolfram Computation Meets Knowledge

CycleGAN Photo-to-Van Gogh Translation

Turn a photo into a Van Gogh-style painting

Released in 2017, this model exploits a novel technique for image translation, in which two models translating from A to B and vice versa are trained jointly with adversarial training. In addition to the adversarial loss, cycle consistency is also enforced in the loss function: when the output of the first translator is fed into the second, the final result is encouraged to match the input of the first translator. This allows successful training for image translation tasks in which only unpaired training data can be collected. This model was trained to translate photos into Van Gogh-style paintings.

Number of layers: 96 | Parameter count: 2,855,811 | Trained size: 12 MB

Training Set Information

Examples

Resource retrieval

Retrieve the resource object:

In[1]:=
ResourceObject["CycleGAN Photo-to-Van Gogh Translation"]
Out[1]=

Get the pre-trained net:

In[2]:=
NetModel["CycleGAN Photo-to-Van Gogh Translation"]
Out[2]=

Basic usage

Run the net on a photo:

In[3]:=
CloudGet["https://www.wolframcloud.com/objects/a4882f00-a6a3-4342-ab7c-a9dd3b76fc38"] (* Evaluate this cell to copy the example input from a cloud object *)
Out[3]=

Adapt to any size

Automatic image resizing can be avoided by replacing the net encoders. First get the net:

In[4]:=
net = NetModel["CycleGAN Photo-to-Van Gogh Translation"]
Out[4]=

Get a photo:

In[5]:=
CloudGet["https://www.wolframcloud.com/objects/5a8b15b2-da71-4227-a128-f6612826f633"] (* Evaluate this cell to copy the example input from a cloud object *)

Create a new encoder with the desired dimensions:

In[6]:=
netEnc = NetEncoder[{"Image", ImageDimensions[img]}]
Out[6]=

Attach the new net encoder and run the network:

In[7]:=
resizedNet = 
 NetReplacePart[
  net, {"Input" -> netEnc, "Output" -> NetDecoder[{"Image"}]}]
Out[7]=
In[8]:=
resizedNet[img]
Out[8]=

Net information

Inspect the sizes of all arrays in the net:

In[9]:=
NetInformation[
 NetModel["CycleGAN Photo-to-Van Gogh Translation"], "ArraysSizes"]
Out[9]=

Obtain the total number of parameters:

In[10]:=
NetInformation[
 NetModel["CycleGAN Photo-to-Van Gogh Translation"], \
"ArraysTotalElementCount"]
Out[10]=

Obtain the layer type counts:

In[11]:=
NetInformation[
 NetModel["CycleGAN Photo-to-Van Gogh Translation"], \
"LayerTypeCounts"]
Out[11]=

Display the summary graphic:

In[12]:=
NetInformation[
 NetModel["CycleGAN Photo-to-Van Gogh Translation"], "SummaryGraphic"]
Out[12]=

Export to MXNet

Export the net into a format that can be opened in MXNet:

In[13]:=
jsonPath = 
 Export[FileNameJoin[{$TemporaryDirectory, "net.json"}], 
  NetModel["CycleGAN Photo-to-Van Gogh Translation"], "MXNet"]
Out[13]=

Export also creates a net.params file containing parameters:

In[14]:=
paramPath = FileNameJoin[{DirectoryName[jsonPath], "net.params"}]
Out[14]=

Get the size of the parameter file:

In[15]:=
FileByteCount[paramPath]
Out[15]=

The size is similar to the byte count of the resource object:

In[16]:=
ResourceObject["CycleGAN Photo-to-Van Gogh Translation"]["ByteCount"]
Out[16]=

Represent the MXNet net as a graph:

In[17]:=
Import[jsonPath, {"MXNet", "NodeGraphPlot"}]
Out[17]=

Requirements

Wolfram Language 11.3 (March 2018) or above

Reference