Deep Learning สำหรับภาพด้วย PyTorch
Michal Oleszak
Machine Learning Engineer
การเทรนโมเดลตั้งแต่เริ่มต้น:
Pre-trained models - โมเดลที่เทรนบนงานหนึ่งแล้ว
ขั้นตอนการใช้ pre-trained models:
torchvisiontorch.save().pt หรือ .pth.state_dict()torch.save(model.state_dict(), "BinaryCNN.pth")
สร้าง instance โมเดลใหม่
new_model = BinaryCNN()
โหลด parameters ที่บันทึกไว้
new_model.load_state_dict(torch.load('BinaryCNN.pth'))
from torchvision.models import ( resnet18, ResNet18_Weights )weights = ResNet18_Weights.DEFAULTmodel = resnet18(weights=weights)transforms = weights.transforms()
resnet และ weightsfrom PIL import Image image = Image.open("cat013.jpg")image_tensor = transform(image)image_reshaped = image_tensors.unsqueeze(0)

model.eval()with torch.no_grad():pred = model(image_reshaped).squeeze(0)pred_cls = pred.softmax(0)cls_id = pred_cls.argmax().item()cls_name = weights.meta["categories"][cls_id]print(cls_name)
Egyptian cat
Deep Learning สำหรับภาพด้วย PyTorch