Почему визуализация моего прогноза в сетке 3 на 3Python

Программы на Python
Ответить
Anonymous
 Почему визуализация моего прогноза в сетке 3 на 3

Сообщение Anonymous »

Я пытаюсь визуализировать, насколько хорошо моя модель предсказала сегментацию изображений после обучения, но сюжет не так, как ожидалось. Входное изображение и целевая маска в порядке, но предсказание, по -видимому, отображается неправильно. Вместо одного прогноза, по -видимому, он уменьшается по размеру и повторяется несколько раз (образуя сетку 3x3). < /P>
from tqdm import tqdm
from sklearn.cluster import MiniBatchKMeans
import torch
import numpy as np
import matplotlib.pyplot as plt

class Metric:
def __init__(self, predictions, masks, n_classes):
self.predictions = predictions
self.masks = masks
self.classes = n_classes

def accuracy(self):
with torch.no_grad():
correct = (self.predictions == self.masks).sum().item()
total = self.masks.numel()
accuracy = correct / total
return accuracy

n_clusters = 2
epochs = 15
batch_size = 10

loss_history = {
"training_accuracy": [],
"validation_accuracy": []
}

kmeans = MiniBatchKMeans(n_clusters=n_clusters, random_state=42, batch_size=batch_size)

for epoch in range(epochs):
training_accuracy = 0.0
validation_accuracy = 0.0

print(f"Epoch {epoch + 1}/{epochs}")

indices = np.random.permutation(len(X_train))
X_train_shuffled = X_train[indices]
y_train_shuffled = y_train[indices]

for i in tqdm(range(0, len(X_train_shuffled), batch_size)):
batch_data = X_train_shuffled[i:i + batch_size].cpu().numpy().reshape(-1, 3)
batch_masks = y_train_shuffled[i:i + batch_size].cpu().numpy().reshape(-1)

kmeans.partial_fit(batch_data)

predictions = torch.tensor(kmeans.predict(batch_data)).to(device)
masks = torch.tensor(batch_masks).to(device)
metric = Metric(predictions, masks, n_clusters)
training_accuracy += metric.accuracy()

with torch.no_grad():
X_val_flat = X_val.cpu().numpy().reshape(-1, 3)
y_val_flat = y_val.cpu().numpy().reshape(-1)

val_predictions = torch.tensor(kmeans.predict(X_val_flat)).to(device)
val_masks = torch.tensor(y_val_flat).to(device)
metric = Metric(val_predictions, val_masks, n_clusters)
validation_accuracy = metric.accuracy()

loss_history["training_accuracy"].append(training_accuracy / len(X_train))
loss_history["validation_accuracy"].append(validation_accuracy)

print(f"Training Accuracy: {training_accuracy / len(X_train):.4f}")
print(f"Validation Accuracy: {validation_accuracy:.4f}")
print("=" * 50)

with torch.no_grad():
X_test_flat = X_test.cpu().numpy().reshape(-1, 3)
y_test_flat = y_test.cpu().numpy().reshape(-1)

test_predictions = torch.tensor(kmeans.predict(X_test_flat)).to(device)
test_masks = torch.tensor(y_test_flat).to(device)
metric = Metric(test_predictions, test_masks, n_clusters)
test_accuracy = metric.accuracy()

print(f"\nTest Accuracy: {test_accuracy:.4f}")

def plot_image_mask_prediction(image, mask, prediction, image_idx):
fig, ax = plt.subplots(1, 3, figsize=(18, 6))
ax[0].imshow(image)
ax[0].set_title(f"Image {image_idx}")
ax[0].axis('off')
ax[1].imshow(mask, cmap='gray')
ax[1].set_title(f"Mask {image_idx}")
ax[1].axis('off')
ax[2].imshow(prediction, cmap='gray')
ax[2].set_title(f"Prediction KMeans {image_idx}")
ax[2].axis('off')
plt.show()

with torch.no_grad():
X_test_flat = X_test.cpu().numpy().reshape(-1, 3)
y_test_flat = y_test.cpu().numpy().reshape(-1)
test_predictions = kmeans.predict(X_test_flat)
test_predictions = torch.tensor(test_predictions).to(device)
test_masks = torch.tensor(y_test_flat).to(device)

for i in range(5):
image_to_display = X_test.cpu().numpy().transpose(1, 2, 0)
image_to_display = image_to_display / 255.0
mask_to_display = y_test.cpu().numpy().squeeze(0)

start_idx = i * X_test.shape[2] * X_test.shape[3]
end_idx = (i + 1) * X_test.shape[2] * X_test.shape[3]
prediction_to_display = test_predictions[start_idx:end_idx].cpu().numpy()
prediction_to_display = prediction_to_display.reshape(X_test.shape[1], X_test.shape[2])
prediction_to_display = (prediction_to_display * 255).astype(np.uint8)
plot_image_mask_prediction(image_to_display, mask_to_display, prediction_to_display, i + 1)



Подробнее здесь: https://stackoverflow.com/questions/793 ... -by-3-grid
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «Python»