How sohamsurve22/maltrain works
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: all_embeddings.npy Phase-2 embeddings · X_test.npy Processed splits · cnn_training.py CNN training · cnn_final_model.keras Trained CNN
Components 20
Data Preparation
- Malimg source · External Kaggle image dataset.
- Prepare images
scripts/prepare_data.py - Create test split
scripts/create_test_split.py - Processed splits · Image tensors and labels are the training input contract.
data/splits/X_test.npy
Model & Embeddings
- CNN training
scripts/cnn_training.py - Three-layer CNN · Classifier with a 256-dimensional embedding layer.
src/models.py - Trained CNN
models/cnn_final_model.keras - Extract embeddings
scripts/extract_embedding.py - Combine embeddings · Preserves split-aware and all-population arrays.
scripts/prepare_embeddings.py - Phase-2 embeddings · Central interchange boundary for downstream analytics.
results/phase2/all_embeddings.npy
Evaluation & Analysis
- Classical classifiers · Trains KNN, SVM, and Random Forest on embeddings.
scripts/train_classical_ml.py - SVM classifier
models/SVM_malware_classifier.pkl - Benchmark evaluation · Compares embedding classifiers with CNN predictions.
scripts/baseline_benchmark.py - Comparison reports
results/model_comparison.csv - Family similarity · Builds centroid/cosine family relationships and heatmaps.
scripts/similarity_analysis_network.py - Top-K retrieval · Performs sample-level nearest-neighbor search.
scripts/topk_retrieval.py
Delivery
- Interactive projection · Creates Plotly embedding exploration.
scripts/interactive_visualization.py - Embedding explorer · Primary user-facing exploratory artifact.
figures/interactive_malware_embeddings.html - Lightweight app · Separate static application surface.
app/main.py - Family knowledge
app/family_knowledge.json
Connections 20
- 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