简体   繁体   English

从 pytorch 中的预训练 resnet50 中提取特征

[英]Extract features from pretrained resnet50 in pytorch

Hy guys, i want to extract the in_features of Fully connected layer of my pretrained resnet50.大家好,我想提取我预训练的 resnet50 的全连接层的 in_features。

I create before a method that give me the vector of features:我在一个给我特征向量的方法之前创建:

def get_vector(image):

#layer = model._modules.get('fc')

layer = model.fc
my_embedding = torch.zeros(2048) #2048 is the in_features of FC , output of avgpool

def copy_data(m, i, o):

    my_embedding.copy_(o.data)


h = layer.register_forward_hook(copy_data)
tmp = model(image)

h.remove()

# return the vector
return my_embedding

after I call this method here:在我在这里调用这个方法之后:

column = ["FlickrID", "Features"]

path = "./train_dataset/train_imgs/"

pathCSV = "./train_dataset/features/img_info_TRAIN.csv"



f_id=[]
features_extr=[]

df = pd.DataFrame(columns=column)


tr=transforms.Compose([transforms.Resize(256),
                       transforms.CenterCrop(224),
                       transforms.ToTensor(),
                       transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])])



test = Dataset(path, pathCSV, transform=tr)

test_loader = DataLoader(test, batch_size=1, num_workers=2, shuffle = False)



#Leggiamo le immagini
for batch in test_loader:
    nome = batch['FlickrID']
    f_id.append(nome)
    image = batch['image']



    #print(image)
    with torch.no_grad():
        pred = get_vector(image)

    features_extr.append(pred)

df["FlickrID"] = f_id
df["Features"] = features_extr  


df.to_hdf("Places.h5", key='df', mode='w')

I have an error like this: output with shape [2048] doesn't match the broadcast shape [1, 2048, 1, 2048]我有这样的错误: output with shape [2048] doesn't match the broadcast shape [1, 2048, 1, 2048]

How can I take the in_feature of Fully Connected of this resnet50?如何获取这个 resnet50 的完全连接的 in_feature? The Dataset is a customized Dataset class.数据集是一个定制的数据集 class。

Sorry for my bad english对不起,我的英语不好

The model takes batched inputs, that means the input to the fully connected layer has size [batch_size, 2048] . model 采用批量输入,这意味着全连接层的输入大小为[batch_size, 2048] Because you are using a batch size of 1, that becomes [1, 2048] .因为您使用的批量大小为 1,所以变为[1, 2048] Therefore that doesn't fit into a the tensor torch.zeros(2048) , so it should be torch.zeros(1, 2048) instead.因此,它不适合张量torch.zeros(2048) ,所以它应该是torch.zeros(1, 2048)

You are also trying to use the output ( o ) of the layer model.fc instead of the input ( i ).您还尝试使用 model.fc 层的model.fc ( o ) 而不是输入 ( i )。

Besides that, using hooks is overly complicated for this and a much easier way to get features is to modify the model by replacing model.fc withnn.Identity , which just returns the input as the output, and since the features are its input, the output of the entire model will be the features.除此之外,为此使用钩子过于复杂,获取特征的更简单方法是修改 model,将model.fc替换为nn.Identity ,它只返回输入为 Z78E6221F6393D1435668,因为特征是其输入整个 model 的 output 将是功能。

model.fc = nn.Identity()

features = model(image)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM