def extract(self,X):
        assert self._models is not None, "Must be trained before calling extract"
        if(len(X) == 2):
            X, num_parts = X
        else:
            X, num_parts, orientation = X
        
        assert X.ndim == 4, "Input X dimension is not correct"
        assert num_parts == self._num_lower_parts

        X = X.reshape((X.shape[0], X.shape[1], X.shape[2]))

        secondLevelCurx = np.zeros((X.shape[0], X.shape[1] + self._lowerLayerShape[0] - self._part_shape[0], X.shape[2] + self._lowerLayerShape[1] - self._part_shape[1],1,1,self._num_lower_parts))
        secondLevelCurxCenter = np.zeros((X.shape[0], X.shape[1] + self._lowerLayerShape[0] - self._part_shape[0], X.shape[2] + self._lowerLayerShape[1] - self._part_shape[1]))

        extractedFeature = np.empty((X.shape[0] * (X.shape[1] + self._lowerLayerShape[0] - self._part_shape[0]) * (X.shape[2] + self._lowerLayerShape[1] - self._part_shape[1])))
        totalRange = X.shape[1]
        frame = (self._part_shape[0] - self._lowerLayerShape[0]) / 2
        frame = int(frame)

        for m in range(totalRange)[frame:totalRange-frame]:
            for n in range(totalRange)[frame:totalRange-frame]:
                secondLevelCurx[:,m-frame,n-frame] = index_map_pooling(X[:,m-frame:m+frame+1,n-frame:n+frame+1],self._num_lower_parts, (2 * frame + 1, 2 * frame + 1), (2 * frame + 1, 2 * frame + 1))
                secondLevelCurxCenter[:,m-frame,n-frame] = X[:,m,n]
        flattenSecondLevelCurx = secondLevelCurx.reshape((secondLevelCurx.shape[0] * secondLevelCurx.shape[1] * secondLevelCurx.shape[2], -1))
        flattenSecondLevelCurxCenter = secondLevelCurxCenter.flatten()
        #for m in range(X[0].shape[0] + self._lowerLayerShape[0]- self._part_shape[0]):
        #for n in range(X[0].shape[1] + self._lowerLayerShape[1] - self._part_shape[1]):
        #codedIndex = np.where(secondLevelCurxCenter[:,m,n]!=-1)[0]
        codedIndex = np.where(flattenSecondLevelCurxCenter!=-1)[0]
        #notCodedIndex = np.where(secondLevelCurxCenter[:,m,n] == 0)[0]
        notCodedIndex = np.where(flattenSecondLevelCurxCenter == 0)[0]
        print(codedIndex.shape)
        #firstLevelPartIndex = secondLevelCurxCenter[codedIndex,m,n]
        firstLevelPartIndex = flattenSecondLevelCurxCenter[codedIndex]
        firstLevelPartIndex = np.array(firstLevelPartIndex,dtype = np.int)
        firstLevelPartIndex = (firstLevelPartIndex - firstLevelPartIndex % self._rotation)//self._rotation

        blockSize = 10000
        for i in range(0, codedIndex.shape[0], blockSize):
            blockend = min(codedIndex.shape[0], i + blockSize)
            theta = self._models[firstLevelPartIndex[i:blockend]]
            XX = flattenSecondLevelCurx[codedIndex[i:blockend]][:,np.newaxis]
            print(XX.shape, theta.shape)
            llh = XX * np.log(theta) + (1 - XX) * np.log(1 - theta)
            bb = np.apply_over_axes
            bb = np.apply_over_axes(np.sum, llh, [-3, -2, -1])[...,0,0,0]
            maxIndex = np.argmax(bb,axis = -1)
            print(maxIndex.shape)
            extensionPart = maxIndex // self._rotation
            rotatedAngle = maxIndex % self._rotation
            extractedFeature[codedIndex[i:blockend]] = np.array(firstLevelPartIndex[i:blockend] * self._num_components * self._rotation + extensionPart * self._rotation + rotatedAngle, dtype = np.int)
        extractedFeature[notCodedIndex] = -1
        extractedFeature = extractedFeature.reshape((X.shape[0], X.shape[1] + self._lowerLayerShape[0] - self._part_shape[0], X.shape[2] + self._lowerLayerShape[1] - self._part_shape[1]))

        extractedFeature = np.array(extractedFeature[:,:,:,np.newaxis],dtype = np.int64)
        return (extractedFeature,self._num_parts, self._rotation)
    def extract(self,X):
        assert self._models is not None, "Must be trained before calling extract"
        if(len(X) == 2):
            X, num_parts = X
        else:
            X, num_parts, orientation = X
        
        assert X.ndim == 4, "Input X dimension is not correct"
        assert num_parts == self._num_lower_parts

        X = X.reshape((X.shape[0], X.shape[1], X.shape[2]))

        secondLevelCurx = np.zeros((X.shape[0], X.shape[1] + self._lowerLayerShape[0] - self._part_shape[0], X.shape[2] + self._lowerLayerShape[1] - self._part_shape[1],1,1,self._num_lower_parts))
        secondLevelCurxCenter = np.zeros((X.shape[0], X.shape[1] + self._lowerLayerShape[0] - self._part_shape[0], X.shape[2] + self._lowerLayerShape[1] - self._part_shape[1]))

        extractedFeature = np.empty((X.shape[0],X.shape[1] + self._lowerLayerShape[0] - self._part_shape[0],X.shape[2] + self._lowerLayerShape[1] - self._part_shape[1]))
        totalRange = X.shape[1]
        frame = (self._part_shape[0] - self._lowerLayerShape[0]) / 2
        frame = int(frame)

        for m in range(totalRange)[frame:totalRange-frame]:
            for n in range(totalRange)[frame:totalRange-frame]:
                secondLevelCurx[:,m-frame,n-frame] = index_map_pooling(X[:,m-frame:m+frame+1,n-frame:n+frame+1],self._num_lower_parts, (2 * frame + 1, 2 * frame + 1), (2 * frame + 1, 2 * frame + 1))
                secondLevelCurxCenter[:,m-frame,n-frame] = X[:,m,n]

        for i in range(X.shape[0]):
            patch = X[i]
            for m in range(patch.shape[0] + self._lowerLayerShape[0]- self._part_shape[0]):
                for n in range(patch.shape[1] + self._lowerLayerShape[1] - self._part_shape[1]):
                    if(secondLevelCurxCenter[i,m,n]!=-1):
                        firstLevelPartIndex = secondLevelCurxCenter[i,m,n]
                        firstLevelPartIndex = int(firstLevelPartIndex)
                        firstLevelPartIndex = (firstLevelPartIndex - firstLevelPartIndex % self._rotation)//self._rotation
                        theta = self._models[firstLevelPartIndex]
                        XX = secondLevelCurx[i, m, n][np.newaxis]
                        llh = XX * np.log(theta) + (1 - XX) * np.log(1 - theta)
                        bb = np.apply_over_axes(np.sum, llh, [-3, -2, -1])[...,0,0,0]
                        maxIndex = np.argmax(bb,axis = 0)
                        extensionPart = maxIndex // self._rotation
                        rotatedAngle = maxIndex % self._rotation
                        extractedFeature[i,m,n] = int( firstLevelPartIndex * self._num_components * self._rotation + extensionPart * self._rotation + rotatedAngle)
                    else:
                        extractedFeature[i,m,n] = -1
        extractedFeature = np.array(extractedFeature[:,:,:,np.newaxis],dtype = np.int64)
        return (extractedFeature,self._num_parts, self._rotation)
            for a in range(2):
                for b in range(2):
                    for m in range(totalRange)[a * (totalRange - subRegionSize) + frame: a * (totalRange - subRegionSize)+ subRegionSize - frame]:
                        for n in range(totalRange)[a * (totalRange - subRegionSize) + frame: a * (totalRange - subRegionSize) + subRegionSize - frame]:
                            if(X[i,m,n]!=-1):
                                partsGrid = self.partsPool(X[i,m-frame:m+frame+1, n-frame:n+frame+1], self._num_lower_parts)
                                partsRegion[a * 2 + b, X[i,m,n]].append(partsGrid)
        return partsRegion





        for m in range(totalRange)[frame:totalRange-frame]:
            for n in range(totalRange)[frame:totalRange-frame]:
                secondLevelCurx[:,m-frame,n-frame] = index_map_pooling(X[:,m-frame:m+frame+1,n-frame:n+frame+1],self._num_lower_parts, (2 * frame + 1, 2 * frame + 1), (2 * frame + 1, 2 * frame + 1))
                secondLevelCurxCenter[:,m-frame,n-frame] = X[:,m,n]






        for a in range(2):
            for b in range(2):
                for m in range(totalRange)[a * (totalRange - subRegionSize) + frame: a * (totalRange - subRegionSize) + subRegionSize - frame]:
                    for n in range(totalRange)[b * (totalRange - subRegionSize) + frame: b * (totalRange - subRegionSize) + subRegionSize - frame]:
                        secondLevelindex_map_pooling(X[:,m-frame:m+frame+1,n-frame:n+frame+1],self._num_lower_parts, (2 * frame + 1, 2 * frame + 1), (2 * frame + 1, 2 * frame + 1))


Пример #4
0
        sup_ims.append(ims0)
        sup_labels.append(d * np.ones(len(ims0),dtype = np.int64))
    sup_ims = np.concatenate(sup_ims, axis = 0)
    sup_labels = np.concatenate(sup_labels,axis = 0)
    

    curX = extract(sup_ims,allLayer[0:2])[0]
    #print(curX.shape)
    curX = curX.reshape(curX.shape[0:3])
    secondLevelCurx = np.zeros((10 * classificationTrainingNum,29 - secondLayerShape,29 - secondLayerShape,1,1,numParts))
    secondLevelCurxCenter = np.zeros((10 * classificationTrainingNum,29- secondLayerShape,29 - secondLayerShape))
    #for i in range(10 * classificationTrainingNum):
    #    codeParts = curX[i]
    for m in range(totalRange)[frame:totalRange-frame]:
        for n in range(totalRange)[frame:totalRange-frame]:
            secondLevelCurx[:,m-frame,n-frame] = index_map_pooling(curX[:,m-frame:m+frame+1,n-frame:n+frame+1],numParts,(2 * frame + 1,2 * frame + 1),(2 * frame + 1,2 * frame + 1))
            secondLevelCurxCenter[:,m-frame,n-frame] = curX[:,m,n]

    thirdLevelCurx = np.zeros((10 * classificationTrainingNum, 29 - secondLayerShape,29 - secondLayerShape))
    for i in range(int(10 * classificationTrainingNum)):
        for m in range(29 - secondLayerShape):
            for n in range(29 - secondLayerShape):
                if(secondLevelCurxCenter[i,m,n]!=-1):
                    firstLevelPartIndex = secondLevelCurxCenter[i,m,n]
                    #print(firstLevelPartIndex)
                    firstLevelPartIndex = int(firstLevelPartIndex)
                    extractedFeaturePart = extract(np.array(secondLevelCurx[i,m,n][np.newaxis,:],dtype = np.uint8),allPartsLayer[firstLevelPartIndex])[0]
                    #print("secondLayerExtraction")
                    #print(extractedFeaturePart.shape)
                    thirdLevelCurx[i,m,n] = int(numSecondLayerParts * firstLevelPartIndex + extractedFeaturePart)
                    #print(numSecondLayerParts,firstLevelPartIndex,extractedFeaturePart,thirdLevelCurx[i,m,n])
Пример #5
0
    def extract(self, X):
        assert self._classificationLayers is not None, "Must be trained before calling extract"
        if len(X) == 2:
            X, num_parts = X
        else:
            X, num_parts, orientation = X

        assert X.ndim == 4, "Input X dimension is not correct"
        assert num_parts == self._num_lower_parts

        X = X.reshape((X.shape[0], X.shape[1], X.shape[2]))

        secondLevelCurx = np.zeros(
            (
                X.shape[0],
                X.shape[1] + self._lowerLayerShape[0] - self._part_shape[0],
                X.shape[2] + self._lowerLayerShape[1] - self._part_shape[1],
                1,
                1,
                self._num_lower_parts,
            )
        )
        secondLevelCurxCenter = np.zeros(
            (
                X.shape[0],
                X.shape[1] + self._lowerLayerShape[0] - self._part_shape[0],
                X.shape[2] + self._lowerLayerShape[1] - self._part_shape[1],
            )
        )

        extractedFeature = np.empty(
            (
                X.shape[0],
                X.shape[1] + self._lowerLayerShape[0] - self._part_shape[0],
                X.shape[2] + self._lowerLayerShape[1] - self._part_shape[1],
            )
        )
        totalRange = X.shape[1]
        frame = (self._part_shape[0] - self._lowerLayerShape[0]) / 2
        frame = int(frame)

        for m in range(totalRange)[frame : totalRange - frame]:
            for n in range(totalRange)[frame : totalRange - frame]:
                secondLevelCurx[:, m - frame, n - frame] = index_map_pooling(
                    X[:, m - frame : m + frame + 1, n - frame : n + frame + 1],
                    self._num_lower_parts,
                    (2 * frame + 1, 2 * frame + 1),
                    (2 * frame + 1, 2 * frame + 1),
                )
                secondLevelCurxCenter[:, m - frame, n - frame] = X[:, m, n]

        for i in range(X.shape[0]):
            patch = X[i]
            for m in range(patch.shape[0] + self._lowerLayerShape[0] - self._part_shape[0]):
                for n in range(patch.shape[1] + self._lowerLayerShape[1] - self._part_shape[1]):
                    if secondLevelCurxCenter[i, m, n] != -1:
                        firstLevelPartIndex = secondLevelCurxCenter[i, m, n]
                        firstLevelPartIndex = int(firstLevelPartIndex)
                        extractedFeaturePart = self.codeParts(
                            np.array(secondLevelCurx[i, m, n][np.newaxis, :], dtype=np.uint8),
                            self._classificationLayers[firstLevelPartIndex],
                        )[0]
                        extractedFeature[i, m, n] = int(
                            self._num_components * firstLevelPartIndex + extractedFeaturePart
                        )
                    else:
                        extractedFeature[i, m, n] = -1
        extractedFeature = np.array(extractedFeature[:, :, :, np.newaxis], dtype=np.int64)
        return (extractedFeature, self._num_parts)
Пример #6
0
        sup_ims.append(ims0)
        sup_labels.append(d * np.ones(len(ims0),dtype = np.int64))
    sup_ims = np.concatenate(sup_ims, axis = 0)
    sup_labels = np.concatenate(sup_labels,axis = 0)
    

    curX = extract(sup_ims,allLayer[0:2])[0]
    #print(curX.shape)
    curX = curX.reshape(curX.shape[0:3])
    secondLevelCurx = np.zeros((10 * classificationTrainingNum,17,17,1,1,numParts))
    secondLevelCurxCenter = np.zeros((10 * classificationTrainingNum,17,17))
    #for i in range(10 * classificationTrainingNum):
    #    codeParts = curX[i]
    for m in range(23)[3:20]:
        for n in range(23)[3:20]:
            secondLevelCurx[:,m-3,n-3] = index_map_pooling(curX[:,m-3:m+4,n-3:n+4],numParts,(7,7),(7,7))
            secondLevelCurxCenter[:,m-3,n-3] = curX[:,m,n]

    thirdLevelCurx = np.zeros((10 * classificationTrainingNum, 17,17))
    for i in range(int(10 * classificationTrainingNum)):
        for m in range(17):
            for n in range(17):
                if(secondLevelCurxCenter[i,m,n]!=-1):
                    firstLevelPartIndex = secondLevelCurxCenter[i,m,n]
                    #print(firstLevelPartIndex)
                    firstLevelPartIndex = int(firstLevelPartIndex)
                    extractedFeaturePart = extract(np.array(secondLevelCurx[i,m,n][np.newaxis,:],dtype = np.uint8),allPartsLayer[firstLevelPartIndex])[0]
                    #print("secondLayerExtraction")
                    #print(extractedFeaturePart.shape)
                    thirdLevelCurx[i,m,n] = int(numSecondLayerParts * firstLevelPartIndex + extractedFeaturePart)
                    #print(numSecondLayerParts,firstLevelPartIndex,extractedFeaturePart,thirdLevelCurx[i,m,n])