Skip to content

Commit ac5316a

Browse files
allnesaobolensk
andauthored
Update dl tech intro, export flow, and performance optimization (#46)
* Update lecture slides: enhanced content on data pipelines, artifact details, export flow, and performance optimization * Enhance lecture slides: improve formatting, add typographic corrections, and refine content for clarity and consistency * Update 06-dl-tech-intro/06-dl-tech-intro.tex * Update 06-dl-tech-intro/06-dl-tech-intro.tex --------- Co-authored-by: Arseniy Obolenskiy <gooddoog@student.su>
1 parent f77eea9 commit ac5316a

1 file changed

Lines changed: 271 additions & 54 deletions

File tree

Lines changed: 271 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,27 @@
11
\documentclass{beamer}
2-
3-
% Theme choice
42
\usetheme{Madrid}
3+
\usepackage{graphicx}
4+
\usepackage{amsmath}
5+
\usepackage{hyperref}
56

6-
% Optional packages
7-
\usepackage{graphicx} % For including images
8-
\usepackage{amsmath} % For math symbols and formulas
9-
\usepackage{hyperref} % For hyperlinks
10-
11-
\title[Deep Learning Tech Introduction]{Deep Learning Tech Introduction}
7+
\title[CV to OpenVINO]{From Images to OpenVINO Inference}
128
\author{Nesterov Alexander, Obolenskiy Arseniy}
139
\institute{ITLab}
14-
1510
\date{\today}
1611

17-
% Redefine the footline to display both the short title and the org name
1812
\setbeamertemplate{footline}{
19-
\leavevmode%
13+
\leavevmode
2014
\hbox{%
21-
\begin{beamercolorbox}[wd=.45\paperwidth,ht=2.5ex,dp=1ex,leftskip=1em,center]{author in head/foot}%
22-
\usebeamerfont{author in head/foot}\insertshortinstitute% Displays the university name
15+
\begin{beamercolorbox}[wd=.45\paperwidth,ht=2.5ex,dp=1ex,leftskip=1em,center]{author in head/foot}
16+
\usebeamerfont{author in head/foot}\insertshortinstitute%
2317
\end{beamercolorbox}%
24-
\begin{beamercolorbox}[wd=.45\paperwidth,ht=2.5ex,dp=1ex,leftskip=1em,center]{author in head/foot}%
25-
\usebeamerfont{author in head/foot}\insertshorttitle% Displays the short title
18+
\begin{beamercolorbox}[wd=.45\paperwidth,ht=2.5ex,dp=1ex,leftskip=1em,center]{author in head/foot}
19+
\usebeamerfont{author in head/foot}\insertshorttitle%
2620
\end{beamercolorbox}%
27-
\begin{beamercolorbox}[wd=.1\paperwidth,ht=2.5ex,dp=1ex,rightskip=1em,center]{author in head/foot}%
21+
\begin{beamercolorbox}[wd=.10\paperwidth,ht=2.5ex,dp=1ex,rightskip=1em,center]{author in head/foot}
2822
\usebeamerfont{author in head/foot}\insertframenumber{} / \inserttotalframenumber%
29-
\end{beamercolorbox}}%
30-
\vskip0pt%
23+
\end{beamercolorbox}}
24+
\vskip0pt
3125
}
3226

3327
\AtBeginSection[]{
@@ -40,58 +34,281 @@
4034
\begin{document}
4135

4236
\begin{frame}
43-
\titlepage%
37+
\titlepage%
4438
\end{frame}
4539

4640
\begin{frame}{Contents}
47-
\tableofcontents
41+
\small
42+
\tableofcontents[hideallsubsections]
43+
\end{frame}
44+
45+
\section{Roadmap}
46+
47+
\begin{frame}{Goal for Today}
48+
\begin{itemize}
49+
\item Follow the path: \textbf{image $\rightarrow$ tensor $\rightarrow$ model artifact $\rightarrow$ ONNX $\rightarrow$ OpenVINO IR}.
50+
\item Keep only the math needed to reason about layout, shape, and precision.
51+
\item Learn how to check each hand-off before sending the model to OpenVINO\@.
52+
\end{itemize}
53+
\end{frame}
54+
55+
\begin{frame}{Guiding Questions}
56+
\begin{itemize}
57+
\item What are the exact inputs and outputs?
58+
\item Where do layout or dtype changes happen, and do we log them?
59+
\item How do we prove ONNX/OpenVINO outputs match PyTorch?
60+
\item Which steps dominate latency, memory, or accuracy?
61+
\end{itemize}
62+
\end{frame}
63+
64+
\begin{frame}{If This Is New\ldots}
65+
\begin{itemize}
66+
\item You only need basic linear algebra (vectors, dot products) and the idea of a function.
67+
\item A \textbf{tensor} is just a multi-dimensional array; we care about its size and data type.
68+
\item Layout words (NCHW, NHWC) simply describe the axis order.
69+
\item Keep these mental anchors and the rest of the lecture will feel less abstract.
70+
\end{itemize}
71+
\end{frame}
72+
73+
\section{Artifact and Interfaces}
74+
75+
\begin{frame}{Model = Portable Artifact}
76+
\begin{columns}
77+
\begin{column}{0.55\textwidth}
78+
\begin{itemize}
79+
\item Deliverable = graph + weights + fixed input/output description.
80+
\item Pre/post-processing belong to the contract, not misc code.
81+
\item Same bundle, same answer: store versions and short notes with it.
82+
\item Training notebooks can change; exported artifact must stay reproducible.
83+
\end{itemize}
84+
\end{column}
85+
\begin{column}{0.45\textwidth}
86+
\includegraphics[width=\textwidth]{images/dnn.png}
87+
\end{column}
88+
\end{columns}
89+
\end{frame}
90+
91+
\begin{frame}{Anatomy of the Artifact}
92+
\begin{itemize}
93+
\item \textbf{Graph}: list of layers (operations) and how tensors flow between them.
94+
\item \textbf{Weights}: numbers learned during training; stored as tensors too.
95+
\item \textbf{I/O signature}: tensor shapes, layouts, dtypes, and names.
96+
\item \textbf{Metadata}: version, training recipe hash, preprocessing notes.
97+
\item Bundle them once and reuse the same bundle everywhere.
98+
\end{itemize}
99+
\end{frame}
100+
101+
\begin{frame}{Inputs and Outputs}
102+
\begin{itemize}
103+
\item Input tensor: $\mathrm{images} \in \mathbb{R}^{N\times3\times H\times W}$, layout NCHW, dtype FP32.
104+
\item Normalize per channel: $x' = (x/255 - \mu)/\sigma$, $\mu=[0.485,0.456,0.406]$, $\sigma=[0.229,0.224,0.225]$.
105+
\item Example: single RGB photo $224\times224$ becomes tensor $[1,3,224,224]$ after preprocessing.
106+
\item Output tensor: $\mathrm{logits} \in \mathbb{R}^{N\times C}$ for softmax/argmax.
107+
\item Changing layout, dtype, or scale = effectively a different model.
108+
\end{itemize}
109+
\end{frame}
110+
111+
\begin{frame}{Tensors and Memory}
112+
\begin{itemize}
113+
\item Memory footprint = elements $\times$ bytes per element.
114+
\item Example: $[1,3,224,224]$ FP32 $=1\cdot3\cdot224\cdot224\cdot4\approx0.6$ MB\@.
115+
\item FP32 (32-bit float) uses 4 bytes; FP16/BF16 use 2 bytes; INT8 uses 1 byte.
116+
\item FP16 halves activations and weights; INT8 shrinks by $\times4$ vs FP32.
117+
\item Keep mental math handy to size batches for GPUs, VPUs, and CPUs.
118+
\end{itemize}
119+
\end{frame}
120+
121+
\begin{frame}{Shape Control}
122+
\begin{columns}
123+
\begin{column}{0.55\textwidth}
124+
\begin{itemize}
125+
\item Typical block: Conv $\rightarrow$ Activation $\rightarrow$ Pool $\rightarrow$ Linear.
126+
\item Conv2D height:
127+
\begin{equation*}
128+
H_{out}=\left\lfloor\frac{H_{in}+2p-d(k-1)-1}{s}\right\rfloor+1
129+
\end{equation*}
130+
\item $p$ = padding, $k$ = kernel, $s$ = stride, $d$ = dilation.
131+
\item Example: $H_{in}=224$, $k=3$, $p=1$, $s=2$, $d=1$ $\Rightarrow H_{out}=112$.
132+
\item Shape logs prevent silent layout swaps when exporting.
133+
\end{itemize}
134+
\end{column}
135+
\begin{column}{0.45\textwidth}
136+
\includegraphics[width=\textwidth]{images/lenet.png}
137+
\end{column}
138+
\end{columns}
139+
\end{frame}
140+
141+
\begin{frame}{Where FLOPs Hide}
142+
\begin{itemize}
143+
\item Conv MACs (multiply-accumulates): $H_{out} W_{out} C_{out} (C_{in} k_H k_W / \text{groups})$.
144+
\item Fully connected (GEMM): $O(MNK)$; later layers dominate channels, early layers dominate spatial size.
145+
\item To save work: shrink spatial size early (stride/pool), use smaller kernels, or fewer channels.
146+
\item Estimate FLOPs before choosing hardware or batching strategy.
147+
\end{itemize}
148+
\end{frame}
149+
150+
\section{Data Pipeline}
151+
152+
\begin{frame}{Data Pipeline}
153+
\framesubtitle{Preprocessing}
154+
\begin{columns}
155+
\begin{column}{0.5\textwidth}
156+
\begin{enumerate}
157+
\item Resize keeping aspect, then center-crop $224\times224$.
158+
\item Convert to RGB, scale to $[0,1]$, normalize per channel.
159+
\item Reorder HWC $\rightarrow$ CHW, append batch dim.
160+
\item Log resulting shape/range for every batch.
161+
\end{enumerate}
162+
\end{column}
163+
\begin{column}{0.5\textwidth}
164+
\includegraphics[width=\textwidth]{images/inference.png}
165+
\end{column}
166+
\end{columns}
167+
\end{frame}
168+
169+
\begin{frame}{Data Pipeline}
170+
\framesubtitle{Postprocessing}
171+
\begin{itemize}
172+
\item Apply softmax with max-shift for numerical stability.
173+
\item Report top-$k$ classes plus confidence for audits.
174+
\item Keep \texttt{labels.txt} in sync with training order; mismatched indices produce wrong answers.
175+
\item Optional: threshold logits before argmax for ``unknown'' class.
176+
\end{itemize}
177+
\end{frame}
178+
179+
\begin{frame}{One Pipeline for All Runtimes}
180+
\begin{itemize}
181+
\item Share the same \texttt{preprocess}\texttt{()}/\texttt{postprocess}\texttt{()} code in training, ONNX tests, and OpenVINO demos.
182+
\item Lock color space (RGB/BGR), resize mode, crop policy, and mean/std arrays in one config.
183+
\item Emit logs with tensor shapes and min/max values at every boundary.
184+
\item Most production bugs come from mismatched preprocessing rather than from the network graph.
185+
\end{itemize}
48186
\end{frame}
49187

50-
\section{Deep neural network}
188+
\section{Export and Inspection}
51189

52-
\begin{frame}{Deep neural network}
53-
\begin{figure}[h]
54-
\includegraphics[width=1\textwidth]{images/dnn.png}
55-
\end{figure}
190+
\begin{frame}{ONNX in the Toolchain}
191+
\begin{columns}
192+
\begin{column}{0.55\textwidth}
193+
\begin{itemize}
194+
\item Open Neural Network Exchange = neutral graph + opset version.
195+
\item Lets PyTorch, TensorFlow, and JAX share the same artifact.
196+
\item Opset = versioned operator list; runtimes only implement certain versions.
197+
\item Inspect graphs with Netron or \texttt{onnxsim} before conversion.
198+
\end{itemize}
199+
\end{column}
200+
\begin{column}{0.45\textwidth}
201+
\includegraphics[width=0.9\textwidth]{images/frameworks.png}
202+
\end{column}
203+
\end{columns}
56204
\end{frame}
57205

58-
\section{What we can to do with DNN?}
59-
\begin{frame}{What we can to do with DNN?}
60-
\begin{figure}[h]
61-
\includegraphics[width=1\textwidth]{images/inference.png}
62-
\end{figure}
206+
\begin{frame}{ONNX to IR}
207+
\begin{columns}
208+
\begin{column}{0.6\textwidth}
209+
\begin{itemize}
210+
\item Model Optimizer converts ONNX to XML + BIN Intermediate Representation.
211+
\item XML stores the graph, BIN stores the weights—keep them together.
212+
\item Conversion folds constants, rewrites layouts, and attaches device hints.
213+
\item Keep opset/precision matrix handy before targeting edge hardware.
214+
\end{itemize}
215+
\end{column}
216+
\begin{column}{0.4\textwidth}
217+
\includegraphics[width=0.9\textwidth]{images/small_ir.png}
218+
\end{column}
219+
\end{columns}
220+
\end{frame}
221+
222+
\begin{frame}{Visual Debugging}
223+
\begin{columns}
224+
\begin{column}{0.55\textwidth}
225+
\begin{itemize}
226+
\item Netron (free desktop/web viewer) shows tensor dims, kernel sizes, and data types in seconds.
227+
\item Use screenshots for before/after layout fixes.
228+
\item Pair with tensor dumps from PyTorch/ONNX/OpenVINO to locate diffs.
229+
\end{itemize}
230+
\end{column}
231+
\begin{column}{0.45\textwidth}
232+
\includegraphics[width=\textwidth]{images/openvino.png}
233+
\end{column}
234+
\end{columns}
235+
\end{frame}
236+
237+
\begin{frame}[fragile]{PyTorch \texorpdfstring{$\rightarrow$}{->} ONNX}
238+
\begin{verbatim}
239+
import torch
240+
241+
model.eval()
242+
dummy = torch.randn(1, 3, 224, 224)
243+
torch.onnx.export(
244+
model, dummy, "model.onnx", opset_version=17,
245+
input_names=["images"], output_names=["logits"],
246+
dynamic_axes={"images": {0: "N"}, "logits": {0: "N"}}
247+
)
248+
\end{verbatim}
249+
\begin{itemize}
250+
\item \texttt{eval}\texttt{()} locks BatchNorm/Dropout behavior.
251+
\item Dummy tensor must reflect real preprocessing and dtype.
252+
\item Dynamic axes keep batch size flexible without retraining.
253+
\end{itemize}
254+
\end{frame}
255+
256+
\begin{frame}{Validate the Export}
257+
\begin{itemize}
258+
\item Compare PyTorch vs ONNX Runtime on 10--20 real images.
259+
\item Metrics: top-1 match, cosine similarity (should be near 1), and L2 distance.
260+
\item Set tolerance $|y_{ref} - y_{onnx}| \le 10^{-4}$ (FP32) or tighter for FP16.
261+
\item Fix seeds and record library versions to make regressions traceable.
262+
\end{itemize}
263+
\end{frame}
264+
265+
\section{Performance and Reliability}
266+
267+
\begin{frame}{Latency and Throughput}
268+
\begin{itemize}
269+
\item Warm up 5--10 iterations before timing.
270+
\item Latency per sample: $\bar{t} = \sum \Delta t_i / K$ (average over $K$ runs).
271+
\item Throughput: $\mathrm{fps} = (N\cdot K)/\sum \Delta t_i$ ($N$ = batch size).
272+
\item Report P50/P95, mention batch size, precision, threading flags, and device.
273+
\item Batch $=1$ for real-time latency; batch $>1$ for throughput demos.
274+
\end{itemize}
63275
\end{frame}
64276

65-
\section{First network}
66-
\begin{frame}{First network}
67-
\begin{figure}[h]
68-
\includegraphics[width=1\textwidth]{images/lenet.png}
69-
\end{figure}
70-
\footnotesize Source: \href{https://blog.insightdatascience.com/convolutional-neural-networks-explained-with-american-ninja-warrior-c6649875861c}{Revisiting the LeNet-5 architecture, from “Gradient-Based Learning Applied to Document Recognition” by Yann LeCun et al., Proc. of the IEEE, 1998.}
277+
\begin{frame}{Precision and Quantization}
278+
\begin{itemize}
279+
\item FP32: baseline accuracy, highest compute and memory.
280+
\item FP16/BF16: half memory, often lossless on modern accelerators.
281+
\item INT8 PTQ\@: $q=\operatorname{round}(x/\text{scale}+z_p)$, recover via $x \approx \text{scale}(q-z_p)$.
282+
\item Mixed precision keeps sensitive ops (first/last layers, softmax) in FP32.
283+
\end{itemize}
71284
\end{frame}
72285

73-
\section{Frameworks}
74-
\begin{frame}{Frameworks}
75-
\begin{figure}[h]
76-
\includegraphics[width=0.85\textwidth]{images/frameworks.png}
77-
\end{figure}
78-
\footnotesize Source: \href{https://devopedia.org/deep-learning-frameworks}{Battle of the Deep Learning frameworks Part 1: 2017, even more frameworks and interfaces. Towards Data Science, December 19. Accessed 2019-01-20.}
286+
\begin{frame}{Calibration Set}
287+
\begin{itemize}
288+
\item Representative set = small but realistic snapshot of production data.
289+
\item Collect 100--200 domain images covering lighting, poses, object sizes.
290+
\item Reuse for INT8 calibration and regression checks after every optimization.
291+
\item Track histograms/min-max per channel to detect drift.
292+
\item Poor sampling leads to quantization drops that benchmarks might miss.
293+
\end{itemize}
79294
\end{frame}
80295

81-
\section{OpenVINO --- Small IR}
82-
\begin{frame}{OpenVINO --- Small IR}
83-
\begin{figure}[h]
84-
\includegraphics[width=0.4\textwidth]{images/small_ir.png}
85-
\end{figure}
86-
\footnotesize Source: \href{https://docs.openvino.ai/}{docs.openvino.ai}
296+
\begin{frame}{Typical Failures}
297+
\begin{itemize}
298+
\item NHWC vs NCHW swaps, wrong normalization constants, or missing batch dimension.
299+
\item Resize mismatch: center crop vs letterbox vs stretch.
300+
\item Misaligned \texttt{labels.txt} or off-by-one IDs (class names shift).
301+
\item Debug with Netron, per-layer tensor diffs, and logging of min/max values.
302+
\end{itemize}
87303
\end{frame}
88304

89-
\section{OpenVINO --- Netron}
90-
\begin{frame}{OpenVINO --- Netron}
91-
\begin{figure}[h]
92-
\includegraphics[width=0.6\textwidth]{images/openvino.png}
93-
\end{figure}
94-
\footnotesize Source: \href{https://netron.app/}{netron.app}
305+
\begin{frame}{Key Messages}
306+
\begin{itemize}
307+
\item Treat the model artifact and I/O contract as a single deliverable.
308+
\item Validate every hop: PyTorch $\rightarrow$ ONNX $\rightarrow$ OpenVINO IR\@.
309+
\item Benchmark under realistic loads and document precision choices.
310+
\item Representative data and logging catch regressions before deployment.
311+
\end{itemize}
95312
\end{frame}
96313

97314
\end{document}

0 commit comments

Comments
 (0)