Generating the data and training process
Generating the data and training process
(*changing this will need a change in the Neural Network*)imagesize={120,120}trainingSize=5;Print["Starting the script"](* Defining the PDE*)sol[mesh_]:=NDSolve[{D[φ[x,y],y] == 0.11D[φ[x,y],x,x]-1.8,DirichletCondition[φ[x,y]==0,True]},φ[x,y],{x,y}∈mesh]Result[mesh_]:=DensityPlot[Evaluate[φ[x,y]/.sol[mesh]],{x,y}∈mesh,ClippingStyle->Automatic,ColorFunctionScaling->False,ColorFunction->ColorData[{"GrayTones",{-0.1,-0.3}}],PlotLegends->False,BoundaryStyle->None,Frame->False,ImageSize->imagesize]
(*Datacode for creating regions polygons and validation*)c0[limit_]:=ConvexHullMesh[RandomReal[limit(*max value of numbers*),{10,2}]]mesh0[limit_]:= DiscretizeRegion[c0[limit]]GenerateRaw[mesh1_]:=ImageResize[Evaluate[ColorNegate[RegionImage[mesh1]]],imagesize];(*later purposes validation*)image=Result[mesh0[1]];Print["Going to generate polygons"](*------------------------*)(*Dataset 1- Polygons *)data={};Do[c=ConvexHullMesh[RandomReal[1(*max value of numbers*),{10,2}]];mesh= DiscretizeRegion[c];rc=ColorNegate[Evaluate[RegionImage[c]]];raw=ImageResize[rc,{120,120}];data=AppendTo[data,raw->Image[Result[mesh]]];,{trainingSize}]Export["poly.data",data]Print["Generating Splines"](*------------------------*)(*splines initialize*)shape[vertices_]:=Graphics[FilledCurve[BSplineCurve@{#2,#2+(#3-#)/4,#3-(#4-#2)/4,#3}&@@@Partition[RandomReal[1,{vertices,2}],4,1,{2,2}]]]c1[v_]:=ImageMesh[ColorNegate[shape[v]]];meshc1[v_]:= DiscretizeRegion[c1[v]];GenerateRaw2[mes1_]:=ImageResize[Evaluate[ColorNegate[RegionImage[mes1]]],imagesize];Print["3 splines"](*data gen for 3 splines*)data2={};Do[cur=shape[3];mesh= DiscretizeRegion[ImageMesh[ColorNegate[cur]]];raw=ImageResize[cur,imagesize];data2=AppendTo[data2,raw->Image[Result[mesh]]];,{trainingSize}](*------------------------*)(* The neural Network *)enc=NetEncoder[{"Image",ImageDimensions[image],ColorSpace->"Grayscale"}];dec=NetDecoder[{"Image",ColorSpace->"Grayscale"}];Print["Initializing neural net"]MyNet3=NetInitialize@NetChain[{ConvolutionLayer[120,1],BatchNormalizationLayer[], ElementwiseLayer[Ramp], PoolingLayer[2,2], ConvolutionLayer[120,1],BatchNormalizationLayer[],PoolingLayer[2,2], ConvolutionLayer[60,1],BatchNormalizationLayer[],Tanh,DeconvolutionLayer[60,1],Tanh,PoolingLayer[2,1], ConvolutionLayer[60,10],BatchNormalizationLayer[],ElementwiseLayer[Ramp],PoolingLayer[2,1], ConvolutionLayer[30,1],BatchNormalizationLayer[],ElementwiseLayer[Ramp],PoolingLayer[2,2], DeconvolutionLayer[1,112]},"Input" -> enc (*encoder => image to tensor*),"Output" ->dec] (*decoder => tensor to image*)CreateDirectory["checkpoints"](*NN just trained on Poly data*)Print["Training the net"]dir="checkpoints/"]Print[dir]net3TP=NetTrain[MyNet3,data,All,TargetDevice->"GPU",ValidationSet->Scaled[0.2],MaxTrainingRounds->100,TrainingProgressCheckpointing->{"Directory","checkpoints"}]net3TP["LossEvolutionPlot"]net3TP["TotalRounds"]net3TP["TotalTrainingTime"]net3TP["InitialLearningRate"]net3TP["FinalLearningRate"]netTrainedPoly=net3TP["TrainedNet"]Export[dir<>"netTrainedPoly.wlnet",netTrainedPoly]Print["Training second net"]dir2="checkpoints2/"(*NN trained on all the data*)net3TAll=NetTrain[MyNet3,data2,All,TargetDevice->"GPU",ValidationSet->Scaled[0.2],BatchSize->128,MaxTrainingRounds->100,TrainingProgressCheckpointing->{"Directory","checkpoints2"}]net3TAll["LossEvolutionPlot"]net3TAll["TotalRounds"]net3TAll["TotalTrainingTime"]net3TAll["InitialLearningRate"]net3TAll["FinalLearningRate"]netTrainedAll=net3TAll["TrainedNet"]Export[dir2<>"netTrainedAll.wlnet",netTrainedAll]