def main(): print("Initalizing Network") for run in RunBuilder.get_runs(params): comment = f'-{run}' device = torch.device(run.device) cnn = CNN() cnn = cnn.to(device) #cnn.load_state_dict(torch.load("E:\LearningStuff\DLcode\Pytorch\Mnist\CNN3MODEL9967.pkl")) #extending_loader=get_expanding_data_loader([8],length=2000,batch_size=run.batch_size) train_loader = get_train_data_loader(run.batch_size) test_loader = get_test_data_loader(run.batch_size) optimizer = optim.Adam(cnn.parameters(), lr=run.learning_rate) ''' Initializing tensorboard ''' tb = SummaryWriter(comment=comment, flush_secs=1) images, labels = next(iter(train_loader)) grid = torchvision.utils.make_grid(images) '''begin to train''' for epoch in range(num_epochs): total_loss = 0 total_correct = 0 adjust_learning_rate(optimizer, epoch, run.learning_rate) for batch in train_loader: images = batch[0].to(device) labels = batch[1].to(device) preds = cnn(images) loss = F.cross_entropy(preds, labels) optimizer.zero_grad() loss.backward() optimizer.step() total_loss += loss.item() total_correct += get_num_correct(preds, labels) tb.add_scalar('Loss', total_loss, epoch) tb.add_scalar('Number Correct', total_correct, epoch) tb.add_scalar('Accuracy', total_correct / 60000, epoch) path = SAVED_PATH + "\MODEL" + str(epoch) + ".pkl" torch.save(cnn.state_dict(), path) print("epoch", epoch, "loss", total_loss, "Accuracy", total_correct / 60000) cnn.to('cpu').eval() for epoch in range(num_epochs): cnn.load_state_dict( torch.load(SAVED_PATH + "\MODEL" + str(epoch) + ".pkl")) total_correct = 0 for batchs in test_loader: images, labels = batchs preds = cnn(images) total_correct += get_num_correct(preds, labels) print("The Accuracy of the ", epoch, "model on test-set is:", total_correct / 60000) tb.add_scalar('Accuracy on testset', total_correct / 10000, epoch) tb.close()
def train(): model = CNN(gConfig['rate']) model = model.creatModel() check_point = ModelCheckpoint(filepath='./model_dir/train.h5', save_best_only=True) model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) image = Image() model.fit_generator( image.gen(), steps_per_epoch=10000, #每次迭代需要多少个Batch epochs=5, #共需迭代几次 validation_data=image.gen(), validation_steps=10, callbacks=[check_point])
def SingleTest(): test_loader = get_test_data_loader(batch_size=256) cnn = CNN() ''' 防止权重改变 ''' cnn.eval() cnn.load_state_dict(torch.load('newMODEL9915.pkl')) print("load cnn net.") total_correct = 0 for batchs in test_loader: images, labels = batchs preds = cnn(images) total_correct += get_num_correct(preds, labels) print('The Accuracy of the model on test-set is:', total_correct / 10000)
def predict(image): if not torch.is_tensor(image): image = image.resize((28, 28)) '''将图像转为tensor''' loader = transforms.Compose( [transforms.ToTensor(), transforms.Normalize(0.1307, 0.3081)]) image = loader(image).unsqueeze(dim=0) ''' 预测 ''' network = CNN() network.eval() network.load_state_dict(torch.load(PATH + "\Model1.pkl")) pred = network(image).argmax(dim=1) return pred
def plot_confusion_matrix(): cnn = CNN() cnn.load_state_dict(torch.load(path + "\Model1.pkl")) loader = get_test_data_loader(10) ConfusionMatrix(cnn, loader)
def CombinationTest(): '''将九个预训练的模型级联进行预测,以达到99.7%的准确率''' cnn1 = CNN() cnn1.load_state_dict(torch.load(path + "\Model1.pkl")) cnn1.eval() cnn2 = CNN() cnn2.load_state_dict(torch.load(path + "\Model2.pkl")) cnn2.eval() cnn3 = CNN() cnn3.load_state_dict(torch.load(path + "\Model3.pkl")) cnn3.eval() cnn4 = CNN() cnn4.load_state_dict(torch.load(path + "\Model4.pkl")) cnn4.eval() cnn5 = CNN() cnn5.load_state_dict(torch.load(path + "\Model5.pkl")) cnn5.eval() cnn6 = CNN() cnn6.load_state_dict(torch.load(path + "\Model6.pkl")) cnn6.eval() cnn7 = CNN() cnn7.load_state_dict(torch.load(path + "\Model7.pkl")) cnn7.eval() cnn8 = CNN() cnn8.load_state_dict(torch.load(path + "\Model8.pkl")) cnn8.eval() cnn9 = CNN() cnn9.load_state_dict(torch.load(path + "\Model9.pkl")) cnn9.eval() total_correct = 0 test_loader = get_test_data_loader(256) for batchs in test_loader: images, labels = batchs preds1 = cnn1(images).argmax(dim=1).unsqueeze(dim=1) preds2 = cnn2(images).argmax(dim=1).unsqueeze(dim=1) preds3 = cnn3(images).argmax(dim=1).unsqueeze(dim=1) preds4 = cnn4(images).argmax(dim=1).unsqueeze(dim=1) preds5 = cnn5(images).argmax(dim=1).unsqueeze(dim=1) preds6 = cnn6(images).argmax(dim=1).unsqueeze(dim=1) preds7 = cnn7(images).argmax(dim=1).unsqueeze(dim=1) preds8 = cnn8(images).argmax(dim=1).unsqueeze(dim=1) preds9 = cnn9(images).argmax(dim=1).unsqueeze(dim=1) preds = torch.cat((preds1, preds2, preds3, preds4, preds5, preds6, preds7, preds8, preds9), dim=1) preds = preds.numpy() pred = [] for i in range(preds.shape[0]): temp = preds[i] pred.append(sorted(temp)[len(temp) // 2]) preds = torch.tensor(pred) total_correct += new_get_num_correct(preds, labels) print("The Accuracy of the combination model on test-set is:", total_correct / 10000)