def predict(model_path='models/mnist.model'): _, loader = dataloader() data, label = loader.dataset[randint(0, len(loader.dataset) - 1)] data = data.view(-1, 28 * 28) model = MnistModel() model.load_state_dict(load(model_path)) output = model(data) prediction = output.argmax(dim=1, keepdim=True) print(f'prediction: {prediction.item()}, label: {label}')
def train(save_path='models/mnist.model'): # parameters epochs = 5 learning_rate = 0.001 # settings model = MnistModel() optimizer = Adam(params=model.parameters(), lr=learning_rate) loss_fn = CrossEntropyLoss() # train and evaluate history = {'train_loss': [], 'test_loss': [], 'test_acc': []} trainloader, testloader = dataloader() for epoch in range(epochs): model.train() # train mode running_loss = 0 for i, (data, labels) in enumerate(trainloader): data = data.view(-1, 28 * 28) # 入力を1次元に変換 optimizer.zero_grad() output = model(data) loss = loss_fn(output, labels) loss.backward() optimizer.step() # 重みの更新 # print statistics (after every 100 minibatches) running_loss += loss.item() if i % 100 == 99: print('[{:d}, {:5d}] loss: {:.3f}'.format( epoch + 1, i + 1, running_loss / 100)) running_loss = 0 history['train_loss'].append(loss) # evaluate model.eval() # eval mode test_loss, correct_cnt = 0, 0 with no_grad(): # 自動微分をoffにしてメモリ節約 for data, labels in testloader: data = data.view(-1, 28 * 28) # 入力を1次元に変換 output = model(data) prediction = output.argmax(dim=1, keepdim=True) correct_cnt += prediction.eq( labels.view_as(prediction)).sum().item() test_loss += loss_fn(output, labels).item() n_samples = len(testloader.dataset) test_loss, test_acc = test_loss / n_samples, correct_cnt / n_samples print(f'Test loss (avg): {test_loss}, Accuracy: {test_acc}') history['test_loss'].append(test_loss) history['test_acc'].append(test_acc) # save model dir = save_path[:save_path.rindex('/')] if len(dir) > 0 and not os.path.exists(dir): os.makedirs(dir) save(model.state_dict(), save_path)
def model_train(fold: int) -> None: # Prepare Data df = pd.read_csv(os.path.join(config.save_dir, 'split_kfold.csv')) df_train = df[df['kfold'] != fold].reset_index(drop=True) df_val = df[df['kfold'] == fold].reset_index(drop=True) df_train.drop(['kfold'], axis=1).to_csv(os.path.join( config.save_dir, f'train-kfold-{fold}.csv'), index=False) df_val.drop(['kfold'], axis=1).to_csv(os.path.join( config.save_dir, f'val-kfold-{fold}.csv'), index=False) train_dataset = MnistDataset(os.path.join(config.data_dir, 'train'), os.path.join( config.save_dir, f'train-kfold-{fold}.csv'), transforms_train) val_dataset = MnistDataset( os.path.join(config.data_dir, 'train'), os.path.join(config.save_dir, f'val-kfold-{fold}.csv'), transforms_test) model = MnistModel(EfficientNet()) checkpoint_callback = ModelCheckpoint( monitor='val_loss', dirpath=os.path.join(save_dir, f'{fold}'), filename='{epoch:02d}-{val_loss:.2f}.pth', save_top_k=5, mode='min', ) early_stopping = EarlyStopping( monitor='val_loss', mode='min', ) if config.device == 'tpu': train_loader = DataLoader(train_dataset, batch_size=16, num_workers=10, shuffle=True) val_loader = DataLoader(val_dataset, batch_size=2, num_workers=10, shuffle=False) trainer = Trainer( tpu_cores=8, num_sanity_val_steps=-1, deterministic=True, max_epochs=config.epochs, callbacks=[checkpoint_callback, early_stopping] ) else: train_loader = DataLoader(train_dataset, batch_size=16, num_workers=10, shuffle=True) val_loader = DataLoader(val_dataset, batch_size=8, num_workers=10, shuffle=False) trainer = Trainer( gpus=1, num_sanity_val_steps=-1, deterministic=True, max_epochs=config.epochs, callbacks=[checkpoint_callback, early_stopping] ) trainer.fit(model, train_loader, val_loader)
def main(): parser = argparse.ArgumentParser() parser.add_argument('--load_model', default="trained_model.pt", help="Load path of Trained model in local") parser.add_argument('--device', default="cuda", help='Save path of Trained model in local') parser.add_argument('--file', default=os.path.dirname(os.path.abspath(__file__)), help='Load path of image dataset in local') args = parser.parse_args() device = torch.device("cuda" if ( args.device == "cuda" and torch.cuda.is_available()) else "cpu") load_model = args.load_model print("Device: {}:".format(device)) print("Load Model: {}:".format(load_model)) print() model = MnistModel() if os.path.exists(load_model): model.load_state_dict(torch.load(load_model, device)) else: print("Warning: Load Model '{}' is not exist".format(load_model)) exit(1) model.set_device(device) img = Image.open(args.file).convert("L") data = torch.stack([transforms.ToTensor()(img)]) result = model.predict(data) result = result.argmax(dim=1, keepdim=True).reshape(-1).cpu().numpy()[0] print("Result: {}".format(result)) plt.title(result) plt.imshow(img) plt.show()
K.random_ops.random_seed.set_random_seed(argv.seed) train_dataset, test_dataset = data_generator(argv.permute, argv.batch_size) filters = { MnistModel.CORE_GRU: [argv.gru_units], MnistModel.CORE_LSTM: [argv.lstm_units], MnistModel.CORE_TCN: [argv.nhid] * argv.levels, MnistModel.CORE_TCN_HE: [argv.nhid] * argv.levels, } histories = {} for core in [MnistModel.CORE_GRU, MnistModel.CORE_LSTM, MnistModel.CORE_TCN, MnistModel.CORE_TCN_HE]: model = MnistModel( core=core, filters=filters[core], kernel_size=argv.ksize, dropout=argv.dropout, ) model.compile( run_eagerly=False, optimizer=keras.optimizers.Adam( lr=argv.lr, clipnorm=argv.clip ), loss='sparse_categorical_crossentropy', metrics=['accuracy'] ) model.summary() histories[core] = model.fit( train_dataset, epochs=argv.epochs,
summary_writer = None if not cfg.no_log: log_name = cfg.exp_name + "_log_" + \ strftime("%Y-%m-%d_%H-%M-%S", gmtime()) + '.txt' sys.stdout = Logger(os.path.join(log_path, log_name)) summary_writer = SummaryWriter(log_dir=log_path) print("Input Args: ") pprint.pprint(cfg) train_loader, test_loader, _, _ = get_data_loader( data_dir=cfg.data_dir, batch_size=cfg.batch_size, test_batch_size=cfg.eval_batch_size, num_workers=4) model = Model() optimizer = torch.optim.Adam(model.parameters(), lr=cfg.lr, weight_decay=0.0005, amsgrad=False) is_cuda = False if torch.cuda.device_count() > 1: model = nn.DataParallel(model) if torch.cuda.is_available(): model = model.cuda() is_cuda = True attack = LinfPGDAttack(model=model, epsilon=cfg.epsilon, k=cfg.k, alpha=cfg.alpha,
import torch.optim as optim from torchvision import datasets, transforms from torch.autograd import Variable from model import BayesianLinear as BLinear, MnistModel from tqdm import tqdm DEVICE = 'cuda' classes = [ 'T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat', 'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot' ] # classes = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'] batch_size = 200 model = MnistModel(BLinear) #model = nn.DataParallel(model) model.to(DEVICE) train_loader = torch.utils.data.DataLoader(datasets.FashionMNIST( 'fashionData', train=True, download=True, transform=transforms.ToTensor()), batch_size=batch_size, shuffle=True) test_loader = torch.utils.data.DataLoader(datasets.FashionMNIST( 'fashionData', train=False, transform=transforms.ToTensor()), batch_size=batch_size) optimizer = optim.Adam(model.parameters(), lr=0.001) scheduler = optim.lr_scheduler.StepLR(optimizer, step_size=5, gamma=0.8, last_epoch=-1)
# This is a sample Python script. # Press Shift+F10 to execute it or replace it with your code. # Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings. import matplotlib.pyplot as plt from tensorflow.keras.layers import Input import numpy as np from model import MnistModel, load_data # Press the green button in the gutter to run the script. if __name__ == '__main__': inputs = Input(shape=(28, 28)) mnist = MnistModel(inputs=inputs) # plot_model(mnist, to_file='model_plot.png', show_shapes=True, show_layer_names=True) (x_train, y_train), (x_test, y_test) = load_data() x_pred = x_train[:15] y_pred = y_train[:15] mnist.my_compile() mnist.my_fit(x=x_train, y=y_train) mnist.summary() acc = mnist.evaluate(x=x_test, y=y_test) print(acc) y_hat = mnist.predict(x_pred) print(np.argmax(y_hat))
from model import MnistModel import torch model = MnistModel() model.train(3) model.test() #model.saveModel()
import numpy as np import os from torchinfo import summary from torch.utils.data import DataLoader from model import MnistModel from dataset import MnistDataset from preprocess import transforms_train, transforms_test from tta import TTA # Set Device device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') # Get Model model0 = MnistModel().to(device) model0.load_state_dict( torch.load('save/dk/b5_fold-0_best.pth', map_location=device)) model1 = MnistModel().to(device) model1.load_state_dict( torch.load('save/dk/b5_fold-1_best.pth', map_location=device)) model2 = MnistModel().to(device) model2.load_state_dict( torch.load('save/dk/b5_fold-2_best.pth', map_location=device)) model3 = MnistModel().to(device) model3.load_state_dict( torch.load('save/dk/b5_fold-3_best.pth', map_location=device))
import torch.optim as optim from torchvision import datasets, transforms from torch.autograd import Variable from model import BayesianLinear as BLinear, MnistModel from sklearn.metrics import confusion_matrix import numpy as np from tqdm import tqdm DEVICE = 'cpu' classes = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat', 'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot'] # classes = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'] batch_size = 200 model = MnistModel(BLinear) #model = nn.DataParallel(model) model.to(DEVICE) train_loader = torch.utils.data.DataLoader( datasets.FashionMNIST('fashionData', train=True, download=True, transform=transforms.ToTensor()), batch_size=batch_size, shuffle=True) test_loader = torch.utils.data.DataLoader( datasets.FashionMNIST('fashionData', train=False, transform=transforms.ToTensor()), batch_size=batch_size) model.load_state_dict(torch.load('model.pt')) model.eval() accuracy = 0 for data, target in test_loader: with torch.no_grad():
def main(): parser = argparse.ArgumentParser() parser.add_argument('--load_model', default="trained_model.pt", help="Load path of Trained model in local") parser.add_argument('--save_model', default="trained_model.pt", help='Save path of Trained model in local') parser.add_argument('--batch', default=100, help='Save path of Trained model in local') parser.add_argument('--epoch', default=10, help='Save path of Trained model in local') parser.add_argument('--lr', default=0.01, help='Save path of Trained model in local') parser.add_argument('--device', default="cuda", help='Save path of Trained model in local') parser.add_argument('--dataset', default=os.path.dirname(os.path.abspath(__file__)), help='Load path of image dataset in local') args = parser.parse_args() batch_size = args.batch epoch = args.epoch lr = args.lr device = torch.device("cuda" if ( args.device == "cuda" and torch.cuda.is_available()) else "cpu") load_model = args.load_model save_model = args.save_model print("Batch_size: {}:".format(batch_size)) print("Epoch: {}:".format(epoch)) print("Learning rate: {}:".format(lr)) print("Device: {}:".format(device)) print("Load Model: {}:".format(load_model)) print("Save Model: {}:".format(save_model)) print() #train_dataset, test_dataset = dataset.get_mnist_dataset(args.dataset) train_dataset = dataset.load_dataset(args.dataset + "/MNIST/train") test_dataset = dataset.load_dataset(args.dataset + "/MNIST/test") train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=batch_size, shuffle=True) test_loader = torch.utils.data.DataLoader(test_dataset, batch_size=batch_size, shuffle=True) model = MnistModel() if os.path.exists(load_model): model.load_state_dict(torch.load(load_model, device)) else: print("Warning: Load Model '{}' is not exist".format(load_model)) optimizer = optim.Adam(model.parameters(), lr=lr) model.set_test_loader(test_loader) model.set_train_loader(train_loader) model.set_optimizer(optimizer) model.set_device(device) model.run(epoch) torch.save(model.state_dict(), save_model)
def get_model(self, model_path, env_params): tf = env_params model = MnistModel(tf, model_path) return model
import torch import pandas as pd import numpy as np from torchinfo import summary from torch.utils.data import DataLoader from model import MnistModel from dataset import MnistDataset from preprocess import transforms_train, transforms_test from tta import TTA # Set Device device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') # Get Model model = MnistModel() model.load_state_dict( torch.load('save/dk/b5_fold-0_best.pth', map_location=device)) # print(summary(model, input_size=(1, 3, 256, 256), verbose=0)) model.to(device) model.eval() # Prepare Data submit = pd.read_csv('data/sample_submission.csv') testset = MnistDataset('data/test', 'data/sample_submission.csv', transforms_test) test_loader = DataLoader(testset, batch_size=8, num_workers=4) # Test time augmentation conf = '{"augs":["NO",\ "ROT90",\