import%20marimo%0A%0A__generated_with%20%3D%20%220.25.0%22%0Aapp%20%3D%20marimo.App()%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20import%20marimo%20as%20mo%0A%0A%20%20%20%20return%20(mo%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20METADATA%20%3D%20%7B%0A%20%20%20%20%20%20%20%20%22id%22%3A%20%22cnn%22%2C%0A%20%20%20%20%20%20%20%20%22name%22%3A%20%22Convolutional%20Neural%20Network%20(CNN)%22%2C%0A%20%20%20%20%20%20%20%20%22types%22%3A%20%5B%22architecture%22%5D%2C%0A%20%20%20%20%20%20%20%20%22families%22%3A%20%5B%22neural_networks%22%2C%20%22convolutional%22%5D%2C%0A%20%20%20%20%20%20%20%20%22tasks%22%3A%20%5B%22classification%22%2C%20%22representation%22%5D%2C%0A%20%20%20%20%20%20%20%20%22data%22%3A%20%5B%22image%22%2C%20%22video%22%5D%2C%0A%20%20%20%20%20%20%20%20%22learning%22%3A%20%5B%22supervised%22%2C%20%22self_supervised%22%5D%2C%0A%20%20%20%20%20%20%20%20%22capacity%22%3A%20%22parametric%22%2C%0A%20%20%20%20%20%20%20%20%22mechanisms%22%3A%20%5B%22backpropagation%22%2C%20%22nonlinear_activation%22%2C%20%22dense_layers%22%5D%2C%0A%20%20%20%20%20%20%20%20%22properties%22%3A%20%5B%22nonlinear%22%2C%20%22representation_learning%22%2C%20%22pretrained_ecosystem%22%5D%2C%0A%20%20%20%20%20%20%20%20%22constraints%22%3A%20%5B%22high_compute%22%2C%20%22requires_large_data%22%2C%20%22sensitive_to_tuning%22%5D%2C%0A%20%20%20%20%20%20%20%20%22difficulty%22%3A%20%22intermediate%22%2C%0A%20%20%20%20%20%20%20%20%22status%22%3A%20%22complete%22%2C%0A%20%20%20%20%20%20%20%20%22explainability%22%3A%20%22low%22%2C%0A%20%20%20%20%20%20%20%20%22training_cost%22%3A%20%22high%22%2C%0A%20%20%20%20%20%20%20%20%22inference_cost%22%3A%20%22medium%22%2C%0A%20%20%20%20%20%20%20%20%22data_appetite%22%3A%20%22high%22%2C%0A%20%20%20%20%20%20%20%20%22summary%22%3A%20%22A%20CNN%20stacks%20convolutional%20layers%20to%20turn%20pixels%20into%20features%20and%2C%20ultimately%2C%20a%20prediction.%22%2C%0A%20%20%20%20%7D%0A%20%20%20%20mo.md(f%22%23%20%7BMETADATA%5B'name'%5D%7D%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20In%20one%20sentence%0A%0A%20%20%20%20A%20CNN%20is%20a%20neural%20network%20architecture%20that%20stacks%20convolutional%20layers%20to%20gradually%20turn%20an%20image%20into%20a%20useful%20representation%20and%20then%20a%20prediction.%0A%0A%20%20%20%20%23%23%20Convolution%20versus%20the%20full%20model%0A%0A%20%20%20%20**Convolution**%20is%20the%20local%20operation%20that%20applies%20a%20filter%20to%20an%20image.%20A%20**CNN**%20is%20the%20full%20model%3A%20it%20combines%20many%20convolutions%2C%20activations%2C%20and%20downsampling%20steps%2C%20then%20trains%20them%20together%20using%20a%20loss%20function%20and%20backpropagation.%0A%0A%20%20%20%20This%20lets%20the%20model%20detect%20a%20pattern%20wherever%20it%20appears%20and%20combine%20simple%20patterns%20into%20more%20complex%20structures.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.image(%0A%20%20%20%20%20%20%20%20%22public%2Fimages%2Fcnn-architecture.png%22%2C%0A%20%20%20%20%20%20%20%20alt%3D%22CNN%20diagram%20showing%20convolutions%2C%20pooling%2C%20flattening%2C%20and%20classification%20layers.%22%2C%0A%20%20%20%20%20%20%20%20width%3D%22100%25%22%2C%0A%20%20%20%20%20%20%20%20caption%3D%22A%20classic%20CNN%20architecture%3A%20convolutional%20layers%20extract%20features%2C%20and%20the%20classification%20head%20predicts%20the%20class.%22%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Anatomy%20of%20a%20CNN%0A%0A%20%20%20%20The%20diagram%20shows%20a%20classic%20design.%20Modern%20architectures%20often%20replace%20the%20final%20layers%20with%20global%20average%20pooling%20and%20a%20small%20head%2C%20but%20the%20flow%20of%20information%20stays%20the%20same.%0A%0A%20%20%20%201.%20**Input**%20%E2%80%94%20an%20%24H%5Ctimes%20W%5Ctimes%20C%24%20image%2C%20such%20as%20%24224%5Ctimes224%5Ctimes3%24%20for%20RGB.%0A%20%20%20%202.%20**Convolution%20and%20activation%20blocks**%20%E2%80%94%20multiple%20filters%20produce%20activation%20maps%3B%20ReLU%20or%20GELU%20adds%20nonlinearity.%0A%20%20%20%203.%20**Downsampling**%20%E2%80%94%20pooling%20or%20strided%20convolution%20expands%20the%20visible%20context%20while%20reducing%20spatial%20size.%0A%20%20%20%204.%20**Representation**%20%E2%80%94%20deeper%20layers%20combine%20textures%2C%20parts%2C%20and%20richer%20configurations.%0A%20%20%20%205.%20**Task%20head**%20%E2%80%94%20classification%2C%20regression%2C%20segmentation%2C%20or%20detection%2C%20depending%20on%20the%20required%20output.%0A%0A%20%20%20%20For%20a%20layer%20with%20%24C_%7Bout%7D%24%20filters%20of%20size%20%24K%5Ctimes%20K%24%20and%20%24C_%7Bin%7D%24%20input%20channels%2C%20the%20parameter%20count%20is%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cboxed%7B(K%5E2C_%7Bin%7D%2B1)C_%7Bout%7D.%7D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20A%20filter%20shares%20its%20weights%20across%20all%20positions.%20This%20is%20why%20a%20%243%5Ctimes3%24%20layer%2C%20even%20on%20a%20large%20image%2C%20has%20far%20fewer%20parameters%20than%20a%20dense%20layer%20connected%20to%20every%20pixel.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20import%20matplotlib.pyplot%20as%20plt%0A%20%20%20%20import%20numpy%20as%20np%0A%0A%20%20%20%20return%20np%2C%20plt%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(np%2C%20plt)%3A%0A%20%20%20%20paper%2C%20ink%2C%20blue%2C%20coral%2C%20gold%2C%20grid%20%3D%20%22%23FAF7F0%22%2C%20%22%2317324D%22%2C%20%22%233A7CA5%22%2C%20%22%23E76F51%22%2C%20%22%23E9C46A%22%2C%20%22%23DCE3E8%22%0A%20%20%20%20stages%20%3D%20%5B%22input%22%2C%20%22block%201%22%2C%20%22block%202%22%2C%20%22block%203%22%2C%20%22head%22%5D%0A%20%20%20%20spatial%20%3D%20np.array(%5B224%2C%20112%2C%2056%2C%2028%2C%201%5D)%0A%20%20%20%20channels%20%3D%20np.array(%5B3%2C%2064%2C%20128%2C%20256%2C%201000%5D)%0A%20%20%20%20receptive%20%3D%20np.array(%5B1%2C%207%2C%2019%2C%2043%2C%20224%5D)%0A%0A%20%20%20%20fig%2C%20axes%20%3D%20plt.subplots(1%2C%202%2C%20figsize%3D(14%2C%204.5)%2C%20facecolor%3Dpaper)%0A%20%20%20%20for%20ax%20in%20axes%3A%0A%20%20%20%20%20%20%20%20ax.set_facecolor(paper)%0A%20%20%20%20%20%20%20%20ax.spines%5B%5B%22top%22%2C%20%22right%22%5D%5D.set_visible(False)%0A%0A%20%20%20%20axes%5B0%5D.plot(stages%2C%20spatial%2C%20marker%3D%22o%22%2C%20color%3Dblue%2C%20linewidth%3D2.6%2C%20label%3D%22spatial%20size%22)%0A%20%20%20%20axes%5B0%5D.set_ylabel(%22feature%20map%20width%20%2F%20height%22)%0A%20%20%20%20axes%5B0%5D.set_yscale(%22log%22%2C%20base%3D2)%0A%20%20%20%20axes%5B0%5D.grid(axis%3D%22y%22%2C%20color%3Dgrid)%0A%20%20%20%20twin%20%3D%20axes%5B0%5D.twinx()%0A%20%20%20%20twin.bar(stages%2C%20channels%2C%20color%3Dgold%2C%20alpha%3D0.7%2C%20label%3D%22channels%22)%0A%20%20%20%20twin.set_ylabel(%22number%20of%20channels%22)%0A%20%20%20%20axes%5B0%5D.set_title(%22Fewer%20pixels%2C%20more%20features%22%2C%20loc%3D%22left%22%2C%20color%3Dink%2C%20weight%3D%22bold%22%2C%20pad%3D12)%0A%0A%20%20%20%20axes%5B1%5D.plot(stages%2C%20receptive%2C%20marker%3D%22o%22%2C%20color%3Dcoral%2C%20linewidth%3D2.6)%0A%20%20%20%20axes%5B1%5D.fill_between(range(len(stages))%2C%20receptive%2C%20color%3Dcoral%2C%20alpha%3D0.12)%0A%20%20%20%20axes%5B1%5D.set_xticks(range(len(stages))%2C%20stages)%0A%20%20%20%20axes%5B1%5D.set_ylabel(%22receptive%20field%20width%20(pixels)%22)%0A%20%20%20%20axes%5B1%5D.grid(axis%3D%22y%22%2C%20color%3Dgrid)%0A%20%20%20%20axes%5B1%5D.set_title(%22Depth%20expands%20the%20visible%20context%22%2C%20loc%3D%22left%22%2C%20color%3Dink%2C%20weight%3D%22bold%22%2C%20pad%3D12)%0A%0A%20%20%20%20fig.suptitle(%22How%20stacking%20blocks%20changes%20a%20CNN%22%2C%20x%3D0.055%2C%20y%3D1.04%2C%20ha%3D%22left%22%2C%20color%3Dink%2C%20fontsize%3D18%2C%20weight%3D%22bold%22)%0A%20%20%20%20plt.tight_layout()%0A%20%20%20%20fig%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20How%20the%20model%20learns%0A%0A%20%20%20%20Given%20a%20batch%20of%20labeled%20images%2C%20the%20CNN%20computes%20logits%20and%20a%20loss%2C%20such%20as%20cross%20entropy.%20Backpropagation%20assigns%20a%20gradient%20to%20each%20filter%20and%20the%20head%3B%20an%20optimizer%20such%20as%20AdamW%20updates%20the%20weights.%20Filters%20are%20not%20programmed%20to%20recognize%20an%20edge%20or%20a%20wheel%3A%20they%20become%20useful%20because%20they%20help%20reduce%20the%20loss.%0A%0A%20%20%20%20For%20many%20vision%20tasks%2C%20**transfer%20learning**%20is%20an%20effective%20starting%20point%3A%20start%20with%20a%20pretrained%20CNN%2C%20replace%20its%20head%2C%20then%20fine%20tune%20part%20or%20all%20of%20the%20network%20on%20data%20close%20to%20the%20target%20task.%0A%0A%20%20%20%20%23%23%20When%20to%20use%20it%0A%0A%20%20%20%20-%20Images%2C%20video%2C%20or%20grids%20where%20spatial%20proximity%20matters.%0A%20%20%20%20-%20Image%20classification%2C%20visual%20inspection%2C%20medical%20imaging%2C%20or%20visual%20representation%20learning.%0A%20%20%20%20-%20Representative%20data%2C%20data%20augmentation%2C%20and%20GPU%20compute%20are%20available%2C%20or%20a%20suitable%20pretrained%20model%20exists.%0A%0A%20%20%20%20%23%23%20When%20to%20consider%20alternatives%0A%0A%20%20%20%20-%20Small%20tabular%20datasets%3A%20tree%20models%20often%20provide%20a%20stronger%2C%20simpler%20baseline.%0A%20%20%20%20-%20Tasks%20that%20depend%20heavily%20on%20global%20context%20or%20long%20range%20relationships%3A%20a%20Vision%20Transformer%20may%20be%20a%20stronger%20candidate.%0A%20%20%20%20-%20Very%20little%20data%20and%20no%20pretrained%20model%3A%20a%20CNN%20may%20overfit%20quickly.%0A%0A%20%20%20%20%23%23%20Practical%20checks%0A%0A%20%20%20%20Evaluate%20each%20class%2C%20not%20just%20overall%20accuracy.%20Inspect%20recurring%20errors%2C%20test%20realistic%20changes%20in%20lighting%2C%20framing%2C%20and%20resolution%2C%20monitor%20the%20gap%20between%20training%20and%20validation%2C%20and%20ensure%20image%20preprocessing%20is%20identical%20in%20training%20and%20production.%0A%0A%20%20%20%20%23%23%20Key%20takeaway%0A%0A%20%20%20%20Convolution%20is%20the%20building%20block%3B%20a%20CNN%20organizes%20these%20blocks%20to%20turn%20an%20image%20into%20a%20decision.%20For%20the%20mechanics%20of%20windows%2C%20filters%2C%20stride%2C%20and%20padding%2C%20see%20%5BConvolution%5D(%2Fconcepts%2Fconvolution).%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20app.run()%0A
49902e1dda3af6c2e9e9d483c404695f