Skip to content

Commit 75ad21c

Browse files
committed
add some
1 parent 355d5cd commit 75ad21c

File tree

13 files changed

+247
-0
lines changed

13 files changed

+247
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ esearch/csvmappings.py
1919

2020
# C extensions
2121
*.so
22+
*.pth
2223

2324
# Distribution / packaging
2425
.DS_Store
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import math
2+
3+
import numpy
4+
def softmax(inMatrix):
5+
m,n = numpy.shape(inMatrix)
6+
outMatrix = numpy.mat(numpy.zeros((m,n)))
7+
soft_sum = 0
8+
for idx in range(0,n):
9+
outMatrix[0,idx] = math.exp(inMatrix[0,idx])
10+
soft_sum += outMatrix[0,idx]
11+
for idx in range(0,n):
12+
outMatrix[0,idx] = outMatrix[0,idx] / soft_sum
13+
return outMatrix
14+
a = numpy.array([[1, 2, 1, 2, 1, 1, 3]])
15+
print(softmax(a))
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import torch
2+
result = torch.tensor(1) + torch.tensor(2.0)
3+
print(result)
17.4 KB
Loading
18.8 KB
Loading
19.1 KB
Loading
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import numpy as np
2+
import matplotlib.pyplot as plt
3+
import torch
4+
5+
grid_size = 5
6+
x_train = np.load("../dataset/mnist/x_train.npy")
7+
#image = torch.tensor(x_train[0]).to("cuda")
8+
image = torch.tensor(x_train[0]).to("cpu")
9+
10+
image = image
11+
print(image.shape)
12+
image = image.cpu().numpy()
13+
plt.imshow(image)
14+
plt.savefig("./img/img.jpg")
15+
plt.show()
16+
17+
18+
19+
20+
# image = torch.tensor(x_train[0]).to("cuda")
21+
# image = torch.squeeze(image,dim=0)
22+
# image = image.cpu().numpy()
23+
# plt.imshow(image)
24+
# plt.show()
25+
26+
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import os
2+
os.environ['CUDA_VISIBLE_DEVICES'] = '0' #指定GPU编
3+
import torch
4+
import numpy as np
5+
import unet
6+
import matplotlib.pyplot as plt
7+
from tqdm import tqdm
8+
9+
batch_size = 320 #设定每次训练的批次数
10+
epochs = 1024 #设定训练次数
11+
12+
#device = "cpu" #Pytorch的特性,需要指定计算的硬件,如果没有GPU的存在,就使用CPU进行计算
13+
device = "cuda" #在这里读者默认使用GPU,如果读者出现运行问题可以将其改成cpu模式
14+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
15+
device = "cpu"
16+
17+
model = unet.Unet() #导入Unet模型
18+
model = model.to(device) #将计算模型传入GPU硬件等待计算
19+
#model = torch.compile(model) #Pytorch2.0的特性,加速计算速度
20+
optimizer = torch.optim.Adam(model.parameters(), lr=2e-5) #设定优化函数
21+
22+
#载入数据
23+
x_train = np.load("../dataset/mnist/x_train.npy")
24+
y_train_label = np.load("../dataset/mnist/y_train_label.npy")
25+
26+
x_train_batch = []
27+
for i in range(len(y_train_label)):
28+
if y_train_label[i] <= 10: #为了加速演示作者只对数据集中的小于2的数字,也就是0和1进行运行,读者可以自行增加训练个数
29+
x_train_batch.append(x_train[i])
30+
31+
x_train = np.reshape(x_train_batch, [-1, 1, 28, 28]) #修正数据输入维度:([30596, 28, 28])
32+
x_train /= 512.
33+
34+
image = x_train[np.random.randint(28)] #随机挑选一条数据进行计算
35+
image = np.reshape(image,[28,28]) #修正数据维度
36+
plt.imshow(image)
37+
plt.show()
38+
39+
state_dict = torch.load("./saver/unet.pth")
40+
model.load_state_dict(state_dict)
41+
image = torch.reshape(torch.tensor(image),[1,1,28,28])
42+
img = model(image)
43+
img = torch.reshape(img, shape=[28,28]) #修正模型输出结果
44+
img = img.detach().cpu().numpy()
45+
plt.imshow(img)
46+
plt.show()
47+
48+
49+
727 KB
Binary file not shown.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import torch
2+
3+
print(torch.__version__)
4+
print(torch.cuda.is_available())
5+
6+
print("是否可用:", torch.cuda.is_available()) # 查看GPU是否可用
7+
print("GPU数量:", torch.cuda.device_count()) # 查看GPU数量
8+
print("torch方法查看CUDA版本:", torch.version.cuda) # torch方法查看CUDA版本
9+
print("GPU索引号:", torch.cuda.current_device()) # 查看GPU索引号
10+
print("GPU名称:", torch.cuda.get_device_name(0)) # 根据索引号得到GPU名称

0 commit comments

Comments
 (0)