def __init__(self, context, devices, dImg): self.context = context self.queue = cl.CommandQueue(context, properties=cl.command_queue_properties.PROFILING_ENABLE) self.dim = dImg.dim options = [ '-D IMAGEW='+str(self.dim[0]), '-D IMAGEH='+str(self.dim[1]), ] self.dImg = dImg self.dFg = Buffer2D(context, cl.mem_flags.READ_WRITE, self.dim, np.uint32) self.dBg = Buffer2D(context, cl.mem_flags.READ_WRITE, self.dim, np.uint32) self.dLcv = Buffer2D(context, cl.mem_flags.READ_WRITE, self.dim, np.float32) self.dAlpha = Buffer2D(context, cl.mem_flags.READ_WRITE, self.dim, np.float32) filename = os.path.join(os.path.dirname(__file__), 'sharedmatting.cl') program = createProgram(context, devices, options, filename) self.kernGather = cl.Kernel(program, 'gather') self.kernLcv = cl.Kernel(program, 'local_color_variation') self.kernRefine = cl.Kernel(program, 'refine') self.kernProcessTrimap = cl.Kernel(program, 'process_trimap') self.trimapFilter = cl.Kernel(program, 'trimap_filter') gWorksize = roundUp(self.dim, SharedMatting.lw) args = [ self.dImg, self.dLcv, self.dAlpha ] self.kernLcv(self.queue, gWorksize, SharedMatting.lw, *args)
def __init__(self, context, devices, capacity): self.context = context if PROFILE_GPU == True: self.queue = cl.CommandQueue(context, properties=cl.command_queue_properties.PROFILING_ENABLE) else: self.queue = cl.CommandQueue(context) filename = os.path.join(os.path.dirname(__file__), "prefixsum.cl") program = createProgram(context, devices, [], filename) self.kernScan_pad_to_pow2 = cl.Kernel(program, "scan_pad_to_pow2") self.kernScan_subarrays = cl.Kernel(program, "scan_subarrays") self.kernScan_inc_subarrays = cl.Kernel(program, "scan_inc_subarrays") self.lw = (LEN_WORKGROUP,) self.capacity = roundUp(capacity, ELEMENTS_PER_WORKGROUP) self.d_parts = [] len = self.capacity / ELEMENTS_PER_WORKGROUP while len > 0: self.d_parts.append(cl.Buffer(context, cl.mem_flags.READ_WRITE, szInt * len)) len = len / ELEMENTS_PER_WORKGROUP self.elapsed = 0
def __init__(self, shape, parent=None): super(CLCanvas, self).__init__(parent) self.w = 0 self.pbo = None self.tex = None self.width = shape[0] self.height = shape[1] self.shape = shape self.zoom = 1.0 self.transX = 0 self.transY = 1 self.flag = 0 self.viewport = None self.resize(self.zoom * self.width, self.zoom * self.height) self.initializeGL() self.initCL() self.installEventFilter(self) self.fbo = glGenFramebuffers(1) self.rbos = glGenRenderbuffers(2) self.rbosCL = [None, None] for i in [0, 1]: glBindRenderbuffer(GL_RENDERBUFFER, self.rbos[i]) glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA, self.width, self.height) self.rbosCL[i] = cl.GLRenderBuffer(self.context, cm.READ_WRITE, int(self.rbos[i])) self.rboRead = 0 self.rboWrite = 1 self.buffers = {} self.layers = [] self.devices = self.context.get_info(cl.context_info.DEVICES) self.queue = cl.CommandQueue(self.context, properties=cl.command_queue_properties.PROFILING_ENABLE) filename = os.path.join(os.path.dirname(__file__), 'clcanvas.cl') program = createProgram(self.context, self.devices, [], filename) self.kernBlendImgf = cl.Kernel(program, 'blend_imgf') self.kernBlendBufui = cl.Kernel(program, 'blend_bufui') self.kernBlendImgui = cl.Kernel(program, 'blend_imgui') self.kernFlip = cl.Kernel(program, 'flip') self.queue.finish() glFinish()
def __init__(self, context, range, hues=None, sats=None, vals=None): self.context = context devices = context.get_info(cl.context_info.DEVICES) filename = os.path.join(os.path.dirname(__file__), 'colorize.cl') program = createProgram(context, devices, [], filename) self.kern_ui8 = cl.Kernel(program, 'colorize_ui8') self.kern_ui = cl.Kernel(program, 'colorize_ui32') self.kern_i = cl.Kernel(program, 'colorize_i32') self.kern_f = cl.Kernel(program, 'colorize_f32') self.format = format self.range = range self.hues = hues if hues != None else Colorize.HUES.STANDARD self.vals = vals if vals != None else (1, 1) self.sats = sats if sats != None else (1, 1)
def __init__(self, context, devices, d_img, d_labels): Brush.__init__(self, context, devices, d_labels) self.context = context self.queue = cl.CommandQueue(context, properties=cl.command_queue_properties.PROFILING_ENABLE) nComponentsFg = 4 nComponentsBg = 4 self.nDim = 3 self.dim = d_img.dim filename = os.path.join(os.path.dirname(__file__), 'quick.cl') program = createProgram(context, context.devices, [], filename) # self.kernSampleBg = cl.Kernel(program, 'sampleBg') self.kern_get_samples = cl.Kernel(program, 'get_samples') self.lWorksize = (16, 16) self.gWorksize = roundUp(self.dim, self.lWorksize) nSamples = 4 * (self.gWorksize[0] / self.lWorksize[0]) * ( self.gWorksize[1] / self.lWorksize[1]) # self.gmmFg_cpu = mixture.GMM(4) self.gmmFg = GMM(context, 65, nComponentsFg, 10240) self.gmmBg = GMM(context, 65, nComponentsBg, nSamples) self.hScore = np.empty(self.dim, np.float32) self.hSampleFg = np.empty((10240, ), np.uint32) self.hSampleBg = np.empty((12000, ), np.uint32) self.hA = np.empty((max(nComponentsFg, nComponentsBg), 8), np.float32) self.d_img = d_img cm = cl.mem_flags self.dSampleFg = cl.Buffer(context, cm.READ_WRITE, size=4 * 10240) self.dSampleBg = cl.Buffer(context, cm.READ_WRITE, size=4 * 12000) self.dA = cl.Buffer(context, cm.READ_ONLY | cm.COPY_HOST_PTR, hostbuf=self.hA) self.dScoreFg = Buffer2D(context, cm.READ_WRITE, self.dim, np.float32) self.dScoreBg = Buffer2D(context, cm.READ_WRITE, self.dim, np.float32) #self.points = Set() self.capPoints = 200 * 200 * 300 #brush radius 200, stroke length 300 self.points = np.empty((self.capPoints), np.uint32) # self.colorize = Colorize.Colorize(clContext, clContext.devices) # self.hTriFlat = self.hTri.reshape(-1) # self.probBg(1200) self.h_img = np.empty(self.dim, np.uint32) self.h_img = self.h_img.ravel() cl.enqueue_copy(self.queue, self.h_img, self.d_img, origin=(0, 0), region=self.dim).wait() self.samples_bg_idx = np.random.randint(0, self.dim[0] * self.dim[1], 12000) self.hSampleBg = self.h_img[self.samples_bg_idx] cl.enqueue_copy(self.queue, self.dSampleBg, self.hSampleBg).wait() w,m,c = self.gmmBg.fit(self.dSampleBg, 300, retParams=True) print w print m print c self.gmmBg.score(self.d_img, self.dScoreBg) pass
def __init__(self, context, devices, img, neighbourhood=NEIGHBOURHOOD.VON_NEUMANN, weight=None): self.context = context self.queue = cl.CommandQueue(context, properties=cl.command_queue_properties.PROFILING_ENABLE) if weight == None: weight = GrowCut.WEIGHT_DEFAULT if isinstance(img, cl.Image): self.dImg = img width = img.get_image_info(cl.image_info.WIDTH) height = img.get_image_info(cl.image_info.HEIGHT) dim = (width, height) else: raise NotImplementedError('Not implemented for {0}'.format(type( img))) self.tilelist = IncrementalTileList(context, devices, dim, (TILEW, TILEH)) self.hHasConverged = np.empty((1,), np.int32) self.hHasConverged[0] = False self.dLabelsIn = Buffer2D(context, cm.READ_WRITE, dim, np.uint8) self.dLabelsOut = Buffer2D(context, cm.READ_WRITE, dim, np.uint8) self.dStrengthIn = Buffer2D(context, cm.READ_WRITE, dim, np.float32) self.dStrengthOut = Buffer2D(context, cm.READ_WRITE, dim, np.float32) self.dHasConverged = cl.Buffer(context, cm.READ_WRITE | cm.COPY_HOST_PTR, hostbuf=self.hHasConverged) self.args = [ self.tilelist.d_list, self.dLabelsIn, self.dLabelsOut, self.dStrengthIn, self.dStrengthOut, self.dHasConverged, np.int32(self.tilelist.iteration), self.tilelist.d_tiles, cl.LocalMemory(szInt * 9), cl.LocalMemory(szInt * (TILEW + 2) * (TILEH + 2)), cl.LocalMemory(szFloat * (TILEW + 2) * (TILEH + 2)), # cl.LocalMemory(4*szFloat*(TILEW+2)*(TILEH+2)), self.dImg, cl.Sampler(context, False, cl.addressing_mode.NONE, cl.filter_mode.NEAREST) ] self.gWorksize = roundUp(dim, self.lw) self.gWorksizeTiles16 = roundUp(dim, self.lWorksizeTiles16) options = [ '-D TILESW=' + str(self.tilelist.dim[0]), '-D TILESH=' + str(self.tilelist.dim[1]), '-D IMAGEW=' + str(dim[0]), '-D IMAGEH=' + str(dim[1]), '-D TILEW=' + str(TILEW), '-D TILEH=' + str(TILEH), '-D G_NORM(X)=' + weight ] filename = os.path.join(os.path.dirname(__file__), 'growcut.cl') program = createProgram(context, devices, options, filename) if neighbourhood == GrowCut.NEIGHBOURHOOD.VON_NEUMANN: self.kernEvolve = cl.Kernel(program, 'evolveVonNeumann') elif neighbourhood == GrowCut.NEIGHBOURHOOD.MOORE: self.kernEvolve = cl.Kernel(program, 'evolveMoore') self.kernLabel = cl.Kernel(program, 'label') self.isComplete = False