def test(model_name='model.pkl'): cnn = CNN() cnn.eval() cnn.load_state_dict(torch.load(model_name)) print('load cnn net.') test_dataloader = dataset.get_test_data_loader() correct = 0 total = 0 for i, (images, labels) in enumerate(test_dataloader): image = images vimage = Variable(image) predict_label = cnn(vimage) chars = '' for i in range(setting.MAX_CAPTCHA): chars += setting.ALL_CHAR_SET[np.argmax( predict_label[0, i * setting.ALL_CHAR_SET_LEN:(i + 1) * setting.ALL_CHAR_SET_LEN].data.numpy())] predict_label = chars true_label = one_hot.decode(labels.numpy()[0]) total += labels.size(0) if (predict_label == true_label): correct += 1 else: print('Predict:' + predict_label) print('Real :' + true_label) if (total % 200 == 0): print('Test Accuracy of the model on the %d test images: %f %%' % (total, 100 * correct / total)) print('Test Accuracy of the model on the %d test images: %f %%' % (total, 100 * correct / total))
def main(): cnn = CNN() cnn.eval() cnn.load_state_dict(torch.load('model/1500_model.pkl')) print("load cnn net.") predict_dataloader = my_dataset.get_predict_data_loader() for i, (images, labels) in enumerate(predict_dataloader): image = images vimage = Variable(image) predict_label = cnn(vimage) c0 = captcha_setting.ALL_CHAR_SET[np.argmax( predict_label[0, 0:captcha_setting.ALL_CHAR_SET_LEN].data.numpy())] c1 = captcha_setting.ALL_CHAR_SET[np.argmax( predict_label[0, captcha_setting.ALL_CHAR_SET_LEN:2 * captcha_setting.ALL_CHAR_SET_LEN].data.numpy())] c2 = captcha_setting.ALL_CHAR_SET[np.argmax( predict_label[0, 2 * captcha_setting.ALL_CHAR_SET_LEN:3 * captcha_setting.ALL_CHAR_SET_LEN].data.numpy())] c3 = captcha_setting.ALL_CHAR_SET[np.argmax( predict_label[0, 3 * captcha_setting.ALL_CHAR_SET_LEN:4 * captcha_setting.ALL_CHAR_SET_LEN].data.numpy())] c = '%s%s%s%s' % (c0, c1, c2, c3) return c
def main(): img = Image.open(IMAGEPATH) transed_img = trans(img) n_channels = 3 n_classes = 5 test_model = CNN(n_channels, n_classes) model_dict = test_model.load_state_dict(torch.load(MODELPATH)) transed_img = transed_img.view(1, 3, DROPSIZE, DROPSIZE) outputs = test_model(transed_img) _, predicted = torch.max(outputs.data, 1) # loss: criterion = nn.CrossEntropyLoss() loss = criterion(outputs, torch.from_numpy(np.array([2]))) # if loss >= 1: # print("Unknown") # return if predicted == 0: print("Dorm") if predicted == 1: print("Gym") if predicted == 2: print("Library") if predicted == 3: print("Market") if predicted == 4: print("Teaching Building 1")
def main(): cnn = CNN() cnn.eval() cnn.load_state_dict(torch.load('model/1500_model.pkl')) print("load cnn net.") test_dataloader = my_dataset.get_test_data_loader() correct = 0 total = 0 error = [] true = [] for i, (images, labels) in enumerate(test_dataloader): image = images vimage = Variable(image) predict_label = cnn(vimage) c0 = captcha_setting.ALL_CHAR_SET[np.argmax( predict_label[0, 0:captcha_setting.ALL_CHAR_SET_LEN].data.numpy())] c1 = captcha_setting.ALL_CHAR_SET[np.argmax( predict_label[0, captcha_setting.ALL_CHAR_SET_LEN:2 * captcha_setting.ALL_CHAR_SET_LEN].data.numpy())] c2 = captcha_setting.ALL_CHAR_SET[np.argmax( predict_label[0, 2 * captcha_setting.ALL_CHAR_SET_LEN:3 * captcha_setting.ALL_CHAR_SET_LEN].data.numpy())] c3 = captcha_setting.ALL_CHAR_SET[np.argmax( predict_label[0, 3 * captcha_setting.ALL_CHAR_SET_LEN:4 * captcha_setting.ALL_CHAR_SET_LEN].data.numpy())] predict_label = '%s%s%s%s' % (c0, c1, c2, c3) true_label = one_hot_encoding.decode(labels.numpy()[0]) print("true_label: ", true_label) print("predict_lable: ", predict_label, "\n") total += labels.size(0) if (predict_label == true_label): correct += 1 else: error.append(predict_label) true.append(true_label) if (total % 200 == 0): print('测试集数量:%d, 准确率 : %f %%' % (total, 100 * correct / total)) print('测试集数量:%d, 准确率 : %f %%' % (total, 100 * correct / total)) print('预测错误例子:\n') print('正确字符:', true) print('错误字符:', error)
def validate_model(args): if torch.cuda.is_available(): device = torch.device("cuda") else: device = torch.device("cpu") criterion = CrossEntropyLoss() mode = args.mode if mode in ['MC','LMC','MLMC']: validation_loader = torch.utils.data.DataLoader( UrbanSound8KDataset('UrbanSound8K_test.pkl', mode), batch_size=args.batch_size, shuffle=True, num_workers=args.worker_count, pin_memory=True ) model = CNN(height=85, width=41, channels=1, class_count=10, dropout=0.5, mode = mode) checkpoint = torch.load(args.checkpoint, map_location = device) model.load_state_dict(checkpoint['model']) print(f"Validating {mode} model with parameters trained for {checkpoint['epoch']} epochs.") loss, accuracy, class_accuracies = validate_single(model, validation_loader, criterion, device) print(f"accuracy: {accuracy * 100:2.2f}") print_class_accuracies(class_accuracies) elif mode == 'TSCNN': loader_LMC = torch.utils.data.DataLoader( UrbanSound8KDataset('UrbanSound8K_test.pkl', 'LMC'), batch_size=args.batch_size, shuffle=False, num_workers=args.worker_count, pin_memory=True ) loader_MC = torch.utils.data.DataLoader( UrbanSound8KDataset('UrbanSound8K_test.pkl', 'MC'), batch_size=args.batch_size, shuffle=False, num_workers=args.worker_count, pin_memory=True ) model1 = CNN(height=85, width=41, channels=1, class_count=10, dropout=0.5, mode = 'LMC') model2 = CNN(height=85, width=41, channels=1, class_count=10, dropout=0.5, mode = 'MC') checkpoint1 = torch.load(args.checkpoint, map_location = device) model1.load_state_dict(checkpoint1['model']) checkpoint2 = torch.load(args.checkpoint2, map_location = device) model2.load_state_dict(checkpoint2['model']) print(f"Validating {mode} model with parameters trained for {checkpoint1['epoch']} and {checkpoint2['epoch']} epochs.") accuracy, class_accuracies = validate_double(model1, model2, loader_LMC, loader_MC, criterion, device) print(f"accuracy: {accuracy * 100:2.2f}") print_class_accuracies(class_accuracies) else: print('Please provide a valid argument.')
def recognize(model_name='model.pk'): cnn = CNN() cnn.eval() cnn.load_state_dict(torch.load(model_name)) # print(load cnn net.) # NUM_LEN = len(setting.NUMBER) captcha_dataloader = dataset.get_captcha_data_loader() code = '' images = {} for image, label in captcha_dataloader: images[label] = image images = [images[key] for key in sorted(images)] for image in images: vimage = Variable(image) predict_label = cnn(vimage) for i in range(setting.MAX_CAPTCHA): code += setting.ALL_CHAR_SET[np.argmax( predict_label[0, i * setting.ALL_CHAR_SET_LEN:(i + 1) * setting.ALL_CHAR_SET_LEN].data.numpy())] return code
to_train = True cnn_model = CNN(vocab_size, embedding_size, n_filters, filter_sizes, pool_size, hidden_size, num_classes, dropout_keep_prob) # optimization algorithm optimizer = torch.optim.Adam(cnn_model.parameters(), lr=lr) # train and evaluation if (to_train): # train and evaluation run_train(num_epochs, cnn_model, train_iterator, valid_iterator, optimizer, loss_func, model_type) # load weights cnn_model.load_state_dict( torch.load(os.path.join(path, "saved_weights_CNN.pt"))) # predict test_loss, test_acc = evaluate(cnn_model, test_iterator, loss_func) print(f'Test Loss: {test_loss:.3f} | Test Acc: {test_acc * 100:.2f}%') if (model_type == "LSTM"): num_hidden_nodes = 93 hidden_dim2 = 128 num_layers = 2 # LSTM layers bi_directional = False num_epochs = 7 to_train = True pad_index = Text.vocab.stoi[Text.pad_token]