Beispiel #1
0
class IFaceRecognize():
    def __init__(self,model_dir,gpu_id):
        model_dir += '/' if not model_dir.endswith('/') else model_dir

        assert(os.path.exists(model_dir+'facerecognize'))
        prototxt = model_dir+"facerecognize/senet50_ft.prototxt"
        weightfile = model_dir+'facerecognize/senet50_ft.caffemodel'

        self.__gpu_id = gpu_id
        self.__classifier = ObjTypeClassifier(prototxt,weightfile,None,gpu_id,[91.4953,103.8827,131.0912])

    def extractFeature(self,im):
        feature  = self.__classifier.extractFeature(im,"classifier")
        im_1 = np.flip(im,axis=1)
        feature_1  = self.__classifier.extractFeature(im_1,"classifier")

        result = np.hstack([feature,feature_1])
        return result

    def recognize(self,feature,im):
        feature_1 = self.extractFeature(im)

        sum_0 = np.dot(feature,feature)
        sum_1 = np.dot(feature_1,feature_1)

        result = np.dot(feature,feature_1)/(np.sqrt(sum_0)*np.sqrt(sum_1))

        return result
Beispiel #2
0
    def __init__(self,model_dir,gpu_id):
        model_dir += '/' if not model_dir.endswith('/') else model_dir

        assert(os.path.exists(model_dir+'facerecognize'))
        prototxt = model_dir+"facerecognize/senet50_ft.prototxt"
        weightfile = model_dir+'facerecognize/senet50_ft.caffemodel'

        self.__gpu_id = gpu_id
        self.__classifier = ObjTypeClassifier(prototxt,weightfile,None,gpu_id,[91.4953,103.8827,131.0912])
    def __init__(self, model_dir, gpu_id=0):
        deploy_file = os.path.join(model_dir,
                                   "patentRetrieval/deploy.prototxt")
        assert os.path.exists(deploy_file)

        weights_file = os.path.join(model_dir,
                                    "patentRetrieval/final.caffemodel")
        assert os.path.exists(weights_file)

        self.__extracter = ObjTypeClassifier(deploy_file, weights_file, gpu_id)
        self.__feature_len = 1024
Beispiel #4
0
class IVehicleColorClassifier():
    def __init__(self, modelDir, gpu_id):
        modelDir += '/' if not modelDir.endswith('/') else modelDir

        assert (os.path.exists(modelDir + 'vehicleTypeColor'))
        prototxt = modelDir + "vehicleTypeColor/vehicleTypeColorD10deploy.prototxt"
        assert (prototxt)
        weightfile = modelDir + 'vehicleTypeColor/vehicleTypeColorD10model.dat'
        assert (weightfile)
        meanfile = modelDir + 'vehicleTypeColor/vehicleTypeColorD10mean.dat'
        assert (meanfile)
        labelfile = modelDir + 'vehicleTypeColor/vehicleTypeColorID10.txt'
        assert (labelfile)

        self.__gpu_id = gpu_id
        self.__classifier = ObjTypeClassifier(prototxt, weightfile, meanfile,
                                              gpu_id)
        self.__labelMap = [
            line.strip() for line in open(labelfile).readlines()
        ]

    def classify(self, im):
        result = {}

        info = self.__classifier.classify(im, 'fc7')
        result['category'] = self.__labelMap[info[0]]
        result['score'] = info[1]

        return result
Beispiel #5
0
class IPatentTypeClassifier():
    def __init__(self, modelDir, gpu_id):
        modelDir += '/' if not modelDir.endswith('/') else modelDir

        assert (os.path.exists(modelDir + 'patentClass'))
        prototxt = modelDir + "patentClass/patentimageclass.prototxt"
        assert (prototxt)
        weightfile = modelDir + 'patentClass/bvlc_googlenet_iter_14000.caffemodel'
        assert (weightfile)
        meanfile = modelDir + 'patentClass/patentimageclassmean.binaryproto'
        assert (meanfile)
        labelfile = modelDir + 'patentClass/patentAllTypeID4.txt'
        assert (labelfile)

        self.__gpu_id = gpu_id
        self.__classifier = ObjTypeClassifier(prototxt, weightfile, meanfile,
                                              gpu_id)
        self.__labelMap = [
            line.strip() for line in open(labelfile).readlines()
        ]

    def classify(self, im):
        result = {}

        info = self.__classifier.classify(im, 'loss2/fc')
        result['category'] = self.__labelMap[info[0]]
        result['score'] = info[1]

        return result
    def __init__(self,modelDir,gpu_id):
        modelDir += '/' if not modelDir.endswith('/') else modelDir

        assert(os.path.exists(modelDir+'vehicleTypeTail'))
        prototxt = modelDir+"vehicleTypeTail/vehicleTypeTailD0deploy.prototxt"
        assert(prototxt)
        weightfile = modelDir+'vehicleTypeTail/vehicleTypeTailD0model.dat'
        assert(weightfile)
        meanfile = modelDir+'vehicleTypeTail/vehicleTypeTailD0mean.dat'
        assert(meanfile)
        labelfile = modelDir+'vehicleTypeTail/vehicleTypeTailID4.txt'
        assert(labelfile)

        self.__gpu_id = gpu_id
        self.__classifier = ObjTypeClassifier(prototxt,weightfile,meanfile,gpu_id)
        self.__labelMap = [line.strip() for line in open(labelfile).readlines()]
Beispiel #7
0
    def __init__(self, modelDir, gpu_id):
        modelDir += '/' if not modelDir.endswith('/') else modelDir

        assert (os.path.exists(modelDir + 'patentClass'))
        prototxt = modelDir + "patentClass/patentimageclass.prototxt"
        assert (prototxt)
        weightfile = modelDir + 'patentClass/bvlc_googlenet_iter_14000.caffemodel'
        assert (weightfile)
        meanfile = modelDir + 'patentClass/patentimageclassmean.binaryproto'
        assert (meanfile)
        labelfile = modelDir + 'patentClass/patentAllTypeID4.txt'
        assert (labelfile)

        self.__gpu_id = gpu_id
        self.__classifier = ObjTypeClassifier(prototxt, weightfile, meanfile,
                                              gpu_id)
        self.__labelMap = [
            line.strip() for line in open(labelfile).readlines()
        ]
Beispiel #8
0
    def __init__(self, model_dir, gpu_id=0):
        deploy_file = os.path.join(model_dir,
                                   "faceretrieval/resnet50_ft.prototxt")
        assert os.path.exists(deploy_file)

        weights_file = os.path.join(model_dir,
                                    "faceretrieval/resnet50_ft.caffemodel")
        assert os.path.exists(weights_file)

        xml_file = os.path.join(model_dir, "faceretrieval/config95-V.xml")
        assert os.path.exists(xml_file)

        fs = cv2.FileStorage(xml_file, cv2.FILE_STORAGE_READ)
        self.__pca_mean = fs.getNode("mean").mat()
        self.__pca_eigen_vector = fs.getNode("eigen_vector").mat()
        self.__feature_len = self.__pca_eigen_vector.shape[0]

        mean_value = [91.4953, 103.8827, 131.0912]
        self.__extracter = ObjTypeClassifier(deploy_file,
                                             weights_file,
                                             meanvalue=mean_value)
class PatentRetrieval:
    def __init__(self, model_dir, gpu_id=0):
        deploy_file = os.path.join(model_dir,
                                   "patentRetrieval/deploy.prototxt")
        assert os.path.exists(deploy_file)

        weights_file = os.path.join(model_dir,
                                    "patentRetrieval/final.caffemodel")
        assert os.path.exists(weights_file)

        self.__extracter = ObjTypeClassifier(deploy_file, weights_file, gpu_id)
        self.__feature_len = 1024

    def create_retrieval_model(self, retrieval_model_path):
        assert (os.path.exists(retrieval_model_path))

        retrieval_model = faiss.read_index(retrieval_model_path)
        return retrieval_model

    def extractFeature(self, im):
        try:
            feature = self.__extracter.extractFeature(im, 'loss2/fc')
        except:
            return []

        return feature

    def buildRetrievalDatabase(self, features, retrieval_model_path):
        if os.path.exists(retrieval_model_path):
            retrieval_model = faiss.read_index(retrieval_model_path)
        else:
            retrieval_model = faiss.IndexFlatL2(self.__feature_len)

        features = np.array(features)
        retrieval_model.add(features)
        faiss.write_index(retrieval_model, retrieval_model_path)

        return retrieval_model

    def query(self, im, k, retrieval_model):
        feature = self.extractFeature(im)

        feature = np.array([feature])
        res = retrieval_model.search(feature, k)
        return res[1][0], res[0][0]
Beispiel #10
0
class FaceRetrieval:
    def __init__(self, model_dir, gpu_id=0):
        deploy_file = os.path.join(model_dir,
                                   "faceretrieval/resnet50_ft.prototxt")
        assert os.path.exists(deploy_file)

        weights_file = os.path.join(model_dir,
                                    "faceretrieval/resnet50_ft.caffemodel")
        assert os.path.exists(weights_file)

        xml_file = os.path.join(model_dir, "faceretrieval/config95-V.xml")
        assert os.path.exists(xml_file)

        fs = cv2.FileStorage(xml_file, cv2.FILE_STORAGE_READ)
        self.__pca_mean = fs.getNode("mean").mat()
        self.__pca_eigen_vector = fs.getNode("eigen_vector").mat()
        self.__feature_len = self.__pca_eigen_vector.shape[0]

        mean_value = [91.4953, 103.8827, 131.0912]
        self.__extracter = ObjTypeClassifier(deploy_file,
                                             weights_file,
                                             meanvalue=mean_value)

    def create_retrieval_model(self, retrieval_model_path):
        assert (os.path.exists(retrieval_model_path))
        retrieval_model = AnnoyIndex(self.__feature_len)
        retrieval_model.load(retrieval_model_path)

        return retrieval_model

    def extractFeature(self, im):
        try:
            feature = self.__extracter.extractFeature(im, "classifier")
            im_1 = np.flip(im, axis=1)
            feature_1 = self.__extracter.extractFeature(im_1, "classifier")

            result = np.hstack([feature, feature_1])
            result = result.reshape(1, -1)

            res = cv2.PCAProject(result, self.__pca_mean,
                                 self.__pca_eigen_vector)
            return res.reshape(-1).tolist()
        except:
            return []

    def buildRetrievalDatabase(self, features, retrieval_model_path):
        self.__database = AnnoyIndex(self.__feature_len)
        index = 0
        for feature in features:
            self.__database.add_item(index, feature)
            index += 1

        self.__database.build(2 * self.__feature_len)
        self.__database.save(retrieval_model_path)

    def query(self, im, k, retrieval_model):
        feature = self.extractFeature(im)
        res = retrieval_model.get_nns_by_vector(feature,
                                                k,
                                                include_distances=False)
        return res