前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >tensorflow版的bvlc模型

tensorflow版的bvlc模型

作者头像
Gxjun
发布2018-03-27 10:19:26
6920
发布2018-03-27 10:19:26
举报
文章被收录于专栏:ml

     研究相关的图片分类,偶然看到bvlc模型,但是没有tensorflow版本的,所以将caffe版本的改成了tensorflow的:

关于模型这个图:

下面贴出通用模板:

代码语言:javascript
复制
  1 from __future__ import print_function
  2 import tensorflow as tf
  3 import numpy as np
  4 from scipy.misc import imread, imresize
  5 
  6 
  7 class BVLG:
  8     def __init__(self, imgs, weights=None, sess=None):
  9         self.imgs = imgs
 10         self.convlayers()
 11         self.fc_layers()
 12 
 13         self.probs = tf.nn.softmax(self.fc3l)
 14         if weights is not None and sess is not None:
 15             self.load_weights(weights,sess)
 16 
 17     def convlayers(self):
 18         self.parameters = []
 19 
 20         # zero-mean input
 21         with tf.name_scope('preprocess') as scope:
 22             mean = tf.constant([123.68, 116.779, 103.939], dtype=tf.float32, shape=[1, 1, 1, 3], name='img_mean')
 23             images = self.imgs - mean
 24 
 25         # conv1
 26         with tf.name_scope('conv1') as scope:
 27             kernel = tf.Variable(tf.truncated_normal([7, 7, 3, 96], dtype=tf.float32,
 28                                                      stddev=1e-1), name='weights')
 29             conv = tf.nn.conv2d(images, kernel, [3, 3, 1, 1], padding='SAME')
 30             biases = tf.Variable(tf.constant(0.0, shape=[96], dtype=tf.float32),
 31                                  trainable=True, name='biases')
 32             out = tf.nn.bias_add(conv, biases)
 33             self.conv1 = tf.nn.relu(out, name=scope)
 34             self.parameters += [kernel, biases]
 35 
 36         # pool1
 37         self.pool1 = tf.nn.max_pool(self.conv1,
 38                                     ksize=[1, 3, 3, 1],
 39                                     strides=[1, 2, 2, 1],
 40                                     padding='SAME',
 41                                     name='pool1')
 42 
 43         # conv2
 44         with tf.name_scope('conv2') as scope:
 45             kernel = tf.Variable(tf.truncated_normal([4, 4, 96, 256], dtype=tf.float32,
 46                                                      stddev=1e-1), name='weights')
 47             conv = tf.nn.conv2d(self.pool1, kernel, [1, 1, 1, 1], padding='SAME')
 48             biases = tf.Variable(tf.constant(0.0, shape=[256], dtype=tf.float32),
 49                                  trainable=True, name='biases')
 50             out = tf.nn.bias_add(conv, biases)
 51             self.conv2_1 = tf.nn.relu(out, name=scope)
 52             self.parameters += [kernel, biases]
 53 
 54 
 55         # pool2
 56         self.pool2 = tf.nn.max_pool(self.conv2,
 57                                     ksize=[1, 3, 3, 1],
 58                                     strides=[1, 2, 2, 1],
 59                                     padding='SAME',
 60                                     name='pool2')
 61 
 62         # conv5
 63         with tf.name_scope('conv5') as scope:
 64             kernel = tf.Variable(tf.truncated_normal([3, 3, 256, 256], dtype=tf.float32,
 65                                                      stddev=1e-1), name='weights')
 66             conv = tf.nn.conv2d(self.pool2, kernel, [1, 1, 1, 1], padding='SAME')
 67             biases = tf.Variable(tf.constant(0.0, shape=[256], dtype=tf.float32),
 68                                  trainable=True, name='biases')
 69             out = tf.nn.bias_add(conv, biases)
 70             self.conv5 = tf.nn.relu(out, name=scope)
 71             self.parameters += [kernel, biases]
 72 
 73         # pool5
 74         self.pool5 = tf.nn.max_pool(self.conv5,
 75                                     ksize=[1, 2, 2, 1],
 76                                     strides=[1, 2, 2, 1],
 77                                     padding='SAME',
 78                                     name='pool4')
 79 
 80     def fc_layers(self):
 81         # fc1
 82         with tf.name_scope('fc1') as scope:
 83             shape = int(np.prod(self.pool5.get_shape()[1:]))
 84             fc1w = tf.Variable(tf.truncated_normal([shape, 4096],
 85                                                    dtype=tf.float32,
 86                                                    stddev=1e-1), name='weights')
 87             fc1b = tf.Variable(tf.constant(1.0, shape=[4096], dtype=tf.float32),
 88                                trainable=True, name='biases')
 89             pool5_flat = tf.reshape(self.pool5, [-1, shape])
 90             fc1l = tf.nn.bias_add(tf.matmul(pool5_flat, fc1w), fc1b)
 91             self.fc1 = tf.nn.relu(fc1l)
 92             self.parameters += [fc1w, fc1b]
 93 
 94         # fc3
 95         with tf.name_scope('fc3') as scope:
 96             fc3w = tf.Variable(tf.truncated_normal([4096, 587],
 97                                                    dtype=tf.float32,
 98                                                    stddev=1e-1), name='weights')
 99             fc3b = tf.Variable(tf.constant(1.0, shape=[587], dtype=tf.float32),
100                                trainable=True, name='biases')
101             self.fc3l = tf.nn.bias_add(tf.matmul(self.fc2, fc3w), fc3b)
102             self.parameters += [fc3w, fc3b]

caffe版本的ImageNet模型地址: https://github.com/BVLC/caffe/tree/master/models/bvlc_reference_caffenet

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2016-10-14 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档