import torch import torch.nn as nn from common_tools import set_seed set_seed(1) # 设置随机种子 # %% Module.register_forward_hook and pre hook class Net(nn.Module): def __init__(self): super(Net, self).__init__() self.conv1 = nn.Conv2d(1, 2, 3) self.pool1 = nn.MaxPool2d(2, 2) def forward(self, x): x = self.conv1(x) x = self.pool1(x) return x def forward_hook(module, data_input, data_output): fmap_block.append(data_output) input_block.append(data_input) def forward_pre_hook(module, data_input): print("forward_pre_hook input:{}".format(data_input))
assert os.path.exists(path_lenet), "{}不存在,请将lenet.py文件放到 {}".format( path_lenet, os.path.dirname(path_lenet)) assert os.path.exists(path_tools), "{}不存在,请将common_tools.py文件放到 {}".format( path_tools, os.path.dirname(path_tools)) import sys hello_pytorch_DIR = os.path.abspath( os.path.dirname(__file__) + os.path.sep + ".." + os.path.sep + "..") sys.path.append(hello_pytorch_DIR) from lenet import LeNet from my_dataset import RMBDataset # from my_dataset import DogOrCatDataset from common_tools import set_seed set_seed() # 设置随机种子 rmb_label = {"1": 0, "100": 1} # 参数设置 MAX_EPOCH = 10 BATCH_SIZE = 16 LR = 0.01 log_interval = 10 val_interval = 1 # ============================ step 1/5 数据 ============================ split_dir = os.path.abspath(os.path.join(BASE_DIR, "data", "rmb_split")) if not os.path.exists(split_dir): raise Exception( r"数据 {} 不存在, 回到lesson-06\1_split_dataset.py生成数据".format(split_dir)) train_dir = os.path.join(split_dir, "train")
import torch from common_tools import set_seed set_seed(1) # %% tensor hook 1 def grad_hook(grad): a_grad.append(grad) w = torch.tensor([1.], requires_grad=True) x = torch.tensor([2.], requires_grad=True) a = torch.add(w, x) b = torch.add(w, 1) y = torch.mul(a, b) a_grad = list() handle = a.register_hook(grad_hook) y.backward() # 查看梯度 print("gradient:", w.grad, x.grad, a.grad, b.grad, y.grad) print("a_grad[0]: ", a_grad[0]) handle.remove() # %% tensor hook 2
# -*- coding: utf-8 -*- import os import torch.nn as nn from PIL import Image from torchvision import transforms from matplotlib import pyplot as plt from common_tools import transform_invert, set_seed set_seed(3) # 设置随机种子 # ================================= load img ================================== path_img = os.path.join(os.path.dirname(os.path.abspath(__file__)), "imgs", "lena.png") print(path_img) img = Image.open(path_img).convert('RGB') # 0~255 # convert to tensor img_transform = transforms.Compose([transforms.ToTensor()]) img_tensor = img_transform(img) # 添加 batch 维度 img_tensor.unsqueeze_(dim=0) # C*H*W to B*C*H*W # ================================= create convolution layer ================================== # ================ 2d # flag = 1 flag = 0 if flag: conv_layer = nn.Conv2d(3, 1, 3) # input:(i, o, size) weights:(o, i , h, w) # 初始化卷积层权值