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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
import streamlit as st
from streamlit_mermaid import st_mermaid
from streamlit_extras.badges import badge

# Set page configuration
st.set_page_config(
    page_title="CafeDL Project | CV Journey",
    page_icon="☕",
    layout="wide",
    initial_sidebar_state="expanded",
)

# Title and introduction
st.header("☕ CafeDL - A Java Deep Learning Library")

st.markdown(
    """
### Building Neural Networks from Scratch in Java

CafeDL is a deep learning framework I developed from scratch during my Software Design undergraduate course. 
Inspired by Keras' architecture and API, it's an educational exploration of neural network fundamentals, 
implemented entirely in Java.

This project combines software engineering principles with machine learning concepts, demonstrating how
modern deep learning frameworks are designed under the hood.
"""
)

st.markdown(
    "[GitHub Repository: samuellimabraz/cafedl](https://github.com/samuellimabraz/cafedl)"
)

# Project motivation
st.markdown("---")
st.subheader("Project Motivation")

st.markdown(
    """
### Why Build a DL Framework from Scratch?

Most deep learning courses teach how to use existing frameworks like TensorFlow or PyTorch.
While valuable for practical applications, this approach often leaves engineers with knowledge gaps 
in the fundamental concepts that power these frameworks.

**By building CafeDL, I aimed to:**

- **Deepen Understanding:** Learn the mathematical foundations and computational challenges of neural networks
- **Apply Design Patterns:** Explore software architecture patterns in a complex domain
- **Bridge Engineering & ML:** Connect software engineering principles with machine learning concepts
- **Challenge Myself:** Implement gradient descent, backpropagation, convolutional operations, and more without relying on existing libraries
"""
)


# Technology Stack
st.markdown("---")
st.subheader("Technology Stack")

col1, col2 = st.columns(2)

with col1:
    st.markdown(
        """
    ### Core Technologies
    
    - **Java:** The entire library is implemented in pure Java
    - **ND4J (N-Dimensional Arrays for Java):** Used for tensor and matrix manipulation
    - **MongoDB & Morphia:** For Object Document Mapping (ODM) and persisting trained models
    - **JavaFX:** For the QuickDraw game interface
    
    ND4J provides efficient data structures similar to NumPy arrays, enabling vectorized operations
    while still implementing my own mathematical operations for learning purposes.
    """
    )

with col2:
    st.markdown(
        """
    ### Design Philosophy
    
    - **Educational Focus:** Prioritizes readability and understanding over raw performance
    - **Object-Oriented Design:** Heavy use of design patterns and clean architecture
    - **API Familiarity:** Interface inspired by Keras for intuitive model building
    - **Modularity:** Components are designed to be mixed and matched
    - **Extensibility:** Easy to add new layers, optimizers, and activation functions
    """
    )

# Key features
st.markdown("---")
st.subheader("Key Features of CafeDL")

col1, col2 = st.columns(2)

with col1:
    st.markdown(
        """
    ### Neural Network Components
    
    - **Layers:**
      - Dense (Fully Connected)
      - Convolutional 2D
      - Max Pooling 2D
      - Zero Padding 2D
      - Flattening
      - Dropout
    
    - **Activation Functions:**
      - ReLU
      - Leaky ReLU 
      - SiLU (Sigmoid Linear Unit)
      - Sigmoid
      - Tanh
      - Softmax
      - Linear
    
    - **Loss Functions:**
      - Mean Squared Error (MSE)
      - Binary Cross-Entropy
      - Categorical Cross-Entropy
      - Softmax Cross-Entropy
    """
    )

with col2:
    st.markdown(
        """
    ### Training Components
    
    - **Optimizers:**
      - SGD (Stochastic Gradient Descent)
      - SGD with Momentum
      - SGD with Nesterov Momentum
      - Regularized SGD
      - Adam
      - RMSProp
      - AdaGrad
      - AdaDelta
    
    - **Learning Rate Strategies:**
      - Linear Decay
      - Exponential Decay
    
    - **Metrics:**
      - Accuracy
      - Precision
      - Recall
      - F1 Score
      - MSE, RMSE, MAE
      - R²
    """
    )

# Data Processing Features
st.markdown("---")
st.subheader("Data Processing Features")

st.markdown(
    """
### Comprehensive Data Pipeline

- **Data Loading:** Utility functions to load and preprocess training/testing data
- **Preprocessing:** Tools for data normalization and transformation
  - StandardScaler
  - MinMaxScaler
  - One-hot encoding
- **Visualization:** Functions to plot model predictions and training performance
- **Image Utilities:** Convert between array and image formats
"""
)

# Example usage code
st.markdown("---")

# Add a more complete model example from the README
st.markdown("### Example: Building & Training a CNN Model")

st.code(
    """
DataLoader dataLoader = new DataLoader(root + "/npy/train/x_train250.npy", root + "/npy/train/y_train250.npy", root + "/npy/test/x_test250.npy", root + "/npy/test/y_test250.npy");
INDArray xTrain = dataLoader.getAllTrainImages().get(NDArrayIndex.interval(0, trainSize));
INDArray yTrain = dataLoader.getAllTrainLabels().reshape(-1, 1).get(NDArrayIndex.interval(0, trainSize));
INDArray xTest = dataLoader.getAllTestImages().get(NDArrayIndex.interval(0, testSize));
INDArray yTest = dataLoader.getAllTestLabels().reshape(-1, 1).get(NDArrayIndex.interval(0, testSize));

// Normalization
xTrain = xTrain.divi(255);
xTest = xTest.divi(255);
// Reshape
xTrain = xTrain.reshape(xTrain.rows(), 28, 28, 1);
xTest = xTest.reshape(xTest.rows(), 28, 28, 1);

NeuralNetwork model = new ModelBuilder()
    .add(new Conv2D(32, 2, Arrays.asList(2, 2), "valid", Activation.create("relu"), "he"))
    .add(new Conv2D(16, 1, Arrays.asList(1, 1), "valid", Activation.create("relu"), "he"))
    .add(new Flatten())
    .add(new Dense(178, Activation.create("relu"), "he"))
    .add(new Dropout(0.4))
    .add(new Dense(49, Activation.create("relu"), "he"))
    .add(new Dropout(0.3))
    .add(new Dense(numClasses,  Activation.create("linear"), "he"))
    .build();

int epochs = 20;
int batchSize = 64;

LearningRateDecayStrategy lr = new ExponentialDecayStrategy(0.01, 0.0001, epochs);
Optimizer optimizer = new RMSProp(lr);
Trainer trainer = new TrainerBuilder(model, xTrain, yTrain, xTest, yTest, new SoftmaxCrossEntropy())
        .setOptimizer(optimizer)
        .setBatchSize(batchSize)
        .setEpochs(epochs)
        .setEvalEvery(2)
        .setEarlyStopping(true)
        .setPatience(4)
        .setMetric(new Accuracy())
        .build();
trainer.fit();
""",
    language="java",
)

# Application: QuickDraw Game
st.markdown("---")
st.subheader("Application: QuickDraw Game Clone")

col1, col2 = st.columns(2)

with col1:
    st.markdown(
        """
    ### Project Demo: Real-time Drawing Recognition
    
    As a demonstration of CafeDL's capabilities, I developed a JavaFX application inspired by Google's QuickDraw game.
    
    **Features:**
    
    - Real-time classification of hand-drawn sketches
    - Drawing canvas with intuitive UI
    - Displays model confidence for each class
    - User feedback and game mechanics
    - 10 different object categories for classification
    - Database integration to store drawings and game sessions
    
    **Technical Implementation:**
    
    - **CNN Model:** Trained using CafeDL on the QuickDraw dataset
    - **Game Logic:** 4 rounds per session, requiring >50% confidence for success
    - **Database:** MongoDB for storing drawings and game statistics
    - **MVC Architecture:** Clean separation of game components using JavaFX
    """
    )

with col2:
    # Placeholder for QuickDraw game video
    st.video(
        "assets/quickdraw_game_video.mp4",
    )

# Regression Examples
st.markdown("---")
st.subheader("Regression Examples")

st.markdown(
    """
### Function Approximation Capabilities

CafeDL isn't limited to classification problems. It can also tackle regression tasks, approximating various functions:

- Linear regression
- Sine waves
- Complex 3D surfaces like Rosenbrock and Saddle functions

Example visualizations from the original project include:
"""
)


col1, col2 = st.columns(2)
col3, col4 = st.columns(2)

with col1:
    st.markdown("#### Saddle Function")
    st.image("assets/saddle_function2.png", use_container_width=True)

with col2:
    st.markdown("#### Rosenbrock Function")
    st.image("assets/rosenbrock2.png", use_container_width=True)

with col3:
    st.markdown("#### Sine Function")
    st.image("assets/sine.png", use_container_width=True)

with col4:
    st.markdown("#### Linear Regression")
    st.image("assets/linear.png", use_container_width=True)


# UML Diagrams with Mermaid
st.markdown("---")
st.subheader("CafeDL Architecture: UML Diagrams")

# Create all diagram file paths
diagram_files = {
    "Full Diagram": "assets/full_diagram.md",
    "Layers": "assets/layers_diagram.md",
    "Activations": "assets/activations_diagram.md",
    "Models": "assets/models_diagram.md",
    "Optimizers": "assets/optimizers_diagram.md",
    "Losses": "assets/losses_diagram.md",
    "Metrics": "assets/metrics_diagram.md",
    "Data": "assets/data_diagram.md",
    "Database": "assets/database_diagram.md",
    "Train": "assets/train_diagram.md",
}


# Function to extract Mermaid diagram text from .md files
def extract_mermaid_from_md(file_path):
    try:
        with open(file_path, "r") as file:
            content = file.read()
            # Extract the Mermaid content between the ```mermaid and ``` tags
            if "```mermaid" in content and "```" in content:
                start_idx = content.find("```mermaid") + len("```mermaid")
                end_idx = content.rfind("```")
                return content[start_idx:end_idx].strip()
            return None
    except Exception as e:
        st.error(f"Error reading diagram file {file_path}: {e}")
        return None


# Display diagrams sequentially
st.info(
    "The following UML diagrams represent the architecture of the CafeDL framework, organized by namespace."
)

# For each diagram, display it sequentially
for name, file_path in diagram_files.items():
    st.markdown(f"### {name}")
    st.markdown(f"UML class diagram showing the {name.lower()} structure.")

    mermaid_content = extract_mermaid_from_md(file_path)

    if mermaid_content:
        st_mermaid(mermaid_content)
    else:
        st.warning(f"Could not load diagram from {file_path}")

        # Fallback diagrams for common namespace views
        if name == "Activations":
            st_mermaid(
                """
                classDiagram
                    class IActivation {
                        <<interface>>
                        +forward(input): INDArray
                        +backward(gradient): INDArray
                    }
                    
                    class Sigmoid {
                        +forward(input): INDArray
                        +backward(gradient): INDArray
                    }
                    
                    class TanH {
                        +forward(input): INDArray
                        +backward(gradient): INDArray
                    }
                    
                    class ReLU {
                        +forward(input): INDArray
                        +backward(gradient): INDArray
                    }
                    
                    class LeakyReLU {
                        -alpha: double
                        +forward(input): INDArray
                        +backward(gradient): INDArray
                    }
                    
                    class Linear {
                        +forward(input): INDArray
                        +backward(gradient): INDArray
                    }
                    
                    class SiLU {
                        +forward(input): INDArray
                        +backward(gradient): INDArray
                    }
                    
                    class Softmax {
                        +forward(input): INDArray
                        +backward(gradient): INDArray
                    }
                    
                    IActivation <|.. Sigmoid
                    IActivation <|.. TanH
                    IActivation <|.. ReLU
                    IActivation <|.. LeakyReLU
                    IActivation <|.. Linear
                    IActivation <|.. SiLU
                    IActivation <|.. Softmax
                """
            )
        elif name == "Layers":
            st_mermaid(
                """
                classDiagram
                    class Layer {
                        <<abstract>>
                        #inputShape: int[]
                        #outputShape: int[]
                        +forward(input): double[][]
                        +backward(gradient): double[][]
                        +getParameters(): Map<String, double[][]>
                        +updateParameters(optimizer): void
                    }
                    
                    class TrainableLayer {
                        <<abstract>>
                        #params: INDArray
                        #grads: INDArray
                        #trainable: boolean
                        +setup(input): void
                        +getParams(): INDArray
                        +getGrads(): INDArray
                    }
                    
                    class Dense {
                        -weights: INDArray
                        -bias: INDArray
                        -activation: IActivation
                        +Dense(units, activation)
                        +forward(input): INDArray
                        +backward(gradient): INDArray
                    }
                    
                    class Conv2D {
                        -filters: INDArray
                        -biases: INDArray
                        -kernelSize: int[]
                        -strides: int[]
                        -padding: String
                        -activation: IActivation
                        +Conv2D(filters, kernelHeight, kernelWidth, activation, padding)
                        +forward(input): INDArray
                        +backward(gradient): INDArray
                    }
                    
                    class MaxPooling2D {
                        -poolSize: int[]
                        -strides: int[]
                        +MaxPooling2D(poolHeight, poolWidth, strides)
                        +forward(input): INDArray
                        +backward(gradient): INDArray
                    }
                    
                    class Flatten {
                        +forward(input): INDArray
                        +backward(gradient): INDArray
                    }
                    
                    class Dropout {
                        -rate: double
                        -mask: INDArray
                        +Dropout(rate)
                        +forward(input): INDArray
                        +backward(gradient): INDArray
                    }
                    
                    class ZeroPadding2D {
                        -padding: int
                        +ZeroPadding2D(padding)
                        +forward(input): INDArray
                        +backward(gradient): INDArray
                    }
                    
                    Layer <|-- TrainableLayer
                    TrainableLayer <|-- Dense
                    TrainableLayer <|-- Conv2D
                    Layer <|-- MaxPooling2D
                    Layer <|-- Flatten
                    Layer <|-- Dropout
                    Layer <|-- ZeroPadding2D
                    Conv2D --> ZeroPadding2D : uses
                """
            )
        elif name == "Optimizers":
            st_mermaid(
                """
                classDiagram
                    class Optimizer {
                        <<abstract>>
                        #learningRate: double
                        #neuralNetwork: NeuralNetwork
                        +update(): void
                        +updateEpoch(): void
                        #createAuxParams(params): List<INDArray>
                        #updateRule(params, grads, auxParams): void
                    }
                    
                    class LearningRateDecayStrategy {
                        <<abstract>>
                        #decayPerEpoch: double
                        #learningRate: double
                        +updateLearningRate(): double
                    }
                    
                    class SGD {
                        +SGD(learningRate)
                        #updateRule(params, grads, auxParams): void
                    }
                    
                    class SGDMomentum {
                        -momentum: double
                        +SGDMomentum(learningRate, momentum)
                        #updateRule(params, grads, auxParams): void
                    }
                    
                    class Adam {
                        -beta1: double
                        -beta2: double
                        -epsilon: double
                        +Adam(learningRate, beta1, beta2, epsilon)
                        #updateRule(params, grads, auxParams): void
                    }
                    
                    class RMSProp {
                        -decayRate: double
                        -epsilon: double
                        +RMSProp(learningRate, decayRate, epsilon)
                        #updateRule(params, grads, auxParams): void
                    }
                    
                    Optimizer <|-- SGD
                    Optimizer <|-- SGDMomentum
                    Optimizer <|-- Adam
                    Optimizer <|-- RMSProp
                    
                    LearningRateDecayStrategy <|-- ExponentialDecayStrategy
                    LearningRateDecayStrategy <|-- LinearDecayStrategy
                    
                    Optimizer o-- LearningRateDecayStrategy
                """
            )

    # Add a separator between diagrams
    st.markdown("---")

# Technical challenges
st.markdown("---")

st.markdown(
    """
### Key Learning Outcomes

- **Deep Understanding of Neural Networks:** Gained insights into the mathematical foundations of deep learning
- **Software Design Patterns:** Applied OOP principles to a complex domain
- **Algorithm Implementation:** Translated mathematical concepts into efficient code
- **Performance Optimization:** Balanced readability with computational efficiency
- **Full Stack Development:** Combined ML models with UI and database components
- **Documentation & API Design:** Created an intuitive interface for users
"""
)

# Conclusion and future work
st.markdown("---")
st.subheader("Conclusion & Future Work")

st.markdown(
    """
### Project Impact & Next Steps

The CafeDL project successfully demonstrates how modern deep learning frameworks function internally,
while providing a practical educational tool for exploring neural network concepts.

**Future Enhancements:**
- Additional layer types (LSTM, GRU, BatchNorm)
- More optimization algorithms
- Transfer learning capabilities
- Data augmentation pipeline
- Enhanced visualization tools
- Performance optimizations

This project represents the intersection of software engineering principles and machine learning concepts,
providing a foundation for deeper exploration of both fields.
"""
)

badge(
    type="github",
    name="samuellimabraz/cafedl",
    url="https://github.com/samuellimabraz/cafedl",
)