-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathtrainModel.py
218 lines (160 loc) · 7.45 KB
/
trainModel.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Jul 12 19:01:52 2018
@author: dudley
"""
import matplotlib.pyplot as plt
import numpy as np
import os, argparse
import torch
from torch import nn
from torch.autograd import Variable
from torch.optim import Adam
from vae import ConvAEDeep
'''
This script is intended to train a 1D Convolutional Autoencoder for the purpose
of generating feature vectors from input well logs that can be used in the
process of automated well correlation.
Run from the command line and provide required command line arguements:
--model-name : Name of model being trained
--data-dir : Location training data
--output-dir : Location where model will be saved
--imgs-dir : Location where the images will be saved
--save-imgs : Save images or not
--epochs : Number of epochs to train model
--batch-size : Not exactly batch size, how to split the training data into batches
--split-ratio : Test, train split ratio
--skip-inc : Decimation factor, skip training data to reduce memory overhead
'''
class TrainModel:
def __init__(self, args):
self.modelName = args.model_name
self.dataDir = args.data_dir
self.dataSplitRatio = args.split_ratio
self.outputDir = args.output_dir
self.saveImgs = args.save_imgs
self.imgsDir = args.imgs_dir
self.epochs = args.epochs
self.batchSize = args.batch_size
self.skipInc = args.skip_inc
self.run()
# Retrieve the training data from disk and format appropriately
def getTrainingData(self):
# Get files from directory, subsample, and shuffle
files = os.listdir(self.dataDir)[::self.skipInc]
np.random.shuffle(files)
# Determine split and load data
trainTestSplit = int(len(files) * self.dataSplitRatio)
trainData = [np.load(os.path.join(self.dataDir, f)) for f in files[:trainTestSplit]]
trainData = np.vstack(trainData)
testData = [np.load(os.path.join(self.dataDir, f)) for f in files[trainTestSplit:]]
testData = np.vstack(testData)
self.data = {'train' : trainData, 'test' : testData}
print('Training data loaded')
# Create a plot of training & testing loss and Input vs Predicted data
def plotData(self, trainLoss, testLoss, inImg, outImg, epoch, phase):
# Initialize plot
f, ax = plt.subplots(1, 2, figsize=(20, 5))
# Create plot of testing and training loss
ax[0].plot(trainLoss, label='Train loss')
ax[0].plot(testLoss, label='Test loss')
ax[0].set_ylabel('Loss (log)')
ax[0].legend()
ax[0].set_yscale('log')
# Create plot of Input curve vs Predicted curve
ax[1].set_title('Curves')
ax[1].plot(inImg, c='b', label='Target')
ax[1].plot(outImg, c='r', label='Predicted')
ax[1].set_yticklabels([])
ax[1].legend()
plt.subplots_adjust(wspace=0)
path = os.path.join(self.imgsDir,
'epoch_{}_{}.png'.format(self.modelName, epoch, phase))
f.savefig(path)
# Run the script
def run(self):
self.setPaths()
self.getTrainingData()
self.setModel()
self.trainModel()
self.saveModel()
# Save model to disk
def saveModel(self):
path = os.path.join(self.outputDir, self.modelName)
torch.save(self.model.state_dict(), path)
print('Model saved')
# Import model, set optimizer and criterion
def setModel(self):
self.model = ConvAEDeep().cuda()
self.optimizer = Adam(self.model.parameters(), lr=1e-3)
self.criterion = nn.MSELoss().cuda()
print('Model set')
# Assess whether paths provided exist, if not create
def setPaths(self):
if not os.path.exists(self.dataDir):
print('Directory of Training Data does not exist')
if not os.path.exists(self.outputDir):
os.mkdir(self.outputDir)
if not os.path.exists(self.imgsDir):
os.mkdir(self.imgsDir)
# Train model
def trainModel(self):
# Record of running losses
trainLoss = []
testLoss = []
# Iterate over model
for epoch in range(self.epochs) :
# For each epoch differentiate between testing and training pahses
for phase in ['train', 'test']:
# Set the model mode and determine the appropriate number of batches
if phase =='train':
self.model.train()
shape = self.data['train'].shape[0]
splits = np.array_split(range(shape), self.batchSize)
else:
self.model.eval()
shape = self.data['test'].shape[0]
splits = np.array_split(range(shape), self.batchSize)
# Iterate over batches, get outputs, compute error, update model weights
running_loss = 0.0
for num, batch in enumerate(splits):
inputs = Variable(torch.from_numpy(self.data[phase][batch])).float().cuda()
self.optimizer.zero_grad()
with torch.set_grad_enabled(phase == 'train'):
outputs = self.model(inputs)
loss = self.criterion(outputs, inputs)
if phase == 'train':
loss.backward()
self.optimizer.step()
running_loss += loss.item() * inputs.size(0)
# Capture epoch loss
epoch_loss = running_loss / inputs.size(0)
if phase == 'train':
trainLoss.append(epoch_loss)
else:
testLoss.append(epoch_loss)
# Save images
if self.saveImgs:
imgNum = np.random.randint(0, inputs.size(0))
inImg = inputs[imgNum].cpu().data.numpy().squeeze()
outImg = outputs[imgNum].cpu().data.numpy().squeeze()
self.plotData(trainLoss, testLoss, inImg, outImg, epoch, phase)
print('-' * 50)
print('Epoch {} {}'.format(epoch, phase))
print('Loss: {}'.format(epoch_loss))
if __name__ == '__main__':
# Get command line arguements
parser = argparse.ArgumentParser()
arg = parser.add_argument
arg('--model-name', type=str, default='model')
arg('--data-dir', type=str)
arg('--output-dir', type=str)
arg('--imgs-dir', type=str)
arg('--save-imgs', type=bool, default=True)
arg('--epochs', type=int, default=250)
arg('--batch-size', type=int, default=25)
arg('--split-ratio', type=float, default=0.75)
arg('--skip-inc', type=int, default=1)
args = parser.parse_args()
TrainModel(args)