gpt4 book ai didi

python - pytorch自动编码器模型评估失败

转载 作者:行者123 更新时间:2023-12-01 01:17:38 25 4
gpt4 key购买 nike

我确实是 PyTorch 的初学者。我训练了一个自动编码器网络,以便可以绘制潜在向量(编码器的结果)的分布。

这是我用于网络训练的代码。

import torch
import torchvision
from torch import nn
from torch.utils.data import DataLoader
from torchvision import transforms
from torchvision.utils import save_image
from torch.utils.data import Dataset
from PIL import Image
import os
import glob

dir_img_decoded = '/media/dohyeong/HDD/mouth_autoencoder/dc_img_2'
if not os.path.exists(dir_img_decoded):
os.mkdir(dir_img_decoded)

dir_check_point = '/media/dohyeong/HDD/mouth_autoencoder/ckpt_2'
if not os.path.exists(dir_check_point):
os.mkdir(dir_check_point)

device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')

num_epochs = 200
batch_size = 150 # up -> GPU memory increase
learning_rate = 1e-3

dir_dataset = '/media/dohyeong/HDD/mouth_autoencoder/mouth_crop/dir_normalized_mouth_cropped_images'
images = glob.glob(os.path.join(dir_dataset, '*.png'))
train_images = images[:-113]
test_images = images[-113:]

train_images.sort()
test_images.sort()





class TrumpMouthDataset(Dataset):
def __init__(self, images):
super(TrumpMouthDataset, self).__init__()
self.images = images

self.transform = transforms.Compose([
# transforms.Resize((28, 28)),
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
])

def __getitem__(self, index):
image = Image.open(self.images[index])

return self.transform(image)

def __len__(self):
return len(self.images)


train_dataset = TrumpMouthDataset(train_images)
test_dataset = TrumpMouthDataset(test_images)

train_dataloader = DataLoader(train_dataset, batch_size=batch_size, shuffle=True)
test_dataloader = DataLoader(test_dataset, batch_size=batch_size, shuffle=True)


class Autoencoder(nn.Module):
def __init__(self):
super(Autoencoder, self).__init__()
self.encoder = nn.Sequential(
nn.Linear(60000, 60),
nn.ReLU(True),
nn.Linear(60, 3),
nn.ReLU(True),
)
self.decoder = nn.Sequential(
nn.Linear(3, 60),
nn.ReLU(True),
nn.Linear(60, 60000),
nn.Tanh()
)

def forward(self, x):
x = x.view(x.size(0), -1)
encoded = self.encoder(x)
decoded = self.decoder(encoded)

return encoded, decoded


model = Autoencoder().cuda()
if torch.cuda.device_count() > 1:
model = nn.DataParallel(model)
model.to(device)

criterion = nn.MSELoss()
optimizer = torch.optim.Adam(model.parameters(),
lr=learning_rate,
weight_decay=1e-5)

for epoch in range(num_epochs):

total_loss = 0

for index, imgs in enumerate(train_dataloader):
imgs = imgs.to(device)

# ===================forward=====================
outputs = model(imgs)

imgs_flatten = imgs.view(imgs.size(0), -1)
loss = criterion(outputs, imgs_flatten)

# ===================backward====================
optimizer.zero_grad()
loss.backward()
optimizer.step()

total_loss += loss.item()

print('{} Epoch, [{}/{}] batch, loss: {:.4f}'.format(epoch, index + 1, len(train_dataloader), loss.item()))

avg_loss = total_loss / len(train_dataset)
print('{} Epoch, avg_loss: {:.4f}'.format(epoch, avg_loss))


if epoch % 10 == 0:
check_point_file = os.path.join(dir_check_point, str(epoch) + ".pth")
torch.save(model.state_dict(), check_point_file)

训练后,我尝试使用此代码获取编码值。

device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')

check_point = '/media/dohyeong/HDD/mouth_autoencoder/290.pth'
model = torch.load(check_point)

for index, imgs in enumerate(train_dataloader):

imgs = imgs.to(device)

# ===================evaluate=====================
encoded, _ = model(imgs)

它已完成并显示此错误消息。“类型错误:‘collections.OrderedDict’对象不可调用”我可以得到一些帮助吗?

最佳答案

您好,欢迎来到 PyTorch 社区:D

TL;DR

更改model = torch.load(check_point)model.load_state_dict(torch.load(check_point)) .

<小时/>

唯一的问题是这行:

model = torch.load(check_point)

您保存检查点的方式是:

torch.save(model.state_dict(), check_point_file)

也就是说,您保存了模型的 state_dict (这只是一起描述模型当前实例的各种参数的字典)在 check_point_file 中.

现在,为了将其加载回来,只需反转该过程即可。 check_point_file仅包含 state_dict

它对模型的内部结构一无所知 - 它的架构是什么,它应该如何工作等等。

所以,加载它:

state_dict = torch.load(check_point)

这个state_dict现在可以复制到您的模型实例上,如下所示:

model.load_state_dict(state_dict)

或者,更简洁地说,

model.load_state_dict(torch.load(check_point))

您收到错误是因为 torch.load(check_point)返回 state_dict 您分配给 model .

当您随后调用model(imgs)时, modelOrderedDict对象(不可调用)。

因此出现错误。

请参阅Serialization Semantics Notes了解更多详情。

除此之外,您的代码对于初学者来说确实是彻底的。干得好!

<小时/>

附注您的设备不可知论非常出色!也许您想看看:

  1. 线路model = Autoencoder().cuda()
  2. map_location torch.load() 的参数

关于python - pytorch自动编码器模型评估失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54166865/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com