Deep Learning untuk Gambar dengan PyTorch
Michal Oleszak
Machine Learning Engineer
Melatih model dari nol:
Model terlatih - model yang sudah dilatih pada suatu tugas
Langkah memanfaatkan model terlatih:
torchvisiontorch.save().pt atau .pth.state_dict()torch.save(model.state_dict(), "BinaryCNN.pth")
Inisialisasi model baru
new_model = BinaryCNN()
Muat parameter tersimpan
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 dan bobotfrom 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 untuk Gambar dengan PyTorch