Main Content

unitPredict

Perform inference using unsupervised image-to-image translation (UNIT) network

Since R2021a

Description

example

translatedImage = unitPredict(net,inputImage) performs unsupervised image-to-image translation of image inputImage using the UNIT network net.

This function requires Deep Learning Toolbox™.

example

translatedImage = unitPredict(net,inputImage,"OutputType",outputType) specifies the direction of image-to-image translation for inference using the outputType argument. The direction can be source-to-target or target-to-source.

Examples

collapse all

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)

Figure contains an axes object. The axes object contains an object of type image.

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)

Figure contains an axes object. The axes object contains an object of type image.

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

collapse all

UNIT generator network, specified as a dlnetwork (Deep Learning Toolbox) object. You can create a UNIT generator network using the unitGenerator function.

Input image for image-to-image translation, specified as a formatted dlarray (Deep Learning Toolbox) object.

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

collapse all

Inferred image after image-to-image translation, returned as a dlarray (Deep Learning Toolbox) object.

Version History

Introduced in R2021a