python - 如何使用 batch_size=1 评估模型并将训练参数用于 BatchNorm 而不是 InstanceNorm?
问题描述
当我运行我model(training=False)
的 with 时batch_size = 2
,模型使用 BatchNorm 的训练参数,但是当我运行 model with 时batch_size = 1
,我的模型不使用训练过的参数,InstanceNorm
实际上是这样做的。如何使用batch_size = 1
as运行模型batch_size = 2
?
class ContextExtractor(tf.keras.Model):
def __init__(self, model_name, pretrained_shape):
super().__init__()
self.model = self.__get_model(model_name, pretrained_shape)
def call(self, x, training=False, **kwargs):
features = self.model(x, training=training)
return features
def __get_model(self, model_name, pretrained_shape):
if model_name == "mobilenetv2":
return MobileNetV2(input_shape=pretrained_shape,
weights='imagenet',
alpha=0.35,
include_top=False,
pooling='avg')
context_extractor = ContextExtractor("mobilenetv2", (224, 224, 3))
bc = tf.random.uniform((10, 224, 224, 3))
a1 = context_extractor(bc, training=False)
print(hash(a1[0].numpy().tobytes()))
# -8545286774071995675
a2 = context_extractor(bc[:2], training=False)
print(hash(a2[0].numpy().tobytes()))
# -8545286774071995675 <-- the same
a3 = context_extractor(bc[:1], training=False)
print(hash(a3[0].numpy().tobytes()))
# 8191470914604248680 <-- another result!
解决方案
推荐阅读
- java - 如何仅从我感兴趣的 OpenCV 中提取特征并为这些特征创建 .so?
- npm - 使用 np 包发布到 npm 或 yarn
- javascript - Karma 不运行 Jasmine 实现回调
- php - 正则表达式查找所有“?” 没有“ ”
- android - 如何在 RecyclerView 中设计播放列表项,如 youtube kids 应用程序
- javascript - 从输入文本中删除的只是数字的值与我的简单数组不匹配
- python - 如何根据另一个小部件的位置放置小部件?
- javascript - 将所有 JS 代码和变量视为执行上下文的属性是否正确?
- python - 平均 groupby 取决于两列
- sql - 如何规范化具有多值字段的表?