├── .gitignore ├── 计算智能导论 ├── long.mat ├── result.jpg ├── 计算智能排列作业.py ├── linear_plot.py ├── perceptron.py ├── kmeans.py ├── 神经网络二分类.py ├── sonar_kmeans.py ├── kmeans_photo.py ├── fcm.py ├── 计算智能.ipynb ├── 遗传算法.py ├── perceptron_self.py └── test_perceptron.py ├── 智能系统实验 ├── 实验报告模板.docx ├── 4_2 │ ├── 测试样本集.py │ ├── 测试动物网络.py │ ├── test-animal.py │ ├── 动物分类2.py │ ├── 动物分类1.py │ ├── net_model.py │ └── train_new.py ├── 1异或self.py ├── 4_1 │ ├── net.py │ ├── dataprocess.py │ └── 04.py ├── 数据回归.py ├── 数据分类.py ├── 03.py ├── 02.py ├── 异或.py ├── 手写识别.py └── google图像分类.py ├── 数据挖掘 ├── data-密度聚类 │ ├── 2d4c.mat │ ├── long.mat │ ├── moon.mat │ ├── sizes5.mat │ ├── smile.mat │ ├── spiral.mat │ ├── square1.mat │ └── square4.mat ├── dbscan.py ├── test.py ├── apriori.py ├── 密度聚类.py ├── Pattern Mining.py ├── net_pattern mining.py └── pattern mining.ipynb ├── README.md ├── 模式识别 ├── svm │ ├── process.py │ └── main.py ├── fcm │ ├── fcm_self.py │ └── fcm.py ├── kmeans │ ├── kmeans_iris.py │ └── kmeans_pic.py └── fisher_knn │ └── iris_classification.py ├── 图像超分辨项目 ├── create_data_lists.py ├── test.py ├── datasets.py ├── train_srresnet.py ├── models.py └── utils.py ├── 深度学习 ├── OCR_ALL │ ├── dataset.py │ ├── test_model.py │ ├── process.py │ ├── main_simple.py │ └── main.py └── FashionMnist_self.py ├── LICENSE ├── 机器学习 ├── 朴素贝叶斯.py ├── 机器学习上机线性回归.py ├── 神经网络法.py ├── 高光谱.py ├── 贝叶斯分类2.ipynb └── price_predict.py └── 人工智能概论 └── 遗传算法.py /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | .vscode -------------------------------------------------------------------------------- /计算智能导论/long.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CoderJackZhu/XDU_AI_project/HEAD/计算智能导论/long.mat -------------------------------------------------------------------------------- /计算智能导论/result.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CoderJackZhu/XDU_AI_project/HEAD/计算智能导论/result.jpg -------------------------------------------------------------------------------- /智能系统实验/实验报告模板.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CoderJackZhu/XDU_AI_project/HEAD/智能系统实验/实验报告模板.docx -------------------------------------------------------------------------------- /数据挖掘/data-密度聚类/2d4c.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CoderJackZhu/XDU_AI_project/HEAD/数据挖掘/data-密度聚类/2d4c.mat -------------------------------------------------------------------------------- /数据挖掘/data-密度聚类/long.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CoderJackZhu/XDU_AI_project/HEAD/数据挖掘/data-密度聚类/long.mat -------------------------------------------------------------------------------- /数据挖掘/data-密度聚类/moon.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CoderJackZhu/XDU_AI_project/HEAD/数据挖掘/data-密度聚类/moon.mat -------------------------------------------------------------------------------- /数据挖掘/data-密度聚类/sizes5.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CoderJackZhu/XDU_AI_project/HEAD/数据挖掘/data-密度聚类/sizes5.mat -------------------------------------------------------------------------------- /数据挖掘/data-密度聚类/smile.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CoderJackZhu/XDU_AI_project/HEAD/数据挖掘/data-密度聚类/smile.mat -------------------------------------------------------------------------------- /数据挖掘/data-密度聚类/spiral.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CoderJackZhu/XDU_AI_project/HEAD/数据挖掘/data-密度聚类/spiral.mat -------------------------------------------------------------------------------- /数据挖掘/data-密度聚类/square1.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CoderJackZhu/XDU_AI_project/HEAD/数据挖掘/data-密度聚类/square1.mat -------------------------------------------------------------------------------- /数据挖掘/data-密度聚类/square4.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CoderJackZhu/XDU_AI_project/HEAD/数据挖掘/data-密度聚类/square4.mat -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 项目简介 2 | 本仓库是西电2018级智能院大三大作业的部分内容,比较基础的代码基本都放在里面了。个人写的,比较简陋,给学弟学妹们一个参考,重要还是要自己学会,虽然本科期间对代码能力基本没什么要求,但是如果将来从事相关行业的话,还是要自己学会的。 -------------------------------------------------------------------------------- /模式识别/svm/process.py: -------------------------------------------------------------------------------- 1 | import os 2 | import cv2 3 | import numpy as np 4 | from sklearn import preprocessing 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /计算智能导论/计算智能排列作业.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Mon Apr 5 19:48:15 2021 4 | 5 | @author: tremble 6 | """ 7 | import numpy as np 8 | array=np.arange(1,26) 9 | result=[] 10 | for i in range(100): 11 | b=[np.random.permutation(array)] 12 | result.append(b) 13 | print(result) -------------------------------------------------------------------------------- /图像超分辨项目/create_data_lists.py: -------------------------------------------------------------------------------- 1 | from utils import create_data_lists 2 | 3 | if __name__ == '__main__': 4 | create_data_lists(train_folders=['./data/Train','./data/BSD100'], 5 | test_folders=['./data/Set5', 6 | './data/Set14'], 7 | min_size=100, 8 | output_folder='./data/') 9 | -------------------------------------------------------------------------------- /计算智能导论/linear_plot.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Fri Apr 2 16:04:00 2021 4 | 5 | @author: tremble 6 | """ 7 | 8 | import matplotlib.pyplot as plt 9 | plt.rcParams['font.sans-serif'] = ['SimHei'] 10 | x1 = [3,6] 11 | x2 = [4,5] 12 | x3 = [0,0] 13 | plt.plot(x1[0],x1[1],'*') 14 | plt.plot(x2[0],x2[1],'*') 15 | for x in range(0,201): 16 | for y in range(0,201): 17 | i = 0.01 * x 18 | j = 0.01 * y 19 | if(i+j == 2 or i==0 or j==0): 20 | x3[0] = i*x1[0] + j*x2[0] 21 | x3[1] = i*x1[1] + j*x2[1] 22 | plt.plot(x3[0],x3[1],'.') 23 | plt.xlabel('x1') 24 | plt.ylabel('x2') 25 | plt.title('线性') 26 | plt.show() 27 | -------------------------------------------------------------------------------- /智能系统实验/4_2/测试样本集.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import torch 3 | import torch.nn as nn 4 | import matplotlib.pyplot as plt 5 | import cv2 6 | from torch.utils.data import TensorDataset 7 | from torch.utils.data import DataLoader 8 | 9 | train_set=np.load('train_set.npy') 10 | test_set = np.load('test_set.npy') 11 | img1=train_set[69,0,:,:] 12 | img2=test_set[30,0,:,:] 13 | img1=img1.astype(np.uint8) 14 | img2=img2.astype(np.uint8) 15 | 16 | print(img1) 17 | 18 | device = torch.device("cuda" if torch.cuda.is_available() else "cpu") 19 | label = np.load('test_label.npy') 20 | 21 | 22 | x=np.load(file='test_set.npy')/255 23 | 24 | x=torch.tensor(x).type(torch.FloatTensor).to(device) 25 | y1=torch.zeros(30) 26 | y2=torch.zeros(30) 27 | y0=torch.cat((y1,y2)).type(torch.LongTensor).to(device) 28 | 29 | print(test_set.shape) 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /智能系统实验/1异或self.py: -------------------------------------------------------------------------------- 1 | import torch 2 | import torch.nn as nn 3 | x=[[0,0],[0,1],[1,0],[1,1]] 4 | y=[[0],[1],[1],[0]] 5 | x=torch.FloatTensor(x).cuda() 6 | y=torch.FloatTensor(y).cuda() 7 | net=nn.Sequential( 8 | nn.Linear(2,20), 9 | nn.ReLU(), 10 | nn.Linear(20,1), 11 | nn.Sigmoid() 12 | ).cuda() 13 | 14 | optimizer=torch.optim.SGD(net.parameters(),lr=0.05) 15 | loss_func=nn.MSELoss() 16 | for epoch in range(5000): 17 | out=net(x) 18 | loss=loss_func(out,y) 19 | optimizer.zero_grad() 20 | loss.backward() 21 | optimizer.step() 22 | if epoch%100==0: 23 | print(f"迭代次数:{epoch}") 24 | print(f"误差:{loss}") 25 | 26 | out=net(x).cpu() 27 | print(f"out:{out.data}") 28 | torch.save(net,'./异或net.pkl') 29 | 30 | test_net=torch.load('异或net.pkl') 31 | test_net.eval() 32 | test_x=[[0,0]] 33 | test_x=torch.FloatTensor(test_x).cuda() 34 | out_final=test_net(test_x) 35 | if out_final>0.5: 36 | print('1') 37 | else: 38 | print('0') -------------------------------------------------------------------------------- /深度学习/OCR_ALL/dataset.py: -------------------------------------------------------------------------------- 1 | import os 2 | import numpy as np 3 | import pandas as pd 4 | from PIL import Image 5 | from torch.utils.data import Dataset 6 | from torchvision.transforms import ToTensor 7 | 8 | 9 | class MyDataset(Dataset): 10 | 11 | def __init__(self, path, mode="test", transformer=ToTensor()): 12 | info_path = f"{path}/{mode}_info.txt" 13 | path = f"{path}/{mode}" 14 | info = pd.read_csv(info_path, sep=" ", header=None) 15 | labels = np.array(info.iloc[:-1, 0]) 16 | img_paths = [] 17 | for i, label in enumerate(labels): 18 | for file_name in os.listdir(f"{path}/{label}"): 19 | img_paths.append((i, f"{path}/{label}/{file_name}")) 20 | self.img_path = img_paths 21 | self.transformer = transformer 22 | 23 | def __len__(self): 24 | return len(self.img_path) 25 | 26 | def __getitem__(self, idx): 27 | i, path = self.img_path[idx] 28 | image = Image.open(path) 29 | return self.transformer(image), i 30 | -------------------------------------------------------------------------------- /智能系统实验/4_1/net.py: -------------------------------------------------------------------------------- 1 | import torch 2 | import numpy as np 3 | import torch.nn as nn 4 | 5 | 6 | class ICNET(nn.Module): 7 | def __init__(self): 8 | super(ICNET, self).__init__() 9 | self.conv = nn.Sequential( 10 | nn.Conv2d(3,8,kernel_size=3, stride=1, padding=1), 11 | nn.ReLU(), 12 | nn.Conv2d(8,8,kernel_size=3, stride=1, padding=1), 13 | nn.ReLU(), 14 | nn.MaxPool2d(kernel_size=2, stride=2, padding=0) 15 | ) 16 | self.fc = nn.Sequential( 17 | nn.Linear(64*64*8,1000), 18 | nn.ReLU(), 19 | nn.Linear(1000, 1000), 20 | nn.ReLU(), 21 | nn.Linear(1000, 2), 22 | nn.Softmax(dim=1) 23 | ) 24 | 25 | def forward(self, x): 26 | x = self.conv(x) 27 | x = x.view(x.size()[0], -1) 28 | x = self.fc(x) 29 | return x 30 | 31 | 32 | if __name__ == '__main__': 33 | x = torch.zeros(2,3,224,224).cuda() 34 | net = ICNET().cuda() 35 | out = net(x) 36 | print(out.detach().cpu().numpy()) 37 | -------------------------------------------------------------------------------- /数据挖掘/dbscan.py: -------------------------------------------------------------------------------- 1 | # -*- coding:utf-8 -*- 2 | # Author : JackZhu 3 | 4 | # Data : 2021/5/9 13:55 5 | 6 | import sklearn 7 | import scipy.io as scio 8 | import matplotlib.pyplot as plt 9 | import random 10 | import numpy as np 11 | 12 | r=0.2 13 | minpts=5 14 | 15 | path='./data-密度聚类/2d4c.mat' 16 | data = scio.loadmat(path)['moon'] 17 | plt.scatter(data[:,0],data[:,1]) 18 | plt.show() 19 | D=data[:,0:2] 20 | row,col=D.shape[0],D.shape[1] 21 | ls=list(range(row)) 22 | type_all=[] 23 | noise_ls=[] 24 | j=0 25 | while(ls): 26 | type_ls=[] 27 | point=random.choice(ls) 28 | print(point) 29 | while(1): 30 | for i in ls: 31 | if(np.linalg.norm(D[point]-D[i])<=r): 32 | type_ls.append(i) 33 | print(type_ls) 34 | if(len(type_ls)>=5): 35 | j=j+1 36 | ls=type_ls 37 | continue 38 | 39 | elif(len(type_ls)<5 and len(type_ls)>0): 40 | pass 41 | elif(len(type_ls)==0): 42 | noise_ls.append(point) 43 | ls.remove(point) 44 | print(type_ls) 45 | break 46 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) JackZhu 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE 22 | -------------------------------------------------------------------------------- /智能系统实验/4_2/测试动物网络.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import torch 3 | import torch.nn as nn 4 | import torch.optim as optim 5 | import matplotlib.pyplot as plt 6 | import cv2 7 | from torch.utils.data import TensorDataset 8 | from torch.utils.data import DataLoader 9 | 10 | feature = np.load('test_set.npy') 11 | label = np.load('test_label.npy') 12 | 13 | 14 | device = torch.device("cuda" if torch.cuda.is_available() else "cpu") 15 | model = torch.load('动物分类.pkl') 16 | feature=torch.tensor(feature).type(torch.FloatTensor) 17 | label=torch.tensor(label).type(torch.FloatTensor) 18 | 19 | 20 | 21 | dataset = TensorDataset(feature, label) 22 | dataloader = DataLoader(dataset, batch_size=4, shuffle=True) 23 | 24 | criterion = nn.CrossEntropyLoss() 25 | optimizer = optim.Adadelta(model.parameters(), lr=1.0) 26 | 27 | def test(): 28 | correct = 0.0 29 | with torch.no_grad(): 30 | for data, target in dataloader: 31 | data, target = data.to(device), target.to(device) 32 | output = model(data) 33 | pred = output.argmax(dim=1, keepdim=True) 34 | correct += pred.eq(target.view_as(pred)).sum().item() 35 | print(correct) 36 | print('accuracy:{:.2f}%'.format(100.0 * correct / len(dataloader.dataset))) 37 | 38 | 39 | if __name__ == '__main__': 40 | test() -------------------------------------------------------------------------------- /深度学习/OCR_ALL/test_model.py: -------------------------------------------------------------------------------- 1 | import os 2 | import main 3 | import torch 4 | from main import test 5 | import torch.nn as nn 6 | from dataset import MyDataset 7 | from torchvision import transforms 8 | from main import Net 9 | import torch.optim as optim 10 | transform = transforms.Compose([ 11 | transforms.Resize((64, 64)), 12 | transforms.ToTensor(), 13 | lambda _: _.mean(0).unsqueeze(0), 14 | transforms.Normalize((0.1307,), (0.3081,)), 15 | ]) 16 | use_cuda = torch.cuda.is_available() 17 | device = torch.device("cuda" if use_cuda else "cpu") 18 | 19 | test_dataset = MyDataset("./dataset", "test", transformer = transform) 20 | testloader = torch.utils.data.DataLoader(test_dataset, batch_size=4, 21 | shuffle=False, num_workers=0) 22 | 23 | if use_cuda: 24 | model = nn.DataParallel(Net()).cuda() 25 | else: 26 | model = Net().to(device) 27 | 28 | optimizer = optim.Adadelta(model.parameters(), lr=1.0) 29 | transform = transforms.Compose([ 30 | transforms.Resize((64, 64)), 31 | transforms.ToTensor(), 32 | lambda _: _.mean(0).unsqueeze(0), 33 | transforms.Normalize((0.1307,), (0.3081,)), 34 | ]) 35 | path = '.\good_models' 36 | files = os.listdir(path) 37 | for file in files: 38 | file_path = os.path.join(path,file) 39 | print(file_path) 40 | model_dict=torch.load(file_path) 41 | model = model.load_state_dict(model_dict) 42 | test_acc = test(model,device, testloader) 43 | -------------------------------------------------------------------------------- /智能系统实验/数据回归.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import torch 3 | import torch.nn as nn 4 | import matplotlib.pyplot as plt 5 | 6 | # plt.rcParams['font.sans-serif'] = ['SimHei'] 7 | 8 | x = torch.unsqueeze(torch.linspace(-np.pi, np.pi, 100), dim=1) 9 | y = torch.sin(x) + 0.5 * torch.rand(x.size()) 10 | 11 | 12 | class Net(nn.Module): 13 | def __init__(self): 14 | super(Net, self).__init__() 15 | self.predict = nn.Sequential( 16 | nn.Linear(1, 10), 17 | nn.ReLU(), 18 | nn.Linear(10, 1) 19 | ) 20 | 21 | def forward(self, x): 22 | prediction = self.predict(x) 23 | return prediction 24 | 25 | 26 | device = torch.device("cuda" if torch.cuda.is_available() else "cpu") 27 | net = Net().to(device) 28 | x, y = x.to(device), y.to(device) 29 | optimizer = torch.optim.SGD(net.parameters(), lr=0.05) 30 | loss_func = nn.MSELoss() 31 | 32 | 33 | fig = plt.figure() 34 | plt.ion() 35 | epoch_list, loss_list = [], [] 36 | for epoch in range(1, 1001): 37 | out = net(x) 38 | loss = loss_func(out, y) 39 | optimizer.zero_grad() 40 | loss.backward() 41 | optimizer.step() 42 | plt.scatter(x.cpu(), y.cpu(), color='y') 43 | epoch_list.append(epoch) 44 | loss_list.append(loss.cpu().detach()) 45 | if epoch % 100 == 0: 46 | print(f"epoch:{epoch},loss:{loss}") 47 | plt.plot(x.cpu(), net(x).cpu().detach(), color='r', linestyle="-") 48 | plt.xlabel('x') 49 | plt.ylabel('y') 50 | plt.title('result') 51 | fig = plt.figure() 52 | plt.plot(epoch_list, loss_list) 53 | plt.xlabel('epoch') 54 | plt.ylabel('loss') 55 | plt.title('error') 56 | plt.ioff() 57 | plt.show() 58 | -------------------------------------------------------------------------------- /智能系统实验/4_1/dataprocess.py: -------------------------------------------------------------------------------- 1 | import torch 2 | import cv2 as cv 3 | import numpy as np 4 | 5 | ''' 6 | 构建样本集 7 | ''' 8 | m1 = 70 9 | m2 = 30 10 | l_x = 128 11 | train_set = np.zeros(m1*2*l_x*l_x*3) 12 | train_set = np.reshape(train_set,(m1*2,3,l_x,l_x)) 13 | test_set = np.zeros(m2*2*l_x*l_x*3) 14 | test_set = np.reshape(test_set,(m2*2,3,l_x,l_x)) 15 | 16 | # 猫为0,狗为1 17 | success_mark = 0 18 | train_label = torch.cat((torch.zeros(70),torch.ones(70))) 19 | for i in range(m1): 20 | path1 = f'./sample/cat.{i}.jpg' 21 | path2 = f'./sample/dog.{i}.jpg' 22 | img1 = cv.resize(cv.imread(path1), (l_x,l_x)) 23 | img2 = cv.resize(cv.imread(path2), (l_x,l_x)) 24 | train_set[i,0,:,:] = img1[:,:,0] 25 | train_set[i,1,:,:] = img1[:,:,1] 26 | train_set[i,2,:,:] = img1[:,:,2] 27 | success_mark+=1 28 | train_set[m1+i,0,:,:] = img2[:,:,0] 29 | train_set[m1+i,1,:,:] = img2[:,:,1] 30 | train_set[m1+i,2,:,:] = img2[:,:,2] 31 | success_mark+=1 32 | 33 | for i in range(m2): 34 | path1 = f'./sample/cat.{i+m1}.jpg' 35 | path2 = f'./sample/dog.{i+m1}.jpg' 36 | img1 = cv.resize(cv.imread(path1), (l_x,l_x)) 37 | img2 = cv.resize(cv.imread(path2), (l_x,l_x)) 38 | test_set[i,0,:,:] = img1[:,:,0] 39 | test_set[i,1,:,:] = img1[:,:,1] 40 | test_set[i,2,:,:] = img1[:,:,2] 41 | success_mark+=1 42 | test_set[m2+i,0,:,:] = img2[:,:,0] 43 | test_set[m2+i,1,:,:] = img2[:,:,1] 44 | test_set[m2+i,2,:,:] = img2[:,:,2] 45 | success_mark+=1 46 | 47 | print('Split the data: Done!') 48 | if success_mark == 200: 49 | np.save('./catdog_train_set224.npy',train_set) 50 | np.save('./catdog_test_set224.npy',test_set) -------------------------------------------------------------------------------- /机器学习/朴素贝叶斯.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Sat Apr 10 19:20:42 2021 4 | 5 | @author: tremble 6 | """ 7 | 8 | import numpy as np 9 | from sklearn.metrics import accuracy_score 10 | 11 | class NaiveBayes: 12 | 13 | def __init__(self): 14 | pass 15 | 16 | def gaussion_pdf(self, x_test, x): 17 | """定义高斯分布""" 18 | temp1 = (x_test - x.mean(0)) * (x_test - x.mean(0)) 19 | temp2 = x.std(0) * x.std(0) 20 | return np.exp(-temp1 / (2 * temp2)) / np.sqrt(2 * np.pi * temp2) 21 | 22 | def fit(self, x_train, y_train): 23 | """读取训练数据""" 24 | self.x_train = x_train 25 | self.y_train = y_train 26 | return self 27 | 28 | def predict(self, x_test): 29 | """根据新样本的已有特征在数据集中的条件概率来判断新样本所属类别""" 30 | assert len(x_test.shape) == 2 31 | self.classes = np.unique(self.y_train) 32 | pred_probs = [] 33 | # 对于每个输入,计算其处于每个类别的概率 34 | for i in self.classes: 35 | idx_i = self.y_train == i 36 | # 计算P(y) 37 | p_y = len(idx_i) / len(self.y_train) 38 | # 利用高斯概率密度函数计算P(x|y) 39 | p_x_y = np.prod(self.gaussion_pdf(x_test, self.x_train[idx_i]), 1) 40 | # 计算x,y的联合概率,P(x|y)P(y) 41 | prob_i = p_y * p_x_y 42 | pred_probs.append(prob_i) 43 | pred_probs = np.vstack(pred_probs) 44 | # 取具有最高概率的类别 45 | label_idx = pred_probs.argmax(0) 46 | y_pred = self.classes[label_idx] 47 | return y_pred 48 | 49 | def score(self, X_test, y_test): 50 | """根据测试数据集确定当前模型的准确度""" 51 | y_predict = self.predict(X_test) 52 | return accuracy_score(y_test, y_predict) 53 | -------------------------------------------------------------------------------- /智能系统实验/数据分类.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import torch 3 | import torch.nn as nn 4 | import matplotlib.pyplot as plt 5 | 6 | 7 | device = torch.device("cuda" if torch.cuda.is_available() else "cpu") 8 | 9 | data=torch.ones(100,2) 10 | x0=torch.normal(2*data,1) 11 | x1=torch.normal(-2*data,1) 12 | x=torch.cat((x0,x1),0).type(torch.FloatTensor).to(device) 13 | y0=torch.zeros(100) 14 | y1=torch.ones(100) 15 | y=torch.cat((y0,y1)).type(torch.LongTensor).to(device) 16 | 17 | class Net(nn.Module): 18 | def __init__(self): 19 | super(Net, self).__init__() 20 | self.classify=nn.Sequential( 21 | nn.Linear(2,15), 22 | nn.ReLU(), 23 | nn.Linear(15,2), 24 | nn.Softmax(dim=1) 25 | ) 26 | def forward(self,x): 27 | classification=self.classify(x) 28 | return classification 29 | 30 | 31 | net=Net().to(device) 32 | optimizer=torch.optim.SGD(net.parameters(),lr=0.03) 33 | loss_func=nn.CrossEntropyLoss() 34 | 35 | epoch_list,loss_list=[],[] 36 | for epoch in range(1000): 37 | out=net(x) 38 | loss=loss_func(out,y) 39 | optimizer.zero_grad() 40 | loss.backward() 41 | optimizer.step() 42 | epoch_list.append(epoch) 43 | loss_list.append(loss.cpu().detach()) 44 | if epoch%10==0: 45 | print(f"epoch{epoch},loss:{loss}") 46 | 47 | classification=torch.max(out,1)[1] 48 | class_y=classification.cpu().data.numpy() 49 | target_y=y.cpu().data.numpy() 50 | plt.figure() 51 | plt.scatter(x.cpu().data.numpy()[:,0],x.cpu().data.numpy()[:,1],c=class_y,s=100,cmap='RdYlGn') 52 | accuracy=(class_y==target_y).sum()/200 53 | plt.text(1.5,-4,f'Accuracy={accuracy}',fontdict={'size':20,'color':'red'}) 54 | plt.figure() 55 | plt.plot(epoch_list,loss_list,':') 56 | plt.xlabel('epoch') 57 | plt.ylabel('loss') 58 | plt.show() 59 | -------------------------------------------------------------------------------- /模式识别/svm/main.py: -------------------------------------------------------------------------------- 1 | from sklearn import svm 2 | from sklearn.model_selection import train_test_split 3 | from sklearn.preprocessing import normalize 4 | import os 5 | import cv2 6 | import numpy as np 7 | from sklearn import preprocessing 8 | 9 | def process(path): 10 | data = [] 11 | label = [] 12 | 13 | packages = os.listdir(path) 14 | for pack in packages: 15 | label = int(pack[1:]) 16 | 17 | dir = os.path.join(path, pak) 18 | faces = os.listdir(dir) 19 | for face in faces: 20 | face_dir = os.path.join(dir, face) 21 | pic = cv2.imread(face_dir, 0) 22 | arr = np.array(pic) 23 | arr = cv2.resize(arr, (50, 50)) 24 | data.append(arr) 25 | label.append(label) 26 | 27 | data = np.array(data).reshape(len(labels), -1) 28 | data = preprocessing.scale(data) 29 | 30 | labels = np.array(labels) 31 | 32 | return data, labels 33 | 34 | def main(): 35 | 36 | path='./YALE' 37 | 38 | datas, labels = process(path) 39 | 40 | 41 | data_train, data_test, label_train, label_test = train_test_split(data, label,test_size=0.3,random_state=1,shuffle=True) 42 | print(data_train) 43 | 44 | 45 | rbf_svm = svm.SVC(C=1.5,kernel='rbf',decision_function_shape='ovr') 46 | linear_svm = svm.SVC(C=1,kernel = 'poly',decision_function_shape = 'ovr',degree= 3) 47 | 48 | 49 | rbf_svm.fit(data_train,label_train) 50 | linear_svm.fit(data_train,label_train) 51 | 52 | rbf_predict = rbf_svm.predict(data_test) 53 | linear_svm = linear_svm.predict(data_test) 54 | 55 | 56 | acc1 = sum(rbf_predict == label_test)/len(label_test) 57 | acc2 = sum(linear_svm == label_test )/len(label_test) 58 | 59 | print('accuracy1',acc1) 60 | print('accuracy2',acc2) 61 | 62 | if __name__ =='__main__': 63 | main() -------------------------------------------------------------------------------- /机器学习/机器学习上机线性回归.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Fri Apr 9 13:25:47 2021 4 | 5 | @author: tremble 6 | """ 7 | 8 | import pandas as pd 9 | import numpy as np 10 | import torch 11 | import torch.nn as nn 12 | import torch.optim as optim 13 | import matplotlib.pyplot as plt 14 | 15 | data = pd.read_csv('./advertising.csv') 16 | features = np.array(data.iloc[:, 0:3]) 17 | features = torch.tensor(features, dtype=torch.float) 18 | labels = np.array(data.iloc[:, 3]) 19 | labels = torch.tensor(labels, dtype=torch.float) 20 | dataset = torch.utils.data.TensorDataset(features, labels) 21 | dataloader = torch.utils.data.DataLoader(dataset, batch_size=4, shuffle=True) 22 | 23 | 24 | class Linear_Net(nn.Module): 25 | def __init__(self, n_features): 26 | super(Linear_Net, self).__init__() 27 | self.linear = nn.Linear(n_features, 1) 28 | 29 | def forward(self, x): 30 | x = self.linear(x) 31 | return x 32 | 33 | 34 | net = Linear_Net(3) 35 | torch.nn.init.normal_(net.linear.weight, mean=0, std=0.001) 36 | torch.nn.init.constant_(net.linear.bias, val=0) 37 | loss = nn.MSELoss() 38 | optimizer = optim.Adam(net.parameters(), lr=0.01) 39 | epochs = 50 40 | epoch_list, avg_l_list = [], [] 41 | for epoch in range(1, epochs + 1): 42 | loss_sum = 0 43 | for x, y in dataloader: 44 | output = net(x) 45 | l = loss(y.view(-1, 1), output) 46 | optimizer.zero_grad() 47 | l.backward() 48 | optimizer.step() 49 | loss_sum += l.detach() 50 | epoch_list.append(epoch) 51 | avg_l_list.append(loss_sum / labels.shape[0]) 52 | print('Epoch:{},Loss:{:.2f}'.format(epoch, loss_sum / labels.shape[0])) 53 | plt.plot(epoch_list, avg_l_list) 54 | plt.title('error') 55 | plt.xlabel('epoch') 56 | plt.ylabel('loss') 57 | plt.show() 58 | 59 | print(net.linear.weight, net.linear.bias) 60 | -------------------------------------------------------------------------------- /智能系统实验/4_2/test-animal.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import torch 3 | import torch.nn as nn 4 | import torch.optim as optim 5 | import torchvision 6 | import torch.nn.functional as F 7 | import matplotlib.pyplot as plt 8 | import cv2 9 | from net_model import ICNET 10 | from net_model import RESNET 11 | from net_model import Net1 12 | from torch.utils.data import TensorDataset 13 | from torch.utils.data import DataLoader 14 | from torchvision import transforms 15 | import os 16 | 17 | transform = transforms.Compose([transforms.Resize((224,224)), transforms.ToTensor()]) 18 | 19 | testset = torchvision.datasets.ImageFolder(root='./data/test', transform=transform) 20 | test_loader = torch.utils.data.DataLoader(testset, batch_size=4, shuffle=False, num_workers=4) 21 | 22 | def test(test_loader, model, criterion, device): 23 | model.eval() 24 | test_loss = 0 25 | correct = 0 26 | 27 | with torch.no_grad(): 28 | for data, target in test_loader: 29 | data, target = data.to(device), target.to(device) 30 | output = model(data) 31 | test_loss += criterion(output, target).item() 32 | pred = output.argmax(dim=1, keepdim=True) 33 | correct += pred.eq(target.view_as(pred)).sum().item() 34 | test_loss /= len(test_loader.dataset) 35 | print('\nTest: Average loss: {:.4f}, Accuracy: {}/{} ({:.2f}%)\n'.format( 36 | test_loss, correct, len(test_loader.dataset), 37 | 100. * correct / len(test_loader.dataset))) 38 | 39 | if __name__ == '__main__': 40 | device = torch.device("cuda" if torch.cuda.is_available() else "cpu") 41 | model = RESNET().to(device) 42 | criterion = nn.CrossEntropyLoss() 43 | PATH='./check_point/best_point.pth' 44 | if os.path.exists(PATH): 45 | checkpoint = torch.load(PATH) 46 | model.load_state_dict(checkpoint['model']) 47 | # model=torch.load(PATH) 48 | test(test_loader, model, criterion, device) 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | -------------------------------------------------------------------------------- /智能系统实验/03.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Task: Dara Classification 3 | ''' 4 | 5 | import numpy as np 6 | import torch 7 | import torch.nn as nn 8 | import matplotlib.pyplot as plt 9 | 10 | 11 | """ 12 | Stage1: 构建数据集 13 | """ 14 | data = torch.ones(100, 2) 15 | x0 = torch.normal(2*data, 1) 16 | x1 = torch.normal(-2*data, 1) 17 | x = torch.cat((x0,x1),0).type(torch.FloatTensor).cuda() 18 | y0 = torch.zeros(100) 19 | y1 = torch.ones(100) 20 | y = torch.cat((y0,y1)).type(torch.LongTensor).cuda() 21 | 22 | """ 23 | Stage2:定义网络 24 | """ 25 | class Net(nn.Module): 26 | def __init__(self): 27 | super(Net,self).__init__() 28 | self.classify = nn.Sequential( 29 | nn.Linear(2,15), 30 | nn.ReLU(), 31 | nn.Linear(15,2), 32 | nn.Softmax(dim=1) 33 | ) 34 | def forward(self, x): 35 | classification = self.classify(x) 36 | return classification 37 | 38 | """ 39 | Stage3:训练模型 40 | """ 41 | net = Net().cuda() 42 | optimizer = torch.optim.SGD(net.parameters(), lr=0.03) 43 | loss_func = nn.CrossEntropyLoss() 44 | 45 | plt.figure(figsize=(8,6),dpi=80) 46 | plt.ion() 47 | for epoch in range(100): 48 | out = net(x) 49 | loss = loss_func(out, y) 50 | optimizer.zero_grad() 51 | loss.backward() 52 | optimizer.step() 53 | 54 | classification = torch.max(out,1)[1] 55 | class_y = classification.cpu().data.numpy() 56 | target_y = y.cpu().data.numpy() 57 | accuracy = sum(class_y==target_y)/200 58 | 59 | plt.cla() 60 | print(f'Epoch={epoch+1}, Accuracy={accuracy}') 61 | plt.scatter(x.cpu().data.numpy()[:,0],x.cpu().data.numpy()[:,1],c=class_y,s=100,cmap='RdYlGn') 62 | plt.text(-0.5,-4.5,f'Epoch={epoch+1}, Accuracy={accuracy}',fontdict={'size':18,'color':'red'}) 63 | plt.pause(0.01) 64 | 65 | if epoch == 99: 66 | plt.savefig('Classification.png') 67 | 68 | plt.ioff() 69 | plt.show() 70 | 71 | 72 | """ 73 | Stage4:测试模型 74 | """ 75 | sample = torch.tensor([[-4,0],[-3,-0.5],[1.5,1.5],[3,1]]).cuda() 76 | print("Sample:", sample) 77 | result = net(sample) 78 | print("Classification result:", torch.max(result,1)[1].data) 79 | -------------------------------------------------------------------------------- /模式识别/fcm/fcm_self.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Tue Nov 3 19:28:19 2020 4 | 5 | @author: tremble 6 | """ 7 | 8 | import pandas as pd 9 | import numpy as np 10 | from sklearn.datasets import load_iris 11 | import matplotlib.pyplot as plt 12 | import random 13 | 14 | 15 | def eucliDist(A,B): 16 | return np.sqrt(np.sum(np.square(A - B))) 17 | 18 | class fcm(object): 19 | def __init__(self,data,c,alpha): 20 | self.data = data 21 | self.c= c 22 | row, col = data.shape 23 | u=np.zeros((row,c)) 24 | for i in range(row): 25 | for j in range(c): 26 | u[i,j]=1/c 27 | self.row = row 28 | self.col = col 29 | self.u = u 30 | self.alpha=alpha 31 | 32 | def cal_cen(self,j): 33 | m=0 34 | n=0 35 | for i in range(self.row): 36 | m=m+ np.power(self.u[i,j],self.alpha) * data[i,0:4] 37 | n=n+ np.power(self.u[i,j],self.alpha) 38 | d=m/n 39 | return d 40 | 41 | 42 | def cal_cost(self): 43 | 44 | Ju=0 45 | for j in range(c): 46 | for i in range(self.row): 47 | Ju=Ju+np.power(self.u[i,j],self.alpha)*np.sum(np.square(data[i,0:4]-self.cal_cen(j))) 48 | return Ju 49 | 50 | 51 | def cal_u(self,i,j,k): 52 | s=0 53 | d_ij=data[i:0:4]-self.cal_cen(j) 54 | d_ik=data[i:0:4]-self.cal_cen(k) 55 | for k in range(c): 56 | s=s+np.power(d_ij/d_ik,2/(alpha-1)) 57 | s=1/s 58 | 59 | 60 | 61 | 62 | 63 | 64 | if __name__ == '__main__': 65 | dat=load_iris() 66 | data = np.array(dat.data[:,0:4]) 67 | print(data) 68 | c=3 69 | alpha = 2 70 | center=np.zeros(c) 71 | a=fcm(data,c,2) 72 | activator = True 73 | U=np.zeros((data.shape[0],c)) 74 | while(activator): 75 | for j in range(c): 76 | center[j] = a.cal_cen(j) 77 | J = a.cal_cost() 78 | if J < 0.001: 79 | break 80 | for i in range(data.shape[0]): 81 | for j in range(c): 82 | U[i,j] = a.cal_u(i, j) 83 | 84 | 85 | print(data) 86 | -------------------------------------------------------------------------------- /智能系统实验/02.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Task: Dara Regression 3 | ''' 4 | 5 | import numpy as np 6 | import torch 7 | import math 8 | import torch.nn as nn 9 | import matplotlib.pyplot as plt 10 | 11 | 12 | """ 13 | Stage1: 构建数据集 14 | """ 15 | # 构造等差数列并转为二维数组 16 | x = torch.unsqueeze(torch.linspace(-np.pi,np.pi,100),dim=1) 17 | # 添加随机数 18 | y = torch.sin(x)+0.5*torch.rand(x.size()) 19 | x = x.clone().detach().cuda() 20 | y = y.clone().detach().cuda() 21 | 22 | """ 23 | Stage2:定义网络 24 | """ 25 | class Net(nn.Module): 26 | def __init__(self): 27 | super(Net,self).__init__() 28 | self.predict = nn.Sequential( 29 | nn.Linear(1,10), 30 | nn.ReLU(), 31 | nn.Linear(10,1) 32 | ) 33 | def forward(self, x): 34 | prediciton = self.predict(x) 35 | return prediciton 36 | 37 | """ 38 | Stage3:训练模型 39 | """ 40 | epochNum = 1000 41 | net = Net().cuda() 42 | optimizer = torch.optim.SGD(net.parameters(), lr=0.062) 43 | loss_func = nn.MSELoss() 44 | 45 | plt.figure(figsize=(8,6),dpi=80) 46 | plt.ion() 47 | for epoch in range(epochNum): 48 | out = net(x) 49 | loss = loss_func(out, y) 50 | optimizer.zero_grad() 51 | loss.backward() 52 | optimizer.step() 53 | if epoch % 100 == 0: 54 | plt.cla() 55 | plt.scatter(x.cpu().detach().numpy(), y.cpu().detach().numpy()) 56 | plt.plot(x.cpu().detach().numpy(), out.cpu().detach().numpy()) 57 | plt.pause(1) 58 | print(f"epoch:{epoch},loss:{loss}") 59 | if epoch == epochNum-1: 60 | plt.clf() 61 | plt.scatter(x.cpu().detach().numpy(), y.cpu().detach().numpy()) 62 | plt.plot(x.cpu().detach().numpy(), out.cpu().detach().numpy()) 63 | plt.savefig('RegressionResult.png') 64 | plt.ioff() 65 | plt.show() 66 | 67 | """ 68 | Stage4:测试模型 69 | """ 70 | sample = torch.unsqueeze(torch.tensor([-math.pi,-math.pi/2,0,math.pi/2,math.pi]),dim=1) 71 | sample = sample.clone().detach().cuda() 72 | label = torch.sin(sample) 73 | label = label.clone().detach().cuda() 74 | print("Input is:", sample) 75 | result = net(sample) 76 | print("Output is:", result.cpu().detach().numpy) 77 | Loss = loss_func(result, label) 78 | print("Loss is:", Loss) 79 | 80 | -------------------------------------------------------------------------------- /图像超分辨项目/test.py: -------------------------------------------------------------------------------- 1 | from utils import * 2 | from torch import nn 3 | from models import SRResNet 4 | import time 5 | from PIL import Image 6 | 7 | # 测试图像 8 | imgPath = './data/Set14/comic.bmp' 9 | 10 | # 模型参数 11 | large_kernel_size = 9 # 第一层卷积和最后一层卷积的核大小 12 | small_kernel_size = 3 # 中间层卷积的核大小 13 | n_channels = 64 # 中间层通道数 14 | n_blocks = 16 # 残差模块数量 15 | scaling_factor = 4 # 放大比例 16 | device = torch.device("cuda" if torch.cuda.is_available() else "cpu") 17 | 18 | 19 | if __name__ == '__main__': 20 | 21 | # 预训练模型 22 | srresnet_checkpoint = "./checkpoint_srresnet.pth" 23 | 24 | checkpoint = torch.load(srresnet_checkpoint, map_location=device) 25 | srresnet = SRResNet(large_kernel_size=large_kernel_size, 26 | small_kernel_size=small_kernel_size, 27 | n_channels=n_channels, 28 | n_blocks=n_blocks, 29 | scaling_factor=scaling_factor) 30 | srresnet = srresnet.to(device) 31 | srresnet.load_state_dict(checkpoint['model']) 32 | 33 | srresnet.eval() 34 | 35 | 36 | # 加载图像 37 | img = Image.open(imgPath, mode='r') 38 | img = img.convert('RGB') 39 | 40 | lr_img = img.resize((int(img.width / scaling_factor), 41 | int(img.height / scaling_factor)), 42 | Image.BICUBIC) 43 | 44 | lr_img.save('./results/lr.jpg') 45 | 46 | # 双线性上采样 47 | Bicubic_img = lr_img.resize((int(lr_img.width * scaling_factor), int(lr_img.height * scaling_factor)), Image.BICUBIC) 48 | Bicubic_img.save('./results/test_bicubic.jpg') 49 | 50 | # 图像预处理 51 | lr_img = convert_image(lr_img, source='pil', target='imagenet-norm') 52 | lr_img.unsqueeze_(0) 53 | 54 | # 记录时间 55 | start = time.time() 56 | 57 | # 转移数据至设备 58 | lr_img = lr_img.to(device) # (1, 3, w, h ), imagenet-normed 59 | 60 | # 模型推理 61 | with torch.no_grad(): 62 | sr_img_res = srresnet(lr_img).squeeze(0).cpu().detach() # (1, 3, w*scale, h*scale), in [-1, 1] 63 | sr_img_res = convert_image(sr_img_res, source='[-1, 1]', target='pil') 64 | sr_img_res.save('./results/test_srres.jpg') 65 | 66 | print('用时 {:.3f} 秒'.format(time.time()-start)) 67 | 68 | -------------------------------------------------------------------------------- /计算智能导论/perceptron.py: -------------------------------------------------------------------------------- 1 | import pandas as pd 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | import torch 5 | 6 | class Perceptron(object): 7 | def __init__(self, input_num, activator_fun): 8 | self.activator = activator_fun 9 | self.weights = [0.0] * input_num 10 | self.bias = 0.0 11 | print("initial weight:{0}, bias:{1}".format(self.weights, self.bias)) 12 | 13 | def __str__(self): 14 | return 'weights: {0} ' \ 15 | 'bias: {1}\n'.format(self.weights, self.bias) 16 | 17 | def predict(self, input_vec): 18 | zipped = list(zip(input_vec, self.weights)) 19 | sum_total = sum(list(map(lambda x_y: x_y[0] * x_y[1], zipped))) 20 | return self.activator(sum_total + self.bias) 21 | 22 | def train(self, input_vecs, labels, iteration, rate): 23 | for i in range(iteration): 24 | self._one_iteration(input_vecs, labels, rate) 25 | 26 | def _one_iteration(self, input_vecs, labels, rate): 27 | samples = list(zip(input_vecs, labels)) 28 | for (input_vec, label) in samples: 29 | output = self.predict(input_vec) 30 | self._update_weights(input_vec, output, label, rate) 31 | 32 | def _update_weights(self, input_vec, output, label, rate): 33 | delta = label - output 34 | self.weights = list(map( 35 | lambda x_w: rate * delta * x_w[0] + x_w[1], 36 | zip(input_vec, self.weights))) 37 | self.bias += rate * delta 38 | 39 | def f_active_function(x): 40 | return 1 if x > 0 else 0 41 | 42 | 43 | def get_training_dataset(): 44 | data = torch.ones(100, 2) 45 | x0 = torch.normal(2 * data, 1) 46 | x1 = torch.normal(-2 * data, 1) 47 | x = torch.cat((x0, x1), 0).type(torch.FloatTensor) 48 | y0 = torch.zeros(100) 49 | y1 = torch.ones(100) 50 | y = torch.cat((y0, y1)).type(torch.LongTensor) 51 | x_ls, y_ls = [], [] 52 | for i in range(100): 53 | x_ls.append(x[i]) 54 | y_ls.append(y[i]) 55 | return x_ls,y_ls 56 | 57 | 58 | 59 | 60 | if __name__ == '__main__': 61 | 62 | data, target = get_training_dataset() 63 | p = Perceptron(len(data), f_active_function) 64 | p.train(data, target, 1000, 0.1) 65 | print(p) 66 | acc=0 67 | for i in range(len(data)): 68 | result=p.predict(data[i]) 69 | print(result) 70 | acc+=(result==target[i]) 71 | print(acc/len(data)) 72 | 73 | 74 | -------------------------------------------------------------------------------- /计算智能导论/kmeans.py: -------------------------------------------------------------------------------- 1 | # -*- coding:utf-8 -*- 2 | # Author : JackZhu 3 | 4 | # Data : 2021/5/10 23:10 5 | import numpy as np 6 | import pandas as pd 7 | import sklearn 8 | import random 9 | from sklearn import datasets 10 | from sklearn.preprocessing import scale 11 | from sklearn.manifold import TSNE 12 | from sklearn.decomposition import PCA 13 | import matplotlib.pyplot as plt 14 | from sklearn.metrics import silhouette_score 15 | 16 | 17 | class Kmeans(): 18 | def __init__(self,dat,k): 19 | data=scale(dat) 20 | self.data=data 21 | self.row, self.col = data.shape 22 | self.k=k 23 | self.centers=np.ndarray((k,self.col)) 24 | choices=random.choices(range(self.row),k=k) 25 | for i in range(k): 26 | self.centers[i,:]=self.data[choices[i],:] 27 | def fit(self): 28 | count=0 29 | while(count<15): 30 | self.labels=np.zeros((self.row)) 31 | for i in range(self.data.shape[0]): 32 | dis=[] 33 | for j in range(self.k): 34 | dis.append(np.linalg.norm(self.data[i,:]-self.centers[j,:],axis=0)) 35 | lab=np.argmin(dis,axis=0) 36 | self.labels[i]=lab 37 | self.result={} 38 | for i in range(self.k): 39 | type=np.where(self.labels==i)[0] 40 | self.result[i]=type 41 | if len(type)==0: 42 | self.centers[i, :] =0 43 | else: 44 | self.centers[i,:]=np.mean(self.data[type,:],axis=0) 45 | count+=1 46 | return self.centers, self.result,self.labels 47 | 48 | def imshow(self): 49 | tsne = TSNE(n_components=2, learning_rate=100).fit_transform(self.data) 50 | pca = PCA().fit_transform(self.data) 51 | plt.figure(figsize=(12, 6)) 52 | plt.subplot(121) 53 | plt.scatter(tsne[:, 0], tsne[:, 1], c=self.labels) 54 | plt.title('t-SNE') 55 | plt.subplot(122) 56 | plt.scatter(pca[:, 0], pca[:, 1], c=self.labels) 57 | plt.title('PCA') 58 | plt.colorbar() 59 | plt.show() 60 | 61 | 62 | if __name__ == "__main__": 63 | iris=datasets.load_iris() 64 | data=iris.data 65 | target=iris.target 66 | kmeans=Kmeans(data,3) 67 | centers,results,labels=kmeans.fit() 68 | kmeans.imshow() 69 | s = silhouette_score(data, labels) 70 | print(centers) 71 | print(results) 72 | print(s) -------------------------------------------------------------------------------- /计算智能导论/神经网络二分类.py: -------------------------------------------------------------------------------- 1 | # -*- coding:utf-8 -*- 2 | # Author : JackZhu 3 | 4 | # Data : 2021/5/13 15:25 5 | import matplotlib.pyplot as plt 6 | import torch 7 | import torch.nn as nn 8 | import pandas as pd 9 | import numpy as np 10 | 11 | 12 | class Classifier(nn.Module): 13 | def __init__(self,in_channel,out_channel): 14 | super().__init__() 15 | self.model = nn.Sequential( 16 | nn.Linear(in_channel, out_channel), 17 | ) 18 | self.optimizer = torch.optim.SGD(self.parameters(),lr=0.01) 19 | self.criterion = nn.MSELoss() 20 | self.counter = 0 21 | self.progress = [] 22 | 23 | def forward(self, inputs): 24 | y=self.model(inputs) 25 | y= y.squeeze(-1) 26 | return y 27 | 28 | def train_model(self, inputs, targets,device): 29 | inputs,targets=inputs.to(device),targets.to(device) 30 | outputs = self.forward(inputs) 31 | loss = self.criterion(outputs, targets) 32 | self.optimizer.zero_grad() 33 | loss.backward() 34 | self.optimizer.step() 35 | self.counter += 1 36 | if (self.counter % 10 == 0): 37 | self.progress.append(loss.item()) 38 | if (self.counter % 10000 == 0): 39 | print('counter=', self.counter) 40 | 41 | def plot_progress(self): 42 | df = pd.DataFrame(self.progress, columns=['loss']) 43 | print(df) 44 | df.plot(figsize=(16, 8), grid=True) 45 | 46 | 47 | 48 | def get_training_dataset(path): 49 | file=pd.read_csv(path,header=None) 50 | data = file.iloc[:, :-1] 51 | target = file.iloc[:, -1] 52 | target = pd.get_dummies(target).iloc[:, -1] 53 | data = np.array(data) 54 | target = np.array(target) 55 | data=torch.from_numpy(data).to(torch.float32) 56 | target=torch.from_numpy(target).to(torch.float32) 57 | 58 | return data,target 59 | 60 | if __name__ == '__main__': 61 | path = './sonar.csv' 62 | data,target=get_training_dataset(path) 63 | device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') 64 | perceptron=Classifier(data.shape[1],1).to(device) 65 | for epoch in range(100): 66 | for i in range(data.shape[0]): 67 | perceptron.train_model(data[i],target[i],device) 68 | perceptron.plot_progress() 69 | acc=0 70 | for i in range(data.shape[0]): 71 | input=data[i].to(device) 72 | result=perceptron.forward(input).cpu().detach() 73 | if result>0: 74 | answer=1 75 | else: 76 | answer=0 77 | acc+=(target[i].numpy()==answer) 78 | print(acc/len(target)) 79 | -------------------------------------------------------------------------------- /模式识别/fcm/fcm.py: -------------------------------------------------------------------------------- 1 | 2 | import numpy as np 3 | import random 4 | import copy 5 | 6 | 7 | class FuzzyCMeans(object): 8 | 9 | def __init__(self, cluster_number, m): 10 | self.cluster = cluster_number 11 | self.center = None 12 | self.u = None 13 | self.m = m 14 | 15 | @staticmethod 16 | def distance(center, train): 17 | result = np.linalg.norm(train - center, axis=1) 18 | return result 19 | 20 | def fit(self, x, y=None): 21 | u = np.random.randint(1, 10000, (x.shape[0], self.cluster)) 22 | u = u / u.sum(1)[:, np.newaxis] 23 | for _ in range(5): 24 | # 创建它的副本,以检查结束条件 25 | u_old = copy.deepcopy(u) 26 | # 计算聚类中心 27 | distance_matrix = np.zeros((self.cluster, x.shape[0])) 28 | for j in range(0, self.cluster): 29 | current_cluster_center = np.zeros(x.shape[1]) 30 | for i in range(0, x.shape[1]): 31 | dummy_sum_num = ((u[:, j] ** self.m) * x[:, i]).sum(0) 32 | dummy_sum_dum = (u[:, j] ** self.m).sum() 33 | # 第i列的聚类中心 34 | current_cluster_center[i] = dummy_sum_num / dummy_sum_dum 35 | # 第j簇的所有聚类中心 36 | distance_matrix[j] = self.distance(x, current_cluster_center) 37 | distance_matrix = distance_matrix.T 38 | # 更新U 39 | for j in range(0, self.cluster): 40 | for i in range(0, len(x)): 41 | dummy = 0.0 42 | for k in range(0, self.cluster): 43 | # 分母 44 | dummy += (distance_matrix[i][j] / distance_matrix[i][k]) ** (2 / (self.m - 1)) 45 | u[i][j] = 1 / dummy 46 | if np.linalg.norm(u - u_old) < 1e-7: 47 | break 48 | for i in range(0, len(u)): 49 | maximum = max(u[i]) 50 | for j in range(0, len(u[0])): 51 | if u[i][j] != maximum: 52 | u[i][j] = 0 53 | else: 54 | u[i][j] = 1 55 | 56 | self.u = u 57 | return self 58 | 59 | def transform(self, x=None): 60 | indices = self.u.argmax(1) 61 | out = [x[indices == i] for i in range(indices.max()+1)] 62 | return out 63 | 64 | def show(self, x=None): 65 | indices = self.u.argmax(1) 66 | for i in range(indices.max() + 1): 67 | x[indices == i] = [50*i, 50*i, 50*i] 68 | return x 69 | 70 | 71 | if __name__ == '__main__': 72 | FuzzyCMeans(3, 2).fit(np.random.randn(200, 3)) 73 | -------------------------------------------------------------------------------- /计算智能导论/sonar_kmeans.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Sun May 30 15:28:26 2021 4 | 5 | @author: tremble 6 | """ 7 | 8 | # -*- coding:utf-8 -*- 9 | # Author : JackZhu 10 | 11 | # Data : 2021/5/10 23:10 12 | import numpy as np 13 | import pandas as pd 14 | import sklearn 15 | import random 16 | from sklearn import datasets 17 | from sklearn.preprocessing import scale 18 | from sklearn.manifold import TSNE 19 | from sklearn.decomposition import PCA 20 | import matplotlib.pyplot as plt 21 | from sklearn.metrics import silhouette_score 22 | 23 | 24 | class Kmeans(): 25 | def __init__(self,dat,k): 26 | data=scale(dat) 27 | self.data=data 28 | self.row, self.col = data.shape 29 | self.k=k 30 | self.centers=np.ndarray((k,self.col)) 31 | choices=random.choices(range(self.row),k=k) 32 | for i in range(k): 33 | self.centers[i,:]=self.data[choices[i],:] 34 | def fit(self): 35 | count=0 36 | while(count<40): 37 | self.labels=np.zeros((self.row)) 38 | for i in range(self.data.shape[0]): 39 | dis=[] 40 | for j in range(self.k): 41 | dis.append(np.linalg.norm(self.data[i,:]-self.centers[j,:],axis=0)) 42 | lab=np.argmin(dis,axis=0) 43 | self.labels[i]=lab 44 | self.result={} 45 | for i in range(self.k): 46 | type=np.where(self.labels==i)[0] 47 | self.result[i]=type 48 | if len(type)==0: 49 | self.centers[i, :] =0 50 | else: 51 | self.centers[i,:]=np.mean(self.data[type,:],axis=0) 52 | count+=1 53 | return self.centers, self.result,self.labels 54 | 55 | def imshow(self): 56 | tsne = TSNE(n_components=2, learning_rate=100).fit_transform(self.data) 57 | pca = PCA().fit_transform(self.data) 58 | plt.figure(figsize=(12, 6)) 59 | plt.subplot(121) 60 | plt.scatter(tsne[:, 0], tsne[:, 1], c=self.labels) 61 | plt.title('t-SNE') 62 | plt.subplot(122) 63 | plt.scatter(pca[:, 0], pca[:, 1], c=self.labels) 64 | plt.title('PCA') 65 | plt.colorbar() 66 | plt.show() 67 | 68 | 69 | if __name__ == "__main__": 70 | path = './sonar.csv' 71 | file = pd.read_csv(path, header=None) 72 | data = file.iloc[:, :-1] 73 | target = file.iloc[:, -1] 74 | target = pd.get_dummies(target).iloc[:, -1] 75 | data = np.array(data) 76 | target = np.array(target) 77 | kmeans=Kmeans(data,2) 78 | centers,results,labels=kmeans.fit() 79 | kmeans.imshow() 80 | s = silhouette_score(data, labels) 81 | print(centers) 82 | print(results) 83 | print(s) -------------------------------------------------------------------------------- /智能系统实验/异或.py: -------------------------------------------------------------------------------- 1 | import os 2 | import torch 3 | import numpy as np 4 | import torch.nn as nn 5 | 6 | 7 | class FCN(nn.Module): 8 | def __init__(self, input_size, hidden_size, output_size): 9 | super(FCN, self).__init__() 10 | 11 | self.model = nn.Sequential( 12 | nn.Linear(input_size, hidden_size), 13 | nn.ReLU(), 14 | nn.Linear(hidden_size, output_size), 15 | nn.Sigmoid(), 16 | ) 17 | 18 | def forward(self, x): 19 | x = self.model(x) 20 | return x 21 | 22 | 23 | def train(): 24 | # Device configuration 25 | device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") 26 | 27 | # Model 28 | print("Building model...") 29 | initial_epoch = 0 30 | model = FCN(input_size, hidden_size, output_size) 31 | model.to(device) 32 | 33 | print("Loading data...") 34 | x = [[0,0],[0,1],[1,0],[1,1]] 35 | x_tensor = torch.tensor(x).float().cuda() 36 | y = [[0],[1],[1],[0]] 37 | y_tensor = torch.tensor(y).float().cuda() 38 | 39 | loss_function = nn.MSELoss(reduce=True, size_average=True) 40 | optimizer = torch.optim.SGD(model.parameters(), lr=0.05) 41 | 42 | print("Beginning to train...") 43 | min_loss = float('inf') 44 | for epoch in range(5000): 45 | out = model(x_tensor) 46 | loss = loss_function(out,y_tensor) 47 | optimizer.zero_grad() 48 | loss.backward() 49 | optimizer.step() 50 | if epoch % 1000 == 0: 51 | print('Epoch num:', epoch) 52 | print('Loss:', loss) 53 | if loss < min_loss: 54 | min_loss = loss 55 | torch.save({'epoch_record': epoch, 'model': model.state_dict()}, 56 | './base_model_path') 57 | 58 | 59 | def test(): 60 | # Device configuration 61 | device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") 62 | # Model 63 | print("Building model...") 64 | initial_epoch = 0 65 | model = FCN(input_size, hidden_size, output_size) 66 | if os.path.exists('./base_model_path'): 67 | load_dict = torch.load('./base_model_path') 68 | initial_epoch = load_dict['epoch_record'] 69 | model.load_state_dict(load_dict['model']) 70 | model.to(device) 71 | # input 72 | x = [[1, 0]] 73 | x_tensor = torch.tensor(x).float().cuda() 74 | # output 75 | print("Begin to test...") 76 | outFinal = model(x_tensor).detach().cpu().numpy() 77 | if outFinal > 0.5: 78 | res = 1 79 | else: 80 | res = 0 81 | print('Input:', x) 82 | print('Answer:', res) 83 | 84 | 85 | if __name__ == '__main__': 86 | input_size = 2 87 | hidden_size = 20 88 | output_size = 1 89 | #train() 90 | test() 91 | 92 | -------------------------------------------------------------------------------- /数据挖掘/test.py: -------------------------------------------------------------------------------- 1 | import numpy 2 | import pandas as pd 3 | import numpy as np 4 | from scipy.io import loadmat 5 | import matplotlib.pyplot as plt 6 | from sklearn.metrics.pairwise import euclidean_distances 7 | 8 | 9 | def split(data, MinPts, eps): 10 | # 划分属性 11 | ptses = [] 12 | dist = euclidean_distances(data) 13 | for row in dist: 14 | # 密度,空间中任意一点的密度是以该点为圆心、以 Eps 为半径的圆区域内包含的点数 15 | density = numpy.sum(row < eps) 16 | if density > MinPts: 17 | # 核心点(Core Points) 18 | pts = 1 19 | elif density > 1: 20 | # 边界点(Border Points) 21 | pts = 2 22 | else: 23 | # 噪声点(Noise Points) 24 | pts = 0 25 | ptses.append(pts) 26 | # 过滤噪声点,因为其无法聚类,自成一类 27 | corePoints = data[pd.Series(ptses) != 0] 28 | coreDist = euclidean_distances(corePoints) 29 | return coreDist 30 | 31 | 32 | def DBSCAN(dataset, points, radius): 33 | # Step1. 把每个点的领域都作为一类 34 | cluster = dict() 35 | i = 0 36 | coreDistance = split(dataset, points, radius) 37 | for row in coreDistance: 38 | cluster[i] = numpy.where(row < radius)[0] 39 | i = i + 1 40 | # Step2. 将有交集的领域,都合并为新的领域 41 | for i in range(len(cluster)): 42 | for j in range(len(cluster)): 43 | if len(set(cluster[j]) & set(cluster[i])) > 0 and i != j: 44 | cluster[i] = list(set(cluster[i]) | set(cluster[j])) 45 | cluster[j] = list() 46 | # Step3. 找出独立(也就是没有交集)的领域,即聚类的结果 47 | result = dict() 48 | j = 0 49 | for i in range(len(cluster)): 50 | if len(cluster[i]) > 0: 51 | result[j] = cluster[i] 52 | j = j + 1 53 | return result 54 | 55 | 56 | if __name__ == '__main__': 57 | # 导入数据 58 | data_ori = loadmat("./data-密度聚类/long.mat") 59 | data_info = np.array(data_ori['long1']) 60 | data = pd.DataFrame(data=data_info[:, :-1], columns=['x', 'y']) 61 | plt.scatter(data['x'],data['y']) 62 | # plt.show() 63 | 64 | eps = [0.01, 0.1, 1, 100] 65 | MinPts = [2, 3, 4, 5] 66 | 67 | # for i in range(len(eps)): 68 | # for j in range(len(MinPts)): 69 | # ans = DBSCAN(data,MinPts[j],eps[i]) 70 | # # 找出每个点所在领域的序号,作为他们最后聚类的结果标记 71 | # for k in range(len(ans)): 72 | # for p in ans[k]: 73 | # data.at[p, 'type'] = k 74 | # # 画出聚类结果 75 | # plt.scatter(data['x'],data['y'],c=data['type']) 76 | # plt.show() 77 | ans = DBSCAN(data, 5, 0.2) 78 | # 找出每个点所在领域的序号,作为他们最后聚类的结果标记 79 | print(ans.keys()) 80 | for k in range(len(ans)): 81 | 82 | for p in ans.keys(): 83 | data.at[p, 'type'] = k 84 | # 画出聚类结果 85 | plt.scatter(data['x'], data['y'], c=data['type']) 86 | plt.show() 87 | -------------------------------------------------------------------------------- /数据挖掘/apriori.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Sun May 16 11:03:01 2021 4 | 5 | @author: tremble 6 | """ 7 | 8 | from numpy import * 9 | 10 | # 构造数据 11 | def loadDataSet(): 12 | return [[1, 3, 4], [2, 3, 5], [1, 2, 3, 5], [2, 5]] 13 | 14 | # 将所有元素转换为frozenset型字典,存放到列表中 15 | def createC1(dataSet): 16 | C1 = [] 17 | for transaction in dataSet: 18 | for item in transaction: 19 | if not [item] in C1: 20 | C1.append([item]) 21 | C1.sort() 22 | # 使用frozenset是为了后面可以将这些值作为字典的键 23 | return list(map(frozenset, C1)) # frozenset一种不可变的集合,set可变集合 24 | 25 | # 过滤掉不符合支持度的集合 26 | # 返回 频繁项集列表retList 所有元素的支持度字典 27 | def scanD(D, Ck, minSupport): 28 | ssCnt = {} 29 | for tid in D: 30 | for can in Ck: 31 | if can.issubset(tid): # 判断can是否是tid的《子集》 (这里使用子集的方式来判断两者的关系) 32 | if can not in ssCnt: # 统计该值在整个记录中满足子集的次数(以字典的形式记录,frozenset为键) 33 | ssCnt[can] = 1 34 | else: 35 | ssCnt[can] += 1 36 | numItems = float(len(D)) 37 | retList = [] # 重新记录满足条件的数据值(即支持度大于阈值的数据) 38 | supportData = {} # 每个数据值的支持度 39 | for key in ssCnt: 40 | support = ssCnt[key] / numItems 41 | if support >= minSupport: 42 | retList.insert(0, key) 43 | supportData[key] = support 44 | return retList, supportData # 排除不符合支持度元素后的元素 每个元素支持度 45 | 46 | # 生成所有可以组合的集合 47 | # 频繁项集列表Lk 项集元素个数k [frozenset({2, 3}), frozenset({3, 5})] -> [frozenset({2, 3, 5})] 48 | def aprioriGen(Lk, k): 49 | retList = [] 50 | lenLk = len(Lk) 51 | for i in range(lenLk): # 两层循环比较Lk中的每个元素与其它元素 52 | for j in range(i+1, lenLk): 53 | L1 = list(Lk[i])[:k-2] # 将集合转为list后取值 54 | L2 = list(Lk[j])[:k-2] 55 | L1.sort(); L2.sort() # 这里说明一下:该函数每次比较两个list的前k-2个元素,如果相同则求并集得到k个元素的集合 56 | if L1==L2: 57 | retList.append(Lk[i] | Lk[j]) # 求并集 58 | return retList # 返回频繁项集列表Ck 59 | 60 | # 封装所有步骤的函数 61 | # 返回 所有满足大于阈值的组合 集合支持度列表 62 | def apriori(dataSet, minSupport = 0.5): 63 | D = list(map(set, dataSet)) # 转换列表记录为字典 [{1, 3, 4}, {2, 3, 5}, {1, 2, 3, 5}, {2, 5}] 64 | C1 = createC1(dataSet) # 将每个元素转会为frozenset字典 [frozenset({1}), frozenset({2}), frozenset({3}), frozenset({4}), frozenset({5})] 65 | L1, supportData = scanD(D, C1, minSupport) # 过滤数据 66 | L = [L1] 67 | k = 2 68 | while (len(L[k-2]) > 0): # 若仍有满足支持度的集合则继续做关联分析 69 | Ck = aprioriGen(L[k-2], k) # Ck候选频繁项集 70 | Lk, supK = scanD(D, Ck, minSupport) # Lk频繁项集 71 | supportData.update(supK) # 更新字典(把新出现的集合:支持度加入到supportData中) 72 | L.append(Lk) 73 | k += 1 # 每次新组合的元素都只增加了一个,所以k也+1(k表示元素个数) 74 | return L, supportData 75 | 76 | dataSet = loadDataSet() 77 | L,suppData = apriori(dataSet) 78 | print(L) 79 | print(suppData) -------------------------------------------------------------------------------- /深度学习/OCR_ALL/process.py: -------------------------------------------------------------------------------- 1 | import os 2 | import numpy as np 3 | import cv2 4 | from matplotlib import pyplot as plt 5 | 6 | 7 | # 解决读取路径为中文的问题 8 | def cv_imread(file_path): 9 | cv_img = cv2.imdecode(np.fromfile(file_path, dtype=np.uint8), 0) 10 | return cv_img 11 | 12 | # 解决保存路径有中文的问题 13 | def cv_imwrite(savePath,tem): 14 | cv2.imencode('.png',tem)[1].tofile(savePath) 15 | 16 | 17 | # 得到训练所用的标签 18 | def get_labels(basic_path): 19 | # 数据集所在路径 20 | label_path=os.path.join(basic_path,'test\\') 21 | 22 | # 获取所有文件名,即为全部500个汉字,即为汉字标签 23 | char_labels=os.listdir(label_path) 24 | 25 | # 创建一个单位矩阵,每一列为一个one-hot向量,作为训练标签 26 | labels=np.identity(len(char_labels)) 27 | return labels,char_labels 28 | 29 | 30 | # 图像预处理 31 | def pre_process(basic_path,*args): 32 | ''' 33 | 图像预处理,将图像去除背景以及噪声点,然后二值化, 34 | basic_path示例为:''E:\OCR\char'' 35 | *args中传入'train'或者'test',用于选择处理对应文件夹下的图像 36 | ''' 37 | # 训练集或者测试集的路径 38 | train_path=os.path.join(basic_path,args[0]) 39 | # 获取当前路径下所有汉字,存到chars中 40 | chars=os.listdir(train_path) 41 | for char in chars: 42 | print(char+'正在处理中。。。。。') 43 | # 创建文件夹 44 | os.makedirs('.\\test_process'+'\\'+args[0]+'\\'+char) 45 | # 每一个汉字文件夹的路径 46 | pic_path=os.path.join(train_path,char) 47 | # 获取当前汉字文件夹下所有图片的名字 48 | pic_names=os.listdir(pic_path) 49 | for name in pic_names: 50 | # 带有背景的图片 51 | if name[0]=='b': 52 | img=cv_imread(os.path.join(pic_path,name)) 53 | for i in range(img.shape[0]): 54 | for j in range(img.shape[1]): 55 | if img[i,j]>146: 56 | img[i,j]=255 57 | else: 58 | img[i,j]=0 59 | # 带有噪声点的图片 60 | elif name[0]=='g': 61 | img=cv_imread(os.path.join(pic_path,name)) 62 | for i in range(img.shape[0]): 63 | for j in range(img.shape[1]): 64 | if img[i,j]<20 or img[i,j]>250: 65 | img[i,j]=255 66 | else: 67 | img[i,j]=0 68 | # 普通图片 69 | else: 70 | img=cv_imread(os.path.join(pic_path,name)) 71 | for i in range(img.shape[0]): 72 | for j in range(img.shape[1]): 73 | if img[i,j]<250: 74 | img[i,j]=0 75 | else: 76 | img[i,j]=255 77 | img=cv2.resize(img,(128,128),interpolation=cv2.INTER_CUBIC) 78 | # print(img.shape) 79 | cv_imwrite(os.path.join(r'.\test_process',args[0],char,name),img) 80 | 81 | 82 | 83 | if __name__=='__main__': 84 | pre_process(r'.\orig_dataset','train') 85 | 86 | 87 | -------------------------------------------------------------------------------- /模式识别/kmeans/kmeans_iris.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Mon Nov 9 21:38:07 2020 4 | 5 | @author: tremble 6 | """ 7 | 8 | import numpy as np 9 | import pandas as pd 10 | import matplotlib.pyplot as plt 11 | plt.rcParams["axes.unicode_minus"] = False 12 | Iris_data = np.array(pd.read_csv(r"H:\iris.csv").values[:,0:4], dtype=np.float32) 13 | 14 | def k_mean(iteration, Iris_data, *init): 15 | iter_ = 0 16 | n_data, width = Iris_data.shape 17 | mean = np.mean(Iris_data, axis=0) 18 | std = np.std(Iris_data, axis=0) 19 | copy = (Iris_data - mean) / std 20 | init_value = np.array(init , dtype=np.float32) 21 | init_value = (init_value - mean) / std 22 | k, _ = init_value.shape 23 | accuracy_list = [] 24 | while iter_ < iteration: 25 | label = np.array([5 for _ in range(n_data)]) 26 | ori_label = [] 27 | for i in range(3): 28 | ori_label.extend([i for _ in range(50)]) 29 | sample_belong_array = np.zeros((k, k), dtype=np.int32) 30 | for i in range(n_data): 31 | result = [] 32 | for j in range(k): 33 | result.append((np.linalg.norm(copy[i, :]-init_value[j, :]), j)) 34 | belong = min(result, key=lambda x: x[0])[1] 35 | label[i] = belong 36 | result = np.zeros((k,4)) 37 | for i in range(k): 38 | return_ = np.where(label == i) 39 | if len(return_) == 0: 40 | pass 41 | else: 42 | result[i] = np.mean(copy[return_],axis=0) 43 | init_value = result 44 | iter_ += 1 45 | for i in range(len(label)): 46 | sample_belong_array[ori_label[i], label[i]] += 1 47 | accuracy1 = np.trace(sample_belong_array) / n_data 48 | P = sample_belong_array / np.sum(sample_belong_array, axis=0) 49 | E = - np.sum(np.sum(P * np.log2(P+1e-8), axis=0) * np.sum(sample_belong_array, axis=0)/150) 50 | accuracy_list.append((accuracy1, E)) 51 | plt.figure() 52 | plt.plot([i+1 for i in range(iteration)], np.array(accuracy_list)[:, 0], label="accuracy") 53 | plt.plot([i+1 for i in range(iteration)], np.array(accuracy_list)[:, 1], label="entropy") 54 | plt.legend() 55 | 56 | def visual(): 57 | m = np.mean(Iris_data, axis=0) 58 | std = np.std(Iris_data, axis=0) 59 | Z = (Iris_data - m) / std 60 | value, vector = np.linalg.eig(np.dot(Z.T, Z)) 61 | index = np.argpartition(value, -2)[-2:] 62 | w = vector[:, index] 63 | Y = np.dot(Iris_data, w) 64 | plt.figure() 65 | plt.scatter(Y[:50, 0], Y[:50, 1], label="class_1",c='r') 66 | plt.scatter(Y[50:100, 0], Y[50:100, 1], label="class_2",c='y') 67 | plt.scatter(Y[100:150, 0], Y[100:150, 1], label="class_3",c='b') 68 | plt.legend() 69 | 70 | if __name__ == "__main__": 71 | k_mean(100, Iris_data, list(Iris_data[25]), list(Iris_data[75]), list(Iris_data[125])) 72 | visual() 73 | plt.show() 74 | -------------------------------------------------------------------------------- /图像超分辨项目/datasets.py: -------------------------------------------------------------------------------- 1 | import torch 2 | from torch.utils.data import Dataset 3 | import json 4 | import os 5 | from PIL import Image 6 | from utils import ImageTransforms 7 | 8 | 9 | class SRDataset(Dataset): 10 | """ 11 | 数据集加载器 12 | """ 13 | 14 | def __init__(self, data_folder, split, crop_size, scaling_factor, lr_img_type, hr_img_type, test_data_name=None): 15 | """ 16 | :参数 data_folder: # Json数据文件所在文件夹路径 17 | :参数 split: 'train' 或者 'test' 18 | :参数 crop_size: 高分辨率图像裁剪尺寸 (实际训练时不会用原图进行放大,而是截取原图的一个子块进行放大) 19 | :参数 scaling_factor: 放大比例 20 | :参数 lr_img_type: 低分辨率图像预处理方式 21 | :参数 hr_img_type: 高分辨率图像预处理方式 22 | :参数 test_data_name: 如果是评估阶段,则需要给出具体的待评估数据集名称,例如 "Set14" 23 | """ 24 | 25 | self.data_folder = data_folder 26 | self.split = split.lower() 27 | self.crop_size = int(crop_size) 28 | self.scaling_factor = int(scaling_factor) 29 | self.lr_img_type = lr_img_type 30 | self.hr_img_type = hr_img_type 31 | self.test_data_name = test_data_name 32 | 33 | assert self.split in {'train', 'test'} 34 | if self.split == 'test' and self.test_data_name is None: 35 | raise ValueError("请提供测试数据集名称!") 36 | assert lr_img_type in {'[0, 255]', '[0, 1]', '[-1, 1]', 'imagenet-norm'} 37 | assert hr_img_type in {'[0, 255]', '[0, 1]', '[-1, 1]', 'imagenet-norm'} 38 | 39 | # 如果是训练,则所有图像必须保持固定的分辨率以此保证能够整除放大比例 40 | # 如果是测试,则不需要对图像的长宽作限定 41 | if self.split == 'train': 42 | assert self.crop_size % self.scaling_factor == 0, "裁剪尺寸不能被放大比例整除!" 43 | 44 | # 读取图像路径 45 | if self.split == 'train': 46 | with open(os.path.join(data_folder, 'train_images.json'), 'r') as j: 47 | self.images = json.load(j) 48 | else: 49 | with open(os.path.join(data_folder, self.test_data_name + '_test_images.json'), 'r') as j: 50 | self.images = json.load(j) 51 | 52 | # 数据处理方式 53 | self.transform = ImageTransforms(split=self.split, 54 | crop_size=self.crop_size, 55 | scaling_factor=self.scaling_factor, 56 | lr_img_type=self.lr_img_type, 57 | hr_img_type=self.hr_img_type) 58 | 59 | def __getitem__(self, i): 60 | """ 61 | 为了使用PyTorch的DataLoader,必须提供该方法. 62 | 63 | :参数 i: 图像检索号 64 | :返回: 返回第i个低分辨率和高分辨率的图像对 65 | """ 66 | # 读取图像 67 | img = Image.open(self.images[i], mode='r') 68 | img = img.convert('RGB') 69 | if img.width <= 96 or img.height <= 96: 70 | print(self.images[i], img.width, img.height) 71 | lr_img, hr_img = self.transform(img) 72 | 73 | return lr_img, hr_img 74 | 75 | def __len__(self): 76 | """ 77 | 为了使用PyTorch的DataLoader,必须提供该方法. 78 | 79 | :返回: 加载的图像总数 80 | """ 81 | return len(self.images) 82 | -------------------------------------------------------------------------------- /计算智能导论/kmeans_photo.py: -------------------------------------------------------------------------------- 1 | # -*- coding:utf-8 -*- 2 | # Author : JackZhu 3 | 4 | # Data : 2021/5/13 12:56 5 | 6 | import numpy as np 7 | import pandas as pd 8 | import sklearn 9 | import random 10 | from sklearn import datasets 11 | from sklearn.preprocessing import scale 12 | from sklearn.manifold import TSNE 13 | from sklearn.decomposition import PCA 14 | import matplotlib.pyplot as plt 15 | from PIL import Image 16 | import cv2 17 | 18 | 19 | class Kmeans(): 20 | def __init__(self,dat,k): 21 | data=scale(dat) 22 | self.data=data 23 | self.row, self.col = data.shape 24 | self.k=k 25 | self.centers=np.ndarray((k,self.col)) 26 | choices=random.choices(range(self.row),k=k) 27 | for i in range(k): 28 | self.centers[i,:]=self.data[choices[i],:] 29 | def fit(self,counts=15): 30 | count=0 31 | while(count=y0: 76 | iris1_y0_target[i]=0 77 | else: 78 | iris1_y0_target[i]=1 79 | #判断是否正确 80 | if iris1_y0_target[i]==target1_test[i,:]: 81 | correct1=correct1+1 82 | else: 83 | continue 84 | 85 | iris2_y0_test[i]=np.dot(w,np.array(iris2_test[i,:])) 86 | if iris2_y0_test[i]= minPts: 40 | coreObjs[i] = neibor 41 | oldCoreObjs = coreObjs.copy() 42 | k = 0 # 初始化聚类簇数 43 | notAccess = list(range(n)) # 初始化未访问样本集合(索引) 44 | while len(coreObjs) > 0: 45 | OldNotAccess = [] 46 | OldNotAccess.extend(notAccess) 47 | cores = coreObjs.keys() 48 | # 随机选取一个核心对象 49 | randNum = random.randint(0, len(cores) - 1) 50 | cores = list(cores) 51 | core = cores[randNum] 52 | queue = [] 53 | queue.append(core) 54 | notAccess.remove(core) 55 | while len(queue) > 0: 56 | q = queue[0] 57 | del queue[0] 58 | if q in oldCoreObjs.keys(): 59 | delte = [val for val in oldCoreObjs[q] if val in notAccess] # Δ = N(q)∩Γ 60 | queue.extend(delte) # 将Δ中的样本加入队列Q 61 | notAccess = [val for val in notAccess if val not in delte] # Γ = Γ\Δ 62 | k += 1 63 | C[k] = [val for val in OldNotAccess if val not in notAccess] 64 | for x in C[k]: 65 | if x in coreObjs.keys(): 66 | del coreObjs[x] 67 | return C 68 | 69 | 70 | def draw(C, D): 71 | colors = list(mcolors.TABLEAU_COLORS.keys()) 72 | predict = np.zeros((D.shape[0], D.shape[1] + 1)) 73 | j = 0 74 | keys = C.keys() 75 | print(keys) 76 | for k in keys: 77 | for i in C[k]: 78 | predict[j, 0:2] = D[i] 79 | predict[j, 2] = k 80 | j = j + 1 81 | plt.scatter(D[i, 0], D[i, 1], color=colors[k + 1]) 82 | plt.show() 83 | return predict 84 | 85 | 86 | def main(): 87 | path = './data-密度聚类/square1.mat' 88 | data = scio.loadmat(path)['square1'] 89 | # plt.scatter(data[:,0],data[:,1]) 90 | # plt.show() 91 | D = data[:, 0:2] 92 | label = data[2] 93 | C = DBSCAN(D, 0.9, 15) 94 | predict = draw(C, D) 95 | s1 = metrics.silhouette_score(predict[:, 0:2], predict[:, 2], metric='euclidean') 96 | s2 = calinski_harabasz_score(predict[:, 0:2], predict[:, 2]) # 计算CH score 97 | s3 = davies_bouldin_score(predict[:, 0:2], predict[:, 2]) # 计算 DBI 98 | 99 | print(s1, s2, s3) 100 | 101 | 102 | if __name__ == '__main__': 103 | main() 104 | -------------------------------------------------------------------------------- /图像超分辨项目/train_srresnet.py: -------------------------------------------------------------------------------- 1 | import torch.backends.cudnn as cudnn 2 | import torch 3 | from torch import nn 4 | from torchvision.utils import make_grid 5 | from models import SRResNet 6 | from datasets import SRDataset 7 | from utils import * 8 | 9 | 10 | # 数据集参数 11 | data_folder = './data/' # 数据存放路径 12 | crop_size = 96 # 高分辨率图像裁剪尺寸 13 | scaling_factor = 4 # 放大比例 14 | 15 | # 模型参数 16 | large_kernel_size = 9 # 第一层卷积和最后一层卷积的核大小 17 | small_kernel_size = 3 # 中间层卷积的核大小 18 | n_channels = 64 # 中间层通道数 19 | n_blocks = 16 # 残差模块数量 20 | 21 | # 学习参数 22 | checkpoint = None # 预训练模型路径,如果不存在则为None 23 | batch_size = 64 # 批大小 24 | start_epoch = 1 # 轮数起始位置 25 | epochs = 130 # 迭代轮数 26 | workers = 4 # 工作线程数 27 | lr = 1e-4 # 学习率 28 | 29 | # 设备参数 30 | device = torch.device("cuda" if torch.cuda.is_available() else "cpu") 31 | cudnn.benchmark = True # 对卷积进行加速 32 | 33 | def main(): 34 | """ 35 | 训练. 36 | """ 37 | global checkpoint,start_epoch,writer 38 | 39 | # 初始化 40 | model = SRResNet(large_kernel_size=large_kernel_size, 41 | small_kernel_size=small_kernel_size, 42 | n_channels=n_channels, 43 | n_blocks=n_blocks, 44 | scaling_factor=scaling_factor) 45 | # 初始化优化器 46 | optimizer = torch.optim.Adam(params=filter(lambda p: p.requires_grad, model.parameters()),lr=lr) 47 | 48 | # 迁移至默认设备进行训练 49 | model = nn.DataParallel(model) 50 | model = model.to(device) 51 | criterion = nn.MSELoss().to(device) 52 | 53 | # 加载预训练模型 54 | if checkpoint is not None: 55 | checkpoint = torch.load(checkpoint) 56 | start_epoch = checkpoint['epoch'] + 1 57 | model.load_state_dict(checkpoint['model']) 58 | optimizer.load_state_dict(checkpoint['optimizer']) 59 | 60 | 61 | 62 | # 定制化的dataloaders 63 | train_dataset = SRDataset(data_folder,split='train', 64 | crop_size=crop_size, 65 | scaling_factor=scaling_factor, 66 | lr_img_type='imagenet-norm', 67 | hr_img_type='[-1, 1]') 68 | train_loader = torch.utils.data.DataLoader(train_dataset, 69 | batch_size=batch_size, 70 | shuffle=True, 71 | num_workers=workers, 72 | pin_memory=True) 73 | 74 | # 开始逐轮训练 75 | for epoch in range(start_epoch, epochs+1): 76 | 77 | model.train() # 训练模式:允许使用批样本归一化 78 | 79 | loss_epoch = AverageMeter() # 统计损失函数 80 | 81 | n_iter = len(train_loader) 82 | 83 | # 按批处理 84 | for i, (lr_imgs, hr_imgs) in enumerate(train_loader): 85 | 86 | # 数据移至默认设备进行训练 87 | lr_imgs = lr_imgs.to(device) # (batch_size (N), 3, 24, 24), imagenet-normed 格式 88 | hr_imgs = hr_imgs.to(device) # (batch_size (N), 3, 96, 96), [-1, 1]格式 89 | 90 | # 前向传播 91 | sr_imgs = model(lr_imgs) 92 | 93 | # 计算损失 94 | loss = criterion(sr_imgs, hr_imgs) 95 | 96 | # 后向传播 97 | optimizer.zero_grad() 98 | loss.backward() 99 | 100 | # 更新模型 101 | optimizer.step() 102 | 103 | # 记录损失值 104 | loss_epoch.update(loss.item(), lr_imgs.size(0)) 105 | 106 | # 打印结果 107 | print("第 "+str(i)+ " 个batch训练结束") 108 | 109 | # 手动释放内存 110 | del lr_imgs, hr_imgs, sr_imgs 111 | 112 | # 保存预训练模型 113 | torch.save({ 114 | 'epoch': epoch, 115 | 'model': model.module.state_dict(), 116 | 'optimizer': optimizer.state_dict() 117 | }, 'results/checkpoint_srresnet.pth') 118 | 119 | 120 | if __name__ == '__main__': 121 | main() 122 | -------------------------------------------------------------------------------- /计算智能导论/遗传算法.py: -------------------------------------------------------------------------------- 1 | # -*- coding:utf-8 -*- 2 | # Author : JackZhu 3 | 4 | # Data : 2021/5/13 19:28 5 | 6 | import matplotlib.pyplot as plt 7 | import torch 8 | import torch.nn as nn 9 | import pandas as pd 10 | import numpy as np 11 | from matplotlib import cm 12 | from mpl_toolkits.mplot3d import Axes3D 13 | 14 | DNA_SIZE = 31 # DNA大小 15 | POP_SIZE = 1000 # 种群大小 16 | CROSSOVER_RATE = 0.8 # 交叉概率 17 | MUTATION_RATE = 0.01 # 变异概率 18 | N_GENERATIONS = 60 # 迭代次数 19 | X_BOUND = [-100, 100] 20 | Y_BOUND = [-100, 100] 21 | 22 | 23 | def F(x, y): 24 | return x ** 2 + y ** 2 25 | 26 | 27 | class GA(): 28 | def __init__(self, F): 29 | self.pop = np.random.randint(2, size=(POP_SIZE, DNA_SIZE * 2)) 30 | self.F = F 31 | 32 | def fit(self): 33 | x, y = self.decode() 34 | pred = F(x, y) 35 | return np.abs(pred - np.max(pred)) + 1e-3 36 | 37 | def select(self,fitness): 38 | idx = np.random.choice(np.arange(POP_SIZE), size=POP_SIZE, replace=True, 39 | p=(fitness) / (fitness.sum())) 40 | return self.pop[idx] 41 | 42 | def crossover_and_mutation(self, CROSSOVER_RATE=0.8): 43 | new_pop = [] 44 | for father in self.pop: # 遍历种群中的每一个个体,将该个体作为父亲 45 | child = father # 孩子先得到父亲的全部基因(这里我把一串二进制串的那些0,1称为基因) 46 | if np.random.rand() < CROSSOVER_RATE: # 产生子代时不是必然发生交叉,而是以一定的概率发生交叉 47 | mother = self.pop[np.random.randint(POP_SIZE)] # 再种群中选择另一个个体,并将该个体作为母亲 48 | cross_points = np.random.randint(low=0, high=DNA_SIZE * 2) # 随机产生交叉的点 49 | child[cross_points:] = mother[cross_points:] # 孩子得到位于交叉点后的母亲的基因 50 | self.mutation(child, MUTATION_RATE) # 每个后代有一定的机率发生变异 51 | new_pop.append(child) 52 | 53 | return new_pop 54 | 55 | def mutation(self, child, MUTATION_RATE=0.005): 56 | if np.random.rand() < MUTATION_RATE: # 以MUTATION_RATE的概率进行变异 57 | mutate_point = np.random.randint(0, DNA_SIZE * 2) # 随机产生一个实数,代表要变异基因的位置 58 | child[mutate_point] = child[mutate_point] ^ 1 # 将变异点的二进制为反转 59 | 60 | def plot(self): 61 | X = np.linspace(*X_BOUND, 100) 62 | Y = np.linspace(*Y_BOUND, 100) 63 | X, Y = np.meshgrid(X, Y) 64 | Z = self.F(X, Y) 65 | ax.plot_surface(X, Y, Z, rstride=1, cstride=1,cmap='rainbow') 66 | ax.set_xlabel('x') 67 | ax.set_ylabel('y') 68 | ax.set_zlabel('f(x,y)') 69 | plt.pause(1) 70 | plt.show() 71 | 72 | def decode(self): 73 | x_pop = self.pop[:, 1::2] # 奇数列表示X 74 | y_pop = self.pop[:, ::2] # 偶数列表示y 75 | # pop:(POP_SIZE,DNA_SIZE)*(DNA_SIZE,1) --> (POP_SIZE,1) 76 | x = x_pop.dot(2 **np.arange(DNA_SIZE)[::-1]) / float(2 ** DNA_SIZE - 1) * (X_BOUND[1] - X_BOUND[0]) + X_BOUND[0] 77 | y = y_pop.dot(2 ** np.arange(DNA_SIZE)[::-1]) / float(2 ** DNA_SIZE - 1) * (Y_BOUND[1] - Y_BOUND[0]) + Y_BOUND[0] 78 | return x, y 79 | 80 | def print_info(self): 81 | x, y = self.decode() 82 | pred = self.F(x, y) 83 | min_prad_index = np.argmin(pred) 84 | print("最优的基因型:", self.pop[min_prad_index]) 85 | print("x:", x[min_prad_index]) 86 | print("y:", y[min_prad_index]) 87 | print("最小值:", pred[min_prad_index]) 88 | 89 | 90 | if __name__ == '__main__': 91 | N = 500 92 | ga = GA(F) 93 | fig = plt.figure() 94 | ax = Axes3D(fig) 95 | plt.ion() 96 | for i in range(N): 97 | if (i > N / 2): 98 | MUTATION_RATE /= 10 99 | x,y=ga.decode() 100 | sca = ax.scatter(x, y, F(x, y), c='black', marker='o') 101 | plt.show() 102 | plt.pause(0.01) 103 | sca.remove() 104 | ga.pop = np.array(ga.crossover_and_mutation(CROSSOVER_RATE)) 105 | fitness=ga.fit() 106 | ga.pop=ga.select(fitness) 107 | 108 | ga.print_info() 109 | plt.ioff() 110 | ga.plot(ax) 111 | 112 | 113 | -------------------------------------------------------------------------------- /计算智能导论/perceptron_self.py: -------------------------------------------------------------------------------- 1 | # -*- coding:utf-8 -*- 2 | # Author : JackZhu 3 | 4 | # Data : 2021/5/20 18:21 5 | import numpy as np 6 | import pandas as pd 7 | import torch 8 | import matplotlib.pyplot as plt 9 | import scipy.io as scio 10 | from sklearn.manifold import TSNE 11 | from sklearn.decomposition import PCA 12 | 13 | class Perceptron(): 14 | def __init__(self,input_num,f): 15 | self.input_num=input_num 16 | self.weights=np.ones(input_num) 17 | self.bias=2.0 18 | self.activation=f 19 | 20 | def __str__(self): 21 | return f'weight={self.weights},bias={self.bias}' 22 | 23 | def predict(self,inputs): 24 | return self.activation(np.dot(inputs,self.weights)+self.bias) 25 | 26 | def train(self,inputs,labels,rate=0.1): 27 | loss = 0 28 | for j in range(inputs.shape[0]): 29 | output = self.predict(inputs[j]) 30 | loss += labels[j] * output 31 | self.weights = self.weights + rate * (labels[j] - output) * inputs[j] 32 | self.bias = self.bias + rate * (labels[j] - output) 33 | loss =-loss/np.linalg.norm(self.weights) 34 | return loss 35 | def f(x): 36 | return 1 if x>0 else 0 37 | 38 | 39 | def get_data(): 40 | path = './sonar.csv' 41 | file = pd.read_csv(path, header=None) 42 | data = file.iloc[:, :-1] 43 | target = file.iloc[:, -1] 44 | target = pd.get_dummies(target).iloc[:, -1] 45 | data = np.array(data) 46 | target = np.array(target) 47 | return data,target 48 | 49 | # def get_data(): 50 | # data=torch.ones(100,2) 51 | # x0=torch.normal(2*data,1.5) 52 | # x1=torch.normal(-2*data,1.5) 53 | # x=torch.cat((x0,x1),0) 54 | # y0=torch.zeros(100) 55 | # y1=torch.ones(100) 56 | # y=torch.cat((y0,y1)) 57 | # data=np.array(x) 58 | # target=np.array(y) 59 | # return data,target 60 | 61 | # def get_data(): 62 | # path = './long.mat' 63 | # file = scio.loadmat(path)['long1'] 64 | # data = file[:, 0:2] 65 | # target = file[:, 2] 66 | # return data,target 67 | 68 | def plot_result(perceptron,result): 69 | knew = -perceptron.weights[0] / perceptron.weights[1] 70 | bnew = -perceptron.bias / perceptron.weights[1] 71 | x = np.linspace(-5, 5) 72 | y = lambda x: knew * x + bnew 73 | plt.xlim(-5,5) 74 | plt.ylim(-1,2) 75 | plt.plot(x, y(x), 'b--') 76 | plt.scatter(data[:, 0], data[:, 1], c=result) 77 | plt.title('Binary Classification') 78 | plt.show() 79 | 80 | def imshow(data,result): 81 | tsne = TSNE(n_components=2, learning_rate=100).fit_transform(data) 82 | pca = PCA().fit_transform(data) 83 | plt.figure(figsize=(12, 6)) 84 | plt.subplot(121) 85 | plt.scatter(tsne[:, 0], tsne[:, 1], c=result) 86 | plt.title('t-SNE') 87 | plt.subplot(122) 88 | plt.scatter(pca[:, 0], pca[:, 1], c=result) 89 | plt.title('PCA') 90 | plt.colorbar() 91 | plt.show() 92 | 93 | def plot_loss(loss_ls,epoch_ls,acc_ls): 94 | fig = plt.figure(figsize=(20, 8)) 95 | plt.subplot(121) 96 | plt.plot(epoch_ls,loss_ls) 97 | plt.title('Loss') 98 | plt.subplot(122) 99 | plt.plot(epoch_ls,acc_ls) 100 | plt.show() 101 | 102 | if __name__=='__main__': 103 | epochs = 50000 104 | data,target=get_data() 105 | perceptron=Perceptron(data.shape[1],f) 106 | print(perceptron) 107 | loss_ls,epoch_ls,acc_ls=[],[],[] 108 | for i in range(epochs): 109 | ls=perceptron.train(data,target) 110 | if i%100==0: 111 | loss_ls.append(ls) 112 | epoch_ls.append(i) 113 | acc, result = 0,0 114 | for i in range(data.shape[0]): 115 | result = perceptron.predict(data[i]) 116 | acc += (result == target[i])/len(target) 117 | acc_ls.append(acc) 118 | plot_loss(loss_ls,epoch_ls,acc_ls) 119 | print(perceptron) 120 | imshow(data,target) 121 | print('acc={}'.format(acc_ls[-1])) 122 | -------------------------------------------------------------------------------- /智能系统实验/手写识别.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import torch 3 | import torchvision 4 | import torch.nn as nn 5 | import torch.optim as optim 6 | import matplotlib.pyplot as plt 7 | import torchvision.transforms as transforms 8 | 9 | 10 | class LeNet(nn.Module): 11 | def __init__(self): 12 | super(LeNet, self).__init__() 13 | self.conv1 = nn.Sequential( 14 | nn.Conv2d(1, 6, 5, 1, 2), 15 | nn.ReLU(), 16 | nn.MaxPool2d(kernel_size=2, stride=2) 17 | ) 18 | self.conv2 = nn.Sequential( 19 | nn.Conv2d(6, 16, 5), 20 | nn.ReLU(), 21 | nn.MaxPool2d(2, 2) 22 | ) 23 | self.linear = nn.Sequential( 24 | nn.Linear(16 * 5 * 5, 120), 25 | nn.Linear(120, 84), 26 | nn.Linear(84, 10) 27 | ) 28 | 29 | def forward(self, x): 30 | x = self.conv1(x) 31 | x = self.conv2(x) 32 | x = x.view(x.size()[0], -1) 33 | x = self.linear(x) 34 | return x 35 | 36 | 37 | def train(): 38 | model.train() 39 | loss = 0.0 40 | for batch_idx, (data, target) in enumerate(trainloader): 41 | data, target = data.to(device), target.to(device) 42 | optimizer.zero_grad() 43 | output = model(data) 44 | loss = criterion(output, target) 45 | loss.backward() 46 | optimizer.step() 47 | if batch_idx % log_interval == 0: 48 | print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format( 49 | epoch, batch_idx * len(data), len(trainloader.dataset), 50 | 100. * batch_idx / len(trainloader), loss.item())) 51 | 52 | 53 | def treasure(): 54 | model.eval() 55 | test_loss = 0 56 | correct = 0 57 | with torch.no_grad(): 58 | for data, target in testloader: 59 | data, target = data.to(device), target.to(device) 60 | output = model(data) 61 | test_loss += criterion(output, target).item() 62 | pred = output.argmax(dim=1, keepdim=True) 63 | correct += pred.eq(target.view_as(pred)).sum().item() 64 | 65 | test_loss /= len(testloader.dataset) 66 | print('\nTest set: Average loss: {:.4f}, Accuracy: {}/{} ({:.2f}%)\n'.format( 67 | test_loss, correct, len(testloader.dataset), 68 | 100. * correct / len(testloader.dataset))) 69 | return 100. * correct / len(testloader.dataset), model.state_dict() 70 | 71 | 72 | transform = transforms.Compose( 73 | [transforms.ToTensor(), 74 | transforms.Normalize((0.1307,), (0.3081,))]) 75 | 76 | trainset = torchvision.datasets.MNIST('./data', 77 | download=True, 78 | train=True, 79 | transform=transform) 80 | testset = torchvision.datasets.MNIST('./data', 81 | download=True, 82 | train=False, 83 | transform=transform) 84 | 85 | trainloader = torch.utils.data.DataLoader(trainset, batch_size=64, 86 | shuffle=True, num_workers=0) 87 | 88 | testloader = torch.utils.data.DataLoader(testset, batch_size=64, 89 | shuffle=False, num_workers=0) 90 | 91 | log_interval = 300 92 | epochs = 5 93 | 94 | device = torch.device("cuda" if torch.cuda.is_available() else "cpu") 95 | torch.manual_seed(1) 96 | criterion = nn.CrossEntropyLoss() 97 | model = LeNet().to(device) 98 | # optimizer=torch.optim.SGD(model.parameters(),lr=lr,momentum=momentum) 99 | optimizer = optim.Adadelta(model.parameters(), lr=1.0) 100 | 101 | if __name__ == '__main__': 102 | max_acc = float('0') 103 | for epoch in range(1, epochs + 1): 104 | train() 105 | accuracy, net_dict = treasure() 106 | if accuracy > max_acc: 107 | max_acc = accuracy 108 | torch.save({'epoch_record': epoch, 'model': net_dict}, 109 | f'./手写识别模型_{max_acc}%.pth') 110 | -------------------------------------------------------------------------------- /计算智能导论/test_perceptron.py: -------------------------------------------------------------------------------- 1 | # -*- coding:utf-8 -*- 2 | # Author : JackZhu 3 | 4 | # Data : 2021/5/20 18:21 5 | import numpy as np 6 | import pandas as pd 7 | import torch 8 | import matplotlib.pyplot as plt 9 | import scipy.io as scio 10 | from sklearn.manifold import TSNE 11 | from sklearn.decomposition import PCA 12 | 13 | 14 | # 定义感知器类 15 | class Perceptron(): 16 | def __init__(self, input_num, f): 17 | self.input_num = input_num 18 | self.weights = np.ones(input_num) 19 | self.bias = 2.0 20 | self.activation = f 21 | 22 | def __str__(self): 23 | return f'weight={self.weights},bias={self.bias}' 24 | 25 | def predict(self, inputs): 26 | return self.activation(np.dot(inputs, self.weights) + self.bias) 27 | 28 | def train(self, inputs, labels, rate=0.1): 29 | loss = 0 30 | for j in range(inputs.shape[0]): 31 | output = self.predict(inputs[j]) 32 | loss += labels[j] * output 33 | self.weights = self.weights + rate * (labels[j] - output) * inputs[j] 34 | self.bias = self.bias + rate * (labels[j] - output) 35 | loss =-loss/np.linalg.norm(self.weights) 36 | return loss 37 | 38 | 39 | # 定义激活函数 40 | def f(x): 41 | return 1 if x > 0 else 0 42 | 43 | 44 | # 定义数据读取函数 45 | def get_data(): 46 | path = './sonar.csv' 47 | file = pd.read_csv(path, header=None) 48 | data = file.iloc[:, :-1] 49 | target = file.iloc[:, -1] 50 | target = pd.get_dummies(target).iloc[:, -1] 51 | data = np.array(data) 52 | target = np.array(target) 53 | return data, target 54 | 55 | 56 | # 二维数据 57 | # def get_data(): 58 | # data=torch.ones(100,2) 59 | # x0=torch.normal(2*data,1.5) 60 | # x1=torch.normal(-2*data,1.5) 61 | # x=torch.cat((x0,x1),0) 62 | # y0=torch.zeros(100) 63 | # y1=torch.ones(100) 64 | # y=torch.cat((y0,y1)) 65 | # data=np.array(x) 66 | # target=np.array(y) 67 | # return data,target 68 | 69 | # 二维数据 70 | # def get_data(): 71 | # path = './long.mat' 72 | # file = scio.loadmat(path)['long1'] 73 | # data = file[:, 0:2] 74 | # target = file[:, 2] 75 | # return data,target 76 | 77 | # 对二维数据的作图函数 78 | # def plot_result(perceptron,result): 79 | # knew = -perceptron.weights[0] / perceptron.weights[1] 80 | # bnew = -perceptron.bias / perceptron.weights[1] 81 | # x = np.linspace(-5, 5) 82 | # y = lambda x: knew * x + bnew 83 | # plt.xlim(-5,5) 84 | # plt.ylim(-1,2) 85 | # plt.plot(x, y(x), 'b--') 86 | # plt.scatter(data[:, 0], data[:, 1], c=result) 87 | # plt.title('Binary Classification') 88 | # plt.show() 89 | 90 | # 对高维数据降维可视化 91 | def imshow(data, result): 92 | tsne = TSNE(n_components=2, learning_rate=100).fit_transform(data) 93 | pca = PCA().fit_transform(data) 94 | plt.figure(figsize=(12, 6)) 95 | plt.subplot(121) 96 | plt.scatter(tsne[:, 0], tsne[:, 1], c=result) 97 | plt.title('t-SNE') 98 | plt.subplot(122) 99 | plt.scatter(pca[:, 0], pca[:, 1], c=result) 100 | plt.title('PCA') 101 | plt.colorbar() 102 | plt.show() 103 | 104 | 105 | # 可视化迭代过程准确率和损失变化 106 | def plot_loss(loss_ls, epoch_ls, acc_ls): 107 | fig = plt.figure(figsize=(20, 8)) 108 | plt.subplot(121) 109 | plt.plot(epoch_ls, loss_ls) 110 | plt.title('Loss') 111 | plt.subplot(122) 112 | plt.plot(epoch_ls, acc_ls) 113 | plt.show() 114 | 115 | 116 | if __name__ == '__main__': 117 | epochs = 5000 118 | data, target = get_data() 119 | perceptron = Perceptron(data.shape[1], f) 120 | print(perceptron) 121 | loss_ls, epoch_ls, acc_ls = [], [], [] 122 | for i in range(epochs): 123 | ls = perceptron.train(data, target) 124 | if i % 100 == 0: 125 | loss_ls.append(ls) 126 | epoch_ls.append(i) 127 | acc, result = 0, 0 128 | for i in range(data.shape[0]): 129 | result = perceptron.predict(data[i]) 130 | acc += (result == target[i]) / len(target) 131 | acc_ls.append(acc) 132 | plot_loss(loss_ls, epoch_ls, acc_ls) 133 | print(perceptron) 134 | imshow(data, target) 135 | print('acc={}'.format(acc_ls[-1])) 136 | -------------------------------------------------------------------------------- /深度学习/FashionMnist_self.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Tue Dec 1 13:30:54 2020 4 | 5 | @author: tremble 6 | """ 7 | 8 | import matplotlib.pyplot as plt 9 | import numpy as np 10 | 11 | import torch 12 | import torchvision 13 | import torchvision.transforms as transforms 14 | 15 | import torch.nn as nn 16 | import torch.nn.functional as F 17 | import torch.optim as optim 18 | 19 | # constant for classes 20 | classes = ('T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat', 21 | 'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle Boot') 22 | 23 | class Net(nn.Module): 24 | def __init__(self): 25 | super(Net, self).__init__() 26 | self.conv1 = nn.Conv2d(1, 32, 3, 1) 27 | self.conv2 = nn.Conv2d(32, 64, 3, 1) 28 | self.dropout1 = nn.Dropout(0.25) 29 | self.dropout2 = nn.Dropout(0.5) 30 | self.fc1 = nn.Linear(9216, 128) 31 | self.fc2 = nn.Linear(128, 10) 32 | 33 | def forward(self, x): 34 | x = self.conv1(x) 35 | x = F.relu(x) 36 | x = self.conv2(x) 37 | x = F.relu(x) 38 | x = F.max_pool2d(x, 2) 39 | x = self.dropout1(x) 40 | x = torch.flatten(x, 1) 41 | x = self.fc1(x) 42 | x = F.relu(x) 43 | x = self.dropout2(x) 44 | x = self.fc2(x) 45 | output = F.log_softmax(x, dim=1) 46 | return output 47 | 48 | def train(model, device, train_loader, optimizer, epoch,criterion): 49 | model.train() 50 | loss=0.0 51 | for i, (data, target) in enumerate(train_loader): 52 | data, target = data.to(device), target.to(device) 53 | optimizer.zero_grad() 54 | output = model(data) 55 | loss = criterion(output, target) 56 | loss.backward() 57 | optimizer.step() 58 | if i % 100 == 0: 59 | print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format( 60 | epoch, i * len(data), len(train_loader.dataset), 61 | 100. * i / len(train_loader), loss.item())) 62 | 63 | 64 | def test(model, device, test_loader,criterion): 65 | model.eval() 66 | test_loss = 0 67 | correct = 0 68 | with torch.no_grad(): 69 | for data, target in test_loader: 70 | data, target = data.to(device), target.to(device) 71 | output = model(data) 72 | test_loss += criterion(output, target).item() # sum up batch loss 73 | pred = output.argmax(dim=1, keepdim=True) # get the index of the max log-probability 74 | correct += pred.eq(target.view_as(pred)).sum().item() 75 | 76 | test_loss /= len(test_loader.dataset) 77 | 78 | print('\nTest set: Average loss: {:.4f}, Accuracy: {}/{} ({:.0f}%)\n'.format( 79 | test_loss, correct, len(test_loader.dataset), 80 | 100. * correct / len(test_loader.dataset))) 81 | 82 | 83 | 84 | use_cuda = torch.cuda.is_available() 85 | print(use_cuda) 86 | if use_cuda: 87 | torch.cuda.manual_seed(1) 88 | else: 89 | torch.manual_seed(1) 90 | device = torch.device("cuda" if use_cuda else "cpu") 91 | transform = transforms.Compose( 92 | [transforms.ToTensor(), 93 | transforms.Normalize((0.5,), (0.5,))]) 94 | 95 | # datasets 96 | trainset = torchvision.datasets.FashionMNIST('./data', 97 | download=True, 98 | train=True, 99 | transform=transform) 100 | testset = torchvision.datasets.FashionMNIST('./data', 101 | download=True, 102 | train=False, 103 | transform=transform) 104 | 105 | # dataloaders 106 | trainloader = torch.utils.data.DataLoader(trainset, batch_size=4, 107 | shuffle=True, num_workers=0) 108 | 109 | 110 | testloader = torch.utils.data.DataLoader(testset, batch_size=4, 111 | shuffle=False, num_workers=0) 112 | 113 | criterion = nn.CrossEntropyLoss() 114 | model = Net().to(device) 115 | #optimizer = optim.SGD(model.parameters(), lr=0.001, momentum=0.9) 116 | optimizer = optim.Adadelta(model.parameters(), lr=1.0) 117 | #optimizer = optim.Adam(model.parameters(),lr=0.01) 118 | for epoch in range(1,10): 119 | train(model, device, trainloader, optimizer, epoch,criterion) 120 | test(model, device, testloader,criterion) 121 | torch.save(model, f"fashion_mnist_cnn{epoch}.pth") 122 | 123 | #model=torch.load('fashion_mnist_cnn.pth') 124 | #print(model) 125 | #model.eval() 126 | 127 | 128 | 129 | 130 | -------------------------------------------------------------------------------- /机器学习/神经网络法.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | import matplotlib.pyplot as plt 4 | import numpy as np 5 | 6 | import torch 7 | import torchvision 8 | import torchvision.transforms as transforms 9 | 10 | import torch.nn as nn 11 | import torch.nn.functional as F 12 | import torch.optim as optim 13 | 14 | # constant for classes 15 | classes = ('T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat', 16 | 'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle Boot') 17 | 18 | 19 | class Net(nn.Module): 20 | def __init__(self): 21 | super(Net, self).__init__() 22 | self.conv1 = nn.Conv2d(1, 32, 3, 1) 23 | self.conv2 = nn.Conv2d(32, 64, 3, 1) 24 | self.dropout1 = nn.Dropout(0.25) 25 | self.dropout2 = nn.Dropout(0.5) 26 | self.fc1 = nn.Linear(9216, 128) 27 | self.fc2 = nn.Linear(128, 10) 28 | 29 | def forward(self, x): 30 | x = self.conv1(x) 31 | x = F.relu(x) 32 | x = self.conv2(x) 33 | x = F.relu(x) 34 | x = F.max_pool2d(x, 2) 35 | x = self.dropout1(x) 36 | x = torch.flatten(x, 1) 37 | x = self.fc1(x) 38 | x = F.relu(x) 39 | x = self.dropout2(x) 40 | x = self.fc2(x) 41 | output = F.log_softmax(x, dim=1) 42 | return output 43 | 44 | 45 | def train(model, device, train_loader, optimizer, epoch, criterion): 46 | model.train() 47 | loss = 0.0 48 | for i, (data, target) in enumerate(train_loader): 49 | data, target = data.to(device), target.to(device) 50 | optimizer.zero_grad() 51 | output = model(data) 52 | loss = criterion(output, target) 53 | loss.backward() 54 | optimizer.step() 55 | if i % 100 == 0: 56 | print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format( 57 | epoch, i * len(data), len(train_loader.dataset), 58 | 100. * i / len(train_loader), loss.item())) 59 | 60 | 61 | def test(model, device, test_loader, criterion): 62 | model.eval() 63 | test_loss = 0 64 | correct = 0 65 | with torch.no_grad(): 66 | for data, target in test_loader: 67 | data, target = data.to(device), target.to(device) 68 | output = model(data) 69 | test_loss += criterion(output, target).item() # sum up batch loss 70 | pred = output.argmax(dim=1, keepdim=True) # get the index of the max log-probability 71 | correct += pred.eq(target.view_as(pred)).sum().item() 72 | 73 | test_loss /= len(test_loader.dataset) 74 | 75 | print('\nTest set: Average loss: {:.4f}, Accuracy: {}/{} ({:.0f}%)\n'.format( 76 | test_loss, correct, len(test_loader.dataset), 77 | 100. * correct / len(test_loader.dataset))) 78 | 79 | 80 | use_cuda = torch.cuda.is_available() 81 | print(use_cuda) 82 | if use_cuda: 83 | torch.cuda.manual_seed(1) 84 | else: 85 | torch.manual_seed(1) 86 | device = torch.device("cuda" if use_cuda else "cpu") 87 | transform = transforms.Compose( 88 | [transforms.ToTensor(), 89 | transforms.Normalize((0.5,), (0.5,))]) 90 | 91 | # datasets 92 | trainset = torchvision.datasets.FashionMNIST('./data', 93 | download=True, 94 | train=True, 95 | transform=transform) 96 | testset = torchvision.datasets.FashionMNIST('./data', 97 | download=True, 98 | train=False, 99 | transform=transform) 100 | 101 | # dataloaders 102 | trainloader = torch.utils.data.DataLoader(trainset, batch_size=4, 103 | shuffle=True, num_workers=0) 104 | 105 | testloader = torch.utils.data.DataLoader(testset, batch_size=4, 106 | shuffle=False, num_workers=0) 107 | 108 | criterion = nn.CrossEntropyLoss() 109 | model = Net().to(device) 110 | # optimizer = optim.SGD(model.parameters(), lr=0.001, momentum=0.9) 111 | optimizer = optim.Adadelta(model.parameters(), lr=1.0) 112 | # optimizer = optim.Adam(model.parameters(),lr=0.01) 113 | for epoch in range(1, 10): 114 | train(model, device, trainloader, optimizer, epoch, criterion) 115 | test(model, device, testloader, criterion) 116 | torch.save(model, f"fashion_mnist_cnn{epoch}.pth") 117 | 118 | # model=torch.load('work1_cnn.pth') 119 | # print(model) 120 | # model.eval() 121 | 122 | 123 | 124 | 125 | -------------------------------------------------------------------------------- /智能系统实验/4_2/动物分类2.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import torch 3 | import torch.nn as nn 4 | import torch.optim as optim 5 | import torchvision 6 | import torch.nn.functional as F 7 | import matplotlib.pyplot as plt 8 | import cv2 9 | from net_model import ICNET 10 | from net_model import RESNET 11 | from net_model import Net1, my_resnet,densenet 12 | from torch.utils.data import TensorDataset 13 | from torch.utils.data import DataLoader 14 | from torchvision import transforms 15 | import net_model 16 | 17 | 18 | # from torch.utils.tensorboard import SummaryWriter 19 | 20 | 21 | def dataloader(): 22 | transform = transforms.Compose([transforms.Resize((128,128)), transforms.ToTensor()]) 23 | 24 | trainset = torchvision.datasets.ImageFolder(root='./data/train', transform=transform) 25 | trainloader = torch.utils.data.DataLoader(trainset, batch_size=16, shuffle=True, num_workers=0) 26 | 27 | testset = torchvision.datasets.ImageFolder(root='./data/test', transform=transform) 28 | testloader = torch.utils.data.DataLoader(testset, batch_size=8, shuffle=False, num_workers=0) 29 | return trainloader, testloader 30 | 31 | 32 | def train(train_loader, model, criterion, optimizer, scheduler, device, epoch): 33 | model.train() 34 | train_acc, train_loss = 0.0, 0.0 35 | for batch_idx, (data, target) in enumerate(train_loader): 36 | data, target = data.to(device), target.to(device) 37 | optimizer.zero_grad() 38 | output = model(data) 39 | print(target.shape) 40 | loss = criterion(output, target) 41 | loss.backward() 42 | optimizer.step() 43 | train_loss += loss.item() 44 | pred = output.argmax(dim=1, keepdim=True) 45 | train_acc += pred.eq(target.view_as(pred)).sum().item() 46 | print( 47 | 'Epoch:{}\n Train: Average Loss: {:.6f},Accuracy:{:.2f}%'.format(epoch, train_loss / (batch_idx + 1), 48 | 100.0 * train_acc / len(train_loader.dataset))) 49 | scheduler.step(train_acc) 50 | 51 | 52 | def measure(test_loader, model, criterion, device, epoch): 53 | model.eval() 54 | test_loss = 0 55 | correct = 0 56 | # writer = SummaryWriter() 57 | with torch.no_grad(): 58 | for data, target in test_loader: 59 | data, target = data.to(device), target.to(device) 60 | output = model(data) 61 | test_loss += criterion(output, target).item() 62 | pred = output.argmax(dim=1, keepdim=True) 63 | correct += pred.eq(target.view_as(pred)).sum().item() 64 | test_loss /= len(test_loader.dataset) 65 | print('\nTest: Average loss: {:.4f}, Accuracy: {}/{} ({:.2f}%)\n'.format( 66 | test_loss, correct, len(test_loader.dataset), 67 | 100. * correct / len(test_loader.dataset))) 68 | return 100. * correct / len(test_loader.dataset), test_loss, model.state_dict() 69 | 70 | 71 | def imshow(epoch_list, loss_list, acc_list): 72 | fig = plt.figure(figsize=(20, 8)) 73 | plt.subplot(121) 74 | plt.plot(epoch_list, loss_list, linestyle=':') 75 | plt.xlabel('epoch') 76 | plt.ylabel('Test loss') 77 | plt.subplot(122) 78 | plt.plot(epoch_list, acc_list, linestyle=':') 79 | plt.xlabel('epoch ') 80 | plt.ylabel('Test accuracy') 81 | plt.savefig('./动物分类.png') 82 | plt.show() 83 | 84 | 85 | if __name__ == '__main__': 86 | device = torch.device("cuda" if torch.cuda.is_available() else "cpu") 87 | model = my_resnet().to(device) 88 | # model=ICNET().to(device) 89 | # model = densenet().to(device) 90 | 91 | criterion = nn.CrossEntropyLoss() 92 | optimizer = optim.Adadelta(model.parameters(), lr=1.0) 93 | # optimizer = optim.Adam(model.parameters(), lr=0.001) 94 | scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(optimizer, mode="max", 95 | factor=0.85, patience=0) 96 | train_loader, test_loader = dataloader() 97 | num_epoch = 50 98 | 99 | max_acc = float('0') 100 | epoch_list, acc_list, loss_list = [], [], [] 101 | for epoch in range(1, num_epoch + 1): 102 | train(train_loader, model, criterion, optimizer, scheduler, device, epoch) 103 | test_acc, test_ls, net_dict = measure(test_loader, model, criterion, device, epoch) 104 | epoch_list.append(epoch) 105 | loss_list.append(test_ls) 106 | acc_list.append(test_acc) 107 | if test_acc > max_acc: 108 | max_acc = test_acc 109 | # torch.save({'epoch_record': epoch, 'model': net_dict},f'./模型/动物分类模型_{max_acc}%.pth') 110 | torch.save(model, f'./模型/动物分类模型_{max_acc}%.pth') 111 | 112 | imshow(epoch_list, loss_list, acc_list) 113 | -------------------------------------------------------------------------------- /机器学习/高光谱.py: -------------------------------------------------------------------------------- 1 | import pandas as pd 2 | import numpy as np 3 | from scipy.io import loadmat 4 | import sklearn 5 | from sklearn import preprocessing 6 | from sklearn.ensemble import RandomForestClassifier as RFC 7 | from sklearn.feature_selection import SelectFromModel 8 | from sklearn.feature_selection import VarianceThreshold 9 | from sklearn.feature_selection import SelectKBest, f_classif 10 | from sklearn.linear_model import LogisticRegression 11 | from sklearn.feature_selection import RFE 12 | from sklearn.model_selection import train_test_split 13 | from sklearn.metrics import confusion_matrix 14 | from sklearn import metrics 15 | 16 | 17 | def get_data(): 18 | dat = loadmat('./高光谱数据集/KSC.mat')['KSC'] 19 | lab = loadmat('./高光谱数据集/KSC_gt.mat')['KSC_gt'] 20 | 21 | dat = dat.reshape(-1, 176) 22 | lab = lab.reshape(-1) 23 | print(dat.shape) 24 | 25 | data, label = [], [] 26 | for i in range(dat.shape[0]): 27 | if lab[i].all() != 0: 28 | data.append(dat[i, :]) 29 | label.append(lab[i]) 30 | 31 | data = np.array(data) 32 | label = np.array(label) 33 | return data, label 34 | 35 | 36 | def process(data): 37 | data = preprocessing.StandardScaler().fit_transform(data) 38 | print('shape={}'.format(data.shape)) 39 | selector = VarianceThreshold() # 实例化,不填参数默认方差为0 40 | data = selector.fit_transform(data) 41 | print(data.shape) 42 | median_num = np.median(data) 43 | data = VarianceThreshold(median_num).fit_transform(data) 44 | print(data.shape) 45 | return data 46 | 47 | 48 | # acc = cross_val_score(KNN(), data, label, cv=5).mean() 49 | # print("accuracy:{},time:{}".format(acc,time.time()-start)) 50 | 51 | def select_k(data, label, k): 52 | results = SelectKBest(f_classif, k=k).fit(data, label) 53 | print(results) 54 | features = pd.DataFrame({ 55 | "score": results.scores_, 56 | "pvalue": results.pvalues_, 57 | "select": results.get_support() 58 | }) 59 | features.sort_values("score", ascending=False) 60 | print(features) 61 | index = results.get_support(indices=True) 62 | print(index) 63 | return index 64 | 65 | 66 | def rfe(data, label, n): 67 | results = RFE(estimator=LogisticRegression(), n_features_to_select=n) 68 | print(results) 69 | results.fit(data, label) 70 | index = results.get_support(indices=True) 71 | print(index) 72 | return index 73 | 74 | 75 | def rfc(data, label): 76 | RFC_ = RFC(n_estimators=50, random_state=0) 77 | X_embedded = SelectFromModel(RFC_, threshold=0.005).fit_transform(data, label) 78 | result = sklearn.model_selection.cross_val_score(RFC_, X_embedded, label, cv=5).mean() 79 | print(result) 80 | 81 | 82 | def select_index_data(index, data, label): 83 | data_after = [] 84 | for i in index: 85 | data_after.append(data[:, i]) 86 | data_after = np.array(data_after).transpose() 87 | print(data_after.shape) 88 | print(label.shape) 89 | return train_test_split(data_after, label, test_size=0.3, random_state=1) 90 | 91 | 92 | def measure_feature(train_data, test_data, train_label, test_label, gamma, c): 93 | clf = sklearn.svm.SVC(kernel='poly', gamma=gamma, C=c) 94 | clf.fit(train_data, train_label) 95 | predict = clf.predict(test_data) 96 | clf.get_params(deep=True) 97 | acc = sklearn.metrics.accuracy_score(test_label, predict) 98 | f1 = sklearn.metrics.f1_score(test_label, predict, average='micro') 99 | recall = metrics.recall_score(test_label, predict, average='micro') 100 | precision = metrics.precision_score(test_label, predict, average='micro') 101 | return acc, f1, recall, precision 102 | 103 | 104 | if __name__ == '__main__': 105 | data, label = get_data() 106 | data = process(data) 107 | 108 | # rfc(data,label) 109 | index = select_k(data, label, k=50) 110 | # index = rfe(data, label,n=30) 111 | train_data, test_data, train_label, test_label = select_index_data(index, data, label) 112 | print(train_data.shape, test_data.shape, train_label.shape, test_label.shape) 113 | gamma, c = 0.125, 60 114 | train_acc, train_f1, train_recall, train_precision = measure_feature(train_data, train_data, train_label, 115 | train_label, gamma, c) 116 | test_acc, test_f1, test_recall, test_precision = measure_feature(train_data, test_data, train_label, test_label, 117 | gamma, c) 118 | 119 | print(train_acc, test_acc) 120 | print(train_f1, test_f1) 121 | print(train_recall, test_recall) 122 | print(train_precision, test_precision) 123 | # print('训练集准确率为{:.4f},测试集准确率为{:.4f}'.format(train_acc, test_acc)) 124 | -------------------------------------------------------------------------------- /智能系统实验/4_2/动物分类1.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import torch 3 | import torch.nn as nn 4 | import torch.optim as optim 5 | import torchvision 6 | import torch.nn.functional as F 7 | import matplotlib.pyplot as plt 8 | import cv2 9 | from net_model import ICNET 10 | from net_model import RESNET 11 | from net_model import animal 12 | from torch.utils.data import TensorDataset 13 | from torch.utils.data import DataLoader 14 | 15 | m1, m2 = 70, 30 16 | l_x = 128 17 | train_set = np.zeros(m1 * 2 * l_x * l_x * 3) 18 | train_set = np.reshape(train_set, (m1 * 2, 3, l_x, l_x)) 19 | 20 | test_set = np.zeros(m2 * 2 * l_x * l_x * 3) 21 | test_set = np.reshape(test_set, (m2 * 2, 3, l_x, l_x)) 22 | 23 | train_label = np.zeros(m1 * 2) 24 | test_label = np.zeros(m2 * 2) 25 | 26 | success_mark = 0 27 | for i in range(m1): 28 | path1 = f'./sample/cat.{i}.jpg' 29 | path2 = f'./sample/dog.{i}.jpg' 30 | img1 = cv2.imread(path1) 31 | img2 = cv2.imread(path2) 32 | img1 = cv2.resize(img1, (l_x, l_x)) 33 | img2 = cv2.resize(img2, (l_x, l_x)) 34 | 35 | train_set[i, 0, :, :] = img1[:, :, 0] 36 | train_set[i, 1, :, :] = img1[:, :, 1] 37 | train_set[i, 2, :, :] = img1[:, :, 2] 38 | train_label[i] = 0 39 | success_mark += 1 40 | 41 | train_set[m1 + i, 0, :, :] = img2[:, :, 0] 42 | train_set[m1 + i, 1, :, :] = img2[:, :, 1] 43 | train_set[m1 + i, 2, :, :] = img2[:, :, 2] 44 | train_label[m1 + i] = 1 45 | success_mark += 1 46 | 47 | for i in range(m2): 48 | path3 = f'./sample/cat.{i + m1}.jpg' 49 | path4 = f'./sample/dog.{i + m1}.jpg' 50 | img3 = cv2.imread(path3) 51 | img4 = cv2.imread(path4) 52 | img3 = cv2.resize(img3, (l_x, l_x)) 53 | img4 = cv2.resize(img4, (l_x, l_x)) 54 | 55 | test_set[i, 0, :, :] = img3[:, :, 0] 56 | test_set[i, 1, :, :] = img3[:, :, 1] 57 | test_set[i, 2, :, :] = img4[:, :, 2] 58 | test_label[i] = 0 59 | success_mark += 1 60 | 61 | test_set[m2 + i, 0, :, :] = img4[:, :, 0] 62 | test_set[m2 + i, 1, :, :] = img4[:, :, 1] 63 | test_set[m2 + i, 2, :, :] = img4[:, :, 2] 64 | test_label[m2 + i] = 1 65 | success_mark += 1 66 | 67 | if success_mark == 200: 68 | np.save('train_set.npy', train_set) 69 | np.save('test_set.npy', test_set) 70 | 71 | np.save('test_label.npy',test_label) 72 | 73 | 74 | 75 | device = torch.device("cuda" if torch.cuda.is_available() else "cpu") 76 | #model = ICNET().to(device) 77 | #model=animal().to(device) 78 | model = RESNET().to(device) 79 | 80 | criterion = nn.CrossEntropyLoss() 81 | optimizer = optim.Adadelta(model.parameters(), lr=1.0) 82 | #optimizer=optim.SGD(model.parameters(),lr=10e-5,momentum=0.5) 83 | 84 | samplenum = 140 85 | minibatch = 2 86 | w_HR = 128 87 | x0 = np.zeros(minibatch * 3 * w_HR * w_HR) 88 | x0 = np.reshape(x0, (minibatch, 3, w_HR, w_HR)) 89 | y0 = np.zeros(minibatch) 90 | x0 = torch.tensor(x0).type(torch.FloatTensor).to(device) 91 | y0 = torch.tensor(y0).type(torch.LongTensor).to(device) 92 | 93 | train_set = torch.tensor(train_set).type(torch.LongTensor).to(device) 94 | min_loss = float('inf') 95 | for epoch in range(1,10): 96 | for iterations in range(int(samplenum / minibatch)): 97 | model.train() 98 | loss=0.0 99 | k = 0 100 | for i in range(iterations * minibatch, min(samplenum, iterations * minibatch + minibatch)): 101 | x0[k, 0, :, :] = train_set[i, 0, :, :] 102 | x0[k, 1, :, :] = train_set[i, 1, :, :] 103 | x0[k, 2, :, :] = train_set[i, 2, :, :] 104 | y0[k] = train_label[i] 105 | k = k + 1 106 | 107 | out = model(x0) 108 | #out_real = torch.max(out, 1)[1] 109 | loss = criterion(out, y0) 110 | optimizer.zero_grad() 111 | loss.backward() 112 | optimizer.step() 113 | print(f"epoch{epoch},loss:{loss.item()}") 114 | if loss < min_loss: 115 | min_loss = loss 116 | torch.save(model,'./动物分类.pkl') 117 | 118 | test_feature = torch.tensor(test_set).type(torch.FloatTensor).cuda() 119 | test_label = torch.tensor(test_label).type(torch.FloatTensor).cuda() 120 | 121 | test_dataset = TensorDataset(test_feature, test_label) 122 | test_loader = DataLoader(test_dataset, batch_size=4, shuffle=True) 123 | 124 | 125 | def test_net(): 126 | correct = 0.0 127 | with torch.no_grad(): 128 | for data, target in test_loader: 129 | model.eval() 130 | data, target = data.to(device), target.to(device) 131 | output = model(data) 132 | pred = output.argmax(dim=1, keepdim=True) 133 | correct += pred.eq(target.view_as(pred)).sum().item() 134 | #pred=torch.max(output,1)[1].cpu().data.numpy() 135 | #tar=target.cpu().data.numpy() 136 | #correct+=sum(tar==pred) 137 | print('accuracy:{:.2f}%'.format(100.0 * correct / len(test_loader.dataset))) 138 | 139 | 140 | if __name__ == '__main__': 141 | test_net() -------------------------------------------------------------------------------- /数据挖掘/Pattern Mining.py: -------------------------------------------------------------------------------- 1 | import sklearn 2 | import torch 3 | import torchvision 4 | import torchvision.transforms as transforms 5 | from torch.utils.data import DataLoader 6 | from PIL import Image 7 | import numpy as np 8 | from skimage import feature as ft 9 | import pandas as pd 10 | import sklearn 11 | import random 12 | from sklearn import datasets 13 | from sklearn.preprocessing import scale 14 | from sklearn.manifold import TSNE 15 | from sklearn.decomposition import PCA 16 | import matplotlib.pyplot as plt 17 | from sklearn.metrics import silhouette_score 18 | from sklearn import svm 19 | from sklearn.cluster import KMeans 20 | from sklearn import metrics 21 | 22 | transform = transforms.Compose( 23 | [transforms.ToTensor(), 24 | transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))]) 25 | 26 | trainset = torchvision.datasets.CIFAR10(root='./data', train=True, 27 | download=True, transform=transform) 28 | # trainloader = DataLoader(trainset, batch_size=64, 29 | # shuffle=True, num_workers=2) 30 | 31 | testset = torchvision.datasets.CIFAR10(root='./data', train=False, 32 | download=True, transform=transform) 33 | # testloader = DataLoader(testset, batch_size=64, 34 | # shuffle=False, num_workers=2) 35 | 36 | train = trainset.data.transpose(0, 3, 1, 2) 37 | test = testset.data.transpose(0, 3, 1, 2) 38 | 39 | 40 | def hog_extraction(data, size=1): 41 | """ 42 | 功能:提取图像HOG特征 43 | 44 | 输入: 45 | data:(numpy array)输入数据[num,3,32,32] 46 | size:(int)(size,size)为提取HOG特征的cellsize 47 | 48 | 输出: 49 | data_hogfeature:(numpy array):data的HOG特征[num,dim] 50 | """ 51 | num = data.shape[0] 52 | data = data.astype('uint8') 53 | # 提取训练样本的HOG特征 54 | data1_hogfeature = [] 55 | for i in range(num): 56 | x = data[i] 57 | r = Image.fromarray(x[0]) 58 | g = Image.fromarray(x[1]) 59 | b = Image.fromarray(x[2]) 60 | 61 | # 合并三通道 62 | img = Image.merge("RGB", (r, g, b)) 63 | # 转为灰度图 64 | gray = img.convert('L') 65 | # out=gray.resize((100,100),Image.ANTIALIAS) 66 | # 转化为array 67 | gray_array = np.array(gray) 68 | # 提取HOG特征 69 | hogfeature = ft.hog(gray_array, pixels_per_cell=(size, size)) 70 | print(hogfeature) 71 | data1_hogfeature.append(hogfeature) 72 | 73 | # 把data1_hogfeature中的特征按行堆叠 74 | data_hogfeature = np.reshape(np.concatenate(data1_hogfeature), [num, -1]) 75 | return data_hogfeature 76 | 77 | 78 | class Kmeans(): 79 | def __init__(self, dat, k): 80 | data = scale(dat) 81 | self.data = data 82 | self.row, self.col = data.shape 83 | self.k = k 84 | self.centers = np.ndarray((k, self.col)) 85 | choices = random.choices(range(self.row), k=k) 86 | for i in range(k): 87 | self.centers[i, :] = self.data[choices[i], :] 88 | 89 | def fit(self): 90 | count = 0 91 | while (count < 15): 92 | self.labels = np.zeros((self.row)) 93 | for i in range(self.data.shape[0]): 94 | dis = [] 95 | for j in range(self.k): 96 | dis.append(np.linalg.norm(self.data[i, :] - self.centers[j, :], axis=0)) 97 | lab = np.argmin(dis, axis=0) 98 | self.labels[i] = lab 99 | self.result = {} 100 | for i in range(self.k): 101 | type = np.where(self.labels == i)[0] 102 | self.result[i] = type 103 | if len(type) == 0: 104 | self.centers[i, :] = 0 105 | else: 106 | self.centers[i, :] = np.mean(self.data[type, :], axis=0) 107 | count += 1 108 | return self.centers, self.result, self.labels 109 | 110 | def imshow(self): 111 | tsne = TSNE(n_components=2, learning_rate=100).fit_transform(self.data) 112 | pca = PCA().fit_transform(self.data) 113 | plt.figure(figsize=(12, 6)) 114 | plt.subplot(121) 115 | plt.scatter(tsne[:, 0], tsne[:, 1], c=self.labels) 116 | plt.title('t-SNE') 117 | plt.subplot(122) 118 | plt.scatter(pca[:, 0], pca[:, 1], c=self.labels) 119 | plt.title('PCA') 120 | plt.colorbar() 121 | plt.show() 122 | 123 | 124 | hog_train = hog_extraction(train, size=4) 125 | hog_val = hog_extraction(test, size=4) 126 | 127 | # kmeans=Kmeans(hog_val,10) 128 | # centers,results,labels=kmeans.fit() 129 | # kmeans.imshow() 130 | # s = silhouette_score(hog_val, testset.targets) 131 | # print(centers) 132 | # print(results) 133 | # print(s) 134 | 135 | 136 | 137 | clf=svm.SVC() 138 | #训练 139 | clf.fit(hog_val,testset.targets) 140 | #预测验证集类别 141 | result=clf.predict(hog_val) 142 | #计算验证集精度 143 | score=clf.score(hog_val,testset.targets) 144 | print(result) 145 | print(score) 146 | -------------------------------------------------------------------------------- /人工智能概论/遗传算法.py: -------------------------------------------------------------------------------- 1 | import math 2 | import random 3 | 4 | class GeneticAlgorithm(object): 5 | 6 | chrNum = 10 # 染色体数目 7 | ipop = [] # 种群 8 | generation = 0 # 染色体代号 9 | GENE = 46 # 基因数 10 | bestFitness = pow(3.4028234663852886*10, 38) # 函数最优解 11 | bestGenerations = 0 # 所有子代与父代中最好的染色体 12 | bestStr = [] # 最优解的染色体的二进制码 13 | 14 | # 初始化一条染色体(二进制字符串) 15 | def initChr(self): 16 | res = "" 17 | for i in range(self.GENE): 18 | if (random.random() > 0.5): 19 | res += "0" 20 | else: 21 | res += "1" 22 | return res 23 | 24 | # 初始化一个种群(10条染色体) 25 | def initPop(self): 26 | ipop_temp = [] 27 | for i in range(self.chrNum): 28 | ipop_temp.append(self.initChr()) 29 | return ipop_temp 30 | 31 | # 将染色体转换成x,y变量的值 32 | def calculateFitnessvalue(self, str): 33 | # 二进制数前23位为x的二进制字符串,后23位为y的二进制字符串 34 | a = int(str[0:23], 2) 35 | b = int(str[23:46], 2) 36 | x = a * (6.0 - 0) / (pow(2, 23) - 1) # x的基因 37 | y = b * (6.0 - 0) / (pow(2, 23) - 1) # y的基因 38 | # 需要优化的函数 39 | fitness = 3 - math.sin(2 * x) * math.sin(2 * x) - math.sin(2 * y) * math.sin(2 * y) 40 | returns = [x, y, fitness] 41 | return returns 42 | 43 | ''' 44 | 轮盘选择 45 | 计算群体上每个个体的适应度值; 46 | 按由个体适应度值所决定的某个规则选择将进入下一代的个体; 47 | ''' 48 | def select(self): 49 | evals = [] # 所有染色体适应值 50 | p = [] # 各染色体选择概率 51 | q = [] # 累计概率 52 | F = 0 # 累计适应值总和 53 | for i in range(self.chrNum): 54 | evals.append(self.calculateFitnessvalue(self.ipop[i])[2]) 55 | if evals[i] < self.bestFitness: # 记录种群中的最小值(最优解) 56 | self.bestFitness = evals[i] 57 | self.bestGenerations = self.generation 58 | self.bestStr = self.ipop[i] 59 | F = F + evals[i] # 所有染色体适应值总和 60 | for i in range(self.chrNum): 61 | p.append(evals[i] / F) 62 | if i == 0: 63 | q.append(p[i]) 64 | else: 65 | q.append(q[i-1]+p[i]) 66 | for i in range(self.chrNum): 67 | r = random.random() 68 | if r <= q[0]: 69 | self.ipop[i] = self.ipop[0] 70 | else: 71 | for j in range(1, self.chrNum): 72 | if r < q[j]: 73 | self.ipop[i] = self.ipop[j] 74 | 75 | ''' 76 | 交叉操作 77 | 交叉率为60%,平均为60%的染色体进行交叉 78 | ''' 79 | def cross(self): 80 | for i in range(self.chrNum): 81 | if random.random() < 0.60: 82 | pos = int(random.random()*self.GENE + 1) # pos位点前后二进制串交叉 83 | 84 | temp1 = self.ipop[i][0:pos] + self.ipop[(i+1) % self.chrNum][pos:] 85 | temp2 = self.ipop[(i + 1) % self.chrNum][0:pos] + self.ipop[i][pos:] 86 | 87 | self.ipop[i] = temp1 88 | self.ipop[(i+1) // self.chrNum] = temp2 89 | 90 | ''' 91 | 基因突变 92 | 1%基因变异 93 | ''' 94 | def mutation(self): 95 | for i in range(0, 4): 96 | num = int(random.random() * self.GENE * self.chrNum + 1) 97 | chromosomeNum = int(num / self.GENE) + 1 # 染色体号 98 | mutationNum = num - (chromosomeNum - 1) * self.GENE # 基因号 99 | if mutationNum == 0: 100 | mutationNum = 1 101 | chromosomeNum = chromosomeNum - 1 102 | if chromosomeNum >= self.chrNum: 103 | chromosomeNum = 9 104 | a = "" # 记录变异位点变异后的编码 105 | temp = "" 106 | 107 | if self.ipop[chromosomeNum][mutationNum - 1] == '0': # 当变异位点为0时 108 | a = '1' 109 | else: 110 | a = '0' 111 | if mutationNum == 1: # 当变异位点在首、中段和尾时的突变情况 112 | temp = a + self.ipop[chromosomeNum][mutationNum:] 113 | else: 114 | if mutationNum != self.GENE: 115 | temp = self.ipop[chromosomeNum][0:mutationNum-1] + a + self.ipop[chromosomeNum][mutationNum:] 116 | else: 117 | temp = self.ipop[chromosomeNum][0:mutationNum-1] + a 118 | self.ipop[chromosomeNum] = temp # 记录下变异后的染色体 119 | 120 | 121 | if __name__ == '__main__': 122 | GA = GeneticAlgorithm() 123 | GA.ipop = GA.initPop() # 产生初始种群 124 | string = "" 125 | 126 | for i in range(100000): # 迭代次数 127 | GA.select() 128 | GA.cross() 129 | GA.mutation() 130 | GA.generation = i 131 | 132 | x = GA.calculateFitnessvalue(GA.bestStr) 133 | string = "最小值" + str(GA.bestFitness) + '\n' + "第" + str(GA.bestGenerations) + "个染色体:<" + str(GA.bestStr) + ">" + '\n' + "x=" + str(x[0]) + '\n' + "y=" + str(x[1]) 134 | print(string) 135 | -------------------------------------------------------------------------------- /机器学习/贝叶斯分类2.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "id": "8492802c", 7 | "metadata": {}, 8 | "outputs": [], 9 | "source": [ 10 | "import torch\n", 11 | "import torch.nn as nn\n", 12 | "import torch.optim as optim\n", 13 | "import torch.nn.functional as F\n", 14 | "import torchvision\n", 15 | "import sklearn\n", 16 | "import numpy as np\n", 17 | "import pandas as pd \n", 18 | "from sklearn import datasets\n", 19 | "from sklearn.model_selection import train_test_split\n", 20 | "from sklearn.preprocessing import OneHotEncoder\n", 21 | "import matplotlib.pyplot as plt\n", 22 | "import time" 23 | ] 24 | }, 25 | { 26 | "cell_type": "code", 27 | "execution_count": 2, 28 | "id": "e0bd774d", 29 | "metadata": {}, 30 | "outputs": [], 31 | "source": [ 32 | "start=time.time()\n", 33 | "file=pd.read_csv('D:/桌面/sonar.csv',header=None)\n", 34 | "data=file.iloc[:,:40]\n", 35 | "target=file.iloc[:,-1]\n", 36 | "data=np.array(data,dtype=float)\n", 37 | "target=pd.get_dummies(target).iloc[:,0]\n", 38 | "data=np.array(data,dtype=float)\n", 39 | "target=np.array(target,dtype=float)\n", 40 | "x_train,x_test,y_train,y_test=train_test_split(data,target,test_size=0.25,random_state=5)" 41 | ] 42 | }, 43 | { 44 | "cell_type": "code", 45 | "execution_count": 3, 46 | "id": "d877b7bf", 47 | "metadata": {}, 48 | "outputs": [], 49 | "source": [ 50 | "def gaussion_pdf(x_test, x):\n", 51 | " temp1 = (x_test - x.mean(0)) * (x_test - x.mean(0))\n", 52 | " temp2 = x.std(0) * x.std(0)\n", 53 | " return np.exp(-temp1 / (2 * temp2)) / np.sqrt(2 * np.pi * temp2)" 54 | ] 55 | }, 56 | { 57 | "cell_type": "code", 58 | "execution_count": 4, 59 | "id": "b9ce2ca7", 60 | "metadata": {}, 61 | "outputs": [], 62 | "source": [ 63 | "def predict(x_train,y_train,x_test):\n", 64 | " assert len(x_test.shape) == 2\n", 65 | " classes = np.unique(y_train)\n", 66 | " pred_probs = []\n", 67 | " for i in classes:\n", 68 | " idx_i = y_train == i\n", 69 | " p_y = len(idx_i) / len(y_train)\n", 70 | " p_x_y = np.prod(gaussion_pdf(x_test,x_train[idx_i]), 1)\n", 71 | " prob_i = p_y * p_x_y\n", 72 | " pred_probs.append(prob_i)\n", 73 | " pred_probs = np.vstack(pred_probs)\n", 74 | " label_idx = pred_probs.argmax(0)\n", 75 | " y_pred = classes[label_idx]\n", 76 | " return y_pred" 77 | ] 78 | }, 79 | { 80 | "cell_type": "code", 81 | "execution_count": 5, 82 | "id": "97cbbf15", 83 | "metadata": {}, 84 | "outputs": [], 85 | "source": [ 86 | "y_predict=predict(x_train,y_train,x_test)" 87 | ] 88 | }, 89 | { 90 | "cell_type": "code", 91 | "execution_count": 6, 92 | "id": "2ada3922", 93 | "metadata": {}, 94 | "outputs": [], 95 | "source": [ 96 | "accuracy=(y_predict==y_test).sum().item()/len(y_test)" 97 | ] 98 | }, 99 | { 100 | "cell_type": "code", 101 | "execution_count": 7, 102 | "id": "fca6e4e8", 103 | "metadata": {}, 104 | "outputs": [ 105 | { 106 | "name": "stdout", 107 | "output_type": "stream", 108 | "text": [ 109 | "准确率为63.46%,用时0.04s\n" 110 | ] 111 | } 112 | ], 113 | "source": [ 114 | "print('准确率为{:.2f}%,用时{:.2f}s'.format(accuracy*100.0,time.time()-start))" 115 | ] 116 | }, 117 | { 118 | "cell_type": "code", 119 | "execution_count": null, 120 | "id": "91c26646", 121 | "metadata": {}, 122 | "outputs": [], 123 | "source": [] 124 | } 125 | ], 126 | "metadata": { 127 | "kernelspec": { 128 | "display_name": "Python 3", 129 | "language": "python", 130 | "name": "python3" 131 | }, 132 | "language_info": { 133 | "codemirror_mode": { 134 | "name": "ipython", 135 | "version": 3 136 | }, 137 | "file_extension": ".py", 138 | "mimetype": "text/x-python", 139 | "name": "python", 140 | "nbconvert_exporter": "python", 141 | "pygments_lexer": "ipython3", 142 | "version": "3.8.8" 143 | }, 144 | "toc": { 145 | "base_numbering": 1, 146 | "nav_menu": {}, 147 | "number_sections": true, 148 | "sideBar": true, 149 | "skip_h1_title": false, 150 | "title_cell": "Table of Contents", 151 | "title_sidebar": "Contents", 152 | "toc_cell": false, 153 | "toc_position": {}, 154 | "toc_section_display": true, 155 | "toc_window_display": false 156 | }, 157 | "varInspector": { 158 | "cols": { 159 | "lenName": 16, 160 | "lenType": 16, 161 | "lenVar": 40 162 | }, 163 | "kernels_config": { 164 | "python": { 165 | "delete_cmd_postfix": "", 166 | "delete_cmd_prefix": "del ", 167 | "library": "var_list.py", 168 | "varRefreshCmd": "print(var_dic_list())" 169 | }, 170 | "r": { 171 | "delete_cmd_postfix": ") ", 172 | "delete_cmd_prefix": "rm(", 173 | "library": "var_list.r", 174 | "varRefreshCmd": "cat(var_dic_list()) " 175 | } 176 | }, 177 | "types_to_exclude": [ 178 | "module", 179 | "function", 180 | "builtin_function_or_method", 181 | "instance", 182 | "_Feature" 183 | ], 184 | "window_display": false 185 | } 186 | }, 187 | "nbformat": 4, 188 | "nbformat_minor": 5 189 | } 190 | -------------------------------------------------------------------------------- /智能系统实验/google图像分类.py: -------------------------------------------------------------------------------- 1 | import torch 2 | import torchvision 3 | import torch.nn as nn 4 | import torch.nn.functional as F 5 | import torch.optim as optim 6 | from torchvision import transforms 7 | from torchvision import datasets 8 | from torch.utils.data import DataLoader 9 | import matplotlib.pyplot as plt 10 | 11 | # classes = ['airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck'] 12 | 13 | transform = transforms.Compose([ 14 | transforms.ToTensor(), 15 | transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)) 16 | ]) 17 | 18 | trainset = datasets.CIFAR10('./data/CIFAR10/cifar-10-python', 19 | download=True, 20 | train=True, 21 | transform=transform) 22 | testset = datasets.CIFAR10('./data/CIFAR10/cifar-10-python', 23 | download=True, 24 | train=False, 25 | transform=transform) 26 | 27 | trainloader = DataLoader(trainset, batch_size=64, 28 | shuffle=True, num_workers=0) 29 | 30 | testloader = DataLoader(testset, batch_size=64, 31 | shuffle=False, num_workers=0) 32 | 33 | 34 | class InceptionA(nn.Module): 35 | def __init__(self, in_channels): 36 | super(InceptionA, self).__init__() 37 | self.branch3x3_1 = nn.Conv2d(in_channels, 16, kernel_size=1) 38 | self.branch3x3_2 = nn.Conv2d(16, 24, kernel_size=3, padding=1) 39 | self.branch3x3_3 = nn.Conv2d(24, 24, kernel_size=3, padding=1) 40 | 41 | self.branch5x5_1 = nn.Conv2d(in_channels, 16, kernel_size=1) 42 | self.branch5x5_2 = nn.Conv2d(16, 24, kernel_size=5, padding=2) 43 | 44 | self.branch1x1 = nn.Conv2d(in_channels, 16, kernel_size=1) 45 | 46 | self.branch_pool = nn.Conv2d(in_channels, 24, kernel_size=1) 47 | 48 | def forward(self, x): 49 | branch3x3 = self.branch3x3_1(x) 50 | branch3x3 = self.branch3x3_2(branch3x3) 51 | branch3x3 = self.branch3x3_3(branch3x3) 52 | 53 | branch5x5 = self.branch5x5_1(x) 54 | branch5x5 = self.branch5x5_2(branch5x5) 55 | 56 | branch1x1 = self.branch1x1(x) 57 | 58 | branch_pool = F.avg_pool2d(x, kernel_size=3, stride=1, padding=1) 59 | branch_pool = self.branch_pool(branch_pool) 60 | 61 | outputs = [branch1x1, branch5x5, branch3x3, branch_pool] 62 | return torch.cat(outputs, dim=1) 63 | 64 | 65 | class Net(torch.nn.Module): 66 | def __init__(self): 67 | super(Net, self).__init__() 68 | self.conv1 = torch.nn.Conv2d(3, 10, kernel_size=5) 69 | self.conv2 = torch.nn.Conv2d(88, 20, kernel_size=5) 70 | 71 | self.incep1 = InceptionA(in_channels=10) 72 | self.incep2 = InceptionA(in_channels=20) 73 | 74 | self.mp = torch.nn.MaxPool2d(2) 75 | self.fc = torch.nn.Linear(2200, 10) 76 | 77 | def forward(self, x): 78 | x = F.relu(self.mp(self.conv1(x))) 79 | x = self.incep1(x) 80 | x = F.relu(self.mp(self.conv2(x))) 81 | x = self.incep2(x) 82 | x = x.view(x.size()[0], -1) 83 | x = self.fc(x) 84 | return x 85 | 86 | 87 | def train(): 88 | for batch_idx, (data, target) in enumerate(trainloader): 89 | data, target = data.to(device), target.to(device) 90 | optimizer.zero_grad() 91 | output = model(data) 92 | loss = criterion(output, target) 93 | loss.backward() 94 | optimizer.step() 95 | if batch_idx % log_interval == 0: 96 | print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format( 97 | epoch, batch_idx * len(data), len(trainloader.dataset), 98 | 100. * batch_idx / len(trainloader), loss.item())) 99 | 100 | 101 | def measure(): 102 | model.eval() 103 | test_loss = 0 104 | correct = 0 105 | with torch.no_grad(): 106 | for data, target in testloader: 107 | data = data.to(device) 108 | target = target.to(device) 109 | output = model(data) 110 | test_loss += criterion(output, target).item() 111 | pred = output.argmax(dim=1, keepdim=True) 112 | correct += pred.eq(target.view_as(pred)).sum().item() 113 | 114 | test_loss /= len(testloader.dataset) 115 | 116 | print('\nTest set: Average loss: {:.4f}, Accuracy: {}/{} ({:.2f}%)\n'.format( 117 | test_loss, correct, len(testloader.dataset), 118 | 100. * correct / len(testloader.dataset))) 119 | return test_loss, 100. * correct / len(testloader.dataset), model.state_dict() 120 | 121 | 122 | device = torch.device("cuda" if torch.cuda.is_available() else "cpu") 123 | model = Net().to(device) 124 | criterion = nn.CrossEntropyLoss() 125 | optimizer = optim.Adadelta(model.parameters(), lr=1.0) 126 | # optimizer=optim.SGD(model.parameters(),lr=0.01,momentum=0.5) 127 | scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(optimizer, mode="max", 128 | factor=0.45, patience=3) 129 | 130 | log_interval = 300 131 | epochs = 20 132 | 133 | epoch_list, ls_list, accuracy_list = [], [], [] 134 | if __name__ == '__main__': 135 | max_acc = float('0') 136 | for epoch in range(1, epochs + 1): 137 | train() 138 | ls, accuracy, net_dict = measure() 139 | scheduler.step(accuracy) 140 | 141 | epoch_list.append(epoch) 142 | ls_list.append(ls) 143 | accuracy_list.append(accuracy) 144 | 145 | if accuracy > max_acc: 146 | max_acc = accuracy 147 | torch.save({'epoch_record': epoch, 'model': net_dict}, 148 | f'./google分类模型_{max_acc}%.pth') 149 | 150 | fig = plt.figure(figsize=(20, 8)) 151 | plt.subplot(121) 152 | plt.plot(epoch_list, ls_list, linestyle=':') 153 | plt.xlabel('epoch') 154 | plt.ylabel('loss') 155 | plt.subplot(122) 156 | plt.plot(epoch_list, accuracy_list, linestyle=':') 157 | plt.xlabel('epoch ') 158 | plt.ylabel('accuracy') 159 | plt.savefig('./google.png') 160 | plt.show() 161 | -------------------------------------------------------------------------------- /深度学习/OCR_ALL/main_simple.py: -------------------------------------------------------------------------------- 1 | import torch 2 | from torchvision import transforms 3 | import torch.nn as nn 4 | import torch.nn.functional as func 5 | import torch.optim as optim 6 | import os 7 | import numpy as np 8 | import pandas as pd 9 | from PIL import Image 10 | from torch.optim.lr_scheduler import StepLR 11 | from torch.utils.data import Dataset 12 | from torchvision.transforms import ToTensor 13 | 14 | 15 | #加载数据集 16 | class MyDataset(Dataset): 17 | 18 | def __init__(self, path, mode="test", transformer=ToTensor()): 19 | info_path = f"{path}/{mode}_info.txt" 20 | path = f"{path}/{mode}" 21 | info = pd.read_csv(info_path, sep=" ", header=None) 22 | labels = np.array(info.iloc[:-1, 0]) 23 | img_paths = [] 24 | for i, label in enumerate(labels): 25 | for file_name in os.listdir(f"{path}/{label}"): 26 | img_paths.append((i, f"{path}/{label}/{file_name}")) 27 | self.img_path = img_paths 28 | self.transformer = transformer 29 | 30 | def __len__(self): 31 | return len(self.img_path) 32 | 33 | def __getitem__(self, idx): 34 | i, path = self.img_path[idx] 35 | image = Image.open(path) 36 | return self.transformer(image), i 37 | 38 | #设置网络结构 39 | class Net(nn.Module): 40 | def __init__(self): 41 | super(Net, self).__init__() 42 | self.conv1 = nn.Conv2d(1, 3, 3, 1, 1) 43 | self.conv2 = nn.Conv2d(3, 16, 3, 1, 1) 44 | self.conv3 = nn.Conv2d(16, 32, 3, 1, 1) 45 | self.conv4 = nn.Conv2d(32, 64, 3, 1, 1) 46 | self.conv5 = nn.Conv2d(64, 128, 3, 1, 1) 47 | self.dropout1 = nn.Dropout(0.25) 48 | self.dropout2 = nn.Dropout(0.25) 49 | self.dropout3 = nn.Dropout(0.25) 50 | self.dropout4 = nn.Dropout(0.25) 51 | self.dropout5 = nn.Dropout(0.25) 52 | self.fc1 = nn.Linear(128*4*4, 4096) 53 | self.fc2 = nn.Linear(4096, 4096) 54 | self.fc3 = nn.Linear(4096, 1024) 55 | self.fc4 = nn.Linear(1024, 500) 56 | 57 | def forward(self, x): 58 | x = self.conv1(x) 59 | x = func.relu(x) 60 | x = func.max_pool2d(x, 2) # 64 61 | x = self.conv2(x) 62 | x = func.relu(x) 63 | x = func.max_pool2d(x, 2) # 32 64 | x = self.dropout1(x) 65 | x = self.conv3(x) 66 | x = func.relu(x) 67 | x = func.max_pool2d(x, 2) # 16 68 | x = self.dropout2(x) 69 | x = self.conv4(x) 70 | x = func.relu(x) 71 | x = func.max_pool2d(x, 2) # 8 72 | x = self.dropout3(x) 73 | x = self.conv5(x) 74 | x = func.relu(x) 75 | x = func.max_pool2d(x, 2) # 4 76 | x = self.dropout4(x) 77 | x = torch.flatten(x, 1) 78 | x = self.fc1(x) 79 | x = func.relu(x) 80 | x = self.dropout3(x) 81 | x = self.fc2(x) 82 | x = func.relu(x) 83 | x = self.fc3(x) 84 | x = func.relu(x) 85 | x = self.fc4(x) 86 | output = func.log_softmax(x, dim=1) 87 | return output 88 | 89 | 90 | def train(model, device, train_loader, optimizer, epoch, criterion): 91 | model.train() 92 | for i, (data, target) in enumerate(train_loader): 93 | data, target = data.to(device), target.to(device) 94 | optimizer.zero_grad() 95 | output = model(data) 96 | loss = criterion(output, target) 97 | loss.backward() 98 | optimizer.step() 99 | if i % 100 == 0: 100 | print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format( 101 | epoch, i * len(data), len(train_loader.dataset), 102 | 100. * i / len(train_loader), loss.item())) 103 | 104 | 105 | def test(model, device, test_loader, criterion): 106 | model.eval() 107 | test_loss = 0 108 | correct = 0 109 | with torch.no_grad(): 110 | for data, target in test_loader: 111 | data, target = data.to(device), target.to(device) 112 | output = model(data) 113 | test_loss += criterion(output, target).item() # sum up batch loss 114 | pred = output.argmax(dim=1, keepdim=True) # get the index of the max log-probability 115 | correct += pred.eq(target.view_as(pred)).sum().item() 116 | 117 | test_loss /= len(test_loader.dataset) 118 | 119 | print('\nTest set: Average loss: {:.4f}, Accuracy: {}/{} ({:.0f}%)\n'.format( 120 | test_loss, correct, len(test_loader.dataset), 121 | 100. * correct / len(test_loader.dataset))) 122 | return correct / len(test_loader.dataset) 123 | 124 | 125 | #是否可用gpu 126 | use_cuda = torch.cuda.is_available() 127 | #设置随机数种子 128 | if use_cuda: 129 | torch.cuda.manual_seed(1) 130 | else: 131 | torch.manual_seed(1) 132 | 133 | device = torch.device("cuda" if use_cuda else "cpu") 134 | transform = transforms.Compose([ 135 | transforms.Resize((128, 128)), 136 | transforms.ToTensor(), 137 | lambda _: _.mean(0).unsqueeze(0), 138 | transforms.Normalize((0.1307,), (0.3081,)), 139 | ]) 140 | 141 | # datasets 142 | train_dataset = MyDataset("./dataset", "train", transformer = transform) 143 | test_dataset = MyDataset("./dataset", "test", transformer = transform) 144 | 145 | # dataloaders 146 | trainloader = torch.utils.data.DataLoader(train_dataset, batch_size=4, 147 | shuffle=True, num_workers=0) 148 | 149 | testloader = torch.utils.data.DataLoader(test_dataset, batch_size=4, 150 | shuffle=False, num_workers=0) 151 | #交叉熵损失 152 | criterion = nn.CrossEntropyLoss() 153 | 154 | 155 | net = torch.load(r"D:/RZZ_net7_0.9072.pt") 156 | 157 | if use_cuda: 158 | model = nn.DataParallel(net).cuda() 159 | else: 160 | model = net.to(device) 161 | 162 | #optimizer = optim.SGD(model.parameters(), lr=0.001, momentum=0.9) 163 | optimizer = optim.Adadelta(model.parameters(), lr=1.0) 164 | 165 | #设置不断缩小步长 166 | scheduler = StepLR(optimizer, step_size=1, gamma=0.7) 167 | for epoch in range(1, 10): 168 | train(model, device, trainloader, optimizer, epoch, criterion) 169 | test_acc = test(model, device, testloader, criterion) 170 | torch.save(model, f"rzz_net{epoch}_{test_acc:.4f}.pth") # 每轮保存模型 171 | scheduler.step() 172 | -------------------------------------------------------------------------------- /机器学习/price_predict.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding: utf-8 3 | 4 | 5 | import sklearn 6 | from sklearn import metrics 7 | from sklearn.model_selection import train_test_split 8 | import pandas as pd 9 | import numpy as np 10 | import torch 11 | import torch.nn as nn 12 | import torch.nn.functional as F 13 | from torch.utils.data import DataLoader 14 | from torch.utils.data import TensorDataset 15 | import matplotlib.pyplot as plt 16 | import seaborn as sns 17 | from skimage.metrics import mean_squared_error 18 | from sklearn.metrics import r2_score 19 | from sklearn.linear_model import LinearRegression 20 | 21 | column_names = ['CRIM', 'ZN', 'INDUS', 'CHAS', 'NOX', 'RM', 'AGE', 'DIS', 'RAD', 'TAX', 'PTRATIO', 'B', 'LSTAT', 22 | 'PRICE'] 23 | all_data = pd.read_csv('./housing.csv', header=None, delimiter=r"\s+", names=column_names) 24 | 25 | all_data.hist() 26 | plt.show() 27 | 28 | all_data.describe() 29 | 30 | plt.figure(figsize=(20, 10)) 31 | plt.boxplot(all_data) 32 | plt.show() 33 | 34 | corr = all_data.corr() 35 | 36 | plt.figure(figsize=(20, 10)) 37 | sns.heatmap(corr, annot=True, cmap='twilight_r') 38 | 39 | data = all_data.iloc[:, :-1] 40 | label = all_data.iloc[:, -1] 41 | 42 | data = np.array(data, dtype=float) 43 | label = np.array(label, dtype=float) 44 | 45 | for i in range(13): 46 | plt.figure(figsize=(10, 7)) 47 | plt.grid() 48 | plt.scatter(data[:, i], label, s=5) # 横纵坐标和点的大小 49 | plt.title(column_names[i]) 50 | plt.show() 51 | 52 | unsF = [] # 次要特征下标 53 | for i in range(data.shape[1]): 54 | if column_names[i] == 'CHAS': 55 | unsF.append(i) 56 | data = np.delete(data, unsF, axis=1) # 删除次要特征 57 | 58 | unsT = [] # 房价异常值下标 59 | for i in range(data.shape[1]): 60 | if label[i] > 46: 61 | unsT.append(i) 62 | data = np.delete(data, unsT, axis=0) # 删除样本异常值数据 63 | label = np.delete(label, unsT, axis=0) # 删除异常房价 64 | 65 | data = torch.tensor(data, dtype=torch.float) 66 | label = torch.tensor(label, dtype=torch.float) 67 | 68 | X_train, X_test, y_train, y_test = train_test_split(data, label, test_size=0.3, random_state=4) 69 | 70 | 71 | def train(model, device, train_loader, optimizer, epoch, criterion): 72 | model.train() 73 | loss = 0.0 74 | for i, (data, target) in enumerate(train_loader): 75 | data, target = data.to(device), target.to(device) 76 | optimizer.zero_grad() 77 | output = model(data) 78 | loss = criterion(output, target.view_as(output)) 79 | loss.backward() 80 | optimizer.step() 81 | if i % 100 == 0: 82 | print('Train Epoch: {} Loss: {:.6f}'.format( 83 | epoch, loss.item() / len(train_loader))) 84 | 85 | 86 | def test(model, device, test_loader, criterion): 87 | model.eval() 88 | test_loss = 0 89 | with torch.no_grad(): 90 | for data, target in test_loader: 91 | data, target = data.to(device), target.to(device) 92 | output = model(data) 93 | 94 | test_loss += criterion(output, target.view_as(output)).item() # sum up batch loss 95 | 96 | test_loss /= len(test_loader.dataset) 97 | 98 | print('Test set: Average loss: {:.4f}\n'.format( 99 | test_loss)) 100 | return test_loss 101 | 102 | 103 | class Net(nn.Module): 104 | def __init__(self): 105 | super(Net, self).__init__() 106 | self.fc1 = nn.Linear(12, 128) 107 | self.fc2 = nn.Linear(128, 1) 108 | 109 | def forward(self, x): 110 | x = self.fc1(x) 111 | x = F.relu(x) 112 | x = self.fc2(x) 113 | return x 114 | 115 | 116 | device = torch.device("cuda" if torch.cuda.is_available() else "cpu") 117 | model = Net().to(device) 118 | optimizer = torch.optim.Adam(params=model.parameters()) 119 | criterion = nn.MSELoss() 120 | 121 | trainset = TensorDataset(X_train, y_train) 122 | trainloader = DataLoader(trainset, batch_size=64, shuffle=True, num_workers=0) 123 | testset = TensorDataset(X_test, y_test) 124 | testloader = DataLoader(testset, batch_size=64, shuffle=False, num_workers=0) 125 | 126 | epoch_list, loss_list = [], [] 127 | 128 | for epoch in range(1, 1000): 129 | train(model, device, trainloader, optimizer, epoch, criterion) 130 | test_loss = test(model, device, testloader, criterion) 131 | epoch_list.append(epoch) 132 | loss_list.append(test_loss) 133 | 134 | fig = plt.figure(figsize=(20, 10)) 135 | plt.plot(epoch_list, loss_list) 136 | plt.xlabel('epoch') 137 | plt.ylabel('loss') 138 | plt.title('error') 139 | plt.show() 140 | 141 | 142 | def read(test_loader): 143 | model.eval() 144 | output_list, target_list = [], [] 145 | with torch.no_grad(): 146 | for data, target in test_loader: 147 | model.to('cpu') 148 | output = model(data).detach().cpu().numpy() 149 | output_list.extend(output) 150 | target_list.extend(target.cpu().numpy()) 151 | p = pd.DataFrame(output_list, columns=['predict']) 152 | p['real'] = target_list 153 | print(p.head()) 154 | return p 155 | 156 | 157 | p = read(testloader) 158 | 159 | error1 = mean_squared_error(p.iloc[:, 1], p.iloc[:, 0]).round(5) # 平方差 160 | score1 = r2_score(p.iloc[:, 1], p.iloc[:, 0]).round(5) # 相关系数 161 | 162 | plt.rcParams['font.family'] = "sans-serif" 163 | plt.rcParams['font.sans-serif'] = "SimHei" 164 | plt.rcParams['axes.unicode_minus'] = False 165 | fig1 = plt.figure(figsize=(20, 10)) 166 | plt.plot(range(p.shape[0]), p.iloc[:, 1], color='red', linewidth=1, linestyle='-') 167 | plt.plot(range(p.shape[0]), p.iloc[:, 0], color='blue', linewidth=1, linestyle='dashdot') 168 | plt.legend(['真实值', '预测值']) 169 | plt.title('神经网络预测值与准确率对比图') 170 | error1 = "标准差d=" + str(error1) + "\n" + "相关指数R^2=" + str(score1) 171 | plt.xlabel(error1, size=18, color="green") 172 | plt.grid() 173 | plt.show() 174 | 175 | lf = LinearRegression() 176 | lf.fit(X_train, y_train) # 训练数据,学习模型参数 177 | y_predict = lf.predict(X_test) 178 | 179 | error2 = mean_squared_error(y_test.numpy(), y_predict).round(5) # 平方差 180 | score2 = r2_score(y_test, y_predict).round(5) 181 | 182 | fig2 = plt.figure(figsize=(20, 10)) 183 | plt.plot(range(y_test.shape[0]), y_test, color='red', linewidth=1, linestyle='-') 184 | plt.plot(range(y_test.shape[0]), y_predict, color='blue', linewidth=1, linestyle='dashdot') 185 | plt.legend(['真实值', '预测值']) 186 | plt.title('线性模型预测值与准确率对比图') 187 | error2 = "标准差d=" + str(error2) + "\n" + "相关指数R^2=" + str(score2) 188 | plt.xlabel(error2, size=18, color="green") 189 | plt.grid() 190 | plt.show() 191 | -------------------------------------------------------------------------------- /深度学习/OCR_ALL/main.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | import argparse 3 | import torch 4 | import torch.nn as nn 5 | import torch.nn.functional as func 6 | import torch.optim as optim 7 | from torchvision import transforms 8 | from torch.optim.lr_scheduler import StepLR 9 | from dataset import MyDataset 10 | #import process 11 | 12 | class Net(nn.Module): 13 | def __init__(self): 14 | super(Net, self).__init__() 15 | self.conv1 = nn.Conv2d(1, 32, 3, 1) 16 | self.conv2 = nn.Conv2d(32, 64, 3, 1) 17 | self.conv3 = nn.Conv2d(64, 64, 3, 1, 1) 18 | self.dropout1 = nn.Dropout(0.25) 19 | self.dropout2 = nn.Dropout(0.25) 20 | self.dropout3 = nn.Dropout(0.5) 21 | self.fc1 = nn.Linear(64*15*15, 4096) 22 | self.fc2 = nn.Linear(4096, 1024) 23 | self.fc3 = nn.Linear(1024, 500) 24 | 25 | def forward(self, x): 26 | x = self.conv1(x) # 26 27 | x = func.relu(x) 28 | x = self.conv2(x) # 24 29 | x = func.relu(x) 30 | x = func.max_pool2d(x, 2) # 12 31 | x = self.dropout1(x) 32 | x = self.conv3(x) + x # 12 33 | x = func.relu(x) 34 | x = func.max_pool2d(x, 2) # 6 35 | x = self.dropout2(x) 36 | x = torch.flatten(x, 1) 37 | x = self.fc1(x) 38 | x = func.relu(x) 39 | x = self.dropout3(x) 40 | x = self.fc2(x) 41 | x = func.relu(x) 42 | x = self.fc3(x) 43 | output = func.log_softmax(x, dim=1) 44 | return output 45 | 46 | 47 | def train(args, model, device, train_loader, optimizer, epoch, criterion): 48 | model.train() 49 | for batch_idx, (data, target) in enumerate(train_loader): 50 | data, target = data.to(device), target.to(device) 51 | optimizer.zero_grad() 52 | output = model(data) 53 | loss = criterion(output, target) 54 | loss.backward() 55 | optimizer.step() 56 | if batch_idx % args.log_interval == 0: 57 | print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format( 58 | epoch, batch_idx * len(data), len(train_loader.dataset), 59 | 100. * batch_idx / len(train_loader), loss.item())) 60 | if args.dry_run: 61 | break 62 | 63 | 64 | def test(model, device, test_loader): 65 | model.eval() 66 | test_loss = 0 67 | correct = 0 68 | with torch.no_grad(): 69 | for data, target in test_loader: 70 | data, target = data.to(device), target.to(device) 71 | output = model(data) 72 | test_loss += func.nll_loss(output, target, reduction='sum').item() # sum up batch loss 73 | pred = output.argmax(dim=1, keepdim=True) # get the index of the max log-probability 74 | correct += pred.eq(target.view_as(pred)).sum().item() 75 | 76 | test_loss /= len(test_loader.dataset) 77 | 78 | print('\nTest set: Average loss: {:.4f}, Accuracy: {}/{} ({:.0f}%)\n'.format( 79 | test_loss, correct, len(test_loader.dataset), 80 | 100.0 * correct / len(test_loader.dataset))) 81 | return correct / len(test_loader.dataset) 82 | 83 | def main(): 84 | # Training settings 85 | parser = argparse.ArgumentParser(description='My OCR work') 86 | parser.add_argument('--batch-size', type=int, default=2048, metavar='N', 87 | help='input batch size for training (default: 64)') 88 | parser.add_argument('--test-batch-size', type=int, default=1000, metavar='N', 89 | help='input batch size for testing (default: 1000)') 90 | parser.add_argument('--epochs', type=int, default=60, metavar='N', 91 | help='number of epochs to train (default: 30)') 92 | parser.add_argument('--lr', type=float, default=1.0, metavar='LR', 93 | help='learning rate (default: 1.0)') 94 | parser.add_argument('--gamma', type=float, default=0.7, metavar='M', 95 | help='Learning rate step gamma (default: 0.7)') 96 | parser.add_argument('--no-cuda', action='store_true', default=False, 97 | help='disables CUDA training') 98 | parser.add_argument('--dry-run', action='store_true', default=False, 99 | help='quickly check a single pass') 100 | parser.add_argument('--seed', type=int, default=1, metavar='S', 101 | help='random seed (default: 1)') 102 | parser.add_argument('--log-interval', type=int, default=10, metavar='N', 103 | help='how many batches to wait before logging training status') 104 | parser.add_argument('--save-model', action='store_true', default=True, 105 | help='For Saving the current Model') 106 | args = parser.parse_args() 107 | use_cuda = not args.no_cuda and torch.cuda.is_available() 108 | 109 | torch.manual_seed(args.seed) 110 | 111 | device = torch.device("cuda" if use_cuda else "cpu") 112 | 113 | train_kwargs = {'batch_size': args.batch_size} 114 | test_kwargs = {'batch_size': args.test_batch_size} 115 | if use_cuda: 116 | cuda_kwargs = {'num_workers': 32, 117 | 'pin_memory': True, 118 | 'shuffle': True} 119 | train_kwargs.update(cuda_kwargs) 120 | test_kwargs.update(cuda_kwargs) 121 | 122 | transform = transforms.Compose([ 123 | transforms.Resize((64, 64)), 124 | transforms.ToTensor(), 125 | lambda _: _.mean(0).unsqueeze(0), 126 | transforms.Normalize((0.1307,), (0.3081,)), 127 | ]) 128 | dataset1 = MyDataset("./dataset", "train", transformer=transform) 129 | dataset2 = MyDataset("./dataset", "test", transformer=transform) 130 | train_loader = torch.utils.data.DataLoader(dataset1, **train_kwargs) 131 | test_loader = torch.utils.data.DataLoader(dataset2, **test_kwargs) 132 | 133 | if use_cuda: 134 | model = nn.DataParallel(Net()).cuda() 135 | else: 136 | model = Net().to(device) 137 | 138 | optimizer = optim.Adadelta(model.parameters(), lr=args.lr) 139 | #optimizer = optim.SGD(model.parameters(), lr=0.001, momentum=0.9) 140 | criterion = nn.CrossEntropyLoss() 141 | 142 | scheduler = StepLR(optimizer, step_size=1, gamma=args.gamma) 143 | for epoch in range(1, args.epochs + 1): 144 | train(args, model, device, train_loader, optimizer, epoch, criterion) 145 | acc=test(model, device, test_loader) 146 | if args.save_model: 147 | torch.save(model.state_dict(), f"RZZ_net{epoch}_{acc}.pt") 148 | scheduler.step() 149 | 150 | 151 | if __name__ == '__main__': 152 | main() 153 | 154 | -------------------------------------------------------------------------------- /数据挖掘/net_pattern mining.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import time 3 | import os 4 | import matplotlib.pyplot as plt 5 | import torch 6 | from torch import nn 7 | import torchvision.models as models 8 | import torchvision.transforms as transforms 9 | import numpy as np 10 | import torch 11 | import torchvision 12 | import torch.optim as optim 13 | import torch.backends.cudnn as cudnn 14 | 15 | savepath='./vis_resnet50/features' 16 | 17 | # if not os.path.exists(savepath): 18 | # os.mkdir(savepath) 19 | 20 | cudnn.benchmark = True # 对卷积进行加速 21 | 22 | def draw_features(width,height,x,savename): 23 | tic=time.time() 24 | fig = plt.figure(figsize=(16, 16)) 25 | fig.subplots_adjust(left=0.05, right=0.95, bottom=0.05, top=0.95, wspace=0.05, hspace=0.05) 26 | for i in range(width*height): 27 | plt.subplot(height,width, i + 1) 28 | plt.axis('off') 29 | # plt.tight_layout() 30 | img = x[0, i, :, :] 31 | pmin = np.min(img) 32 | pmax = np.max(img) 33 | img = (img - pmin) / (pmax - pmin + 0.000001) 34 | plt.imshow(img, cmap='gray') 35 | print("{}/{}".format(i,width*height)) 36 | fig.savefig(savename, dpi=100) 37 | fig.clf() 38 | plt.close() 39 | print("time:{}".format(time.time()-tic)) 40 | 41 | 42 | class ft_net(nn.Module): 43 | 44 | def __init__(self): 45 | super(ft_net, self).__init__() 46 | model_ft = models.resnet50(pretrained=True) 47 | self.model = model_ft 48 | 49 | def forward(self, x): 50 | if True: # draw features or not 51 | x = self.model.conv1(x) 52 | draw_features(8,8,x.cpu().detach().numpy(),"{}/f1_conv1.png".format(savepath)) 53 | 54 | x = self.model.bn1(x) 55 | draw_features(8, 8, x.cpu().detach().numpy(),"{}/f2_bn1.png".format(savepath)) 56 | 57 | x = self.model.relu(x) 58 | draw_features(8, 8, x.cpu().detach().numpy(), "{}/f3_relu.png".format(savepath)) 59 | 60 | x = self.model.maxpool(x) 61 | draw_features(8, 8, x.cpu().detach().numpy(), "{}/f4_maxpool.png".format(savepath)) 62 | 63 | x = self.model.layer1(x) 64 | draw_features(16, 16, x.cpu().detach().numpy(), "{}/f5_layer1.png".format(savepath)) 65 | 66 | x = self.model.layer2(x) 67 | draw_features(16, 32, x.cpu().detach().numpy(), "{}/f6_layer2.png".format(savepath)) 68 | 69 | x = self.model.layer3(x) 70 | draw_features(32, 32, x.cpu().detach().numpy(), "{}/f7_layer3.png".format(savepath)) 71 | 72 | x = self.model.layer4(x) 73 | draw_features(32, 32, x.cpu().detach().numpy()[:, 0:1024, :, :], "{}/f8_layer4_1.png".format(savepath)) 74 | draw_features(32, 32, x.cpu().detach().numpy()[:, 1024:2048, :, :], "{}/f8_layer4_2.png".format(savepath)) 75 | 76 | x = self.model.avgpool(x) 77 | plt.plot(np.linspace(1, 2048, 2048), x.cpu().detach().numpy()[0, :, 0, 0]) 78 | plt.savefig("{}/f9_avgpool.png".format(savepath)) 79 | plt.clf() 80 | plt.close() 81 | 82 | x = x.view(x.size(0), -1) 83 | x = self.model.fc(x) 84 | plt.plot(np.linspace(1, 1000, 1000), x.cpu().detach().numpy()[0, :]) 85 | plt.savefig("{}/f10_fc.png".format(savepath)) 86 | plt.clf() 87 | plt.close() 88 | else : 89 | x = self.model.conv1(x) 90 | x = self.model.bn1(x) 91 | x = self.model.relu(x) 92 | x = self.model.maxpool(x) 93 | x = self.model.layer1(x) 94 | x = self.model.layer2(x) 95 | x = self.model.layer3(x) 96 | x = self.model.layer4(x) 97 | x = self.model.avgpool(x) 98 | x = x.view(x.size(0), -1) 99 | x = self.model.fc(x) 100 | return x 101 | 102 | 103 | def train(): 104 | model.train() 105 | loss = 0.0 106 | for batch_idx, (data, target) in enumerate(trainloader): 107 | data, target = data.to(device), target.to(device) 108 | optimizer.zero_grad() 109 | output = model(data) 110 | loss = criterion(output, target) 111 | loss.backward() 112 | optimizer.step() 113 | if batch_idx % log_interval == 0: 114 | print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format( 115 | epoch, batch_idx * len(data), len(trainloader.dataset), 116 | 100. * batch_idx / len(trainloader), loss.item())) 117 | 118 | 119 | def test(): 120 | model.eval() 121 | test_loss = 0 122 | correct = 0 123 | with torch.no_grad(): 124 | for data, target in testloader: 125 | data, target = data.to(device), target.to(device) 126 | output = model(data) 127 | test_loss += criterion(output, target).item() 128 | pred = output.argmax(dim=1, keepdim=True) 129 | correct += pred.eq(target.view_as(pred)).sum().item() 130 | 131 | test_loss /= len(testloader.dataset) 132 | print('\nTest set: Average loss: {:.4f}, Accuracy: {}/{} ({:.2f}%)\n'.format( 133 | test_loss, correct, len(testloader.dataset), 134 | 100. * correct / len(testloader.dataset))) 135 | return 100. * correct / len(testloader.dataset), model.state_dict() 136 | 137 | 138 | transform = transforms.Compose( 139 | [transforms.ToTensor(), 140 | transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))]) 141 | 142 | trainset = torchvision.datasets.CIFAR10('./data', 143 | download=True, 144 | train=True, 145 | transform=transform) 146 | testset = torchvision.datasets.CIFAR10('./data', 147 | download=True, 148 | train=False, 149 | transform=transform) 150 | 151 | trainloader = torch.utils.data.DataLoader(trainset, batch_size=64, 152 | shuffle=True, num_workers=0) 153 | 154 | testloader = torch.utils.data.DataLoader(testset, batch_size=64, 155 | shuffle=False, num_workers=0) 156 | 157 | log_interval = 300 158 | epochs = 5 159 | 160 | device = torch.device("cuda" if torch.cuda.is_available() else "cpu") 161 | torch.manual_seed(1) 162 | criterion = nn.CrossEntropyLoss() 163 | model = ft_net().to(device) 164 | model = nn.DataParallel(model) 165 | 166 | # optimizer=torch.optim.SGD(model.parameters(),lr=lr,momentum=momentum) 167 | optimizer = optim.Adadelta(model.parameters(), lr=1.0) 168 | 169 | if __name__ == '__main__': 170 | max_acc = float('0') 171 | for epoch in range(1, epochs + 1): 172 | train() 173 | accuracy, net_dict = test() 174 | if accuracy > max_acc: 175 | max_acc = accuracy 176 | torch.save({'epoch_record': epoch, 'model': net_dict}, 177 | f'./网络特征提取模型_{max_acc}%.pth') -------------------------------------------------------------------------------- /智能系统实验/4_2/net_model.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import torch 3 | import torch.nn as nn 4 | import torch.nn.functional as F 5 | import matplotlib.pyplot as plt 6 | import torchvision 7 | 8 | 9 | class ICNET(nn.Module): 10 | def __init__(self): 11 | super(ICNET, self).__init__() 12 | self.conv1 = nn.Sequential( 13 | nn.Conv2d(3, 8, 3, 1, 1), 14 | nn.ReLU() 15 | ) 16 | self.conv2 = nn.Sequential( 17 | nn.Conv2d(8, 8, 3, 1, 1), 18 | nn.ReLU(), 19 | nn.MaxPool2d(2, 2) 20 | ) 21 | self.fc = nn.Sequential( 22 | nn.Linear(64 * 64 * 8, 1000), 23 | nn.ReLU(), 24 | nn.Linear(1000, 1000), 25 | nn.ReLU(), 26 | nn.Linear(1000, 2), 27 | nn.Softmax(dim=1) 28 | ) 29 | 30 | def forward(self, x): 31 | x = self.conv1(x) 32 | x = self.conv2(x) 33 | x = x.view(x.size()[0], -1) 34 | x = self.fc(x) 35 | # output = F.log_softmax(x, dim=1) 36 | return x 37 | 38 | 39 | class RESNET(nn.Module): 40 | def __init__(self): 41 | super(RESNET, self).__init__() 42 | self.model = torchvision.models.resnet34(pretrained=True) 43 | self.model.fc = nn.Linear(512, 2) 44 | 45 | def forward(self, x): 46 | return self.model(x) 47 | 48 | 49 | class Net1(nn.Module): 50 | def __init__(self): 51 | super().__init__() 52 | self.conv1 = nn.Conv2d(3, 32, 20, 4) 53 | self.norm1 = nn.BatchNorm2d(32) 54 | # nn.init.xavier_uniform_(self.conv1.weight) 55 | # MaxPool的移动步长默认为kernel_size 56 | self.maxpool1 = nn.MaxPool2d(kernel_size=2) # n*32*28*28-->n*32*14*14 57 | 58 | self.conv2 = nn.Conv2d(32, 64, 3, 1, 2) # n*32*14*14-->n*64*16*16 59 | self.norm2 = nn.BatchNorm2d(64) 60 | # nn.init.xavier_uniform_(self.conv2.weight) 61 | self.maxpool2 = nn.MaxPool2d(2, 2) # n*64*16*16-->n*64*8*8 62 | self.dropout1 = nn.Dropout(0.25) 63 | self.dropout2 = nn.Dropout(0.5) 64 | 65 | self.fc1 = nn.Linear(4096, 128) # n*4096-->n*4096 66 | self.fc2 = nn.Linear(128, 2) # n*4096-->n*10 67 | 68 | def forward(self, x): 69 | out = F.relu(self.conv1(x)) 70 | out = self.norm1(out) 71 | out = self.maxpool1(out) 72 | out = self.dropout1(out) 73 | 74 | out = F.relu(self.conv2(out)) 75 | out = self.norm2(out) 76 | out = self.maxpool2(out) 77 | out = self.dropout2(out) 78 | 79 | out = out.view(out.size(0), -1) 80 | out = F.relu(self.fc1(out)) 81 | out = self.fc2(out) 82 | return out 83 | 84 | 85 | class Residual(nn.Module): 86 | def __init__(self, in_channels, out_channels, use_1x1conv=False, stride=1): 87 | super(Residual, self).__init__() 88 | self.conv1 = nn.Conv2d(in_channels, out_channels, kernel_size=3, padding=1, stride=stride) 89 | self.conv2 = nn.Conv2d(out_channels, out_channels, kernel_size=3, padding=1) 90 | if use_1x1conv: 91 | self.conv3 = nn.Conv2d(in_channels, out_channels, kernel_size=1, stride=stride) 92 | else: 93 | self.conv3 = None 94 | self.bn1 = nn.BatchNorm2d(out_channels) 95 | self.bn2 = nn.BatchNorm2d(out_channels) 96 | 97 | def forward(self, X): 98 | Y = F.relu(self.bn1(self.conv1(X))) 99 | Y = self.bn2(self.conv2(Y)) 100 | if self.conv3: 101 | X = self.conv3(X) 102 | return F.relu(Y + X) 103 | 104 | 105 | def resnet_block(in_channels, out_channels, num_residuals, first_block=False): 106 | if first_block: 107 | assert in_channels == out_channels # 第一个模块的通道数同输入通道数一致 108 | blk = [] 109 | for i in range(num_residuals): 110 | if i == 0 and not first_block: 111 | blk.append(Residual(in_channels, out_channels, use_1x1conv=True, stride=2)) 112 | else: 113 | blk.append(Residual(out_channels, out_channels)) 114 | return nn.Sequential(*blk) 115 | 116 | 117 | class my_resnet(nn.Module): 118 | def __init__(self): 119 | super(my_resnet, self).__init__() 120 | self.net = nn.Sequential( 121 | nn.Conv2d(3, 32, kernel_size=(7, 7), stride=(1, 1), padding=(3, 3)), 122 | nn.BatchNorm2d(32), 123 | nn.ReLU(), 124 | nn.MaxPool2d(kernel_size=2, stride=2), 125 | 126 | resnet_block(32, 32, 2, True), 127 | resnet_block(32, 64, 2) 128 | ) 129 | 130 | self.fc = nn.Sequential( 131 | nn.Linear(64, 2), 132 | nn.Softmax(dim=1) 133 | ) 134 | 135 | def forward(self, x): 136 | x = self.net(x) 137 | # x = F.adaptive_avg_pool2d(x, (1, 1)) 138 | x = F.avg_pool2d(x, kernel_size=x.size()[2:]) 139 | x = x.view(x.size(0), -1) 140 | x = self.fc(x) 141 | return x 142 | 143 | 144 | def conv_block(in_channels, out_channels): 145 | blk = nn.Sequential(nn.BatchNorm2d(in_channels), 146 | nn.ReLU(), 147 | nn.Conv2d(in_channels, out_channels, kernel_size=3, padding=1)) 148 | return blk 149 | 150 | 151 | class DenseBlock(nn.Module): 152 | def __init__(self, num_convs, in_channels, out_channels): 153 | super(DenseBlock, self).__init__() 154 | net = [] 155 | for i in range(num_convs): 156 | in_c = in_channels + i * out_channels 157 | net.append(conv_block(in_c, out_channels)) 158 | self.net = nn.ModuleList(net) 159 | self.out_channels = in_channels + num_convs * out_channels # 计算输出通道数 160 | 161 | def forward(self, X): 162 | for blk in self.net: 163 | Y = blk(X) 164 | X = torch.cat((X, Y), dim=1) # 在通道维上将输入和输出连结 165 | return X 166 | 167 | 168 | def transition_block(in_channels, out_channels): 169 | blk = nn.Sequential( 170 | nn.BatchNorm2d(in_channels), 171 | nn.ReLU(), 172 | nn.Conv2d(in_channels, out_channels, kernel_size=1), 173 | nn.AvgPool2d(kernel_size=2, stride=2)) 174 | return blk 175 | 176 | 177 | class densenet(nn.Module): 178 | def __init__(self): 179 | super(densenet, self).__init__() 180 | self.net = nn.Sequential( 181 | nn.Conv2d(3, 64, kernel_size=(7, 7), stride=(1, 1), padding=(3, 3)), 182 | nn.BatchNorm2d(64), 183 | nn.ReLU(), 184 | nn.MaxPool2d(kernel_size=2, stride=2), 185 | 186 | DenseBlock(2, 64, 64), 187 | transition_block(192, 96), 188 | DenseBlock(2, 96, 64), 189 | transition_block(224, 112), 190 | 191 | ) 192 | 193 | self.fc = nn.Sequential( 194 | nn.Linear(112, 2), 195 | nn.Softmax(dim=1) 196 | ) 197 | 198 | def forward(self, x): 199 | x = self.net(x) 200 | # x = F.adaptive_avg_pool2d(x, (1, 1)) 201 | x = F.avg_pool2d(x, kernel_size=x.size()[2:]) 202 | x = x.view(x.size(0), -1) 203 | x = self.fc(x) 204 | return x 205 | 206 | 207 | net = densenet() 208 | x = torch.rand((1, 3, 128, 128)) 209 | 210 | X = net(x) 211 | print(X.shape) 212 | print(net) 213 | 214 | # TODO 干啥 -------------------------------------------------------------------------------- /图像超分辨项目/models.py: -------------------------------------------------------------------------------- 1 | import torch 2 | from torch import nn 3 | import torchvision 4 | import math 5 | 6 | 7 | class ConvolutionalBlock(nn.Module): 8 | """ 9 | 卷积模块,由卷积层, BN归一化层, 激活层构成. 10 | """ 11 | 12 | def __init__(self, in_channels, out_channels, kernel_size, stride=1, batch_norm=False, activation=None): 13 | """ 14 | :参数 in_channels: 输入通道数 15 | :参数 out_channels: 输出通道数 16 | :参数 kernel_size: 核大小 17 | :参数 stride: 步长 18 | :参数 batch_norm: 是否包含BN层 19 | :参数 activation: 激活层类型; 如果没有则为None 20 | """ 21 | super(ConvolutionalBlock, self).__init__() 22 | 23 | if activation is not None: 24 | activation = activation.lower() 25 | assert activation in {'prelu', 'leakyrelu', 'tanh'} 26 | 27 | # 层列表 28 | layers = list() 29 | 30 | # 1个卷积层 31 | layers.append( 32 | nn.Conv2d(in_channels=in_channels, out_channels=out_channels, kernel_size=kernel_size, stride=stride, 33 | padding=kernel_size // 2)) 34 | 35 | # 1个BN归一化层 36 | if batch_norm is True: 37 | layers.append(nn.BatchNorm2d(num_features=out_channels)) 38 | 39 | # 1个激活层 40 | if activation == 'prelu': 41 | layers.append(nn.PReLU()) 42 | elif activation == 'leakyrelu': 43 | layers.append(nn.LeakyReLU(0.2)) 44 | elif activation == 'tanh': 45 | layers.append(nn.Tanh()) 46 | 47 | # 合并层 48 | self.conv_block = nn.Sequential(*layers) 49 | 50 | def forward(self, input): 51 | """ 52 | 前向传播 53 | 54 | :参数 input: 输入图像集,张量表示,大小为 (N, in_channels, w, h) 55 | :返回: 输出图像集,张量表示,大小为(N, out_channels, w, h) 56 | """ 57 | output = self.conv_block(input) 58 | 59 | return output 60 | 61 | 62 | class SubPixelConvolutionalBlock(nn.Module): 63 | """ 64 | 子像素卷积模块, 包含卷积, 像素清洗和激活层. 65 | """ 66 | 67 | def __init__(self, kernel_size=3, n_channels=64, scaling_factor=2): 68 | """ 69 | :参数 kernel_size: 卷积核大小 70 | :参数 n_channels: 输入和输出通道数 71 | :参数 scaling_factor: 放大比例 72 | """ 73 | super(SubPixelConvolutionalBlock, self).__init__() 74 | 75 | # 首先通过卷积将通道数扩展为 scaling factor^2 倍 76 | self.conv = nn.Conv2d(in_channels=n_channels, out_channels=n_channels * (scaling_factor ** 2), 77 | kernel_size=kernel_size, padding=kernel_size // 2) 78 | # 进行像素清洗,合并相关通道数据 79 | self.pixel_shuffle = nn.PixelShuffle(upscale_factor=scaling_factor) 80 | # 最后添加激活层 81 | self.prelu = nn.PReLU() 82 | 83 | def forward(self, input): 84 | """ 85 | 前向传播. 86 | 87 | :参数 input: 输入图像数据集,张量表示,大小为(N, n_channels, w, h) 88 | :返回: 输出图像数据集,张量表示,大小为 (N, n_channels, w * scaling factor, h * scaling factor) 89 | """ 90 | output = self.conv(input) # (N, n_channels * scaling factor^2, w, h) 91 | output = self.pixel_shuffle(output) # (N, n_channels, w * scaling factor, h * scaling factor) 92 | output = self.prelu(output) # (N, n_channels, w * scaling factor, h * scaling factor) 93 | 94 | return output 95 | 96 | 97 | class ResidualBlock(nn.Module): 98 | """ 99 | 残差模块, 包含两个卷积模块和一个跳连. 100 | """ 101 | 102 | def __init__(self, kernel_size=3, n_channels=64): 103 | """ 104 | :参数 kernel_size: 核大小 105 | :参数 n_channels: 输入和输出通道数(由于是ResNet网络,需要做跳连,因此输入和输出通道数是一致的) 106 | """ 107 | super(ResidualBlock, self).__init__() 108 | 109 | # 第一个卷积块 110 | self.conv_block1 = ConvolutionalBlock(in_channels=n_channels, out_channels=n_channels, kernel_size=kernel_size, 111 | batch_norm=True, activation='PReLu') 112 | 113 | # 第二个卷积块 114 | self.conv_block2 = ConvolutionalBlock(in_channels=n_channels, out_channels=n_channels, kernel_size=kernel_size, 115 | batch_norm=True, activation=None) 116 | 117 | def forward(self, input): 118 | """ 119 | 前向传播. 120 | 121 | :参数 input: 输入图像集,张量表示,大小为 (N, n_channels, w, h) 122 | :返回: 输出图像集,张量表示,大小为 (N, n_channels, w, h) 123 | """ 124 | residual = input # (N, n_channels, w, h) 125 | output = self.conv_block1(input) # (N, n_channels, w, h) 126 | output = self.conv_block2(output) # (N, n_channels, w, h) 127 | output = output + residual # (N, n_channels, w, h) 128 | 129 | return output 130 | 131 | 132 | class SRResNet(nn.Module): 133 | """ 134 | SRResNet模型 135 | """ 136 | def __init__(self, large_kernel_size=9, small_kernel_size=3, n_channels=64, n_blocks=16, scaling_factor=4): 137 | """ 138 | :参数 large_kernel_size: 第一层卷积和最后一层卷积核大小 139 | :参数 small_kernel_size: 中间层卷积核大小 140 | :参数 n_channels: 中间层通道数 141 | :参数 n_blocks: 残差模块数 142 | :参数 scaling_factor: 放大比例 143 | """ 144 | super(SRResNet, self).__init__() 145 | 146 | # 放大比例必须为 2、 4 或 8 147 | scaling_factor = int(scaling_factor) 148 | assert scaling_factor in {2, 4, 8}, "放大比例必须为 2、 4 或 8!" 149 | 150 | # 第一个卷积块 151 | self.conv_block1 = ConvolutionalBlock(in_channels=3, out_channels=n_channels, kernel_size=large_kernel_size, 152 | batch_norm=False, activation='PReLu') 153 | 154 | # 一系列残差模块, 每个残差模块包含一个跳连接 155 | self.residual_blocks = nn.Sequential( 156 | *[ResidualBlock(kernel_size=small_kernel_size, n_channels=n_channels) for i in range(n_blocks)]) 157 | 158 | # 第二个卷积块 159 | self.conv_block2 = ConvolutionalBlock(in_channels=n_channels, out_channels=n_channels, 160 | kernel_size=small_kernel_size, 161 | batch_norm=True, activation=None) 162 | 163 | # 放大通过子像素卷积模块实现, 每个模块放大两倍 164 | n_subpixel_convolution_blocks = int(math.log2(scaling_factor)) 165 | self.subpixel_convolutional_blocks = nn.Sequential( 166 | *[SubPixelConvolutionalBlock(kernel_size=small_kernel_size, n_channels=n_channels, scaling_factor=2) for i 167 | in range(n_subpixel_convolution_blocks)]) 168 | 169 | # 最后一个卷积模块 170 | self.conv_block3 = ConvolutionalBlock(in_channels=n_channels, out_channels=3, kernel_size=large_kernel_size, 171 | batch_norm=False, activation='Tanh') 172 | 173 | def forward(self, lr_imgs): 174 | """ 175 | 前向传播. 176 | 177 | :参数 lr_imgs: 低分辨率输入图像集, 张量表示,大小为 (N, 3, w, h) 178 | :返回: 高分辨率输出图像集, 张量表示, 大小为 (N, 3, w * scaling factor, h * scaling factor) 179 | """ 180 | output = self.conv_block1(lr_imgs) # (16, 3, 24, 24) 181 | residual = output # (16, 64, 24, 24) 182 | output = self.residual_blocks(output) # (16, 64, 24, 24) 183 | output = self.conv_block2(output) # (16, 64, 24, 24) 184 | output = output + residual # (16, 64, 24, 24) 185 | output = self.subpixel_convolutional_blocks(output) # (16, 64, 24 * 4, 24 * 4) 186 | sr_imgs = self.conv_block3(output) # (16, 3, 24 * 4, 24 * 4) 187 | 188 | return sr_imgs 189 | 190 | -------------------------------------------------------------------------------- /图像超分辨项目/utils.py: -------------------------------------------------------------------------------- 1 | from PIL import Image 2 | import os 3 | import json 4 | import random 5 | import torchvision.transforms.functional as FT 6 | import torch 7 | import math 8 | 9 | device = torch.device("cuda" if torch.cuda.is_available() else "cpu") 10 | 11 | # 常量 12 | rgb_weights = torch.FloatTensor([65.481, 128.553, 24.966]).to(device) 13 | imagenet_mean = torch.FloatTensor([0.485, 0.456, 14 | 0.406]).unsqueeze(1).unsqueeze(2) 15 | imagenet_std = torch.FloatTensor([0.229, 0.224, 16 | 0.225]).unsqueeze(1).unsqueeze(2) 17 | imagenet_mean_cuda = torch.FloatTensor( 18 | [0.485, 0.456, 0.406]).to(device).unsqueeze(0).unsqueeze(2).unsqueeze(3) 19 | imagenet_std_cuda = torch.FloatTensor( 20 | [0.229, 0.224, 0.225]).to(device).unsqueeze(0).unsqueeze(2).unsqueeze(3) 21 | 22 | 23 | def create_data_lists(train_folders, test_folders, min_size, output_folder): 24 | """ 25 | 创建训练集和测试集列表文件. 26 | 参数 train_folders: 训练文件夹集合; 各文件夹中的图像将被合并到一个图片列表文件里面 27 | 参数 test_folders: 测试文件夹集合; 每个文件夹将形成一个图片列表文件 28 | 参数 min_size: 图像宽、高的最小容忍值 29 | 参数 output_folder: 最终生成的文件列表,json格式 30 | """ 31 | print("\n正在创建文件列表... 请耐心等待.\n") 32 | train_images = list() 33 | for d in train_folders: 34 | for i in os.listdir(d): 35 | img_path = os.path.join(d, i) 36 | img = Image.open(img_path, mode='r') 37 | if img.width >= min_size and img.height >= min_size: 38 | train_images.append(img_path) 39 | print("训练集中共有 %d 张图像\n" % len(train_images)) 40 | with open(os.path.join(output_folder, 'train_images.json'), 'w') as j: 41 | json.dump(train_images, j) 42 | 43 | for d in test_folders: 44 | test_images = list() 45 | test_name = d.split("/")[-1] 46 | for i in os.listdir(d): 47 | img_path = os.path.join(d, i) 48 | img = Image.open(img_path, mode='r') 49 | if img.width >= min_size and img.height >= min_size: 50 | test_images.append(img_path) 51 | print("在测试集 %s 中共有 %d 张图像\n" % 52 | (test_name, len(test_images))) 53 | with open(os.path.join(output_folder, test_name + '_test_images.json'),'w') as j: 54 | json.dump(test_images, j) 55 | 56 | print("生成完毕。训练集和测试集文件列表已保存在 %s 下\n" % output_folder) 57 | 58 | 59 | def convert_image(img, source, target): 60 | """ 61 | 转换图像格式. 62 | 63 | :参数 img: 输入图像 64 | :参数 source: 数据源格式, 共有3种 65 | (1) 'pil' (PIL图像) 66 | (2) '[0, 1]' 67 | (3) '[-1, 1]' 68 | :参数 target: 数据目标格式, 共5种 69 | (1) 'pil' (PIL图像) 70 | (2) '[0, 1]' 71 | (3) '[-1, 1]' 72 | (4) 'imagenet-norm' (由imagenet数据集的平均值和方差进行标准化) 73 | (5) 'y-channel' (亮度通道Y,采用YCbCr颜色空间, 用于计算PSNR 和 SSIM) 74 | :返回: 转换后的图像 75 | """ 76 | assert source in {'pil', '[0, 1]', '[-1, 1]' 77 | }, "无法转换图像源格式 %s!" % source 78 | assert target in { 79 | 'pil', '[0, 255]', '[0, 1]', '[-1, 1]', 'imagenet-norm' 80 | }, "无法转换图像目标格式t %s!" % target 81 | 82 | # 转换图像数据至 [0, 1] 83 | if source == 'pil': 84 | img = FT.to_tensor(img) #把一个取值范围是[0,255]的PIL.Image 转换成形状为[C,H,W]的Tensor,取值范围是[0,1.0] 85 | 86 | elif source == '[0, 1]': 87 | pass # 已经在[0, 1]范围内无需处理 88 | 89 | elif source == '[-1, 1]': 90 | img = (img + 1.) / 2. 91 | 92 | # 从 [0, 1] 转换至目标格式 93 | if target == 'pil': 94 | img = FT.to_pil_image(img) 95 | 96 | elif target == '[0, 255]': 97 | img = 255. * img 98 | 99 | elif target == '[0, 1]': 100 | pass # 无需处理 101 | 102 | elif target == '[-1, 1]': 103 | img = 2. * img - 1. 104 | 105 | elif target == 'imagenet-norm': 106 | if img.ndimension() == 3: 107 | img = (img - imagenet_mean) / imagenet_std 108 | elif img.ndimension() == 4: 109 | img = (img - imagenet_mean_cuda) / imagenet_std_cuda 110 | 111 | return img 112 | 113 | 114 | class ImageTransforms(object): 115 | """ 116 | 图像变换. 117 | """ 118 | 119 | def __init__(self, split, crop_size, scaling_factor, lr_img_type, 120 | hr_img_type): 121 | """ 122 | :参数 split: 'train' 或 'test' 123 | :参数 crop_size: 高分辨率图像裁剪尺寸 124 | :参数 scaling_factor: 放大比例 125 | :参数 lr_img_type: 低分辨率图像预处理方式 126 | :参数 hr_img_type: 高分辨率图像预处理方式 127 | """ 128 | self.split = split.lower() 129 | self.crop_size = crop_size 130 | self.scaling_factor = scaling_factor 131 | self.lr_img_type = lr_img_type 132 | self.hr_img_type = hr_img_type 133 | 134 | assert self.split in {'train', 'test'} 135 | 136 | def __call__(self, img): 137 | """ 138 | 对图像进行裁剪和下采样形成低分辨率图像 139 | :参数 img: 由PIL库读取的图像 140 | :返回: 特定形式的低分辨率和高分辨率图像 141 | """ 142 | 143 | # 裁剪 144 | if self.split == 'train': 145 | # 从原图中随机裁剪一个子块作为高分辨率图像 146 | left = random.randint(1, img.width - self.crop_size) 147 | top = random.randint(1, img.height - self.crop_size) 148 | right = left + self.crop_size 149 | bottom = top + self.crop_size 150 | hr_img = img.crop((left, top, right, bottom)) 151 | else: 152 | # 从图像中尽可能大的裁剪出能被放大比例整除的图像 153 | x_remainder = img.width % self.scaling_factor 154 | y_remainder = img.height % self.scaling_factor 155 | left = x_remainder // 2 156 | top = y_remainder // 2 157 | right = left + (img.width - x_remainder) 158 | bottom = top + (img.height - y_remainder) 159 | hr_img = img.crop((left, top, right, bottom)) 160 | 161 | # 下采样(双三次差值) 162 | lr_img = hr_img.resize((int(hr_img.width / self.scaling_factor), 163 | int(hr_img.height / self.scaling_factor)), 164 | Image.BICUBIC) 165 | 166 | # 安全性检查 167 | assert hr_img.width == lr_img.width * self.scaling_factor and hr_img.height == lr_img.height * self.scaling_factor 168 | 169 | # 转换图像 170 | lr_img = convert_image(lr_img, source='pil', target=self.lr_img_type) 171 | hr_img = convert_image(hr_img, source='pil', target=self.hr_img_type) 172 | 173 | return lr_img, hr_img 174 | 175 | 176 | class AverageMeter(object): 177 | """ 178 | 跟踪记录类,用于统计一组数据的平均值、累加和、数据个数. 179 | """ 180 | 181 | def __init__(self): 182 | self.reset() 183 | 184 | def reset(self): 185 | self.val = 0 186 | self.avg = 0 187 | self.sum = 0 188 | self.count = 0 189 | 190 | def update(self, val, n=1): 191 | self.val = val 192 | self.sum += val * n 193 | self.count += n 194 | self.avg = self.sum / self.count 195 | 196 | 197 | 198 | 199 | 200 | 201 | def adjust_learning_rate(optimizer, shrink_factor): 202 | """ 203 | 调整学习率. 204 | 205 | :参数 optimizer: 需要调整的优化器 206 | :参数 shrink_factor: 调整因子,范围在 (0, 1) 之间,用于乘上原学习率. 207 | """ 208 | 209 | print("\n调整学习率.") 210 | for param_group in optimizer.param_groups: 211 | param_group['lr'] = param_group['lr'] * shrink_factor 212 | print("新的学习率为 %f\n" % (optimizer.param_groups[0]['lr'], )) 213 | -------------------------------------------------------------------------------- /模式识别/kmeans/kmeans_pic.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Sun Nov 1 18:00:00 2020 4 | 5 | @author: tremble 6 | """ 7 | 8 | import numpy as np 9 | import matplotlib.pyplot as plt 10 | import pandas as pd 11 | from sklearn.datasets import load_iris 12 | import random 13 | from sklearn.manifold import TSNE 14 | 15 | 16 | 17 | 18 | def eucliDist(A,B): 19 | return np.sqrt(np.sum(np.square(A - B))) 20 | 21 | class kmeans_classify: 22 | 23 | def __init__(self, data, k): 24 | row, col = data.shape 25 | 26 | self.row = row 27 | self.col = col 28 | self.k=k 29 | self.data=data 30 | #print(self.row,self.col,self.k) 31 | 32 | 33 | def kmeans(self): 34 | data_mean = random.sample(range(self.row), self.k) 35 | num1 = data_mean[0] 36 | num2 = data_mean[1] 37 | num3 = data_mean[2] 38 | 39 | mean1 = np.array([self.data[num1,0:4]]) 40 | mean2 = np.array([self.data[num2,0:4]]) 41 | mean3 = np.array([self.data[num3,0:4]]) 42 | #print(mean1,mean2,mean3) 43 | 44 | 45 | 46 | test1 = mean1 47 | test2 = mean2 48 | test3 = mean3 49 | 50 | data_copy = self.data.copy() 51 | #d=0 52 | 53 | active = True 54 | while(active): 55 | 56 | data1 = np.zeros((1, 4)) 57 | data2 = np.zeros((1, 4)) 58 | data3 = np.zeros((1, 4)) 59 | 60 | data = data_copy 61 | 62 | for i in range(self.row): 63 | distance1 = eucliDist(data[i,0:4], mean1) 64 | distance2 = eucliDist(data[i,0:4], mean2) 65 | distance3 = eucliDist(data[i,0:4], mean3) 66 | #print(distance1) 67 | if distance1 < distance2 and distance1 < distance3: 68 | data1 = np.vstack((data1, data[i,0:4])) 69 | #print(data1) 70 | if distance2 < distance1 and distance2 < distance3: 71 | data2 = np.vstack((data2, data[i,0:4])) 72 | if distance3 < distance1 and distance3 < distance2: 73 | data3 = np.vstack((data3, data[i,0:4])) 74 | #print(data1,data2,data3) 75 | 76 | data1 = np.delete(data1, 0, axis=0) 77 | data2 = np.delete(data2, 0, axis=0) 78 | data3 = np.delete(data3, 0, axis=0) 79 | #print(data1,data2,data3) 80 | 81 | mean1 = np.mean(data1, axis=0) 82 | mean2 = np.mean(data2, axis=0) 83 | mean3 = np.mean(data3, axis=0) 84 | #print(mean1, mean2, mean3) 85 | 86 | J1 = 0 87 | for j in range(data1.shape[0]): 88 | J1 += np.sum(np.square(data1[j] - mean1)) 89 | 90 | J2 = 0 91 | for j in range(data2.shape[0]): 92 | J2 += np.sum(np.square(data2[j] - mean2)) 93 | 94 | J3 = 0 95 | for j in range(data3.shape[0]): 96 | J3 += np.sum(np.square(data3[j] - mean3)) 97 | 98 | 99 | 100 | if (test1 == mean1).all() and (test2 == mean2).all and (test3 == mean3).all: 101 | #d=d+1 102 | #if d==100: 103 | active = False 104 | else: 105 | test1 = mean1 106 | test2 = mean2 107 | test3 = mean3 108 | #print(d) 109 | self.data1=data1 110 | self.data2=data2 111 | self.data3=data3 112 | 113 | return data1, data2, data3 114 | 115 | def plot_data(self): 116 | plt.title('k-means') 117 | plt.show() 118 | 119 | 120 | def plot_2D(self): 121 | tsne = TSNE(n_components=2,init='pca',random_state=0) 122 | result1 = tsne.fit_transform(self.data1) 123 | result2 = tsne.fit_transform(self.data2) 124 | result3 = tsne.fit_transform(self.data3) 125 | 126 | 127 | plt.scatter(result1[:,0],result1[:,1],c='r') 128 | plt.scatter(result2[:,0],result2[:,1],c='b') 129 | plt.scatter(result3[:,0],result3[:,1],c='y') 130 | plt.show() 131 | 132 | 133 | 134 | 135 | 136 | def ass_kmeans(self): 137 | 138 | data1 = self.data1 139 | data2 = self.data2 140 | data3 = self.data3 141 | 142 | s1=np.zeros(data1.shape[0]) 143 | s2=np.zeros(data2.shape[0]) 144 | s3=np.zeros(data3.shape[0]) 145 | 146 | 147 | for i in range(data1.shape[0]): 148 | ai_1=0 149 | for j in range(data1.shape[0]): 150 | dis = eucliDist(data1[i,0:4], data1[j,0:4]) 151 | ai_1 += dis 152 | ai_1 = ai_1/data1.shape[0] 153 | 154 | bi_2=0 155 | for j in range(data2.shape[0]): 156 | dis = eucliDist(data1[i,0:4], data2[j,0:4]) 157 | bi_2 += dis 158 | bi_2 = bi_2/data2.shape[0] 159 | 160 | bi_3 = 0 161 | for j in range(data3.shape[0]): 162 | dis = eucliDist(data1[i,0:4], data3[j,0:4]) 163 | bi_3 += dis 164 | bi_3 = bi_3/data3.shape[0] 165 | 166 | bi=min(bi_2,bi_3) 167 | 168 | s1[i] = (bi-ai_1)/(max(bi,ai_1)) 169 | 170 | 171 | for i in range(data2.shape[0]): 172 | ai_2=0 173 | for j in range(data2.shape[0]): 174 | dis = eucliDist(data2[i,0:4], data2[j,0:4]) 175 | ai_2 += dis 176 | ai_2 = ai_2/data2.shape[0] 177 | 178 | bi_1=0 179 | for j in range(data1.shape[0]): 180 | dis = eucliDist(data2[i,0:4], data1[j,0:4]) 181 | bi_1 += dis 182 | bi_1 = bi_1/data1.shape[0] 183 | 184 | bi_3 = 0 185 | for j in range(data3.shape[0]): 186 | dis = eucliDist(data2[i,0:4], data3[j,0:4]) 187 | bi_3 += dis 188 | bi_3 = bi_3/data3.shape[0] 189 | 190 | bi=min(bi_3,bi_1) 191 | 192 | s2[i] = (bi-ai_2)/(max(bi,ai_2)) 193 | 194 | 195 | for i in range(data3.shape[0]): 196 | ai_3=0 197 | for j in range(data3.shape[0]): 198 | dis = eucliDist(data3[i,0:4], data3[j,0:4]) 199 | ai_3 += dis 200 | ai_3 = ai_3/data3.shape[0] 201 | 202 | bi_1=0 203 | for j in range(data1.shape[0]): 204 | dis = eucliDist(data3[i,0:4], data1[j,0:4]) 205 | bi_1 += dis 206 | bi_1 = bi_1/data1.shape[0] 207 | 208 | bi_2 = 0 209 | for j in range(data2.shape[0]): 210 | dis = eucliDist(data3[i,0:4], data2[j,0:4]) 211 | bi_2 += dis 212 | bi_2 = bi_2/data2.shape[0] 213 | 214 | bi=min(bi_2,bi_1) 215 | 216 | s3[i] = (bi-ai_3)/(max(bi,ai_3)) 217 | 218 | 219 | 220 | s1 = np.mean(s1) 221 | s2 = np.mean(s2) 222 | s3 = np.mean(s3) 223 | s=s1+s2+s3 224 | s=s/3 225 | return s 226 | 227 | 228 | 229 | if __name__ == '__main__': 230 | 231 | dat = np.array(pd.read_csv(r"H:\Datasets\Iris.csv",header = None)) 232 | dat = np.array(dat[:,0:4],dtype=np.float32) 233 | a=kmeans_classify(dat, 3) 234 | dat1,dat2,dat3 =a.kmeans() 235 | #print(dat1,dat2,dat3) 236 | #a.plot_data() 237 | print('轮廓系数为') 238 | s = a.ass_kmeans() 239 | print(s) 240 | a.plot_2D() 241 | 242 | 243 | 244 | -------------------------------------------------------------------------------- /智能系统实验/4_2/train_new.py: -------------------------------------------------------------------------------- 1 | import os 2 | import cv2 3 | import torch 4 | import torchvision 5 | import numpy as np 6 | from time import time 7 | import torch.nn as nn 8 | import torch.nn.functional as F 9 | import matplotlib.pyplot as plt 10 | import torchvision.transforms as transforms 11 | from torch.utils.tensorboard import SummaryWriter 12 | 13 | def data_loader(): 14 | """加载数据并数据增强""" 15 | train_transforms = transforms.Compose([ 16 | transforms.Resize((224,224)), 17 | transforms.RandomHorizontalFlip(), # 随机水平翻转 18 | transforms.RandomGrayscale(), # 随机灰度 19 | # transforms.Grayscale(1), 20 | transforms.ToTensor() # 数据类型转换为tensor,并归一化到[0,1] 21 | ]) 22 | test_transforms = transforms.Compose([transforms.Resize((224,224)), 23 | # transforms.Grayscale(1), 24 | transforms.ToTensor()]) 25 | # cat_dog_dataset = CDDataset() 26 | trainset = torchvision.datasets.ImageFolder(root='./data/train',transform=train_transforms) 27 | trainloader = torch.utils.data.DataLoader(trainset, batch_size=32,shuffle=True, num_workers=4) 28 | testset = torchvision.datasets.ImageFolder(root='./data/test',transform=test_transforms) 29 | testloader = torch.utils.data.DataLoader(testset, batch_size=4,shuffle=True, num_workers=4) 30 | 31 | # 显示数据信息 32 | print(len(trainset),len(testset)) 33 | return trainloader,testloader 34 | 35 | def imshow(dataloader): 36 | """可视化数据""" 37 | classes_names = ['cat', 'dog'] 38 | 39 | plt.figure(figsize=(4,4)) 40 | dataiter = iter(dataloader) 41 | features,labels = dataiter.next() 42 | print(features.shape,labels) 43 | for i in range(len(labels)): 44 | plt.subplot(4,4,i+1) 45 | plt.xticks([]) 46 | plt.yticks([]) 47 | plt.grid(False) 48 | npimg = features[i].numpy() 49 | plt.imshow(np.transpose(npimg, (1, 2, 0))) 50 | plt.xlabel(classes_names[labels[i]]) 51 | plt.show() 52 | 53 | class Net(nn.Module): 54 | def __init__(self): 55 | super().__init__() 56 | ##########################model 1################################## 57 | # self.conv1 = nn.Conv2d(1, 32, 20, 4) # n*1*128*128-->n*32*28*28 58 | # self.norm1 = nn.BatchNorm2d(32) 59 | # nn.init.xavier_uniform_(self.conv1.weight) 60 | # # MaxPool的移动步长默认为kernel_size 61 | # self.maxpool1 = nn.MaxPool2d(kernel_size=2) # n*32*28*28-->n*32*14*14 62 | # 63 | # self.conv2 = nn.Conv2d(32, 64, 3, 1, 2) # n*32*14*14-->n*64*16*16 64 | # self.norm2 = nn.BatchNorm2d(64) 65 | # nn.init.xavier_uniform_(self.conv2.weight) 66 | # self.maxpool2 = nn.MaxPool2d(2, 2) # n*64*16*16-->n*64*8*8 67 | # 68 | # 69 | # self.fc1 = nn.Linear(4096,4096) # n*4096-->n*4096 70 | # self.fc2 = nn.Linear(4096, 10) # n*4096-->n*10 71 | 72 | ##########################model 2##################################### 73 | self.model = torchvision.models.resnet34(pretrained=True) 74 | self.model.fc = nn.Linear(512,2) 75 | 76 | def forward(self, x): 77 | # out = F.relu(self.conv1(x)) 78 | # out = self.norm1(out) 79 | # out = self.maxpool1(out) 80 | # 81 | # out = F.relu(self.conv2(out)) 82 | # out = self.norm2(out) 83 | # out = self.maxpool2(out) 84 | # 85 | # out = out.view(out.size(0),-1) 86 | # out = F.relu(self.fc1(out)) 87 | # out = self.fc2(out) 88 | # return out 89 | return self.model(x) 90 | 91 | def evaluate_acc(self,criterion,test_iter, device): 92 | """模型测试""" 93 | acc_sum, n, test_loss,batch_cont = 0.0, 0,0.0,0 94 | with torch.no_grad(): 95 | for X, y in test_iter: 96 | self.eval() 97 | acc_sum += (self(X.to(device)).argmax(dim=1) == y.to(device)).sum().item() 98 | loss = criterion(self(X.to(device)), y.to(device)) 99 | test_loss += loss.item() 100 | self.train() 101 | n += y.shape[0] 102 | batch_cont += 1 103 | test_loss = test_loss/batch_cont 104 | acc_sum = acc_sum/n 105 | return test_loss,acc_sum 106 | 107 | def train_model(self,device,num_epochs,PATH): 108 | """模型训练""" 109 | train_iter,test_iter = data_loader() 110 | imshow(test_iter) 111 | writer = SummaryWriter() 112 | start_epoch = 0 113 | best_score = 0 114 | optimizer = torch.optim.Adam(net.parameters(),lr=0.001) 115 | # 自适应学习率 116 | scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(optimizer, mode="max", 117 | factor=0.85, patience=0) 118 | if os.path.exists(PATH) is not True: 119 | criterion = nn.CrossEntropyLoss() 120 | else: 121 | checkpoint = torch.load(PATH) 122 | self.load_state_dict(checkpoint['model_state_dict']) 123 | optimizer.load_state_dict(checkpoint['optimizer_state_dict']) 124 | start_epoch = checkpoint['epoch'] 125 | criterion = checkpoint['criterion'] 126 | 127 | patience_counter = 0 128 | for epoch in range(start_epoch,num_epochs): 129 | 130 | train_loss, train_acc, n, batch_count,start = 0.0, 0.0, 0, 0,time() 131 | 132 | for features, labels in train_iter: 133 | features,labels = features.to(device), labels.to(device) 134 | outputs = self(features) 135 | loss = criterion(outputs, labels) 136 | optimizer.zero_grad() 137 | loss.backward() 138 | optimizer.step() 139 | train_loss += loss.item() 140 | train_acc += (outputs.argmax(dim=1) == labels).sum().item() 141 | n += labels.shape[0] 142 | batch_count += 1 143 | 144 | test_loss, test_acc = self.evaluate_acc(criterion,test_iter,device) 145 | print('epoch %d, train_loss: %.4f, train acc: %.2f%%, test_loss: %.4f,test acc: %.2f%%, time %.1f sec' 146 | % (epoch + 1, train_loss / batch_count, 100 * train_acc / n, test_loss, 100 * test_acc, time() - start)) 147 | writer.add_scalar('train/train_acc', train_acc/n, epoch) 148 | writer.add_scalar('test/test_acc',test_acc,epoch) 149 | writer.add_scalar('test/test_loss', test_loss, epoch) 150 | writer.add_scalar('train/train_loss', train_loss/batch_count, epoch) 151 | 152 | scheduler.step(test_acc) 153 | if test_acc < best_score: 154 | patience_counter += 1 155 | else: 156 | best_score = test_acc 157 | patience_counter = 0 158 | torch.save({"epoch": epoch, 159 | "model": net.state_dict(), 160 | "best_score": best_score}, 161 | './check_point/best_point.pth') 162 | 163 | if epoch % 5 == 0: 164 | torch.save({ 165 | 'epoch': epoch, 166 | 'model_state_dict': net.state_dict(), 167 | 'optimizer_state_dict': optimizer.state_dict(), 168 | 'criterion':criterion 169 | }, PATH) 170 | 171 | if patience_counter >= 5: 172 | print("-> Early stopping: patience limit reached, stopping...") 173 | break 174 | writer.close() 175 | print('Finished Training') 176 | 177 | 178 | if __name__ == '__main__': 179 | 180 | device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') 181 | # device = 'cpu' 182 | print(device) 183 | net = Net() 184 | net = net.to(device) 185 | num_epochs = 80 186 | learning_rate = 0.001 187 | model_path = './check_point/check_point.pth' 188 | 189 | net.train_model(device,num_epochs,model_path) 190 | 191 | 192 | -------------------------------------------------------------------------------- /数据挖掘/pattern mining.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": { 7 | "collapsed": true 8 | }, 9 | "outputs": [], 10 | "source": [ 11 | "import sklearn\n", 12 | "import torch\n", 13 | "import torchvision\n", 14 | "import torchvision.transforms as transforms\n", 15 | "from torch.utils.data import DataLoader\n", 16 | "from PIL import Image\n", 17 | "import numpy as np\n", 18 | "from skimage import feature as ft\n", 19 | "import pandas as pd\n", 20 | "import sklearn\n", 21 | "import random\n", 22 | "from sklearn import datasets\n", 23 | "from sklearn.preprocessing import scale\n", 24 | "from sklearn.manifold import TSNE\n", 25 | "from sklearn.decomposition import PCA\n", 26 | "import matplotlib.pyplot as plt\n", 27 | "from sklearn.metrics import silhouette_score\n", 28 | "from sklearn import svm\n", 29 | "from sklearn.cluster import KMeans\n", 30 | "from sklearn import metrics\n", 31 | "\n", 32 | "\n", 33 | "transform = transforms.Compose(\n", 34 | " [transforms.ToTensor(),\n", 35 | " transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])\n", 36 | "\n", 37 | "trainset = torchvision.datasets.CIFAR10(root='./data', train=True,\n", 38 | " download=True, transform=transform)\n", 39 | "trainloader = DataLoader(trainset, batch_size=64,\n", 40 | " shuffle=True, num_workers=2)\n", 41 | "\n", 42 | "testset = torchvision.datasets.CIFAR10(root='./data', train=False,\n", 43 | " download=True, transform=transform)\n", 44 | "testloader = DataLoader(testset, batch_size=64,\n", 45 | " shuffle=False, num_workers=2)\n", 46 | "\n", 47 | "train=trainset.data.transpose(0,3,1,2)\n", 48 | "test=testset.data.transpose(0,3,1,2)\n", 49 | "\n", 50 | "def hog_extraction(data, size=1):\n", 51 | " \"\"\"\n", 52 | " 功能:提取图像HOG特征\n", 53 | "\n", 54 | " 输入:\n", 55 | " data:(numpy array)输入数据[num,3,32,32]\n", 56 | " size:(int)(size,size)为提取HOG特征的cellsize\n", 57 | "\n", 58 | " 输出:\n", 59 | " data_hogfeature:(numpy array):data的HOG特征[num,dim]\n", 60 | " \"\"\"\n", 61 | " num = data.shape[0]\n", 62 | " data = data.astype('uint8')\n", 63 | " # 提取训练样本的HOG特征\n", 64 | " data1_hogfeature = []\n", 65 | " for i in range(num):\n", 66 | " x = data[i]\n", 67 | " r = Image.fromarray(x[0])\n", 68 | " g = Image.fromarray(x[1])\n", 69 | " b = Image.fromarray(x[2])\n", 70 | "\n", 71 | " # 合并三通道\n", 72 | " img = Image.merge(\"RGB\", (r, g, b))\n", 73 | " # 转为灰度图\n", 74 | " gray = img.convert('L')\n", 75 | " # out=gray.resize((100,100),Image.ANTIALIAS)\n", 76 | " # 转化为array\n", 77 | " gray_array = np.array(gray)\n", 78 | " # 提取HOG特征\n", 79 | " hogfeature = ft.hog(gray_array, pixels_per_cell=(size, size))\n", 80 | "\n", 81 | " data1_hogfeature.append(hogfeature)\n", 82 | "\n", 83 | " # 把data1_hogfeature中的特征按行堆叠\n", 84 | " data_hogfeature = np.reshape(np.concatenate(data1_hogfeature), [num, -1])\n", 85 | " return data_hogfeature\n", 86 | "\n", 87 | "\n", 88 | "class Kmeans():\n", 89 | " def __init__(self,dat,k):\n", 90 | " data=scale(dat)\n", 91 | " self.data=data\n", 92 | " self.row, self.col = data.shape\n", 93 | " self.k=k\n", 94 | " self.centers=np.ndarray((k,self.col))\n", 95 | " choices=random.choices(range(self.row),k=k)\n", 96 | " for i in range(k):\n", 97 | " self.centers[i,:]=self.data[choices[i],:]\n", 98 | " def fit(self):\n", 99 | " count=0\n", 100 | " while(count<15):\n", 101 | " self.labels=np.zeros((self.row))\n", 102 | " for i in range(self.data.shape[0]):\n", 103 | " dis=[]\n", 104 | " for j in range(self.k):\n", 105 | " dis.append(np.linalg.norm(self.data[i,:]-self.centers[j,:],axis=0))\n", 106 | " lab=np.argmin(dis,axis=0)\n", 107 | " self.labels[i]=lab\n", 108 | " self.result={}\n", 109 | " for i in range(self.k):\n", 110 | " type=np.where(self.labels==i)[0]\n", 111 | " self.result[i]=type\n", 112 | " if len(type)==0:\n", 113 | " self.centers[i, :] =0\n", 114 | " else:\n", 115 | " self.centers[i,:]=np.mean(self.data[type,:],axis=0)\n", 116 | " count+=1\n", 117 | " return self.centers, self.result,self.labels\n", 118 | "\n", 119 | " def imshow(self):\n", 120 | " tsne = TSNE(n_components=2, learning_rate=100).fit_transform(self.data)\n", 121 | " pca = PCA().fit_transform(self.data)\n", 122 | " plt.figure(figsize=(12, 6))\n", 123 | " plt.subplot(121)\n", 124 | " plt.scatter(tsne[:, 0], tsne[:, 1], c=self.labels)\n", 125 | " plt.title('t-SNE')\n", 126 | " plt.subplot(122)\n", 127 | " plt.scatter(pca[:, 0], pca[:, 1], c=self.labels)\n", 128 | " plt.title('PCA')\n", 129 | " plt.colorbar()\n", 130 | " plt.show()\n", 131 | "\n", 132 | "\n", 133 | "\n", 134 | "\n", 135 | "hog_train=hog_extraction(trainset.data,size=1)\n", 136 | "hog_val=hog_extraction(test,size=2)\n", 137 | "\n", 138 | "\n", 139 | "kmeans=Kmeans(hog_val,10)\n", 140 | "centers,results,labels=kmeans.fit()\n", 141 | "kmeans.imshow()\n", 142 | "s = silhouette_score(hog_val, testset.targets)\n", 143 | "print(centers)\n", 144 | "print(results)\n", 145 | "print(s)\n", 146 | "\n", 147 | "\n", 148 | "\n", 149 | "clf=svm.SVC()\n", 150 | "#训练\n", 151 | "clf.fit(hog_val,testset.targets)\n", 152 | "#预测验证集类别\n", 153 | "result=clf.predict(hog_val)\n", 154 | "#计算验证集精度\n", 155 | "score=clf.score(hog_val,testset.targets)\n", 156 | "print(result)\n", 157 | "print(score)\n", 158 | "\n", 159 | "\n", 160 | "\n" 161 | ] 162 | } 163 | ], 164 | "metadata": { 165 | "kernelspec": { 166 | "display_name": "Python 3", 167 | "language": "python", 168 | "name": "python3" 169 | }, 170 | "language_info": { 171 | "codemirror_mode": { 172 | "name": "ipython", 173 | "version": 3 174 | }, 175 | "file_extension": ".py", 176 | "mimetype": "text/x-python", 177 | "name": "python", 178 | "nbconvert_exporter": "python", 179 | "pygments_lexer": "ipython3", 180 | "version": "3.8.5" 181 | }, 182 | "toc": { 183 | "base_numbering": 1, 184 | "nav_menu": {}, 185 | "number_sections": true, 186 | "sideBar": true, 187 | "skip_h1_title": false, 188 | "title_cell": "Table of Contents", 189 | "title_sidebar": "Contents", 190 | "toc_cell": false, 191 | "toc_position": {}, 192 | "toc_section_display": true, 193 | "toc_window_display": false 194 | }, 195 | "varInspector": { 196 | "cols": { 197 | "lenName": 16, 198 | "lenType": 16, 199 | "lenVar": 40 200 | }, 201 | "kernels_config": { 202 | "python": { 203 | "delete_cmd_postfix": "", 204 | "delete_cmd_prefix": "del ", 205 | "library": "var_list.py", 206 | "varRefreshCmd": "print(var_dic_list())" 207 | }, 208 | "r": { 209 | "delete_cmd_postfix": ") ", 210 | "delete_cmd_prefix": "rm(", 211 | "library": "var_list.r", 212 | "varRefreshCmd": "cat(var_dic_list()) " 213 | } 214 | }, 215 | "types_to_exclude": [ 216 | "module", 217 | "function", 218 | "builtin_function_or_method", 219 | "instance", 220 | "_Feature" 221 | ], 222 | "window_display": false 223 | } 224 | }, 225 | "nbformat": 4, 226 | "nbformat_minor": 1 227 | } --------------------------------------------------------------------------------