import tensorflow as tf
import utils
import numpy as np
import scipy.io as sio
import cv2
class Resnet:
def __init__(self, gpu=0, cls=2, res_name='resnet101', checkpoint_dir='./model', model_name='test',
lr=1e-5):
self.cls = cls
self.checkpoint_dir = checkpoint_dir
self.model_name = model_name
self.res_name = res_name
self.lr = lr
CFG = {'resnet50': [3, 4, 6, 3], 'resnet101': [3, 4, 23, 3], 'resnet152': [3, 8, 36, 3]}
self.resnet50_path = 'weights\\imgnet_resnet101.npz'
if gpu:
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
self.sess = tf.Session(config=config)
else:
self.sess = tf.Session()
self.build(CFG[res_name])
def branch1(self, x, numOut, s):
with tf.variable_scope("conv1"):
conv1 = utils.relu(utils.Bn(utils.conv2d(x, numOut / 4, d_h=s, d_w=s), training=self.is_training))
with tf.variable_scope("conv2"):
conv2 = utils.relu(utils.Bn(utils.conv2d(conv1, numOut / 4, 3, 3), training=self.is_training))
with tf.variable_scope("conv3"):
conv3 = utils.Bn(utils.conv2d(conv2, numOut), training=self.is_training)
return conv3
def branch2(self, x, numOut, s):
with tf.variable_scope("convshortcut"):
return utils.Bn(utils.conv2d(x, numOut, d_h=s, d_w=s), training=self.is_training)
def residual(self, x, numOut, stride=1, name='res'):
with tf.variable_scope(name):
block = self.branch1(x, numOut, stride)
if x.get_shape().as_list()[3] == numOut:
return utils.relu(tf.add_n([x, block]))
else:
skip = self.branch2(x, numOut, stride)
return utils.relu(tf.add_n([block, skip]))
def inference(self, x, grah):
with tf.variable_scope("conv0"):
if self.res_name == "resnet50":
net = utils.relu(utils.Bn(utils.conv2d(x, 64, 7, 7, 2, 2, bias=True), training=self.is_training))
else:
net = utils.relu(utils.Bn(utils.conv2d(x, 64, 7, 7, 2, 2, bias=False), training=self.is_training))
with tf.name_scope("pool1"):
net = utils.max_pool(net, 3, 3, 2, 2)
with tf.variable_scope("group0"):
for i in range(grah[0]):
net = self.residual(net, 256, name='block' + str(i))
with tf.variable_scope("group1"):
for i in range(grah[1]):
if i == 0:
net = self.residual(net, 512, 2, name='block' + str(i))
else:
net = self.residual(net, 512, name='block' + str(i))
with tf.variable_scope("group2"):
for i in range(grah[2]):
if i == 0:
net = self.residual(net, 1024, 2, name='block' + str(i))
else:
net = self.residual(net, 1024, name='block' + str(i))
with tf.variable_scope("group3"):
for i in range(grah[3]):
if i == 0:
net = self.residual(net, 2048, 2, name='block' + str(i))
else:
net = self.residual(net, 2048, name='block' + str(i))
with tf.name_scope("pool5"):
net = utils.global_pool(net)
with tf.variable_scope("linear"):
net = tf.nn.dropout(net, keep_prob=self.keep_prob)
net = utils.linear(net, 1000)
return net
def load_weight(self):
param = dict(np.load(self.resnet50_path))
vars = tf.global_variables(scope="inference")
for v in vars:
nameEnd = v.name.split('/')[-1]
if nameEnd == "moving_mean:0":
name = v.name[10:-13] + "mean/EMA"
elif nameEnd == "moving_variance:0":
name = v.name[10:-17] + "variance/EMA"
else:
name = v.name[10:-2]
if name == 'linear/W':
param[name] = param['linear/W'].reshape(2048, 1000)
self.sess.run(v.assign(param[name]))
print("Copy weights: " + name + "---->" + v.name)
def build(self, CFG):
self.inputs = tf.placeholder(tf.float32, [None, 224, 224, 3], "inputs")
self.labels = tf.placeholder(tf.int32, [None], "labels")
self.keep_prob = tf.placeholder(tf.float32)
self.is_training = tf.placeholder(tf.bool, name="is_training")
with tf.variable_scope("inference"):
self.outs = self.inference(self.inputs, CFG)
# 定义损失函数
self.loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(logits=self.outs, labels=self.labels))
with tf.variable_scope('minimizer'):
adam = tf.train.AdamOptimizer(self.lr)
update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
with tf.control_dependencies(update_ops):
self.optm = adam.minimize(self.loss)
# 模型保存
self.saver = tf.train.Saver(max_to_keep=2)
# 初始化
init = tf.global_variables_initializer()
self.sess.run(init)
# 加载预训练权重
self.load_weight()
def train(self):
pass
def test(self):
pp_mean = sio.loadmat('pp_mean.mat')['normalization']
pp_mean_224 = pp_mean[16:-16, 16:-16, :]
img = cv2.imread('C:\\Users\\sk\\Desktop\\test\\ILSVRC2012_val_00000001.JPEG').astype('float32')
im = img[75:-76, 138:-138, :]
im = im - pp_mean_224
im = np.reshape(im, (1, 224, 224, 3))
A = self.sess.run(self.outs, feed_dict={self.inputs: im})
print(A[0].argsort()[-10:][::-1])
print('-------')
def main():
model = Resnet()
model.test()
if __name__ == "__main__":
main()
没有合适的资源?快使用搜索试试~ 我知道了~
tensorflow版resnet代码(18,50,101,152)
![preview](https://csdnimg.cn/release/downloadcmsfe/public/img/white-bg.ca8570fa.png)
共5个文件
py:4个
mat:1个
![preview-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/scale.ab9e0183.png)
![star](https://csdnimg.cn/release/downloadcmsfe/public/img/star.98a08eaa.png)
温馨提示
tensorflow版本的resnet代码,以最简化的方式构建。可以适用于各种网络的结构的变形,另外提供预训练tensorflow版本的预训练权重,由于上传文件限制,下载好代码之后:https://pan.baidu.com/s/1Kuvad3P9gn39vyONL2glZg 这里下载各个层数的预训练模型
资源推荐
资源详情
资源评论
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![py](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![py](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083646.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
收起资源包目录
![package](https://csdnimg.cn/release/downloadcmsfe/public/img/package.f3fc750b.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
共 5 条
- 1
资源评论
![avatar-default](https://csdnimg.cn/release/downloadcmsfe/public/img/lazyLogo2.1882d7f4.png)
- weixin_451543052021-03-09要是有注解就好了
- 小香芋2019-03-22代码挺齐全的
- 422523432020-06-08能提供下ckpt格式吗?
![avatar](https://profile-avatar.csdnimg.cn/0838ed731e014a249809cd6bd95d009a_supe_king.jpg!1)
supe_king
- 粉丝: 69
- 资源: 3
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![voice](https://csdnimg.cn/release/downloadcmsfe/public/img/voice.245cc511.png)
![center-task](https://csdnimg.cn/release/downloadcmsfe/public/img/center-task.c2eda91a.png)
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback-tip](https://img-home.csdnimg.cn/images/20220527035111.png)
安全验证
文档复制为VIP权益,开通VIP直接复制
![dialog-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/green-success.6a4acb44.png)