refactor: re-structure folder
This commit is contained in:
@@ -0,0 +1,99 @@
|
||||
import sys
|
||||
import os
|
||||
import time
|
||||
|
||||
# Tambahkan root directory ke path agar dapat import dari src
|
||||
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
|
||||
|
||||
from src.data_loader import load_mnist_for_mlp
|
||||
from src.models import create_mlp_model
|
||||
|
||||
|
||||
def main():
|
||||
print("=" * 60)
|
||||
print("TUGAS 1: MEMUAT DATASET MNIST & EKSPERIMEN MLP")
|
||||
print("=" * 60)
|
||||
|
||||
# 1. Load Data
|
||||
(x_train, y_train), (x_test, y_test) = load_mnist_for_mlp()
|
||||
print(f"Data training : {x_train.shape}")
|
||||
print(f"Data testing : {x_test.shape}")
|
||||
|
||||
# 2. Konfigurasi 3 Model MLP
|
||||
models_config = {
|
||||
"Model A - 1 Hidden Layer": [64],
|
||||
"Model B - 2 Hidden Layer": [128, 64],
|
||||
"Model C - 3 Hidden Layer": [256, 128, 64]
|
||||
}
|
||||
|
||||
results = []
|
||||
EPOCHS = 10
|
||||
BATCH_SIZE = 128
|
||||
|
||||
# 3. Training & Evaluasi
|
||||
for model_name, architecture in models_config.items():
|
||||
print("\n" + "=" * 60)
|
||||
print(model_name)
|
||||
print("=" * 60)
|
||||
|
||||
model = create_mlp_model(architecture)
|
||||
model.summary()
|
||||
|
||||
print("\nMemulai training...")
|
||||
start_time = time.time()
|
||||
|
||||
model.fit(
|
||||
x_train,
|
||||
y_train,
|
||||
epochs=EPOCHS,
|
||||
batch_size=BATCH_SIZE,
|
||||
validation_split=0.1,
|
||||
verbose=1
|
||||
)
|
||||
|
||||
total_time = time.time() - start_time
|
||||
average_time = total_time / EPOCHS
|
||||
|
||||
test_loss, test_accuracy = model.evaluate(x_test, y_test, verbose=0)
|
||||
total_parameters = model.count_params()
|
||||
|
||||
results.append({
|
||||
"model": model_name,
|
||||
"parameters": total_parameters,
|
||||
"accuracy": test_accuracy * 100,
|
||||
"time_per_epoch": average_time
|
||||
})
|
||||
|
||||
print("\nHasil:")
|
||||
print(f"Test Accuracy : {test_accuracy * 100:.2f}%")
|
||||
print(f"Jumlah Parameter : {total_parameters:,}")
|
||||
print(f"Waktu/Epoch : {average_time:.2f} detik")
|
||||
|
||||
# 4. Ringkasan Perbandingan
|
||||
print("\n\n" + "=" * 80)
|
||||
print("HASIL PERBANDINGAN 3 MODEL MLP")
|
||||
print("=" * 80)
|
||||
print(f"{'Model':<30}{'Parameter':>15}{'Accuracy':>15}{'Time/Epoch':>15}")
|
||||
print("-" * 80)
|
||||
|
||||
for res in results:
|
||||
print(
|
||||
f"{res['model']:<30}"
|
||||
f"{res['parameters']:>15,}"
|
||||
f"{res['accuracy']:>14.2f}%"
|
||||
f"{res['time_per_epoch']:>14.2f}s"
|
||||
)
|
||||
|
||||
best_model = max(results, key=lambda x: x["accuracy"])
|
||||
print("\n" + "=" * 60)
|
||||
print("MODEL DENGAN TEST ACCURACY TERTINGGI")
|
||||
print("=" * 60)
|
||||
print(f"Model : {best_model['model']}")
|
||||
print(f"Test Accuracy : {best_model['accuracy']:.2f}%")
|
||||
print(f"Jumlah Parameter : {best_model['parameters']:,}")
|
||||
print(f"Waktu Training/Epoch: {best_model['time_per_epoch']:.2f} detik")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
@@ -0,0 +1,93 @@
|
||||
import sys
|
||||
import os
|
||||
import matplotlib
|
||||
matplotlib.use("Agg")
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
# Tambahkan root directory ke path agar dapat import dari src
|
||||
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
|
||||
|
||||
from src.data_loader import load_mnist_for_mlp
|
||||
from src.models import create_mlp_model, create_regularized_mlp_model
|
||||
from src.utils import ensure_dir
|
||||
|
||||
|
||||
def main():
|
||||
print("=" * 60)
|
||||
print("TUGAS 2: EKSPERIMEN REGULARISASI (BASELINE vs BATCHNORM + DROPOUT)")
|
||||
print("=" * 60)
|
||||
|
||||
# 1. Load Data
|
||||
(x_train, y_train), (x_test, y_test) = load_mnist_for_mlp()
|
||||
|
||||
EPOCHS = 20
|
||||
BATCH_SIZE = 128
|
||||
|
||||
# 2. Baseline Model (Without Regularization)
|
||||
print("\n" + "=" * 60)
|
||||
print("TRAINING BASELINE MODEL (WITHOUT REGULARIZATION)")
|
||||
print("=" * 60)
|
||||
model_base = create_mlp_model([256, 128, 64])
|
||||
history_base = model_base.fit(
|
||||
x_train, y_train,
|
||||
epochs=EPOCHS,
|
||||
batch_size=BATCH_SIZE,
|
||||
validation_split=0.1,
|
||||
verbose=1
|
||||
)
|
||||
|
||||
# 3. Regularized Model (BatchNorm + Dropout 0.3)
|
||||
print("\n" + "=" * 60)
|
||||
print("TRAINING REGULARIZED MODEL (WITH BATCHNORM + DROPOUT)")
|
||||
print("=" * 60)
|
||||
model_reg = create_regularized_mlp_model(hidden_layers=[256, 128, 64], dropout_rate=0.3)
|
||||
history_reg = model_reg.fit(
|
||||
x_train, y_train,
|
||||
epochs=EPOCHS,
|
||||
batch_size=BATCH_SIZE,
|
||||
validation_split=0.1,
|
||||
verbose=1
|
||||
)
|
||||
|
||||
# 4. Save Model Artifact
|
||||
results_model_dir = os.path.join("results", "models")
|
||||
ensure_dir(results_model_dir)
|
||||
model_reg_path = os.path.join(results_model_dir, "model_c_regularized.keras")
|
||||
model_reg.save(model_reg_path)
|
||||
print(f"\n[INFO] Model teregularisasi disimpan di: {model_reg_path}")
|
||||
|
||||
# 5. Plot Comparison Curves
|
||||
results_fig_dir = os.path.join("results", "figures")
|
||||
ensure_dir(results_fig_dir)
|
||||
fig_path = os.path.join(results_fig_dir, "loss_regularization.png")
|
||||
|
||||
plt.figure(figsize=(14, 5))
|
||||
|
||||
# Subplot 1: Baseline
|
||||
plt.subplot(1, 2, 1)
|
||||
plt.plot(history_base.history['loss'], label='Training Loss', color='blue')
|
||||
plt.plot(history_base.history['val_loss'], label='Validation Loss', color='orange', linestyle='--')
|
||||
plt.title('Model C Without Regularization')
|
||||
plt.xlabel('Epoch')
|
||||
plt.ylabel('Loss')
|
||||
plt.legend()
|
||||
plt.grid(True, alpha=0.3)
|
||||
|
||||
# Subplot 2: Regularized
|
||||
plt.subplot(1, 2, 2)
|
||||
plt.plot(history_reg.history['loss'], label='Training Loss', color='blue')
|
||||
plt.plot(history_reg.history['val_loss'], label='Validation Loss', color='green', linestyle='--')
|
||||
plt.title('Model C With BatchNorm + Dropout(0.3)')
|
||||
plt.xlabel('Epoch')
|
||||
plt.ylabel('Loss')
|
||||
plt.legend()
|
||||
plt.grid(True, alpha=0.3)
|
||||
|
||||
plt.tight_layout()
|
||||
plt.savefig(fig_path, dpi=130)
|
||||
print(f"[INFO] Grafik perbandingan loss disimpan di: {fig_path}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
"""
|
||||
Tugas 3: Contoh Implementasi Berbagai Arsitektur Model Sesuai Karakteristik Data
|
||||
"""
|
||||
|
||||
import tensorflow as tf
|
||||
from tensorflow import keras
|
||||
from tensorflow.keras.layers import Conv2D, Dense, Embedding, Flatten, LSTM, MaxPooling2D
|
||||
from tensorflow.keras.models import Sequential
|
||||
from xgboost import XGBClassifier
|
||||
|
||||
|
||||
def build_models():
|
||||
print("=" * 60)
|
||||
print("TUGAS 3: CONTOH ARSITEKTUR MODEL SELECTION")
|
||||
print("=" * 60)
|
||||
|
||||
# (a) Prediksi Stok Barang dari Deret Waktu (LSTM)
|
||||
# Input shape: (samples, time_steps=12, features=1)
|
||||
model_a = Sequential([
|
||||
LSTM(32, input_shape=(12, 1)),
|
||||
Dense(1)
|
||||
], name="LSTM_Stock_Forecasting")
|
||||
model_a.compile(optimizer='adam', loss='mse')
|
||||
print("\n[Case A] Model LSTM untuk Prediksi Stok Barang (Time Series):")
|
||||
model_a.summary()
|
||||
|
||||
# (b) Klasifikasi Foto Produk Rusak vs Normal (CNN)
|
||||
# Input shape contoh citra RGB 128x128
|
||||
model_b = Sequential([
|
||||
Conv2D(32, (3, 3), activation='relu', input_shape=(128, 128, 3)),
|
||||
MaxPooling2D((2, 2)),
|
||||
Flatten(),
|
||||
Dense(64, activation='relu'),
|
||||
Dense(1, activation='sigmoid') # binary: rusak vs normal
|
||||
], name="CNN_Defect_Classification")
|
||||
model_b.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
|
||||
print("\n[Case B] Model CNN untuk Klasifikasi Foto Produk (Computer Vision):")
|
||||
model_b.summary()
|
||||
|
||||
# (c) Analisis Sentimen Ulasan Play Store (LSTM + Embedding)
|
||||
vocab_size = 5000
|
||||
max_length = 100
|
||||
model_c = Sequential([
|
||||
Embedding(input_dim=vocab_size, output_dim=64, input_length=max_length),
|
||||
LSTM(64),
|
||||
Dense(32, activation='relu'),
|
||||
Dense(1, activation='sigmoid') # binary: positif vs negatif
|
||||
], name="LSTM_Sentiment_Analysis")
|
||||
model_c.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
|
||||
print("\n[Case C] Model LSTM + Embedding untuk Analisis Sentimen (NLP):")
|
||||
model_c.summary()
|
||||
|
||||
# (d) Prediksi Churn Pelanggan Tabular (ML Klasik - XGBoost)
|
||||
# X_train memiliki dimensi (n_samples, 15)
|
||||
model_d = XGBClassifier(
|
||||
n_estimators=100,
|
||||
max_depth=4,
|
||||
learning_rate=0.1,
|
||||
random_state=42
|
||||
)
|
||||
print("\n[Case D] Model XGBoost untuk Prediksi Churn (Tabular 15 fitur):")
|
||||
print(model_d)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
build_models()
|
||||
|
||||
@@ -0,0 +1,148 @@
|
||||
"""
|
||||
Tugas No. 4 - Implementasi CNN sederhana untuk MNIST
|
||||
dan perbandingannya dengan MLP dari Tugas No. 1.
|
||||
"""
|
||||
|
||||
import os
|
||||
import sys
|
||||
import time
|
||||
|
||||
# Sembunyikan log informasi TensorFlow
|
||||
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2"
|
||||
|
||||
import matplotlib
|
||||
matplotlib.use("Agg") # simpan plot sebagai file
|
||||
import matplotlib.pyplot as plt
|
||||
import numpy as np
|
||||
import tensorflow as tf
|
||||
|
||||
# Tambahkan root directory ke path agar dapat import dari src
|
||||
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
|
||||
|
||||
from src.data_loader import load_mnist_for_cnn
|
||||
from src.models import create_cnn_mnist_model
|
||||
from src.utils import EpochTimerCallback, ensure_dir
|
||||
|
||||
|
||||
def main():
|
||||
# Konfigurasi
|
||||
SEED = 42
|
||||
EPOCHS = 10
|
||||
BATCH_SIZE = 128
|
||||
VAL_SPLIT = 0.1
|
||||
|
||||
tf.random.set_seed(SEED)
|
||||
np.random.seed(SEED)
|
||||
|
||||
# Hasil MLP terbaik dari Tugas No. 1 (Model C: 256-128-64)
|
||||
MLP_ACCURACY = 0.9764
|
||||
MLP_PARAMS = 242_762
|
||||
MLP_EPOCH_TIME = 2.50
|
||||
|
||||
# 1. Load dan preprocessing data
|
||||
print("Memuat dataset MNIST untuk CNN...")
|
||||
(x_train, y_train), (x_test, y_test) = load_mnist_for_cnn()
|
||||
|
||||
print(f"Data training : {x_train.shape[0]:,} gambar")
|
||||
print(f"Data testing : {x_test.shape[0]:,} gambar")
|
||||
print(f"Ukuran gambar : {x_train.shape[1]} x {x_train.shape[2]} piksel (channel={x_train.shape[3]})\n")
|
||||
|
||||
# 2. Bangun model CNN
|
||||
model = create_cnn_mnist_model()
|
||||
model.summary()
|
||||
|
||||
# 3. Training dengan pencatatan waktu per epoch
|
||||
timer_cb = EpochTimerCallback()
|
||||
|
||||
print("\nMemulai training CNN...")
|
||||
print("Perkiraan waktu: 15-20 detik per epoch di CPU (total sekitar 3 menit).\n")
|
||||
|
||||
history = model.fit(
|
||||
x_train,
|
||||
y_train,
|
||||
validation_split=VAL_SPLIT,
|
||||
epochs=EPOCHS,
|
||||
batch_size=BATCH_SIZE,
|
||||
verbose=2,
|
||||
callbacks=[timer_cb],
|
||||
)
|
||||
|
||||
# 4. Evaluasi
|
||||
test_loss, test_acc = model.evaluate(x_test, y_test, verbose=0)
|
||||
n_params = model.count_params()
|
||||
avg_epoch_time = float(np.mean(timer_cb.epoch_times)) if timer_cb.epoch_times else 0.0
|
||||
|
||||
predictions = np.argmax(model.predict(x_test, verbose=0), axis=1)
|
||||
n_wrong = int(np.sum(predictions != y_test))
|
||||
|
||||
# 5. Perbandingan CNN vs MLP
|
||||
error_mlp = (1 - MLP_ACCURACY) * 100
|
||||
error_cnn = (1 - test_acc) * 100
|
||||
gain_point = (test_acc - MLP_ACCURACY) * 100
|
||||
error_reduction = (1 - error_cnn / error_mlp) * 100
|
||||
|
||||
print("\n" + "=" * 62)
|
||||
print("HASIL EKSPERIMEN CNN")
|
||||
print("=" * 62)
|
||||
print(f"Test accuracy : {test_acc * 100:.2f}%")
|
||||
print(f"Test loss : {test_loss:.4f}")
|
||||
print(f"Jumlah parameter : {n_params:,}")
|
||||
print(f"Waktu training/epoch : {avg_epoch_time:.2f} detik")
|
||||
print(f"Salah klasifikasi : {n_wrong} dari {len(y_test):,} gambar")
|
||||
|
||||
print("\n" + "=" * 62)
|
||||
print("PERBANDINGAN CNN vs MLP")
|
||||
print("=" * 62)
|
||||
print(f"{'Metrik':<24}{'MLP':>14}{'CNN':>14}")
|
||||
print("-" * 62)
|
||||
print(f"{'Test accuracy':<24}{MLP_ACCURACY * 100:>13.2f}%{test_acc * 100:>13.2f}%")
|
||||
print(f"{'Error rate':<24}{error_mlp:>13.2f}%{error_cnn:>13.2f}%")
|
||||
print(f"{'Jumlah parameter':<24}{MLP_PARAMS:>14,}{n_params:>14,}")
|
||||
print(f"{'Waktu/epoch (detik)':<24}{MLP_EPOCH_TIME:>14.2f}{avg_epoch_time:>14.2f}")
|
||||
print("-" * 62)
|
||||
print(f"Improvement akurasi : +{gain_point:.2f} poin persentase")
|
||||
print(f"Pengurangan error rate : {error_reduction:.1f}% (relatif)")
|
||||
print(f"Selisih parameter : {n_params - MLP_PARAMS:+,}")
|
||||
print("=" * 62)
|
||||
|
||||
# 6. Simpan grafik ke folder results/figures
|
||||
results_fig_dir = os.path.join("results", "figures")
|
||||
ensure_dir(results_fig_dir)
|
||||
fig_path = os.path.join(results_fig_dir, "hasil_cnn.png")
|
||||
|
||||
fig, axes = plt.subplots(1, 2, figsize=(12, 4.5))
|
||||
epochs_range = range(1, EPOCHS + 1)
|
||||
axes[0].plot(epochs_range, history.history["accuracy"], label="Train accuracy")
|
||||
axes[0].plot(epochs_range, history.history["val_accuracy"], label="Validation accuracy")
|
||||
axes[0].set_title("Learning Curve CNN")
|
||||
axes[0].set_xlabel("Epoch")
|
||||
axes[0].set_ylabel("Accuracy")
|
||||
axes[0].legend()
|
||||
axes[0].grid(alpha=0.3)
|
||||
|
||||
axes[1].bar(
|
||||
["MLP terbaik", "CNN"],
|
||||
[MLP_ACCURACY * 100, test_acc * 100],
|
||||
color=["#55A868", "#C44E52"],
|
||||
)
|
||||
axes[1].set_ylim(96, 100)
|
||||
axes[1].set_ylabel("Test Accuracy (%)")
|
||||
axes[1].set_title("Perbandingan MLP vs CNN")
|
||||
for i, value in enumerate([MLP_ACCURACY * 100, test_acc * 100]):
|
||||
axes[1].text(i, value + 0.05, f"{value:.2f}%", ha="center")
|
||||
|
||||
plt.tight_layout()
|
||||
plt.savefig(fig_path, dpi=130)
|
||||
print(f"\n[INFO] Grafik disimpan di: {fig_path}")
|
||||
|
||||
# 7. Simpan model terlatih ke folder results/models
|
||||
results_model_dir = os.path.join("results", "models")
|
||||
ensure_dir(results_model_dir)
|
||||
model_path = os.path.join(results_model_dir, "model_cnn_mnist.keras")
|
||||
model.save(model_path)
|
||||
print(f"[INFO] Model disimpan di: {model_path}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
Reference in New Issue
Block a user