Пример #1
0
 def _extractParams(self):
     '''open caffe meta information'''
     info.resetCaffeProtoModulesvar()
     info.CaffeMetaInformation().updateCaffeMetaInformation()
     allSolver = info.CaffeMetaInformation().availableSolverTypes()
     for solver in allSolver:
         self._iterParams(allSolver[solver].parameters())
     allLayer = info.CaffeMetaInformation().availableLayerTypes()
     for layer in allLayer:
         self._iterParams(allLayer[layer].parameters())
 def createNewSession(self, transaction):
     """create a new session with SID and PID"""
     msg = transaction.asyncRead()
     if "pid" in msg and "sid" in msg and "layers" in msg:
         layers = msg["layers"]
         try:
             for layer in layers:
                 typename = info.CaffeMetaInformation().getLayerType(layer)
         except UnknownLayerTypeException as e:
             msg["status"] = False
             msg["error"] = [e._msg]
             logging.error("Could not create session. Unknown layer")
             transaction.send(msg)
             return
         dirp = self._createDirName(msg["sid"])
         uid = self._createSession(msg["pid"], dirp)
         self.findSessionBySessionUid(uid).setInitialSid(msg["sid"])
         msg["status"] = True
         msg["uid"] = uid
         logging.info("Session created with UID '%s'", uid)
     else:
         msg["status"] = False
         msg["error"] = ["CreateNewSession: No PID or SID provided."]
         logging.error("Could not create session. No PID or SID provided.")
     transaction.send(msg)
Пример #3
0
    def loadRemoteSession(self, remote, uid):
        sid = self.getNextSessionId()
        session = ClientSession(self, remote, uid, sid)

        availableTypes = info.CaffeMetaInformation().availableLayerTypes()
        unknownLayerTypes = []
        for layerID in session.state_dictionary["network"]["layers"]:
            type = session.state_dictionary["network"]["layers"][layerID][
                "parameters"]["type"]
            if not type in availableTypes and not type in unknownLayerTypes:
                unknownLayerTypes.append(type)

        if len(unknownLayerTypes) > 0:
            msg = "Cannot load session. The selected session contains layers unknown to the current caffe-version.\n\nUnknown layers:"
            for type in unknownLayerTypes:
                msg += "\n" + type
            msgBox = QMessageBox(QMessageBox.Warning, "Warning", msg)
            msgBox.addButton("Ok", QMessageBox.NoRole)
            msgBox.exec_()
            session._disconnect()
            return None

        self.__sessions[sid] = session
        self.newSession.emit(sid)
        return sid
Пример #4
0
    def __parseSettings(self, settings):
        if settings:
            if "SessionState" in settings:
                self.state = settings["SessionState"]
                self.__previousState = self.state

            if "Iteration" in settings:
                self.iteration = settings["Iteration"]

            if "solver" in settings:
                if "max_iter" in settings["solver"]:
                    self.max_iter = settings["solver"]["max_iter"]
            elif "MaxIter" in settings:
                self.max_iter = settings["MaxIter"]

            if "LastSnapshot" in settings:
                self.last_solverstate = settings["LastSnapshot"]

            if "PretrainedWeights" in settings:
                self.setPretrainedWeights(settings["PretrainedWeights"])

            if "NetworkState" in settings:
                self.state_dictionary = settings["NetworkState"]
                layers = self.state_dictionary["network"]["layers"]
                for id in layers:
                    if "parameters" in layers[id]:
                        if "type" in layers[id]["parameters"]:
                            typename = layers[id]["parameters"]["type"]
                            layers[id]["type"] = info.CaffeMetaInformation(
                            ).getLayerType(typename)
                            # layers[id]["type"] = info.CaffeMetaInformation().availableLayerTypes()[typename]
                solver = self.state_dictionary["solver"]
                if solver:
                    if "snapshot_prefix" in solver:
                        self.snapshot_prefix = solver["snapshot_prefix"]
Пример #5
0
 def buildRootInfo(self, name=""):
     """ Builds the information about "network" in the state dictionary """
     prot = info.CaffeMetaInformation().availableNetParameters().copy()
     del prot["layers"]
     del prot["layer"]
     prot["layers"] = "LayerDict"
     prot["layerOrder"] = "LayerOrder"
     return self.buildInfo(name,prot,[])
Пример #6
0
    def _addSoftmax(self):
        """Add a softmax layer to the very end of the net, but only if a SoftmaxWithLoss layer was used before."""

        # check whether the net used to contain a SoftmaxWithLoss layer
        softmaxWithLossWasUsed = False
        for id, layer in self._originalNetworkDictionary["layers"].iteritems():
            if layer["type"].name() == "SoftmaxWithLoss":
                softmaxWithLossWasUsed = True
                break

        if softmaxWithLossWasUsed:
            # ensure that the remaining deployment net has at least one layer
            if len(self._deployedNetworkDictionary["layers"]) > 0:

                softmaxLayerType = info.CaffeMetaInformation(
                ).availableLayerTypes()["Softmax"]

                # do not add another softmax, if the current deployment network already contains one
                softmaxAlreadyIncluded = False
                for id, layer in self._deployedNetworkDictionary[
                        "layers"].iteritems():
                    if layer["type"].name() == softmaxLayerType.name():
                        softmaxAlreadyIncluded = True
                        break

                if not softmaxAlreadyIncluded:
                    # get the very last layer
                    lastLayerId = self._deployedNetworkDictionary[
                        "layerOrder"][-1]
                    lastLayer = self._deployedNetworkDictionary["layers"][
                        lastLayerId]

                    # ensure that the determined last layer does have a top blob
                    if "top" in lastLayer["parameters"] and len(
                            lastLayer["parameters"]["top"]) > 0:

                        # create new softmax layer with default values and add it to the deployment net
                        name = "softmax"
                        position = len(
                            self._deployedNetworkDictionary["layers"])
                        softmaxLayer, softmaxLayerId = self._dHelper.addLayer(
                            softmaxLayerType, name, position)

                        # connect the softmax layer with the existing network
                        softmaxLayer["parameters"]["bottom"] = [
                            lastLayer["parameters"]["top"][0]
                        ]

                        # name the output
                        softmaxLayer["parameters"]["top"] = ["probabilities"]
                    else:
                        Log.log(
                            "Could not add Softmax layer as the very last layer of the deployment net does not have any "
                            "top blobs.", self._logId)
            else:
                Log.log(
                    "Could not add Softmax layer as the remaining deployment net does not have any layers.",
                    self._logId)
Пример #7
0
def _load_layers(layerlist):
    """ Build the dictionary of all layers in layerlist. The dictionary has the form loadNet needs.
    """
    allLayers = info.CaffeMetaInformation().availableLayerTypes()
    order = []
    res = {}
    dicLayerTypeCounter = {}
    for layer in allLayers:
        dicLayerTypeCounter[layer] = 1

    for layer in layerlist:
        typename = layer.type
        layerinfo = info.CaffeMetaInformation().getLayerType(typename)
        id = str(uuid.uuid4())
        res[id] = {
            "type": layerinfo,
            "parameters": _extract_param(layer, layerinfo.parameters())
        }
        order.append(id)

    for id in order:
        if "name" not in res[id]["parameters"]:
            typeName = res[id]["parameters"]["type"]
            newName = typeName + " #" + str(dicLayerTypeCounter[typeName])
            changed = True
            while changed == True:
                newName = typeName + " #" + str(dicLayerTypeCounter[typeName])
                changed = False
                for id2 in order:
                    if "name" in res[id2]["parameters"] and res[id2][
                            "parameters"]["name"] == newName:
                        dicLayerTypeCounter[typeName] += 1
                        changed = True
            res[id]["parameters"]["name"] = newName

    return res, order
Пример #8
0
def isNetDictValid(netdic):
    """ This function returns a tuple (valid, msg). Value is true iff netdic has a valid format.
    If the format is not valid, msg contains a text which describes the problem.
    """
    if not {"layers", "layerOrder"}.issubset(netdic.keys()):
        missing = {"layers", "layerOrder"}.difference(netdic.keys())
        return False, "Needed parameters {} not in dictionary".format(
            list(missing))
    layers = netdic["layers"]
    order = netdic["layerOrder"]

    #Check if netparameters are valid (except layers)
    netparam = info.CaffeMetaInformation().availableNetParameters().copy()
    del netparam["layers"]
    tmp = netdic.copy()
    del tmp["layers"]
    del tmp["layerOrder"]
    if not _areParameterValid(netparam, tmp):
        return False, "Invalid net paremters"

    # Check for correct types of this special values
    if type(order) != list or type(layers) != dict:
        return False, "layerOrder or layers are invalid types"
    # Test if all ids are in the layerOrder list and vise versa
    if set(layers.keys()) != set(order):
        missingOrder = set(layers.keys()).difference(order)
        missingLayer = set(order).difference(layers.keys())
        return False, "layerOrder and layers does not have same ids - missing ids in layers: {} ; missing ids in order: {}"\
                      .format(missingLayer, missingOrder)
    # Check every sublayer
    for id in order:
        if not {"parameters", "type"}.issubset(layers[id].keys()):
            return False, "parameters or type are not in layers subdictionary"
        params = layers[id]["parameters"]
        if params is None:
            return False, "parameters is None, should be at least {'name': '..', 'type': '...'}"
        layer = layers[id]["type"]
        if not isinstance(layer, info.LayerType):
            return False, "layers is {}, expected instance of protoinfo.LayerType".format(
                layer)
        validparam, msg = _areParameterValid(layer.parameters(), params)
        if not validparam:
            return False, "Invalid parameters for {}: {}".format(id, msg)

    return True, ""
Пример #9
0
    def _insertInputLayers(self):
        """Insert new input layers with fixed dimensions.

        Note that, the number of newly-added input layer might be lower than the number of previously-removed data
        layers. On the one hand, we will add only one new input layer for each unique data blob name, while multiple
        data layers might have used the same data blob name. On the other hand, input layers will only be added, if
        at least one other layer is using the provided data.
        """
        inputLayerType = info.CaffeMetaInformation().availableLayerTypes(
        )["Input"]
        inputLayerNr = 1
        for dataBlobName in self._dataBlobNames:

            # create a new input layer with default values and add it to the deployment net
            name = self._getNewInputLayerName(inputLayerNr)
            inputLayer, inputLayerId = self._dHelper.addLayer(
                inputLayerType, name, inputLayerNr - 1)

            # modify the layer template
            inputLayer["parameters"]["top"] = [dataBlobName]

            # set input_param.shape with batch size 1 and the dimensions of the first data element
            inputLayer["parameters"]["input_param"] = dict()
            inputLayer["parameters"]["input_param"]["shape"] = []
            inputLayer["parameters"]["input_param"]["shape"].append(dict())
            inputLayer["parameters"]["input_param"]["shape"][0][
                "dim"] = self._inputBlobShapes[dataBlobName]
            inputLayer["parameters"]["input_param"]["shape"][0]["dim"].insert(
                0, 1)

            # prepare next input layer
            inputLayerNr += 1

        # check whether there is any shape that could not be determined automatically and needs to be set manually
        inputShapeWarning = False
        for inputShape in self._inputBlobShapes:
            if len(inputShape) < 1:
                inputShapeWarning = True
                break
        if inputShapeWarning:
            Log.log(
                "At least one input shape could not be determined automatically. Please open the created prototxt "
                "and manually fix all input shapes which include only the batch size (1).",
                self._logId)
    def __init__(self, mainWindow, title):

        # Set title and initialize the dock widget
        DockElement.__init__(self, mainWindow, title)
        self.name = title
        widget = QtWidgets.QWidget()
        self.layout = QtWidgets.QVBoxLayout()

        # Add the text field and the layer list to the widget
        self.addDescription()
        self.addTextfieldForSearch()
        self.addListOfAllLayers()

        self.allLayersDict = info.CaffeMetaInformation().availableLayerTypes()
        self.allLayers = self.dictionaryToList(self.allLayersDict)
        self.setAllLayersNewLayerList(self.allLayers)

        widget.setLayout(self.layout)
        self.setWidget(widget)
Пример #11
0
def _bareLayer(layertype, name):
    """ Creates a dictionary of the given layertype with the given name
        initialized with default values if required.
    """
    from backend.caffe.path_loader import PathLoader
    proto = PathLoader().importProto()
    res = {"type": layertype}
    layerParamInst = proto.LayerParameter()
    res["parameters"] = _extract_param(
        layerParamInst,
        info.CaffeMetaInformation().commonParameters())
    res["parameters"]["name"] = unicode(name)
    layerparamkeys = layertype.layerParamKeys()
    for layerparamkey in layerparamkeys:
        layerTypeParam = layertype.parameters()[layerparamkey]
        layerTypeParamInst = layerTypeParam.protoclass()()
        res["parameters"][layerparamkey] = _extract_param(
            layerTypeParamInst, layerTypeParam.parameter())
    res["parameters"]["type"] = unicode(layertype.name())
    return res
 def updateListOfAllLayers(self):
     info.CaffeMetaInformation().updateCaffeMetaInformation()
     self.allLayersDict = info.CaffeMetaInformation().availableLayerTypes()
     self.allLayers = self.dictionaryToList(self.allLayersDict)
     self.setAllLayersNewLayerList(self.allLayers)
     self.updateAllLayersWindowNewList(self.allLayers)
Пример #13
0
    def loadProject(self, set_settings=False):
        """ Loads the settings of the project. Returns a state-dictionary for
            the NetworkManager.
            State looks like: {
                "projectid": "xyz...",
                "caffeVersion": {...},
                "inputdb": { .. },
                "network": { .. },
                "solver": {...},
                "selection": [],
                "position": {}
            }
        """
        filename = self.projectConfigFileName()
        projectData = None
        if not os.path.exists(filename):
            self.__loadDefaultSettings()
            self.projectId = str(uuid.uuid4())
            sessionID = self.createSession(state_dictionary=None)
            self.setActiveSID(sessionID)
        else:
            with open(filename, "r") as file:
                res = json.load(file)
            if set_settings and ("environment" in res):
                self.settings = res["environment"]

            if "inputdb" in res:
                self.__inputManagerState = res["inputdb"]

            if "caffeVersion" in res:
                self.caffeVersion = res["caffeVersion"]
                caffeVersions.setDefaultVersion(self.caffeVersion)

            if "projectdata" in res:  # this is for backward compatibility, only
                # Fill missing type-instances
                allLayers = info.CaffeMetaInformation().availableLayerTypes()
                layers = res["projectdata"]["network"]["layers"]
                for id in layers:
                    typename = layers[id]["parameters"]["type"]
                    layers[id]["type"] = allLayers[typename]

                projectData = res["projectdata"]

            if "transform" in res:
                self.__transform = res["transform"]

            if "projectid" in res:
                self.projectId = res["projectid"]
            else:
                self.projectId = str(uuid.uuid4())  # backwards compatibility

            self.loadSessions()

            if len(self.__sessions) == 0:
                Log.error(
                    "Could not find a valid Session, creating new empty Session. Maybe the project "
                    "contains only remote Sessions?", self.getCallerId())
                sessionID = self.createSession()

            #check which session was last active, if this is not available, take the last Session in SID order, if no
            # valid sessions are available, create a new empty session and set this active
            if "activeSession" in res:
                self.setActiveSID(res["activeSession"])
            elif len(self.getValidSIDs()) > 0:
                self.setActiveSID(self.getValidSIDs()[-1])
            else:  # this branch should be reached only if an old projekt format is opened, where no sessions are stored
                sessionID = self.createSession(state_dictionary=projectData)
                self.setActiveSID(sessionID)
                if projectData:
                    self.setActiveSessionStateDict(projectData)

            if self.getActiveSession().hasStateDict():
                projectData = self.getActiveSession().state_dictionary
            else:
                if projectData:
                    # this branch should be reached only if an old projekt format is opened,
                    # where sessions do not have their own state dictionary
                    self.setActiveSessionStateDict(projectData)

        return projectData, self.__inputManagerState, self.__transform
Пример #14
0
 def enumOptions(self):
     if self.uri == ["type"]:
         return info.CaffeMetaInformation().availableSolverTypes()
     return super(PropertyInfo, self).enumOptions()