Example #1
0
    def saveannotations(self, imageId, projectId, data):
        # Always save the annotations to the labels folder.
        path = '%s/%s.%s.json'%(Paths.Labels, imageId,projectId)
        with open(path, 'w') as outfile:
            outfile.write(data)

        # Add a training and prediction task to the database
        DB.saveAnnotations( projectId, imageId, path )

        H5Data.generate_preview( DATA_PATH, DATA_NAME, DATA_PATH_LABELS, DATA_PATH_SEGMENTATION, DATA_PATH_IMAGES, imageId, projectId )

        images = DB.getTrainingImages( projectId )
        for img in images:
            print img.id, img.annotationFile, img.annotationTime, img.annotationStatus
Example #2
0
 def getimage(self, imageId):
     image = H5Data.get_slice(DATA_PATH, DATA_NAME, imageId )
     image = Image.fromarray(np.uint8(image*255))
     image.thumbnail((525,525), Image.ANTIALIAS)
     output = StringIO.StringIO()
     image.save(output, 'TIFF')
     return output.getvalue()
Example #3
0
    def getimage(self, imageId, projectId):
        image = H5Data.get_slice(DATA_PATH, DATA_NAME, imageId )
        image = Image.fromarray(np.uint8(image*255))
 
        output = StringIO.StringIO()
        image.save(output, 'TIFF')
        return output.getvalue()
Example #4
0
 def getimage(self, imageId):
     image = H5Data.get_slice(DATA_PATH, DATA_NAME, imageId)
     image = Image.fromarray(np.uint8(image * 255))
     image.thumbnail((525, 525), Image.ANTIALIAS)
     output = StringIO.StringIO()
     image.save(output, 'TIFF')
     return output.getvalue()
Example #5
0
    def saveannotations(self, imageId, projectId, data):
        # Always save the annotations to the labels folder.
        path = '%s/%s.%s.json' % (Paths.Labels, imageId, projectId)
        with open(path, 'w') as outfile:
            outfile.write(data)

        # Add a training and prediction task to the database
        DB.saveAnnotations(projectId, imageId, path)

        H5Data.generate_preview(DATA_PATH, DATA_NAME, DATA_PATH_LABELS,
                                DATA_PATH_SEGMENTATION, DATA_PATH_IMAGES,
                                imageId, projectId)

        images = DB.getTrainingImages(projectId)
        for img in images:
            print img.id, img.annotationFile, img.annotationTime, img.annotationStatus
Example #6
0
    def getimage(self, imageId, projectId):
        image = H5Data.get_slice(DATA_PATH, DATA_NAME, imageId)
        image = Image.fromarray(np.uint8(image * 255))

        output = StringIO.StringIO()
        image.save(output, 'TIFF')
        return output.getvalue()
Example #7
0
    def getProjectsData(self, projectId):
        print 'browse.getProjectEditData'

        project = DB.getProject(projectId)
        data = {}
        data['names'] = DB.getProjectNames()
        #data['num_slices']    = H5Data.getSliceCount()
        #data['num_slices'] = self.getSliceCount()

        data['num_slices'] = H5Data.getSliceCount(Paths.Data)

        if project == None and len(data['names']) > 0:
            project = DB.getProject(data['names'][0])

        active = DB.getActiveProject()
        data['project'] = project.toJson()
        #DB.getProject( projectId ).toJson()
        #data['images']   = [ i.toJson() for i in DB.getImages( projectId ) ]
        #data['offline']  = DB.getOfflinePerformance( projectId )
        #data['online']   = DB.getOnlinePerformance( projectId )
        #data['baseline'] = DB.getBaselinePerformance( projectId )
        data['active'] = active.toJson() if active is not None else {}

        return Utility.compress(json.dumps(data))
Example #8
0
    def getProjectsData(self, projectId):
        print 'browse.getProjectEditData'

        project          = DB.getProject( projectId )
        data             = {}
        data['names']    = DB.getProjectNames()
        #data['num_slices']    = H5Data.getSliceCount()
        #data['num_slices'] = self.getSliceCount()

        data['num_slices'] = H5Data.getSliceCount(Paths.Data)

        if project == None and len(data['names']) > 0:
            project  = DB.getProject( data['names'][0] )

        active           = DB.getActiveProject()
        data['project']  = project.toJson()
        #DB.getProject( projectId ).toJson()
        #data['images']   = [ i.toJson() for i in DB.getImages( projectId ) ]
        #data['offline']  = DB.getOfflinePerformance( projectId )
        #data['online']   = DB.getOnlinePerformance( projectId )
        #data['baseline'] = DB.getBaselinePerformance( projectId )
        data['active']   = active.toJson() if active is not None else {}

        return Utility.compress( json.dumps( data ) )
Example #9
0
File: data.py Project: afcarl/icon
    def gen_samples(self, grayPath, project, imageId, nsamples):

        data_mean = project.mean
        data_std = project.std

        annPath = '%s/%s.%s.json' % (Paths.Labels, imageId, project.id)
        #imgPath = '%s/%s.tif'%(grayPath, imageId)
        #if not os.path.exists(annPath) or not os.path.exists( imgPath):
        #    return [], [], 0, 0

        with open(annPath) as json_file:
            annotations = json.load(json_file)

        if len(annotations) == 0:
            return [], [], 0, 0

        n_labels = len(annotations)

        # compute the sample sizes for each label in the annotations
        n_samples_size = nsamples / n_labels
        samples_sizes = []
        for coordinates in annotations:
            n_label_samples_size = len(coordinates) / 2
            n_label_samples_size = min(n_label_samples_size, n_samples_size)
            samples_sizes.append(n_label_samples_size)

        # bailout if not enough samples in the annotations
        n_total = np.sum(samples_sizes)
        if n_total < Data.MinSamples:
            print 'Not enough samples in image: %s' % (imageId)
            return [], [], 0, 0

        # recompute the label sample sizes to ensure the min required samples
        # is fullfiled
        n_diff = nsamples - n_total
        i = 0
        while n_diff > 0 and i < n_labels:
            n_label_samples_size = len(annotations[i]) / 2
            n_add_samples_size = n_label_samples_size - samples_sizes[i]
            n_add_samples_size = min(n_add_samples_size, n_diff)
            n_add_samples_size = max(n_add_samples_size, 0)
            samples_sizes[i] += n_add_samples_size
            n_diff -= n_add_samples_size
            i += 1
        '''
        print '---Data.gen_samples---'
        print 'nsamples:', nsamples
        print 'nsamples actual:', np.sum( samples_sizes )
        print 'n_samples_size:', n_samples_size
        print 'sample sizes:', samples_sizes
        print 'len samples:', len(samples_sizes)
        print '#samples: ', np.sum(samples_sizes)
        print '#actual:', np.sum( [ len(c)/2 for c in annotations ] )
        '''

        mode = 'symmetric'
        patchSize = project.patchSize
        pad = patchSize

        #img = tiff.imread( imgPath )
        p_h5data = '../../data'
        print 'data_stack_file:', data_stack_file
        print 'data_stack_name:', data_stack_name
        print 'path:', p_h5data
        img = H5Data.get_slice(p_h5data, data_stack_name, imageId)
        img = np.pad(img, ((pad, pad), (pad, pad)), mode)
        img = Utility.normalizeImage(img)

        whole_set_patches = np.zeros((n_total, patchSize * patchSize),
                                     dtype=np.float)
        whole_set_labels = np.zeros(n_total, dtype=np.int32)

        border_patch = int(np.ceil(patchSize / 2.0))

        counter = 0
        for label, coordinates in enumerate(annotations):

            if counter >= n_total:
                break

            ncoordinates = len(coordinates)

            if ncoordinates == 0:
                continue

            # randomly sample from the label
            indices = np.random.choice(ncoordinates,
                                       samples_sizes[label],
                                       replace=False)

            for i in indices:
                if i % 2 == 1:
                    i = i - 1

                if counter >= n_total:
                    break

                col = coordinates[i]
                row = coordinates[i + 1]
                r1 = row + patchSize - border_patch
                r2 = row + patchSize + border_patch + 1
                c1 = col + patchSize - border_patch
                c2 = col + patchSize + border_patch + 1

                imgPatch = img[r1:r2, c1:c2]
                imgPatch = skimage.transform.rotate(imgPatch,
                                                    random.choice(xrange(360)))
                imgPatch = imgPatch[0:patchSize, 0:patchSize]

                if random.random() < 0.5:
                    imgPatch = np.fliplr(imgPatch)

                whole_set_patches[counter, :] = imgPatch.flatten()
                whole_set_labels[counter] = label
                counter += 1

        whole_data = np.float32(whole_set_patches)
        if data_mean == None:
            whole_data_mean = np.mean(whole_data, axis=0)
        else:
            whole_data_mean = data_mean

        whole_data = whole_data - np.tile(whole_data_mean,
                                          (np.shape(whole_data)[0], 1))
        if data_std == None:
            whole_data_std = np.std(whole_data, axis=0)
        else:
            whole_data_std = data_std

        whole_data_std = np.clip(whole_data_std, 0.00001,
                                 np.max(whole_data_std))
        whole_data = whole_data / np.tile(whole_data_std,
                                          (np.shape(whole_data)[0], 1))

        print 'min:', np.min(whole_data), np.max(whole_data)

        return whole_data, whole_set_labels, whole_data_mean, whole_data_std
Example #10
0
File: data.py Project: Rhoana/icon
    def gen_samples(self, grayPath, project, imageId, nsamples):

        data_mean=project.mean
        data_std=project.std

        annPath = '%s/%s.%s.json'%(Paths.Labels, imageId, project.id)
        #imgPath = '%s/%s.tif'%(grayPath, imageId)
        #if not os.path.exists(annPath) or not os.path.exists( imgPath):
        #    return [], [], 0, 0

        with open(annPath) as json_file:
            annotations = json.load( json_file )

        if len(annotations) == 0:
            return [], [], 0, 0

        n_labels  = len(annotations)

        # compute the sample sizes for each label in the annotations
        n_samples_size  = nsamples/n_labels
        samples_sizes = []
        for coordinates in annotations:
            n_label_samples_size = len(coordinates)/2
            n_label_samples_size = min( n_label_samples_size, n_samples_size )
            samples_sizes.append( n_label_samples_size )

        # bailout if not enough samples in the annotations
        n_total = np.sum(samples_sizes)
        if n_total < Data.MinSamples:
            print 'Not enough samples in image: %s'%(imageId)
            return [], [], 0, 0

        # recompute the label sample sizes to ensure the min required samples
        # is fullfiled
        n_diff = nsamples - n_total
        i = 0
        while n_diff > 0 and i < n_labels:
            n_label_samples_size = len(annotations[i])/2
            n_add_samples_size   = n_label_samples_size - samples_sizes[i]
            n_add_samples_size   = min( n_add_samples_size, n_diff )
            n_add_samples_size   = max( n_add_samples_size, 0)
            samples_sizes[i]  += n_add_samples_size
            n_diff              -= n_add_samples_size
            i                   += 1

        '''
        print '---Data.gen_samples---'
        print 'nsamples:', nsamples
        print 'nsamples actual:', np.sum( samples_sizes )
        print 'n_samples_size:', n_samples_size
        print 'sample sizes:', samples_sizes
        print 'len samples:', len(samples_sizes)
        print '#samples: ', np.sum(samples_sizes)
        print '#actual:', np.sum( [ len(c)/2 for c in annotations ] )
        '''

        mode   = 'symmetric'
        patchSize = project.patchSize
        pad = patchSize

        #img = tiff.imread( imgPath )
        p_h5data = '../../data'
        print 'data_stack_file:', data_stack_file
        print 'data_stack_name:', data_stack_name
        print 'path:', p_h5data
        img = H5Data.get_slice(p_h5data, data_stack_name,imageId)
        img = np.pad(img, ((pad, pad), (pad, pad)), mode)
        img = Utility.normalizeImage(img)

        whole_set_patches = np.zeros((n_total, patchSize*patchSize), dtype=np.float)
        whole_set_labels = np.zeros(n_total, dtype=np.int32)

        border_patch = int(np.ceil(patchSize/2.0))

        counter = 0
        for label, coordinates in enumerate( annotations ):

            if counter >= n_total:
                break

            ncoordinates = len(coordinates)
        
            if ncoordinates == 0:
                continue

            # randomly sample from the label
            indices = np.random.choice( ncoordinates, samples_sizes[label], replace=False)
 
            for i in indices:
                if i%2 == 1:
                    i = i-1

                if counter >= n_total:
                    break

                col = coordinates[i]
                row = coordinates[i+1]
                r1  = row+patchSize-border_patch
                r2  = row+patchSize+border_patch+1
                c1  = col+patchSize-border_patch
                c2  = col+patchSize+border_patch+1

                imgPatch = img[r1:r2,c1:c2]
                imgPatch = skimage.transform.rotate(imgPatch, random.choice(xrange(360)))
                imgPatch = imgPatch[0:patchSize,0:patchSize]

                if random.random() < 0.5:
                    imgPatch = np.fliplr(imgPatch)

                whole_set_patches[counter,:] = imgPatch.flatten()
                whole_set_labels[counter] = label
                counter += 1

        whole_data = np.float32(whole_set_patches)
        if data_mean == None:
            whole_data_mean = np.mean(whole_data,axis=0)
        else:
            whole_data_mean = data_mean

        whole_data = whole_data - np.tile(whole_data_mean,(np.shape(whole_data)[0],1))
        if data_std == None:
            whole_data_std = np.std(whole_data,axis=0)
        else:
            whole_data_std = data_std

        whole_data_std = np.clip(whole_data_std, 0.00001, np.max(whole_data_std))
        whole_data = whole_data / np.tile(whole_data_std,(np.shape(whole_data)[0],1))

        print 'min:', np.min(whole_data), np.max(whole_data)

        return whole_data, whole_set_labels, whole_data_mean, whole_data_std
Example #11
0
    def renderimage(self, projectId, imageId, purpose):
        H5Data.extract_to(DATA_PATH, DATA_NAME, DATA_PATH_IMAGES, projectId, imageId, purpose )

        self.render("annotate.html")
Example #12
0
    def work(self, project):

        if not self.online:
            self.work_offline(project)
            self.done = True
            return

        start_time = time.clock()

        if project is None:
            return

        print 'prediction.... running', len(self.high)

        if len(self.high) == 0:
            self.high = DB.getPredictionImages(project.id, 1)

        #FG - march 4th 2016
        #if len(self.low) == 0:
        #    self.low = DB.getPredictionImages( project.id, 0 )
        '''
        for img in self.high:
            print 'hid:', img.id, img.modelModifiedTime, img.segmentationTime

        print '----'
        for img in self.low:
            print 'lid:', img.id, img.modelModifiedTime, img.segmentationTime

        exit(1)
        '''

        task = None
        if (self.priority == 0 or len(self.low) == 0) and len(self.high) > 0:
            self.priority = 1
            task = self.high[0]
            del self.high[0]
        elif len(self.low) > 0:
            self.priority = 0
            task = self.low[0]
            del self.low[0]

        if task == None:
            return

        has_new_model = (self.modTime != project.modelTime)
        revision = DB.getRevision(project.id)
        print 'revision:', revision
        #has_new_model = (revision != self.revision or has_new_model)

        # reload the model if it changed
        if has_new_model:
            #self.revision = revision
            print 'initializing...'
            self.model.initialize()
            self.modTime = project.modelTime

        # read image to segment
        basepath = Paths.TrainGrayscale if task.purpose == 0 else Paths.ValidGrayscale
        path = '%s/%s.tif' % (basepath, task.id)
        #success, image = Utility.get_image_padded(path, project.patchSize ) #model.get_patch_size())

        print 'segment - path:', path
        print 'priority - ', task.segmentationPriority
        # perform segmentation

        Utility.report_status('segmenting %s' % (task.id), '')
        #probs = self.model.predict( path )
        #probs = self.model.classify( image )

        # serialize to file
        segPath = '%s/%s.%s.seg' % (Paths.Segmentation, task.id, project.id)
        seg = H5Data.get_slice(DATA_PATH, DATA_NAME, task.id)
        self.classify_n_save(seg, segPath, project)
        #self.classify_n_save( path, segPath, project )

        H5Data.generate_preview(DATA_PATH, DATA_NAME, DATA_PATH_LABELS,
                                DATA_PATH_SEGMENTATION, DATA_PATH_IMAGES,
                                task.id, project.id)

        end_time = time.clock()
        duration = (end_time - start_time)
        DB.finishPrediction(self.projectId, task.id, duration, self.modTime)
Example #13
0
File: setup.py Project: afcarl/icon
        i += 1
        if i > 20:
            break

    # store the project
    DB.storeProject(project)


#---------------------------------------------------------------------------
# Entry point to the main function of the program.
#---------------------------------------------------------------------------
if __name__ == '__main__':
    print 'Icon database (installation interface)'

    # extract H5Data
    H5Data.extract_all(DATA_PATH, DATA_NAME, DATA_PATH_IMAGES)

    # start in a blank slate
    Tables.drop()

    # install the tables
    Tables.create()

    # install mlp project
    p = Project(id='testmlp', type='MLP')
    p.batchSize = 16
    p.patchSize = 39
    p.hiddenUnits = [500, 500, 500]
    install(p)

    # install cnn project
Example #14
0
    def work(self, project):

        if not self.online:
            self.work_offline(project)
            self.done = True
            return

        start_time = time.clock()

        if project is None:
            return

        print 'prediction.... running', len(self.high)

        if len(self.high) == 0:
            self.high = DB.getPredictionImages( project.id, 1)


        #FG - march 4th 2016
        #if len(self.low) == 0:
        #    self.low = DB.getPredictionImages( project.id, 0 )

        '''
        for img in self.high:
            print 'hid:', img.id, img.modelModifiedTime, img.segmentationTime

        print '----'
        for img in self.low:
            print 'lid:', img.id, img.modelModifiedTime, img.segmentationTime

        exit(1)
        '''

        task = None
        if (self.priority == 0 or len(self.low) == 0) and len(self.high) > 0:
            self.priority = 1
            task = self.high[0]
            del self.high[0]
        elif len(self.low) > 0:
            self.priority = 0
            task = self.low[0]
            del self.low[0]

        if task == None:
            return

        has_new_model = (self.modTime != project.modelTime)
        revision = DB.getRevision( project.id )
        print 'revision:', revision
        #has_new_model = (revision != self.revision or has_new_model)

        # reload the model if it changed
        if has_new_model:
            #self.revision = revision
            print 'initializing...'
            self.model.initialize()
            self.modTime = project.modelTime

        # read image to segment
        basepath = Paths.TrainGrayscale if task.purpose == 0 else Paths.ValidGrayscale
        path = '%s/%s.tif'%(basepath, task.id)
        #success, image = Utility.get_image_padded(path, project.patchSize ) #model.get_patch_size())

        print 'segment - path:', path
        print 'priority - ', task.segmentationPriority
        # perform segmentation

        Utility.report_status('segmenting %s'%(task.id),'')
        #probs = self.model.predict( path )
        #probs = self.model.classify( image )

        # serialize to file
        segPath = '%s/%s.%s.seg'%(Paths.Segmentation, task.id, project.id)
        seg = H5Data.get_slice( DATA_PATH, DATA_NAME, task.id )
        self.classify_n_save( seg, segPath, project )
        #self.classify_n_save( path, segPath, project )         

        H5Data.generate_preview( DATA_PATH, DATA_NAME, DATA_PATH_LABELS, DATA_PATH_SEGMENTATION, DATA_PATH_IMAGES, task.id, project.id )

        end_time = time.clock()
        duration = (end_time - start_time)
        DB.finishPrediction( self.projectId, task.id, duration, self.modTime )
Example #15
0
    def renderimage(self, projectId, imageId, purpose):
        H5Data.extract_to(DATA_PATH, DATA_NAME, DATA_PATH_IMAGES, projectId,
                          imageId, purpose)

        self.render("annotate.html")