def demo(): """ demo for MNIST handwritten digits recognition """ model = Net() train_dataset = MnistDataset(True) test_dataset = MnistDataset(False) train_dataloader = MNN.data.DataLoader(train_dataset, batch_size=64, shuffle=True) test_dataloader = MNN.data.DataLoader(test_dataset, batch_size=100, shuffle=False) opt = MNN.optim.SGD(model, 0.01, 0.9, 0.0005) F.set_thread_number(4) for epoch in range(0, 10): opt.learning_rate = learning_rate_scheduler(opt.learning_rate, epoch) train_func(model, train_dataloader, opt) # save model file_name = '%d.mnist.mnn' % epoch model.train(False) predict = model.forward(F.placeholder([1, 1, 28, 28], F.NC4HW4)) print("Save to " + file_name) F.save([predict], file_name) test_func(model, test_dataloader)
def main(): # Load the train data. train_csv = './mnist_data/train.csv' train_x, train_y, val_x, val_y = load_train_csv_dataset( train_csv, validation_percent=0.1) # Create pytorch dataloaders for train and validation sets. train_dataset = MnistDataset(train_x, train_y) train_dataloader = DataLoader(train_dataset, batch_size=200, shuffle=True, num_workers=2) val_dataset = MnistDataset(val_x, val_y) val_dataloader = DataLoader(val_dataset, batch_size=200, shuffle=False, num_workers=2) # Define model, optimizer and loss function. model = MnistCNN() optimizer = optim.Adam(model.parameters(), lr=LEARNING_RATE) loss_func = nn.CrossEntropyLoss() # Train our model. train_model(model, train_dataloader, loss_func, optimizer, epochs=NUM_EPOCHS) val_accuracy = eval_model(model, val_dataloader) print('Validation set accuracy: {}'.format(val_accuracy)) # Save model weights for inference. torch.save(model.state_dict(), 'trained_model.pt')
def dataset(self): if self.suffix == 'test': return MnistDataset(file='data/test.csv') return MnistDataset(file='data/train.csv', fold_csv='data/fold.csv', is_test=True )
def test_file_exists(self): # Above I tested that if the file doesn't exist it will download it # Here I test that if the file was already downloaded, it will not download it again # run it once to make sure the file is downloaded MnistDataset().get_dataset() urlretrieve = MagicMock() MnistDataset().get_dataset(urlretrieve) urlretrieve.method.assert_not_called()
def get_datasets(self, stage: str, **kwargs): datasets = OrderedDict() train = MnistDataset('data/train.csv', fold_csv='data/fold.csv') valid = MnistDataset('data/train.csv', fold_csv='data/fold.csv', is_test=True) datasets['train'] = train datasets['valid'] = valid return datasets
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 __init__(self, model: Interface): self.model = model self.dataset = MnistDataset(file='data/train.csv', fold_csv='data/fold.csv', is_test=True )
def get_datasets(self, stage: str, **kwargs): datasets = OrderedDict() train = MnistDataset('data/train.csv', fold_csv='data/fold.csv', transforms=Experiment.get_test_transforms()) valid = MnistDataset('data/train.csv', fold_csv='data/fold.csv', is_test=True, transforms=Experiment.get_test_transforms()) datasets['train'] = train datasets['valid'] = valid return datasets
def test_file_doesnt_exist(self): filename = 'mnist.npz' if os.path.exists(filename): os.remove(filename) (x_train, y_train), (x_test, y_test) = MnistDataset().get_dataset() assert x_train.shape == (60000, 28, 28) assert y_train.shape == (60000, ) assert x_test.shape == (10000, 28, 28) assert y_test.shape == (10000, )
def main(_): # Import data mnist = MnistDataset(FLAGS.data_dir) model = Model() sess = tf.InteractiveSession() tf.global_variables_initializer().run() model.train(sess, mnist.train) # Test trained model print(model.test(sess, mnist.test))
def main(_): start_time = time.time() # Import data mnist = MnistDataset(FLAGS.data_dir) model = Model() sess = dm.get_session() model.train(sess, mnist.train) # Test trained model result = model.test(sess, mnist.test) end_time = time.time() print("%0.2f percent accuracy in %d ms." % (result * 100, end_time - start_time))
def create_dataset(path, test=False): """ Parameters ---------- path: path to data directory test: specify whether the data is for training or testing Returns ------- Loaded dataset """ return MnistDataset(path, test=test)
def __init__(self, **kwargs): cache_names = ['y'] super().__init__(cache_names=cache_names, layout='img_classify', **kwargs) self.x_source = MnistDataset( file='data/train.csv', fold_csv='data/fold.csv', is_test=True, max_count=self.max_count, transforms=Experiment.get_test_transforms()) self.builder = None self.x = None self.scores = []
def main(args): analysis_dataset = MnistDataset(args.test_data_path, num_classes=args.class_nums, shape=(args.image_width, args.image_height)) analysis_dataloader = torch.utils.data.DataLoader( analysis_dataset, batch_size=args.batch_size, shuffle=False, num_workers=args.num_workers, drop_last=False) face_index, face_label = load_nn_model( os.path.join(args.model_dir, args.faiss_model_file), os.path.join(args.model_dir, args.faiss_label_file)) device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') model = torch.jit.load(os.path.join(args.model_dir, args.model_file), map_location=device) results = { "predicts": [], "labels": analysis_dataset.labels, "distances": [] } for i, data in enumerate(analysis_dataloader): images, _ = data with torch.no_grad(): embeddings = model(images.to(device)) dists, inds = face_index.search(embeddings.detach().cpu().numpy(), 3) predicts = face_label[inds[:, 0]] results["predicts"] += predicts.tolist() results["distances"] += dists.tolist() del images del embeddings print("[+] Analysis using {} image data".format(str( len(analysis_dataset)))) save_cm(results, args.class_nums) print("finnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn")
def __init__(self): # Data and training : self.batch_size = 64 self.dataset = MnistDataset(self.batch_size) self.n_epochs = 10 # Model hyperparameters : self.z_size = 25 # TODO : make this more modular, adapt to other datasets self.images = tf.placeholder(tf.float32, [None, 28, 28, 1]) # estimators of mean and sdt in latent space z_mu, z_sigma = self.encoder(self.images) eps = tf.random.normal([self.batch_size, self.z_size], 0, 1, dtype=tf.float32) self.z = z_mu + (z_sigma * eps) # generated images self.gen_images = self.decoder(self.z) tiny = 1e-7 # to avoid nan from logs # latent loss : KL divergence between our estimators and a normal distribution self.lat_loss = 0.5 * tf.reduce_sum( tf.square(z_mu) + tf.square(z_sigma) - tf.log(tf.square(z_sigma)) - 1, 1) # generative loss : distance between original image and reconstructed one flat = tf.reshape(self.images, shape=[self.batch_size, 28 * 28]) flat = tf.clip_by_value(flat, tiny, 1 - tiny) gen_flat = tf.reshape(self.gen_images, shape=[self.batch_size, 28 * 28]) gen_flat = tf.clip_by_value(gen_flat, tiny, 1 - tiny) # cross entropy between original and reconstructed image self.gen_loss = -tf.reduce_sum( flat * tf.log(gen_flat) + (1 - flat) * tf.log(1 - gen_flat), 1) # total loss is the sum of both losses : self.loss = tf.reduce_mean(self.gen_loss + self.lat_loss) self.opt = tf.train.AdamOptimizer(0.001).minimize(self.loss) # other stuff self.save_path = os.getcwd() + "/saves/model"
import tensorflow as tf from models.convolutional_model import ConvModel from dataset import MnistDataset epochs = 40 batch_size = 20 # Load Data from target directory train_set = MnistDataset('flowers/train') n_batches = len(train_set) // batch_size # hold_prob = tf.placeholder(tf.float32) # Construct CNN model # model = ConvModel(resolution=[64, 64], channels=3, hold_prob=hold_prob) model = ConvModel(resolution=[64, 64], channels=3) # We use this to save the model. Instantiate it after all Variables have been created saver = tf.train.Saver() # Define tensors label_placeholder = tf.placeholder(tf.float32, shape=[batch_size, 5]) # Define loss functions and Optimizer loss = tf.losses.softmax_cross_entropy(label_placeholder, model.predictions) update = tf.train.AdamOptimizer(learning_rate=0.0001).minimize(loss) top_predictions = tf.argmax(model.predictions, axis=1) # probabilities -> top prediction top_labels = tf.argmax(label_placeholder, axis=1) # one_hot -> number correct = tf.equal(top_predictions, top_labels) # bool Tensor accuracy = tf.reduce_mean(tf.cast(correct,
def main(): # Training settings parser = argparse.ArgumentParser(description='PyTorch MNIST Example') parser.add_argument('--batch-size', type=int, default=64, metavar='N', help='input batch size for training (default: 64)') parser.add_argument('--test-batch-size', type=int, default=1000, metavar='N', help='input batch size for testing (default: 1000)') parser.add_argument('--epochs', type=int, default=14, metavar='N', help='number of epochs to train (default: 14)') parser.add_argument('--lr', type=float, default=1.0, metavar='LR', help='learning rate (default: 1.0)') parser.add_argument('--gamma', type=float, default=0.7, metavar='M', help='Learning rate step gamma (default: 0.7)') parser.add_argument('--no-cuda', action='store_true', default=False, help='disables CUDA training') parser.add_argument('--seed', type=int, default=1, metavar='S', help='random seed (default: 1)') parser.add_argument('--log-interval', type=int, default=10, metavar='N', help='how many batches to wait before logging training status') parser.add_argument('--save-model', action='store_true', default=False, help='For Saving the current Model') args = parser.parse_args() use_cuda = not args.no_cuda and torch.cuda.is_available() torch.manual_seed(args.seed) device = torch.device("cuda" if use_cuda else "cpu") if True: kwargs = {'num_workers': 1, 'pin_memory': True} if use_cuda else {} train_loader = torch.utils.data.DataLoader( datasets.MNIST('../data', train=True, download=True, transform=transforms.Compose([ transforms.ToTensor(), transforms.Normalize((0.1307,), (0.3081,)) ])), batch_size=args.batch_size, shuffle=True, **kwargs) test_loader = torch.utils.data.DataLoader( datasets.MNIST('../data', train=False, transform=transforms.Compose([ transforms.ToTensor(), transforms.Normalize((0.1307,), (0.3081,)) ])), batch_size=args.test_batch_size, shuffle=True, **kwargs) else: kwargs = {'num_workers': 16, 'pin_memory': True} if use_cuda else {} train_loader = torch.utils.data.DataLoader( MnistDataset(train=True, transform=transforms.Compose([ transforms.ToTensor(), transforms.Normalize((0.1307,), (0.3081,)) ])), batch_size=args.batch_size, shuffle=True, **kwargs) test_loader = torch.utils.data.DataLoader( MnistDataset(train=False, transform=transforms.Compose([ transforms.ToTensor(), transforms.Normalize((0.1307,), (0.3081,)) ])), batch_size=args.batch_size, shuffle=True, **kwargs) model = Net().to(device) optimizer = optim.Adadelta(model.parameters(), lr=args.lr) scheduler = StepLR(optimizer, step_size=1, gamma=args.gamma) start = dt.datetime.now() for epoch in range(1, args.epochs + 1): train(args, model, device, train_loader, optimizer, epoch) test(model, device, test_loader) scheduler.step() if args.save_model: torch.save(model.state_dict(), "mnist_cnn.pt") end = dt.datetime.now() print(f"elapsed time: {(end - start).total_seconds()} secs")
if not os.path.isdir("./generate_image"): os.mkdir("./generate_image") checkpoint_dir = './training_checkpoints' print("load models!!") generator = Generator() discriminator = Discriminator() print("load ckpt!!") checkpoint = tf.train.Checkpoint(generator=generator, discriminator=discriminator) checkpoint.restore(tf.train.latest_checkpoint(checkpoint_dir)) dataset_name = 'fashion_mnist' assert dataset_name in ['mnist', 'fashion_mnist'] if dataset_name == 'mnist': mnist_dataset = MnistDataset() test_images, test_labels = mnist_dataset.get_test_data() if dataset_name == 'fashion_mnist': fashin_mnist_dataset = FashinMnistDataset() test_images, test_labels = fashin_mnist_dataset.get_test_data() random_noise_test_images, _ = fashin_mnist_dataset.get_random_noise_test_data() random_noise_test_images = random_noise_test_images.reshape(-1, 28, 28, 1).astype('float32') test_images = test_images.reshape(-1, 28, 28, 1).astype('float32') print("Compute anomaly scores!!") for idx, (test_image, test_label) in enumerate(zip(test_images, test_labels)): if dataset_name == 'fashion_mnist' and idx % 2 == 1: test_image = random_noise_test_images[idx] test_image = (test_image / 127.5) - 1
import tensorflow as tf import numpy as np from models.convolutional_model import ConvModel from dataset import MnistDataset epochs = 50 batch_size = 150 # Load Data from target directory train_set = MnistDataset('kaggle_inria/train') n_batches = len(train_set) // batch_size # hold_prob = tf.placeholder(tf.float32) with tf.device('/gpu:1'): # Construct CNN model # model = ConvModel(resolution=[64, 64], channels=3, hold_prob=hold_prob) model = ConvModel(resolution=[128, 128], channels=3) # We use this to save the model. Instantiate it after all Variables have been created saver = tf.train.Saver() # Define tensors label_placeholder = tf.placeholder(tf.float32, shape=[batch_size, 2]) # Define loss functions and Optimizer loss = tf.losses.softmax_cross_entropy(label_placeholder, model.predictions) update = tf.train.AdamOptimizer(learning_rate=0.0001).minimize(loss) top_predictions = tf.argmax(model.predictions,
def __init__(self, network: tf.Tensor): self.network = network self.mnist = MnistDataset() self.loss = None
def main(): use_cuda = False device = torch.device('cuda' if use_cuda else 'cpu') epochs = 50 torch.manual_seed(123) transforms = T.Compose([ T.ToTensor(), T.Normalize((0.5, ), (0.5, )), ]) trainSet = MnistDataset('data', 'train', transforms) testSet = MnistDataset('data', 'test', transforms) trainSet, validSet = torch.utils.data.random_split(trainSet, [24000, 6000]) traindataLoader = DataLoader(trainSet, batch_size=64, shuffle=True, num_workers=4) validDataLoader = DataLoader(validSet, batch_size=64, shuffle=True, num_workers=4) testDataLoader = DataLoader(testSet, batch_size=64, num_workers=4) #for grid search, in comment since it was used just for finding best hyperparameter # # #besthypepar = gridSearch(traindataLoader,validDataLoader,validSet,epochs,device) #print(besthypepar) besthypeloss = float('inf') #besthyperparameters: 2 hidden layer, 512 layer size, relu function, 0.001 learning rate lastModel = Model(512, 2, 2) lastModel.to(device) optimizer = torch.optim.Adam(lastModel.parameters(), lr=0.001) lastModel, train_loss, valid_loss, best_val_loss, best_correct = train( lastModel, optimizer, traindataLoader, validDataLoader, epochs, device, besthypeloss) #plot train and valid loss fig = plt.figure() plt.plot(train_loss, label='Training Loss') plt.plot(valid_loss, label='Validation Loss') plt.axvline(valid_loss.index(min(valid_loss)), color="r", label='Early Stop') plt.xlabel('epoch') plt.ylabel('loss') plt.legend() plt.tight_layout() fig.savefig('best_loss_plot.png') #test lastModel.load_state_dict(torch.load('model_state_dict')) lastModel.eval() index = 0 test_results = [] for test_images, test_labels in testDataLoader: pred = lastModel(test_images) _, output = torch.max(pred, 1) for i in range(output.size(0)): head, tail = os.path.split(testSet.data[index][0]) test_results.append(f'{tail} ' + f'{output[i].item()}\n') index = index + 1 with open("test_labels.txt", "w") as f: f.writelines(test_results)
import tensorflow as tf from dataset import MnistDataset with tf.Session() as sess: saver = tf.train.import_meta_graph( 'saved_models/model.ckpt.meta' ) # Don't have to recreate the entire graph saver.restore(sess, 'saved_models/model.ckpt') # Restore all graph variables model = tf.get_collection('model')[0] inputs = tf.get_collection('model_inputs')[0] test_inputs = [ 'data/test/img_1.jpg', 'data/test/img_2.jpg', 'data/test/img_3.jpg' ] test_inputs = [MnistDataset.read_image(input) for input in test_inputs] predictions = sess.run(model, feed_dict={inputs: test_inputs}) print(predictions)
def main(args): train_dataset = MnistDataset(args.train_data_path, num_classes=args.class_nums) train_dataloader = torch.utils.data.DataLoader( train_dataset, batch_size=args.batch_size, shuffle=True, num_workers=args.num_workers, drop_last=False) print("[+] " + str(len(train_dataset)) + " train dataset") eval_dataset = MnistDataset(args.test_data_path, num_classes=args.class_nums, transforms=False) eval_dataloader = torch.utils.data.DataLoader(eval_dataset, batch_size=args.batch_size, shuffle=False, num_workers=args.num_workers, drop_last=False) print("[+] " + str(len(eval_dataset)) + " evaluate dataset") model = Embedding(input_shape=(args.image_width, args.image_height), input_channel=args.image_channel, d_embedding=args.d_embedding) print(model) metric = ArcMarginProduct(args.d_embedding, args.class_nums, s=args.scale_size) print(metric) if args.resume: print("[+] Resume the model({}) and metric({})".format( args.model_resume, args.metric_resume)) model.load_state_dict( torch.load(args.model_resume)["model_state_dict"]) metric.load_state_dict( torch.load(args.metric_resume)["metric_state_dict"]) criterion = torch.nn.CrossEntropyLoss() if args.optimizer == "adam": print("[+] Using Adam Optimizer") optimizer = torch.optim.Adam([{ "params": model.parameters() }, { "params": metric.parameters() }], lr=args.lr, weight_decay=args.weight_decay) else: print("[+] Using SGD Optimizer") optimizer = torch.optim.SGD([{ "params": model.parameters() }, { "params": metric.parameters() }], lr=args.lr, weight_decay=args.weight_decay) # lr_sched = lr_scheduler.MultiStepLR(optimizer, gamma=0.1) if args.n_gpus > 1: net = DataParallel(net) metric = DataParallel(metric) device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') model.to(device) metric.to(device) criterion.to(device) it = 0 for epoch in range(1, args.epoch + 1): # logging.info('{} epoch started'.format(str(epoch).zfill(3))) print('[+] {} epoch started'.format(str(epoch).zfill(3))) for data in train_dataloader: images, labels = data[0].to(device), data[1].to(device) optimizer.zero_grad() embeddings = model(images) output = metric(embeddings, labels) total_loss = criterion(output, labels) total_loss.backward() optimizer.step() if it % args.log_iter == 0: _, predict = torch.max(output.data, 1) correct = (np.array(predict.cpu()) == np.array( labels.data.cpu())).sum() now_accuracy = correct / labels.size(0) # logging.info('{} iterations Accuracy :{}'.format(str(it).zfill(5), str(now_accuracy))) print('[+] {} iterations Accuracy :{}'.format( str(it).zfill(5), str(now_accuracy))) if it % args.save_iter == 0: if args.n_gpus > 1: model_state_dict = model.module.state_dict() metric_state_dict = metric.module.state_dict() else: model_state_dict = model.state_dict() metric_state_dict = metric.state_dict() if not os.path.exists(args.ckpt_dir): os.makedirs(args.ckpt_dir) torch.save( { "epoch": epoch, "iters": it, "model_state_dict": model_state_dict }, os.path.join( args.ckpt_dir, "iter_{}_".format(str(it).zfill(5)) + args.model_ckpt)) torch.save( { "epoch": epoch, "iters": it, "metric_state_dict": metric_state_dict }, os.path.join( args.ckpt_dir, "iter_{}_".format(str(it).zfill(5)) + args.metric_ckpt)) print('[+] {} iterations model saved'.format(str(it).zfill(5))) if it % args.eval_iter == 0: model.eval() metric.eval() correct = 0 with torch.no_grad(): for data in eval_dataloader: images, labels = data[0].to(device), data[1].to(device) embeddings = model(images) output = metric(embeddings, labels) _, predict = torch.max(output.data, 1) correct = correct + (np.array(predict.cpu()) == np.array( labels.data.cpu())).sum() acc = correct / len(eval_dataloader.dataset) # logging.info('{} iterations Eval Accuracy :{}'.format(str(it).zfill(5), str(acc))) print('[+] {} iterations Eval Accuracy :{}'.format( str(it).zfill(5), str(acc))) model.train() metric.train() it = it + 1 with torch.no_grad(): model.eval() metric.eval() if args.n_gpus > 1: model_ = torch.jit.script(model.module) metric_ = torch.jit.script(metric.module) else: model_ = torch.jit.script(model) metric_ = torch.jit.script(metric) if not os.path.exists(args.model_dir): os.makedirs(args.model_dir) torch.jit.save(model_, os.path.join(args.model_dir, args.model_file)) torch.jit.save(metric_, os.path.join(args.model_dir, args.metric_file)) print("[+] Saved final Models") del train_dataset del eval_dataset
print("fit") self.model.fit( x=self.train_dataset, steps_per_epoch=self.train_size//batch_size, validation_data=self.validation_dataset, validation_steps=self.validation_size//batch_size, epochs=epochs ) # callbacks=callback # ) if __name__ =="__main__": from dataset import MnistDataset from dataset import DatasetUtil from model import SimpleCNN from model import SimpleSoftmaxClassificationModel train = MnistDataset.get_train_set().map(DatasetUtil.image_classification_util(10)) validation = MnistDataset.get_validation_set().map(DatasetUtil.image_classification_util(10)) train_size, validation_size, _ = MnistDataset.get_data_size() base = SimpleCNN.get_base_model(28,28,1) softmax_model = SimpleSoftmaxClassificationModel.get_classification_model(base,10) trainer:Trainer = Trainer( softmax_model, train=train, train_size=train_size, validation=validation, validation_size=validation_size ) trainer.train()
from dataset import MnistDataset from keras_gan import GAN mnist = MnistDataset(64) gan = GAN(mnist)
x = F.relu(x) x = self.fc2(x) if self.act == 0: x = torch.sigmoid(x) elif self.act == 1: x = torch.tanh(x) elif self.act == 2: x = F.relu(x) x = self.fc3(x) x = torch.log_softmax(x, dim=1) return x if __name__ == '__main__': transforms = T.Compose([ T.ToTensor(), T.Normalize((0.5, ), (0.5, )), ]) dataset = MnistDataset('data', 'train', transforms) dataLoader = DataLoader(dataset, batch_size=64, shuffle=True, num_workers=4) model = Model(512, 2, 2) for images, labels in dataLoader: pred = model(images) print(pred) exit()
from __future__ import print_function from tensorflow.python import keras from tensorflow.python.keras.layers import (Conv2D, Dense, Dropout, Flatten, MaxPooling2D) from tensorflow.python.keras.models import Sequential from dataset import MnistDataset # Create the dataset dataset = MnistDataset(augmentation='pad') # Params batch_size = 128 epochs = 12 # Define the model model = Sequential() model.add( Conv2D(32, kernel_size=(3, 3), activation='relu', input_shape=dataset.shape)) model.add(Conv2D(64, (3, 3), activation='relu')) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Dropout(0.25)) model.add(Flatten()) model.add(Dense(128, activation='relu')) model.add(Dropout(0.5)) model.add(Dense(dataset.num_classes, activation='softmax'))
model3.load_state_dict( torch.load('save/dk/b5_fold-3_best.pth', map_location=device)) model4 = MnistModel().to(device) model4.load_state_dict( torch.load('save/dk/b5_fold-4_best.pth', map_location=device)) model0.eval() model1.eval() model2.eval() model3.eval() model4.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",\ "ROT180",\ "ROT270"],\ "mean":"ARITH"}' model0 = TTA(model0, device, conf) model1 = TTA(model1, device, conf) model2 = TTA(model2, device, conf) model3 = TTA(model3, device, conf) model4 = TTA(model4, device, conf)
def work(self): dataset = MnistDataset(file='data/test.csv') torch_interface = Torch('models/net.pth', 128, name='net') prob = torch_interface({'dataset': dataset})['prob'] np.save(f'data/net_test', prob)