一.深度学习可视化的一些工具
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) * 5
y = x * 2 + 3 + np.random.rand(batch_size, 1)*2
return x, y
def get_gradient(theta,x,y):
m=x.shape[0]
Y_estimate=np.dot(x,theta)
assert (Y_estimate.shape==(m,))
error=Y_estimate-y
assert (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*grad
if i%100==0:
print('{} iterations cost={}'.format(i,cost))
costs.append(cost)
theta=new_theta
return costs,theta
def 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=32
data_x, data_y = get_fake_data(batch_size=batch_size)
#添加一列为1的向量 实际上就是乘以 theta 就是b
data_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 t
device=t.device('cpu')
def get_fake_data(batch_size=8):
''' 产生随机数据:y=x*2+3,加上了一些噪声'''
x = t.rand(batch_size, 1,device=device) * 5
y = x * 2 + 3 + t.rand(batch_size, 1)*2
return x, y
def 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=32
data_x, data_y = get_fake_data(batch_size=batch_size)
#添加一列为1的向量 实际上就是乘以 theta 就是b
data_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=500
lr = 0.005 # 学习率
losses=[]
for i in range(iterations):
# forward:计算loss
y_pred = data_x.mm(theta)
print('y_pred',y_pred.shape)
loss = 1/(2*m) * (y_pred - data_y) ** 2
print('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()) # predicted
plt.show()
三.实现ResNet34
from torch import nn
import torch as t
from torch.nn import functional as F
class 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 = shortcut
def forward(self, x):
out = self.left(x)
residual = x if self.right is None else self.right(x)
out += residual
return F.relu(out)
class ResNet(nn.Module):
'''
实现主module:ResNet34
ResNet34 包含多个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 block
self.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 Filters
filter_1 = filter_vals
filter_2 = -filter_1
filter_3 = filter_1.T
filter_4 = -filter_3
filters = np.array([filter_1, filter_2, filter_3, filter_4])
return filters
def vis_filter(filters):
# Check the Filters
fig = 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 Filters
filter_1 = filter_vals
filter_2 = -filter_1
filter_3 = filter_1.T
filter_4 = -filter_3
filters = np.array([filter_1, filter_2, filter_3, filter_4])
return filters
def vis_filter(filters):
# Check the Filters
fig = 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 F
class 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 filters
k_height, k_width = weight.shape[2:]
# assumes there are 4 grayscale filters
self.conv = nn.Conv2d(1, 4, kernel_size=(k_height, k_width), bias=False)
# initializes the weights of the convolutional layer
self.conv.weight = torch.nn.Parameter(weight)
print(self.conv.weight.shape)
# define a pooling layer
self.pool = nn.MaxPool2d(2, 2)
def forward(self, x):
# calculates the output of a convolutional layer
# pre- and post-activation
conv_x = self.conv(x)
activated_x = F.relu(conv_x)
# applies pooling layer
pooled_x = self.pool(activated_x)
# returns all layers
return conv_x, activated_x, pooled_x
if __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)
# Normalise
gray_img = gray_img.astype("float32") / 255
filters=get_filters()
print(filters.shape)
vis_filter(filters)
# instantiate the model and set the weights
weight = torch.from_numpy(filters).unsqueeze(1).type(torch.FloatTensor)
print(weight.shape)
model = Net(weight)
# print out the layer in the network
print(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输出结果