-
大小: 31.5MB文件类型: .rar金币: 1下载: 0 次发布日期: 2023-08-06
- 语言: Python
- 标签: yolov3-tiny yolov3-tiny.
资源简介
基于tensorflow实现yolov3-tiny的检测网络,直接加载官方提供的权重文件给模型中的参数赋值,而不是网上说的什么.h5或者是pb模型。
tensorflow版本:1.11
python版本:3.5
文件中包含权重文件,若想要使用纯tensorflow实现yolov的其他版本,可以按照我这个代码来改
代码片段和文件信息
import tensorflow as tf
import numpy as np
from PIL import Image ImageDraw ImageFont
import time
import os
import colorsys
def get_anchors(anchors_path):
anchors_path = os.path.expanduser(anchors_path)
with open(anchors_path) as f:
anchors = f.readline()
anchors = [float(x) for x in anchors.split(‘‘)]
return np.array(anchors).reshape(-1 2)
def get_class(class_path):
class_path = os.path.expanduser(class_path)
with open(class_path) as f:
class_names = f.readlines()
class_names = [c.strip() for c in class_names]
return class_names
def print_tensor_info(tensor):
# print(tensor.name ““ tensor.dtype “shape=“ tensor.get_shape().as_list())
return
def bn_layer(tensor name=“BatchNormal“ moving_decay=0.9 eps=5e-4 is_trainning=False):
shape = tensor.get_shape().as_list()
param_shape = shape[-1]
with tf.variable_scope(name):
# 声明BN中唯一需要学习的两个参数
gamma = tf.get_variable(
“gamma“ shape=param_shape dtype=tf.float32
initializer=tf.constant_initializer(1)
)
beta = tf.get_variable(
“beta“ shape=param_shape dtype=tf.float32
initializer=tf.constant_initializer(0)
)
# 计算整个batch的均值与方差
axies = list(range(len(shape) - 1))
batch_mean batch_var = tf.nn.moments(tensor axies name=“moments“)
# 使用滑动平均值更新均值与方差
ema = tf.train.ExponentialMovingAverage(moving_decay)
ema_apply_op = ema.apply([batch_mean batch_var])
def mean_var_with_update():
with tf.control_dependencies([ema_apply_op]):
return tf.identity(batch_mean) tf.identity(batch_var)
# 训练时,更新均值与方差,测试时使用之前最后一次保存的均值与方差
mean var = tf.cond(
tf.equal(is_trainning True) mean_var_with_update
lambda: (ema.average(batch_mean) ema.average(batch_var))
)
return tf.nn.batch_normalization(tensor mean var beta gamma eps name=“normalize“)
def inference(input_tensor):
outputs = []
with tf.variable_scope(“Conv2d_1“):
# 创建卷积核
kernel = tf.get_variable(
“kernel“ shape=[3 3 3 16] dtype=tf.float32
initializer=tf.truncated_normal_initializer(stddev=0.1))
# 进行卷积运算,对卷积后的结果进行批归一化
conv = tf.nn.conv2d(input_tensor kernel [1 1 1 1] padding=“SAME“)
normalize_tensor_1 = bn_layer(conv)
# 使用激活函数对结果进行非线性处理
conv_1 = tf.nn.leaky_relu(normalize_tensor_1 alpha=0.1 name=“leaky_relu“)
print_tensor_info(conv_1)
with tf.variable_scope(“Max_pooling2d_1“):
pool_1 = tf.nn.max_pool(
conv_1 ksize=[1 2 2 1] strides=[1 2 2 1] padding=“VALID“ name=“maxpool“
)
print_tensor_info(pool_1)
with tf.variable_scope(“Conv2d_2“):
kernel = tf.get_variable(
“kernel“ shape=[3 3 16 32] d
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 21540 2018-11-09 15:14 yolov3_tiny_weights.py
文件 35434956 2018-05-10 17:20 yolov3-tiny.weights
----------- --------- ---------- ----- ----
35456496 2
- 上一篇:2018汽车之家汽车品牌车型数据新鲜出炉
- 下一篇:姿势检测的动作识别
评论
共有 条评论