Created
July 16, 2021 12:14
-
-
Save e96031413/5e00f174fc6fffded710b920b23e4380 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
for i, (img, target,_) in enumerate(tqdm(dataloader)): | |
feat_list = [] | |
def hook(module, input, output): | |
# 由於MobileNetv2不像ResNet18有宣告self.avgpool(),因此我的作法是將模型卷積層的最後一層的輸出手動進行adaptive_avg_pool2d | |
# 接著將它加到feature_list中 | |
feat_list.append(nn.functional.adaptive_avg_pool2d(output.clone(), (1, 1)).reshape(output.clone().shape[0], -1).detach()) | |
images = img.to(device) | |
target = target.squeeze().tolist() | |
for element in target: | |
labels.append(element) | |
with torch.no_grad(): | |
handle=model.features[-1].register_forward_hook(hook) # 模型卷積層最後一層的輸出(model.features[-1]) | |
output = model.forward(images) | |
feat = torch.flatten(feat_list[0], 1) | |
handle.remove() | |
current_features = feat.cpu().numpy() | |
if features is not None: | |
features = np.concatenate((features, current_features)) | |
else: | |
features = current_features | |
return features, labels |
謝謝您的答覆,這個code我看過了,但仍無法看出來features = None之後,哪個步驟有更新到"features"?
features = None
current_features = feat.cpu().numpy()
if features is not None:
features = np.concatenate((features, current_features))
else:
features = current_features
您好,這段程式碼當中,我把其他程式碼先暫時去掉,只看features相關的程式碼
- 初始狀態為features = None
- 第一次更新features的時候,由於features = None,所以會執行else部分的程式碼,這時候features被更新後,就不是None了
- 因此下一次更新features的時候,會從if features is not None開始執行,這邊會透過numpy把features和current_features進行concat
謝謝您的解說!我理解了!原本以為是最後一個步驟才執行if else,是我忽略了他會不斷讀取dataloader裡的資料去做更新。
謝謝您。
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
您好,以下的程式碼應該會比較清楚一點:
features is not None
是為了避免程式出現例外情形導致中斷