Closed kyakuno closed 3 months ago
@kyakuno @TKMSBY お疲れ様です。 こちらについてですが、現状が仕掛中でなく、かつ、差支えもなければ、私の方で担当させて頂けたらと思ったのですが、いかがでしょうか?
@TKMSBY ご対応頂き、ありがとうございます。 大変恐縮ながら、私の方で担当させて頂こうと思います。
📝 (備忘)
特徴抽出に用いるEfficientNetは、以下リポジトリのものを使用
https://github.com/lukemelas/EfficientNet-PyTorch/tree/master
その内の以下メソッドを上書きして使用(※最早、forwardは使わずに、extract_featuresだけを使用) (オリジナル)
def extract_features(self, inputs):
"""use convolution layer to extract feature .
Args:
inputs (tensor): Input tensor.
Returns:
Output of the final convolution
layer in the efficientnet model.
"""
# Stem
x = self._swish(self._bn0(self._conv_stem(inputs)))
# Blocks
for idx, block in enumerate(self._blocks):
drop_connect_rate = self._global_params.drop_connect_rate
if drop_connect_rate:
drop_connect_rate *= float(idx) / len(self._blocks) # scale drop connect_rate
x = block(x, drop_connect_rate=drop_connect_rate)
# Head
x = self._swish(self._bn1(self._conv_head(x)))
return x
def forward(self, inputs):
"""EfficientNet's forward function.
Calls extract_features to extract features, applies final linear layer, and returns logits.
Args:
inputs (tensor): Input tensor.
Returns:
Output of this model after processing.
"""
# Convolution layers
x = self.extract_features(inputs)
# Pooling and final linear layer
x = self._avg_pooling(x)
if self._global_params.include_top:
x = x.flatten(start_dim=1)
x = self._dropout(x)
x = self._fc(x)
return x
↓ (上書き版)
def extract_features(self, inputs):
""" Returns list of the feature at each level of the EfficientNet """
feat_list = []
# Stem
x = self._swish(self._bn0(self._conv_stem(inputs)))
feat_list.append(F.adaptive_avg_pool2d(x, 1))
# Blocks
x_prev = x
for idx, block in enumerate(self._blocks):
drop_connect_rate = self._global_params.drop_connect_rate
if drop_connect_rate:
drop_connect_rate *= float(idx) / len(self._blocks)
x = block(x, drop_connect_rate=drop_connect_rate)
print('x.shape =', x.shape)
if (x_prev.shape[1] != x.shape[1] and idx != 0) or idx == (len(self._blocks) - 1):
feat_list.append(F.adaptive_avg_pool2d(x_prev, 1))
print('F.adaptive_avg_pool2d(x_prev, 1).shape =', F.adaptive_avg_pool2d(x_prev, 1).shape)
x_prev = x
# Head
x = self._swish(self._bn1(self._conv_head(x)))
feat_list.append(F.adaptive_avg_pool2d(x, 1))
return feat_list
efficientnet_pytorch==0.6.3
と記載されているが、READMEに記載される精度結果を得るのに必要であるとのこと
efficientnet_pytorch==0.7.0
で実施したところ、精度が下がったケースがあったとのこと→https://github.com/byungjae89/MahalanobisAD-pytorch/issues/2#issuecomment-771366404efficientnet_pytorch==0.6.3
efficientnet_pytorch==0.7.0
efficientnet_pytorch==0.6.3
のものにて行わせて頂くonnx作成は、extract_featuresのoverwriteがされているところを、forwardのoverwriteに変更した上で行う
class EfficientNetModified(EfficientNet):
# def extract_features(self, inputs):
def forward(self, inputs): # <-replace for onnx export
""" Returns list of the feature at each level of the EfficientNet """
feat_list = []
# Stem
x = self._swish(self._bn0(self._conv_stem(inputs)))
feat_list.append(F.adaptive_avg_pool2d(x, 1))
# Blocks
x_prev = x
for idx, block in enumerate(self._blocks):
drop_connect_rate = self._global_params.drop_connect_rate
if drop_connect_rate:
drop_connect_rate *= float(idx) / len(self._blocks)
x = block(x, drop_connect_rate=drop_connect_rate)
if (x_prev.shape[1] != x.shape[1] and idx != 0) or idx == (len(self._blocks) - 1):
feat_list.append(F.adaptive_avg_pool2d(x_prev, 1))
x_prev = x
# Head
x = self._swish(self._bn1(self._conv_head(x)))
feat_list.append(F.adaptive_avg_pool2d(x, 1))
return feat_list
https://github.com/byungjae89/MahalanobisAD-pytorch Abnormal detection Apache