Negative TrainedVariance error when running matlab example of image GAN(i change the code enlarge the image size from 64x64 to 128x128)

조회 수: 1 (최근 30일)
I follow this: https://www.mathworks.com/matlabcentral/answers/585146-how-to-change-gan-example-to-generate-images-with-a-larger-size to change the image size processed in GAN from 64x64 to 128x128. A guy said he can make it at the end of post but I got a negative TrainedVariance error when running it.
Here is the system error warning:
Error using nnet.internal.cnn.dlnetwork/set.State
Layer 'batchnorm_1': Invalid State. Expected TrainedVariance to be positive.
Error in dlnetwork/set.State (line 551)
net.PrivateNetworkStorage.State = values;
Error in trainGAN (line 70)
netG.State = stateG;
The error pumped out when it runs for 1 to 2 minutes.
I Googled it and tried to add those codes to replace the line netG.State = stateG;:
idx = netG.State.Parameter == "TrainedVariance";
boundAwayFromZero = @(X) max(X, eps('single'));
netG.State(idx,:) = dlupdate(boundAwayFromZero, netG.State(idx,:));
But cant work since netG.State isnt get value from stateG
  댓글 수: 3
Suhail Mahmud
Suhail Mahmud 2022년 11월 9일
Hi @Ziqi Sun, were you able to resolve the trainedVariance issue and generate images higher than 128 pixel?

댓글을 달려면 로그인하십시오.

답변 (1개)

Ayush Modi
Ayush Modi 2024년 1월 12일
Hi Ziqi,
As per my understanding, you are trying to process 128x128 images in GAN but you are getting negative TrainedVariance error while running it.
I was able to achieve this by following the answer provided by @Fred Liu in the Matlab community question you are following (https://www.mathworks.com/matlabcentral/answers/585146-how-to-change-gan-example-to-generate-images-with-a-larger-size).
Below is the code for your reference:
filterSize = 5;
numFilters = 128;
numLatentInputs = 100;
projectionSize = [4 4 512];
layersGenerator = [
featureInputLayer(numLatentInputs)
projectAndReshapeLayer(projectionSize)
transposedConv2dLayer(filterSize,8*numFilters)
batchNormalizationLayer
reluLayer
transposedConv2dLayer(filterSize,4*numFilters,Stride=2,Cropping="same")
batchNormalizationLayer
reluLayer
transposedConv2dLayer(filterSize,2*numFilters,Stride=2,Cropping="same")
batchNormalizationLayer
reluLayer
transposedConv2dLayer(filterSize,numFilters,Stride=2,Cropping="same")
batchNormalizationLayer
reluLayer
transposedConv2dLayer(filterSize,3,Stride=2,Cropping="same")
tanhLayer];
netG = dlnetwork(layersGenerator);
dropoutProb = 0.5;
numFilters = 128;
scale = 0.2;
inputSize = [128 128 3];
filterSize = 5;
layersDiscriminator = [
imageInputLayer(inputSize,Normalization="none")
dropoutLayer(dropoutProb)
convolution2dLayer(filterSize,numFilters,Stride=2,Padding="same")
leakyReluLayer(scale)
dropoutLayer(dropoutProb)
convolution2dLayer(filterSize,2*numFilters,Stride=2,Padding="same")
batchNormalizationLayer
leakyReluLayer(scale)
dropoutLayer(dropoutProb)
convolution2dLayer(filterSize,4*numFilters,Stride=2,Padding="same")
batchNormalizationLayer
leakyReluLayer(scale)
dropoutLayer(dropoutProb)
convolution2dLayer(filterSize,8*numFilters,Stride=2,Padding="same")
batchNormalizationLayer
leakyReluLayer(scale)
convolution2dLayer(8,1)
sigmoidLayer];
netD = dlnetwork(layersDiscriminator);
I hope this helps!

카테고리

Help CenterFile Exchange에서 Image Data Workflows에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by