download=True, transform=transform_test) testloader = torch.utils.data.DataLoader(testset, batch_size=100, shuffle=False, num_workers=2) classes = ('plane', 'car', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck') for seed in range(3): net = VGG('VGG16').cuda() criterion = nn.CrossEntropyLoss() optimizer = optim.SGD(net.parameters(), lr=1e-2) def train(epoch): print('\nEpoch: %d' % epoch) net.train() train_loss = 0 correct = 0 total = 0 for batch_idx, (inputs, targets) in enumerate(trainloader): inputs, targets = inputs.cuda(), targets.cuda().long() optimizer.zero_grad() outputs = net(inputs) loss = criterion(outputs, targets) loss.backward() optimizer.step()
SHOW_PIC_NUM = args.show_picture_num DATASET_PATH = args.dataset_path RGB = args.rgb CSV = args.csv SHOW_VALID_PIC = args.show_valid DATASET_PATH, TRAIN_PATH, VALID_PATH = set_path(DEVICE) # print(DATASET_PATH) if torch.cuda.is_available(): net = VGG(NET).cuda() else: net = VGG(NET) params = net.parameters() if ONE_HOT: loss_func = nn.CrossEntropyLoss() optimizer = optim.SGD(net.parameters(), lr=LR) else: loss_func = nn.BCELoss() optimizer = optim.RMSprop(net.parameters(), lr=LR, alpha=0.9) def train(epoch=10, batch_size=10, dataset_path=None, one_hot=False): if one_hot: loss_func = nn.CrossEntropyLoss() optimizer = optim.SGD(net.parameters(), lr=LR) else:
# optimizer = optim.SGD(model.parameters(), lr=the_lr, momentum=0.9) # # model.load_state_dict(torch.load('{}.pth.tar'.format(icon))) # trained_model, val_acc_history, loss, acc = train_model(model, train_loader, test_loader, optimizer, 5) # print('{}_loss_{:2f}__acc_{:2f}.pth.tar'.format(icon, loss, acc)) # torch.save(trained_model.state_dict(), '{}.pth.tar'.format(icon)) # # ######################################################################################################### in_size = 64 icon = 'gun_butt' dataRoot = os.path.join("dataSets", icon, "train") out_num = len(os.listdir(dataRoot)) model = VGG(in_size, out_num) train_loader, test_loader = load_dataset(dataRoot, in_size) optimizer = optim.SGD(model.parameters(), lr=the_lr, momentum=0.9) model.load_state_dict(torch.load('{}.pth.tar'.format(icon))) trained_model, val_acc_history, loss, acc = train_model(model, train_loader, test_loader, optimizer, 10) print('{}_loss_{:2f}__acc_{:2f}.pth.tar'.format(icon, loss, acc)) torch.save(trained_model.state_dict(), '{}.pth.tar'.format(icon)) # # # ######################################################################################################## # in_size = 64 # icon = 'gun_name' # # dataRoot = os.path.join("dataSets", icon, "train") # out_num = len(os.listdir(dataRoot)) # # model = VGG(in_size, out_num) # train_loader, test_loader = load_dataset(dataRoot, in_size)