-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain_model.py
145 lines (111 loc) · 3.9 KB
/
train_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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import numpy as np
import torch
import torch.nn as nn
import torch.optim as optim
import torchvision
import torchvision.models as models
import torchvision.transforms as transforms
import argparse
#TODO: Import dependencies for Debugging andd Profiling
def test(model, test_loader):
'''
TODO: Complete this function that can take a model and a
testing data loader and will get the test accuray/loss of the model
Remember to include any debugging/profiling hooks that you might need
'''
pass
def train(model, train_loader, criterion, optimizer):
'''
TODO: Complete this function that can take a model and
data loaders for training and will get train the model
Remember to include any debugging/profiling hooks that you might need
'''
pass
def net():
'''
Initializes pre-trained model
'''
model = models.resnet50(pretrained=True) # Pulling in a pre-trained model
for param in model.parameters():
param.requires_grad = False # Freezing convoluational layers
num_features=model.fc.in_features # See the number of features present in the model, so we know how to add the Fully Connected layer at the end
model.fc = nn.Sequential(
nn.Linear(num_features, 133))
return model
def create_data_loaders(data, batch_size):
# Dataset paths
training_path = os.path.join(data, 'train')
testing_path = os.path.join(data, 'test')
validating_path = os.path.join(data, 'valid')
# Configuring Image Transformers
train_transform = transforms.Compose([
transforms.Resize((224, 224)), # Images standardzied to 224 X 224 for training.
transforms.RandomHorizontalFlip(), # To prevent potential possible biases in the training model.
transforms.ToTensor(), # Converting to a tensor (a multi-dimensional array).
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)) # Normalizing the data -> image = (image - mean) / std
])
test_transform = transforms.Compose([
transforms.Resize((224, 224)),
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
])
# Creating the Datasets
train_data = torchvision.datasets.ImageFolder(
root = training_path,
transform = train_transform
)
test_data = torchvision.datasets.ImageFolder(
root = testing_path,
transform = test_transform
)
validation_data = torchvision.datasets.ImageFolder(
root = validating_path,
transform = test_transform,
)
# Data Loaders
train_loader = torch.utils.data.DataLoader(
train_data,
batch_size = batch_size,
shuffle = True,
)
test_loader = torch.utils.data.DataLoader(
test_data,
batch_size = batch_size,
shuffle = False,
)
validation_loader = torch.utils.data.DataLoader(
validation_data,
batch_size = batch_size,
shuffle = False,
)
return train_loader, test_loader, validation_loader
def main(args):
'''
TODO: Initialize a model by calling the net function
'''
model=net()
'''
TODO: Create your loss and optimizer
'''
loss_criterion = None
optimizer = None
'''
TODO: Call the train function to start training your model
Remember that you will need to set up a way to get training data from S3
'''
model=train(model, train_loader, loss_criterion, optimizer)
'''
TODO: Test the model to see its accuracy
'''
test(model, test_loader, criterion)
'''
TODO: Save the trained model
'''
torch.save(model, path)
if __name__=='__main__':
parser=argparse.ArgumentParser()
'''
TODO: Specify any training args that you might need
'''
args=parser.parse_args()
main(args)