为什么我自己写的tensorflow框架inception_v3跑出来的结果这么低?准确度一直在徘徊,对于猫狗大战一直维持在0.5左右和没有分类一样。但是我用keras写的只是一个简单的网络分类结果都能到80.能否帮我把我写的tf版本代码找出问题并修改对?
kreas:
model = Sequential() model.add(Conv2D(32, (3, 3), input_shape=input_shape)) model.add(Activation('relu')) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Conv2D(32, (3, 3))) model.add(Activation('relu')) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Conv2D(64, (3, 3))) model.add(Activation('relu')) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Flatten()) model.add(Dense(64)) model.add(Activation('relu')) model.add(Dropout(0.5)) model.add(Dense(1)) model.add(Activation('sigmoid'))
我自己的:
with tf.variable_scope('model', reuse=reuse): end_points = {} end_point = 'conv0' net = my_layer.conv2layer(inputs, [3, 3], 32, padding='VALID', activation='relu', trainable=is_training, name=end_point) end_points[end_point] = net end_point = 'pool0' net = my_layer.pool2layer(net, [2, 2], mode='max', name=end_point) end_points[end_point] = net end_point = 'conv1' net = my_layer.conv2layer(net, [3, 3], 32, padding='VALID', activation='relu', trainable=is_training, name=end_point) end_points[end_point] = net end_point = 'pool1' net = my_layer.pool2layer(net, [2, 2], mode='max', name=end_point) end_points[end_point] = net end_point = 'conv2' net = my_layer.conv2layer(net, [3, 3], 64, padding='VALID', activation='relu', trainable=is_training, name=end_point) end_points[end_point] = net end_point = 'pool2' net = my_layer.pool2layer(net, [2, 2], mode='max', name=end_point) end_points[end_point] = net end_point = 'flatten' net = my_layer.conv2layer(net, [17, 17], 64, batch_normal=True, drop_out=0.5, padding='VALID', weight_decay=weight_decay, trainable=is_training, name=end_point) end_points[end_point] = net end_point = 'logits' logits = my_layer.conv2layer(net, [1, 1], num_class, batch_normal=True, activation='none', padding='VALID', weight_decay=weight_decay, trainable=is_training, name=end_point) end_points[end_point] = logits if spatial_squeeze: end_point = 'logits_spatialsqueeze' logits = tf.squeeze(logits, [1, 2], name=end_point) end_points[end_point] = logits end_points['Predictions'] = prediction_fn(logits, name='Predictions') return logits, end_points
from tensorflow.contrib.layers import batch_norm as bn
trunc_normal = lambda stddev: tf.truncated_normal_initializer(0.0, stddev)
def define_weight_trunc_normal(shape, kernel_elements, trainable):
weight = tf.get_variable('W', shape=shape,
initializer=trunc_normal(np.sqrt(2 / kernel_elements)),
trainable=trainable)
return weight
def conv2layer(input, kernel_size, out_size, stride=[1, 1], padding='SAME', activation='relu',
batch_normal=False, weight_decay=None, drop_out=0,
trainable=True, name='conv'):
# Args:
# inputs: a tensor of size [batch_size, height, width, channels].
# kernel_size: the size of the convolution kernel.
# out_size: the nums of the outp0ut feature maps
# stride: the slid size of the windows
with tf.variable_scope('%s' % name):
# 权重矩阵
kernel_shape = [kernel_size[0], kernel_size[1], input.get_shape().as_list()[3], out_size]
kernel_elements = input.get_shape().as_list()[1] * input.get_shape().as_list()[2] * input.get_shape().as_list()[3]
weight = define_weight_trunc_normal(shape=kernel_shape, kernel_elements=kernel_elements, trainable=trainable)
# 偏置向量
bias = define_bias(out_size=out_size, trainable=trainable)
# weight decay技术
if weight_decay:
weight_decay = tf.multiply(tf.nn.l2_loss(weight), weight_decay)
tf.add_to_collection('loss_weight_decay', weight_decay)
# hidden states
conv = tf.nn.conv2d(
input=input, filter=weight,
strides=[1, stride[0], stride[1], 1], padding=padding)
# batch normalization 技术
if batch_normal:
hidden = bn(conv, trainable=trainable, scope='batchnorm')
else:
hidden = conv + bias
# dropout 技术
if drop_out:
keep_prob = drop_out
hidden = tf.nn.dropout(hidden, keep_prob=keep_prob)
# activation
if activation == 'relu':
output = tf.nn.relu(hidden)
elif activation == 'tanh':
output = tf.nn.tanh(hidden)
elif activation == 'none':
output = hidden
return output
def pool2layer(input, kernel_size, stride=[2, 2], mode='avg',
padding='SAME', resp_normal=False, name='pool'):
# Args:
# inputs: a tensor of size [batch_size, height, width, channels].
# kernel_size: the size of the pooling kernel.
# out_size: the nums of the output feature maps
# stride: the slid size of the windows
with tf.variable_scope('%s' % name):
if mode == 'max':
pool = tf.nn.max_pool(
value=input, ksize=[1, kernel_size[0], kernel_size[1], 1],
strides=[1, stride[0], stride[1], 1], padding=padding)
elif mode == 'avg':
pool = tf.nn.avg_pool(
value=input, ksize=[1,kernel_size[0], kernel_size[1], 1],
strides=[1, stride[0], stride[1], 1], padding=padding)
# response normal 技术
if resp_normal:
hidden = tf.nn.local_response_normalization(
pool, depth_radius=7, alpha=0.001, beta=0.75)
else:
hidden = pool
output = hidden
return output
发表回答
你还没有登录,请先登录 或 注册!