├── 1 搭建一个超简单的网络.py ├── 10 pytorch常见运算.py ├── 11 MobileNet PyTorch.py ├── 12 SENet PyTorch.py ├── 13 EfficientNet PyTorch.py ├── 14 tensorboardX可视化MNIST.py ├── 15 TF之FashionMNIST.py ├── 16 TF的数据集构建 ├── 16 TF的数据集构建.py ├── bug1.jpeg └── bug2.jpeg ├── 17 TFrec文件的创建与读取 ├── 17 .1 TFrec文件的创建.py ├── 17 .2 TFrec文件的读取.py ├── bug1.jpeg ├── bug2.jpeg ├── bug3.jpeg ├── bug4.jpeg ├── bug5.jpeg ├── bug6.jpeg ├── bug7.jpeg ├── bug8.jpeg └── train.tfrec ├── 18 TF2构建网络模型 ├── 18.1 TF2构建自定义网络层.py └── 18.2 TF2构建常见卷积网络.py ├── 19 TF2模型的存储与载入 ├── 19.1 模型参数存储载入.py ├── checkpoint ├── model_weight.data-00000-of-00001 └── model_weight.index ├── 20 TF2的eager模式与求导 ├── 20.1 eager模式.py └── 20.2 二阶导数的eager计算.py ├── 21 Keras的API详解 ├── 21.1 简单例子.py ├── 21.2 初始化.py ├── 21.3 激活函数.py ├── 21.4 正则化.py ├── 21.5 去卷积.py ├── 21.6 池化层.py └── 21.7 Normalization层.py ├── 3 浅谈Dataset和Dataloader.py ├── 4 构建模型三要素与权重初始化.py ├── 5 torchvision全函数.py ├── 6.1 模型构建(进阶).py ├── 6.2 模型遍历与存储(进阶).py ├── 7 torchvision.transform常用API.py ├── 8.1 MNIST小试牛刀之EDA.py ├── 8.2 MNIST小试牛刀之训练与推理.py ├── 9.1 PyTorch的数据结构.py ├── 9.2 torch vs numpy.py ├── 9.3 torch张量.py └── README.md /1 搭建一个超简单的网络.py: -------------------------------------------------------------------------------- 1 | import torch 2 | import torch.nn as nn 3 | import numpy as np 4 | 5 | # 构建输入集 6 | x = np.mat('0 0;' 7 | '0 1;' 8 | '1 0;' 9 | '1 1') 10 | x = torch.tensor(x).float() 11 | y = np.mat('1;' 12 | '0;' 13 | '0;' 14 | '1') 15 | y = torch.tensor(y).float() 16 | 17 | myNet = nn.Sequential( 18 | nn.Linear(2,10), 19 | nn.ReLU(), 20 | nn.Linear(10,1), 21 | nn.Sigmoid() 22 | ) 23 | print(myNet) 24 | 25 | optimzer = torch.optim.SGD(myNet.parameters(),lr=0.05) 26 | loss_func = nn.MSELoss() 27 | 28 | for epoch in range(5000): 29 | out = myNet(x) 30 | loss = loss_func(out,y) 31 | optimzer.zero_grad() 32 | loss.backward() 33 | optimzer.step() 34 | 35 | print(myNet(x).data) -------------------------------------------------------------------------------- /10 pytorch常见运算.py: -------------------------------------------------------------------------------- 1 | import torch 2 | a = torch.tensor([1.,2.]) 3 | b = torch.tensor([2.,3.]) 4 | #------------------------- 5 | print('减法') 6 | c1 = a - b 7 | c2 = torch.sub(a, b) 8 | print(c1,c2) 9 | #------------------------- 10 | print('乘法') 11 | c1 = a * b 12 | c2 = torch.mul(a, b) 13 | print(c1,c2) 14 | #------------------------- 15 | print('除法') 16 | c1 = a / b 17 | c2 = torch.div(a, b) 18 | print(c1,c2) 19 | #------------------------- 20 | print('加法') 21 | c1 = a + b 22 | c2 = torch.add(a, b) 23 | print(c1,c2) 24 | #------------------------- 25 | print('矩阵乘法') 26 | a = torch.tensor([1.,2.]).view(2,1) 27 | b = torch.tensor([2.,3.]).view(1,2) 28 | print(torch.mm(a, b)) 29 | print(torch.matmul(a, b)) 30 | print(a @ b) 31 | #------------------------- 32 | print('多维张量矩阵乘法') 33 | a = torch.rand((3,2,64,32)) 34 | b = torch.rand((1,2,32,64)) 35 | print(torch.matmul(a, b).shape) 36 | #------------------------- 37 | print('幂运算') 38 | a = torch.tensor([1.,2.]) 39 | b = torch.tensor([2.,3.]) 40 | c1 = a ** b 41 | c2 = torch.pow(a, b) 42 | print(c1,c2) 43 | #------------------------- 44 | print('幂运算') 45 | a = torch.tensor([1.,2.]) 46 | b = torch.tensor([2.,3.]) 47 | c1 = a ** b 48 | c2 = torch.pow(a, b) 49 | print(c1,c2) 50 | #------------------------- 51 | import numpy as np 52 | print('对数运算') 53 | a = torch.tensor([2,10,np.e]) 54 | print(torch.log(a)) 55 | print(torch.log2(a)) 56 | print(torch.log10(a)) 57 | #------------------------- 58 | print('近似运算') 59 | a = torch.tensor(1.2345) 60 | print(a.ceil()) 61 | print(a.floor()) 62 | print(a.trunc()) 63 | print(a.frac()) 64 | print(a.round()) 65 | #------------------------- 66 | print('剪裁运算') 67 | a = torch.rand(5) 68 | print(a) 69 | print(a.clamp(0.3,0.7)) 70 | -------------------------------------------------------------------------------- /11 MobileNet PyTorch.py: -------------------------------------------------------------------------------- 1 | import torch 2 | import torch.nn as nn 3 | import torch.nn.functional as F 4 | 5 | 6 | class Block(nn.Module): 7 | '''Depthwise conv + Pointwise conv''' 8 | def __init__(self, in_planes, out_planes, stride=1): 9 | super(Block, self).__init__() 10 | self.conv1 = nn.Conv2d\ 11 | (in_planes, in_planes, kernel_size=3, stride=stride, 12 | padding=1, groups=in_planes, bias=False) 13 | self.bn1 = nn.BatchNorm2d(in_planes) 14 | self.conv2 = nn.Conv2d\ 15 | (in_planes, out_planes, kernel_size=1, 16 | stride=1, padding=0, bias=False) 17 | self.bn2 = nn.BatchNorm2d(out_planes) 18 | 19 | def forward(self, x): 20 | out = F.relu(self.bn1(self.conv1(x))) 21 | out = F.relu(self.bn2(self.conv2(out))) 22 | return out 23 | 24 | 25 | class MobileNet(nn.Module): 26 | # (128,2) means conv planes=128, conv stride=2, 27 | # by default conv stride=1 28 | cfg = [64, (128,2), 128, (256,2), 256, (512,2), 29 | 512, 512, 512, 512, 512, (1024,2), 1024] 30 | 31 | def __init__(self, num_classes=10): 32 | super(MobileNet, self).__init__() 33 | self.conv1 = nn.Conv2d(3, 32, kernel_size=3, 34 | stride=1, padding=1, bias=False) 35 | self.bn1 = nn.BatchNorm2d(32) 36 | self.layers = self._make_layers(in_planes=32) 37 | self.linear = nn.Linear(1024, num_classes) 38 | 39 | def _make_layers(self, in_planes): 40 | layers = [] 41 | for x in self.cfg: 42 | out_planes = x if isinstance(x, int) else x[0] 43 | stride = 1 if isinstance(x, int) else x[1] 44 | layers.append(Block(in_planes, out_planes, stride)) 45 | in_planes = out_planes 46 | return nn.Sequential(*layers) 47 | 48 | def forward(self, x): 49 | out = F.relu(self.bn1(self.conv1(x))) 50 | out = self.layers(out) 51 | out = F.adaptive_avg_pool2d(out,output_size=1) 52 | out = out.view(out.size(0), -1) 53 | out = self.linear(out) 54 | return out 55 | 56 | 57 | net = MobileNet() 58 | x = torch.randn(1,3,32,32) 59 | y = net(x) 60 | print(y.size()) 61 | print(net) 62 | 63 | -------------------------------------------------------------------------------- /12 SENet PyTorch.py: -------------------------------------------------------------------------------- 1 | import torch 2 | import torch.nn as nn 3 | import torch.nn.functional as F 4 | 5 | class PreActBlock(nn.Module): 6 | def __init__(self, in_planes, planes, stride=1): 7 | super(PreActBlock, self).__init__() 8 | self.bn1 = nn.BatchNorm2d(in_planes) 9 | self.conv1 = nn.Conv2d(in_planes, planes, kernel_size=3, stride=stride, padding=1, bias=False) 10 | self.bn2 = nn.BatchNorm2d(planes) 11 | self.conv2 = nn.Conv2d(planes, planes, kernel_size=3, stride=1, padding=1, bias=False) 12 | 13 | if stride != 1 or in_planes != planes: 14 | self.shortcut = nn.Sequential( 15 | nn.Conv2d(in_planes, planes, kernel_size=1, stride=stride, bias=False) 16 | ) 17 | 18 | # SE layers 19 | self.fc1 = nn.Conv2d(planes, planes//16, kernel_size=1) 20 | self.fc2 = nn.Conv2d(planes//16, planes, kernel_size=1) 21 | 22 | def forward(self, x): 23 | out = F.relu(self.bn1(x)) 24 | shortcut = self.shortcut(out) if hasattr(self, 'shortcut') else x 25 | out = self.conv1(out) 26 | out = self.conv2(F.relu(self.bn2(out))) 27 | 28 | # Squeeze 29 | w = F.avg_pool2d(out, out.size(2)) 30 | w = F.relu(self.fc1(w)) 31 | w = F.sigmoid(self.fc2(w)) 32 | # Excitation 33 | out = out * w 34 | 35 | out += shortcut 36 | return out 37 | 38 | 39 | class SENet(nn.Module): 40 | def __init__(self, block, num_blocks, num_classes=10): 41 | super(SENet, self).__init__() 42 | self.in_planes = 64 43 | 44 | self.conv1 = nn.Conv2d(3, 64, kernel_size=3, stride=1, padding=1, bias=False) 45 | self.bn1 = nn.BatchNorm2d(64) 46 | self.layer1 = self._make_layer(block, 64, num_blocks[0], stride=1) 47 | self.layer2 = self._make_layer(block, 128, num_blocks[1], stride=2) 48 | self.layer3 = self._make_layer(block, 256, num_blocks[2], stride=2) 49 | self.layer4 = self._make_layer(block, 512, num_blocks[3], stride=2) 50 | self.linear = nn.Linear(512, num_classes) 51 | 52 | def _make_layer(self, block, planes, num_blocks, stride): 53 | strides = [stride] + [1]*(num_blocks-1) 54 | layers = [] 55 | for stride in strides: 56 | layers.append(block(self.in_planes, planes, stride)) 57 | self.in_planes = planes 58 | return nn.Sequential(*layers) 59 | 60 | def forward(self, x): 61 | out = F.relu(self.bn1(self.conv1(x))) 62 | out = self.layer1(out) 63 | out = self.layer2(out) 64 | out = self.layer3(out) 65 | out = self.layer4(out) 66 | out = F.avg_pool2d(out, 4) 67 | out = out.view(out.size(0), -1) 68 | out = self.linear(out) 69 | return out 70 | 71 | 72 | def SENet18(): 73 | return SENet(PreActBlock, [2,2,2,2]) 74 | 75 | 76 | net = SENet18() 77 | y = net(torch.randn(1,3,32,32)) 78 | print(y.size()) 79 | print(net) 80 | 81 | -------------------------------------------------------------------------------- /13 EfficientNet PyTorch.py: -------------------------------------------------------------------------------- 1 | from efficientnet_pytorch import EfficientNet 2 | import efficientnet_pytorch 3 | import torch 4 | model = EfficientNet.from_name('efficientnet-b0') 5 | stage1 = [];stage2 = [];stage3 = [];stage4 = [];stem=[] 6 | # for i,module in enumerate(model.modules()): 7 | # if i in [1,2,3]: 8 | # stem.append(module) 9 | # if i in [5,17,32]: 10 | # stage1.append(module) 11 | # if i in [47,62]: 12 | # stage2.append(module) 13 | # if i in [77,92,107,122,137,152]: 14 | # stage3.append(module) 15 | # if i in [167,182,197,212,227]: 16 | # stage4.append(module) 17 | # stem = torch.nn.Sequential(*stem) 18 | # stage1 = torch.nn.Sequential(*stage1) 19 | # stage2 = torch.nn.Sequential(*stage2) 20 | # stage3 = torch.nn.Sequential(*stage3) 21 | # stage4 = torch.nn.Sequential(*stage4) 22 | # 23 | # for module in stage1.modules(): 24 | # if isinstance(module,torch.nn.BatchNorm2d): 25 | # stage1_out_channels = module.weight.shape[0] 26 | # for module in stage2.modules(): 27 | # if isinstance(module,torch.nn.BatchNorm2d): 28 | # stage2_out_channels = module.weight.shape[0] 29 | # for module in stage3.modules(): 30 | # if isinstance(module,torch.nn.BatchNorm2d): 31 | # stage3_out_channels = module.weight.shape[0] 32 | # for module in stage4.modules(): 33 | # if isinstance(module,torch.nn.BatchNorm2d): 34 | # stage4_out_channels = module.weight.shape[0] 35 | stage = [[],[],[],[],[],[]];ind = 0 36 | for i,module in enumerate(model.modules()): 37 | if isinstance(module,efficientnet_pytorch.model.MBConvBlock): 38 | if ind == 0: 39 | ind += 1 40 | if module._depthwise_conv.stride[0] == 2: 41 | ind += 1 42 | stage[ind].append(module) 43 | elif module._depthwise_conv.stride[0] == 1: 44 | stage[ind].append(module) 45 | elif i in [1,2,3]: 46 | stage[0].append(module) 47 | stem = torch.nn.Sequential(*stage[0]) 48 | stage1 = torch.nn.Sequential(*stage[1]) 49 | stage2 = torch.nn.Sequential(*stage[2]) 50 | stage3 = torch.nn.Sequential(*stage[3]) 51 | stage4 = torch.nn.Sequential(*stage[4]) 52 | stage5 = torch.nn.Sequential(*stage[5]) 53 | for module in stage1.modules(): 54 | if isinstance(module,torch.nn.BatchNorm2d): 55 | stage1_out_channels = module.weight.shape[0] 56 | for module in stage2.modules(): 57 | if isinstance(module,torch.nn.BatchNorm2d): 58 | stage2_out_channels = module.weight.shape[0] 59 | for module in stage3.modules(): 60 | if isinstance(module,torch.nn.BatchNorm2d): 61 | stage3_out_channels = module.weight.shape[0] 62 | for module in stage4.modules(): 63 | if isinstance(module,torch.nn.BatchNorm2d): 64 | stage4_out_channels = module.weight.shape[0] 65 | for module in stage5.modules(): 66 | if isinstance(module,torch.nn.BatchNorm2d): 67 | stage5_out_channels = module.weight.shape[0] 68 | 69 | x1 = stem(torch.rand(1,3,1000,1000)) 70 | x1 = stage1(x1) 71 | 72 | print(x1.shape) 73 | exit() 74 | print(stage1) 75 | print(stage2) 76 | print(stage3) 77 | print(stage4) 78 | print(stage5) 79 | -------------------------------------------------------------------------------- /14 tensorboardX可视化MNIST.py: -------------------------------------------------------------------------------- 1 | import pandas as pd 2 | import torch 3 | from torch.utils.data import Dataset,DataLoader 4 | from torchvision import transforms 5 | import numpy as np 6 | import torch.nn as nn 7 | import torch.optim as optim 8 | import matplotlib.pyplot as plt 9 | from torchvision.utils import make_grid 10 | # 导入可视化模块 11 | from tensorboardX import SummaryWriter 12 | writer = SummaryWriter('../result_tensorboard') 13 | 14 | train_df = pd.read_csv('./MNIST_csv/train.csv') 15 | test_df = pd.read_csv('./MNIST_csv/test.csv') 16 | n_train = len(train_df) 17 | n_test = len(test_df) 18 | n_pixels = len(train_df.columns) - 1 19 | n_class = len(set(train_df['label'])) 20 | 21 | class MNIST_data(Dataset): 22 | def __init__(self, file_path, 23 | transform=transforms.Compose([transforms.ToPILImage(), transforms.ToTensor(), 24 | transforms.Normalize(mean=(0.5,), std=(0.5,))]) 25 | ): 26 | df = pd.read_csv(file_path) 27 | if len(df.columns) == n_pixels: 28 | # test data 29 | self.X = df.values.reshape((-1, 28, 28)).astype(np.uint8)[:,:,:,None] 30 | self.y = None 31 | else: 32 | # training data 33 | self.X = df.iloc[:, 1:].values.reshape((-1, 28, 28)).astype(np.uint8)[:, :, :, None] 34 | self.y = torch.from_numpy(df.iloc[:, 0].values) 35 | self.transform = transform 36 | 37 | def __len__(self): 38 | return len(self.X) 39 | 40 | def __getitem__(self, idx): 41 | if self.y is not None: 42 | return self.transform(self.X[idx]), self.y[idx] 43 | else: 44 | return self.transform(self.X[idx]) 45 | 46 | 47 | batch_size = 64 48 | 49 | train_dataset = MNIST_data('./MNIST_csv/train.csv', 50 | transform= transforms.Compose([ 51 | transforms.ToPILImage(), 52 | transforms.Grayscale(num_output_channels=3), 53 | transforms.RandomRotation(degrees=20), 54 | transforms.ToTensor(), 55 | transforms.Normalize(mean=(0.5,), std=(0.5,))])) 56 | test_dataset = MNIST_data('./MNIST_csv/test.csv', 57 | transform=transforms.Compose([ 58 | transforms.ToPILImage(), 59 | transforms.Grayscale(num_output_channels=3), 60 | transforms.ToTensor(), 61 | transforms.Normalize(mean=(0.5,), std=(0.5,))])) 62 | 63 | train_loader = torch.utils.data.DataLoader(dataset=train_dataset, 64 | batch_size=batch_size, shuffle=True) 65 | test_loader = torch.utils.data.DataLoader(dataset=test_dataset, 66 | batch_size=batch_size, shuffle=False) 67 | 68 | 69 | class Net(nn.Module): 70 | def __init__(self): 71 | super(Net, self).__init__() 72 | 73 | self.features1 = nn.Conv2d(3, 32, kernel_size=3, stride=1, padding=1) 74 | self.features = nn.Sequential( 75 | nn.BatchNorm2d(32), 76 | nn.ReLU(inplace=True), 77 | nn.Conv2d(32, 32, kernel_size=3, stride=1, padding=1), 78 | nn.BatchNorm2d(32), 79 | nn.ReLU(inplace=True), 80 | nn.Conv2d(32, 32, kernel_size=3, stride=1, padding=1), 81 | nn.BatchNorm2d(32), 82 | nn.ReLU(inplace=True), 83 | nn.MaxPool2d(kernel_size=2, stride=2), 84 | nn.Conv2d(32, 64, kernel_size=3, padding=1), 85 | nn.BatchNorm2d(64), 86 | nn.ReLU(inplace=True), 87 | nn.Conv2d(64, 64, kernel_size=3, padding=1), 88 | nn.BatchNorm2d(64), 89 | nn.ReLU(inplace=True), 90 | nn.MaxPool2d(kernel_size=2, stride=2) 91 | ) 92 | 93 | self.classifier = nn.Sequential( 94 | nn.Dropout(p=0.5), 95 | nn.Linear(64 * 7 * 7, 512), 96 | nn.BatchNorm1d(512), 97 | nn.ReLU(inplace=True), 98 | nn.Dropout(p=0.5), 99 | nn.Linear(512, 512), 100 | nn.BatchNorm1d(512), 101 | nn.ReLU(inplace=True), 102 | nn.Dropout(p=0.5), 103 | nn.Linear(512, 10), 104 | ) 105 | 106 | for m in self.modules(): 107 | if isinstance(m, nn.Conv2d) or isinstance(m, nn.Linear): 108 | nn.init.xavier_uniform_(m.weight) 109 | elif isinstance(m, nn.BatchNorm2d): 110 | m.weight.data.fill_(1) 111 | m.bias.data.zero_() 112 | 113 | def forward(self, x): 114 | x = self.features1(x) 115 | x = self.features(x) 116 | x = x.view(x.size(0), -1) 117 | x = self.classifier(x) 118 | return x 119 | 120 | 121 | 122 | device = 'cuda' if torch.cuda.is_available() else 'cpu' 123 | model = Net().to(device) 124 | # writer.add_graph(model, (torch.rand([1,3,28,28]),)) 125 | # model = torchvision.models.resnet50(pretrained=True).to(device) 126 | optimizer = optim.Adam(model.parameters(), lr=0.003) 127 | criterion = nn.CrossEntropyLoss().to(device) 128 | exp_lr_scheduler = optim.lr_scheduler.StepLR(optimizer, step_size=7, gamma=0.1) 129 | print(model) 130 | 131 | def train(epoch): 132 | global tensorboard_ind 133 | model.train() 134 | for batch_idx, (data, target) in enumerate(train_loader): 135 | # 读入数据 136 | data = data.to(device) 137 | target = target.to(device) 138 | 139 | # 第一个batch记录数据 140 | if batch_idx == 0: 141 | out1 = model.features1(data[0:1,:,:,:]) 142 | out2 = model.features(out1) 143 | grid1 = make_grid(out1.view(-1,1,out1.shape[2],out1.shape[3]), nrow=8) 144 | grid2 = make_grid(out2.view(-1,1,out1.shape[2],out1.shape[3]), nrow=8) 145 | writer.add_image('features1', grid1, global_step=epoch) 146 | writer.add_image('features', grid2, global_step=epoch) 147 | 148 | # 计算模型预测结果和损失 149 | output = model(data) 150 | loss = criterion(output, target) 151 | 152 | optimizer.zero_grad() # 计算图梯度清零 153 | loss.backward() # 损失反向传播 154 | optimizer.step() # 然后更新参数 155 | if (batch_idx + 1) % 50 == 0: 156 | print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format( 157 | epoch, (batch_idx + 1) * len(data), len(train_loader.dataset), 158 | 100. * (batch_idx + 1) / len(train_loader), loss.item())) 159 | # log.append(loss.item()) 160 | writer.add_scalar('loss',loss.item(),tensorboard_ind) 161 | tensorboard_ind += 1 162 | print(tensorboard_ind) 163 | 164 | exp_lr_scheduler.step() 165 | 166 | n_epochs = 5 167 | tensorboard_ind = 0 168 | for epoch in range(n_epochs): 169 | train(epoch) 170 | # 每一个epoch之后输出网络中每一层的权重值的直方图 171 | for i, (name, param) in enumerate(model.named_parameters()): 172 | if 'bn' not in name: 173 | writer.add_histogram(name, param, epoch) 174 | # 卷积核的可视化 175 | for idx, (name, m) in enumerate(model.named_modules()): 176 | if name == 'features1': 177 | print(m.weight.shape) 178 | in_channels = m.weight.shape[1] 179 | out_channels = m.weight.shape[0] 180 | k_w,k_h = m.weight.shape[3],m.weight.shape[2] 181 | kernel_all = m.weight.view(-1, 1, k_w, k_h) # 每个通道的卷积核 182 | kernel_grid = make_grid(kernel_all, nrow=in_channels) 183 | writer.add_image(f'{name}_kernel', kernel_grid, global_step=epoch) 184 | writer.close() 185 | 186 | -------------------------------------------------------------------------------- /15 TF之FashionMNIST.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | from tensorflow import keras 3 | import numpy as np 4 | 5 | fashion_mnist = keras.datasets.fashion_mnist 6 | (train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data() 7 | print('train_images shape:',train_images.shape) 8 | print('train_labels shape:',train_labels.shape) 9 | print('test_images shape:',test_images.shape) 10 | print('test_labels shape:',test_labels.shape) 11 | 12 | train_images = train_images / 255.0 13 | test_images = test_images / 255.0 14 | 15 | # 模型搭建 16 | model = keras.Sequential([ 17 | keras.layers.Flatten(input_shape=(28, 28)), 18 | keras.layers.Dense(128, activation='relu'), 19 | keras.layers.Dense(10, activation='softmax') 20 | ]) 21 | 22 | model.compile(optimizer='adam', 23 | loss='sparse_categorical_crossentropy', 24 | metrics=['accuracy']) 25 | 26 | model.fit(train_images, train_labels, epochs=10) 27 | 28 | # %% [code] 29 | test_loss, test_acc = model.evaluate(test_images, test_labels) 30 | print('\nTest accuracy:', test_acc) 31 | 32 | # %% [code] 33 | predictions = model.predict(test_images) 34 | predictions[0] 35 | 36 | # %% [code] 37 | -------------------------------------------------------------------------------- /16 TF的数据集构建/16 TF的数据集构建.py: -------------------------------------------------------------------------------- 1 | from tensorflow import keras 2 | import tensorflow as tf 3 | from PIL import Image 4 | import numpy as np 5 | # image = Image.open('./bug1.jpeg') 6 | # image.show() 7 | # image = np.array(image) 8 | # print(image.shape) 9 | #-------------------------------- 10 | # images = tf.io.gfile.glob('./*.jpeg') 11 | # image = tf.io.read_file('./bug1.jpeg') 12 | # image = tf.image.decode_jpeg(image,channels=3) 13 | # print(image.shape,type(image)) 14 | #-------------------------------- 15 | def read_image(path): 16 | image = tf.io.read_file(path) 17 | image = tf.image.decode_jpeg(image, channels=3, ratio=1) 18 | image = tf.image.resize(image, [256, 256]) # 统一图片大小 19 | image = tf.cast(image, tf.float32) # 转换类型 20 | image = image / 255 # 归一化 21 | return image 22 | images = tf.io.gfile.glob('./*.jpeg') 23 | dataset = tf.data.Dataset.from_tensor_slices(images) 24 | AUTOTUNE = tf.data.experimental.AUTOTUNE 25 | dataset = dataset.map(read_image,num_parallel_calls=AUTOTUNE) 26 | dataset = dataset.shuffle(1).batch(1) 27 | for a in dataset.take(2): 28 | print(a.shape) 29 | #----------- 30 | # dataset = tf.data.TFRecordDataset(['bug1.jpeg','bug2.jpeg']) 31 | # image = tf.io.read_file(input_path) -------------------------------------------------------------------------------- /16 TF的数据集构建/bug1.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YixinChen-AI/pytorch-learner/dde23b71c36f12a3887517345fc068cc06d1a00b/16 TF的数据集构建/bug1.jpeg -------------------------------------------------------------------------------- /16 TF的数据集构建/bug2.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YixinChen-AI/pytorch-learner/dde23b71c36f12a3887517345fc068cc06d1a00b/16 TF的数据集构建/bug2.jpeg -------------------------------------------------------------------------------- /17 TFrec文件的创建与读取/17 .1 TFrec文件的创建.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | import glob 3 | # 先记录一下要保存的tfrec文件的名字 4 | tfrecord_file = './train.tfrec' 5 | # 获取指定目录的所有以jpeg结尾的文件list 6 | images = glob.glob('./*.jpeg') 7 | with tf.io.TFRecordWriter(tfrecord_file) as writer: 8 | for filename in images: 9 | image = open(filename, 'rb').read() # 读取数据集图片到内存,image 为一个 Byte 类型的字符串 10 | feature = { # 建立 tf.train.Feature 字典 11 | 'image': tf.train.Feature(bytes_list=tf.train.BytesList(value=[image])), # 图片是一个 Bytes 对象 12 | 'label': tf.train.Feature(int64_list=tf.train.Int64List(value=[1])), 13 | 'float':tf.train.Feature(float_list=tf.train.FloatList(value=[1.0,2.0])), 14 | 'name':tf.train.Feature(bytes_list=tf.train.BytesList(value=[str.encode(filename)])) 15 | } 16 | # tf.train.Example 在 tf.train.Features 外面又多了一层封装 17 | example = tf.train.Example(features=tf.train.Features(feature=feature)) # 通过字典建立 Example 18 | writer.write(example.SerializeToString()) # 将 Example 序列化并写入 TFRecord 文件 -------------------------------------------------------------------------------- /17 TFrec文件的创建与读取/17 .2 TFrec文件的读取.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | 3 | dataset = tf.data.TFRecordDataset('./train.tfrec') 4 | 5 | def decode(example): 6 | feature_description = { 7 | 'image': tf.io.FixedLenFeature([], tf.string), 8 | 'label': tf.io.FixedLenFeature([], tf.int64), 9 | 'float': tf.io.FixedLenFeature([1, 2], tf.float32), 10 | 'name': tf.io.FixedLenFeature([], tf.string) 11 | } 12 | feature_dict = tf.io.parse_single_example(example, feature_description) 13 | feature_dict['image'] = tf.io.decode_jpeg(feature_dict['image']) # 解码 JEPG 图片 14 | return feature_dict 15 | 16 | dataset = dataset.map(decode).batch(4) 17 | for i in dataset.take(1): 18 | print(i['image'].shape) 19 | print(i['label'].shape) 20 | print(i['float'].shape) 21 | print(bytes.decode(i['name'][0].numpy())) 22 | 23 | 24 | -------------------------------------------------------------------------------- /17 TFrec文件的创建与读取/bug1.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YixinChen-AI/pytorch-learner/dde23b71c36f12a3887517345fc068cc06d1a00b/17 TFrec文件的创建与读取/bug1.jpeg -------------------------------------------------------------------------------- /17 TFrec文件的创建与读取/bug2.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YixinChen-AI/pytorch-learner/dde23b71c36f12a3887517345fc068cc06d1a00b/17 TFrec文件的创建与读取/bug2.jpeg -------------------------------------------------------------------------------- /17 TFrec文件的创建与读取/bug3.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YixinChen-AI/pytorch-learner/dde23b71c36f12a3887517345fc068cc06d1a00b/17 TFrec文件的创建与读取/bug3.jpeg -------------------------------------------------------------------------------- /17 TFrec文件的创建与读取/bug4.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YixinChen-AI/pytorch-learner/dde23b71c36f12a3887517345fc068cc06d1a00b/17 TFrec文件的创建与读取/bug4.jpeg -------------------------------------------------------------------------------- /17 TFrec文件的创建与读取/bug5.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YixinChen-AI/pytorch-learner/dde23b71c36f12a3887517345fc068cc06d1a00b/17 TFrec文件的创建与读取/bug5.jpeg -------------------------------------------------------------------------------- /17 TFrec文件的创建与读取/bug6.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YixinChen-AI/pytorch-learner/dde23b71c36f12a3887517345fc068cc06d1a00b/17 TFrec文件的创建与读取/bug6.jpeg -------------------------------------------------------------------------------- /17 TFrec文件的创建与读取/bug7.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YixinChen-AI/pytorch-learner/dde23b71c36f12a3887517345fc068cc06d1a00b/17 TFrec文件的创建与读取/bug7.jpeg -------------------------------------------------------------------------------- /17 TFrec文件的创建与读取/bug8.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YixinChen-AI/pytorch-learner/dde23b71c36f12a3887517345fc068cc06d1a00b/17 TFrec文件的创建与读取/bug8.jpeg -------------------------------------------------------------------------------- /17 TFrec文件的创建与读取/train.tfrec: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YixinChen-AI/pytorch-learner/dde23b71c36f12a3887517345fc068cc06d1a00b/17 TFrec文件的创建与读取/train.tfrec -------------------------------------------------------------------------------- /18 TF2构建网络模型/18.1 TF2构建自定义网络层.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | import tensorflow.keras as keras 3 | 4 | class MyLayer(keras.layers.Layer): 5 | def __init__(self, input_dim=32, output_dim=32): 6 | super(MyLayer, self).__init__() 7 | 8 | w_init = tf.random_normal_initializer() 9 | self.weight = tf.Variable( 10 | initial_value=w_init(shape=(input_dim, output_dim), dtype=tf.float32), 11 | trainable=True) # 如果是false则是不参与梯度下降的变量 12 | 13 | b_init = tf.zeros_initializer() 14 | self.bias = tf.Variable(initial_value=b_init( 15 | shape=(output_dim), dtype=tf.float32), trainable=True) 16 | 17 | def call(self, inputs): 18 | return tf.matmul(inputs, self.weight) + self.bias 19 | 20 | 21 | x = tf.ones((3,5)) 22 | my_layer = MyLayer(input_dim=5, 23 | output_dim=10) 24 | out = my_layer(x) 25 | print(out.shape) -------------------------------------------------------------------------------- /18 TF2构建网络模型/18.2 TF2构建常见卷积网络.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | import tensorflow.keras as keras 3 | 4 | class CBR(keras.layers.Layer): 5 | def __init__(self,output_dim): 6 | super(CBR,self).__init__() 7 | self.conv = keras.layers.Conv2D(filters=output_dim, kernel_size=4, padding='same', strides=1) 8 | self.bn = keras.layers.BatchNormalization(axis=3) 9 | self.ReLU = keras.layers.ReLU() 10 | 11 | def call(self, inputs): 12 | inputs = self.conv(inputs) 13 | inputs = self.ReLU(self.bn(inputs)) 14 | return inputs 15 | 16 | class MyNet(keras.Model): 17 | def __init__ (self,input_dim=3): 18 | super(MyNet,self).__init__() 19 | self.cbr1 = CBR(16) 20 | self.maxpool1 = keras.layers.MaxPool2D(pool_size=(2,2)) 21 | self.cbr2 = CBR(32) 22 | self.maxpool2 = keras.layers.MaxPool2D(pool_size=(2,2)) 23 | 24 | def call(self, inputs): 25 | inputs = self.maxpool1(self.cbr1(inputs)) 26 | inputs = self.maxpool2(self.cbr2(inputs)) 27 | return inputs 28 | 29 | model = MyNet(3) 30 | data = tf.random.normal((16,224,224,3)) 31 | output = model(data) 32 | print(output.shape) 33 | -------------------------------------------------------------------------------- /19 TF2模型的存储与载入/19.1 模型参数存储载入.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | import tensorflow.keras as keras 3 | 4 | class CBR(keras.layers.Layer): 5 | def __init__(self,output_dim): 6 | super(CBR,self).__init__() 7 | self.conv = keras.layers.Conv2D(filters=output_dim, kernel_size=4, padding='same', strides=1) 8 | self.bn = keras.layers.BatchNormalization(axis=3) 9 | self.ReLU = keras.layers.ReLU() 10 | 11 | def call(self, inputs): 12 | inputs = self.conv(inputs) 13 | inputs = self.ReLU(self.bn(inputs)) 14 | return inputs 15 | 16 | class MyNet(keras.Model): 17 | def __init__ (self): 18 | super(MyNet,self).__init__() 19 | self.cbr1 = CBR(16) 20 | self.maxpool1 = keras.layers.MaxPool2D(pool_size=(2,2)) 21 | self.cbr2 = CBR(32) 22 | self.maxpool2 = keras.layers.MaxPool2D(pool_size=(2,2)) 23 | self.cbr3 = CBR(16) 24 | self.maxpool3 = keras.layers.MaxPool2D(pool_size=(2, 2)) 25 | self.cbr4 = CBR(1) 26 | 27 | def call(self, inputs): 28 | inputs = self.maxpool1(self.cbr1(inputs)) 29 | inputs = self.maxpool2(self.cbr2(inputs)) 30 | inputs = self.maxpool3(self.cbr3(inputs)) 31 | inputs = self.cbr4(inputs) 32 | return inputs 33 | 34 | model = MyNet() 35 | model.build((16,8,8,3)) 36 | print(model.summary()) 37 | 38 | 39 | # model.save('save_model.h5') 40 | # new_model = keras.models.load_model('save_model.h5') 41 | model.save_weights('model_weight') 42 | new_model = MyNet() 43 | new_model.load_weights('model_weight') 44 | # 看一下原来的模型和载入的模型预测相同的样本的输出 45 | test = tf.ones((1,8,8,3)) 46 | prediction = model.predict(test) 47 | new_prediction = new_model.predict(test) 48 | print(prediction,new_prediction) 49 | -------------------------------------------------------------------------------- /19 TF2模型的存储与载入/checkpoint: -------------------------------------------------------------------------------- 1 | model_checkpoint_path: "model_weight" 2 | all_model_checkpoint_paths: "model_weight" 3 | -------------------------------------------------------------------------------- /19 TF2模型的存储与载入/model_weight.data-00000-of-00001: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YixinChen-AI/pytorch-learner/dde23b71c36f12a3887517345fc068cc06d1a00b/19 TF2模型的存储与载入/model_weight.data-00000-of-00001 -------------------------------------------------------------------------------- /19 TF2模型的存储与载入/model_weight.index: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YixinChen-AI/pytorch-learner/dde23b71c36f12a3887517345fc068cc06d1a00b/19 TF2模型的存储与载入/model_weight.index -------------------------------------------------------------------------------- /20 TF2的eager模式与求导/20.1 eager模式.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | x = tf.convert_to_tensor(10.) 3 | w = tf.Variable(2.) 4 | b = tf.Variable(3.) 5 | with tf.GradientTape(persistent=True) as tape: 6 | z = w * x + b 7 | dz_dw = tape.gradient(z,w) 8 | dz_db = tape.gradient(z,b) 9 | print(dz_dw) 10 | print(dz_db) 11 | -------------------------------------------------------------------------------- /20 TF2的eager模式与求导/20.2 二阶导数的eager计算.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | x = tf.Variable(1.0) 3 | with tf.GradientTape() as t1: 4 | with tf.GradientTape() as t2: 5 | y = x * x * x 6 | dy_dx = t2.gradient(y, x) 7 | print(dy_dx) 8 | d2y_d2x = t1.gradient(dy_dx, x) 9 | print(d2y_d2x) 10 | -------------------------------------------------------------------------------- /21 Keras的API详解/21.1 简单例子.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | input_shape = (4, 28, 28, 3) 3 | x = tf.random.normal(input_shape) 4 | y = tf.keras.layers.Conv2D( 5 | filters=2,kernel_size=3, 6 | activation='relu',padding='same' 7 | ) 8 | print(y(x).shape) -------------------------------------------------------------------------------- /21 Keras的API详解/21.2 初始化.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | import tensorflow as tf 3 | 4 | class MyInitializer(tf.keras.initializers.Initializer): 5 | 6 | def __init__(self, mean, stddev): 7 | self.mean = mean 8 | self.stddev = stddev 9 | 10 | def __call__(self, shape, dtype=None): 11 | return tf.random.normal( 12 | shape, mean=self.mean, stddev=self.stddev, dtype=dtype) 13 | 14 | def get_config(self): # To support serialization 15 | return {'mean': self.mean, 'stddev': self.stddev} 16 | 17 | input_shape = (4, 28, 28, 3) 18 | initializer = MyInitializer(mean=0., stddev=1.) 19 | x = tf.random.normal(input_shape) 20 | y = tf.keras.layers.Conv2D( 21 | filters=2,kernel_size=3, 22 | activation='relu',padding='same', 23 | kernel_initializer=initializer, 24 | bias_initializer=initializer 25 | ) 26 | print(y(x).shape) -------------------------------------------------------------------------------- /21 Keras的API详解/21.3 激活函数.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | import tensorflow as tf 3 | 4 | class MyInitializer(tf.keras.initializers.Initializer): 5 | 6 | def __init__(self, mean, stddev): 7 | self.mean = mean 8 | self.stddev = stddev 9 | 10 | def __call__(self, shape, dtype=None): 11 | return tf.random.normal( 12 | shape, mean=self.mean, stddev=self.stddev, dtype=dtype) 13 | 14 | def get_config(self): # To support serialization 15 | return {'mean': self.mean, 'stddev': self.stddev} 16 | 17 | input_shape = (4, 28, 28, 3) 18 | initializer = MyInitializer(mean=0., stddev=1.) 19 | x = tf.random.normal(input_shape) 20 | y = tf.keras.layers.Conv2D( 21 | filters=2,kernel_size=3, 22 | activation='selu',padding='same', 23 | kernel_initializer=initializer, 24 | bias_initializer=initializer 25 | ) 26 | print(y(x).shape) -------------------------------------------------------------------------------- /21 Keras的API详解/21.4 正则化.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | import tensorflow as tf 3 | 4 | class MyInitializer(tf.keras.initializers.Initializer): 5 | 6 | def __init__(self, mean, stddev): 7 | self.mean = mean 8 | self.stddev = stddev 9 | 10 | def __call__(self, shape, dtype=None): 11 | return tf.random.normal( 12 | shape, mean=self.mean, stddev=self.stddev, dtype=dtype) 13 | 14 | def get_config(self): # To support serialization 15 | return {'mean': self.mean, 'stddev': self.stddev} 16 | 17 | class MyRegularizer(tf.keras.regularizers.Regularizer): 18 | 19 | def __init__(self, strength): 20 | self.strength = strength 21 | 22 | def __call__(self, x): 23 | return self.strength * tf.reduce_sum(tf.square(x)) 24 | 25 | input_shape = (4, 28, 28, 3) 26 | initializer = MyInitializer(mean=0., stddev=1.) 27 | x = tf.random.normal(input_shape) 28 | y = tf.keras.layers.Conv2D( 29 | filters=2,kernel_size=3, 30 | activation='selu',padding='same', 31 | kernel_initializer=initializer, 32 | bias_initializer=initializer, 33 | kernel_regularizer=MyRegularizer, 34 | ) 35 | print(y(x).shape) -------------------------------------------------------------------------------- /21 Keras的API详解/21.5 去卷积.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | from tensorflow import keras 3 | 4 | input_shape = (4, 28, 28, 3) 5 | x = tf.random.normal(input_shape) 6 | y = keras.layers.Conv2DTranspose( 7 | filters=10,kernel_size=3,strides=1,padding='same') 8 | print(y(x).shape) 9 | 10 | input_shape = (4, 28, 28, 3) 11 | x = tf.random.normal(input_shape) 12 | y = keras.layers.Conv2DTranspose( 13 | filters=10,kernel_size=3,strides=1) 14 | print(y(x).shape) 15 | 16 | input_shape = (4, 28, 28, 3) 17 | x = tf.random.normal(input_shape) 18 | y = keras.layers.Conv2DTranspose( 19 | filters=10,kernel_size=3,strides=2) 20 | print(y(x).shape) 21 | 22 | input_shape = (4, 28, 28, 3) 23 | x = tf.random.normal(input_shape) 24 | y = keras.layers.Conv2DTranspose( 25 | filters=10,kernel_size=3,strides=2,padding='same') 26 | print(y(x).shape) 27 | 28 | -------------------------------------------------------------------------------- /21 Keras的API详解/21.6 池化层.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | x = tf.random.normal((4,28,28,3)) 3 | y = tf.keras.layers.MaxPooling2D( 4 | pool_size=(2,2), 5 | strides = 1, 6 | padding='same') 7 | print(y(x).shape) 8 | #------------------------------------- 9 | import tensorflow as tf 10 | x = tf.random.normal((4,28,28,3)) 11 | y = tf.keras.layers.GlobalMaxPooling2D() 12 | print(y(x).shape) -------------------------------------------------------------------------------- /21 Keras的API详解/21.7 Normalization层.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | import numpy as np 3 | x = tf.constant(np.arange(10).reshape(5,2)*10, 4 | dtype=tf.float32) 5 | print(x) 6 | y = tf.keras.layers.LayerNormalization(axis=1) 7 | print(y(x)) -------------------------------------------------------------------------------- /3 浅谈Dataset和Dataloader.py: -------------------------------------------------------------------------------- 1 | import torch 2 | from torch.utils.data import Dataset,DataLoader 3 | 4 | class MyDataset(Dataset): 5 | def __init__(self): 6 | self.data = torch.tensor([[1,2,3],[2,3,4],[3,4,5],[4,5,6]]) 7 | self.label = torch.LongTensor([1,1,0,0]) 8 | 9 | def __getitem__(self,index): 10 | return self.data[index],self.label[index] 11 | 12 | def __len__(self): 13 | return len(self.data) 14 | #-------------------------------------------- 15 | print('第一部分') 16 | mydataset = MyDataset() 17 | mydataloader = DataLoader(dataset=mydataset, 18 | batch_size=1) 19 | 20 | for i,(data,label) in enumerate(mydataloader): 21 | print(data,label) 22 | #-------------------------------------------- 23 | print('第二部分') 24 | mydataloader = DataLoader(dataset=mydataset, 25 | batch_size=2, 26 | shuffle=True) 27 | 28 | for i,(data,label) in enumerate(mydataloader): 29 | print(data,label) 30 | #-------------------------------------------- 31 | print('第三部分') 32 | device = 'cuda' if torch.cuda.is_available() else 'cpu' 33 | for i,(data,label) in enumerate(mydataloader): 34 | data = data.to(device) 35 | label = label.to(device) 36 | print(data,label) -------------------------------------------------------------------------------- /4 构建模型三要素与权重初始化.py: -------------------------------------------------------------------------------- 1 | import torch 2 | import torch.nn as nn 3 | import torch.nn.functional as F 4 | from torch.utils.data import Dataset,DataLoader 5 | 6 | class Net(nn.Module): 7 | def __init__(self): 8 | super(Net, self).__init__() 9 | self.conv1 = nn.Conv2d(3, 6, 5) 10 | self.pool1 = nn.MaxPool2d(2, 2) 11 | self.conv2 = nn.Conv2d(6, 16, 5) 12 | self.pool2 = nn.MaxPool2d(2, 2) 13 | self.fc1 = nn.Linear(16 * 5 * 5, 120) 14 | self.fc2 = nn.Linear(120, 84) 15 | self.fc3 = nn.Linear(84, 10) 16 | 17 | def forward(self, x): 18 | x = self.pool1(F.relu(self.conv1(x))) 19 | x = self.pool2(F.relu(self.conv2(x))) 20 | x = x.view(-1, 16 * 5 * 5) 21 | x = F.relu(self.fc1(x)) 22 | x = F.relu(self.fc2(x)) 23 | x = self.fc3(x) 24 | return x 25 | 26 | def initialize_weights(self): 27 | for m in self.modules(): 28 | if isinstance(m, nn.Conv2d): 29 | torch.nn.init.xavier_normal_(m.weight.data) 30 | if m.bias is not None: 31 | m.bias.data.zero_() 32 | elif isinstance(m, nn.BatchNorm2d): 33 | m.weight.data.fill_(1) 34 | m.bias.data.zero_() 35 | elif isinstance(m, nn.Linear): 36 | torch.nn.init.normal_(m.weight.data, 0, 0.01) 37 | # m.weight.data.normal_(0,0.01) 38 | m.bias.data.zero_() 39 | 40 | net = Net() 41 | net.initialize_weights() 42 | print(net.modules()) 43 | for m in net.modules(): 44 | print(m) -------------------------------------------------------------------------------- /5 torchvision全函数.py: -------------------------------------------------------------------------------- 1 | import torchvision 2 | import torch 3 | import torchvision.models as models 4 | 5 | mydataset = torchvision.datasets.MNIST(root='./', 6 | train=True, 7 | transform=None, 8 | target_transform=None, 9 | download=True) 10 | 11 | resnet18 = models.resnet18(pretrained=True) 12 | print(resnet18.layer3) 13 | exit() 14 | alexnet = models.alexnet(pretrained=True) 15 | squeezenet = models.squeezenet1_0(pretrained=True) 16 | vgg16 = models.vgg16(pretrained=True) 17 | densenet = models.densenet161(pretrained=True) 18 | inception = models.inception_v3(pretrained=True) 19 | googlenet = models.googlenet(pretrained=True) 20 | shufflenet = models.shufflenet_v2_x1_0(pretrained=True) 21 | mobilenet = models.mobilenet_v2(pretrained=True) 22 | resnext50_32x4d = models.resnext50_32x4d(pretrained=True) 23 | wide_resnet50_2 = models.wide_resnet50_2(pretrained=True) 24 | mnasnet = models.mnasnet1_0(pretrained=True) -------------------------------------------------------------------------------- /6.1 模型构建(进阶).py: -------------------------------------------------------------------------------- 1 | import torch.nn as nn 2 | import torch 3 | 4 | class MyNet(nn.Module): 5 | def __init__(self): 6 | super(MyNet,self).__init__() 7 | self.conv1 = nn.Conv2d(3,64,3) 8 | self.conv2 = nn.Conv2d(64,64,3) 9 | 10 | def forward(self,x): 11 | x = self.conv1(x) 12 | x = self.conv2(x) 13 | return x 14 | net = MyNet() 15 | print(net) 16 | #-------------------------------------- 17 | print('------------------------------') 18 | class MyNet(nn.Module): 19 | def __init__(self): 20 | super(MyNet,self).__init__() 21 | self.add_module('conv1',nn.Conv2d(3,64,3)) 22 | self.add_module('conv2',nn.Conv2d(64,64,3)) 23 | 24 | def forward(self,x): 25 | x = self.conv1(x) 26 | x = self.conv2(x) 27 | return x 28 | net = MyNet() 29 | print(net) 30 | #-------------------------------------- 31 | print('------------------------------') 32 | class MyNet(nn.Module): 33 | def __init__(self): 34 | super(MyNet,self).__init__() 35 | self.linears = [nn.Linear(10,10) for i in range(5)] 36 | 37 | def forward(self,x): 38 | for l in self.linears: 39 | x = l(x) 40 | return x 41 | net = MyNet() 42 | print(net) 43 | #-------------------------------------- 44 | print('------------------------------') 45 | vgg_cfg = [64, 64, 'M', 128, 128, 'M', 256, 256, 256, 'C', 512, 512, 512, 'M', 46 | 512, 512, 512, 'M'] 47 | 48 | def vgg(cfg, i, batch_norm=False): 49 | layers = [] 50 | in_channels = i 51 | for v in cfg: 52 | if v == 'M': 53 | layers += [nn.MaxPool2d(kernel_size=2, stride=2)] 54 | elif v == 'C': 55 | layers += [nn.MaxPool2d(kernel_size=2, stride=2, ceil_mode=True)] 56 | else: 57 | conv2d = nn.Conv2d(in_channels, v, kernel_size=3, padding=1) 58 | if batch_norm: 59 | layers += [conv2d, nn.BatchNorm2d(v), nn.ReLU(inplace=True)] 60 | else: 61 | layers += [conv2d, nn.ReLU(inplace=True)] 62 | in_channels = v 63 | return layers 64 | 65 | class Model1(nn.Module): 66 | def __init__(self): 67 | super(Model1,self).__init__() 68 | 69 | self.vgg = nn.ModuleList(vgg(vgg_cfg,3)) 70 | 71 | def forward(self,x): 72 | 73 | for l in self.vgg: 74 | x = l(x) 75 | m1 = Model1() 76 | print(m1) 77 | 78 | #-------------------------------------- 79 | print('------------------------------') 80 | class MyNet(nn.Module): 81 | def __init__(self): 82 | super(MyNet,self).__init__() 83 | self.conv = nn.Sequential( 84 | nn.Conv2d(3,64,3), 85 | nn.Conv2d(64,64,3) 86 | ) 87 | 88 | def forward(self,x): 89 | x = self.conv(x) 90 | return x 91 | net = MyNet() 92 | print(net) 93 | 94 | #-------------------------------------- 95 | print('------------------------------') 96 | from collections import OrderedDict 97 | class MyNet(nn.Module): 98 | def __init__(self): 99 | super(MyNet,self).__init__() 100 | self.conv = nn.Sequential(OrderedDict([ 101 | ('conv1',nn.Conv2d(3,64,3)), 102 | ('conv2',nn.Conv2d(64,64,3)) 103 | ])) 104 | 105 | def forward(self,x): 106 | x = self.conv(x) 107 | return x 108 | net = MyNet() 109 | print(net) -------------------------------------------------------------------------------- /6.2 模型遍历与存储(进阶).py: -------------------------------------------------------------------------------- 1 | import torch.nn as nn 2 | import torch 3 | from collections import OrderedDict 4 | class MyNet(nn.Module): 5 | def __init__(self): 6 | super(MyNet,self).__init__() 7 | self.conv1 = nn.Conv2d(in_channels=3,out_channels=64,kernel_size=3) 8 | self.conv2 = nn.Conv2d(64,64,3) 9 | self.maxpool1 = nn.MaxPool2d(2,2) 10 | 11 | self.features = nn.Sequential(OrderedDict([ 12 | ('conv3', nn.Conv2d(64,128,3)), 13 | ('conv4', nn.Conv2d(128,128,3)), 14 | ('relu1', nn.ReLU()) 15 | ])) 16 | 17 | def forward(self,x): 18 | x = self.conv1(x) 19 | x = self.conv2(x) 20 | x = self.maxpool1(x) 21 | x = self.features(x) 22 | 23 | return x 24 | net = MyNet() 25 | print(net) 26 | 27 | #-------------------------------------- 28 | print('-------------------------------') 29 | for idx,m in enumerate(net.modules()): 30 | print(idx,"-",m) 31 | #-------------------------------------- 32 | print('-------------------------------') 33 | for idx,(name,m) in enumerate(net.named_modules()): 34 | print(idx,"-",name) 35 | 36 | #-------------------------------------- 37 | print('-------------------------------') 38 | for p in net.parameters(): 39 | print(type(p.data),p.size()) 40 | #-------------------------------------- 41 | print('-------------------------------') 42 | for idx,(name,m) in enumerate(net.named_parameters()): 43 | print(idx,"-",name,m.size()) 44 | #-------------------------------------- 45 | print('-------------------------------') 46 | for idx,(name,m) in enumerate(net.named_modules()): 47 | if isinstance(m,nn.Conv2d): 48 | print(m.weight.shape) 49 | print(m.bias.shape) 50 | #-------------------------------------- 51 | # print('-------------------------------') 52 | # torch.save(net,'model.pth') # 保存 53 | # net = torch.load("model.pth") # 加载 -------------------------------------------------------------------------------- /7 torchvision.transform常用API.py: -------------------------------------------------------------------------------- 1 | from PIL import Image 2 | from torchvision import transforms 3 | 4 | def loadImage(): 5 | # 读取图片 6 | im = Image.open("brunch.jpg") 7 | im = im.convert("RGB") 8 | # im.show() 9 | return im 10 | im = loadImage() 11 | 12 | #从中心裁剪一个600*600的图像 13 | output = transforms.CenterCrop(600)(im) 14 | # output.show() 15 | # 从中心裁一个长为600,宽为800的图像 16 | output = transforms.CenterCrop((600,800))(im) 17 | # output.show() 18 | #随机裁剪一个600*600的图像 19 | output = transforms.RandomCrop(600)(im) 20 | # output.show() 21 | #随机裁剪一个600*800的图像 22 | output = transforms.RandomCrop((600,800))(im) 23 | # output.show() 24 | #从上、下、左、右、中心各裁一个300*300的图像 25 | outputs = transforms.FiveCrop(600)(im) 26 | # outputs[4].show() 27 | #p默认为0.5,这里设成1,那么就肯定会水平翻转 28 | output = transforms.RandomHorizontalFlip(p=1.0)(im) 29 | # output.show() 30 | output = transforms.RandomVerticalFlip(p=1)(im) 31 | # output.show() 32 | #在(-30,30)之间选择一个角度进行旋转 33 | output = transforms.RandomRotation(30)(im) 34 | # output.show() 35 | #在60-90之间选择一个角度进行旋转 36 | output = transforms.RandomRotation((60,90))(im) 37 | # output.show() 38 | output = transforms.Resize((400,500))(im) 39 | output.show() 40 | 41 | trans = transforms.Compose([transforms.CenterCrop(600), 42 | transforms.RandomRotation(30), 43 | ]) 44 | output = trans(im) 45 | output.show() 46 | 47 | 48 | 49 | 50 | -------------------------------------------------------------------------------- /8.1 MNIST小试牛刀之EDA.py: -------------------------------------------------------------------------------- 1 | import pandas as pd 2 | # 读取训练集 3 | train_df = pd.read_csv('./MNIST_csv/train.csv') 4 | n_train = len(train_df) 5 | n_pixels = len(train_df.columns) - 1 6 | n_class = len(set(train_df['label'])) 7 | print('Number of training samples: {0}'.format(n_train)) 8 | print('Number of training pixels: {0}'.format(n_pixels)) 9 | print('Number of classes: {0}'.format(n_class)) 10 | 11 | # 读取测试集 12 | test_df = pd.read_csv('./MNIST_csv/test.csv') 13 | n_test = len(test_df) 14 | n_pixels = len(test_df.columns) 15 | print('Number of test samples: {0}'.format(n_test)) 16 | print('Number of test pixels: {0}'.format(n_pixels)) 17 | # 展示一些图片 18 | import numpy as np 19 | from torchvision.utils import make_grid 20 | import torch 21 | import matplotlib.pyplot as plt 22 | random_sel = np.random.randint(len(train_df), size=8) 23 | data = (train_df.iloc[random_sel,1:].values.reshape(-1,1,28,28)/255.) 24 | 25 | grid = make_grid(torch.Tensor(data), nrow=8) 26 | plt.rcParams['figure.figsize'] = (16, 2) 27 | plt.imshow(grid.numpy().transpose((1,2,0))) 28 | plt.axis('off') 29 | plt.show() 30 | print(*list(train_df.iloc[random_sel, 0].values), sep = ', ') 31 | 32 | # 检查类别是否不均衡 33 | plt.figure(figsize=(8,5)) 34 | plt.bar(train_df['label'].value_counts().index, train_df['label'].value_counts()) 35 | plt.xticks(np.arange(n_class)) 36 | plt.xlabel('Class', fontsize=16) 37 | plt.ylabel('Count', fontsize=16) 38 | plt.grid('on', axis='y') 39 | plt.show() -------------------------------------------------------------------------------- /8.2 MNIST小试牛刀之训练与推理.py: -------------------------------------------------------------------------------- 1 | import pandas as pd 2 | import torch 3 | from torch.utils.data import Dataset,DataLoader 4 | from torchvision import transforms 5 | import numpy as np 6 | import torch.nn as nn 7 | import torch.optim as optim 8 | import matplotlib.pyplot as plt 9 | 10 | train_df = pd.read_csv('./MNIST_csv/train.csv') 11 | test_df = pd.read_csv('./MNIST_csv/test.csv') 12 | n_train = len(train_df) 13 | n_test = len(test_df) 14 | n_pixels = len(train_df.columns) - 1 15 | n_class = len(set(train_df['label'])) 16 | 17 | 18 | 19 | class MNIST_data(Dataset): 20 | def __init__(self, file_path, 21 | transform=transforms.Compose([transforms.ToPILImage(), transforms.ToTensor(), 22 | transforms.Normalize(mean=(0.5,), std=(0.5,))]) 23 | ): 24 | df = pd.read_csv(file_path) 25 | if len(df.columns) == n_pixels: 26 | # test data 27 | self.X = df.values.reshape((-1, 28, 28)).astype(np.uint8)[:,:,:,None] 28 | self.y = None 29 | else: 30 | # training data 31 | self.X = df.iloc[:, 1:].values.reshape((-1, 28, 28)).astype(np.uint8)[:, :, :, None] 32 | self.y = torch.from_numpy(df.iloc[:, 0].values) 33 | self.transform = transform 34 | 35 | def __len__(self): 36 | return len(self.X) 37 | 38 | def __getitem__(self, idx): 39 | if self.y is not None: 40 | return self.transform(self.X[idx]), self.y[idx] 41 | else: 42 | return self.transform(self.X[idx]) 43 | 44 | 45 | batch_size = 64 46 | 47 | train_dataset = MNIST_data('./MNIST_csv/train.csv', 48 | transform= transforms.Compose([ 49 | transforms.ToPILImage(), 50 | transforms.Grayscale(num_output_channels=3), 51 | transforms.RandomRotation(degrees=20), 52 | transforms.ToTensor(), 53 | transforms.Normalize(mean=(0.5,), std=(0.5,))])) 54 | test_dataset = MNIST_data('./MNIST_csv/test.csv', 55 | transform=transforms.Compose([ 56 | transforms.ToPILImage(), 57 | transforms.Grayscale(num_output_channels=3), 58 | transforms.ToTensor(), 59 | transforms.Normalize(mean=(0.5,), std=(0.5,))])) 60 | 61 | train_loader = torch.utils.data.DataLoader(dataset=train_dataset, 62 | batch_size=batch_size, shuffle=True) 63 | test_loader = torch.utils.data.DataLoader(dataset=test_dataset, 64 | batch_size=batch_size, shuffle=False) 65 | 66 | 67 | class Net(nn.Module): 68 | def __init__(self): 69 | super(Net, self).__init__() 70 | 71 | self.features1 = nn.Conv2d(3, 32, kernel_size=3, stride=1, padding=1) 72 | self.features = nn.Sequential( 73 | nn.BatchNorm2d(32), 74 | nn.ReLU(inplace=True), 75 | nn.Conv2d(32, 32, kernel_size=3, stride=1, padding=1), 76 | nn.BatchNorm2d(32), 77 | nn.ReLU(inplace=True), 78 | nn.Conv2d(32, 32, kernel_size=3, stride=1, padding=1), 79 | nn.BatchNorm2d(32), 80 | nn.ReLU(inplace=True), 81 | nn.MaxPool2d(kernel_size=2, stride=2), 82 | nn.Conv2d(32, 64, kernel_size=3, padding=1), 83 | nn.BatchNorm2d(64), 84 | nn.ReLU(inplace=True), 85 | nn.Conv2d(64, 64, kernel_size=3, padding=1), 86 | nn.BatchNorm2d(64), 87 | nn.ReLU(inplace=True), 88 | nn.MaxPool2d(kernel_size=2, stride=2) 89 | ) 90 | 91 | self.classifier = nn.Sequential( 92 | nn.Dropout(p=0.5), 93 | nn.Linear(64 * 7 * 7, 512), 94 | nn.BatchNorm1d(512), 95 | nn.ReLU(inplace=True), 96 | nn.Dropout(p=0.5), 97 | nn.Linear(512, 512), 98 | nn.BatchNorm1d(512), 99 | nn.ReLU(inplace=True), 100 | nn.Dropout(p=0.5), 101 | nn.Linear(512, 10), 102 | ) 103 | 104 | for m in self.modules(): 105 | if isinstance(m, nn.Conv2d) or isinstance(m, nn.Linear): 106 | nn.init.xavier_uniform_(m.weight) 107 | elif isinstance(m, nn.BatchNorm2d): 108 | m.weight.data.fill_(1) 109 | m.bias.data.zero_() 110 | 111 | def forward(self, x): 112 | x = self.features1(x) 113 | x = self.features(x) 114 | x = x.view(x.size(0), -1) 115 | x = self.classifier(x) 116 | return x 117 | 118 | 119 | 120 | device = 'cuda' if torch.cuda.is_available() else 'cpu' 121 | model = Net().to(device) 122 | # model = torchvision.models.resnet50(pretrained=True).to(device) 123 | optimizer = optim.Adam(model.parameters(), lr=0.003) 124 | criterion = nn.CrossEntropyLoss().to(device) 125 | exp_lr_scheduler = optim.lr_scheduler.StepLR(optimizer, step_size=7, gamma=0.1) 126 | print(model) 127 | 128 | 129 | def train(epoch): 130 | model.train() 131 | 132 | for batch_idx, (data, target) in enumerate(train_loader): 133 | # 读入数据 134 | data = data.to(device) 135 | target = target.to(device) 136 | # 计算模型预测结果和损失 137 | output = model(data) 138 | loss = criterion(output, target) 139 | 140 | optimizer.zero_grad() # 计算图梯度清零 141 | loss.backward() # 损失反向传播 142 | optimizer.step()# 然后更新参数 143 | if (batch_idx + 1) % 50 == 0: 144 | print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format( 145 | epoch, (batch_idx + 1) * len(data), len(train_loader.dataset), 146 | 100. * (batch_idx + 1) / len(train_loader), loss.item())) 147 | log.append(loss.item()) 148 | exp_lr_scheduler.step() 149 | 150 | log = [] 151 | n_epochs = 10 152 | for epoch in range(n_epochs): 153 | train(epoch) 154 | 155 | plt.plot(log) 156 | plt.show() 157 | # 保存(载入)模型 158 | torch.save(model,'./MNIST_csv/model.pth') 159 | model = torch.load('./MNIST_csv/model.pth') 160 | 161 | def prediciton(data_loader): 162 | model.eval() 163 | test_pred = torch.LongTensor() 164 | 165 | for i, data in enumerate(data_loader): 166 | data = data.to(device) 167 | output = model(data) 168 | pred = output.cpu().data.max(1, keepdim=True)[1] 169 | test_pred = torch.cat((test_pred, pred), dim=0) 170 | return test_pred 171 | 172 | test_pred = prediciton(test_loader) 173 | 174 | from torchvision.utils import make_grid 175 | random_sel = np.random.randint(len(test_df), size=8) 176 | data = (test_df.iloc[random_sel,:].values.reshape(-1,1,28,28)/255.) 177 | 178 | grid = make_grid(torch.Tensor(data), nrow=8) 179 | plt.rcParams['figure.figsize'] = (16, 2) 180 | plt.imshow(grid.numpy().transpose((1,2,0))) 181 | plt.axis('off') 182 | plt.show() 183 | print(*list(test_pred[random_sel].numpy()), sep = ', ') -------------------------------------------------------------------------------- /9.1 PyTorch的数据结构.py: -------------------------------------------------------------------------------- 1 | import torch 2 | import numpy as np 3 | #---------------------- 4 | print('torch的浮点数与整数的默认数据类型') 5 | a = torch.tensor([1,2,3]) 6 | b = torch.tensor([1.,2.,3.]) 7 | print(a,a.dtype) 8 | print(b,b.dtype) 9 | 10 | #---------------------- 11 | print('torch的浮点数与整数的默认数据类型') 12 | a = torch.tensor([1,2,3],dtype=torch.int8) 13 | b = torch.tensor([1.,2.,3.],dtype = torch.float64) 14 | print(a,a.dtype) 15 | print(b,b.dtype) 16 | 17 | #---------------------- 18 | print('torch的浮点数与numpy的浮点数的默认数据类型') 19 | a = torch.tensor([1.,2.,3.]) 20 | b = np.array([1.,2.,3.]) 21 | print(a,a.dtype) 22 | print(b,b.dtype) 23 | 24 | #----------------------- 25 | print('torch的构造函数') 26 | a = torch.IntTensor([1,2,3]) 27 | b = torch.LongTensor([1,2,3]) 28 | c = torch.FloatTensor([1,2,3]) 29 | d = torch.DoubleTensor([1,2,3]) 30 | e = torch.tensor([1,2,3]) 31 | f = torch.tensor([1.,2.,3.]) 32 | print(a.dtype) 33 | print(b.dtype) 34 | print(c.dtype) 35 | print(d.dtype) 36 | print(e.dtype) 37 | print(f.dtype) 38 | 39 | #----------------------- 40 | print('数据类型转换1') 41 | a = torch.tensor([1,2,3]) 42 | b = a.float() 43 | c = a.double() 44 | d = a.long() 45 | print(b.dtype) 46 | print(c.dtype) 47 | print(d.dtype) 48 | print('数据类型转换2') 49 | b = a.type(torch.float32) 50 | c = a.type(torch.float64) 51 | d = a.type(torch.int64) 52 | print(b.dtype) 53 | print(c.dtype) 54 | print(d.dtype) 55 | 56 | 57 | 58 | 59 | -------------------------------------------------------------------------------- /9.2 torch vs numpy.py: -------------------------------------------------------------------------------- 1 | import torch 2 | import numpy as np 3 | #--------------------------- 4 | print('numpy 和torch互相转换') 5 | a = np.array([1.,2.,3.]) 6 | b = torch.tensor(a) 7 | c = b.numpy() 8 | print(a) 9 | print(b) 10 | print(c) 11 | #--------------------------- 12 | print('numpy 和torch互相转换1') 13 | a = np.array([1,2,3],dtype=np.float64) 14 | b = torch.Tensor(a) 15 | b[0] = 999 16 | print('共享内存' if a[0]==b[0] else '不共享内存') 17 | #--------------------------- 18 | print('numpy 和torch互相转换2') 19 | a = np.array([1,2,3],dtype=np.float32) 20 | b = torch.Tensor(a) 21 | b[0] = 999 22 | print('共享内存' if a[0]==b[0] else '不共享内存') 23 | 24 | #--------------------------- 25 | print('from_numpy()') 26 | a = np.array([1,2,3],dtype=np.float64) 27 | b = torch.from_numpy(a) 28 | print('共享内存' if a[0]==b[0] else '不共享内存') 29 | a = np.array([1,2,3],dtype=np.float32) 30 | b = torch.from_numpy(a) 31 | b[0] = 999 32 | print('共享内存' if a[0]==b[0] else '不共享内存') 33 | 34 | #--------------------------- 35 | print('numpy()与numpy().copy()') 36 | b = torch.tensor([1.,2.,3.]) 37 | a = b.numpy() 38 | a[0]=999 39 | print(a,b) 40 | print('共享内存' if a[0]==b[0] else '不共享内存') 41 | b = torch.tensor([1.,2.,3.]) 42 | a = b.numpy().copy() 43 | a[0]=999 44 | print(a,b) 45 | print('共享内存' if a[0]==b[0] else '不共享内存') 46 | 47 | #--------------------------- 48 | print('命名规则') 49 | a = torch.rand(2,3,4) 50 | b = np.random.rand(2,3,4) 51 | 52 | #--------------------------- 53 | print('张量分割') 54 | a = torch.rand(2,3,4) 55 | b = a.numpy() 56 | pytorch_masked = a[a > 0.5] 57 | numpy_masked = b[b > 0.5] 58 | print(pytorch_masked) 59 | print(numpy_masked) -------------------------------------------------------------------------------- /9.3 torch张量.py: -------------------------------------------------------------------------------- 1 | import torch 2 | print('tensor的存储区') 3 | a = torch.arange(0,6) 4 | print(a.storage()) 5 | b = a.view(2,3) 6 | print(b.storage()) 7 | print(id(a)==id(b)) 8 | print(id(a.storage)==id(b.storage)) 9 | # ---------------------------------- 10 | print('研究tensor的切片') 11 | a = torch.arange(0,6) 12 | b = a[2] 13 | print(id(a.storage)==id(b.storage)) 14 | # ---------------------------------- 15 | print('data_ptr()') 16 | print(a.data_ptr(),b.data_ptr()) 17 | print(b.data_ptr()-a.data_ptr()) 18 | # ---------------------------------- 19 | print('头信息区') 20 | a = torch.arange(0,6) 21 | b = a.view(2,3) 22 | print(a.stride(),b.stride()) 23 | print(a.storage_offset(),b.storage_offset()) 24 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # pytorch-learner 2 | 这是公众号【机器学习炼丹术】中pytorch系列教程《小白学pytorch》笔记中,配套的代码。 3 | 如果有什么问题可以联系微信:cyx645016617 我的研究方向为医疗图像和机器视觉。 4 | 5 | 顺便宣传下公众号哈哈,公众号主要是我看论文和学习其他AI相关内容的笔记,目前更新的系列有: 6 | - 《小白学PyTorch》 7 | - 《小白学图像(基础)》 8 | - 《小白学机器学习》 9 | - 《小白面经》 10 | - 《CVPR论文阅读及笔记》 11 | - 《轮廓检测相关论文》 12 | - 《图像质量评估相关论文》 13 | - 《医疗AI相关论文》 14 | - 《健身笔记》 15 | 16 | 觉得公众号内容不错的可以关注一下,可以**点个星星、转发**支持一下原创人的努力~谢谢。 17 | --------------------------------------------------------------------------------