Esempio n. 1
0
def model_inference(sess, pred, inputs, batch_size, n_his, n_pred, step_idx, min_va_val, min_val):
    '''
    Model inference function.
    :param sess: tf.Session().
    :param pred: placeholder.
    :param inputs: instance of class Dataset, data source for inference.
    :param batch_size: int, the size of batch.
    :param n_his: int, the length of historical records for training.
    :param n_pred: int, the length of prediction.
    :param step_idx: int or list, index for prediction slice.
    :param min_va_val: np.ndarray, metric values on validation set.
    :param min_val: np.ndarray, metric values on test set.
    '''
    x_val, x_test, x_stats = inputs.get_data('val'), inputs.get_data('test'), inputs.get_stats()

    if n_his + n_pred > x_val.shape[1]:
        raise ValueError(f'ERROR: the value of n_pred "{n_pred}" exceeds the length limit.')

    y_val,y2, len_val = multi_pred(sess, pred, x_val, batch_size, n_his, n_pred, step_idx)
    evl_val,v,v_ = evaluation(x_val[0:len_val, step_idx + n_his, :, :], y_val, x_stats)

    # chks: indicator that reflects the relationship of values between evl_val and min_va_val.
    chks = evl_val < min_va_val
    # update the metric on test set, if model's performance got improved on the validation.
    if sum(chks):
        min_va_val[chks] = evl_val[chks]
        y_pred,y2, len_pred = multi_pred(sess, pred, x_test, batch_size, n_his, n_pred, step_idx)
        evl_pred,v,v_ = evaluation(x_test[0:len_pred, step_idx + n_his, :, :], y_pred, x_stats)
        min_val = evl_pred
    return min_va_val, min_val
Esempio n. 2
0
def model_inference(exe, gw, gf, program, pred, inputs, args, step_idx,
                    min_va_val, min_val):
    """inference model"""
    x_val, x_test, x_stats = inputs.get_data('val'), inputs.get_data(
        'test'), inputs.get_stats()

    if args.n_his + args.n_pred > x_val.shape[1]:
        raise ValueError(
            f'ERROR: the value of n_pred "{args.n_pred}" exceeds the length limit.'
        )

    # y_val shape: [n_pred, len(x_val), n_route, C_0)
    y_val, len_val = multi_pred(exe, gw, gf, program, pred, \
            x_val, args.batch_size, args.n_his, args.n_pred, step_idx)

    evl_val = evaluation(x_val[0:len_val, step_idx + args.n_his, :, :],
                         y_val[step_idx], x_stats)

    # chks: indicator that reflects the relationship of values between evl_val and min_va_val.
    chks = evl_val < min_va_val
    # update the metric on test set, if model's performance got improved on the validation.
    if sum(chks):
        min_va_val[chks] = evl_val[chks]
        y_pred, len_pred = multi_pred(exe, gw, gf, program, pred, \
                x_test, args.batch_size, args.n_his, args.n_pred, step_idx)

        evl_pred = evaluation(x_test[0:len_pred, step_idx + args.n_his, :, :],
                              y_pred[step_idx], x_stats)
        min_val = evl_pred

    return min_va_val, min_val
Esempio n. 3
0
def model_test(inputs, batch_size, n_his, n_pred, inf_mode, load_path='./output/models/'):
    '''
    Load and test saved model from the checkpoint.
    :param inputs: instance of class Dataset, data source for test.
    :param batch_size: int, the size of batch.
    :param n_his: int, the length of historical records for training.
    :param n_pred: int, the length of prediction.
    :param inf_mode: str, test mode - 'merge / multi-step test' or 'separate / single-step test'.
    :param load_path: str, the path of loaded model.
    '''
    start_time = time.time()
    model_path = tf.train.get_checkpoint_state(load_path).model_checkpoint_path

    test_graph = tf.Graph()

    with test_graph.as_default():
        saver = tf.train.import_meta_graph(pjoin(f'{model_path}.meta'))

    with tf.Session(graph=test_graph) as test_sess:
        saver.restore(test_sess, tf.train.latest_checkpoint(load_path))
        print('>> Loading saved model from {model_path} ...')

        pred = test_graph.get_collection('y_pred')

        if inf_mode == 'sep':
            # for inference mode 'sep', the type of step index is int.
            step_idx = n_pred - 1
            tmp_idx = [step_idx]
        elif inf_mode == 'merge':
            # for inference mode 'merge', the type of step index is np.ndarray.
            step_idx = tmp_idx = np.arange(3, n_pred + 1, 3)-1
        else:
            raise ValueError(f'ERROR: test mode "{inf_mode}" is not defined.')
        x_test, x_stats = inputs.get_data('test'), inputs.get_stats()
        io.savemat('xtest.mat', {'xtest': x_test})
        y_test, y2,len_test = multi_pred(test_sess, pred, x_test, batch_size, n_his, n_pred, step_idx)
        io.savemat('ytest.mat', {'ytest': y_test})
        io.savemat('y2.mat', {'y2': y2})
        evl1,v1,v1_ = evaluation(x_test[0:len_test, 0 + n_his, :, :], y2[0, :, :, :], x_stats)
        evl2,v2,v2_ = evaluation(x_test[0:len_test, 1 + n_his, :, :], y2[2, :, :, :], x_stats)
        evl3,v3,v3_= evaluation(x_test[0:len_test, 2 + n_his, :, :], y2[2, :, :, :], x_stats)
        evl,v,v_ = evaluation(x_test[0:len_test, step_idx + n_his, :, :], y_test, x_stats)
        m=x_test[0:len_test, step_idx + n_his, :, :]
        io.savemat('xtest1.mat',{'m':m})
        io.savemat('evl1.mat', {'evl1': evl1})
        io.savemat('evl2.mat', {'evl2': evl2})
        io.savemat('evl3.mat', {'evl3': evl3})
        io.savemat('v1.mat', {'v1': v1})
        io.savemat('v2.mat', {'v2': v2})
        io.savemat('v3.mat', {'v3': v3})
        io.savemat('v1_.mat', {'v1_': v1_})
        io.savemat('v2_.mat', {'v2_': v2_})
        io.savemat('v3_.mat', {'v3_': v3_})
        for ix in [2]:
            te = evl[ix - 2:ix + 1]
            print(f'Time Step {ix + 1}: MAPE {te[0]:7.3%}; MAE  {te[1]:4.3f}; RMSE {te[2]:6.3f}.')
        print(f'Model Test Time {time.time() - start_time:.3f}s')
    print('Testing model finished!')
Esempio n. 4
0
def model_test(inputs,
               batch_size,
               n_his,
               n_pred,
               inf_mode,
               load_path='./output/models/'):
    '''
    Load and test saved model from the checkpoint.
    :param inputs: instance of class Dataset, data source for test.
    :param batch_size: int, the size of batch.
    :param n_his: int, the length of historical records for training.
    :param n_pred: int, the length of prediction.
    :param inf_mode: str, test mode - 'merge / multi-step test' or 'separate / single-step test'.
    :param load_path: str, the path of loaded model.
    '''
    start_time = time.time()
    model_path = tf.train.get_checkpoint_state(load_path).model_checkpoint_path

    test_graph = tf.Graph()

    with test_graph.as_default():
        saver = tf.train.import_meta_graph(pjoin(
            '{0}.meta'.format(model_path)))

    with tf.Session(graph=test_graph) as test_sess:
        saver.restore(test_sess, tf.train.latest_checkpoint(load_path))
        print('>> Loading saved model from {0} ...'.format(model_path))

        pred = test_graph.get_collection('y_pred')

        if inf_mode == 'sep':
            # for inference mode 'sep', the type of step index is int.
            step_idx = n_pred - 1
            tmp_idx = [step_idx]
        elif inf_mode == 'merge':
            # for inference mode 'merge', the type of step index is np.ndarray.
            step_idx = tmp_idx = np.arange(3, n_pred + 1, 3) - 1
        else:
            raise ValueError(
                'ERROR: test mode "{0}" is not defined.'.format(inf_mode))

        x_test, x_stats = inputs.get_data('test'), inputs.get_stats()

        y_test, len_test = multi_pred(test_sess, pred, x_test, batch_size,
                                      n_his, n_pred, step_idx)
        evl = evaluation(x_test[0:len_test, step_idx + n_his, :, :], y_test,
                         x_stats)

        for ix in tmp_idx:
            #te = evl[ix - 2:ix + 1]
            te = evl
            print(
                f'Time Step {ix + 1}: MAPE {te[0]:7.3%}; MAE  {te[1]:4.3f}; RMSE {te[2]:6.3f}.'
            )
        print(f'Model Test Time {time.time() - start_time:.3f}s')
    print('Testing model finished!')
Esempio n. 5
0
def model_test(inputs, batch_size, n_his, n_pred, inf_mode, load_path='./output/models/'):
    '''
    Load and test saved model from the checkpoint.
    :param inputs: instance of class Dataset, data source for test.
    :param batch_size: int, the size of batch.
    :param n_his: int, the length of historical records for training.
    :param n_pred: int, the length of prediction.
    :param inf_mode: str, test mode - 'merge / multi-step test' or 'separate / single-step test'.
    :param load_path: str, the path of loaded model.
    '''
    start_time = time.time()
    model_path = tf.train.get_checkpoint_state(load_path).model_checkpoint_path

    test_graph = tf.Graph()

    with test_graph.as_default():
        saver = tf.train.import_meta_graph(pjoin(f'{model_path}.meta'))

    config = tf.ConfigProto() 
    config.gpu_options.per_process_gpu_memory_fraction = 0.25 # 占用GPU25%的显存 
    config.gpu_options.allow_growth = True 

    with tf.Session(graph=test_graph,config=config) as test_sess:
        saver.restore(test_sess, tf.train.latest_checkpoint(load_path))
        print(f'>> Loading saved model from {model_path} ...')

        pred = test_graph.get_collection('y_pred')

        if inf_mode == 'sep':
            # for inference mode 'sep', the type of step index is int.
            step_idx = n_pred - 1
            tmp_idx = [step_idx]
        elif inf_mode == 'merge':
            # for inference mode 'merge', the type of step index is np.ndarray.
            # step_idx = tmp_idx = np.arange(3, n_pred + 1, 3) - 1
            step_idx = tmp_idx = np.array([1,2,3,4,5,6,7,8])-1
        else:
            raise ValueError(f'ERROR: test mode "{inf_mode}" is not defined.')

        x_test, x_stats = inputs.get_data('test'), inputs.get_stats()

        y_test, len_test = multi_pred(test_sess, pred, x_test, batch_size, n_his, n_pred, step_idx)
        evl = evaluation(x_test[0:len_test, step_idx + n_his, :, :], y_test, x_stats)
        print(f'evl.shape={evl.shape}')

        cnt=0;
        for ix in tmp_idx:
            # te = evl[ix - 2:ix + 1]
            te = evl[cnt:cnt+3]
            cnt+=3;
            print(f'Time {(ix + 1)*15}: MAPE {te[0]:7.3%}; MAE  {te[1]:4.3f}; RMSE {te[2]:6.3f}.')
        print(f'Model Test Time {time.time() - start_time:.3f}s')
    print('Testing model finished!')
Esempio n. 6
0
def model_test(exe, gw, gf, program, pred, inputs, args):
    """test model"""
    if args.inf_mode == 'sep':
        # for inference mode 'sep', the type of step index is int.
        step_idx = args.n_pred - 1
        tmp_idx = [step_idx]
    elif args.inf_mode == 'merge':
        # for inference mode 'merge', the type of step index is np.ndarray.
        step_idx = tmp_idx = np.arange(3, args.n_pred + 1, 3) - 1
        print(step_idx)
    else:
        raise ValueError(f'ERROR: test mode "{args.inf_mode}" is not defined.')

    x_test, x_stats = inputs.get_data('test'), inputs.get_stats()
    y_test, len_test = multi_pred(exe, gw, gf, program, pred, \
            x_test, args.batch_size, args.n_his, args.n_pred, step_idx)

    # save result
    gt = x_test[0:len_test, args.n_his:, :, :].reshape(-1, args.n_route)
    y_pred = y_test.reshape(-1, args.n_route)
    city_df = pd.read_csv(args.city_file)
    city_df = city_df.drop(0)

    np.savetxt(os.path.join(args.output_path, "groundtruth.csv"),
               gt.astype(np.int32),
               fmt='%d',
               delimiter=',',
               header=",".join(city_df['city']))
    np.savetxt(os.path.join(args.output_path, "prediction.csv"),
               y_pred.astype(np.int32),
               fmt='%d',
               delimiter=",",
               header=",".join(city_df['city']))

    for i in range(step_idx + 1):
        evl = evaluation(x_test[0:len_test, step_idx + args.n_his, :, :],
                         y_test[i], x_stats)
        for ix in tmp_idx:
            te = evl[ix - 2:ix + 1]
            print(
                f'Time Step {i + 1}: MAPE {te[0]:7.3%}; MAE  {te[1]:4.3f}; RMSE {te[2]:6.3f}.'
            )
Esempio n. 7
0
def model_test(inputs,
               batch_size,
               n_his,
               n_pred,
               inf_mode,
               load_path='./output/models/'):
    '''
    Load and test saved model from the checkpoint.
    :param inputs: instance of class Dataset, data source for test.
    :param batch_size: int, the size of batch.
    :param n_his: int, the length of historical records for training.
    :param n_pred: int, the length of prediction.
    :param inf_mode: str, test mode - 'merge / multi-step test' or 'separate / single-step test'.
    :param load_path: str, the path of loaded model.
    '''
    start_time = time.time()
    model_path = tf.train.get_checkpoint_state(load_path).model_checkpoint_path

    test_graph = tf.Graph()

    with test_graph.as_default():
        saver = tf.train.import_meta_graph(pjoin(f'{model_path}.meta'))

    with tf.Session(graph=test_graph) as test_sess:
        saver.restore(test_sess, tf.train.latest_checkpoint(load_path))
        print(f'>> Loading saved model from {model_path} ...')

        pred = test_graph.get_collection('y_pred')

        if inf_mode == 'sep':
            # for inference mode 'sep', the type of step index is int.
            step_idx = n_pred - 1
            tmp_idx = [step_idx]
        elif inf_mode == 'merge':
            # for inference mode 'merge', the type of step index is np.ndarray.
            step_idx = tmp_idx = np.arange(3, n_pred + 1, 3) - 1
        else:
            raise ValueError(f'ERROR: test mode "{inf_mode}" is not defined.')

        x_test, x_stats = inputs.get_data('test'), inputs.get_stats()

        y_test, len_test = multi_pred(test_sess, pred, x_test, batch_size,
                                      n_his, n_pred, step_idx)
        v_ = z_inverse(y_test, x_stats['mean'], x_stats['std'])
        print("v_ shape", v_.shape)
        cnt = 0
        with open('result.csv', 'w') as csvFile:
            writer = csv.writer(csvFile)
            writer.writerow(["Id", "Expected"])
            for i in range(7):
                for j in range(12):
                    idx = 268 * i + 24 * j
                    r = v_[:, idx, :, :]
                    #print(idx, r[0].flatten())
                    for k in range(3):
                        for p in range(228):
                            _r = r[k].flatten()
                            name = str(cnt - 4) + "_" + str(15 * k +
                                                            15) + "_" + str(p)
                            if (cnt >= 4):
                                writer.writerow([name, _r[p]])
                    cnt += 1
            print("cnt", cnt)

            #writer.writerow(v_)
        csvFile.close()

        evl = evaluation(x_test[0:len_test, step_idx + n_his, :, :], y_test,
                         x_stats)

        for ix in tmp_idx:
            te = evl[ix - 2:ix + 1]
            print(
                f'Time Step {ix + 1}: MAPE {te[0]:7.3%}; MAE  {te[1]:4.3f}; RMSE {te[2]:6.3f}.'
            )
        print(f'Model Test Time {time.time() - start_time:.3f}s')
    print('Testing model finished!')