unitPredict
Perform inference using unsupervised image-to-image translation (UNIT) network
Since R2021a
Syntax
Description
performs unsupervised image-to-image translation of image translatedImage
= unitPredict(net
,inputImage
)inputImage
using the UNIT network net
.
This function requires Deep Learning Toolbox™.
specifies the direction of image-to-image translation for inference using the
translatedImage
= unitPredict(net
,inputImage
,"OutputType",outputType
)outputType
argument. The direction can be source-to-target or
target-to-source.
Examples
Perform Source-to-Target Image Translation
Download a pretrained UNIT generator network that translates images between daytime and dusk lighting conditions using the helper function downloadTrainedDayDuskGeneratorNet
. The source domain is daytime lighting and the target domain is dusk lighting.
trainedUNIT_url = "https://ssd.mathworks.com/supportfiles/"+ ... "vision/data/trainedDayDuskUNITGeneratorNet.zip"; trainedUNIT_filename = "trainedDayDuskUNITGeneratorNet.mat"; downloadTrainedDayDuskGeneratorNet(trainedUNIT_url,pwd); load(trainedUNIT_filename);
Read and display a test image acquired in daytime conditions.
input = imread("car1.jpg");
imshow(input)
Preprocess the image so that it is compatible with the network. Convert the data to data type single
in the range [-1, 1]. Decrease the size of the image, and store the data in a dlarray
object.
input = (im2single(input) - 0.5)/0.5;
input = imresize(input,0.25);
dlInput = dlarray(input,"SSCB");
Translate the source image to the target domain using the UNIT generator network.
dlOutput = unitPredict(gen,dlInput);
Extract the translated image data from the dlarray
object and rescale the data to the range [0, 1]. Display the translated image. The translated image resembles images acquired in dusk conditions.
output = rescale(extractdata(dlOutput)); imshow(output)
Perform Target-to-Source Image Translation
Download a pretrained UNIT generator network that translates images between daytime and dusk lighting conditions using the helper function downloadTrainedDayDuskGeneratorNet
. The source domain is daytime lighting and the target domain is dusk lighting.
trainedUNIT_url = 'https://ssd.mathworks.com/supportfiles/vision/data/trainedDayDuskUNITGeneratorNet.zip'; trainedUNIT_filename = 'trainedDayDuskUNITGeneratorNet.mat'; downloadTrainedDayDuskGeneratorNet(trainedUNIT_url,pwd); load(trainedUNIT_filename);
Read and display a test image acquired in dusk conditions.
input = imread("office_2.jpg");
imshow(input)
Preprocess the image so that it is compatible with the network. Convert the data to data type single
in the range [-1, 1]. Store the data in a dlarray
object.
input = (im2single(input) - 0.5)/0.5;
dlInput = dlarray(input,"SSCB");
Translate the target image to the source domain using the pretrained UNIT generator network, gen
.
dlOutput = unitPredict(gen,dlInput,"OutputType","TargetToSource");
Extract the translated image data from the dlarray
object and rescale the data to the range [0, 1]. Display the translated image. The translated image resembles images acquired in daytime lighting conditions.
output = rescale(extractdata(dlOutput)); imshow(output)
Input Arguments
net
— UNIT generator network
dlnetwork
object
UNIT generator network, specified as a dlnetwork
(Deep Learning Toolbox) object. You can create a
UNIT generator network using the unitGenerator
function.
inputImage
— Input image
formatted dlarray
object
Input image for image-to-image translation, specified as a formatted dlarray
(Deep Learning Toolbox)
object.
outputType
— Direction of image-to-image translation
"SourceToTarget"
(default) | "TargetToSource"
Direction of image-to-image translation for inference, specified as one of these values.
"SourceToTarget"
– translate from the source domain to the target domain"TargetToSource"
– translate from the target domain to the source domain
Data Types: char
| string
Output Arguments
translatedImage
— Inferred image
dlarray
object
Inferred image after image-to-image translation, returned as a dlarray
(Deep Learning Toolbox)
object.
Version History
Introduced in R2021a
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)