-
Notifications
You must be signed in to change notification settings - Fork 88
/
Copy pathyolov2_train.py
102 lines (89 loc) · 2.73 KB
/
yolov2_train.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
import time
import cv2
import numpy as np
import chainer
import glob
import os
from chainer import serializers, optimizers, Variable, cuda
import chainer.functions as F
from yolov2 import *
from lib.utils import *
from lib.image_generator import *
# hyper parameters
train_sizes = [320, 352, 384, 416, 448]
item_path = "./items"
background_path = "./backgrounds"
initial_weight_file = "./backup/partial.model"
backup_path = "backup"
backup_file = "%s/backup.model" % (backup_path)
batch_size = 16
max_batches = 30000
learning_rate = 1e-5
learning_schedules = {
"0" : 1e-5,
"500" : 1e-4,
"10000": 1e-5,
"20000": 1e-6
}
lr_decay_power = 4
momentum = 0.9
weight_decay = 0.005
n_classes = 10
n_boxes = 5
# load image generator
print("loading image generator...")
generator = ImageGenerator(item_path, background_path)
# load model
print("loading initial model...")
yolov2 = YOLOv2(n_classes=n_classes, n_boxes=n_boxes)
model = YOLOv2Predictor(yolov2)
serializers.load_hdf5(initial_weight_file, model)
model.predictor.train = True
model.predictor.finetune = False
cuda.get_device(0).use()
model.to_gpu()
optimizer = optimizers.MomentumSGD(lr=learning_rate, momentum=momentum)
optimizer.use_cleargrads()
optimizer.setup(model)
#optimizer.add_hook(chainer.optimizer.WeightDecay(weight_decay))
# start to train
print("start training")
for batch in range(max_batches):
if str(batch) in learning_schedules:
optimizer.lr = learning_schedules[str(batch)]
if batch % 80 == 0:
input_width = input_height = train_sizes[np.random.randint(len(train_sizes))]
# generate sample
x, t = generator.generate_samples(
n_samples=16,
n_items=3,
crop_width=input_width,
crop_height=input_height,
min_item_scale=0.5,
max_item_scale=2.5,
rand_angle=15,
minimum_crop=0.8,
delta_hue=0.01,
delta_sat_scale=0.5,
delta_val_scale=0.5
)
x = Variable(x)
x.to_gpu()
# forward
loss = model(x, t)
print("batch: %d input size: %dx%d learning rate: %f loss: %f" % (batch, input_height, input_width, optimizer.lr, loss.data))
print("/////////////////////////////////////")
# backward and optimize
optimizer.zero_grads()
loss.backward()
optimizer.update()
# save model
if (batch+1) % 500 == 0:
model_file = "%s/%s.model" % (backup_path, batch+1)
print("saving model to %s" % (model_file))
serializers.save_hdf5(model_file, model)
serializers.save_hdf5(backup_file, model)
print("saving model to %s/yolov2_final.model" % (backup_path))
serializers.save_hdf5("%s/yolov2_final.model" % (backup_path), model)
model.to_cpu()
serializers.save_hdf5("%s/yolov2_final_cpu.model" % (backup_path), model)