Beispiel #1
0
Datei: util.py Projekt: surban/ml
def map(X, batch_size, map_func, 
        caption="", force_output_type=None):

    if force_output_type is not None:
        if force_output_type == 'gnumpy':
            xp = gp
        elif force_output_type == 'numpy':
            xp = np
        else:
            assert False, "force_output_type must be either numpy or gnumpy"
    else:
        if isinstance(X, gp.garray):
            xp = gp
        else:
            xp = np

    ms = None
    for b, x in enumerate(draw_slices(X, batch_size, kind='sequential', 
                                      samples_are='rows', stop=True, 
                                      last_batch_may_be_smaller=True)):
        progress.status(b, X.shape[0] / batch_size, caption)

        m = map_func(x)
        if ms is None:
            if m.ndim == 1:
                ms = xp.zeros((X.shape[0],))
            elif m.ndim == 2:
                ms = xp.zeros((X.shape[0], m.shape[1]))
            elif m.ndim == 3:
                ms = xp.zeros((X.shape[0], m.shape[1], m.shape[2]))
            elif m.ndim == 4:
                ms = xp.zeros((X.shape[0], m.shape[1], m.shape[2], m.shape[3]))
            else:
                assert False, "%d dimensions are not supported" % m.ndim
        
        if ms.ndim == 1:
            ms[b*batch_size : (b+1)*batch_size] = m
        elif ms.ndim == 2:
            ms[b*batch_size : (b+1)*batch_size, :] = m
        elif ms.ndim == 3:
            ms[b*batch_size : (b+1)*batch_size, :, :] = m
        elif ms.ndim == 4:
            ms[b*batch_size : (b+1)*batch_size, :, :, :] = m

    progress.done()
    return ms
Beispiel #2
0
    def predict_multicurve(self, predictor, force, skin, valid, what='skin'):
        assert what in ['skin', 'force']
        if what == 'skin':
            predicted = np.zeros(skin.shape)
        else:
            predicted = np.zeros(force.shape)
        predicted_conf = np.zeros(predicted.shape)
        using_conf = False
        predicted_prob = None
        using_prob = False

        for smpl in range(force.shape[1]):
            status(smpl, force.shape[1], "Predicting")
            if skin.ndim == 3:
                f, s = self.trim_to_valid(force[:, smpl], skin[:, :, smpl], valid[:, smpl])
            else:
                f, s = self.trim_to_valid(force[:, smpl], skin[:, smpl], valid[:, smpl])
            if what == 'skin':
                pp = predictor(f)
            else:
                pp = predictor(s)
            if isinstance(pp, tuple):
                predicted[0:pp[0].shape[0], smpl] = pp[0]
                if pp[1].ndim == 1:
                    predicted_conf[0:pp[1].shape[0], smpl] = pp[1]
                    using_conf = True
                elif pp[1].ndim == 2:
                    if predicted_prob is None:
                        predicted_prob = np.zeros((pp[1].shape[0], predicted.shape[0], predicted.shape[1]))
                    predicted_prob[:, 0:pp[1].shape[1], smpl] = pp[1]
                    using_prob = True
            else:
                predicted[0:pp.shape[0], smpl] = pp
        done()

        if using_prob:
            return predicted, predicted_prob
        elif using_conf:
            return predicted, predicted_conf
        else:
            return predicted