Exemplo n.º 1
0
def buildTrainingSet(DF, segImages):

	sparseImages = SparseImageSource('/data/datasets/lung/resampled_order1/segmentedNonzero.h5')

	outArray = '/ssd/camsB.h5'
	outTsv = outArray.replace('.h5', '.tsv')

	camImageDF = pandas.DataFrame()



	DBo = tables.open_file(outArray, mode='w')
	filters = tables.Filters(complevel=6, complib='blosc:snappy')      # 7.7sec / 1.2 GB   (14 sec 1015MB if precision is reduced)           140s 3.7GB
	#filters = None
	cams = DBo.create_earray(DBo.root, 'cams', atom=tables.Int16Atom(shape=CAM_SHAPE), shape=(0,), expectedrows=len(DF), filters=filters)




	for index, row in tqdm(DF.iterrows(), total=len(DF)):
		print row
		cancer = row['cancer']

		# slow
		#image, imgNum = getImage(segImages, row)
		#camImage = makeCamImgFromImage(image, cubeSize)

		# faster
		#image = sparseImages.getImageFromSparse(row)
		#camImage = makeCamImgFromImage(image, cubeSize)

		# should be fastest
		cubes, positions = sparseImages.getCubesAndPositions(row, posType='pos')
		camImage = makeCamImageFromCubes(cubes, positions)


		print 'CAM IMAGE SHAPE %s    mean %s   max %s     ==========', camImage.shape, camImage.mean(), camImage.max()

		if camImage.mean() == 0: print 'THIS IMAGE IS BAD ========================'


		cam = forceImageIntoShape(camImage, CAM_SHAPE)

		cams.append([cam])
		camImageDF = camImageDF.append(row)

		camImageDF.to_csv(outTsv, sep='\t')
Exemplo n.º 2
0
	class cubeSource(mpipe.OrderedWorker):
		def doInit(self):
			self.sparseImages = SparseImageSource(inArray)

		def doTask(self, row):
			cubes, positions = self.sparseImages.getCubesAndPositions(row, posType='pos')
			self.putResult((row, cubes, positions))
			print 'returning image'
Exemplo n.º 3
0
    from keras.models import load_model

    modelFile = sys.argv[1]
    model = load_model(modelFile)
    _, x, y, z, channels = model.input_shape
    cubeSize = x

    gradient_function, camShape = buildGradientFunction(model)

    dsbImages = ImageArray(arrayFile, tsvFile=tsvFile)
    array, DF = dsbImages.array, dsbImages.DF

    DF = DF[DF.cancer != -1]  # remove images from the submission set
    DF = DF.head(10)

    sparseImages = SparseImageSource(
        '/data/datasets/lung/resampled_order1/segmentedNonzero.h5')

    testY, y_score = [], []
    for index, row in DF.iterrows():
        cancer = row['cancer']

        # predictions based on images
        image, imgNum = getImage(array, row)
        noduleScores = predictImage(image, model, cubeSize)

        # predictions based on sparse images
        image = sparseImages.getImageFromSparse(row)
        print image.shape
        crash
        noduleScores = predictImage(image, model, cubeSize)
Exemplo n.º 4
0
def buildTrainingSet(DF, inArray, outArray, storeSourceImg=True):
    sparseImages = SparseImageSource(inArray)
    outTsv = outArray.replace('.h5', '.tsv')

    # single output neuron cams
    #noduleGrad, cubeCamSize  = buildGradientFunction(model)
    #diamGrad, cubeCamSize  = buildGradientFunction(model, output='diam')
    #gradientFunctions = [noduleGrad]

    # softmax output models
    noNodule, cubeCamSize = buildSoftmaxGradientFunction(model, 0)
    smallNodule, cubeCamSize = buildSoftmaxGradientFunction(model, 1)
    bigNodule, cubeCamSize = buildSoftmaxGradientFunction(model, 2)
    gradientFunctions = [noNodule, smallNodule, bigNodule]

    nChannels = len(gradientFunctions)
    if storeSourceImg: nChannels += 1

    CAM_SHAPE = (160, 160, 160, nChannels)

    DBo = tables.open_file(outArray, mode='w')
    filters = tables.Filters(
        complevel=1, complib='blosc:snappy'
    )  # 7.7sec / 1.2 GB   (14 sec 1015MB if precision is reduced)           140s 3.7GB
    #filters = None
    cams = DBo.create_earray(DBo.root,
                             'cams',
                             atom=tables.Float32Atom(shape=CAM_SHAPE),
                             shape=(0, ),
                             expectedrows=len(DF),
                             filters=filters)

    camImageDF = pandas.DataFrame()

    #DF = DF.head(20)

    for index, row in tqdm(DF.iterrows(), total=len(DF)):
        #print row
        cancer = row['cancer']

        #image, imgNum = getImage(segImages, row)						# slow
        #camImage = makeCamImgFromImage(image, cubeSize)
        #image = sparseImages.getImageFromSparse(row)					# faster
        #camImage = makeCamImgFromImage(image, cubeSize)

        # should be fastest
        cubes, positions = sparseImages.getCubesAndPositions(row,
                                                             posType='pos')
        #camImage = makeCamImageFromCubes(cubes, positions, gradientFunctions, cubeCamSize, storeSourceImg=storeSourceImg)
        camImage = makeCamImageFromCubesFaster(cubes,
                                               positions,
                                               gradientFunctions,
                                               cubeCamSize,
                                               storeSourceImg=storeSourceImg)

        if camImage.mean() == 0:
            print 'THIS IMAGE IS BAD ========================'

        print camImage.shape

        print 'nodule image    mean %s    min %s    max %s : ' % (
            camImage[:, :, :, 0].mean(), camImage[:, :, :, 0].min(),
            camImage[:, :, :, 0].max())

        #print 'diam image      mean %s    min %s    max %s : ' % (
        #	camImage[:,:,:,1].mean(), camImage[:,:,:,1].min(), camImage[:,:,:,1].max())

        print 'source image      mean %s    min %s    max %s : ' % (
            camImage[:, :, :, -1].mean(), camImage[:, :, :, -1].min(),
            camImage[:, :, :, -1].max())

        cam = forceImageIntoShape(camImage, CAM_SHAPE)
        #cam = resize(camImage, CAM_SHAPE)

        #crop = boundingBox(camImage, channel=0)
        print cam.shape

        cams.append([cam])
        camImageDF = camImageDF.append(row)
        camImageDF.to_csv(outTsv, sep='\t')
Exemplo n.º 5
0
def testAndVisualize(DF, array, model, modelFile):

    DF = DF[DF.cancer == 1]
    imgRow = DF.iloc[0]

    sparseImages = SparseImageSource(
        '/data/datasets/lung/resampled_order1/segmentedNonzero.h5')
    sDF = sparseImages.DF
    sRow = sDF[sDF.imgNum == imgRow.imgNum].iloc[0]

    cubes, positions = sparseImages.getCubesAndPositions(sRow, posType='pos')

    ########    For models trained that predict binary nodule and linear diameter

    #noduleGrad, cubeCamSize  = buildGradientFunction(model)
    #diamGrad, cubeCamSize  = buildGradientFunction(model, output='diam')
    #gradientFunctions = [noduleGrad, diamGrad]

    ########    For models trained with softmax nodule+diameter classifier

    smallNoduleGrad, cubeCamSize = buildSoftmaxGradientFunction(model, 1)
    bigNoduleGrad, cubeCamSize = buildSoftmaxGradientFunction(model, 2)
    gradientFunctions = [smallNoduleGrad, bigNoduleGrad]

    # guided backprop functions
    register_gradient()
    guided_model = modify_backprop(model, 'GuidedBackProp', modelFile)
    saliency_fn = compile_saliency_function(guided_model)

    ######### Do it and save stuff

    cam = makeCamImageFromCubesFaster(cubes,
                                      positions,
                                      gradientFunctions,
                                      cubeCamSize,
                                      storeSourceImg=True)
    #cam = makeCamImageFromCubesFaster(cubes, positions, gradientFunctions, cubeCamSize, saliencyFn=saliency_fn, storeSourceImg=True)

    vol2Nifti(cam[:, :, :, 0], 'scam1.nii.gz')
    vol2Nifti(cam[:, :, :, 1], 'scam2.nii.gz')
    vol2Nifti(cam[:, :, :, 2], 'simg.nii.gz')

    #numpy.save('cam.npy', bigCam)
    #numpy.save('simage.npy', resizedSourceImage)

    crash

    ###### Guided backprop tests

    register_gradient()
    guided_model = modify_backprop(model, 'GuidedBackProp', modelFile)
    saliency_fn = compile_saliency_function(guided_model)

    cam = cam[:, :, :, 1]

    heatmap = cam / numpy.max(cam)

    preppedCubes = [prepCube(cube, augment=False) for cube in cubes]

    saliencyCubes = saliency_fn([preppedCubes[0:50], 0])  # works!

    sc = saliencyCubes[0]
    print sc.shape, sc.min(), sc.mean(), sc.max()

    crash

    #inputs = [[cube, 0] for cube in preppedCubes]

    for cube, pos in zip(preppedCubes, positions):

        saliency = saliency_fn([cube, 0])
        print saliency.shape
        gradcam = saliency[0] * heatmap[..., numpy.newaxis]
        print gradcam.min(), gradcam.mean(), gradcam.max()
    '''
Exemplo n.º 6
0
def buildTrainingSet(DF, inArray, outArray, storeSourceImg=True):

    #DF = DF.head(573).tail(10)

    sparseImages = SparseImageSource(inArray)
    outTsv = outArray.replace('.h5', '.tsv')

    # single output neuron cams
    noduleGrad, cubeCamSize = buildGradientFunction(model)
    #diamGrad, cubeCamSize  = buildGradientFunction(model, output='diam')
    gradientFunctions = [noduleGrad]

    # softmax output models
    #noduleGrad, cubeCamSize  = buildSoftmaxGradientFunction(model, 0)

    nChannels = len(gradientFunctions)
    if storeSourceImg: nChannels += 1

    CAM_SHAPE = (160, 160, 160, nChannels)

    DBo = tables.open_file(outArray, mode='w')
    filters = tables.Filters(
        complevel=1, complib='blosc:snappy'
    )  # 7.7sec / 1.2 GB   (14 sec 1015MB if precision is reduced)           140s 3.7GB
    #filters = None
    cams = DBo.create_earray(DBo.root,
                             'cams',
                             atom=tables.Float32Atom(shape=CAM_SHAPE),
                             shape=(0, ),
                             expectedrows=len(DF),
                             filters=filters)

    camImageDF = pandas.DataFrame()

    #camImages = []

    #DF = DF.head(20)

    def cubeDecompressor(Q):
        for index, row in tqdm(DF.iterrows(), total=len(DF)):
            # print row
            row = row.copy(deep=True)
            cancer = row['cancer']
            cubes, positions = sparseImages.getCubesAndPositions(row,
                                                                 posType='pos')
            Q.put((row, cubes, positions))
        Q.put(None)

    Q = Queue(maxsize=4)

    t = Thread(target=cubeDecompressor, args=(Q, ))
    t.daemon = True
    t.start()

    while True:
        res = Q.get()
        if res is None:
            Q.task_done()
            break
        row, cubes, positions = res

        #camImage = makeCamImageFromCubes(cubes, positions, gradientFunctions, cubeCamSize, storeSourceImg=storeSourceImg)
        camImage = makeCamImageFromCubesFaster(cubes,
                                               positions,
                                               gradientFunctions,
                                               cubeCamSize,
                                               storeSourceImg=storeSourceImg)

        if camImage.mean() == 0:
            print 'THIS IMAGE IS BAD ========================'

        print camImage.shape

        print 'nodule image    mean %s    min %s    max %s : ' % (
            camImage[:, :, :, 0].mean(), camImage[:, :, :, 0].min(),
            camImage[:, :, :, 0].max())

        print 'diam image      mean %s    min %s    max %s : ' % (
            camImage[:, :, :, 1].mean(), camImage[:, :, :, 1].min(),
            camImage[:, :, :, 1].max())

        #print 'source image      mean %s    min %s    max %s : ' % (
        #	camImage[:,:,:,2].mean(), camImage[:,:,:,2].min(), camImage[:,:,:,2].max())

        cam = forceImageIntoShape(camImage, CAM_SHAPE)
        #cam = resize(camImage, CAM_SHAPE)

        #crop = boundingBox(camImage, channel=0)
        print 'Cam shape: ', cam.shape

        cams.append([cam])
        camImageDF = camImageDF.append(row)
        #camImages.append(row)
        camImageDF.to_csv(outTsv, sep='\t')
Exemplo n.º 7
0
		def doInit(self):
			self.sparseImages = SparseImageSource(inArray)