Пример #1
0
def forwardAUViewFold(au, view, fold, targetSet, trainFilesDir,
                      baseTargetForward, layerData, foldModelsPath,
                      baseFlowImagesPath, baseRGBImagesPath,
                      baseJitterImagesPath):

    auOhne0 = au.replace('0', '')

    fileGT = trainFilesDir + view + '/Training_' + auOhne0 + '.txt'
    modelsRootPath = foldModelsPath + '/fold_' + str(fold)
    targetForward = baseTargetForward + '/' + targetSet + '/' + au + '_' + view + '_Fold' + str(
        fold)

    print('fileGT ', fileGT)
    print('modelsRootPath ', modelsRootPath)
    print('targetForward ', targetForward)

    net = None
    try:
        net = loadNetModel(au, view, modelsRootPath)
    except:
        print('No model Yet')
        return

    transformerFLOW, transformerRGB = createTransformers(net)
    netParams = [net, transformerRGB, transformerFLOW]

    gts, preds, scores = forwardFormGTFile(netParams, fileGT, targetForward,
                                           baseFlowImagesPath, layerData, au,
                                           baseRGBImagesPath,
                                           baseJitterImagesPath)

    eng = matlab.engine.start_matlab()
    cs = classification_report(gts, preds)
    ps = eng.CalcRankPerformance(matlab.int8(gts), matlab.double(scores), 1,
                                 'All')
    F1_MAX = max(np.array(ps['F1']))[0]

    with open(targetForward + '/overalReport.txt', 'a') as reportFile:
        reportFile.write(str(cs) + ' \n ' + ' F1Max: ' + str(F1_MAX))
Пример #2
0
def forwardFormGTFile(netParams, fileGT, targetForward, baseFlowImagesPath,
                      layerData, au, baseRGBImagesPath, baseJitterImagesPath):
    gts = []
    preds = []
    scores = []

    net = netParams[0]
    transformerRGB = netParams[1]
    transformerFLOW = netParams[2]

    with open(fileGT) as f:
        content = f.readlines()

        idx = 0
        for aLine in content:
            #print('aLine ',aLine)
            convertedRGBPath, convertedFlowPath = getConvertedPathsFromGTLine(
                aLine, baseFlowImagesPath, baseRGBImagesPath,
                baseJitterImagesPath)
            net, flag = netForward(net, transformerFLOW, transformerRGB,
                                   convertedRGBPath, convertedFlowPath)

            if flag == False:
                print('Pair not found for ', convertedRGBPath,
                      convertedFlowPath)
                continue

            dirTargetForward, finalTargetForward = getTargetForward(
                aLine, targetForward)
            targetLabels = dirTargetForward + '/labels.txt'
            """
            print('convertedRGBPath ', convertedRGBPath)
            print('convertedFlowPath ', convertedFlowPath)
            
            print('dirTargetForward ', dirTargetForward)
            print('finalTargetForward ', finalTargetForward)
            print('OK ')
            """
            with open(targetLabels, 'a') as gtFile:
                imgName, label = getFileNameAndLabel(aLine)
                tokensFTF = finalTargetForward.split('/')
                fileName = tokensFTF[-1][:-4]
                gtFile.write(fileName + '.jpg' + ',' + label)

            gts.append(int(label))
            preds.append(net.blobs['softmax'].data[0].argmax())
            scores.append(net.blobs['softmax'].data[0][1])

            feat = net.blobs[layerData].data[0].flatten()  #just in case

            with open(finalTargetForward, 'wb') as myFile:
                np.savetxt(myFile, feat, delimiter=",")

            if idx % 200 == 0 and idx > 0:
                print(classification_report(gts, preds))
            idx = idx + 1

            if idx % 1000 == 0 and idx > 1:
                print('fileGT', fileGT)
                print('au', au)
                eng = matlab.engine.start_matlab()
                cs = classification_report(gts, preds)
                ps = eng.CalcRankPerformance(matlab.int8(gts),
                                             matlab.double(scores), 1, 'All')
                F1_MAX = max(np.array(ps['F1']))[0]
                print('F1_MAX ', F1_MAX)

    return gts, preds, scores
Пример #3
0
def forwardFormGTFile(net, transformerFLOW, transformerRGB, fileGT,
                      targetForward, basePathFLow, layer, au):
    gts = []
    preds = []
    scores = []

    with open(fileGT) as f:
        content = f.readlines()

        idx = 0
        for aLine in content:
            lineTokens = aLine.split(' ')
            pathTokens = lineTokens[0].split('/')

            pathFLow = basePathFLow + '/' + pathTokens[7] + '/' + pathTokens[
                8] + '/' + pathTokens[9] + '/' + pathTokens[10]

            #print('Path RGB ', lineTokens[0])
            #print('Path Flow ', pathFLow)
            net, flag = netForward(net, transformerFLOW, transformerRGB,
                                   lineTokens[0], pathFLow)

            #print (out['loss'])
            if flag == False:
                print('Pair not found for ', lineTokens[0])
                continue

            tempTargetForward = targetForward + '/' + pathTokens[
                7] + '/' + pathTokens[8] + '/' + pathTokens[9]
            if not os.path.exists(tempTargetForward):
                os.makedirs(tempTargetForward)
                print('created ', tempTargetForward)

            finalTargetForward = tempTargetForward + '/' + pathTokens[
                10][:-4] + '.txt'
            targetLabels = tempTargetForward + '/labels.txt'

            with open(targetLabels, 'a') as gtFile:
                gtFile.write(pathTokens[10] + ',' + lineTokens[1] + ',' +
                             lineTokens[2] + ',' + lineTokens[3] + ',' +
                             lineTokens[4] + ',' + lineTokens[5] + ',' +
                             lineTokens[6] + ',' + lineTokens[7] + ',' +
                             lineTokens[8] + ',' + lineTokens[9] + ',' +
                             lineTokens[10])

            auIdx = auArray.index(au)
            gts.append(int(lineTokens[auIdx + 1]))
            #print('GT ', int(lineTokens[auIdx + 1]))
            preds.append(net.blobs['softmax'].data[0].argmax())
            scores.append(net.blobs['softmax'].data[0][1])

            feat = net.blobs[layer].data[0].flatten()  #just in case

            #print(' ')
            #print('softmax ', net.blobs['softmax'].data[0])
            #print('feat ', feat)
            #print('finalTargetForward ',finalTargetForward )
            with open(finalTargetForward, 'wb') as myFile:
                np.savetxt(myFile, feat, delimiter=",")

            if idx % 200 == 0:
                print('Forwards ', idx)
                print(classification_report(gts, preds))

            if idx % 1000 == 0 and idx > 1:
                print('fileGT', fileGT)
                print('au', au)
                eng = matlab.engine.start_matlab()
                cs = classification_report(gts, preds)
                ps = eng.CalcRankPerformance(matlab.int8(gts),
                                             matlab.double(scores), 1, 'All')
                F1_MAX = max(np.array(ps['F1']))[0]
                print('F1_MAX ', F1_MAX)

            idx = idx + 1

    return gts, preds, scores
Пример #4
0
tasksTest = ['T1', 'T2', 'T3', 'T4', 'T5', 'T6', 'T7', 'T8', 'T9', 'T10', 'T11', 'T12', 'T13', 'T14', 'T15']
testFeats, testLabels = loadSet(testSubjects, tasksTest, txtFeatsTestDir, view, aK, bulkLoadDirVal, au)

print('txtFeatsTestDir',txtFeatsTestDir)
print('bulkLoadDirVal',bulkLoadDirVal)
print('LSTM Classficiation Val Set')
print('testFeats',testFeats);
probs = jointModel.predict_proba([testFeats, testFeats])

eng = matlab.engine.start_matlab()
probsAsArray = probs.tolist()
testLabelsAsArray = testLabels.tolist()


print('Counts Val UNBalanced')
getCounts(testLabels)
print('Classification MAX Score Val UNBalanced')
predsMax,cr = getClassificationScoreMaxCriteria(testFeats, testLabels)


#print('probsAsArray ',probsAsArray)
#print('testLabelsAsArray ',testLabelsAsArray)

ps = eng.CalcRankPerformance(matlab.int8(testLabelsAsArray), matlab.double(probsAsArray), 1, 'All')
F1_MAX_LSTM = max(np.array(ps['F1']))[0]
print('F1_MAX_LSTM ', F1_MAX_LSTM)

ps = eng.CalcRankPerformance(matlab.int8(testLabelsAsArray), matlab.double(predsMax), 1, 'All')
F1_MAX_PRED_MAX = max(np.array(ps['F1']))[0]
print('F1_MAX_PRED_MAX ', F1_MAX_PRED_MAX)
Пример #5
0
 eng = matlab.engine.start_matlab()
 for j, k, l in zip(x, anno_match, final_seg):
     img = cv2.cvtColor(j, cv2.COLOR_RGB2GRAY)
     img = img.astype('float64')
     img -= img.mean()
     img /= img.std()
     region_prop = regionprops(l)
     tmp_hara22 = []
     tmp_hara88 = []
     for anno in k:
         minr, minc, maxr, maxc = region_prop[anno[5].astype('int16') -
                                              1].bbox
         subImage = img[minr:maxr + 1, minc:maxc + 1]
         glcm = eng.graycomatrix(
             matlab.double(subImage.tolist()), 'Offset',
             matlab.int8([[0, 1], [-1, 1], [-1, 0], [-1, -1]]),
             'Symmetric', True, 'NumLevels', 32, 'GrayLimits',
             matlab.double([]))
         # compute isotropical haralick
         glcm_iso = np.asarray(glcm)
         glcm_iso = np.sum(glcm_iso, 2, keepdims=True)
         feat_iso = eng.GLCM_Features4(matlab.double(glcm_iso.tolist()),
                                       0)
         tmp_hara22.append([
             anno[4],
             np.mean(feat_iso['autoc']),
             np.mean(feat_iso['contr']),
             np.mean(feat_iso['corrm']),
             np.mean(feat_iso['corrp']),
             np.mean(feat_iso['cprom']),
             np.mean(feat_iso['cshad']),
Пример #6
0
def asiduj_test():
    """
    Test of the module

    It runs the doctest and create other tests with matlab engine calls."""
    import scipy.linalg as spl

    print("Run matlab engine...")
    if len(matlab.engine.find_matlab()) == 0:
        # si aucune session share, run
        eng = matlab.engine.start_matlab()
    else:
        # connect to a session
        eng = matlab.engine.connect_matlab(matlab.engine.find_matlab()[0])
        print("connected...")

    print("Further tests....\n")

    # create matlab data
    # ------------------------------------------------------------------------
    mf = eng.rand(3)
    mc = matlab.double([[1 + 1j, 0.3, 1j], [1.2j - 1, 0, 1 + 1j]],
                       is_complex=True)
    mi64 = matlab.int64([1, 2, 3])
    mi8 = matlab.int8([1, 2, 3])
    mb = matlab.logical([True, True, False])

    # Test conversion from matlab to numpy
    # ------------------------------------------------------------------------
    npf = matlab_to_ndarray(mf)  # no copy, if mf is changed, npf change!
    npc = matlab_to_ndarray(mc)  # still copy for complex (only)
    npi64 = matlab_to_ndarray(mi64)
    npi8 = matlab_to_ndarray(mi8)
    npb = matlab_to_ndarray(mb)

    # Test conversion from numpy to matlab
    # ------------------------------------------------------------------------
    npi = numpy.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]],
                      dtype=numpy.int64,
                      order="F")
    mi = ndarray_to_matlab(npi)
    mc2 = ndarray_to_matlab(npc)
    mf2 = ndarray_to_matlab(
        npf)  # copy, because npf has 'F' order (comes from mlarray)
    mi64_2 = ndarray_to_matlab(npi)
    mb2 = ndarray_to_matlab(npb)

    # test orientation in the matlab workspace
    # ------------------------------------------------------------------------
    eng.workspace["mi"] = mi64_2
    eng.workspace["mc2"] = mc2

    # check results
    # ------------------------------------------------------------------------
    npcc = numpy.array(
        [
            [1.0, 1.1 + 1j],
            [
                1.12 + 0.13j,
                22.1,
            ],
        ],
        dtype=numpy.complex,
    )  # assume C
    mcc = ndarray_to_matlab(npcc)
    npcc_inv = spl.inv(npcc)
    mcc_inv = eng.inv(mcc)
    print("Are the inverse of matrix equal ?")
    print(mcc_inv)
    print(npcc_inv)

    #    # no copy check
    #    # ------------------------------------------------------------------------
    #    # complex
    #
    #    npcc[0,0]=0.25
    #    print("Are the data reuse ?", ", OWNDATA =", mcc._real.flags.owndata,
    #          "same base =", mcc._real.base is npcc,
    #          ', If one is modified, the other is modified =', mcc._real[0]==npcc[0,0])
    #

    # test sparse matrix requiert Recast4py.m
    K1, K2 = eng.sptest(3.0, nargout=2)
    Ksp1 = dict_to_sparse(K1)
    Ksp2 = dict_to_sparse(K2)
Пример #7
0
def test_dummy(engine):
    assert engine.isreal(matlab.double([1]))
    assert engine.isinteger(matlab.int8([1]))
Пример #8
0
        else:
            # connect to a session
            eng = matlab.engine.connect_matlab(matlab.engine.find_matlab()[0])
            print('connected...')
    else:
        print('Matlab engine is already runnig...')

    print('Further tests....\n')

    # create matlab data
    # ------------------------------------------------------------------------
    mf = eng.rand(3)
    mc = matlab.double([[1 + 1j, 0.3, 1j], [1.2j - 1, 0, 1 + 1j]],
                       is_complex=True)
    mi64 = matlab.int64([1, 2, 3])
    mi8 = matlab.int8([1, 2, 3])
    mb = matlab.logical([True, True, False])

    # Test conversion from matlab to numpy
    # ------------------------------------------------------------------------
    npf = mlarray2np(mf)  # no copy, if mf is changed, npf change!
    npc = mlarray2np(mc)  # still copy for complex (only)
    npi64 = mlarray2np(mi64)
    npi8 = mlarray2np(mi8)
    npb = mlarray2np(mb)

    # Test conversion from numpy to matlab
    # ------------------------------------------------------------------------
    npi = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]],
                   dtype=np.int64,
                   order='F')
Пример #9
0
    def mlem(self,x_k_1=[],z_k_1=[],w_k_1=[],rho=0,iters=1):
        x_k_1 = x_k_1 if x_k_1 == [] else np.array(x_k_1).reshape(-1,1).tolist()
        z_k_1 = z_k_1 if z_k_1 == [] else np.array(z_k_1).reshape(-1,1).tolist()
        w_k_1 = w_k_1 if w_k_1 == [] else np.array(w_k_1).reshape(-1,1).tolist()

        x_k = mlb.mlem_regularized(matlab.double(x_k_1),matlab.double(z_k_1),matlab.double(w_k_1),matlab.double([rho]),matlab.int8([iters]),matlab.int8([self.frame]),matlab.int8([self.curr_real+1]))
        return np.array( x_k).reshape(1,1,self.image_shape[0],self.image_shape[1])