def gen_coxlab_features(chip_resized, feature_x, feature_y):

   image_vector = []

   im_array = np.asarray(chip_resized).astype('f')

   # -- get L3 prime SLM description (from sthor)
   desc = sthor.model.parameters.fg11.fg11_ht_l3_1_description

   # -- generate random PLOS09 3-layer model
   #desc = sthor.model.parameters.plos09.get_random_plos09_l3_description()

   # -- instantiate the SLM model
   model = SequentialLayeredModel((feature_x, feature_y), desc)

   # -- compute feature map, shape [height, width, depth]
   f_map = model.transform(im_array, pad_apron=True, interleave_stride=False)
   f_map_dims = f_map.shape
   print "shape", f_map.shape

   for j in range(f_map_dims[0]):
      for k in range(f_map_dims[1]):
         for l in range(f_map_dims[2]):
            image_vector.append(f_map[j][k][l])

   return image_vector
Example #2
0
def test_outout_with_interleave_and_stride_and_no_interleave():

    in_shape = 200, 200
    desc = L3_first_desc
    slm = SequentialLayeredModel(in_shape, desc)

    img = np.random.randn(200, 200).astype('f')

    full_features = slm.process(img, pad_apron=True, interleave_stride=True)
    features = slm.process(img, pad_apron=True, interleave_stride=False)

    assert_allclose(features, full_features[::8, ::8], rtol=RTOL, atol=ATOL)
Example #3
0
def test_null_image_same_size_as_receptive_field():

    in_shape = 121, 121
    desc = L3_first_desc
    slm = SequentialLayeredModel(in_shape, desc)

    img = np.zeros(in_shape).astype('f')

    features = slm.process(img)

    assert features.shape == (1, 1, 256)
    assert features.sum() == 0.
Example #4
0
def test_zero_input_image_with_pad_with_interleave():

    in_shape = 200, 200
    desc = L3_first_desc
    slm = SequentialLayeredModel(in_shape, desc)

    img = np.zeros(in_shape).astype('f')

    features = slm.process(img, pad_apron=True, interleave_stride=True)

    assert features.shape == (200, 200, 256)
    assert features.sum() == 0.
Example #5
0
def test_null_image_same_size_as_receptive_field():

    in_shape = 121, 121
    desc = L3_first_desc
    slm = SequentialLayeredModel(in_shape, desc)

    img = np.zeros(in_shape).astype('f')

    features = slm.process(img)

    assert features.shape == (1, 1, 256)
    assert features.sum() == 0.
Example #6
0
def test_outout_with_interleave_and_stride_and_no_interleave():

    in_shape = 200, 200
    desc = L3_first_desc
    slm = SequentialLayeredModel(in_shape, desc)

    img = np.random.randn(200, 200).astype('f')

    full_features = slm.process(img, pad_apron=True, interleave_stride=True)
    features = slm.process(img, pad_apron=True, interleave_stride=False)

    assert_allclose(features, full_features[::8, ::8], rtol=RTOL, atol=ATOL)
Example #7
0
def test_zero_input_image_with_pad_with_interleave():

    in_shape = 200, 200
    desc = L3_first_desc
    slm = SequentialLayeredModel(in_shape, desc)

    img = np.zeros(in_shape).astype('f')

    features = slm.process(img, pad_apron=True, interleave_stride=True)

    assert features.shape == (200, 200, 256)
    assert features.sum() == 0.
Example #8
0
def eDN_features(img, desc, outSize=None):
    """ Computes eDN features for given image or image sequence 'img' based  
on the given descriptor(s) 'desc'"""

    # iterates through individual DN models (Deep Networks) in the blend
    for i in xrange(len(desc)):
        imgC = img.copy()

        if desc[i]['colorSp'] == 'yuv':
            # either a single image or an image sequence
            for j in xrange(imgC.shape[2] / 3):
                imgC[:, :,
                     j * 3:j * 3 + 3] = convertRGB2YUV(imgC[:, :,
                                                            j * 3:j * 3 + 3])

        imgC = imgC.astype('f')

        model = SequentialLayeredModel((imgC.shape[0], imgC.shape[1]),
                                       desc[i]['desc'])
        fm = model.transform(imgC, pad_apron=True, interleave_stride=False)

        if outSize:
            # zoom seems to round down when non-integer shapes are requested
            # and zoom does not accept an `output_shape` parameter
            fMap = sp.ndimage.interpolation.zoom(
                fm, (outSize[0] * (1 + 1e-5) / fm.shape[0], outSize[1] *
                     (1 + 1e-5) / fm.shape[1], 1.0))
        else:
            if i == 0:
                fmShape = fm.shape[:2]
                fMap = fm
            else:
                if fm.shape[:2] == fmShape:
                    fMap = fm
                else:
                    # models with different number of layers have different
                    # output sizes, so resizing is necessary
                    fMap = sp.ndimage.interpolation.zoom(
                        fm,
                        (fmShape[0] * (1 + 1e-5) / fm.shape[0], fmShape[1] *
                         (1 + 1e-5) / fm.shape[1], 1.0))

        fMap = fMap.reshape(fMap.shape[0] * fMap.shape[1], -1, order='F')

        if i == 0:
            fMaps = fMap
        else:
            fMaps = np.hstack((fMaps, fMap))
    return fMaps, fmShape
Example #9
0
def eDN_features(img, desc, outSize=None): 
    """ Computes eDN features for given image or image sequence 'img' based  
on the given descriptor(s) 'desc'"""

    # iterates through individual DN models (Deep Networks) in the blend
    for i in xrange(len(desc)): 
        imgC = img.copy() 

        if desc[i]['colorSp'] == 'yuv':
            # either a single image or an image sequence
            for j in xrange(imgC.shape[2]/3):
                imgC[:,:,j*3:j*3+3] = convertRGB2YUV(imgC[:,:,j*3:j*3+3])

        imgC = imgC.astype('f')

        model = SequentialLayeredModel((imgC.shape[0], 
                    imgC.shape[1]), desc[i]['desc']) 
        fm = model.transform(imgC, pad_apron=True,
                interleave_stride=False)  

        if outSize: 
            # zoom seems to round down when non-integer shapes are requested
            # and zoom does not accept an `output_shape` parameter 
            fMap = sp.ndimage.interpolation.zoom(fm, 
                (outSize[0]*(1+1e-5)/fm.shape[0], 
                 outSize[1]*(1+1e-5)/fm.shape[1], 
                 1.0))  
        else:
            if i == 0: 
                fmShape = fm.shape[:2] 
                fMap = fm
            else:
                if fm.shape[:2] == fmShape:
                    fMap = fm
                else:
                    # models with different number of layers have different 
                    # output sizes, so resizing is necessary  
                    fMap = sp.ndimage.interpolation.zoom(fm, 
                        (fmShape[0]*(1+1e-5)/fm.shape[0], 
                         fmShape[1]*(1+1e-5)/fm.shape[1], 
                         1.0))  

        fMap = fMap.reshape(fMap.shape[0]*fMap.shape[1], -1, order='F')

        if i == 0:
            fMaps = fMap
        else:
            fMaps = np.hstack((fMaps, fMap))
    return fMaps, fmShape
Example #10
0
def test_no_description():

    in_shape = 256, 256
    desc = []
    slm = SequentialLayeredModel(in_shape, desc)

    assert slm.n_layers == 0
    assert slm.ops_nbh_nbw_stride == []
    assert slm.receptive_field_shape == (1, 1)
Example #11
0
def test_L3_first_desc():

    in_shape = 256, 256
    desc = L3_first_desc
    slm = SequentialLayeredModel(in_shape, desc)

    assert slm.n_layers == 4
    assert slm.ops_nbh_nbw_stride == [('lnorm', 9, 9, 1), ('fbcorr', 3, 3, 1),
                                      ('lpool', 7, 7, 2), ('lnorm', 5, 5, 1),
                                      ('fbcorr', 5, 5, 1), ('lpool', 5, 5, 2),
                                      ('lnorm', 7, 7, 1), ('fbcorr', 5, 5, 1),
                                      ('lpool', 7, 7, 2), ('lnorm', 3, 3, 1)]
    assert slm.receptive_field_shape == (121, 121)