This repository has been archived by the owner on Jun 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathcifar10-vgg-small.py
executable file
·155 lines (128 loc) · 5.27 KB
/
cifar10-vgg-small.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
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
# File: cifar10-vgg-small.py
import argparse
import os
import tensorflow as tf
from tensorflow.contrib.layers import variance_scaling_initializer
from tensorpack import *
from tensorpack.dataflow import dataset
from tensorpack.tfutils.summary import *
from tensorpack.tfutils.symbolic_functions import *
from tensorpack.utils.gpu import get_nr_gpu
from learned_quantization import *
BATCH_SIZE = 100
NUM_UNITS = None
WEIGHT_DECAY = 5e-4
class Model(ModelDesc):
def __init__(self, qw=1, qa=0):
super(Model, self).__init__()
self.qw = qw
self.qa = qa
def _get_inputs(self):
return [InputDesc(tf.float32, [None, 32, 32, 3], 'input'),
InputDesc(tf.int32, [None], 'label')]
def _build_graph(self, inputs):
image, label = inputs
image = image / 128.0
assert tf.test.is_gpu_available()
image = tf.transpose(image, [0, 3, 1, 2])
with argscope([Conv2DQuant, MaxPooling, BatchNorm], data_format='NCHW'), \
argscope(Conv2DQuant, nl=tf.identity, use_bias=False, kernel_shape=3,
W_init=variance_scaling_initializer(mode='FAN_IN'),
nbit=self.qw, is_quant=True if self.qw > 0 else False):
l = Conv2DQuant('conv0', image, 128, nl=BNReLU, is_quant=False)
if self.qa > 0:
l = QuantizedActiv('quant1', l, self.qa)
l = Conv2DQuant('conv1', l, 128)
# 32
l = MaxPooling('pool2', l, shape=2, stride=2, padding='VALID')
l = BNReLU('bn2', l)
if self.qa > 0:
l = QuantizedActiv('quant2', l, self.qa)
l = Conv2DQuant('conv2', l, 256, nl=BNReLU)
if self.qa > 0:
l = QuantizedActiv('quant3', l, self.qa)
l = Conv2DQuant('conv3', l, 256)
# 16
l = MaxPooling('pool4', l, shape=2, stride=2, padding='VALID')
l = BNReLU('bn4', l)
if self.qa > 0:
l = QuantizedActiv('quant4', l, self.qa)
l = Conv2DQuant('conv4', l, 512, nl=BNReLU)
if self.qa > 0:
l = QuantizedActiv('quant5', l, self.qa)
l = Conv2DQuant('conv5', l, 512)
# 8
l = MaxPooling('pool6', l, shape=2, stride=2, padding='VALID')
l = BNReLU('bn6', l)
# 4
logits = FullyConnected('linear', l, out_dim=10, nl=tf.identity)
prob = tf.nn.softmax(logits, name='output')
cost = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits, labels=label)
cost = tf.reduce_mean(cost, name='cross_entropy_loss')
wrong = prediction_incorrect(logits, label)
# monitor training error
add_moving_summary(tf.reduce_mean(wrong, name='train_error'))
wd_cost = tf.multiply(WEIGHT_DECAY, regularize_cost('.*/W', tf.nn.l2_loss), name='wd_cost')
add_moving_summary(cost, wd_cost)
add_param_summary(('.*/W', ['histogram'])) # monitor W
self.cost = tf.add_n([cost, wd_cost], name='cost')
def _get_optimizer(self):
lr = get_scalar_var('learning_rate', 0.02, summary=True)
opt = tf.train.MomentumOptimizer(lr, 0.9)
return opt
def get_data(train_or_test):
isTrain = train_or_test == 'train'
ds = dataset.Cifar10(train_or_test)
pp_mean = ds.get_per_pixel_mean()
if isTrain:
augmentors = [
imgaug.CenterPaste((40, 40)),
imgaug.RandomCrop((32, 32)),
imgaug.Flip(horiz=True),
imgaug.MapImage(lambda x: x - pp_mean),
]
else:
augmentors = [
imgaug.MapImage(lambda x: x - pp_mean)
]
ds = AugmentImageComponent(ds, augmentors)
ds = BatchData(ds, BATCH_SIZE, remainder=not isTrain)
if isTrain:
ds = PrefetchData(ds, 3, 2)
return ds
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--gpu', help='comma separated list of GPU(s) to use.')
parser.add_argument('--qw',
help='quantization weight',
type=int, default=1)
parser.add_argument('--qa',
help='quantization activation',
type=int, default=0)
parser.add_argument('--load', help='load model')
parser.add_argument('--logdir', help='identify of logdir',
type=str, default='cifar10-vgg-small')
args = parser.parse_args()
if args.gpu:
os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu
logger.set_logger_dir(
os.path.join('train_log', args.logdir))
dataset_train = get_data('train')
dataset_test = get_data('test')
config = TrainConfig(
model=Model(qw=args.qw, qa=args.qa),
dataflow=dataset_train,
callbacks=[
ModelSaver(),
InferenceRunner(dataset_test,
[ScalarStats('cost'), ClassificationError()]),
ScheduledHyperParamSetter('learning_rate',
[(1, 0.02), (80, 0.002), (160, 0.0002), (300, 0.00002)])
],
max_epoch=400,
nr_tower=max(get_nr_gpu(), 1),
session_init=SaverRestore(args.load) if args.load else None
)
SyncMultiGPUTrainerParameterServer(config).train()