def inception_v3_parameters(weight_decay=0.00004, stddev=0.1,
batch_norm_decay=0.9997, batch_norm_epsilon=0.001):
"""Yields the scope with the default parameters for inception_v3.
Args:
weight_decay: the weight decay for weights variables.
stddev: standard deviation of the truncated guassian weight distribution.
batch_norm_decay: decay for the moving average of batch_norm momentums.
batch_norm_epsilon: small float added to variance to avoid dividing by zero.
Yields:
a arg_scope with the parameters needed for inception_v3.
"""
# Set weight_decay for weights in Conv and FC layers.
with scopes.arg_scope([ops.conv2d, ops.fc],
weight_decay=weight_decay):
# Set stddev, activation and parameters for batch_norm.
with scopes.arg_scope([ops.conv2d],
stddev=stddev,
activation=tf.nn.relu,
batch_norm_params={
'decay': batch_norm_decay,
'epsilon': batch_norm_epsilon}) as arg_scope:
yield arg_scope
python类fc()的实例源码
def __call__(self, z, y):
"""
:param z: 2D [batch_size, z_dim]
:param y: 2D [batch_size, y_dim]
:return:
"""
batch_size, y_dim = y.get_shape().as_list()
batch_size_, z_dim = z.get_shape().as_list()
assert batch_size == batch_size_
h1_size = int(self._output_size / 4)
h2_size = int(self._output_size / 2)
with tf.variable_scope(self._name):
yb = tf.reshape(y, shape=[-1, 1, 1, y_dim]) # (100, 1, 1, 10)
z = tf.concat([z, y], axis=1) # (batch_size=100, y_dim+z_dim=110)
h0 = tf.nn.relu(
ops.batch_norm(
ops.fc(z, self._fc_dim, reuse=self._reuse, name='g_fc0'),
is_training=self._is_training,
reuse=self._reuse,
name_scope='g_bn0'
)
)
h0 = tf.concat([h0, y], axis=1) # (batch_size=100, fc_dim+y_dim=794)
h1 = tf.nn.relu(
ops.batch_norm(
ops.fc(h0, self._ngf*h1_size*h1_size, reuse=self._reuse, name='g_fc1'),
is_training=self._is_training,
reuse=self._reuse,
name_scope='g_bn1'
)
)
h1 = tf.reshape(h1, shape=[-1, h1_size, h1_size, self._ngf])
h1 = tf.concat([h1, yb*tf.ones([batch_size, h1_size, h1_size, y_dim])], axis=3) # (100, 7, 7, 522)
h2 = tf.nn.relu(
ops.batch_norm(
ops.deconv2d(h1, self._ngf, reuse=self._reuse, name='g_conv2'),
is_training=self._is_training,
reuse=self._reuse,
name_scope='g_bn2'
)
)
h2 = tf.concat([h2, yb*tf.ones([batch_size, h2_size, h2_size, y_dim])], axis=3) # (100, 14, 14, 522)
h3 = tf.nn.sigmoid(
ops.deconv2d(h2, self._channel_dim, reuse=self._reuse, name='g_conv3')
) # TODO DIMENSION??? SHRINK
self._reuse = True
return h3 # (100, 28, 28, 1)
def __call__(self, input_, y):
batch_size, y_dim = y.get_shape().as_list()
batch_size_, height, width, c_dim = input_.get_shape().as_list()
assert batch_size == batch_size_
assert (self._input_size == width) and (self._input_size == height)
h0_size = int(self._input_size / 2)
h1_size = int(self._input_size / 4)
with tf.variable_scope(self._name):
yb = tf.reshape(y, shape=[-1, 1, 1, y_dim])
# dim(x) = (100, 28, 28, 11)
x = tf.concat([input_, yb*tf.ones([batch_size, self._input_size, self._input_size, y_dim])], axis=3)
h0 = ops.leaky_relu(
ops.conv2d(x, c_dim + y_dim, reuse=self._reuse, name='d_conv0'),
slope=0.2
)
h0 = tf.concat([h0, yb*tf.ones([batch_size, h0_size, h0_size, y_dim])], axis=3) # (100, 14, 14, 21)
h1 = ops.leaky_relu(
ops.batch_norm(
ops.conv2d(h0, c_dim + self._ndf, reuse=self._reuse, name='d_conv1'),
is_training=self._is_training,
reuse=self._reuse,
name_scope='d_bn1'
),
slope=0.2
)
h1 = tf.reshape(h1, [batch_size, h1_size*h1_size*(c_dim+self._ndf)])
h1 = tf.concat([h1, y], axis=1) # (100, 28*28*(1+64)+10)
h2 = ops.leaky_relu(
ops.batch_norm(
ops.fc(h1, self._fc_dim, reuse=self._reuse, name='d_fc2'),
is_training=self._is_training,
reuse=self._reuse,
name_scope='d_bn2'
),
slope=0.2
)
h2 = tf.concat([h2, y], axis=1) # (100, 794)
# h3 = tf.nn.sigmoid(
h3 = ops.fc(h2, 1, reuse=self._reuse, name='d_fc3')
# )
self._reuse = True
return h3 # (100, 1)
def discriminator(images, labels, reuse=False):
with tf.variable_scope("discriminator") as scope:
if reuse:
scope.reuse_variables()
# conv1
conv1 = ops.conv_2d(images, 64, scope="conv1")
# leakly ReLu
h1 = ops.leaky_relu(conv1)
# conv2
conv2 = ops.conv_2d(h1, 128, scope="conv2")
# batch norm
norm2 = ops.batch_norm(conv2, scope="batch_norm2", is_training=True)
# leaky ReLU
h2 = ops.leaky_relu(norm2)
# conv3
conv3 = ops.conv_2d(h2, 256, scope="conv3")
# batch norm
norm3 = ops.batch_norm(conv3, scope="batch_norm3", is_training=True)
# leaky ReLU
h3 = ops.leaky_relu(norm3)
# conv4
conv4 = ops.conv_2d(h3, 512, scope="conv4")
# batch norm
norm4 = ops.batch_norm(conv4, scope="batch_norm4", is_training=True)
# leaky ReLU
h4 = ops.leaky_relu(norm4)
# reshape
h4_reshape = tf.reshape(h4, [FLAGS.batch_size, -1])
# source logits
source_logits = ops.fc(h4_reshape, 1, scope="source_logits")
# class logits
class_logits = ops.fc(
h4_reshape, FLAGS.n_classes, scope="class_logits")
return source_logits, class_logits
def build(self, is_train=True):
n = self.a_dim
conv_info = self.conv_info
# build loss and accuracy {{{
def build_loss(logits, labels):
# Cross-entropy loss
loss = tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=labels)
# Classification accuracy
correct_prediction = tf.equal(tf.argmax(logits, 1), tf.argmax(labels, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
return tf.reduce_mean(loss), accuracy
# }}}
# Classifier: takes images as input and outputs class label [B, m]
def C(img, q, scope='Classifier'):
with tf.variable_scope(scope) as scope:
log.warn(scope.name)
conv_1 = conv2d(img, conv_info[0], is_train, s_h=3, s_w=3, name='conv_1')
conv_2 = conv2d(conv_1, conv_info[1], is_train, s_h=3, s_w=3, name='conv_2')
conv_3 = conv2d(conv_2, conv_info[2], is_train, name='conv_3')
conv_4 = conv2d(conv_3, conv_info[3], is_train, name='conv_4')
conv_q = tf.concat([tf.reshape(conv_4, [self.batch_size, -1]), q], axis=1)
fc_1 = fc(conv_q, 256, name='fc_1')
fc_2 = fc(fc_1, 256, name='fc_2')
fc_2 = slim.dropout(fc_2, keep_prob=0.5, is_training=is_train, scope='fc_3/')
fc_3 = fc(fc_2, n, activation_fn=None, name='fc_3')
return fc_3
logits = C(self.img, self.q, scope='Classifier')
self.all_preds = tf.nn.softmax(logits)
self.loss, self.accuracy = build_loss(logits, self.a)
# Add summaries
def draw_iqa(img, q, target_a, pred_a):
fig, ax = tfplot.subplots(figsize=(6, 6))
ax.imshow(img)
ax.set_title(question2str(q))
ax.set_xlabel(answer2str(target_a)+answer2str(pred_a, 'Predicted'))
return fig
try:
tfplot.summary.plot_many('IQA/',
draw_iqa, [self.img, self.q, self.a, self.all_preds],
max_outputs=3,
collections=["plot_summaries"])
except:
pass
tf.summary.scalar("loss/accuracy", self.accuracy)
tf.summary.scalar("loss/cross_entropy", self.loss)
log.warn('Successfully loaded the model.')