深度学习可视化的一些工具+pytorch实现回归与卷积可视化
生活随笔
收集整理的這篇文章主要介紹了
深度学习可视化的一些工具+pytorch实现回归与卷积可视化
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一.深度學習可視化的一些工具
1.深度學習網絡結構畫圖工具:https://cbovar.github.io/ConvNetDraw/
2.將.onnx放入即可,可視化網絡結構:https://lutzroeder.github.io/netron/
3.結構可視化工具:https://github.com/HarisIqbal88/PlotNeuralNet
二.回歸
線性回歸的損失函數和梯度更新如下圖:
一,numpy實現線性回歸梯度下降
import numpy as np import matplotlib.pyplot as plt def get_fake_data(batch_size=8):''' 產生隨機數據:y=x*2+3,加上了一些噪聲'''x = np.random.rand(batch_size, 1) * 5y = x * 2 + 3 + np.random.rand(batch_size, 1)*2return x, ydef get_gradient(theta,x,y):m=x.shape[0]Y_estimate=np.dot(x,theta)assert (Y_estimate.shape==(m,))error=Y_estimate-yassert (error.shape==(m,))cost =1.0/(2*m)*np.sum(error**2)#grad=(1.0/m)*np.dot(x.T,error).reshape(-1)#(2,)grad = (1.0 / m) * np.dot(error,x) # (2,)return grad,cost def gradient_descent(x,y,iterations,alpha):theta=np.random.randn(2)costs=[]for i in range(iterations):grad,cost=get_gradient(theta,x,y)new_theta=theta-alpha*gradif i%100==0:print('{} iterations cost={}'.format(i,cost))costs.append(cost)theta=new_thetareturn costs,thetadef vis_data():# 來看看產生的x-y分布x, y = get_fake_data(batch_size=16)print(x.shape)print(y.shape)plt.scatter(np.squeeze(x), np.squeeze(y))plt.show() if __name__=='__main__':batch_size=32data_x, data_y = get_fake_data(batch_size=batch_size)#添加一列為1的向量 實際上就是乘以 theta 就是bdata_x=np.hstack((data_x,np.ones_like(data_x)))#(m,2)print(data_x)print(data_x.shape)costs,theta=gradient_descent(data_x,np.squeeze(data_y),iterations=50000,alpha=0.002)print(data_y.shape)#print(theta)y_predict=np.dot(data_x,theta)#theta[0]+theta[1]*data_x[:,1]print(y_predict.shape)plt.figure()#樣本圖print(data_x[:2])plt.scatter(data_x[:,0],np.squeeze(data_y),c='red')plt.plot(data_x[:,0],y_predict)plt.show()紅色的是散列點,藍色的線是擬合的直線。
二,pytorch實現線性回歸梯度下降
import numpy as np import matplotlib.pyplot as plt import torch as tdevice=t.device('cpu')def get_fake_data(batch_size=8):''' 產生隨機數據:y=x*2+3,加上了一些噪聲'''x = t.rand(batch_size, 1,device=device) * 5y = x * 2 + 3 + t.rand(batch_size, 1)*2return x, ydef vis_data():# 來看看產生的x-y分布x, y = get_fake_data(batch_size=16)print(x.shape)print(y.shape)plt.scatter(np.squeeze(x), np.squeeze(y))plt.show() if __name__=='__main__':# vis_data()m=batch_size=32data_x, data_y = get_fake_data(batch_size=batch_size)#添加一列為1的向量 實際上就是乘以 theta 就是bdata_x=t.from_numpy(np.hstack((data_x,np.ones_like(data_x))))#(m,2)print(data_x.shape)theta = t.randn((2, 1),requires_grad=True)iterations=500lr = 0.005 # 學習率losses=[]for i in range(iterations):# forward:計算lossy_pred = data_x.mm(theta)print('y_pred',y_pred.shape)loss = 1/(2*m) * (y_pred - data_y) ** 2print('loss',loss.shape)loss = loss.sum()print('loss', loss.shape)losses.append(loss.item())# backward:手動計算梯度loss.backward()# 更新參數theta.data.sub_(lr * theta.grad.data)# 梯度清零theta.grad.data.zero_()print('losses=',losses)# 畫圖plt.scatter(np.squeeze(data_x[:,0]), np.squeeze(data_y),c='red')y_predict=data_x.mm(theta)print('y_predict.shape',y_predict.shape)print(data_x.detach().numpy())plt.plot(data_x.detach().numpy()[:,0], y_predict.detach().numpy()) # predictedplt.show()三.實現ResNet34
from torch import nn import torch as t from torch.nn import functional as Fclass ResidualBlock(nn.Module):'''實現子module: Residual Block'''def __init__(self, inchannel, outchannel, stride=1, shortcut=None):super(ResidualBlock, self).__init__()self.left = nn.Sequential(nn.Conv2d(inchannel, outchannel, 3, stride, 1, bias=False),nn.BatchNorm2d(outchannel),nn.ReLU(inplace=True),nn.Conv2d(outchannel, outchannel, 3, 1, 1, bias=False),nn.BatchNorm2d(outchannel))self.right = shortcutdef forward(self, x):out = self.left(x)residual = x if self.right is None else self.right(x)out += residualreturn F.relu(out)class ResNet(nn.Module):'''實現主module:ResNet34ResNet34 包含多個layer,每個layer又包含多個residual block用子module來實現residual block,用_make_layer函數來實現layer'''def __init__(self, num_classes=1000):super(ResNet, self).__init__()# 前幾層圖像轉換self.pre = nn.Sequential(nn.Conv2d(3, 64, 7, 2, 3, bias=False),nn.BatchNorm2d(64),nn.ReLU(inplace=True),nn.MaxPool2d(3, 2, 1))# 重復的layer,分別有3,4,6,3個residual blockself.layer1 = self._make_layer(64, 64, 3)self.layer2 = self._make_layer(64, 128, 4, stride=2)self.layer3 = self._make_layer(128, 256, 6, stride=2)self.layer4 = self._make_layer(256, 512, 3, stride=2)# 分類用的全連接self.fc = nn.Linear(512, num_classes)def _make_layer(self, inchannel, outchannel, block_num, stride=1):'''構建layer,包含多個residual block'''shortcut = nn.Sequential(nn.Conv2d(inchannel, outchannel, 1, stride, bias=False),nn.BatchNorm2d(outchannel))layers = []layers.append(ResidualBlock(inchannel, outchannel, stride, shortcut))for i in range(1, block_num):layers.append(ResidualBlock(outchannel, outchannel))return nn.Sequential(*layers)def forward(self, x):x = self.pre(x)x = self.layer1(x)x = self.layer2(x)x = self.layer3(x)x = self.layer4(x)x = F.avg_pool2d(x, 7)x = x.view(x.size(0), -1)return self.fc(x) model = ResNet() input = t.randn(1, 3, 224, 224) o = model(input) print(o.shape)四,卷積可視化
1.可視化濾波器
import numpy as np import matplotlib.pyplot as plt import cv2 def get_filters():filter_vals = np.array([[-1, -1, 1, 1],[-1, -1, 1, 1],[-1, -1, 1, 1],[-1, -1, 1, 1]])print('Filter shape: ', filter_vals.shape)# Defining the Filtersfilter_1 = filter_valsfilter_2 = -filter_1filter_3 = filter_1.Tfilter_4 = -filter_3filters = np.array([filter_1, filter_2, filter_3, filter_4])return filtersdef vis_filter(filters):# Check the Filtersfig = plt.figure(figsize=(10, 5))for i in range(4):ax = fig.add_subplot(1, 4, i + 1, xticks=[], yticks=[])ax.imshow(filters[i], cmap='gray')ax.set_title('Filter %s' % str(i + 1))# width, height = filters[i].shape# for x in range(width):# for y in range(height):# ax.annotate(str(filters[i][x][y]), xy=(y, x),# color='white' if filters[i][x][y] < 0 else 'black')plt.show()filters=get_filters() print(filters.shape) vis_filter(filters)2.將自定義的濾波核作為卷積核對狗狗進行卷積,并可視化
import numpy as np import matplotlib.pyplot as plt import cv2 def get_filters():filter_vals = np.array([[-1, -1, 1, 1],[-1, -1, 1, 1],[-1, -1, 1, 1],[-1, -1, 1, 1]])print('Filter shape: ', filter_vals.shape)# Defining the Filtersfilter_1 = filter_valsfilter_2 = -filter_1filter_3 = filter_1.Tfilter_4 = -filter_3filters = np.array([filter_1, filter_2, filter_3, filter_4])return filtersdef vis_filter(filters):# Check the Filtersfig = plt.figure(figsize=(10, 5))for i in range(4):ax = fig.add_subplot(1, 4, i + 1, xticks=[], yticks=[])ax.imshow(filters[i], cmap='gray')ax.set_title('Filter %s' % str(i + 1))# width, height = filters[i].shape# for x in range(width):# for y in range(height):# ax.annotate(str(filters[i][x][y]), xy=(y, x),# color='white' if filters[i][x][y] < 0 else 'black')plt.show()import torch import torch.nn as nn import torch.nn.functional as Fclass Net(nn.Module):def __init__(self, weight):super(Net, self).__init__()# initializes the weights of the convolutional layer to be the weights of the 4 defined filtersk_height, k_width = weight.shape[2:]# assumes there are 4 grayscale filtersself.conv = nn.Conv2d(1, 4, kernel_size=(k_height, k_width), bias=False)# initializes the weights of the convolutional layerself.conv.weight = torch.nn.Parameter(weight)print(self.conv.weight.shape)# define a pooling layerself.pool = nn.MaxPool2d(2, 2)def forward(self, x):# calculates the output of a convolutional layer# pre- and post-activationconv_x = self.conv(x)activated_x = F.relu(conv_x)# applies pooling layerpooled_x = self.pool(activated_x)# returns all layersreturn conv_x, activated_x, pooled_xif __name__ == '__main__':img_path = 'dog.png'bgr_img = cv2.imread(img_path)gray_img = cv2.cvtColor(bgr_img, cv2.COLOR_BGR2GRAY)print(gray_img.shape)# Normalisegray_img = gray_img.astype("float32") / 255filters=get_filters()print(filters.shape)vis_filter(filters)# instantiate the model and set the weightsweight = torch.from_numpy(filters).unsqueeze(1).type(torch.FloatTensor)print(weight.shape)model = Net(weight)# print out the layer in the networkprint(model)gray_img_tensor = torch.from_numpy(gray_img).unsqueeze(0).unsqueeze(1)print(gray_img_tensor.shape)conv_img,relu_img,pool_img=model(gray_img_tensor)print(conv_img.shape)print(relu_img.shape)print(pool_img.shape)conv_img=conv_img.detach().numpy().squeeze()relu_img=relu_img.detach().numpy().squeeze()pool_img=pool_img.detach().numpy().squeeze()print(conv_img.shape)vis_filter(conv_img)vis_filter(relu_img)vis_filter(pool_img)conv輸出
relu輸出
pool輸出
繪圖形式換成:cmap=plt.cm.jet
conv輸出結果
relu輸出結果
pool輸出結果
總結
以上是生活随笔為你收集整理的深度学习可视化的一些工具+pytorch实现回归与卷积可视化的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: OpenCV与图像处理学习十五——LBP
- 下一篇: Deep Alignment Netwo