fje isfhdifhsifs

This commit is contained in:
MTFTau-5 2024-10-26 11:34:07 +08:00
parent 2d78b097a3
commit db33e84265
1086 changed files with 406 additions and 6 deletions
MNIST/raw
data/MNIST/raw
logs
matplotlibmodel.pth
myenv
read_data.py
venv/人工智能/作业
venv_2
插件
数据集/data/train/ants_image

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

0
matplotlib Normal file

BIN
model.pth Normal file

Binary file not shown.

1
myenv/bin/python Symbolic link

@ -0,0 +1 @@
python3

1
myenv/bin/python3 Symbolic link

@ -0,0 +1 @@
/usr/bin/python3

1
myenv/bin/python3.10 Symbolic link

@ -0,0 +1 @@
python3

1
myenv/lib64 Symbolic link

@ -0,0 +1 @@
lib

3
myenv/pyvenv.cfg Normal file

@ -0,0 +1,3 @@
home = /usr/bin
include-system-site-packages = false
version = 3.10.12

70
read_data.py Normal file

@ -0,0 +1,70 @@
from torch.utils.data import Dataset, DataLoader
import numpy as np
from PIL import Image
import os
from torchvision import transforms
from torch.utils.tensorboard import SummaryWriter
from torchvision.utils import make_grid
writer = SummaryWriter("logs")
class MyData(Dataset):
def __init__(self, root_dir, image_dir, label_dir, transform):
self.root_dir = root_dir
self.image_dir = image_dir
self.label_dir = label_dir
self.label_path = os.path.join(self.root_dir, self.label_dir)
self.image_path = os.path.join(self.root_dir, self.image_dir)
self.image_list = os.listdir(self.image_path)
self.label_list = os.listdir(self.label_path)
self.transform = transform
# 因为label 和 Image文件名相同进行一样的排序可以保证取出的数据和label是一一对应的
self.image_list.sort()
self.label_list.sort()
def __getitem__(self, idx):
img_name = self.image_list[idx]
label_name = self.label_list[idx]
img_item_path = os.path.join(self.root_dir, self.image_dir, img_name)
label_item_path = os.path.join(self.root_dir, self.label_dir, label_name)
img = Image.open(img_item_path)
with open(label_item_path, 'r') as f:
label = f.readline()
# img = np.array(img)
img = self.transform(img)
sample = {'img': img, 'label': label}
return sample
def __len__(self):
assert len(self.image_list) == len(self.label_list)
return len(self.image_list)
if __name__ == '__main__':
transform = transforms.Compose([transforms.Resize((256, 256)), transforms.ToTensor()])
root_dir = "dataset/train"
image_ants = "ants_image"
label_ants = "ants_label"
ants_dataset = MyData(root_dir, image_ants, label_ants, transform)
image_bees = "bees_image"
label_bees = "bees_label"
bees_dataset = MyData(root_dir, image_bees, label_bees, transform)
train_dataset = ants_dataset + bees_dataset
# transforms = transforms.Compose([transforms.Resize(256, 256)])
dataloader = DataLoader(train_dataset, batch_size=1, num_workers=2)
writer.add_image('error', train_dataset[119]['img'])
writer.close()
for i, j in enumerate(dataloader):
# imgs, labels = j
print(type(j))
print(i, j['img'].shape)
# writer.add_image("train_data_b2", make_grid(j['img']), i)
writer.close()

@ -1,8 +1,9 @@
#这些注释都是原来的错误代码。最后是找了AI 解决的(🤓)
import torch import torch
from torchvision import datasets, transforms from torchvision import datasets, transforms
from torch.utils.data import DataLoader from torch.utils.data import DataLoader
from torchvision.datasets import MNIST from torchvision.datasets import MNIST
import matplotlib.pyplot as plt #import matplotlib.pyplot as plt #可有可无
device = 'cuda:0' device = 'cuda:0'
class Net(torch.nn.Module): class Net(torch.nn.Module):
@ -31,7 +32,7 @@ def evaluate(test_data, net):
n_total = 0 n_total = 0
with torch.no_grad(): with torch.no_grad():
for (x, y) in test_data: for (x, y) in test_data:
# 将数据发送到GPU # 将数据发送到GPU(这个02是没有的)
x = x.view(-1, 28 * 28).to(device) x = x.view(-1, 28 * 28).to(device)
y = y.to(device) y = y.to(device)
output = net(x) output = net(x)
@ -57,15 +58,15 @@ def main():
print('initial accuracy', evaluate(test_data, net)) print('initial accuracy', evaluate(test_data, net))
optimizer = torch.optim.Adam(net.parameters()) optimizer = torch.optim.Adam(net.parameters())
for epoch in range(3): for epoch in range(100):
for (x, y) in train_data: for (x, y) in train_data:
# 将数据发送到GPU # 将数据发送到GPU(这个02是没有的)
x = x.view(-1, 28 * 28).to(device) x = x.view(-1, 28 * 28).to(device)
y = y.to(device) y = y.to(device)
net.zero_grad() net.zero_grad()
output = net(x) output = net(x)
loss = torch.nn.functional.nll_loss(output, y) loss = torch.nn.functional.nll_loss(output, y)
loss.backward() loss.backward()#02没有这个导致正确率也没有提高
optimizer.step() optimizer.step()
print('epoch', epoch, 'accuracy', evaluate(test_data, net)) print('epoch', epoch, 'accuracy', evaluate(test_data, net))
torch.save(net,'./model.pth') torch.save(net,'./model.pth')

1
venv_2/bin/python Symbolic link

@ -0,0 +1 @@
python3

1
venv_2/bin/python3 Symbolic link

@ -0,0 +1 @@
/home/mtftau-5/workplace/.venv/bin/python3

1
venv_2/bin/python3.10 Symbolic link

@ -0,0 +1 @@
python3

1
venv_2/lib64 Symbolic link

@ -0,0 +1 @@
lib

3
venv_2/pyvenv.cfg Normal file

@ -0,0 +1,3 @@
home = /home/mtftau-5/workplace/.venv/bin
include-system-site-packages = false
version = 3.10.12

0
插件/nn Normal file

0
插件/np Normal file

0
插件/os Normal file

0
插件/plt Normal file

0
插件/sympy Normal file

0
插件/torch Normal file

0
插件/torchvision Normal file

Binary file not shown.

After

(image error) Size: 46 KiB

Binary file not shown.

After

(image error) Size: 170 KiB

Binary file not shown.

After

(image error) Size: 124 KiB

Binary file not shown.

After

(image error) Size: 72 KiB

Binary file not shown.

After

(image error) Size: 140 KiB

Binary file not shown.

After

(image error) Size: 67 KiB

Binary file not shown.

After

(image error) Size: 90 KiB

Binary file not shown.

After

(image error) Size: 57 KiB

Binary file not shown.

After

(image error) Size: 99 KiB

Binary file not shown.

After

(image error) Size: 111 KiB

Binary file not shown.

After

(image error) Size: 139 KiB

Binary file not shown.

After

(image error) Size: 89 KiB

Binary file not shown.

After

(image error) Size: 149 KiB

Binary file not shown.

After

(image error) Size: 100 KiB

Binary file not shown.

After

(image error) Size: 105 KiB

Binary file not shown.

After

(image error) Size: 107 KiB

Binary file not shown.

After

(image error) Size: 164 KiB

Binary file not shown.

After

(image error) Size: 155 KiB

Binary file not shown.

After

(image error) Size: 104 KiB

Binary file not shown.

After

(image error) Size: 57 KiB

Binary file not shown.

After

(image error) Size: 164 KiB

Binary file not shown.

After

(image error) Size: 162 KiB

Binary file not shown.

After

(image error) Size: 73 KiB

Binary file not shown.

After

(image error) Size: 49 KiB

Binary file not shown.

After

(image error) Size: 88 KiB

Binary file not shown.

After

(image error) Size: 29 KiB

Binary file not shown.

After

(image error) Size: 112 KiB

Binary file not shown.

After

(image error) Size: 191 KiB

Binary file not shown.

After

(image error) Size: 100 KiB

Binary file not shown.

After

(image error) Size: 79 KiB

Binary file not shown.

After

(image error) Size: 128 KiB

Binary file not shown.

After

(image error) Size: 174 KiB

Binary file not shown.

After

(image error) Size: 96 KiB

Binary file not shown.

After

(image error) Size: 134 KiB

Binary file not shown.

After

(image error) Size: 91 KiB

Binary file not shown.

After

(image error) Size: 186 KiB

Binary file not shown.

After

(image error) Size: 40 KiB

Binary file not shown.

After

(image error) Size: 69 KiB

Binary file not shown.

After

(image error) Size: 136 KiB

Binary file not shown.

After

(image error) Size: 104 KiB

Binary file not shown.

After

(image error) Size: 208 KiB

Binary file not shown.

After

(image error) Size: 136 KiB

Binary file not shown.

After

(image error) Size: 110 KiB

Binary file not shown.

After

(image error) Size: 127 KiB

Binary file not shown.

After

(image error) Size: 142 KiB

Binary file not shown.

After

(image error) Size: 38 KiB

Binary file not shown.

After

(image error) Size: 66 KiB

Binary file not shown.

After

(image error) Size: 72 KiB

Binary file not shown.

After

(image error) Size: 94 KiB

Binary file not shown.

After

(image error) Size: 106 KiB

Binary file not shown.

After

(image error) Size: 61 KiB

Binary file not shown.

After

(image error) Size: 192 KiB

Binary file not shown.

After

(image error) Size: 93 KiB

Binary file not shown.

After

(image error) Size: 218 KiB

Binary file not shown.

After

(image error) Size: 76 KiB

Binary file not shown.

After

(image error) Size: 19 KiB

Some files were not shown because too many files have changed in this diff Show More