1
0
Fork 0
PlantNetLibre-300K/main.py

200 lines
6.0 KiB
Python
Raw Normal View History

2023-07-05 09:56:35 -06:00
# SPDX-License-Identifier: BSD-2-Clause
#
# Copyright (c) 2021, Pl@ntNet
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2021-05-06 07:35:27 -06:00
import os
from tqdm import tqdm
import pickle
import argparse
import time
import torch
from torch.optim import SGD
from torch.nn import CrossEntropyLoss
from utils import set_seed, load_model, save, get_model, update_optimizer, get_data
from epoch import train_epoch, val_epoch, test_epoch
from cli import add_all_parsers
def train(args):
set_seed(args, use_gpu=torch.cuda.is_available())
2023-07-05 13:55:21 -06:00
train_loader, val_loader, test_loader, dataset_attributes = get_data(
args.root,
args.image_size,
args.crop_size,
args.batch_size,
args.num_workers,
args.pretrained,
)
model = get_model(args, n_classes=dataset_attributes["n_classes"])
2021-05-06 07:35:27 -06:00
criteria = CrossEntropyLoss()
if args.use_gpu:
2023-07-05 13:55:21 -06:00
print("USING GPU")
2021-05-06 07:35:27 -06:00
torch.cuda.set_device(0)
model.cuda()
criteria.cuda()
2023-07-05 13:55:21 -06:00
optimizer = SGD(
model.parameters(),
lr=args.lr,
momentum=0.9,
weight_decay=args.mu,
nesterov=True,
)
2021-05-06 07:35:27 -06:00
2021-06-30 06:55:52 -06:00
# Containers for storing metrics over epochs
loss_train, acc_train, topk_acc_train = [], [], []
loss_val, acc_val, topk_acc_val, avgk_acc_val, class_acc_val = [], [], [], [], []
2021-05-06 07:35:27 -06:00
save_name = args.save_name_xp.strip()
2023-07-05 13:55:21 -06:00
save_dir = os.path.join(os.getcwd(), "results", save_name)
2021-05-06 07:35:27 -06:00
if not os.path.exists(save_dir):
os.makedirs(save_dir)
2023-07-05 13:55:21 -06:00
print("args.k : ", args.k)
2021-06-10 06:39:22 -06:00
2021-05-06 07:35:27 -06:00
lmbda_best_acc = None
2023-07-05 13:55:21 -06:00
best_val_acc = float("-inf")
2021-05-06 07:35:27 -06:00
2023-07-05 13:55:21 -06:00
for epoch in tqdm(range(args.n_epochs), desc="epoch", position=0):
2021-05-06 07:35:27 -06:00
t = time.time()
2023-07-05 13:55:21 -06:00
optimizer = update_optimizer(
optimizer, lr_schedule=args.epoch_decay, epoch=epoch
)
loss_epoch_train, acc_epoch_train, topk_acc_epoch_train = train_epoch(
model,
optimizer,
train_loader,
criteria,
loss_train,
acc_train,
topk_acc_train,
args.k,
dataset_attributes["n_train"],
args.use_gpu,
)
(
loss_epoch_val,
acc_epoch_val,
topk_acc_epoch_val,
avgk_acc_epoch_val,
lmbda_val,
) = val_epoch(
model,
val_loader,
criteria,
loss_val,
acc_val,
topk_acc_val,
avgk_acc_val,
class_acc_val,
args.k,
dataset_attributes,
args.use_gpu,
)
2021-06-30 06:55:52 -06:00
# save model at every epoch
2023-07-05 13:55:21 -06:00
save(
model, optimizer, epoch, os.path.join(save_dir, save_name + "_weights.tar")
)
2021-05-06 07:35:27 -06:00
# save model with best val accuracy
2021-06-30 06:55:52 -06:00
if acc_epoch_val > best_val_acc:
best_val_acc = acc_epoch_val
2021-05-06 07:35:27 -06:00
lmbda_best_acc = lmbda_val
2023-07-05 13:55:21 -06:00
save(
model,
optimizer,
epoch,
os.path.join(save_dir, save_name + "_weights_best_acc.tar"),
)
2021-05-06 07:35:27 -06:00
print()
2023-07-05 13:55:21 -06:00
print(f"epoch {epoch} took {time.time()-t:.2f}")
print(f"loss_train : {loss_epoch_train}")
print(f"loss_val : {loss_epoch_val}")
print(
f"acc_train : {acc_epoch_train} / topk_acc_train : {topk_acc_epoch_train}"
)
print(
f"acc_val : {acc_epoch_val} / topk_acc_val : {topk_acc_epoch_val} / "
f"avgk_acc_val : {avgk_acc_epoch_val}"
)
2021-05-06 07:35:27 -06:00
# load weights corresponding to best val accuracy and evaluate on test
2023-07-05 13:55:21 -06:00
load_model(
model, os.path.join(save_dir, save_name + "_weights_best_acc.tar"), args.use_gpu
)
(
loss_test_ba,
acc_test_ba,
topk_acc_test_ba,
avgk_acc_test_ba,
class_acc_test,
) = test_epoch(
model,
test_loader,
criteria,
args.k,
lmbda_best_acc,
args.use_gpu,
dataset_attributes,
)
2021-05-06 07:35:27 -06:00
# Save the results as a dictionary and save it as a pickle file in desired location
2023-07-05 13:55:21 -06:00
results = {
"loss_train": loss_train,
"acc_train": acc_train,
"topk_acc_train": topk_acc_train,
"loss_val": loss_val,
"acc_val": acc_val,
"topk_acc_val": topk_acc_val,
"class_acc_val": class_acc_val,
"avgk_acc_val": avgk_acc_val,
"test_results": {
"loss": loss_test_ba,
"accuracy": acc_test_ba,
"topk_accuracy": topk_acc_test_ba,
"avgk_accuracy": avgk_acc_test_ba,
"class_acc_dict": class_acc_test,
},
"params": args.__dict__,
}
with open(os.path.join(save_dir, save_name + ".pkl"), "wb") as f:
2021-05-06 07:35:27 -06:00
pickle.dump(results, f)
2023-07-05 13:55:21 -06:00
if __name__ == "__main__":
2021-05-06 07:35:27 -06:00
parser = argparse.ArgumentParser()
add_all_parsers(parser)
args = parser.parse_args()
train(args)