-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtrain_code.py
More file actions
65 lines (54 loc) · 2.31 KB
/
train_code.py
File metadata and controls
65 lines (54 loc) · 2.31 KB
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
from arcface import Arcface_Layer
from network import Resnet
import tensorflow as tf
import hypar
import time
import numpy as np
from numpy import load
class train_model(tf.keras.Model):
def __init__(self):
super(train_model, self).__init__()
self.resnet = Resnet()
self.arcface = Arcface_Layer()
def call(self, x, y):
x = self.resnet(x)
return self.arcface(x, y)
# Instantiate a loss function.
def loss_fxn(logits,labels):
loss_fn = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits, labels=labels))
return loss_fn
# Instantiate an optimizer to train the model.
optimizer = tf.keras.optimizers.Adam(lr=hypar.learning_rate)
model = train_model()
@tf.function
def train_step(images, labels):
with tf.GradientTape() as tape:
logits = model(images,labels)
pred = tf.nn.softmax(logits)
inf_loss = loss_fxn(pred,labels)
loss = inf_loss
gradients = tape.gradient(loss, model.trainable_variables)
optimizer.apply_gradients(zip(gradients, model.trainable_variables))
train_loss = tf.reduce_mean(loss)
accuracy = tf.reduce_mean(tf.cast(tf.equal(tf.argmax(pred, axis=1, output_type=tf.dtypes.int32), labels), dtype=tf.float32))
inference_loss = tf.reduce_mean(inf_loss)
regularization_loss = 0
return accuracy, train_loss, inference_loss, regularization_loss
images = load('x_train.npy')
labels = load('y_train.npy')
# Prepare the training dataset.
train_dataset = tf.data.Dataset.from_tensor_slices((images, labels))
train_dataset = train_dataset.shuffle(buffer_size=1024).batch(hypar.batch_size)
epochs = 2
for epoch in range(epochs):
print("\nStart of epoch %d" % (epoch,))
start_time = time.time()
# Iterate over the batches of the dataset.
for step, (x_batch_train, y_batch_train) in enumerate(train_dataset):
accuracy, train_loss, inference_loss, regularization_loss = train_step(x_batch_train, y_batch_train, hypar.reg_coef)
# Log every 200 batches.
if step % 200 == 0:
print("Training loss (for one batch) at step %d: %.4f"% (step, float(train_loss)))
print("Seen so far: %d samples" % ((step + 1) * hypar.batch_size))
print("Training acc over epoch: %.4f" % (float(accuracy)))
print("Time taken: %.2fs" % (time.time() - start_time))