Я пытаюсь последовать примеру создания синтетических изображений из этой статьи
(https://medium.com/@sofeikov/implementi ... 3782d8eb95) о вариационных автоэнкодерах, и код работает нормально, но когда я пытаюсь включить T4-GPU, он выдает эту ошибку. Я также не знаю, почему все генерируемые изображения одинаковы и все они слишком плохи... в статье реконструированные изображения довольно хороши. Попробовал переключить обратно - ошибка пропала.
import numpy as np
import gzip
import os
import matplotlib.pyplot as plt
import torch.nn as nn
import torch.optim as optim
import torch
def show_images(images, labels):
"""
Display a set of images and their labels using matplotlib.
The first column of `images` should contain the image indices,
and the second column should contain the flattened image pixels
reshaped into 28x28 arrays.
"""
# Extract the image indices and reshaped pixels
pixels = images.reshape(-1, 28, 28)
# Create a figure with subplots for each image
fig, axs = plt.subplots(
ncols=len(images), nrows=1, figsize=(10, 3 * len(images))
)
# Loop over the images and display them with their labels
for i in range(len(images)):
# Display the image and its label
axs.imshow(pixels, cmap="gray")
axs.set_title("Label: {}".format(labels))
# Remove the tick marks and axis labels
axs.set_xticks([])
axs.set_yticks([])
axs.set_xlabel("Index: {}".format(i))
# Adjust the spacing between subplots
fig.subplots_adjust(hspace=0.5)
# Show the figure
plt.show()
class AutoEncoder(nn.Module):
def __init__(self):
super().__init__()
# Set the number of hidden units
self.num_hidden = 8
# Define the encoder part of the autoencoder
self.encoder = nn.Sequential(
nn.Linear(784, 256), # input size: 784, output size: 256
nn.ReLU(), # apply the ReLU activation function
nn.Linear(256, self.num_hidden), # input size: 256, output size: num_hidden
nn.ReLU(), # apply the ReLU activation function
)
# Define the decoder part of the autoencoder
self.decoder = nn.Sequential(
nn.Linear(self.num_hidden, 256), # input size: num_hidden, output size: 256
nn.ReLU(), # apply the ReLU activation function
nn.Linear(256, 784), # input size: 256, output size: 784
nn.Sigmoid(), # apply the sigmoid activation function to compress the output to a range of (0, 1)
)
def forward(self, x):
# Pass the input through the encoder
encoded = self.encoder(x)
# Pass the encoded representation through the decoder
decoded = self.decoder(encoded)
# Return both the encoded representation and the reconstructed output
return encoded, decoded
dir_path = r'/content'
# List to store files
filenames = []
# Iterate directory
for path in os.listdir(dir_path):
# Check if current path is a file
if os.path.isfile(os.path.join(dir_path, path)):
filenames.append(os.path.join(dir_path, path)) # Store full path
data = []
for filename in filenames:
with gzip.open(filename, 'rb') as f:
if 'labels' in filename:
# Load the labels as a one-dimensional array of integers
data.append(np.frombuffer(f.read(), np.uint8, offset=8))
else:
# Load the images as a two-dimensional array of pixels
data.append(np.frombuffer(f.read(), np.uint8, offset=16).reshape(-1, 28*28))
# Split into training and testing sets
X_train, y_train, X_test, y_test = data
# Normalize the pixel values
X_train = X_train.astype(np.float32) / 255.0
X_test = X_test.astype(np.float32) / 255.0
# Convert labels to integers
y_train = y_train.astype(np.int64)
y_test = y_test.astype(np.int64)
show_images(X_train[:5], y_train[:5])
# Convert the training data to PyTorch tensors
X_train = torch.from_numpy(X_train)
learning_rate = 20
batch_size = 16
# Create the autoencoder model and optimizer
model = AutoEncoder()
optimizer = optim.Adam(model.parameters(), lr=learning_rate)
# Define the loss function
criterion = nn.MSELoss()
# Set the device to GPU if available, otherwise use CPU
# Create a DataLoader to handle batching of the training data
train_loader = torch.utils.data.DataLoader(
X_train, batch_size=batch_size, shuffle=True
)
num_epochs = 10
for epoch in range(num_epochs):
total_loss = 0.0
for batch_idx, data in enumerate(train_loader):
# Get a batch of training data and move it to the device
# Forward pass
encoded, decoded = model(data)
# Compute the loss and perform backpropagation
loss = criterion(decoded, data)
optimizer.zero_grad()
loss.backward()
optimizer.step()
# Update the running loss
total_loss += loss.item() * data.size(0)
# Print the epoch loss
epoch_loss = total_loss / len(train_loader.dataset)
print(
"Epoch {}/{}: loss={:.4f}".format(epoch + 1, num_epochs, epoch_loss)
)
def generate_images(model, images, num_images=5):
"""
Pass images through the autoencoder model to generate and display reconstructed images.
"""
with torch.no_grad(): # No need to compute gradients during inference
_, decoded_images = model(images[:num_images])
decoded_images = decoded_images.reshape(-1, 28, 28).numpy()
fig, axs = plt.subplots(2, num_images, figsize=(10, 4))
for i in range(num_images):
axs[0, i].imshow(images.reshape(28, 28), cmap="gray")
axs[0, i].set_title("Original")
axs[0, i].axis("off")
axs[1, i].imshow(decoded_images, cmap="gray")
axs[1, i].set_title("Reconstructed")
axs[1, i].axis("off")
plt.show()
generate_images(model, X_train[:5])
Я получаю эту ошибку.
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
in ()
104 y_train = y_train.astype(np.int64)
105 y_test = y_test.astype(np.int64)
--> 106 show_images(X_train[:5], y_train[:5])
107
108 # Convert the training data to PyTorch tensors
in show_images(images, labels)
14 """
15 # Extract the image indices and reshaped pixels
---> 16 pixels = images.reshape(-1, 28, 28)
17
18 # Create a figure with subplots for each image
ValueError: cannot reshape array of size 5 into shape (28,28)
И когда я пытаюсь прокомментировать функцию, я получаю эту ошибку. Я терпеть не могу, как только меняется время выполнения
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
in ()
133
134 # Forward pass
--> 135 encoded, decoded = model(data)
136
137 # Compute the loss and perform backpropagation
8 frames
/usr/local/lib/python3.10/dist-packages/torch/nn/modules/linear.py in forward(self, input)
123
124 def forward(self, input: Tensor) -> Tensor:
--> 125 return F.linear(input, self.weight, self.bias)
126
127 def extra_repr(self) -> str:
RuntimeError: mat1 and mat2 shapes cannot be multiplied (1x16 and 784x256)
Подробнее здесь: https://stackoverflow.com/questions/791 ... ead-of-cpu
Ошибка Python colab только при работе графического процессора вместо процессора ⇐ Python
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение