首页 > 编程 > Python > 正文

使用tensorflow实现AlexNet

2020-02-16 10:46:48
字体:
来源:转载
供稿:网友

AlexNet是2012年ImageNet比赛的冠军,虽然过去了很长时间,但是作为深度学习中的经典模型,AlexNet不但有助于我们理解其中所使用的很多技巧,而且非常有助于提升我们使用深度学习工具箱的熟练度。尤其是我刚入门深度学习,迫切需要一个能让自己熟悉tensorflow的小练习,于是就有了这个小玩意儿......

先放上我的代码:https://github.com/hjptriplebee/AlexNet_with_tensorflow

如果想运行代码,详细的配置要求都在上面链接的readme文件中了。本文建立在一定的tensorflow基础上,不会对太细的点进行说明。

模型结构

关于模型结构网上的文献很多,我这里不赘述,一会儿都在代码里解释。

有一点需要注意,AlexNet将网络分成了上下两个部分,在论文中两部分结构完全相同,唯一不同的是他们放在不同GPU上训练,因为每一层的feature map之间都是独立的(除了全连接层),所以这相当于是提升训练速度的一种方法。很多AlexNet的复现都将上下两部分合并了,因为他们都是在单个GPU上运行的。虽然我也是在单个GPU上运行,但是我还是很想将最原始的网络结构还原出来,所以我的代码里也是分开的。

模型定义

def maxPoolLayer(x, kHeight, kWidth, strideX, strideY, name, padding = "SAME"):   """max-pooling"""   return tf.nn.max_pool(x, ksize = [1, kHeight, kWidth, 1],              strides = [1, strideX, strideY, 1], padding = padding, name = name)  def dropout(x, keepPro, name = None):   """dropout"""   return tf.nn.dropout(x, keepPro, name)  def LRN(x, R, alpha, beta, name = None, bias = 1.0):   """LRN"""   return tf.nn.local_response_normalization(x, depth_radius = R, alpha = alpha,                        beta = beta, bias = bias, name = name)  def fcLayer(x, inputD, outputD, reluFlag, name):   """fully-connect"""   with tf.variable_scope(name) as scope:     w = tf.get_variable("w", shape = [inputD, outputD], dtype = "float")     b = tf.get_variable("b", [outputD], dtype = "float")     out = tf.nn.xw_plus_b(x, w, b, name = scope.name)     if reluFlag:       return tf.nn.relu(out)     else:       return out  def convLayer(x, kHeight, kWidth, strideX, strideY,        featureNum, name, padding = "SAME", groups = 1):#group为2时等于AlexNet中分上下两部分   """convlutional"""   channel = int(x.get_shape()[-1])#获取channel   conv = lambda a, b: tf.nn.conv2d(a, b, strides = [1, strideY, strideX, 1], padding = padding)#定义卷积的匿名函数   with tf.variable_scope(name) as scope:     w = tf.get_variable("w", shape = [kHeight, kWidth, channel/groups, featureNum])     b = tf.get_variable("b", shape = [featureNum])      xNew = tf.split(value = x, num_or_size_splits = groups, axis = 3)#划分后的输入和权重     wNew = tf.split(value = w, num_or_size_splits = groups, axis = 3)      featureMap = [conv(t1, t2) for t1, t2 in zip(xNew, wNew)] #分别提取feature map     mergeFeatureMap = tf.concat(axis = 3, values = featureMap) #feature map整合     # print mergeFeatureMap.shape     out = tf.nn.bias_add(mergeFeatureMap, b)     return tf.nn.relu(tf.reshape(out, mergeFeatureMap.get_shape().as_list()), name = scope.name) #relu后的结果            
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表