Exemplo n.º 1
0
def test(path):
    train_dir = pj(path, 'training')
    check_path = tf.train.latest_checkpoint(train_dir)
    print 'Restoring from:', check_path
    net = NetClf(check_path, gpu)
    data = ut.load(pj(path, 'test.pk'))

    labels = []
    probs = []
    accs = []
    for i in xrange(len(data)):
        ex = data[i]
        label = ex['is_gripping']
        ex = {k: ig.uncompress(ex[k]) for k in im_names}
        prob = net.predict(**ex)
        print prob, label
        pred = int(prob >= 0.5)

        labels.append(label)
        probs.append(prob)
        accs.append(pred == label)
    labels = np.array(labels, 'bool')
    probs = np.array(probs, 'float32')
    accs = np.array(accs)

    print 'Accuracy:', np.mean(accs)
    print 'mAP:', sklearn.metrics.average_precision_score(labels, probs)
 def im(x, crop=False, compress=True):
     x = ig.uncompress(x)
     x = np.array(x)
     if crop:
         x = crop_kinect(x)
         #ig.show(x)
     x = ig.scale(x, (256, 256), 1)
     if compress:
         x = ig.compress(x)
     return x
 def load_im(k, v):
     if k.startswith('gel') or k.startswith('im'):
         im = ig.uncompress(v)
     elif k.startswith('depth'):
         #v = np.tile(v, (1, 1, 3))
         im = v.astype('float32')
     else:
         raise RuntimeError()
     if center_crop:
         im = ut.crop_center(im, 224)
     return im
def vis_example(db_file):
    with h5py.File(db_file, 'r') as db:
        pre, mid, post = milestone_frames(db)
        #sc = lambda x : ig.scale(x, (600, None))
        sc = lambda x: x
        crop_kinect = lambda x: x
        im_base = ig.uncompress(db['color_image_KinectA'][mid])
        im_mid = sc(crop_kinect(im_base))
        im_post = sc(
            crop_kinect(ig.uncompress(db['color_image_KinectA'][post])))
        im_crop = sc(
            crop_obj(im_base,
                     np.array(db['cylinder_data']['xyz_kinect'].value)))
        depth = sc(color_depth(crop_kinect(db['depth_image_KinectA'][mid])))
        gel_a_0 = sc(ig.uncompress(db['GelSightA_image'][pre]))
        gel_b_0 = sc(ig.uncompress(db['GelSightB_image'][pre]))
        gel_a_1 = sc(ig.uncompress(db['GelSightA_image'][mid]))
        gel_b_1 = sc(ig.uncompress(db['GelSightB_image'][mid]))
        row = [
            'Color:', im_mid, 'Depth:', depth, 'Gel_A_1:', gel_a_1, 'Gel_B_1:',
            gel_b_1, 'Gel_A_0:', gel_a_0, 'Gel_B_0:', gel_b_0, 'Im after:',
            im_post, 'Cropped:', im_crop, 'Name:',
            str(np.array(db['object_name'].value[0])), 'Path:',
            db_file.split('/')[-1]
        ]
        return row
def train_clf(pr):
    data = ut.load(pj(pr.dsdir, 'train.pk'))
    xs, ys = [], []
    for ex in data:
        ex = copy.copy(ex)
        for k, v in ex.items():
            if k.startswith('gel'):
                ex[k] = ut.crop_center(ig.uncompress(v), 224)
        xs.append(example_feats(ex, pr))
        ys.append(ex['is_gripping'])
    xs = np.array(xs, 'float32')
    ys = np.array(ys, 'int64')
    clf = sklearn.pipeline.Pipeline([
        ('scale',
         sklearn.preprocessing.StandardScaler(with_mean=True, with_std=True)),
        ('svm', sklearn.svm.SVC(C=1., kernel='linear'))
    ])
    clf.fit(xs, ys)
    ut.save(pj(pr.resdir, 'clf.pk'), clf)
        def load_im(k, v):
            if k.startswith('gel') or k.startswith('im'):
                im = ig.uncompress(v)
            elif k.startswith('depth'):
                #v = np.tile(v, (1, 1, 3))
                im = v.astype('float32')
            else:
                raise RuntimeError()

            if crop_type == 'center':
                crops = [ut.crop_center(im, 224)]
            elif crop_type == 'multi':
                crops = []
                dh = (im.shape[0] - crop_dim)
                num_dim_samples = 3
                for y in np.linspace(0, dh, num_dim_samples).astype('l'):
                    dw = (im.shape[1] - crop_dim)
                    for x in np.linspace(0, dw, num_dim_samples).astype('l'):
                        crops.append(im[y:y + crop_dim, x:x + crop_dim])
            return ut.shuffled_with_seed(crops, k.split('_')[0] + str(i))