Pytorch (3)

1. Classic network architecture image classification model

Data preprocessing part:

  • data augmentation
  • data preprocessing
  • The DataLoader module directly reads batch data

Network module settings:

  • Load the pre-trained model, there are many classic network architectures in torchvision, which can be called directly
  • Note that the tasks trained by others are not exactly the same as ours. You need to change the last head layer, which is generally the last fully connected layer, and change it to your own tasks.
  • During the continuation, you can train all from the beginning, or you can only train the last layer of our task, because the first few layers are all for feature extraction, and the essential task goals are the same

Network model saving and testing:

  • The model can be saved selectively, for example, if the current effect is good in the verification set, save it
  • Read the model for actual testing

2. Transfer Learning

Use the model trained by others to train your own model

Note: Both objects are as similar as possible

Transfer Learning Website: Start Locally | PyTorch

3. Flower image classification case

unfinished

#数据读取与预处理操作
data_dir = './a/'
# 训练集
train_dir = data_dir + '/train'
#验证集
valid_ir = data_dir + '/valid'

#制作数据源
data_transfroms = {
    'train':transforms.Compose([transforms.RandomRotation(45), #随机旋转(-45~45)
    transforms.CenterCrop(224), #从中心开始裁剪
    transforms.RandomHorizontalFlip(p = 0.5), #随机水平翻转
    transforms.RandomVerticalFlip(p = 0.5), #随机垂直翻转
    transforms.ColorJitter(brightness=0.2,contrast=0.1,saturation=0.1,hue = 0.1),
    transforms.RandomGrayscale(p = 0.025), #概率转换成灰度率,3通道就是R=G=B
    transforms.ToTensor(),
    transforms.Normalize([0.485,0.456,0.406],[0.229,0.224,0.225])
    ]),
    'valid':transforms.Compose([transforms.Resize(256),
        transforms.CenterCrop(224),
        transforms.ToTensor(),
        transforms.Normalize([0.485,0.456,0.406],[0.229,0.224,0.225])
    ]),
}

#batch数据制作
batch_size = 8
image_datasets = {x: datasets.ImageFolder(os.path.join(data_dir,x),data_transfroms[x]) for x in ['train','valid']}
dataloaders = {x: torch.utils.data.DataLoader(image_datasets[x],batch_size = batch_size,shuffle = True) for x in ['train','valid']}
dataset_sizes = {x: len(image_datasets[x]) for x in ['train','valid']}
class_names = image_datasets['train'].classes


#读取标签对应的实际名字
with open('cat_to_name.json','r') as f:
    cat_to_name = json.load(f)

#加载model中提供的模型,并且直接用训练好的权重当做初始化参数
model_name = 'resnet'
#是否用人家训练好的特征来做
feature_extract = True

#是否用GPU来训练
train_on_gpu = torch.cuda.is_available()

if not train_on_gpu:
    print('cuda is not available. Training on CPU')
else:
    print('cuda is available. Training on GPU')

device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")

def set_parameter_requires_grad(model,feature_extracting):
    if feature_extracting:
        for param in model.parameter():
            param.requires_grad = False

model_ft = models.resnet152()

Guess you like

Origin blog.csdn.net/weixin_64443786/article/details/132007292