Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
曾经的那一瞬间
Models
提交
bae839cf
M
Models
项目概览
曾经的那一瞬间
/
Models
9 个月 前同步成功
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
M
Models
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
bae839cf
编写于
7月 31, 2023
作者:
A
A. Unique TensorFlower
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add SWAP pooling to YT8M open-source code base.
PiperOrigin-RevId: 552603688
上级
e11f5294
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
78 addition
and
0 deletion
+78
-0
official/projects/yt8m/modeling/yt8m_model_utils.py
official/projects/yt8m/modeling/yt8m_model_utils.py
+44
-0
official/projects/yt8m/modeling/yt8m_model_utils_test.py
official/projects/yt8m/modeling/yt8m_model_utils_test.py
+34
-0
未找到文件。
official/projects/yt8m/modeling/yt8m_model_utils.py
浏览文件 @
bae839cf
...
...
@@ -19,6 +19,45 @@ from typing import Any, Dict, Optional, Union
import
tensorflow
as
tf
def
weighted_average_pooling
(
features
,
weights
,
axis
):
"""Weighted average pooling.
Args:
features: a tensor of at least rank 1.
weights: a weight tensor whose shape is broadcast compatible with features.
It doesn't have to be normalized.
axis: the dimensions to reduce.
Returns:
The reduced tensor.
"""
return
tf
.
math
.
divide_no_nan
(
tf
.
reduce_sum
(
weights
*
features
,
axis
),
# numerator.
tf
.
reduce_sum
(
weights
,
axis
),
# denominator.
)
def
frame_swap
(
frames
:
tf
.
Tensor
)
->
tf
.
Tensor
:
"""Self-weighted average pooling over all frames of a video.
It does the following operation independently for each feature:
x_pooled = (sum_i x_i * |x_i|) / (sum_i |x_i|).
Basically the weight for the feature in each frame is determined by the
magnitude of the feature itself.
Paper: https://research.google/pubs/pub48351/
Args:
frames: A tensor with shape [batch_size, max_frames, feature_size].
Returns:
A tensor with shape [batch_size, feature_size].
"""
weights
=
tf
.
abs
(
frames
)
# We set axis to 1 to reduce the dimension corresponding to max_frames.
return
weighted_average_pooling
(
frames
,
weights
,
axis
=
1
)
def
frame_pooling
(
frames
,
method
):
"""Pools over the frames of a video.
...
...
@@ -39,6 +78,11 @@ def frame_pooling(frames, method):
reduced
=
tf
.
reduce_mean
(
frames
,
1
)
elif
method
==
"max"
:
reduced
=
tf
.
reduce_max
(
frames
,
1
)
elif
method
==
"swap"
:
# Note we assume the frames are in the shape of
# [batch_size, num_frames, feature_size]. Otherwise this function might
# fail.
reduced
=
frame_swap
(
frames
)
elif
method
==
"none"
:
feature_size
=
frames
.
shape_as_list
()[
2
]
reduced
=
tf
.
reshape
(
frames
,
[
-
1
,
feature_size
])
...
...
official/projects/yt8m/modeling/yt8m_model_utils_test.py
0 → 100644
浏览文件 @
bae839cf
# Copyright 2023 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Tests for YT8M modeling utilities."""
import
tensorflow
as
tf
from
official.projects.yt8m.modeling
import
yt8m_model_utils
class
Yt8MModelUtilsTest
(
tf
.
test
.
TestCase
):
def
test_swap_pooling
(
self
):
frame
=
tf
.
constant
([
[[
0.0
,
0.0
,
0.0
],
[
0.0
,
1.0
,
-
1.0
]],
[[
0.0
,
0.0
,
0.0
],
[
0.0
,
2.0
,
-
2.0
]],
])
swap_frame
=
yt8m_model_utils
.
frame_pooling
(
frame
,
"swap"
)
self
.
assertAllClose
([[
0.0
,
1.0
,
-
1.0
],
[
0.0
,
2.0
,
-
2.0
]],
swap_frame
)
if
__name__
==
"__main__"
:
tf
.
test
.
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录