resnet50训练cifar10,请各位⾼⼿指正
使⽤resnet50从头训练cifar10,最终结果只有84%左右,貌似和 论⽂差很多,请各位⾼⼿指正。
⾸先加⼊cifar10的数据结构代码:
import cifar10,cifar10_input
import tensorflow as tf
import numpy as np
import time
#max_steps = 100000
max_steps = 100
data_dir = 'cifar-10-batches-bin'
batch_size = 128
# 配置每个 GPU 上占⽤的内存的⽐例
gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.95)
ss = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options))
images_train, labels_train = cifar10_input.distorted_inputs(data_dir=data_dir,batch_size=batch_size)
images_test, labels_test = cifar10_input.inputs(eval_data=True,data_dir=data_dir,batch_size=batch_size)
ss = tf.InteractiveSession()
tf.global_variables_initializer().run()
start_time = time.time()
image_batch,label_batch = ss.run([images_train,labels_train])
duration = time.time() - start_time
tynisha keliprint('U Time = %.3f c'%duration)
start_time = time.time()
image_batch,label_batch = ss.run([images_train,labels_train])
duration = time.time() - start_time
print('U Time = %.3f c'%duration)
加⼊resnet50的代码:
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import tensorflow as tf
闭嘴英语
阿卡索外教网怎么样import resnet_utils
resnet_arg_scope = snet_arg_scope
slim = tf.contrib.slim
class NoOpScope(object):
"""No-op context manager."""
def__enter__(lf):
return None
def__exit__(lf, exc_type, exc_value, traceback):
return Fal
@slim.add_arg_scope
def bottleneck(inputs,
depth,
depth_bottleneck,
stride,
rate=1,
outputs_collections=None,
scope=None,
u_bounded_activations=Fal):
"""Bottleneck residual unit variant with BN after convolutions.
This is the original residual unit propod in [1]. See Fig. 1(a) of [2] for
its definition. Note that we u here the bottleneck variant which has an
extra bottleneck layer.
When putting together two concutive ResNet blocks that u this unit, one
should u stride = 2 in the last unit of the first block.
Args:
inputs: A tensor of size [batch, height, width, channels].
depth: The depth of the ResNet unit output.
depth_bottleneck: The depth of the bottleneck layers.
stride: The ResNet unit's stride. Determines the amount of downsampling of
the units output compared to its input.
rate: An integer, rate for atrous convolution.
outputs_collections: Collection to add the ResNet unit output.
scope: Optional variable_scope.
u_bounded_activations: Whether or not to u bounded activations. Bounded activations better lend themlves to quantized inference.
Returns:
The ResNet unit's output.
"""
with tf.variable_scope(scope, 'bottleneck_v1', [inputs]) as sc:
depth_in = slim.utils.last__shape(), min_rank=4)
if depth == depth_in:
shortcut = resnet_utils.subsample(inputs, stride, 'shortcut')
el:
shortcut = v2d(
inputs,
depth, [1, 1],
stride=stride,
activation_lu6 if u_bounded_activations el None,
scope='shortcut')
residual = v2d(inputs, depth_bottleneck, [1, 1], stride=1,
scope='conv1')
residual = v2d_same(residual, depth_bottleneck, 3, stride,
rate=rate, scope='conv2')
residual = v2d(residual, depth, [1, 1], stride=1,
activation_fn=None, scope='conv3')
if u_bounded_activations:
# U clip_by_value to simulate bandpass activation.
residual = tf.clip_by_value(residual, -6.0, 6.0)
output = lu6(shortcut + residual)
el:
output = lu(shortcut + residual)
return llect_named_outputs(outputs_collections,
sc.name,
output)
def resnet_v1(inputs,
def resnet_v1(inputs,
blocks,
num_class=None,
is_training=True,
global_pool=True,
output_stride=None,
include_root_block=True,
spatial_squeeze=True,
store_non_strided_activations=Fal,
reu=None,
scope=None):
"""Generator for v1 ResNet models.
This function generates a family of ResNet v1 models. See the resnet_v1_*()
methods for specific model instantiations, obtained by lecting different
企业老板电话
block instantiations that produce ResNets of various depths.
学生会主席竞选演讲稿
Training for image classification on Imagenet is usually done with [224, 224]
inputs, resulting in [7, 7] feature maps at the output of the last ResNet
block for the ResNets defined in [1] that have nominal stride equal to 32.
However, for den prediction tasks we advi that one us inputs with
spatial dimensions that are multiples of 32 plus 1, e.g., [321, 321]. In
this ca the feature maps at the ResNet output will have spatial shape
[(height - 1) / output_stride + 1, (width - 1) / output_stride + 1]
and corners exactly aligned with the input image corners, which greatly
facilitates alignment of the features to the image. Using as input [225, 225]
images results in [8, 8] feature maps at the output of the last ResNet block.
For den prediction tasks, the ResNet needs to run in fully-convolutional
(FCN) mode and global_pool needs to be t to Fal. The ResNets in [1, 2] all have nominal stride equal to 32 and a good choice in FCN mode is to u
output_stride=16 in order to increa the density of the computed features at
small computational and memory overhead, cf. /abs/1606.00915.
Args:
inputs: A tensor of size [batch, height_in, width_in, channels].
blocks: A list of length equal to the number of ResNet blocks. Each element
is a resnet_utils.Block object describing the units in the block.
num_class: Number of predicted class for classification tasks.holly
If 0 or None, we return the features before the logit layer.
is_training: whether batch_norm layers are in training mode. If this is t
to None, the callers can specify slim.batch_norm's is_training parameter
from an outer slim.arg_scope.
global_pool: If True, we perform global average pooling before computing the logits. Set to True for image classification, Fal for den prediction.
output_stride: If None, then the output will be computed at the nominal
network stride. If output_stride is not None, it specifies the requested
ratio of input to output spatial resolution.双月刊
include_root_block: If True, include the initial convolution followed by
max-pooling, if Fal excludes it.
spatial_squeeze: if True, logits is of shape [B, C], if fal logits is
of shape [B, 1, 1, C], where B is batch_size and C is number of class.
To u this parameter, the input images must be smaller than 300x300
pixels, in which ca the output logit layer does not contain spatial
information and can be removed.
store_non_strided_activations: If True, we compute non-strided (undecimated) activations at the last unit of each block and store them in the
`outputs_collections` before subsampling them. This gives us access to
higher resolution intermediate activations which are uful in some
den prediction problems but increas 4x the computation and memory cost at the last unit of each block.
reu: whether or not the network and its variables should be reud. To be
able to reu 'scope' must be given.
scope: Optional variable_scope.
Returns:
Returns:
net: A rank-4 tensor of size [batch, height_out, width_out, channels_out].
If global_pool is Fal, then height_out and width_out are reduced by a
factor of output_stride compared to the respective height_in and width_in,
el both height_out and width_out equal one. If num_class is 0 or None, then net is the output of the last ResNet block, potentially after global
average pooling. If num_class a non-zero integer, net contains the
pre-softmax activations.
end_points: A dictionary from components of the network to the corresponding activation.
Rais:
ValueError: If the target output_stride is not valid.
"""
with tf.variable_scope(scope, 'resnet_v1', [inputs], reu=reu) as sc:
end_points_collection = sc.original_name_scope + '_end_points'
with slim.arg_scope([v2d, bottleneck,
resnet_utils.stack_blocks_den],
outputs_collections=end_points_collection):
with (slim.arg_scope([slim.batch_norm], is_training=is_training)
if is_training is not None el NoOpScope()):
net = inputs
if include_root_block:
if output_stride is not None:
英语小幽默if output_stride % 4 != 0:
rai ValueError('The output_stride needs to be a multiple of 4.')
output_stride /= 4
net = v2d_same(net, 64, 7, stride=2, scope='conv1')
net = slim.max_pool2d(net, [3, 3], stride=2, scope='pool1')
net = resnet_utils.stack_blocks_den(net, blocks, output_stride,
store_non_strided_activations)
# Convert end_points_collection into a dictionary of end_points.
end_points = vert_collection_to_dict(
end_points_collection)
if global_pool:
# Global average pooling.
net = tf.reduce_mean(net, [1, 2], name='pool5', keep_dims=True)如何跟客户沟通
end_points['global_pool'] = net
全国冻哭预警地图if num_class:
net = v2d(net, num_class, [1, 1], activation_fn=None,
normalizer_fn=None, scope='logits')
end_points[sc.name + '/logits'] = net
if spatial_squeeze:
net = tf.squeeze(net, [1, 2], name='SpatialSqueeze')
end_points[sc.name + '/spatial_squeeze'] = net
end_points['predictions'] = slim.softmax(net, scope='predictions') return net, end_points
resnet_v1.default_image_size = 224
def resnet_v1_block(scope, ba_depth, num_units, stride):
"""Helper function for creating a resnet_v1 bottleneck block.
Args:
scope: The scope of the block.
ba_depth: The depth of the bottleneck layer for each unit.
num_units: The number of units in the block.
stride: The stride of the block, implemented as a stride in the last unit.
All other units have stride=1.
Returns:
A resnet_v1 bottleneck block.
"""
return resnet_utils.Block(scope, bottleneck, [{
'depth': ba_depth * 4,
'depth_bottleneck': ba_depth,
'depth_bottleneck': ba_depth,
'stride': 1
}] * (num_units - 1) + [{
'depth': ba_depth * 4,
'depth_bottleneck': ba_depth,
'stride': stride
}])
def resnet_v1_50(inputs,
num_class=None,
is_training=True,
global_pool=True,
output_stride=None,
spatial_squeeze=True,
store_non_strided_activations=Fal,
reu=None,
scope='resnet_v1_50'):
"""ResNet-50 model of [1]. See resnet_v1() for arg and return description."""
blocks = [
resnet_v1_block('block1', ba_depth=64, num_units=3, stride=2),
resnet_v1_block('block2', ba_depth=128, num_units=4, stride=2),
resnet_v1_block('block3', ba_depth=256, num_units=6, stride=2),
resnet_v1_block('block4', ba_depth=512, num_units=3, stride=1),
]
return resnet_v1(inputs, blocks, num_class, is_training,
global_pool=global_pool, output_stride=output_stride,
include_root_block=True, spatial_squeeze=spatial_squeeze,
store_non_strided_activations=store_non_strided_activations,
reu=reu, scope=scope)
resnet_v1_50.default_image_size = 24
建⽴resnet50⽹络
height, width = 24, 24
image_holder = tf.placeholder(tf.float32, [batch_size, 24, 24, 3])
label_holder = tf.placeholder(tf.int32, [batch_size])
with slim.arg_scope(resnet_arg_scope()):
net, end_points = resnet_v1_50(image_holder,10)
print(end_points)
#看看⽹络情况
tf.global_variables_initializer().run()
e_net,e_end_points= ss.run([net,end_points],feed_dict={image_holder:image_batch,label_holder:label_batch})
print(e_net.shape)#应该是最后输出值
print(e_end_points)#应该是整个⽹络节点参数
定义loss函数,优化器: