import time
import torch
from torch import nn
from torch.utils.data import DataLoader
from utils import LoadData,WriteData

from torchvision.models import resnet18

def train(dataloader, model, loss_fn, optimizer,device):
    size = len(dataloader.dataset)
    avg_loss = 0
    # 从数据加载器中读取batch（一次读取多少张，即批次数），X(图片数据)，y（图片真实标签）。
    for batch, (X, y) in enumerate(dataloader):#固定格式：batch：第几批数据，不是批次大小，（X，y）：数值用括号
        # 将数据存到显卡
        X, y = X.to(device), y.to(device)
        # 得到预测的结果pred
        pred = model(X)
        loss = loss_fn(pred, y)
        avg_loss += loss
        # 反向传播，更新模型参数
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()
        # 每训练10次，输出一次当前信息
        if batch % 10 == 0:
            loss, current = loss.item(), batch * len(X)
            print(f"loss: {loss:>7f}  [{current:>5d}/{size:>5d}]")

    # 当一个epoch完了后返回平均 loss
    avg_loss /= size
    avg_loss = avg_loss.detach().cpu().numpy()
    return avg_loss


def validate(dataloader, model, loss_fn, device):
    size = len(dataloader.dataset)
    # 将模型转为验证模式
    model.eval()
    # 初始化test_loss 和 correct， 用来统计每次的误差
    test_loss, correct = 0, 0
    # 测试时模型参数不用更新，所以no_gard()
    # 非训练， 推理期用到
    with torch.no_grad():
        # 加载数据加载器，得到里面的X（图片数据）和y(真实标签）
        for X, y in dataloader:
            # 将数据转到GPU
            X, y = X.to(device), y.to(device)
            # 将图片传入到模型当中就，得到预测的值pred
            pred = model(X)
            # 计算预测值pred和真实值y的差距
            test_loss += loss_fn(pred, y).item()
            # 统计预测正确的个数(针对分类)
            correct += (pred.argmax(1) == y).type(torch.float).sum().item()
    test_loss /= size
    correct /= size
    print(f"correct = {correct}, Test Error: \n Accuracy: {(100 * correct):>0.1f}%, Avg loss: {test_loss:>8f} \n")
    return correct, test_loss


if __name__=='__main__':
    batch_size = 20#设置批次大小，每次训练时用于更新模型的样本数量

    # # 给训练集和测试集分别创建一个数据集加载器
    train_data = LoadData("train.txt", True)#true为训练集
    valid_data = LoadData("test.txt", False)#false为测试集

    #批量加载数据，num_workers并行工作线程数，
    train_dataloader = DataLoader(dataset=train_data, num_workers=4, pin_memory=True, batch_size=batch_size, shuffle=True)
    valid_dataloader = DataLoader(dataset=valid_data, num_workers=4, pin_memory=True, batch_size=batch_size)

    # 如果显卡可用，则用显卡进行训练
    device = "cuda" if torch.cuda.is_available() else "cpu"
    print(f"Using {device} device")

    #创建一个ResNet-18模型实例
    model = resnet18(num_classes=55)

    model = model.to(device)
    state_dict = torch.load(r"output/resnet18_no_pretrain_best.pth")
    # print("state_dict = ",state_dict)
    model.load_state_dict(state_dict)

    # 定义损失函数，计算相差多少，交叉熵，
    loss_fn = nn.CrossEntropyLoss()

    # 定义优化器，随机梯度下降法
    learning_rate = 1e-3
    optimizer = torch.optim.SGD(model.parameters(), lr=learning_rate)

    #设置训练总轮数，初始化变量loss用于跟踪最低的平均损失值
    epochs = 50
    loss_ = 10
    save_root = "output/"


    for t in range(epochs):
        print(f"Epoch {t + 1}\n-------------------------------")
        time_start = time.time()
        avg_loss = train(train_dataloader, model, loss_fn, optimizer, device)
        time_end = time.time()
        print(f"train time: {(time_end - time_start)}")
        # (dataloader, model, loss_fn, device)jif
        #调用 validate 函数进行验证集上的性能评估，计算验证集的准确率和损失
        val_accuracy, val_loss = validate(valid_dataloader, model,loss_fn, device)
        # 将训练和验证的结果写入文件写入数据
        WriteData(save_root + "resnet18_no_pretrain.txt",
                  "epoch", t,#epoch数
                  "train_loss", avg_loss,#训练损失
                  "val_loss", val_loss,#验证损失
                  "val_accuracy", val_accuracy)#验证准确率
        #如果当前epoch是5的倍数，保存模型参数文件，以便后续恢复或继续训练。
        if t % 5 == 0:
            torch.save(model.state_dict(), save_root + "resnet18_no_pretrain_epoch" + str(t) + "_loss_" + str(avg_loss) + ".pth")

        #每个epoch都保存模型参数文件，以便后续分析和恢复。
        torch.save(model.state_dict(), save_root + "resnet18_no_pretrain_last.pth")

        #如果当前epoch的平均损失小于 loss_，则更新 loss_ 为当前损失值，并保存当前模型参数作为最佳模型。
        if avg_loss < loss_:
            loss_ = avg_loss
            torch.save(model.state_dict(), save_root + "resnet18_no_pretrain_best.pth")

        torch.cuda.empty_cache()

