pytorch学习心得

Pytorch学习笔记(原创)


操作系统:ubuntu18.04
显卡:GTX1080ti
python版本:2.7(3.7)
QQ群:加入深度学习交流群 获取更多环境配置细节和学习资料(147960154)


一、 pytorch是什么
pytorch是一种帮助搭建神经网络的模块。它提供了很多库文件来帮助我们搭建神经网络,支持GPU加速计算,提供多进程迭代器,动态过程显示。

二、 pytorch有哪些常用库

tensor(tensor is like ndarray with autogard),
torchvision(toolkit to operate image and transform)
torch.utils.data(support data diversion ,provide multiprocessing)
torch.nn  (set up networks ,support math function)
torch.optim 

三、 pytorch基本框架流程
1. 数据采集:train_data
Mdthon1:torchvision.datasets(一些自带的数据集)

train_data=torchvision.datasets.MNIST(root=./mnist’,train=False, download=False)

!train_data是一个关于MNIST的类,如果想调用里面的数值,必须附加类函数

Train_value = train_data.train_data  #shape: (8000,28,28)
Train_target=train_data.train_labels  #shape(8000,)

Mdthon2:Class FaceDataset(Dataset)(通过本地图像来创建一个数据集)
借助skimage库的io

class FaceLandmarksDataset(Dataset):
    """Face Landmarks dataset."""

    def __init__(self, csv_file, root_dir, transform=None):
        """
        Args:
            csv_file (string): Path to the csv file with annotations.
            root_dir (string): Directory with all the images.
            transform (callable, optional): Optional transform to be applied
                on a sample.
        """
        self.landmarks_frame = pd.read_csv(csv_file)
        self.root_dir = root_dir
        self.transform = transform

    def __len__(self):
        return len(self.landmarks_frame)

    def __getitem__(self, idx):
        img_name = os.path.join(self.root_dir,self.landmarks_frame.iloc[idx, 0])
        image = io.imread(img_name)
        landmarks = self.landmarks_frame.iloc[idx, 1:].as_matrix()
        landmarks = landmarks.astype('float').reshape(-1, 2)
        sample = {'image': image, 'landmarks': landmarks}

        if self.transform:
            sample = self.transform(sample)

        return sample

face_dataset = FaceLandmarksDataset(csv_file='data/faces/face_landmarks.csv',root_dir='data/faces/')

2. 数据处理
超参数:
BATCH_SIZE=50
LR = 0.02
EPOCH =1
1、 对数据集进行分流:import torch.utils.data.Dataloader as loader
Eg. loader=Loader(dataset=train_data,batch_size,
shuffle=True,num_work=2)

2、 对图像进行处理:import torchvision.transforms as transforms
Eg. transforms.Compose([
transforms.RandomSizedCrop(244), #随机截取244方块
transforms.ToTensor(), #将数据类型转化为tensor
transforms.Normalize(std=(0.5,0.5,0.5),mean=(0.5,0.5,0.5))
#将图像数据进行(0,255)标准化(0,1)方便后续计算

  1. 网络结构 torch.nn #建议先看吴恩达的视频,理解为主
Class CNN(nn.Module):
		def __init__(self):
			super(CNN,self).__init__()
			self.conv1 = nn.Sequential( #input( 1, 28, 28)
							nn.Conv2d(in_channels =1,
										 out_channels=16,
							          kernel_size=5,
							          stride=1, 
#(28-5+2*p)/s+1=28 p=2
				          padding=’SAME’ #’valid’),
				nn.ReLU(),
				nn.MaxPool2d(2)	)
self.conv2 = nn.Sequential( 
				nn.Conv2d(16,32,5,1,2)
				nn.ReLU(),
				nn.MaxPool2d(2))
self.fc1 = nn.Linear(32*7*7,120)
self.fc2 =nn.Linear(120,10)
def forward(self,x):
			x=self.conv1(x)
 x=self.conv2(x)
 x=x.view(x.size(0),-1)
			x=F.relu(self.fc1(x))
output = self.fc2(x)
cnn =CNN()
  1. 优化算法
  2. 结果显示
    四、 如何用pytorch来搭建CNN神经网络
    五、 如何用GPU来加速计算

你可能感兴趣的:(deep,learning)