-
Notifications
You must be signed in to change notification settings - Fork 1
/
train_tinyvgg_model.py
45 lines (38 loc) · 1.43 KB
/
train_tinyvgg_model.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import torch
import predictions
import os
from torchvision import transforms
from data_setup import download_data, create_dataloader
from models import TinyVGG
from engine import train
from utils import save_model, plot_training_and_testing_results
# Setup huperparameters
NUM_EPOCHS = 10
BATCH_SIZE = 32
HIDDEN_UNITS = 10
NUM_WORKERS = 0
LR = 0.001
# Setup directories
train_dir, valid_dir = download_data(root_path='./data', zipfile_name='dogvscat.zip')
# Setup target device
device = 'cuda' if torch.cuda.is_available() else 'cpu'
transformer = transforms.Compose([
transforms.Resize(size=(64,64)),
transforms.RandomHorizontalFlip(),
transforms.ToTensor()
])
# Create DataLoader
train_dataloader, valid_dataloade, cls_names = create_dataloader(train_dir, valid_dir,
transformer,
BATCH_SIZE,
NUM_WORKERS)
# Create model
model = TinyVGG(input_shape=3,
hidden_units=HIDDEN_UNITS,
output_shape=len(cls_names)).to(device)
# Start training
results = train(model, train_dataloader, valid_dataloade, NUM_EPOCHS, LR, device)
# Save the model
save_model(model=model, tar_dir="models", model_name="10_mode_tinybgg.pth")
# plot the results
plot_training_and_testing_results(results)