# sohamsurve22/maltrain architecture

> Architecture diagram of the GitHub repository sohamsurve22/maltrain, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-09-17). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/sohamsurve22/maltrain
- Repository: https://github.com/sohamsurve22/maltrain

## Overview

#### Purpose and execution model
Maltrain is a research-oriented Python pipeline for classifying Malimg malware-family images, extracting CNN embeddings, and analyzing family similarity, retrieval, and 2D projections. It is primarily script-driven rather than a unified workflow runner. Main stages execute from `scripts/cnn_training.py`, `scripts/extract_embedding.py`, and analysis scripts under `scripts/`.

#### Data preparation and dataset boundary
Image ingestion, resize/normalization, label handling, and dataset splitting are performed before model training. The processed-data contract is NumPy split arrays under `data/splits/`, consumed by training and extraction stages.
- `scripts/prepare_data.py`
- `scripts/create_test_split.py`
- `data/splits/X_test.npy`

The raw Malimg source is external (Kaggle) and is not present in the tree; repository artifacts begin at prepared splits.

#### CNN training and representation learning
A three-layer CNN learns malware-family classification and exposes a 256-dimensional embedding representation. Training outputs the deployable Keras CNN model and archived evaluation/embedding artifacts.
- `scripts/cnn_training.py`
- `src/models.py`
- `models/cnn_final_model.keras`

`src/train.py` and `src/constants.py` appear to provide reusable training/domain support, while the scripts are the practical entry points.

#### Embedding artifact layer
CNN inference converts split images into feature vectors and labels. These persisted arrays form the central interchange boundary: downstream classifiers, clustering, retrieval, and visualizations operate on embeddings rather than image tensors.
- `scripts/extract_embedding.py`
- `scripts/prepare_embeddings.py`
- `results/phase2/all_embeddings.npy`

Associated train/validation/test embeddings and label arrays in `results/phase2/` preserve split-aware evaluation and combined-population analysis.

#### Classical classification and evaluation
Embedding vectors feed independent scikit-learn classifiers—KNN, SVM, and Random Forest—for benchmark comparison against end-to-end CNN predictions. Serialized classifiers are retained for reuse.
- `scripts/train_classical_ml.py`
- `scripts/baseline_benchmark.py`
- `models/SVM_malware_classifier.pkl`

Evaluation and comparison reports are materialized under `results/`, notably `results/model_comparison.csv` and `results/test_evaluation_report.txt`.

#### Similarity, clustering, and retrieval analysis
Family-level centroid/cosine-similarity analyses construct relationship graphs and heatmaps; sample-level nearest-neighbor search produces Top-K retrieval results. This is an offline analytics layer over phase-2 embeddings, not an external search service.
- `scripts/similarity_analysis_network.py`
- `scripts/family_sim_heatmap.py`
- `scripts/topk_retrieval.py`

Family naming/knowledge is represented locally, including `app/family_knowledge.json` and `scripts/family_name.py`.

#### Visualization and delivery surfaces
Static t-SNE/UMAP and interactive Plotly visualizations translate embeddings into exploratory artifacts. The generated interactive HTML is the clearest user-facing result.
- `scripts/tsne_visualization.py`
- `scripts/interactive_visualization.py`
- `figures/interactive_malware_embeddings.html`

There is also a lightweight application surface at `app/main.py` with static content in `app/static/index.html`; its runtime dependencies are scoped in `app/requirements.txt`. Core compute relies on Python, TensorFlow/Keras, NumPy/scikit-learn, and visualization libraries; no cloud infrastructure or external runtime service is indicated.

Main entry points: [`results/phase2/all_embeddings.npy`](https://github.com/sohamsurve22/maltrain/blob/master/results/phase2/all_embeddings.npy) (Phase-2 embeddings), [`data/splits/X_test.npy`](https://github.com/sohamsurve22/maltrain/blob/master/data/splits/X_test.npy) (Processed splits), [`scripts/cnn_training.py`](https://github.com/sohamsurve22/maltrain/blob/master/scripts/cnn_training.py) (CNN training), [`models/cnn_final_model.keras`](https://github.com/sohamsurve22/maltrain/blob/master/models/cnn_final_model.keras) (Trained CNN)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_prep["Data Preparation"]
  node_raw_data[("Malimg source<br/>external dataset")]
  node_prepare_data["Prepare images<br/>Python script<br/>[prepare_data.py]"]
  node_test_split["Create test split<br/>Python script"]
  node_splits[("Processed splits<br/>NumPy arrays<br/>[X_test.npy]")]
end

subgraph group_learning["Model &amp; Embeddings"]
  node_cnn_training["CNN training<br/>Python script<br/>[cnn_training.py]"]
  node_cnn_model["Three-layer CNN<br/>Keras model<br/>[models.py]"]
  node_trained_model[("Trained CNN<br/>Keras artifact")]
  node_extract_embeddings["Extract embeddings<br/>Python script"]
  node_embedding_prep["Combine embeddings<br/>Python script"]
  node_embeddings[("Phase-2 embeddings<br/>NumPy arrays<br/>[all_embeddings.npy]")]
end

subgraph group_analysis["Evaluation &amp; Analysis"]
  node_classical_ml["Classical classifiers<br/>Python script"]
  node_classifier[("SVM classifier<br/>serialized model")]
  node_benchmark["Benchmark evaluation<br/>Python script"]
  node_reports["Comparison reports<br/>evaluation artifacts"]
  node_similarity["Family similarity<br/>Python analysis"]
  node_retrieval["Top-K retrieval<br/>Python analysis<br/>[topk_retrieval.py]"]
end

subgraph group_delivery["Delivery"]
  node_visualization["Interactive projection<br/>Python script"]
  node_interactive_html["Embedding explorer<br/>interactive HTML"]
  node_app["Lightweight app<br/>Python application<br/>[main.py]"]
  node_family_knowledge["Family knowledge<br/>JSON metadata"]
end

node_raw_data -->|"ingest"| node_prepare_data
node_prepare_data -->|"prepared data"| node_test_split
node_test_split -->|"write arrays"| node_splits
node_splits -->|"train input"| node_cnn_training
node_cnn_training -->|"uses architecture"| node_cnn_model
node_cnn_training -->|"save"| node_trained_model
node_splits -->|"inference input"| node_extract_embeddings
node_trained_model -->|"load"| node_extract_embeddings
node_extract_embeddings -->|"split embeddings"| node_embedding_prep
node_embedding_prep -->|"persist"| node_embeddings
node_embeddings -->|"features and labels"| node_classical_ml
node_classical_ml -->|"serialize"| node_classifier
node_classical_ml -->|"predictions"| node_benchmark
node_trained_model -->|"CNN predictions"| node_benchmark
node_benchmark -->|"write results"| node_reports
node_embeddings -->|"family vectors"| node_similarity
node_embeddings -->|"nearest neighbors"| node_retrieval
node_embeddings -->|"project"| node_visualization
node_visualization -->|"export"| node_interactive_html
node_family_knowledge -->|"family metadata"| node_app

click node_prepare_data "https://github.com/sohamsurve22/maltrain/blob/master/scripts/prepare_data.py"
click node_test_split "https://github.com/sohamsurve22/maltrain/blob/master/scripts/create_test_split.py"
click node_splits "https://github.com/sohamsurve22/maltrain/blob/master/data/splits/X_test.npy"
click node_cnn_training "https://github.com/sohamsurve22/maltrain/blob/master/scripts/cnn_training.py"
click node_cnn_model "https://github.com/sohamsurve22/maltrain/blob/master/src/models.py"
click node_trained_model "https://github.com/sohamsurve22/maltrain/blob/master/models/cnn_final_model.keras"
click node_extract_embeddings "https://github.com/sohamsurve22/maltrain/blob/master/scripts/extract_embedding.py"
click node_embedding_prep "https://github.com/sohamsurve22/maltrain/blob/master/scripts/prepare_embeddings.py"
click node_embeddings "https://github.com/sohamsurve22/maltrain/blob/master/results/phase2/all_embeddings.npy"
click node_classical_ml "https://github.com/sohamsurve22/maltrain/blob/master/scripts/train_classical_ml.py"
click node_classifier "https://github.com/sohamsurve22/maltrain/blob/master/models/SVM_malware_classifier.pkl"
click node_benchmark "https://github.com/sohamsurve22/maltrain/blob/master/scripts/baseline_benchmark.py"
click node_reports "https://github.com/sohamsurve22/maltrain/blob/master/results/model_comparison.csv"
click node_similarity "https://github.com/sohamsurve22/maltrain/blob/master/scripts/similarity_analysis_network.py"
click node_retrieval "https://github.com/sohamsurve22/maltrain/blob/master/scripts/topk_retrieval.py"
click node_visualization "https://github.com/sohamsurve22/maltrain/blob/master/scripts/interactive_visualization.py"
click node_interactive_html "https://github.com/sohamsurve22/maltrain/blob/master/figures/interactive_malware_embeddings.html"
click node_app "https://github.com/sohamsurve22/maltrain/blob/master/app/main.py"
click node_family_knowledge "https://github.com/sohamsurve22/maltrain/blob/master/app/family_knowledge.json"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_raw_data,node_prepare_data,node_test_split,node_splits toneBlue
class node_cnn_training,node_cnn_model,node_trained_model,node_extract_embeddings,node_embedding_prep,node_embeddings toneAmber
class node_classical_ml,node_classifier,node_benchmark,node_reports,node_similarity,node_retrieval toneMint
class node_visualization,node_interactive_html,node_app,node_family_knowledge toneRose
```

## Components

### Data Preparation

- **Malimg source**: external dataset · External Kaggle image dataset.
- [Prepare images](https://github.com/sohamsurve22/maltrain/blob/master/scripts/prepare_data.py): Python script · `scripts/prepare_data.py`
- [Create test split](https://github.com/sohamsurve22/maltrain/blob/master/scripts/create_test_split.py): Python script · `scripts/create_test_split.py`
- [Processed splits](https://github.com/sohamsurve22/maltrain/blob/master/data/splits/X_test.npy): NumPy arrays · Image tensors and labels are the training input contract. · `data/splits/X_test.npy`

### Model & Embeddings

- [CNN training](https://github.com/sohamsurve22/maltrain/blob/master/scripts/cnn_training.py): Python script · `scripts/cnn_training.py`
- [Three-layer CNN](https://github.com/sohamsurve22/maltrain/blob/master/src/models.py): Keras model · Classifier with a 256-dimensional embedding layer. · `src/models.py`
- [Trained CNN](https://github.com/sohamsurve22/maltrain/blob/master/models/cnn_final_model.keras): Keras artifact · `models/cnn_final_model.keras`
- [Extract embeddings](https://github.com/sohamsurve22/maltrain/blob/master/scripts/extract_embedding.py): Python script · `scripts/extract_embedding.py`
- [Combine embeddings](https://github.com/sohamsurve22/maltrain/blob/master/scripts/prepare_embeddings.py): Python script · Preserves split-aware and all-population arrays. · `scripts/prepare_embeddings.py`
- [Phase-2 embeddings](https://github.com/sohamsurve22/maltrain/blob/master/results/phase2/all_embeddings.npy): NumPy arrays · Central interchange boundary for downstream analytics. · `results/phase2/all_embeddings.npy`

### Evaluation & Analysis

- [Classical classifiers](https://github.com/sohamsurve22/maltrain/blob/master/scripts/train_classical_ml.py): Python script · Trains KNN, SVM, and Random Forest on embeddings. · `scripts/train_classical_ml.py`
- [SVM classifier](https://github.com/sohamsurve22/maltrain/blob/master/models/SVM_malware_classifier.pkl): serialized model · `models/SVM_malware_classifier.pkl`
- [Benchmark evaluation](https://github.com/sohamsurve22/maltrain/blob/master/scripts/baseline_benchmark.py): Python script · Compares embedding classifiers with CNN predictions. · `scripts/baseline_benchmark.py`
- [Comparison reports](https://github.com/sohamsurve22/maltrain/blob/master/results/model_comparison.csv): evaluation artifacts · `results/model_comparison.csv`
- [Family similarity](https://github.com/sohamsurve22/maltrain/blob/master/scripts/similarity_analysis_network.py): Python analysis · Builds centroid/cosine family relationships and heatmaps. · `scripts/similarity_analysis_network.py`
- [Top-K retrieval](https://github.com/sohamsurve22/maltrain/blob/master/scripts/topk_retrieval.py): Python analysis · Performs sample-level nearest-neighbor search. · `scripts/topk_retrieval.py`

### Delivery

- [Interactive projection](https://github.com/sohamsurve22/maltrain/blob/master/scripts/interactive_visualization.py): Python script · Creates Plotly embedding exploration. · `scripts/interactive_visualization.py`
- [Embedding explorer](https://github.com/sohamsurve22/maltrain/blob/master/figures/interactive_malware_embeddings.html): interactive HTML · Primary user-facing exploratory artifact. · `figures/interactive_malware_embeddings.html`
- [Lightweight app](https://github.com/sohamsurve22/maltrain/blob/master/app/main.py): Python application · Separate static application surface. · `app/main.py`
- [Family knowledge](https://github.com/sohamsurve22/maltrain/blob/master/app/family_knowledge.json): JSON metadata · `app/family_knowledge.json`

## Connections

- Malimg source → Prepare images: ingest
- Prepare images → Create test split: prepared data
- Create test split → Processed splits: write arrays
- Processed splits → CNN training: train input
- CNN training → Three-layer CNN: uses architecture
- CNN training → Trained CNN: save
- Processed splits → Extract embeddings: inference input
- Trained CNN → Extract embeddings: load
- Extract embeddings → Combine embeddings: split embeddings
- Combine embeddings → Phase-2 embeddings: persist
- Phase-2 embeddings → Classical classifiers: features and labels
- Classical classifiers → SVM classifier: serialize
- Classical classifiers → Benchmark evaluation: predictions
- Trained CNN → Benchmark evaluation: CNN predictions
- Benchmark evaluation → Comparison reports: write results
- Phase-2 embeddings → Family similarity: family vectors
- Phase-2 embeddings → Top-K retrieval: nearest neighbors
- Phase-2 embeddings → Interactive projection: project
- Interactive projection → Embedding explorer: export
- Family knowledge → Lightweight app: family metadata
