Kaggle樹葉分類Leaves Classify總結

專注的阿熊發表於2021-07-29

if __name__ == '__main__':

# 啟動 visdom

     visdom = Visdom()

     # 建立一個 (0, 0) 作為起點, window id 作為 'train_loss' ,標題為 train loss 的視窗

     visdom.line(Y=[0], X=[0], win='train_loss', opts=dict(title='train loss'))

     # 建立一個 (0, 0) 作為起點, window id 作為 'accuracy' ,標題為 accuracy 的視窗

     visdom.line(Y=[0], X=[0], win='accuracy',  opts=dict(title='accuracy', legend=['train accuracy', 'valid accuracy']))

######################################################################

# 不使用遷移學習,

     isPreTrained = False

     net = resnest50(pretrain=isPreTrained)

     if isPreTrained:

         net.load_state_dict(torch.load('ModelPath'))

         # 凍結所有層的梯度計算

         for name, parameter in net.named_parameters():

         parameter.requires_grad = False

         # 重定義全連線層後 requires_grad 自動為 True

         net.fc = nn.Linear(net.fc.in_features, nClass)

######################################################################

     trainTransform = transforms.Compose([

         # 隨機拉伸並裁切 224x224 大小的圖片

         transforms.RandomResizedCrop(size=224, scale=[0.64, 1.0], ratio=[1.0, 1.0]),

         # 隨機水平翻轉

         transforms.RandomHorizontalFlip(p=0.5),

         # 隨機垂直翻轉

         transforms.RandomVerticalFlip(p=0.5),

         # # 隨機銳化

         # transforms.RandomAdjustSharpness(sharpness_factor=10),

         # # 隨機曝光

         # transforms.RandomSolarize(threshold=0.3),

         # # 隨機更改亮度,對比度和飽和度

         # transforms.ColorJitter(brightness=0.4, contrast=0.4, saturation=0.4),

         # 隨機應用下列 transform

         transforms.RandomApply([

            transforms.RandomRotation([-45,45], fill=[255, 255, 255]),

             # 仿射變換

            transforms.RandomAffine(degrees=[-30,30], translate=[0, 0.2], scale=[0.8, 1], fill=[255, 255, 255]),

         ]),   

         transforms.ToTensor(),

         # 標準化影像的每個通道

         transforms.Normalize([0.485, 0.456, 0.406],[0.229, 0.224, 0.225]),

         transforms.RandomErasing(),

     ])

     validTestTransform = transforms.Compose([

         transforms.Resize(256),

         # 從影像中心裁切 224x224 大小的圖片

         transforms.CenterCrop(224),

         transforms.ToTensor(),

         transforms.Normalize([0.485, 0.456, 0.406],[0.229, 0.224, 0.225])

     ])

######################################################################

     # 建立自己的訓練集,驗證集和測試集

     trainData = CLeavesData(filePath + 'train.csv', filePath, mode='train', transform=trainTransform)

     validData = CLeavesData(filePath + 'train.csv', filePath, mode='valid', transform=validTestTransform)

     testData = CLeavesData(filePath + 'test.csv', filePath, mode='test', transform=validTestTransform)

     # 將訓練集進行 CutMix 處理

     trainAData = CutMix(dataset=trainData, num_class=176, beta=1.0, prob=0.5, num_mix=2)

     trainDataLoader = DataLoader(

             dataset=trainAData,

             batch_size=batchSize,

             shuffle=False, # 外匯跟單gendan5.com 是否隨機打亂順序

             num_workers=3, # CPU 核心分配數

             # 固定 CPU 核心分配則在切換讀取訓練集和驗證集二者之間多執行緒不用重新分配核心,節省訓練時間

             persistent_workers=True,  # 固定處理資料集的 CPU 核心

         )

     validDataLoader = DataLoader(

             dataset=validData,

             batch_size=batchSize,

             shuffle=False,

             num_workers=3,

             persistent_workers=True,

         )

     testDataLoader =DataLoader(

             dataset=testData,

             batch_size=batchSize,

             shuffle=False,

             num_workers=6,

         )

######################################################################

     learningRate = 1e-4

     numEpochs = 100

     weightDecay = 1e-3

     # 開始訓練

     train(net, trainDataLoader, validDataLoader, numEpochs, learningRate, weightDecay, d2l.try_gpu())


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69946337/viewspace-2784154/,如需轉載,請註明出處,否則將追究法律責任。

相關文章