tensorflow版的bvlc模型

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

关于模型这个图:

下面贴出通用模板:

  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

时间: 2024-11-05 18:03:06

tensorflow版的bvlc模型的相关文章

语义分割之车道线检测(tensorflow版)

      由于项目需要,参考了多篇相关车道线检测论文与源码,设计了一套Tensorflow版车道线检测功能. 二.基本结构:       该模型主要由以下部分组成: 1.数据源:包括所有原始数据,分组后的数据: 2.数据预处理:包括数据的准备,数据的导入,数据的提取,数据的分组(训练与测试): 3.配置文件:包括各种参数与超参数,如:训练周期,训练步长,批量数据,学习率,卷积核大小,全连接大小,训练模型存放路径(checkpoint),摘要存放路径(summary)等: 4.基础网络:包括基本

TensorFlow Saver 保存最佳模型 tf.train.Saver Save Best Model

TensorFlow Saver 保存最佳模型 tf.train.Saver Save Best Model Checkmate is designed to be a simple drop-in solution for a very common Tensorflow use-case: keeping track of the best model checkpoints during training. The BestCheckpointSaver is a wrapper arou

Tensorflow restore 恢复/载入模型.

Tensorflow 恢复/载入模型.可以有两种方法: 1.首先利用tensorflow建立和以前一样的图.再利用restore恢复参数. .....定义图...... sess=tf.Session() saver=tf.train.Saver() sess.run(tf.global_variables_initializer()) saver.restore(sess, weigths_path) 2.或者直接加载以前的图,将saver修改为: sess=tf.Session() save

tensorflow加载embedding模型进行可视化

1.功能 采用python的gensim模块训练的word2vec模型,然后采用tensorflow读取模型可视化embedding向量 ps:采用C++版本训练的w2v模型,python的gensim模块读不了. 2.python训练word2vec模型代码 import multiprocessing from gensim.models.word2vec import Word2Vec, LineSentence print('开始训练') train_file = "/tmp/train

win10下NeuralStyle的tensorflow版实验

---恢复内容开始--- 首先配置win10下的tensorflow-gpu的运行环境,然后在github上将NeuralStyle拷贝下来,最后根据文档说明参数,运行文件,即可得到自己喜欢的style了. 整个实验项目的步骤: 一.环境配置 1.安装vs2015 2.安装cuda 8.0 3.python3.5.2安装(注意win10下要安装tensorflow-gpu版则需要安装较高版本的python,亲测3.5.2是可以的) 4.安装tensorflow-gpu版 5.安装项目运行依赖包(

[ML]keras和tensorflow实现同样的模型

import tensorflow as tf from input_data import Data import matplotlib.pyplot as plt import os data=Data("./data/") X=tf.placeholder(tf.float32,[None,40,40,3]) y=tf.placeholder(tf.float32,[None,62]) keep_prob=tf.placeholder(tf.float32) conv1_1=tf

语义分割之车道线检测Lanenet(tensorflow版)

Lanenet 一个端到端的网络,包含Lanenet+HNet两个网络模型,其中,Lanenet完成对车道线的实例分割,HNet是一个小网络结构,负责预测变换矩阵H,使用转换矩阵H对同属一条车道线的所有像素点进行重新建模 将语义分割和对像素进行向量表示结合起来的多任务模型,最近利用聚类完成对车道线的实例分割. 将实例分割任务拆解成语义分割和聚类,分割分支负责对输入图像进行语义分割(对像素进行二分类,判断像素属于车道线还是背景),嵌入分支对像素进行嵌入式表示,可将分割后得的车道线分离成不同的车道实

tensorflow版线性回归

import os os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' import tensorflow as tf def linearregression(): X = tf.random_normal([100,1],mean=0.0,stddev=1.0) y_true = tf.matmul(X,[[0.8]]) + [[0.7]] weights = tf.Variable(initial_value=tf.random_normal([1,1]))

Tensorflow 之finetune微调模型方法

https://github.com/joelthchao/tensorflow-finetune-flickr-style https://github.com/kratzert/finetune_alexnet_with_tensorflow https://github.com/Shirhe-Lyh/finetune_classification 原文地址:https://www.cnblogs.com/ranjiewen/p/10231170.html