🏕️Praktikum 4
Last updated
Last updated
Penerapan metode Self-Organizing Map (SOM) untuk segmentasi citra Lenna.
Download dataset terlebih dahulu
instalasi minisom
pip install minisom
Nanti akan muncul tampilan seperti dibawah ini:
Collecting minisom
Downloading MiniSom-2.3.1.tar.gz (10 kB)
Preparing metadata (setup.py) ... done
Building wheels for collected packages: minisom
Building wheel for minisom (setup.py) ... done
Created wheel for minisom: filename=MiniSom-2.3.1-py3-none-any.whl size=10588 sha256=c45ce374f32484fbd730daef8874570c408920644f2a0e7f17402eae6556fd9d
Stored in directory: /root/.cache/pip/wheels/c7/92/d2/33bbda5f86fd8830510b16aa98c8dd420129b5cb24248fd6db
Successfully built minisom
Installing collected packages: minisom
Successfully installed minisom-2.3.1
import numpy as np
import matplotlib.pyplot as plt
from skimage import io
# Fungsi untuk menginisialisasi bobot SOM
def initialize_weights(input_shape, output_shape):
return np.random.rand(output_shape[0], output_shape[1], input_shape[2])
# Fungsi untuk menghitung jarak antara vektor input dan bobot SOM
def calculate_distance(input_vector, weights):
return np.linalg.norm(input_vector - weights, axis=2)
# Fungsi untuk menemukan indeks unit pemenang (unit dengan bobot terdekat)
def find_winner_unit_in_image(input_vector, weights):
distances = calculate_distance(input_vector, weights)
return np.unravel_index(np.argmin(distances), distances.shape)
# Fungsi untuk memperbarui bobot SOM
def update_weights(input_vector, weights, winner, learning_rate, neighborhood_radius):
distances = np.linalg.norm(np.indices(weights.shape[:2]).T - np.array(winner).reshape(1, -1), axis=2)
influence = np.exp(-distances / (2 * neighborhood_radius**2))
weights += learning_rate * influence[:, :, np.newaxis] * (input_vector - weights)
# Fungsi untuk melatih SOM
def train_som(image, num_epochs, initial_learning_rate, initial_neighborhood_radius):
input_shape = image.shape
som_shape = (10, 10, input_shape[2]) # Ukuran SOM sesuai dengan jumlah saluran warna
weights = initialize_weights(input_shape, som_shape)
for epoch in range(num_epochs):
# Update parameter pembelajaran dan radius tetangga
learning_rate = initial_learning_rate * np.exp(-epoch / num_epochs)
neighborhood_radius = initial_neighborhood_radius * np.exp(-epoch / num_epochs)
# Pemrosesan SOM
for i in range(input_shape[0]):
for j in range(input_shape[1]):
input_vector = image[i, j, :]
winner = find_winner_unit_in_image(input_vector, weights)
update_weights(input_vector, weights, winner, learning_rate, neighborhood_radius)
return weights
# Load citra Lenna (Anda bisa mengganti ini dengan citra lain jika diperlukan)
Lenna_path = "Lenna.png"
Lenna = io.imread(Lenna_path) / 255.0 # Normalisasi intensitas piksel menjadi rentang [0, 1]
# Latih SOM
num_epochs = 100
initial_learning_rate = 0.1
initial_neighborhood_radius = 5
trained_weights = train_som(Lenna, num_epochs, initial_learning_rate, initial_neighborhood_radius)
# Visualisasi bobot SOM
plt.imshow(trained_weights)
plt.title('Trained SOM Weights for Lena')
plt.show()
Nanti akan muncul tampilan seperti dibawah ini: