def main(): parser = argparse.ArgumentParser(description='pytorch example: MNIST') parser.add_argument('--batch', '-b', type=int, default=100, help='Number of images in each mini-batch') parser.add_argument('--model', '-m', default='model.pth', help='Network Model') args = parser.parse_args() batch_size = args.batch print('show training log') df = pd.read_csv('train.log') plt.plot(df['epoch'], df['train/accuracy'], label='train/acc.', marker="o") plt.plot(df['epoch'], df['test/accuracy'], label='test/acc.', marker="o") plt.legend(loc='lower right') plt.ylim([0.8, 1.0]) plt.savefig('accuracy.png') plt.show() transform = transforms.Compose([ transforms.ToTensor(), # transform to torch.Tensor transforms.Normalize(mean=(0.5, ), std=(0.5, )) ]) trainset = torchvision.datasets.CIFAR10(root='../cifar10_root', train=True, download=True, transform=transform) testset = torchvision.datasets.CIFAR10(root='../cifar10_root', train=False, download=True, transform=transform) dataset = trainset + testset dataloader = torch.utils.data.DataLoader(dataset, batch_size=batch_size, shuffle=False, num_workers=2) device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") print('device:', device) # Load & Predict Test param = torch.load('model.pth') net = CNN() #読み込む前にクラス宣言が必要 net.to(device) # for GPU net.load_state_dict(param) true_list = [] pred_list = [] with torch.no_grad(): for data in dataloader: images, labels = data true_list.extend(labels.tolist()) images, labels = images.to(device), labels.to(device) # for GPU outputs = net(images) _, predicted = torch.max(outputs.data, 1) pred_list.extend(predicted.tolist()) acc = accuracy_score(true_list, pred_list) print('Predict... all data acc.: {:.3f}'.format(acc)) confmat = confusion_matrix(y_true=true_list, y_pred=pred_list) fig, ax = plt.subplots(figsize=(6, 6)) ax.matshow(confmat, cmap=plt.cm.Purples, alpha=0.8) for i in range(confmat.shape[0]): for j in range(confmat.shape[1]): if confmat[i, j] > 0: ax.text(x=j, y=i, s=confmat[i, j], va='center', ha='center') plt.xlabel('predicted label') plt.ylabel('true label') plt.tight_layout() plt.savefig('confusion_matrix.png') plt.show()
def main(): start_time = time() args = get_args() if args.checkpoint_dir_name: dir_name = args.checkpoint_dir_name else: dir_name = datetime.datetime.now().strftime('%y%m%d%H%M%S') path_to_dir = Path(__file__).resolve().parents[1] path_to_dir = os.path.join(path_to_dir, *['log', dir_name]) os.makedirs(path_to_dir, exist_ok=True) # tensorboard path_to_tensorboard = os.path.join(path_to_dir, 'tensorboard') os.makedirs(path_to_tensorboard, exist_ok=True) writer = SummaryWriter(path_to_tensorboard) # model saving os.makedirs(os.path.join(path_to_dir, 'model'), exist_ok=True) path_to_model = os.path.join(path_to_dir, *['model', 'model.tar']) # csv os.makedirs(os.path.join(path_to_dir, 'csv'), exist_ok=True) path_to_results_csv = os.path.join(path_to_dir, *['csv', 'results.csv']) path_to_args_csv = os.path.join(path_to_dir, *['csv', 'args.csv']) if not args.checkpoint_dir_name: with open(path_to_args_csv, 'a') as f: args_dict = vars(args) param_writer = csv.DictWriter(f, list(args_dict.keys())) param_writer.writeheader() param_writer.writerow(args_dict) # logging using hyperdash if not args.no_hyperdash: from hyperdash import Experiment exp = Experiment('Classification task on CIFAR10 dataset with CNN') for key in vars(args).keys(): exec("args.%s = exp.param('%s', args.%s)" % (key, key, key)) else: exp = None path_to_dataset = os.path.join( Path(__file__).resolve().parents[2], 'datasets') os.makedirs(path_to_dataset, exist_ok=True) train_loader, eval_loader, classes = get_loader( batch_size=args.batch_size, num_workers=args.num_workers, path_to_dataset=path_to_dataset) # show some of the training images, for fun. dataiter = iter(train_loader) images, labels = dataiter.next() img_grid = torchvision.utils.make_grid(images) matplotlib_imshow(img_grid) writer.add_image('four_CIFAR10_images', img_grid) # define a network, loss function and optimizer model = CNN() writer.add_graph(model, images) model = torch.nn.DataParallel(model) criterion = nn.CrossEntropyLoss() optimizer = optim.SGD(model.parameters(), lr=args.lr, momentum=args.momentum) start_epoch = 0 # resume training if args.checkpoint_dir_name: print('\nLoading the model...') checkpoint = torch.load(path_to_model) model.state_dict(checkpoint['model_state_dict']) optimizer.load_state_dict(checkpoint['optimizer_state_dict']) start_epoch = checkpoint['epoch'] + 1 summary(model, input_size=(3, 32, 32)) model.to(args.device) # train the network print('\n--------------------') print('Start training and evaluating the CNN') for epoch in range(start_epoch, args.n_epoch): start_time_per_epoch = time() train_loss, train_acc = train(train_loader, model, criterion, optimizer, args.device, writer, epoch, classes) eval_loss, eval_acc = eval(eval_loader, model, criterion, args.device) elapsed_time_per_epoch = time() - start_time_per_epoch result_dict = { 'epoch': epoch, 'train_loss': train_loss, 'eval_loss': eval_loss, 'train_acc': train_acc, 'eval_acc': eval_acc, 'elapsed time': elapsed_time_per_epoch } with open(path_to_results_csv, 'a') as f: result_writer = csv.DictWriter(f, list(result_dict.keys())) if epoch == 0: result_writer.writeheader() result_writer.writerow(result_dict) # checkpoint torch.save( { 'epoch': epoch, 'model_state_dict': model.state_dict(), 'optimizer_state_dict': optimizer.state_dict() }, path_to_model) if exp: exp.metric('train loss', train_loss) exp.metric('eval loss', eval_loss) exp.metric('train acc', train_acc) exp.metric('eval acc', eval_acc) else: print(result_dict) writer.add_scalar('loss/train_loss', train_loss, epoch * len(train_loader)) writer.add_scalar('loss/eval_loss', eval_loss, epoch * len(eval_loader)) writer.add_scalar('acc/train_acc', train_acc, epoch * len(train_loader)) writer.add_scalar('acc/eval_acc', eval_acc, epoch * len(eval_loader)) elapsed_time = time() - start_time print('\nFinished Training, elapsed time ===> %f' % elapsed_time) if exp: exp.end() writer.close()
import torch from net import MLP, CNN # from torchvision import datasets, transforms from sklearn.metrics import multilabel_confusion_matrix # test_loader = torch.utils.data.DataLoader(datasets.FashionMNIST( './fashionmnist_data/', train=False, transform=transforms.Compose( [transforms.ToTensor(), transforms.Normalize((0.1307, ), (0.3081, ))])), batch_size=1, shuffle=True) model = CNN() device = torch.device('cpu') model = model.to(device) model.load_state_dict(torch.load('output/CNN.pt')) model.eval() pres = [] labels = [] i = 0 for data, target in test_loader: data, target = data.to(device), target.to(device) output = model(data) pred = output.argmax( dim=1, keepdim=True) # get the index of the max log-probability pres.append(pred[0][0].item()) labels.append(target[0].item()) mcm = multilabel_confusion_matrix(labels, pres) #mcm print(mcm)
def main(): parser = argparse.ArgumentParser(description='pytorch example: MNIST') parser.add_argument('--batch', '-b', type=int, default=100, help='Number of images in each mini-batch') parser.add_argument('--epoch', '-e', type=int, default=20, help='Number of sweeps over the dataset to train') parser.add_argument('--display', '-d', type=int, default=100, help='Number of interval to show progress') args = parser.parse_args() batch_size = args.batch epoch_size = args.epoch display_interval = args.display transform = transforms.Compose( [transforms.ToTensor(), # transform to torch.Tensor transforms.Normalize(mean=(0.5,), std=(0.5,))]) trainset = torchvision.datasets.MNIST(root='./data', train=True, download=True, transform=transform) # trainset... # <class 'object'> # <class 'torch.utils.data.dataset.Dataset'> # <class 'torchvision.datasets.mnist.MNIST'> # trainset[0][0]... # <class 'object'> # <class 'torch._C._TensorBase'> # <class 'torch.Tensor'> torch.Size([1, 28, 28]) trainloader = torch.utils.data.DataLoader(trainset, batch_size=batch_size, shuffle=True, num_workers=2) testset = torchvision.datasets.MNIST(root='./data', train=False, download=True, transform=transform) testloader = torch.utils.data.DataLoader(testset, batch_size=batch_size, shuffle=False, num_workers=2) device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") print('device:', device) net = CNN() print(net) print() net.to(device) # for GPU criterion = nn.CrossEntropyLoss() optimizer = optim.SGD(net.parameters(), lr=0.001, momentum=0.9) epoch_list = [] train_acc_list = [] test_acc_list = [] for epoch in range(epoch_size): # loop over the dataset multiple times running_loss = 0.0 train_true = [] train_pred = [] for i, data in enumerate(trainloader, 0): # get the inputs inputs, labels = data # inputs... # <class 'object'> # <class 'torch._C._TensorBase'> # <class 'torch.Tensor'> torch.Size([100, 1, 28, 28]) train_true.extend(labels.tolist()) inputs, labels = inputs.to(device), labels.to(device) # for GPU # zero the parameter gradients optimizer.zero_grad() # forward + backward + optimize outputs = net(inputs) loss = criterion(outputs, labels) loss.backward() optimizer.step() _, predicted = torch.max(outputs.data, 1) train_pred.extend(predicted.tolist()) # print statistics running_loss += loss.item() if i % display_interval == display_interval - 1: # print every 100 mini-batches print('[epochs: {}, mini-batches: {}, images: {}] loss: {:.3f}'.format( epoch + 1, i + 1, (i + 1) * batch_size, running_loss / display_interval)) running_loss = 0.0 test_true = [] test_pred = [] with torch.no_grad(): for data in testloader: images, labels = data test_true.extend(labels.tolist()) images, labels = images.to(device), labels.to(device) # for GPU outputs = net(images) _, predicted = torch.max(outputs.data, 1) test_pred.extend(predicted.tolist()) train_acc = accuracy_score(train_true, train_pred) test_acc = accuracy_score(test_true, test_pred) print(' epocs: {}, train acc.: {:.3f}, test acc.: {:.3f}'.format(epoch + 1, train_acc, test_acc)) print() epoch_list.append(epoch + 1) train_acc_list.append(train_acc) test_acc_list.append(test_acc) print('Finished Training') print('Save Network') torch.save(net.state_dict(), 'model.pth') df = pd.DataFrame({'epoch': epoch_list, 'train/accuracy': train_acc_list, 'test/accuracy': test_acc_list}) print('Save Training Log') df.to_csv('train.log', index=False)