从T7模型中取训练参数

上一章中,我们已经取出层名,层名和层名之间,是存放参数的空间,这样相当于已经给模型分了层。

我们只要在层间搜索参数就可以了。


第2步:搜索参数

以卷积层为例:

if(startsWith(TorchClassName,"nn.SpatialConvolution"))
{

	const char *findstr[] = {
		"name", //名称
		"dW", "dH", //步长
		"kW", "kH", //核宽
		"weight", "bias", //权重,偏移
		"padW", "padH", //补边(2参数)
		"pad_l", "pad_r", "pad_t", "pad_b", //补边(4参数)
		"nInputPlane", "nOutputPlane", //输入、输出维度
		NULL};

	fpos = ftell(m_FilePointer);//记录层开头位置(在文件中)。
	for (int i = 0; findstr[i]; i++)
	{
			
		fout<<findstr[i]<<":"<<endl;

		bool ret=false;
		while(!ret){
			ret=跳到某字符串位置后(findstr[i]); //注意不要跑到下一层去
			if(ret)
				ret=readObject(fout);//这里的一些函数可以从OpenCV稍稍修改而来
		}


		fseek(m_FilePointer, fpos,SEEK_SET);//回到开头位置后再搜索
	}

}

上面我们已经把所有参数都保存到一个文件中了。

这里,我们的目的不是拿出所有参数,我们只要拿出卷积层的训练参数 weight,bias,其它在训练前就知道的就不用了

按OpenCV的代码分析几个t7模型,

存放方式分两种,1是统一存放,另1是分别存入

T7存放方式按:数据首址,数据偏移,数据类型

这里就有两种取法,

1。取数据(OpenCV方式)

2。取地址(=数据首址+数据偏移x sizeof(数据类型))

取地址方式应该灵活一点

取数据偏移:

void readTorchTensor(ofstream &fout, int typeTensor,const char * ch_str)
{
    int ndims = readInt();
    my_readLongRaw(fout, ndims);//读取长原始数据//THFile_readLongRaw
    my_readLongRaw(fout, ndims);

    int64_t offset = readLong() - 1;

	fout<<"偏移:"<<offset<<endl;
	if(this_norm_conv==is_conv)
	{
		if(strcmp(ch_str,"weight")==0)
		
			*file_data_offset.weight_offset =(long)offset;//权重数据偏移 <--这里
		else if(strcmp(ch_str,"bias")==0)
			*file_data_offset.bias_offset =(long)offset;//偏移数据偏移 <--这里
	}
    //read Storage
    int typeidx = readInt();
		
	//fout<<"typeidx:"<<typeidx<<endl;

    assert(typeidx == TYPE_TORCH || (typeidx == TYPE_NIL && ndims == 0));

    if (typeidx == TYPE_NIL)
    {
        return;
    }

    int indexStorage = readInt();
    {
        string className = readTorchClassName();
	fout<<"className:"<<className<<endl;

        int typeStorage = parseStorageType(className);
        readTorchStorage(fout, typeStorage);
    }

}

 

取首址和类型:

void my_readDoubleRaw(ofstream &fout, int64_t size)
{
	//double data;
	fout<<"尺寸:"<<size<<endl;
	fout<<"地址:"<<ftell(m_FilePointer)<<endl;
	if(this_norm_conv==is_conv)
		file_data_offset.data_offset=ftell(m_FilePointer);//数据首址 <--这里
		
	file_data_offset.data_type=sizeof(double);//数据类型 <--这里
  //  for (int64_t i = size - 1; i >= 0; i--)
  //  {
 	//	fread(&data, sizeof(double), 1, m_FilePointer);   
		//fout<<data<<' ';
  //  }fout<<endl;
}

void my_readFloatRaw(ofstream &fout, int64_t size)
{
	//float data;
	fout<<"尺寸:"<<size<<endl;
	fout<<"地址:"<<ftell(m_FilePointer)<<endl;
	if(this_norm_conv==is_conv)
		file_data_offset.data_offset=ftell(m_FilePointer);//数据首址 <--这里
	if(this_norm_conv==is_norm)
		file_InstanceNormalization_data_offset.data_offset=ftell(m_FilePointer);
	if(this_norm_conv==is_batchnorm)
		file_SpatialBatchNormalization_data_offset.data_offset=ftell(m_FilePointer);
		
	file_data_offset.data_type=sizeof(float);//数据类型 <--这里

  //  for (int64_t i = size - 1; i >= 0; i--)
  //  {
 	//	fread(&data, sizeof(float), 1, m_FilePointer);   
		//fout<<data<<' ';
  //  }fout<<endl;
}

这些函数都是从OpenCV中抄过来再稍改改, my_ 对应 THFile_

然后组合:

// 还要加上文件位置
if(strcmp(ch_str,"weight")==0)
{
	*file_data_offset.weight_offset   *= file_data_offset.data_type;//乘 类型 <--这里
	*file_data_offset.weight_offset++ += file_data_offset.data_offset;//加 数据首址 <--这里
}
else if(strcmp(ch_str,"bias")==0)
{
	*file_data_offset.bias_offset   *= file_data_offset.data_offset;
	*file_data_offset.bias_offset++ += file_data_offset.data_offset;
}

这里用到地址池:

struct Data位置
{
	long data_offset;
	long * weight_offset;
	long * bias_offset;
	int data_type;

		
	//构造函数
	Data位置();

};

Data位置::Data位置()
{
	data_offset=0;
	weight_offset=NULL;
	bias_offset=NULL;
	data_type=0;
}

Data位置 file_data_offset;

使用时用:

long *weight_offset=new long[2+2+4+1 ];
long *bias_offset=new long[2+2+4+1 ];
	string pathname ="D:/3D/cv33/cv33/AdaIN-style-master/models/";
	string torch_model_name	="vgg_normalised.t7";
							
							//decoder-content-similar 解码器颜色和内容相似
							//decoder
	pathname += torch_model_name;

loadModel_data_offset(pathname,weight_offset,bias_offset);

loadModel_data_offset函数:

void 	loadModel_data_offset(string torch_model_name,long * weight_offset,long * bias_offset)
{
	file_data_offset.weight_offset=weight_offset;
	file_data_offset.bias_offset=bias_offset;
	readModel(torch_model_name);

}

这样就已经从t7模型取出训练数据了。

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值