plt.semilogy(val_errs)
    plt.show(block=False)
    fig.savefig('./eval/' + eval_prefix + '/' + eval_prefix + '_errs.png')

    # save results
    poseNet.save("./eval/{}/net_{}.pkl".format(eval_prefix, eval_prefix))
    # poseNet.load("./eval/{}/net_{}.pkl".format(eval_prefix,eval_prefix))

    # add prior to network
    cfg = HiddenLayerParams(inputDim=(batchSize, train_gt3D_embed.shape[1]),
                            outputDim=(batchSize,
                                       numpy.prod(train_gt3D.shape[1:])),
                            activation=None)
    pcalayer = HiddenLayer(rng,
                           poseNet.layers[-1].output,
                           cfg,
                           copyLayer=None,
                           layerNum=len(poseNet.layers))
    pcalayer.W.set_value(pca.components_)
    pcalayer.b.set_value(pca.mean_)
    poseNet.layers.append(pcalayer)
    poseNet.output = pcalayer.output
    poseNet.cfgParams.numJoints = train_gt3D.shape[1]
    poseNet.cfgParams.nDims = train_gt3D.shape[2]
    poseNet.cfgParams.outputDim = pcalayer.cfgParams.outputDim
    poseNet.save("./eval/{}/network_prior.pkl".format(eval_prefix))

    ###################################################################
    #  test
    print("Testing ...")
    gt3D = [j.gt3Dorig for j in testSeqs[0].data]
Пример #2
0
    def __init__(self, rng, inputVar=None, cfgParams=None):
        """
        @see https://github.com/KaimingHe/resnet-1k-layers/blob/master/resnet-pre-act.lua
        :type cfgParams: DescriptorNetParams
        """
        import theano.tensor as T

        self._params_filter = []
        self._weights_filter = []

        if cfgParams is None:
            raise Exception("Cannot create a Net without config parameters (ie. cfgParams==None)")

        if inputVar is None:
            inputVar = T.tensor4('x')  # input variable
        elif isinstance(inputVar, str):
            raise NotImplementedError()

        self.inputVar = inputVar
        self.cfgParams = cfgParams

        # create network
        self.layers = []

        batchSize = cfgParams.batch_size

        # create structure
        if cfgParams.type == 0:
            # Try ResNet similar configuration
            depth = 47
            assert (depth - 2) % 9 == 0, 'depth should be 9n+2 (e.g., 164 or 1001)'
            n = (depth - 2) / 9

            nStages = [32, 64, 128, 256, 256]

            self.layers.append(ConvPoolLayer(rng, self.inputVar,
                                             ConvPoolLayerParams(inputDim=self.cfgParams.inputDim, nFilters=nStages[0],
                                                                 filterDim=(5, 5), stride=(1, 1),
                                                                 poolsize=(2, 2), border_mode='same', activation=None,
                                                                 init_method='He'),
                                             layerNum=len(self.layers)))  # one conv at the beginning
            rout = self.add_res_layers(rng, self.layers[-1].output, self.layers[-1].cfgParams.outputDim, nStages[1], n, 2)  # Stage 1
            rout = self.add_res_layers(rng, rout, self.layers[-1].cfgParams.outputDim, nStages[2], n, 2)  # Stage 2
            rout = self.add_res_layers(rng, rout, self.layers[-1].cfgParams.outputDim, nStages[3], n, 2)  # Stage 3
            rout = self.add_res_layers(rng, rout, self.layers[-1].cfgParams.outputDim, nStages[4], n, 2)  # Stage 4
            self.layers.append(BatchNormLayer(rng, rout, BatchNormLayerParams(inputDim=self.layers[-1].cfgParams.outputDim), layerNum=len(self.layers)))
            self.layers.append(NonlinearityLayer(rng, self.layers[-1].output, NonlinearityLayerParams(inputDim=self.layers[-1].cfgParams.outputDim, activation=ReLU), layerNum=len(self.layers)))
            # self.layers.append(GlobalAveragePoolLayer(rng, self.layers[-1].output, GlobalAveragePoolLayerParams(inputDim=self.layers[-1].cfgParams.outputDim), layerNum=len(self.layers)))
            self.layers.append(HiddenLayer(rng, self.layers[-1].output.flatten(2),
                                           HiddenLayerParams(# inputDim=self.layers[-1].cfgParams.outputDim,
                                                             inputDim=(self.layers[-1].cfgParams.outputDim[0], numpy.prod(self.layers[-1].cfgParams.outputDim[1:])),
                                                             outputDim=(batchSize, 1024), activation=ReLU),
                                           layerNum=len(self.layers)))
            self.layers.append(HiddenLayer(rng, self.layers[-1].output,
                                           HiddenLayerParams(inputDim=self.layers[-1].cfgParams.outputDim,
                                                             outputDim=(batchSize, 1024), activation=ReLU),
                                           layerNum=len(self.layers)))
            self.layers.append(HiddenLayer(rng, self.layers[-1].output,
                                           HiddenLayerParams(inputDim=self.layers[-1].cfgParams.outputDim,
                                                             outputDim=(batchSize, self.cfgParams.numJoints*self.cfgParams.nDims),
                                                             activation=None),
                                           layerNum=len(self.layers)))

            self.output = self.layers[-1].output
        elif cfgParams.type == 1:
            # Try ResNet similar configuration
            depth = 47
            assert (depth - 2) % 9 == 0, 'depth should be 9n+2 (e.g., 164 or 1001)'
            n = (depth - 2) / 9

            nStages = [32, 64, 128, 256, 256]

            self.layers.append(ConvPoolLayer(rng, self.inputVar,
                                             ConvPoolLayerParams(inputDim=self.cfgParams.inputDim, nFilters=nStages[0],
                                                                 filterDim=(5, 5), stride=(1, 1),
                                                                 poolsize=(2, 2), border_mode='same', activation=None,
                                                                 init_method='He'),
                                             layerNum=len(self.layers)))  # one conv at the beginning
            rout = self.add_res_layers(rng, self.layers[-1].output, self.layers[-1].cfgParams.outputDim, nStages[1], n, 2)  # Stage 1
            rout = self.add_res_layers(rng, rout, self.layers[-1].cfgParams.outputDim, nStages[2], n, 2)  # Stage 2
            rout = self.add_res_layers(rng, rout, self.layers[-1].cfgParams.outputDim, nStages[3], n, 2)  # Stage 3
            rout = self.add_res_layers(rng, rout, self.layers[-1].cfgParams.outputDim, nStages[4], n, 2)  # Stage 4
            self.layers.append(BatchNormLayer(rng, rout, BatchNormLayerParams(inputDim=self.layers[-1].cfgParams.outputDim), layerNum=len(self.layers)))
            self.layers.append(NonlinearityLayer(rng, self.layers[-1].output, NonlinearityLayerParams(inputDim=self.layers[-1].cfgParams.outputDim, activation=ReLU), layerNum=len(self.layers)))
            self.layers.append(HiddenLayer(rng, self.layers[-1].output.flatten(2),
                                           HiddenLayerParams(inputDim=(self.layers[-1].cfgParams.outputDim[0], numpy.prod(self.layers[-1].cfgParams.outputDim[1:])),
                                                             outputDim=(batchSize, 1024), activation=ReLU),
                                           layerNum=len(self.layers)))
            self.layers.append(HiddenLayer(rng, self.layers[-1].output,
                                           HiddenLayerParams(inputDim=self.layers[-1].cfgParams.outputDim,
                                                             outputDim=(batchSize, 1024), activation=ReLU),
                                           layerNum=len(self.layers)))
            self.layers.append(HiddenLayer(rng, self.layers[-1].output,
                                           HiddenLayerParams(inputDim=self.layers[-1].cfgParams.outputDim,
                                                             outputDim=(batchSize, 30), activation=None),
                                           layerNum=len(self.layers)))
            self.layers.append(HiddenLayer(rng, self.layers[-1].output,
                                           HiddenLayerParams(inputDim=self.layers[-1].cfgParams.outputDim,
                                                             outputDim=(batchSize, self.cfgParams.numJoints*self.cfgParams.nDims),
                                                             activation=None),
                                           layerNum=len(self.layers)))

            self.output = self.layers[-1].output
        elif cfgParams.type == 2:
            # Try ResNet similar configuration
            depth = 47
            assert (depth - 2) % 9 == 0, 'depth should be 9n+2 (e.g., 164 or 1001)'
            n = (depth - 2) / 9

            nStages = [32, 64, 128, 256, 256]

            self.layers.append(ConvPoolLayer(rng, self.inputVar,
                                             ConvPoolLayerParams(inputDim=self.cfgParams.inputDim, nFilters=nStages[0],
                                                                 filterDim=(5, 5), stride=(1, 1),
                                                                 poolsize=(2, 2), border_mode='same', activation=None,
                                                                 init_method='He'),
                                             layerNum=len(self.layers)))  # one conv at the beginning
            rout = self.add_res_layers(rng, self.layers[-1].output, self.layers[-1].cfgParams.outputDim, nStages[1], n, 2)  # Stage 1
            rout = self.add_res_layers(rng, rout, self.layers[-1].cfgParams.outputDim, nStages[2], n, 2)  # Stage 2
            rout = self.add_res_layers(rng, rout, self.layers[-1].cfgParams.outputDim, nStages[3], n, 2)  # Stage 3
            rout = self.add_res_layers(rng, rout, self.layers[-1].cfgParams.outputDim, nStages[4], n, 2)  # Stage 4
            self.layers.append(BatchNormLayer(rng, rout, BatchNormLayerParams(inputDim=self.layers[-1].cfgParams.outputDim), layerNum=len(self.layers)))
            self.layers.append(NonlinearityLayer(rng, self.layers[-1].output, NonlinearityLayerParams(inputDim=self.layers[-1].cfgParams.outputDim, activation=ReLU), layerNum=len(self.layers)))
            self.layers.append(HiddenLayer(rng, self.layers[-1].output.flatten(2),
                                           HiddenLayerParams(inputDim=(self.layers[-1].cfgParams.outputDim[0], numpy.prod(self.layers[-1].cfgParams.outputDim[1:])),
                                                             outputDim=(batchSize, 1024), activation=ReLU),
                                           layerNum=len(self.layers)))

            self.layers.append(DropoutLayer(rng, self.layers[-1].output,
                                            DropoutLayerParams(inputDim=self.layers[-1].cfgParams.outputDim,
                                                               outputDim=self.layers[-1].cfgParams.outputDim),
                                            layerNum=len(self.layers)))

            self.layers.append(HiddenLayer(rng, self.layers[-1].output,
                                           HiddenLayerParams(inputDim=self.layers[-1].cfgParams.outputDim,
                                                             outputDim=(batchSize, 1024), activation=ReLU),
                                           layerNum=len(self.layers)))

            self.layers.append(DropoutLayer(rng, self.layers[-1].output,
                                            DropoutLayerParams(inputDim=self.layers[-1].cfgParams.outputDim,
                                                               outputDim=self.layers[-1].cfgParams.outputDim),
                                            layerNum=len(self.layers)))

            self.layers.append(HiddenLayer(rng, self.layers[-1].output,
                                           HiddenLayerParams(inputDim=self.layers[-1].cfgParams.outputDim,
                                                             outputDim=(batchSize, self.cfgParams.numJoints*self.cfgParams.nDims),
                                                             activation=None),
                                           layerNum=len(self.layers)))

            self.output = self.layers[-1].output
        elif cfgParams.type == 3:
            # Try ResNet similar configuration
            depth = 47
            assert (depth - 2) % 9 == 0, 'depth should be 9n+2 (e.g., 164 or 1001)'
            n = (depth - 2) / 9

            nStages = [32, 64, 128, 128, 128]

            self.layers.append(ConvPoolLayer(rng, self.inputVar,
                                             ConvPoolLayerParams(inputDim=self.cfgParams.inputDim, nFilters=nStages[0],
                                                                 filterDim=(5, 5), stride=(1, 1),
                                                                 poolsize=(2, 2), border_mode='same', activation=None,
                                                                 init_method='He'),
                                             layerNum=len(self.layers)))  # one conv at the beginning
            rout = self.add_res_layers(rng, self.layers[-1].output, self.layers[-1].cfgParams.outputDim, nStages[1], n, 2)  # Stage 1
            rout = self.add_res_layers(rng, rout, self.layers[-1].cfgParams.outputDim, nStages[2], n, 2)  # Stage 2
            rout = self.add_res_layers(rng, rout, self.layers[-1].cfgParams.outputDim, nStages[3], n, 2)  # Stage 3
            rout = self.add_res_layers(rng, rout, self.layers[-1].cfgParams.outputDim, nStages[4], n, 2)  # Stage 4
            self.layers.append(BatchNormLayer(rng, rout, BatchNormLayerParams(inputDim=self.layers[-1].cfgParams.outputDim), layerNum=len(self.layers)))
            self.layers.append(NonlinearityLayer(rng, self.layers[-1].output, NonlinearityLayerParams(inputDim=self.layers[-1].cfgParams.outputDim, activation=ReLU), layerNum=len(self.layers)))
            self.layers.append(HiddenLayer(rng, self.layers[-1].output.flatten(2),
                                           HiddenLayerParams(inputDim=(self.layers[-1].cfgParams.outputDim[0], numpy.prod(self.layers[-1].cfgParams.outputDim[1:])),
                                                             outputDim=(batchSize, 1024), activation=ReLU),
                                           layerNum=len(self.layers)))

            self.layers.append(DropoutLayer(rng, self.layers[-1].output,
                                            DropoutLayerParams(inputDim=self.layers[-1].cfgParams.outputDim,
                                                               outputDim=self.layers[-1].cfgParams.outputDim),
                                            layerNum=len(self.layers)))

            self.layers.append(HiddenLayer(rng, self.layers[-1].output,
                                           HiddenLayerParams(inputDim=self.layers[-1].cfgParams.outputDim,
                                                             outputDim=(batchSize, 1024), activation=ReLU),
                                           layerNum=len(self.layers)))

            self.layers.append(DropoutLayer(rng, self.layers[-1].output,
                                            DropoutLayerParams(inputDim=self.layers[-1].cfgParams.outputDim,
                                                               outputDim=self.layers[-1].cfgParams.outputDim),
                                            layerNum=len(self.layers)))

            self.layers.append(HiddenLayer(rng, self.layers[-1].output,
                                           HiddenLayerParams(inputDim=self.layers[-1].cfgParams.outputDim,
                                                             outputDim=(batchSize, self.cfgParams.numJoints*self.cfgParams.nDims),
                                                             activation=None),
                                           layerNum=len(self.layers)))

            self.output = self.layers[-1].output
        elif cfgParams.type == 4:
            # Try ResNet similar configuration
            depth = 47
            assert (depth - 2) % 9 == 0, 'depth should be 9n+2 (e.g., 164 or 1001)'
            n = (depth - 2) / 9

            nStages = [32, 64, 128, 256, 256]

            self.layers.append(ConvPoolLayer(rng, self.inputVar,
                                             ConvPoolLayerParams(inputDim=self.cfgParams.inputDim, nFilters=nStages[0],
                                                                 filterDim=(5, 5), stride=(1, 1),
                                                                 poolsize=(2, 2), border_mode='same', activation=None,
                                                                 init_method='He'),
                                             layerNum=len(self.layers)))  # one conv at the beginning
            rout = self.add_res_layers(rng, self.layers[-1].output, self.layers[-1].cfgParams.outputDim, nStages[1], n, 2)  # Stage 1
            rout = self.add_res_layers(rng, rout, self.layers[-1].cfgParams.outputDim, nStages[2], n, 2)  # Stage 2
            rout = self.add_res_layers(rng, rout, self.layers[-1].cfgParams.outputDim, nStages[3], n, 2)  # Stage 3
            rout = self.add_res_layers(rng, rout, self.layers[-1].cfgParams.outputDim, nStages[4], n, 2)  # Stage 4
            self.layers.append(BatchNormLayer(rng, rout, BatchNormLayerParams(inputDim=self.layers[-1].cfgParams.outputDim), layerNum=len(self.layers)))
            self.layers.append(NonlinearityLayer(rng, self.layers[-1].output, NonlinearityLayerParams(inputDim=self.layers[-1].cfgParams.outputDim, activation=ReLU), layerNum=len(self.layers)))
            self.layers.append(HiddenLayer(rng, self.layers[-1].output.flatten(2),
                                           HiddenLayerParams(inputDim=(self.layers[-1].cfgParams.outputDim[0], numpy.prod(self.layers[-1].cfgParams.outputDim[1:])),
                                                             outputDim=(batchSize, 1024), activation=ReLU),
                                           layerNum=len(self.layers)))
            self.layers.append(DropoutLayer(rng, self.layers[-1].output,
                                            DropoutLayerParams(inputDim=self.layers[-1].cfgParams.outputDim,
                                                               outputDim=self.layers[-1].cfgParams.outputDim),
                                            layerNum=len(self.layers)))
            self.layers.append(HiddenLayer(rng, self.layers[-1].output,
                                           HiddenLayerParams(inputDim=self.layers[-1].cfgParams.outputDim,
                                                             outputDim=(batchSize, 1024), activation=ReLU),
                                           layerNum=len(self.layers)))
            self.layers.append(DropoutLayer(rng, self.layers[-1].output,
                                            DropoutLayerParams(inputDim=self.layers[-1].cfgParams.outputDim,
                                                               outputDim=self.layers[-1].cfgParams.outputDim),
                                            layerNum=len(self.layers)))
            self.layers.append(HiddenLayer(rng, self.layers[-1].output,
                                           HiddenLayerParams(inputDim=self.layers[-1].cfgParams.outputDim,
                                                             outputDim=(batchSize, 30), activation=None),
                                           layerNum=len(self.layers)))
            self.layers.append(HiddenLayer(rng, self.layers[-1].output,
                                           HiddenLayerParams(inputDim=self.layers[-1].cfgParams.outputDim,
                                                             outputDim=(batchSize, self.cfgParams.numJoints*self.cfgParams.nDims),
                                                             activation=None),
                                           layerNum=len(self.layers)))

            self.output = self.layers[-1].output
        else:
            raise NotImplementedError()

        self.load(self.cfgParams.loadFile)