Exemplo n.º 1
0
class TestTpshopReg(unittest.TestCase):
    __session = None

    @classmethod
    def setUpClass(cls) -> None:
        cls.tp_reg_api = TpshopRegApi()
        cls.tp_login_api = TpshopLoginApi()

    def setUp(self) -> None:
        if self.__session is None:
            self.__session = requests.Session()

    def tearDown(self) -> None:
        self.__session.close()

    @parameterized.expand(get_test_data(app.BASE_PATH + '/data/reg_data.json'))
    def test_reg(self, reg_data, http_code, status, msg):
        """注册用例"""
        # 获取注册验证码
        response_reg_verify = self.tp_reg_api.get_reg_verify(self.__session)
        # 断言注册验证码获取成功
        self.assertIn("image", response_reg_verify.headers.get('Content-Type'))
        # 获取注册接口
        response_reg = self.tp_reg_api.get_reg(self.__session, reg_data)
        # 断言注册成功-status,msg,响应状态码与预期是否一致
        print(response_reg.status_code)
        if response_reg.json().get('status') == 1:
            print(response_reg.json().get('result').get('mobile'))
        print(response_reg.json().get('status'))
        print(response_reg.json().get('msg'))
        assert_reg(self, http_code, status, msg, reg_data.get('username'),
                   response_reg)
        # 如果注册成功,执行登陆,否则不登录
        if (response_reg.json().get('msg')) == "注册成功":
            # 获取登陆验证码
            response_login_verify = self.tp_login_api.get_login_verify(
                self.__session)
            # 断言登陆验证码获取成功
            self.assertIn("image",
                          response_login_verify.headers.get('Content-Type'))
            # 获取登陆接口
            login_data = {
                'username': reg_data.get('username'),
                'verify_code': reg_data.get('verify_code'),
                'password': reg_data.get('password')
            }
            response_login = self.tp_login_api.get_login(
                self.__session, login_data)
            # 断言登陆成功-断言status,msg,响应状态码与预期是否一致
            print("登陆:", response_login.status_code)
            print("登陆:", response_login.json().get('result').get('mobile'))
            print("登陆:", response_login.json().get('status'))
            print("登陆:", response_login.json().get('msg'))
            assert_reg(self, http_code, status, msg, reg_data.get('username'),
                       response_login)
        else:
            print("反向用例,不做登陆")
Exemplo n.º 2
0
def test(model, k, num_each_class):
    model.eval()  # TODO: notice there is a difference in L0 gate
    base, test, label = utils.get_test_data(num_each_class=num_each_class)
    latent_test = get_latent(test)
    latent_base = get_latent(base)

    pred = kNNClassifer(latent_base, latent_test, k)

    acc = utils.get_accuracy(pred, label).tolist()
    print('test acc of {}-NN is {}:'.format(k, acc))
Exemplo n.º 3
0
def nn_baseline_predict(net):
	test_data = utils.get_test_data()
	test_examples = features.generate_feature_vectors(test_data, USE_GLOVE)
	#test_examples = np.array([np.array([random.uniform(-0.5, 0.5) for i in range(BASELINE_VECTOR_SIZE)]) for j in range(len(test_data))])
	test_target = [1 if int(p[1]) > 2 else -1 for p in test_data]
	predictions = []
	for ex in test_examples:
		pred = net.predict(ex)
		if pred > 0: predictions.append(1)
		else: predictions.append(-1)
	report_results(test_target, predictions, True)
Exemplo n.º 4
0
def main():
    parser = argparse.ArgumentParser(description='Pretraining argument parser')
    parser = load_pretrain_args(parser)
    parser = load_test_args(parser)
    args = parser.parse_args()

    set_seeds(args.seed)

    train_data = get_train_data()
    valid_data = get_valid_data()
    test_data = get_test_data()

    nnet = create_nnet(train_data, args)

    optimizer = Adam(nnet.parameters(), lr=args.lr)
    ce_loss = nn.CrossEntropyLoss()
    mse_loss = nn.MSELoss()

    action_space = ActionSpace()

    tb = SummaryWriter()

    best_score = 0

    for epoch in range(1, args.update_epochs + 1):
        print(f'Epoch {epoch}')

        for indice in random_batch(len(train_data), args.train_batch_size):
            batch = train_data[indice]
            input_batch = to_input_batch(batch, torch.device('cuda'))

            policies, values = nnet(input_batch)

            target_policies = get_target_policies(batch, action_space).cuda()
            target_values = get_target_values(batch).cuda()

            policy_loss = ce_loss(policies, target_policies)
            value_loss = mse_loss(values, target_values)
            loss = policy_loss + value_loss

            optimizer.zero_grad()
            loss.backward()
            optimizer.step()

        accuracy = test(valid_data, nnet, args, tb, epoch)

        if accuracy > best_score:
            best_score = accuracy
            torch.save(nnet.module.state_dict(), 'models/pretrained.pt')

    nnet.module.load_state_dict(torch.load('models/pretrained.pt'))

    test(test_data, nnet, args, tb, args.update_epochs + 1)
Exemplo n.º 5
0
def evaluate_model(model, load_file, test_ind = None):
    file_test = ['cb513_700', 'ts115_700', 'casp12_700']
    if test_ind is None:
        test_ind = range(len(file_test))
    test_accs = []
    names = []
    for i in test_ind:
        X_test_aug, y_test = get_test_data(file_test[i])
        model.load_weights(load_file)
        score = model.evaluate(X_test_aug, y_test, verbose=2, batch_size=1)
        #print(file_test[i] +' test accuracy: ' + str(score[1]))
        test_accs.append(score[1])
        names.append(file_test[i])
    return dict(zip(names, test_accs))
Exemplo n.º 6
0
def run_svm_baseline():
	raw_data = get_data()
	#input_data = np.array([np.array([random.uniform(-0.5, 0.5) for i in range(BASELINE_VECTOR_SIZE)]) for j in range(len(raw_data))])
	input_data = features.generate_feature_vectors(raw_data, False, True)
	target_data = np.array([int(pair[1]) for pair in raw_data])
	state = random.randint(0, int(time.time()))
	#X_train, X_test, Y_train, Y_test = cross_validation.train_test_split(input_data, target_data, test_size=0.1, random_state=state)
	test_data = utils.get_test_data()
	target_test = np.array([int(pair[1]) for pair in test_data])
	input_test = features.generate_feature_vectors(test_data, False, True)
	clf = svm.SVC(kernel='linear', C=1).fit(input_data, target_data)
	preds = [clf.predict(x)[0] for x in input_test]
	#x = clf.score(input_test, target_test)
	#print x 
	print "reporting..."
	report_results(target_test, preds)
Exemplo n.º 7
0
def main():
    parser = argparse.ArgumentParser(description='Test argument parser')
    parser = load_test_args(parser)
    args = parser.parse_args()

    set_seeds(args.seed)

    test_data = get_test_data()

    nnet = create_nnet(test_data, args)

    nnet.module.load_state_dict(torch.load(f'models/{args.load}'))

    tb = SummaryWriter()

    start_time = time()
    policy_test(test_data, nnet, args, tb, epoch=0)
    print(f'test time: {time() - start_time:.3f} sec.')
Exemplo n.º 8
0
def train():
    # ======= paramter ==============
    use_local_net = False
    epoch = 20
    num_epoches = 40
    batch_size = 32

    # =======net ================
    model_path = 'model_without_sigmoid\\' + \
        'model' + '_' + str(epoch - 1) + model_ext
    if use_local_net:
        model = load_model(model_path)
        logger.info('use local model from %d epoch', epoch)
    else:
        model = net()
    logger.info('model inited')
    test_cells, test_masks = get_test_data()

    # ===========train =============
    while epoch < num_epoches:
        logger.info('%s epoch %d started', now_str(), epoch)
        generator = batch_data_generator(batch_size=batch_size,
                                         use_shuffle=True)
        batch_idx = 0
        for cell_mask in generator:
            cell_list = np.array([item[0] for item in cell_mask])
            mask_list = np.array([item[1] for item in cell_mask])
            model.train_on_batch(cell_list, mask_list)
            batch_idx += 1
            if batch_idx % 20:
                logger.info('%s epoch %d batch_index %d', now_str(), epoch,
                            batch_idx)
        batch_idx = 0

        # ===========test==================
        predict_masks = model.predict(test_cells, batch_size=64)
        error = test_error(test_masks, predict_masks)
        logger.info('predict error %f in epoch %d', error, epoch)
        model.save('model_without_sigmoid\\' + 'model' + '_' + str(epoch) +
                   model_ext)
        logger.info('%s epoch %d model saved', now_str(), epoch)
        epoch += 1
Exemplo n.º 9
0
	def __init__(self, use_glove=False, unigrams=False):
		self.unigrams = unigrams
		self.submodels = []
		self.use_glove = use_glove
		self.test_data = utils.get_test_data()
		self.ONEvALL = 0
		self.TWOvALL = 1
		self.THREEvALL = 2
		self.FOURvALL = 3
		self.FIVEvALL = 4
		self.ONEvTWO = 5
		self.ONEvTHREE = 6
		self.ONEvFOUR = 7
		self.ONEvFIVE = 8
		self.TWOvTHREE = 9
		self.TWOvFOUR = 10
		self.TWOvFIVE = 11
		self.THREEvFOUR = 12
		self.THREEvFIVE = 13
		self.FOURvFIVE = 14
Exemplo n.º 10
0
    def evaluate(self):
        # get predictions for all species
        X_test, y_true = get_test_data()
        test_df = pd.read_csv("../plantbase/data/test_data.csv").drop(
            columns="Unnamed: 0")
        y_pred = self.model.predict(X_test)

        y_pred_df = pd.DataFrame(y_pred,
                                 columns=np.sort(test_df.genus.unique()))

        # get species with top prediction and true species
        y_pred_df['pred_genus'] = y_pred_df.idxmax(axis=1)
        y_pred_df['true_genus'] = y_true[:, 0]

        # measure success rate
        prediction_review = (
            y_pred_df['pred_genus'] == y_pred_df['true_genus'])
        accuracy = prediction_review.value_counts(
        )[True] / prediction_review.count()
        print(f'test accuracy: {accuracy}')
Exemplo n.º 11
0
    def test(self, config):
        input_ = get_test_data('test.h5')
        images_shape = [1, 288, 288, 1]
        labels_shape = [1, 288, 288, 1]
        num = 200
        results = np.zeros(
            [num, input_.shape[1], input_.shape[2], input_.shape[3]])
        for i in range(num):
            _input_ = input_[i, :, :, :]
            _input_ = np.expand_dims(_input_, 0)
            self.build_model(images_shape, labels_shape)
            tf.global_variables_initializer().run(session=self.sess)
            self.load(config.checkpoint_dir, restore=True)
            result = self.sess.run([self.pred],
                                   feed_dict={self.images: _input_})
            result = np.asarray(result)
            print(result.shape)
            self.sess.close()
            tf.reset_default_graph()
            self.sess = tf.Session()
            results[i] = result[0][0]

        save_test_results(results)
def run_experiment(num_models,
                   training_dir,
                   test_dir,
                   results_dir,
                   transform,
                   weights_file=None):
    training_set = get_training_data(training_dir,
                                     COLOURS,
                                     IMG_ROWS,
                                     IMG_COLS,
                                     transform=transform)
    test_set = get_test_data(test_dir,
                             COLOURS,
                             IMG_ROWS,
                             IMG_COLS,
                             transform=transform)
    for epoch in range(num_models):
        callbacks_list = []
        if weights_file:
            callbacks_list.append(
                ModelCheckpoint(weights_file,
                                monitor='val_accuracy',
                                verbose=1,
                                save_best_only=True))
        callbacks_list.append(
            LearnedAccuracyWriter(COLOURS, test_set, epoch, PATIENCE,
                                  results_dir))
        callbacks_list.append(
            EarlyStopping(monitor='val_accuracy', patience=PATIENCE))
        model = colour_net(NUM_CLASSES, weights_file)
        model.fit_generator(training_set,
                            steps_per_epoch=STEPS_PER_EPOCH,
                            epochs=MAX_EPOCHS,
                            validation_data=test_set,
                            callbacks=callbacks_list,
                            verbose=1)
Exemplo n.º 13
0
    def evaluate(self):
        #self.mlflow_log_metric("y_pred", y_pred)
        # get predictions for all species
        X_test, y_true = get_test_data()
        test_df = pd.read_csv("../plantbase/data/test_data.csv").drop(
            columns="Unnamed: 0")
        y_pred = self.model.predict(X_test)
        self.mlflow_log_metric("y_pred", y_pred)
        estimator_params = self.kwargs.get("estimator_params", 'CNN_basic')
        self.mlflow_log_param("estimator", estimator)
        y_pred_df = pd.DataFrame(y_pred,
                                 columns=np.sort(test_df.genus.unique()))

        # get species with top prediction and true species
        y_pred_df['pred_genus'] = y_pred_df.idxmax(axis=1)
        y_pred_df['true_genus'] = y_true[:, 0]

        # measure success rate
        prediction_review = (
            y_pred_df['pred_genus'] == y_pred_df['true_genus'])
        accuracy = prediction_review.value_counts(
        )[True] / prediction_review.count()
        self.mlflow_log_metric("accuracy", accuracy)
        print(f'test accuracy: {accuracy}')
Exemplo n.º 14
0
    rest_of_search_space = generate_parameters_search_space(rest_of_variations)
    extended_search_space = []
    for parameters in rest_of_search_space:
        for value in variations[first_parameter]:
            extended_parameters = copy(parameters)
            extended_parameters[first_parameter] = value
            extended_search_space.append(extended_parameters)
    else:
        extended_search_space.extend({
            first_parameter: value
        } for value in variations[first_parameter])
    return extended_search_space


if __name__ == '__main__':
    test_set, training_set = get_test_data()
    stylized_facts = (
        Return60,
        Return30,
    )

    parameters_search_space = generate_parameters_search_space()

    w = numpy.ones((2,))

    optimal_parameters = smm_optimize(training_set, parameters_search_space, stylized_facts, w)

    print(optimal_parameters)

    error = smm_error(test_set, optimal_parameters, stylized_facts, w)
Exemplo n.º 15
0
            model_num_DR + '.ckpt')
        print("Model DR restored.")
with sess2.as_default():
    with NormNet_graph.as_default():
        tf.global_variables_initializer().run()
        saver2 = tf.train.Saver()
        saver2 = tf.train.import_meta_graph(pre_ck_pnts_dir_NP + '/model_' +
                                            model_num_NP + '/model_' +
                                            model_num_NP + '.ckpt.meta')
        saver2.restore(
            sess2, pre_ck_pnts_dir_NP + '/model_' + model_num_NP + '/model_' +
            model_num_NP + '.ckpt')
        print("Model NP restored.")

# Read the test images and run the HDNet
test_files = get_test_data(data_main_path)

# Read the HUMBI test images and run the HDNet
#test_files = get_HUMBI_data(os.path.join(data_main_path, 'HUMBI_example.pkl'),'test')

for f in range(len(test_files)):
    #for f in range(len(test_files[0])):
    #time_stamp = test_files[3][f]
    #data_name = str(test_files[6][f])
    data_name = str(test_files[f])
    #print('Processing time stamp: ', time_stamp)
    print('Processing file: ', data_name)
    print('\n')
    #X,Z, Z3, DP = read_HUMBI_data(test_files,f,IMAGE_HEIGHT,IMAGE_WIDTH)
    X, Z, Z3, _, _, _, _, _, _, _, _, DP = read_test_data(
        data_main_path, data_name, IMAGE_HEIGHT, IMAGE_WIDTH)
Exemplo n.º 16
0
def get_data(test_file_path):
    data_list, target_list = utils.get_test_data(test_file_path)

    data_int = [[residue_to_int.get(residue, residue_to_int["<UNK>"])
                 for residue in line] for line in data_list]
    return data_int,target_list
Exemplo n.º 17
0
def cross_val(fold):
    #build model
    model_loss = 'binary_crossentropy'
    input_dim = (n_mels, 2 * context_len, 1)
    clf = []
    clf = utils.build_model(input_dim)
    clf.compile(optimizer='adam', loss=model_loss, metrics=['accuracy'])
    if fold == 0: print(clf.summary())

    #test song indices
    song_ids_val = song_ids[fold * fold_size:(fold + 1) * fold_size]

    #split filenames into train and val
    partition = {'train': np.array([]), 'val': np.array([])}
    for song_id in np.arange(songdata.shape[0]):
        if song_id not in song_ids_val:
            partition['train'] = np.append(partition['train'],
                                           dataset_ids[str(song_id)])

    #load data files
    data_train = {
        'features':
        np.zeros([
            len(partition['train']), input_dim[0], input_dim[1], input_dim[2]
        ]),
        'labels': []
    }
    for i_samp in range(len(partition['train'])):
        data_train['features'][i_samp, ] = np.load(os.path.join(
            datadir, partition['train'][i_samp] + '.npy'),
                                                   allow_pickle=True)
        data_train['labels'].append(labels_all[partition['train'][i_samp]])

    #shuffle data
    data_train['features'], data_train['labels'] = shuffle(
        data_train['features'], data_train['labels'], random_state=0)

    #make labels one-hot
    data_train['labels'] = keras.utils.to_categorical(data_train['labels'],
                                                      num_classes=2)

    print('===\nCross-validation fold no. %d\n===\n' % song_ids_val[0])

    #iteratively calling model fit function over batches and epochs
    batch_size = 64
    n_batches = int(data_train['features'].shape[0] / batch_size)
    n_epochs = 20

    train_losses = []
    eval_losses = [1e5]
    for i_epoch in range(n_epochs):
        #train
        train_loss = 0
        for i_batch in range(n_batches):
            data_batch = data_train['features'][i_batch *
                                                batch_size:(i_batch + 1) *
                                                batch_size]
            labels_batch = data_train['labels'][i_batch *
                                                batch_size:(i_batch + 1) *
                                                batch_size]
            train_loss_batch, _ = clf.train_on_batch(data_batch, labels_batch)
            train_loss += train_loss_batch
        train_losses.append(train_loss / n_batches)

        #evaluate to save best model across epochs
        eval_loss = 0
        for song_id in song_ids_val:
            boundaries = songdata['Boundaries'][song_id].split(',')
            filepath = os.path.join(audio_dir,
                                    songdata['Concert name'][song_id] + '.wav')
            data_val = utils.get_test_data(audio_filepath=filepath,
                                           samp_rate=sr,
                                           gt_boundaries=boundaries,
                                           offset=0)
            data_val['labels'] = keras.utils.to_categorical(data_val['labels'],
                                                            num_classes=2)

            eval_loss_song, _ = clf.evaluate(data_val['features'],
                                             data_val['labels'],
                                             verbose=0)
            eval_loss += (eval_loss_song / len(song_ids_val))

        if eval_loss < np.min(eval_losses):
            clf.save_weights(
                os.path.join(model_savepath, 'weights_fold%d.hdf5' % fold))
        eval_losses.append(eval_loss)

        print('\nEpoch %d/%d\tTrain Loss: %1.3f\tVal Loss: %1.3f' %
              (i_epoch + 1, n_epochs, train_losses[-1], eval_losses[-1]))

    #Validate saved model on left-out song
    clf.load_weights(os.path.join(model_savepath,
                                  'weights_fold%d.hdf5' % fold))

    scores = []
    for song_id in song_ids_val:
        scores_song = []
        for offset in audio_offset_list:
            scores_offset = []
            boundaries = songdata['Boundaries'][song_id].split(',')
            filepath = os.path.join(audio_dir,
                                    songdata['Concert name'][song_id] + '.wav')
            data_val = utils.get_test_data(audio_filepath=filepath,
                                           samp_rate=sr,
                                           gt_boundaries=boundaries,
                                           offset=offset)
            out_probs = clf.predict(data_val['features'])[:, 1]

            for predict_thresh in [0.5, 0.6, 0.65, 0.7, 0.75, 0.8, 0.85]:
                out_labels = (out_probs > predict_thresh).astype(int)
                plot_name = 'fold%d_' % fold + songdata['Concert name'][
                    song_id] + '_thresh_' + str(predict_thresh)
                scores_offset = np.append(
                    scores_offset,
                    utils.eval_output(out_labels, out_probs,
                                      data_val['labels'], eval_tol,
                                      context_len, plot_savepath, plot_name))

            if len(scores_song) == 0: scores_song = scores_offset
            else: scores_song += scores_offset

        if len(scores) == 0:
            scores = np.atleast_2d(scores_song)
        else:
            scores = np.vstack((scores, np.atleast_2d(scores_song)))
    return scores
Exemplo n.º 18
0
import sys

if __name__ == "__main__":
    mode = sys.argv[1]
    data_from = sys.argv[2]
    data_path = sys.argv[3]

    if data_from == "image":
        data = utils.load_pie_jpg(data_path)
        with open("./data/PIE/data.pkl", "wb") as fout:
            pickle.dump(data, fout)

    else:
        with open("./data/PIE/data.pkl", "rb") as fin:
            data = pickle.load(fin)

    data = data.sample(frac=1).reset_index(drop=True)
    train_data = utils.get_train_data(data)
    test_data = utils.get_test_data(data)

    del data
    if mode == "train":
        module = VGG11(64, 64, batch_size=32, learning_rate=0.001, mode=mode)
        del test_data
        gc.collect()
        module.train(train_data[0], train_data[1])
    elif mode == "eval":
        module = VGG11(64, 64, batch_size=test_data[0].shape[0], mode=mode)
        del train_data
        gc.collect()
        module.eval(test_data[0], test_data[1])
Exemplo n.º 19
0
## Parameters
batch_size  = 8
patch_h     = 128
patch_w     = 128
stride_h    = 5
stride_w    = 5
Batch_size  = 8


Estimated_masks = []
True_masks      = []

## Get the dataset for test
Dataset_add = './DIBCO/' 
Te_d, Te_m = UT.get_test_data(Dataset_add, 2016)

## Model
model = M.BCDU_net_D3(input_size = (128, 128,1))
model.load_weights('weight_text.hdf5')

y_scores = []
y_true   = []

## Mask estimation using batches
for idx in range(len(Te_d)):
    IMG = Te_d[idx]
    MSK = Te_m[idx]
    patches , new_h, new_w = UT.extract_ordered_overlap(IMG, patch_h, patch_w, stride_h, stride_w)
    patches     = np.expand_dims(patches,  axis = 3)
    predictions = model.predict(patches, batch_size= Batch_size, verbose=1)
Exemplo n.º 20
0
    #args.load = "checkpoints/0402CP120.pth"
    if args.load:
        net.load_state_dict(torch.load(args.load))
        print('Model loaded from {}'.format(args.load))

    if args.gpu:
        net.cuda()
        #net = torch.nn.DataParallel(net, device_ids=args.gpu).cuda()
        # cudnn.benchmark = True # faster convolutions, but more memory

    input_path = '../data/train_feature.npy'
    target_path = '../data/train_social_label.npy'

    train_set = get_train_data(input_path, target_path)
    test_set = get_test_data(input_path, target_path)

    train_data_loader = DataLoader(dataset=train_set,
                                   num_workers=4,
                                   batch_size=args.batchsize,
                                   shuffle=True)
    test_data_loader = DataLoader(dataset=test_set,
                                  num_workers=4,
                                  batch_size=args.batchsize,
                                  shuffle=False)

    # predit(net=net,
    #           train_loader = train_data_loader,
    #           val_loader = test_data_loader,
    #           epochs=args.epochs,
    #           batch_size=args.batchsize,
Exemplo n.º 21
0
import numpy as np
import pandas as pd
from skimage.transform import resize
from matplotlib import pyplot as plt

SIZE = 256
ORI_SIZE = 1024
TH = 0.5
parent_path = "E:/Kaggle/RSNA Pneumonia Detection Challenge/"
model_dir = os.path.join(parent_path, "seg_models/COORD_ASPP_UNET_DENSE/")
models = ["model_0.h5", "model_1.h5", "model_2.h5", "model_3.h5", "model_4.h5"]
#models=["model_0.h5","model_1.h5"]
test_dir = "E:/Kaggle/RSNA Pneumonia Detection Challenge/stage_1_test_images/"
#test_dir="E:/Kaggle/RSNA Pneumonia Detection Challenge/test/"

test_x, patient_ids = get_test_data(test_dir, SIZE)
model = MODEL(SIZE)

avg_preds = np.zeros((len(test_x), SIZE, SIZE, 1))

for model_p in models:

    model_path = os.path.join(model_dir, model_p)
    model = MODEL(SIZE)

    model.load_weights(model_path)
    preds = model.predict(test_x, verbose=1)
    avg_preds += preds

avg_preds /= len(models)
Exemplo n.º 22
0
import numpy as np
from sklearn import tree
import utils

X, Y = utils.get_train_data()

clf = tree.DecisionTreeClassifier()
clf = clf.fit(X, Y)

test_images, test_labels = utils.get_test_data()

num_correct = 0
confusion_matrix = np.zeros((10, 10))
guesses = clf.predict(test_images)
for i in range(len(test_images)):
    guess = guesses[i]
    real = test_labels[i]

    if guess == real:
        num_correct += 1

    confusion_matrix[real][guess] += 1

for i in range(10):
    confusion_matrix[i] /= np.sum(confusion_matrix[i])
accuracy = num_correct / len(test_labels)

print("Accuracy: " + str(accuracy))
print(np.around(confusion_matrix, 3))
    #if boundaries provided, read them
    try:
        boundaries = songs_data['Boundaries'][i_song].split(',')
    except:
        boundaries = []

    scores = []
    #loop over offset versions of test audio (test-time augmentation)
    for offset in audio_offset_list:
        if os.path.exists(song_name):
            filepath = song_name
        else:
            filepath = os.path.join(audio_dir, song_name)

        data_test = utils.get_test_data(audio_filepath=filepath,
                                        gt_boundaries=boundaries,
                                        offset=offset)
        out_probs = clf.predict(data_test['features'])[:, 1]
        out_probs /= np.max(out_probs)
        out_labels = (out_probs > 0.3).astype(int)
        out_labels = utils.smooth_predictions(out_labels, out_probs, eval_tol)

        #if no boundaries available, plot output and save predicted boundaries to log
        if len(boundaries) == 0:
            #plt.plot(out_probs); plt.plot(out_labels); plt.show()
            flog.write('Predicted boundaries:\t')
            for item in np.where(out_labels == 1)[0]:
                flog.write('%5.2f s, ' % (item * frame_len))
            flog.write('\n')

        #else, evaluate predictions
Exemplo n.º 24
0
    'oscillating_linear_oracle': simulate_oscillating_linear_oracle,
    'identity': simulate_identity,
}


def get_growth_series(series: list):
    last_price = series[0]
    result = []
    for i, number in enumerate(series):
        result.append(number / last_price)
        last_price = number
    return result


if __name__ == '__main__':
    test_data, _ = get_test_data(1)
    divergences = defaultdict(list)
    for trade in test_data:
        real_time_series = get_time_series_from_trade(trade)
        # pyplot.plot(range(len(real_time_series)), real_time_series, label='real')
        for simulator_name, simulator in simulators.items():
            simulated_time_series = simulator(real_time_series)
            # pyplot.plot(range(len(simulated_time_series)), simulated_time_series, label=simulator_name)
            divergences[simulator_name].append(
                gsl_div(
                    numpy.array([get_growth_series(real_time_series)]),
                    numpy.array([get_growth_series(simulated_time_series)]),
                ))
        # pyplot.legend()
        # pyplot.show()
        for simulator_name, divs in divergences.items():
# Quadratischer Fehler
def quadratic_error(y_true, y_pred):
    return np.mean((y_true - y_pred)**2)


# Absoluter Fehler
def absolute_error(y_true, y_pred):
    return np.mean(np.abs(y_true - y_pred))


# Daten erstellen und umformen
x_train, y_train = utils.get_train_data()
x_train = x_train[:, np.newaxis]

x_test, y_test = utils.get_test_data()
x_test = x_test[:, np.newaxis]

# Lineare Regression
model = lr()
model.fit(x_train, y_train)

### Fehler berechnen
y_pred_train = model.predict(x_train)
y_pred_test = model.predict(x_test)

# Trainingsfehler
quadratic_train_error = quadratic_error(y_train, y_pred_train)
absolute_train_error = absolute_error(y_train, y_pred_train)

# Testfehler
if config['use_hyperynm']:
    num_labels = 3

test_sets = config['test_files']
batch_size = config['batch_size']
results_dir = config['results_dir']
eval_dir = config['eval_dir']

logging.info("Loading Model ...")
model = CrossEncoder(os.path.join(config['saved_model_dir'],
                                  config['eval_base']),
                     num_labels=num_labels)
logging.info("Done Loading Model ...")

for test_set in test_sets:
    test_name = test_set.split('.')[0]
    logging.info("Reading " + test_name + " Data")
    test_data, all_sentences, all_definitions = get_test_data(
        os.path.join(eval_dir, test_set), True)
    logging.info("Computing and Writing " + test_name + " Scores")
    scores = get_crossencoder_scores(all_sentences, all_definitions,
                                     batch_size, model)
    populate_scores(test_data, scores)
    scores_dict = compute_test_metrics(test_data, False)
    out_dir = os.path.join(results_dir, config['eval_base'])
    if not os.path.exists(out_dir):
        os.makedirs(out_dir)
    out_file = os.path.join(out_dir, test_name + '_results.csv')
    write_scores(out_file, scores_dict)
    logging.info("Done Writing Scores for " + test_name + " ....")
Exemplo n.º 27
0
# 导包
import unittest, logging, app

import pymysql
import requests
from parameterized import parameterized

from api.emp_api import EmployeeApi
from utils import assert_utils, get_test_data


test_data = get_test_data(app.BASE_DIR + "/data/login.json")
# 创建测试类集成unittest.TestCase
class TestLogin(unittest.TestCase):

    # 初始化unittest的函数
    def setUp(self):
        # 实例化EmployeeApi
        self.emp_handle = EmployeeApi()

    def tearDown(self):
        pass

    @parameterized.expand(test_data)
    def test_01_login(self, mobile, password, http_code, success, code, message):
        # 调用登陆
        response = self.emp_handle.login(mobile, password)
        # 打印登陆结果
        logging.info("员工模块的登陆结果为:{}".format(response.json()))
        assert_utils(self, response, http_code, success, code, message)
Exemplo n.º 28
0
#!/usr/bin/env python

from numpy import exp
from sklearn.linear_model import ElasticNet
from sklearn.ensemble import RandomForestRegressor
from sklearn.ensemble import GradientBoostingRegressor
import utils
import encode as enc

best_ratio = .825
best_alpha = 0.00062

if __name__ == '__main__':
    train = utils.get_train_data([], False)
    test = utils.get_test_data(None, [])
    ncols = [
        c for c, d in zip(train.columns, train.dtypes)
        if str(d) in ['float64', 'int64']
    ]
    ncols.remove('SalePrice')
    ncols.remove('GarageYrBlt')
    ncols.remove('Id')
    ids = test.Id
    train, test = enc.fix_categorical(train.drop(utils.TEST_IGNORE, axis=1),
                                      test.drop(utils.TEST_IGNORE, axis=1),
                                      option='one_hot')
    enc.fix_numeric(train, test, ncols, scaling='uniform')

    clf3 = GradientBoostingRegressor()
    cv = utils.run_cross_val(clf3, train)
    print("GradientBoosting")
Exemplo n.º 29
0
 def test_nested_user(self):
     for json in ['deal-detail.json', 'deal-detail-numeric-user-id.json']:
         data = get_test_data(json)
         model = dict_to_model(data, Deal)
         self.assertTrue(isinstance(model.user, User))
Exemplo n.º 30
0
        return accuracy, confusion_matrix

    # get most/least prototypical
    def get_prototypical(self):
        return self.most_prototypical, self.least_prototypical

    # get odds ratio heatmap
    def get_odds_ratio(self, a, b):
        a_map = (self.black_count[a] + self.laplace_smoothing) / (self.digit_count[a] + 2.0 * self.laplace_smoothing)
        b_map = (self.black_count[b] + self.laplace_smoothing) / (self.digit_count[b] + 2.0 * self.laplace_smoothing)
        odds_map = a_map / b_map
        return np.log(a_map), np.log(b_map), np.log(odds_map)

if __name__ == "__main__":
    train_images, train_numbers = utils.get_train_data()
    test_images, test_numbers = utils.get_test_data()

    # train here
    classifier = SinglePixelClassifier(train_images, train_numbers)

    # classify here
    accuracy, confusion_matrix = classifier.evaluate(test_images, test_numbers)

    print("\nAccuracy over all of test data: {:.2%}".format(accuracy))

    print("\nConfusion matrix: row - truth label, column - classifier output")
    print(np.around(confusion_matrix, 3))

    most_prototypical, least_prototypical = classifier.get_prototypical()

    print("\nMost Prototypical: (index, prior, truth label, classifier output)")
Exemplo n.º 31
0
    def loss(self, prediction, target):
        """
        """
        loss = nn.CrossEntropyLoss()
        return loss(prediction, target)


if __name__ == '__main__':
    root = Path('/data/FSDKaggle2018/FSDKaggle2018.audio_train')
    train_fname = Path(
        '/data/FSDKaggle2018/FSDKaggle2018.meta/train_post_competition.csv')
    labels, label_dct = utils.get_dataset_meta(train_fname)

    num_class = len(labels)
    full_df = utils.get_test_data(train_fname)

    train_df = full_df[full_df.test == False]
    train_df.index = range(len(train_df))

    test_df = full_df[full_df.test == True]
    test_df.index = range(len(test_df))

    config = parse_config('config.json')
    input_length = config.input_length
    train_dataset = utils.Dataset(root,
                                  train_df,
                                  input_length,
                                  num_class=num_class,
                                  label_dct=label_dct)
    train_data_iter = data.DataLoader(train_dataset, batch_size=8)
Exemplo n.º 32
0
class TestGmail:
    """
    Check that email can be sent and received using gmail service

    """
    def login(self, username, password):
        "Login scenario implementation"
        gmail_url = "https://mail.google.com/"
        logging.info("Opening {}".format(gmail_url))

        page = LoginPage(self.driver)
        self.driver.get(page.url)
        logging.info("Inserting username")
        page.username.send_keys(username, Keys.RETURN)
        time.sleep(1)  # sleep to avoid 405 response
        logging.info("Inserting password")
        page.password.send_keys(password, Keys.RETURN)

    @pytest.mark.parametrize(
        "from_username, from_password, to_username, to_password",
        get_test_data())
    def test_mail(self, from_username, from_password, to_username,
                  to_password):
        "Test mail sending using the data file specified in config.py"
        logging.getLogger(__name__)
        logging.info("TestGmail started for the dataset: {}".format(
            (from_username, from_password, to_username, to_password)))

        today = datetime.today()
        from_email = "{}@gmail.com".format(from_username)
        to_email = "{}@gmail.com".format(to_username)

        options = Options()
        options.add_argument("--headless")

        self.driver = WebDriver(firefox_options=options)
        browser = self.driver.desired_capabilities["browserName"]
        mail_text = "{}-{}".format(browser, today)
        self.login(from_username, from_password)

        # send mail
        page = MailPage(self.driver)
        page.new_mail_button.click()

        logging.info("Filling mail form")
        page.recipient_field.click()
        page.recipient_field.send_keys(to_email)
        page.cc_link.click()
        page.cc_field.send_keys(to_email)
        page.bcc_link.click()
        page.bcc_field.send_keys(to_email)
        page.subject_field.send_keys(mail_text)
        page.body_field.click()
        page.body_field.send_keys(mail_text)

        logging.info("Sending mail with text '{}'".format(mail_text))
        page.send_button.click()
        assert page.element("Your message has been sent")

        # check mail in sent
        url = "https://mail.google.com/mail/#sent"
        self.driver.get(url)
        page = MailPage(self.driver)
        logging.info("Checking mail in sent")
        assert page.element(mail_text)
        self.driver.quit()

        # login to second account
        self.driver = WebDriver()
        self.login(to_username, to_password)
        page = MailPage(self.driver)

        logging.info("Opening email")
        page.received_email(mail_text).click()

        logging.info("Checking text")
        assert page.element(mail_text)
        logging.info("Checking sender")
        assert page.element(from_email)

        logging.info("test passed")

    def teardown(self):
        self.driver.quit()