WSS Colorization in v13

The code from this post was experimental and no longer works: https://community.wolfram.com/groups/-/m/t/884348

Original code


Attempt to Fix

Global Variables

$α=1/300;​​$numClasses=4314; (* Image identify classes? *)

Net Layers

conv[out_Integer, k_Integer, str_Integer, p_Integer] := ConvolutionLayer[out, k, "Stride" -> str, "PaddingSize" -> p]; ​​fc[n_Integer] := LinearLayer[n];​​relu = ElementwiseLayer[Ramp]; ​​σ = ElementwiseLayer[LogisticSigmoid];​​σ1 = ElementwiseLayer[LogisticSigmoid];​​tl1 = ElementwiseLayer[100*#&]; (* Why are these two x100 needed? *)​​tl2 = ElementwiseLayer[100*#&];​​timesLoss = ElementwiseLayer[$α*#&];​​bn = BatchNormalizationLayer[]; ​​​​upSampl = ResizeLayer[{Scaled @ 2, Scaled @ 2}]; (* Are these the right fixes? *)​​sl = TransposeLayer[3->1]; ​​cl = CatenateLayer[];​​​​rshL = ReshapeLayer[{256, 1, 1}]; (* Is this the right fix? *)​​bl = CatenateLayer[InputPorts -> {"LHS", "RHS"}]; ​​​​lossMS = MeanSquaredLossLayer[]; ​​lossCE = CrossEntropyLossLayer["Index"];

Net Chains

lln = NetChain[{conv[64,3,2,1],bn,relu,conv[128,3,1,1],bn,relu,conv[128,3,2,1],bn,relu,conv[256,3,1,1],bn,relu,conv[256,3,2,1],bn,relu,conv[512,3,1,1],bn,relu}]​​mln = NetChain[{conv[512,3,1,1],bn,relu,conv[256,3,1,1],bn,relu}]​​coln = NetChain[{conv[256,3,1,1],bn,relu,conv[128,3,1,1],bn,relu,upSampl,conv[64,3,1,1],bn,relu,conv[64,3,1,1],bn,relu,upSampl,conv[32,3,1,1],bn,relu,conv[2,3,1,1],σ,upSampl}]​​gln = NetChain[{conv[512,3,2,1],bn,relu,conv[512,3,1,1],bn,relu,conv[512,3,2,1],bn,relu,conv[512,3,1,1],bn,relu,FlattenLayer[],fc[1024],bn,relu,fc[512],bn,relu}]​​gln2 = NetChain[{fc[256],bn,relu}]​​classn = NetChain[{fc[256],bn,relu,fc[$numClasses],bn,relu}]
Out[]=
NetChain

uniniti
aliz
ed
Input
port:
matrix
(rank≥1)
Output
port:
array

Out[]=
NetChain

uniniti
aliz
ed
Input
port:
matrix
(rank≥1)
Output
port:
array

Out[]=
NetChain

uniniti
aliz
ed
Input
port:
matrix
(rank≥1)
Output
port:
array
(size: 2××)

Out[]=
NetChain

uniniti
aliz
ed
Input
port:
matrix
(rank≥1)
Output
port:
vector
(size: 512)

Out[]=
NetChain

uniniti
aliz
ed
Input
port:
array
Output
port:
vector
(size: 256)

Out[]=
NetChain

uniniti
aliz
ed
Input
port:
array
Output
port:
vector
(size: 4314)


Net Structure (broken)

classNet = NetGraph[​​ <| "SplitL" -> sl, "LowLev" -> lln, "MidLev" -> mln, "GlobLev" -> gln, "GlobLev2" -> gln2, ​​ "ColNet" -> coln, "Sigmoid" -> σ1, "TimesL1" -> tl1, "TimesL2" -> tl2, "CatL" -> cl, ​​ "LossMS" -> lossMS, "LossCE" -> lossCE, "Broadcast" -> bl, "ReshapeL" -> rshL, "ClassN" -> classn, "timesLoss" -> timesLoss ​​ |>,​​ { ​​ NetPort["Image"] -> "SplitL" -> {"LowLev", "TimesL1", "TimesL2"},​​ {"TimesL1", "TimesL2"} -> "CatL" -> "Sigmoid", "LowLev" -> {"MidLev", "GlobLev"}, ​​ "GlobLev" -> {"GlobLev2", "ClassN"}, "MidLev" -> NetPort["Broadcast", "LHS"],​​ "GlobLev2" -> "ReshapeL", "ReshapeL" -> NetPort["Broadcast", "RHS"], "Broadcast" -> "ColNet",​​ "ColNet" -> NetPort["LossMS", "Input"], "Sigmoid" -> NetPort["LossMS", "Target"], ​​ "ClassN" -> NetPort["LossCE", "Input"], NetPort["Class"] -> NetPort["LossCE", "Target"], "LossCE" -> "timesLoss"​​ }, ​​ "Image" -> NetEncoder[{"Image", {224, 224}, ColorSpace -> "LAB"}] ​​]
NetGraph
:Inferred inconsistent dimensions for output of layer "CatL" (​a 2×× array of real numbers versus a 448×3×224 array of real numbers​).
Out[]=
$Failed

Training

tnet = NetTrain[ (* I'm going to use places205 and try this *)​​ classNet,​​ <|"Image" -> $trainPathsFile, "Class" -> $trainClasses|>,​​ ValidationSet -> <|"Image" -> $testPathsFile, "Class" -> $testClasses|>,​​ TargetDevice -> {"GPU", 1},​​ "Method" -> "ADAM"​​ ]

Evaluation Net

evalNet = Take[tnet, {"LowLev", "ColNet"}]​​evalNet = NetChain[{evalNet}, "Input" -> NetEncoder[{"Image", {224,224}, ColorSpace->"Grayscale"}]];