Python has generators which save memory, is there a technique for generating in memory examples for your training set "on the fly".
For example purposes, I constructed here a regressor for blur:
randomMask[img_] :=
Module[{t, h, g, d = ImageDimensions[img]},
t = Table[{PointSize@RandomReal[{0, .1}],
RandomChoice[{Point,
Rectangle[#, # + RandomReal[{-200, 200}, {2}]] &}]@
RandomPoint[Rectangle[{0, 0}, d]]}, {RandomChoice[{0, 1, 2, 3,
4, 8, 14, 20, 50, 200}]}];
g = Graphics[t, PlotRange -> Transpose[{{0, 0}, d}], ImageSize -> d];
{g, Area@DiscretizeGraphics@g/Times @@ d}]
makeExample[img_] := Module[{g, v},
{g, v} = randomMask[img];
ImageCompose[img, SetAlphaChannel[Blur[img, 15], ColorNegate@g]] ->
v
];
imgs = ConformImages[ExampleData /@ ExampleData["TestImage"], {100, 100}];
(* this is a large set that I don't want to precompute !!! *)
train = Table[makeExample@RandomChoice[imgs], {3000}]
test = Table[makeExample@RandomChoice[imgs], {500}];
convnet=NetChain[{
ConvolutionLayer[20,{5,5}],
ElementwiseLayer[Ramp],
PoolingLayer[{2,2},{2,2}],
ConvolutionLayer[50,{5,5}],
ElementwiseLayer[Ramp],
PoolingLayer[{2,2},{2,2}],
FlattenLayer[],
DotPlusLayer[500],
ElementwiseLayer[Ramp],
DotPlusLayer[50],
ElementwiseLayer[Ramp],
DotPlusLayer[1]
},
"Input"->NetEncoder[{"Image",{100,100}}],
"Output"->NetDecoder["Scalar"]
]
trainedConvnet = NetTrain[convnet, train, TargetDevice -> "GPU"]
output = trainedConvnet /@ Keys[test];
target = test // Values;
meanSquareLoss = Mean@Flatten[(#Output - #Target)^2, Infinity] &;
data = <|"Output" -> {{output}}, "Target" -> {{target}}|>;
N@meanSquareLoss@data
Comments
Post a Comment