In[]:=
$DropBoxPath="/home/sw/Dropbox/GeneralBox/Research/LLMScience/";
In[]:=
unet=Import[FileNameJoin[{$DropBoxPath,"FromNik","Parameters","Stable-Diffusion-V1","Unet.wlnet"}]];
In[]:=
layers=Information[unet,"Layers"];
In[]:=
graph=VertexDelete[Information[unet,"LayersGraph"],_Missing];
In[]:=
tlayers=Select[layers,MatchQ[_ConvolutionLayer|_LinearLayer|_NormalizationLayer]];
In[]:=
graph=Graph[graph,VertexStyle->Normal[Replace[layers,{_ConvolutionLayer->Red,_LinearLayer->Blue,_NormalizationLayer->Green,_->White},{1}]],AspectRatio->1/3,VertexSize->5];
In[]:=
graphEmbedding=AssociationThread[VertexList[graph],GraphEmbedding[graph]];
In[]:=
legend=PointLegend[{Red,Blue,Green,White},{ConvolutionLayer,LinearLayer,NormalizationLayer,"Other"},LegendMarkersGraphics[{EdgeForm[{Thick,Black}],Disk[]}]];
In[]:=
Block[{randomLayerIndices,layerNames,knockoutImages},randomLayerIndices=BlockRandom[SeedRandom[4];Catenate@Lookup[PositionIndex[Keys[tlayers]],Catenate[RandomChoice[Keys@Select[tlayers,MatchQ[#]],3]&/@{_ConvolutionLayer,_LinearLayer,_NormalizationLayer}]]];layerNames=Keys[tlayers][[randomLayerIndices]];knockoutImages=Import@FileNameJoin[{$DropBoxPath,"SWData","Knockouts1Random",StringTemplate["``.png"][#]}]&/@randomLayerIndices;Legended[Show[graph,ListPlot[MapThread[Callout[graphEmbedding[#1],#2,LeaderSize->64,CalloutStyle->#3,CalloutMarker->"Circle",LabelVisibility->All]&,{layerNames,knockoutImages,Join[Table[Red,3],Table[Blue,3],Table[Green,3]]}],PlotStyle->Transparent,LabelingSize->35]],legend]]
Out[]=
In[]:=
With[{ct=5},Block[{randomLayerIndices,layerNames,knockoutImages},randomLayerIndices=BlockRandom[SeedRandom[4];Catenate@Lookup[PositionIndex[Keys[tlayers]],Catenate[RandomChoice[Keys@Select[tlayers,MatchQ[#]],ct]&/@{_ConvolutionLayer,_LinearLayer,_NormalizationLayer}]]];layerNames=Keys[tlayers][[randomLayerIndices]];knockoutImages=Import@FileNameJoin[{$DropBoxPath,"SWData","Knockouts1Random",StringTemplate["``.png"][#]}]&/@randomLayerIndices;Legended[Show[graph,ListPlot[MapThread[Callout[graphEmbedding[#1],#2,LeaderSize->100,CalloutStyle->#3,CalloutMarker->"Circle",LabelVisibility->All]&,{layerNames,knockoutImages,Join[Table[Red,ct],Table[Blue,ct],Table[Green,ct]]}],PlotStyle->Transparent,LabelingSize->55]],legend]]]
Out[]=