Esempio n. 1
0
 def writeWeights(self, ii) :
     from dataset.debugger import saveTiledImage
     saveTiledImage(image=self._weights.get_value(borrow=True),
                    path=self.layerID + '_cae_filters_' + str(ii) + '.png',
                    imageShape=(self._kernelSize[2], self._kernelSize[3]),
                    spacing=1,
                    interleave=True)
Esempio n. 2
0
 def writeWeights(self, ii):
     from dataset.debugger import saveTiledImage
     saveTiledImage(image=self._weights.get_value(borrow=True),
                    path=self.layerID + '_cae_filters_' + str(ii) + '.png',
                    imageShape=(self._kernelSize[2], self._kernelSize[3]),
                    spacing=1,
                    interleave=True)
Esempio n. 3
0
    def writeWeights(self, ii, imageShape=None) :
        from dataset.debugger import saveTiledImage
        matSize = self._weights.get_value(borrow=True).shape

        # transpose the weight matrix to alighn the kernels contiguously
        saveTiledImage(
            image=self._weights.get_value(borrow=True).T,
            path=self.layerID + '_cae_filters_' + str(ii) + '.png',
            imageShape=(1, matSize[0]) if imageShape is None else imageShape,
            tileShape=(matSize[1], 1) if imageShape is None else None,
            spacing=0 if imageShape is None else 1,
            interleave=True)
Esempio n. 4
0
def sortDataset(nets, target, imagery, percReturned=100., debug=False) :
    '''Test the imagery for how close it is to the target data. This also sorts
       the results according to closeness, so we can create a tiled tip-sheet.
    '''
    from dataset.debugger import saveTiledImage
    import numpy as np
    import math

    # reload the target directory into the networks
    [net.loadFeatureMatrix(target) for net in nets]

    sims = []
    batchSize = imagery.shape[1]
    for ii, batch in enumerate(imagery) :
        # average the results found by multiple networks
        cos = np.zeros((batchSize,), dtype=np.float32)
        [net.closeness(batch, cos) for net in nets]
        sims.append(cos / float(len(nets)))

    # rank their closeness from high to low
    sims = [(ii // batchSize, ii % batchSize, sim) \
            for ii, sim in enumerate(np.concatenate(sims))]
    sims = sorted(sims, key=lambda x: x[-1], reverse=True)

    # reorder the imagery to match the ranking
    numImages = int((percReturned / 100.) * np.prod(imagery.shape[:2]))
    sortedImagery = np.zeros([numImages] + list(imagery.shape[-3:]),
                             dtype=imagery.dtype)
    sortedConfidence = np.zeros([numImages], dtype=np.float32)
    for counter, (ii, jj, sim) in enumerate(sims[:numImages]) :
        sortedImagery[counter][:] = imagery[ii][jj][:]
        sortedConfidence[counter] = sim

    # dump the ranked result as a series of batches
    if debug :
        newNumBatch = int(math.ceil(float(numImages) / batchSize))
        newFlatShape = [newNumBatch * batchSize] + list(imagery.shape[-3:])
        newBatchShape = [newNumBatch, batchSize] + list(imagery.shape[-3:])
        dimdiff = tuple([(0, a - b) for a, b in zip(newFlatShape,
                                                    sortedImagery.shape)])
        tmp = np.pad(sortedImagery, dimdiff, 
                     mode='constant', constant_values=0)
        tmp = np.reshape(tmp, newBatchShape)
        for ii in range(len(imagery)) :
            saveTiledImage(imagery[ii], str(ii) + '.tif',
                           imagery.shape[-2:])
        for ii in range(len(tmp)) :
            saveTiledImage(tmp[ii], str(ii) + '_sorted.tif',
                           imagery.shape[-2:])

    return sortedImagery, sortedConfidence
Esempio n. 5
0
def sortDataset(netList, imagery, percentile=.95, debug=False):
    '''Test the imagery for how close it is to the target data. This also sorts
       the results according to closeness, so we can create a tiled tip-sheet.
    '''
    from dataset.debugger import saveTiledImage
    import numpy as np
    import math
    sims = []
    batchSize = imagery.shape[1]
    for ii, batch in enumerate(imagery):
        # average the results found by multiple networks
        cos = np.zeros((batchSize, ), dtype=np.float32)
        [net.closeness(batch, cos) for net in nets]
        sims.append(cos / float(len(nets)))

    # rank their closeness from high to low
    sims = [(ii // batchSize, ii % batchSize, sim) \
            for ii, sim in enumerate(np.concatenate(sims))]
    sims = sorted(sims, key=lambda x: x[-1], reverse=True)

    # reorder the imagery to match the ranking
    numImages = int((1. - percentile) * np.prod(imagery.shape[:2]))
    sortedImagery = np.zeros([numImages] + list(imagery.shape[-3:]),
                             dtype=imagery.dtype)
    sortedConfidence = np.zeros([numImages], dtype=np.float32)
    for counter, (ii, jj, sim) in enumerate(sims[:numImages]):
        sortedImagery[counter][:] = imagery[ii][jj][:]
        sortedConfidence[counter] = sim

    # dump the ranked result as a series of batches
    if debug:
        newNumBatch = math.ceil(numImages / batchSize)
        newFlatShape = [newNumBatch * batchSize] + list(imagery.shape[-3:])
        newBatchShape = [newNumBatch, batchSize] + list(imagery.shape[-3:])
        dimdiff = tuple([(0, a - b)
                         for a, b in zip(newFlatShape, sortedImagery.shape)])
        tmp = np.pad(sortedImagery,
                     dimdiff,
                     mode='constant',
                     constant_values=0)
        tmp = np.reshape(tmp, newBatchShape)
        for ii in range(len(imagery)):
            saveTiledImage(imagery[ii], str(ii) + '.tif', imagery.shape[-2:])
        for ii in range(len(tmp)):
            saveTiledImage(tmp[ii],
                           str(ii) + '_sorted.tif', imagery.shape[-2:])

    return sortedImagery, sortedConfidence
Esempio n. 6
0
def testCloseness(net, imagery):
    '''Test the imagery for how close it is to the target data. This also sorts
       the results according to closeness, so we can create a tiled tip-sheet.
    '''
    from dataset.debugger import saveTiledImage
    import numpy as np
    for ii, batch in enumerate(imagery):
        sims = net.closeness(batch)
        sims = [(jj, sim) for jj, sim in enumerate(sims)]
        sims = sorted(sims, key=lambda x: x[1], reverse=True)

        sortedBatch = np.ndarray(batch.shape, dtype=np.float32)
        for jj, sim in enumerate(sims):
            sortedBatch[jj][:] = batch[sim[0]][:]

        saveTiledImage(batch, str(ii) + '.tif', (28, 28))
        saveTiledImage(sortedBatch, str(ii) + '_sorted.tif', (28, 28))
Esempio n. 7
0
def testCloseness(net, imagery) :
    '''Test the imagery for how close it is to the target data. This also sorts
       the results according to closeness, so we can create a tiled tip-sheet.
    '''
    from dataset.debugger import saveTiledImage
    import numpy as np
    for ii, batch in enumerate(imagery) :
        sims = net.closeness(batch)
        sims = [(jj, sim) for jj, sim in enumerate(sims)]
        sims = sorted(sims, key=lambda x: x[1], reverse=True)

        sortedBatch = np.ndarray(batch.shape, dtype=np.float32)
        for jj, sim in enumerate(sims) :
            sortedBatch[jj][:] = batch[sim[0]][:]

        imgDims = imagery.shape[-2:]
        saveTiledImage(batch, str(ii) + '.tif', imgDims)
        saveTiledImage(sortedBatch, str(ii) + '_sorted.tif', imgDims)
Esempio n. 8
0
    def writeWeights(self, ii, imageShape=None) :
        from dataset.debugger import saveTiledImage
        matSize = self._weights.get_value(borrow=True).shape

        buffer = self._weights.get_value(borrow=True).T
        if self.input[0].ndim > 2 :
            import numpy as np
            buffer = np.reshape(buffer, [self._numNeurons] +
                                        list(self.input[0].shape.eval()[1:]))
            imageShape = buffer.shape[-2:]

        # transpose the weight matrix to alighn the kernels contiguously
        saveTiledImage(
            image=buffer,
            path=self.layerID + '_cae_filters_' + str(ii) + '.png',
            imageShape=(1, matSize[0]) if imageShape is None else imageShape,
            tileShape=(matSize[1], 1) if imageShape is None else None,
            spacing=0 if imageShape is None else 1,
            interleave=True)
Esempio n. 9
0
    def writeWeights(self, ii, imageShape=None) :
        from dataset.debugger import saveTiledImage
        matSize = self._weights.get_value(borrow=True).shape

        buffer = self._weights.get_value(borrow=True).T
        if self.input[0].ndim > 2 :
            import numpy as np
            buffer = np.reshape(buffer, [self._numNeurons] +
                                        list(self.input[0].shape.eval()[1:]))
            imageShape = buffer.shape[-2:]

        # transpose the weight matrix to alighn the kernels contiguously
        saveTiledImage(
            image=buffer,
            path=self.layerID + '_cae_filters_' + str(ii) + '.png',
            imageShape=(1, matSize[0]) if imageShape is None else imageShape,
            tileShape=(matSize[1], 1) if imageShape is None else None,
            spacing=0 if imageShape is None else 1,
            interleave=True)
Esempio n. 10
0
    def trainEpoch(self, layerIndex, globalEpoch, numEpochs=1) :
        '''Train the network against the pre-loaded inputs for a user-specified
           number of epochs.

           layerIndex  : index of the layer to train
           globalEpoch : total number of epochs the network has previously 
                         trained
           numEpochs   : number of epochs to train this round before stopping
        '''
        globCost = []
        for localEpoch in range(numEpochs) :
            layerEpochStr = 'Layer[' + str(layerIndex) + '] Epoch[' + \
                            str(globalEpoch + localEpoch) + ']'
            self._startProfile('Running ' + layerEpochStr, 'info')
            locCost = []
            for ii in range(self._numTrainBatches) :
                locCost.append(self.train(layerIndex, ii))

            # log the costs
            locCost = np.sum(locCost, axis=0)
            costMessage = layerEpochStr
            for ii, l in enumerate(self._layers[layerIndex].getCostLabels()) :
                costMessage += '|' + l + ': ' + str(locCost[ii])
            if self._greedyNetwork :
                costMessage += '|Network Cost: ' + str(locCost[-1])
            self._startProfile(costMessage, 'info')
            globCost.append(locCost)
            self._endProfile()

            self._endProfile()

        # optionally dump debugging images
        if self._debug :
            from dataset.debugger import saveTiledImage
            from dataset.shared import getShape

            # write the layer's reconstruction
            reconstructedInput = self._layers[layerIndex].reconstruction(
                self._trainData.get_value(borrow=True)[0])
            reconstructedInput = np.resize(
                reconstructedInput,
                getShape(self._layers[layerIndex].input[0]))
            imageShape = getShape(self._layers[layerIndex].input[0])[-2:]

            # reshape for fully-connected layers
            tileShape = None
            if len(self._layers[layerIndex].getInputSize()) == 2 and \
                len(getShape(self._layers[layerIndex].input[0])) == 2 :
                imageShape=(1, self._layers[layerIndex].getInputSize()[1])
                tileShape=(getShape(self._layers[layerIndex].input[0])[0], 1)

            self.writeWeights(layerIndex, globalEpoch + localEpoch)
            saveTiledImage(image=reconstructedInput,
                            path=self._layers[layerIndex].layerID +
                                '_reconstruction_' +
                                str(globalEpoch+localEpoch) + '.png',
                            imageShape=imageShape, tileShape=tileShape,
                            spacing=1, interleave=True)

            # write the sub-network's reconstruction
            reconstructedInput = self.reconstruction[layerIndex](
                self._trainData.get_value(borrow=True)[0])
            imageShape = self._testData.shape.eval()[-2:]
            reconstructedInput = np.resize(
                reconstructedInput, self._testData.shape.eval()[-4:])
            saveTiledImage(image=reconstructedInput,
                            path='network_reconstruction_' +
                                    str(globalEpoch+localEpoch) + '.png',
                            imageShape=imageShape, spacing=1,
                            interleave=True)

        return globalEpoch + numEpochs, globCost
Esempio n. 11
0
                                                      batchSize=100,
                                                      holdoutPercentage=0,
                                                      log=log),
                                        shared=False,
                                        log=log)
    vectorized = (train[0].shape[0], train[0].shape[1],
                  train[0].shape[3] * train[0].shape[4])
    train = (np.reshape(train[0], vectorized), train[1])

    input = t.fmatrix()
    ae = ContiguousAutoEncoder('cae',
                               input=input,
                               inputSize=(train[0].shape[1],
                                          train[0].shape[2]),
                               numNeurons=options.neuron,
                               learningRate=options.learn,
                               dropout=.5 if options.dropout else 1.)
    for ii in range(50):
        start = time.time()
        for jj in range(len(train[0])):
            ae.train(train[0][jj])

        ae.writeWeights(ii + 1, (28, 28))

        saveTiledImage(image=ae.reconstruction(train[0][0]),
                       path='cae_filters_reconstructed_' + str(ii + 1) +
                       '.png',
                       imageShape=(28, 28),
                       spacing=1)
        print('Epoch [' + str(ii) + ']: ' + str(ae.train(train[0][0])) + \
              ' ' + str(time.time() - start) + 's')
Esempio n. 12
0
    stream.setFormatter(formatter)
    log.addHandler(stream)

    # NOTE: The pickleDataset will silently use previously created pickles if
    #       one exists (for efficiency). So watch out for stale pickles!
    train, test, labels = ingestImagery(pickleDataset(
            options.data, batchSize=100, 
            holdoutPercentage=0, log=log), shared=False, log=log)
    vectorized = (train[0].shape[0], train[0].shape[1], 
                  train[0].shape[3] * train[0].shape[4])
    train = (np.reshape(train[0], vectorized), train[1])

    input = t.fmatrix()
    ae = ContractiveAutoEncoder('cae', input=input, 
                                inputSize=(train[0].shape[1],
                                           train[0].shape[2]),
                                numNeurons=options.neuron,
                                learningRate=options.learn,
                                dropout=.5 if options.dropout else 1.)
    for ii in range(50) :
        start = time.time()
        for jj in range(len(train[0])) :
            ae.train(train[0][jj])

        ae.writeWeights(ii+1, (28,28))

        saveTiledImage(image=ae.reconstruction(train[0][0]),
                       path='cae_filters_reconstructed_' + str(ii+1) + '.png',
                       imageShape=(28, 28), spacing=1)
        print 'Epoch [' + str(ii) + ']: ' + str(ae.train(train[0][0])) + \
              ' ' + str(time.time() - start) + 's'
Esempio n. 13
0
    stream.setLevel(options.level.upper())
    stream.setFormatter(formatter)
    log.addHandler(stream)

    # NOTE: The pickleDataset will silently use previously created pickles if
    #       one exists (for efficiency). So watch out for stale pickles!
    train, test, labels = ingestImagery(pickleDataset(options.data,
                                                      batchSize=100,
                                                      holdoutPercentage=0,
                                                      log=log),
                                        shared=False,
                                        log=log)

    input = t.ftensor4()
    ae = ConvolutionalAutoEncoder('cae', input, train[0].shape[1:],
                                  (options.kernel, train[0].shape[2], 5, 5),
                                  (2, 2))
    ae.writeWeights(0)
    for ii in range(100):
        start = time.time()
        for jj in range(len(train[0])):
            ae.train(train[0][jj])
        ae.writeWeights(ii + 1)

        saveTiledImage(image=ae.reconstruction(train[0][0]),
                       path=ae.layerID + '_cae_filters_reconstructed_' +
                       str(ii + 1) + '.png',
                       imageShape=(28, 28),
                       spacing=1,
                       interleave=True)