(self, image, label)
| 32 | tf.TensorSpec((None,), tf.int32, 'label')] |
| 33 | |
| 34 | def build_graph(self, image, label): |
| 35 | drop_rate = tf.constant(0.5 if self.training else 0.0) |
| 36 | |
| 37 | if self.training: |
| 38 | tf.summary.image("train_image", image, 10) |
| 39 | if tf.test.is_gpu_available(): |
| 40 | image = tf.transpose(image, [0, 3, 1, 2]) |
| 41 | data_format = 'channels_first' |
| 42 | else: |
| 43 | data_format = 'channels_last' |
| 44 | |
| 45 | image = image / 4.0 # just to make range smaller |
| 46 | with argscope(Conv2D, activation=BNReLU, use_bias=False, kernel_size=3), \ |
| 47 | argscope([Conv2D, MaxPooling, BatchNorm], data_format=data_format): |
| 48 | logits = LinearWrap(image) \ |
| 49 | .Conv2D('conv1.1', filters=64) \ |
| 50 | .Conv2D('conv1.2', filters=64) \ |
| 51 | .MaxPooling('pool1', 3, stride=2, padding='SAME') \ |
| 52 | .Conv2D('conv2.1', filters=128) \ |
| 53 | .Conv2D('conv2.2', filters=128) \ |
| 54 | .MaxPooling('pool2', 3, stride=2, padding='SAME') \ |
| 55 | .Conv2D('conv3.1', filters=128, padding='VALID') \ |
| 56 | .Conv2D('conv3.2', filters=128, padding='VALID') \ |
| 57 | .FullyConnected('fc0', 1024 + 512, activation=tf.nn.relu) \ |
| 58 | .Dropout(rate=drop_rate) \ |
| 59 | .FullyConnected('fc1', 512, activation=tf.nn.relu) \ |
| 60 | .FullyConnected('linear', out_dim=self.cifar_classnum)() |
| 61 | |
| 62 | cost = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits, labels=label) |
| 63 | cost = tf.reduce_mean(cost, name='cross_entropy_loss') |
| 64 | |
| 65 | correct = tf.cast(tf.nn.in_top_k(predictions=logits, targets=label, k=1), tf.float32, name='correct') |
| 66 | # monitor training error |
| 67 | add_moving_summary(tf.reduce_mean(correct, name='accuracy')) |
| 68 | |
| 69 | # weight decay on all W of fc layers |
| 70 | wd_cost = regularize_cost('fc.*/W', l2_regularizer(4e-4), name='regularize_loss') |
| 71 | add_moving_summary(cost, wd_cost) |
| 72 | |
| 73 | add_param_summary(('.*/W', ['histogram'])) # monitor W |
| 74 | return tf.add_n([cost, wd_cost], name='cost') |
| 75 | |
| 76 | def optimizer(self): |
| 77 | lr = tf.get_variable('learning_rate', initializer=1e-2, trainable=False) |
nothing calls this directly
no test coverage detected