4. Propose 2048 features of TSN

4. Propose 2048 features of TSN

4.1 Packages to be imported

import numpy as np

4.2 Code implementation form

base_out = self.base_model(input.view((-1, sample_len) + input.size()[-2:])) # 此时的bsae_out就是我们要抽取的特征
        print(base_out.shape) # [3,2048]
        # 由于笔记本硬件限制的问题,只提取了一个epoch的2048特征
        root = 'data_feature/' # 定义保存特征的根目录
        """
        # 定义一个周期保存特征文件的每一个批次的文件名
        # i的传入方式
        for i, (input, target) in enumerate(train_loader):
        	output = model(input_var,i)
        """
        txt = 'feature_2048_' + str(i+1)+'.npy' # 定义一个周期保存特征文件的每一个批次的文件名 
        root_dir = root + txt
        print(str(root_dir))

        # 创建对应npy文件

        def text_create(name, msg):

            desktop_path = root  # 新创建的npy文件的存放路径

            # full_path = desktop_path + name + '.npy'  

            file = open(root_dir, 'w')

            file.write(msg)

            # file.close()

        text_create(txt, '') # 调用创建.npy文件函数

		# 打开.npy文件并将特征向量以numpy的形式写入
        with open(root_dir, 'wb') as f:
            np.save(f, base_out.detach().cpu().numpy())
		# 加载.npy文件,输出特征
        print(np.load(root_dir))

4.3 The file format generated by save is as follows:

Insert picture description here

4.4 The format of load output is as follows:

​ The output of the blue arrow is only for debugging, not the saved .npy

Insert picture description here

Guess you like

Origin blog.csdn.net/better_boy/article/details/108936868