import os
import cv2
import torch
import numpy as np
import random
import cv2 as cv
from matplotlib import pyplot as plt
def f_VerifyConv2D():
"""
验证torch.nn.Conv2d, 并将输入数据及权重保存到txt文件中
"""
x = torch.randn(1, 1, 10, 10)
x = x.round().float()
print('================================== 输入数据 ')
print(x)
conv_zeros = torch.nn.Conv2d(in_channels=1, out_channels=3, kernel_size=3, stride=1, padding=1, bias=True)
conv_zeros.weight = torch.nn.Parameter(torch.ones(1, 1, 3, 3))
conv_zeros.bias = torch.nn.Parameter(torch.ones(1))
y = conv_zeros(x)
print('================================== 卷积权重数据 ')
print(conv_zeros.state_dict())
print('================================== 卷积输出数据 ')
print(y)
# 转成numpy
print('================================== 保存参数txt ')
ndarray = x.numpy()
print(ndarray)
ndarray = ndarray.reshape(-1).astype(np.int16)
ndarray[np.where(ndarray < 0)] = np.add(ndarray[np.where(ndarray < 0)], 255) # 将负数以补码的形式进行存储
np.savetxt("1.txt", ndarray, fmt='%x', delimiter='\n')
f_VerifyConv2D()
AI预测福彩3D第10套算法实战化赚米验证第2弹2024年5月6日第2次测试
2024-05-16 10:34:05 113 阅读