def form_patch(ii,jj,patch_height,patch_width,height,width): ytop = max(0,ii - patch_height/2) ybot = ytop+patch_height if ybot >= height: ybot = height-1 ytop = ybot - patch_height if ytop < 0: ytop = 0 xleft = max(0,jj - patch_width/2) xright = xleft+patch_width if xright >= width: xright = width-1 xleft = xleft - patch_width if xleft < 0: xleft = 0 return pyl.int32(ytop), pyl.int32(ybot), pyl.int32(xleft), pyl.int32(xright)
def form_patch(ii, jj, patch_height, patch_width, height, width): ytop = max(0, ii - patch_height / 2) ybot = ytop + patch_height if ybot >= height: ybot = height - 1 ytop = ybot - patch_height if ytop < 0: ytop = 0 xleft = max(0, jj - patch_width / 2) xright = xleft + patch_width if xright >= width: xright = width - 1 xleft = xleft - patch_width if xleft < 0: xleft = 0 return pyl.int32(ytop), pyl.int32(ybot), pyl.int32(xleft), pyl.int32( xright)
gpu_image = t_image.ravel().astype(np.float32) t_patch_im = 255 * t_image[t_patch[0]:t_patch[1], t_patch[2]:t_patch[3]] im_out = open( cur_poselet_dir + "training_patch_" + str(close_poselet_indx) + ".png", 'w') im_writer = png.Writer(t_patch_im.shape[1], t_patch_im.shape[0], greyscale=True) im_writer.write(im_out, np.asarray(t_patch_im)) im_out.close() gpu_image = t_image.ravel().astype(np.float32) height = pyl.int32(t_image.shape[0]) width = pyl.int32(t_image.shape[1]) num_pixels = width * height # Variables needed during the GPU computation gaussian_array = np.zeros( (t_image.shape[0], t_image.shape[1])).ravel().astype(np.float32) x_gradient = np.zeros( (t_image.shape[0], t_image.shape[1])).ravel().astype(np.float32) y_gradient = np.zeros( (t_image.shape[0], t_image.shape[1])).ravel().astype(np.float32) gradient_mag = np.zeros( (t_image.shape[0], t_image.shape[1])).ravel().astype(np.float32) angles = np.zeros( (t_image.shape[0], t_image.shape[1])).ravel().astype(np.float32) hog_output = np.zeros((1, 81)).ravel().astype(np.float32)
def queryDb(self, file_version = None, db_user = '******'): """ Execute the DB queries for a month or 24 hours depending on self.mode. INPUT: None OUTPUT: None SIDE EFFECTS: popultes required arrays for plotting """ if self.db in self.DB.keys(): import psycopg2 as dbdrv hsql="""select count(file_id), sum(uncompressed_file_size)/ (date_part('epoch', to_timestamp(max(ingestion_date),'YYYY-MM-DD"T"HH24:MI:SS.MS')- to_timestamp(min(ingestion_date),'YYYY-MM-DD"T"HH24:MI:SS.MS')) + 10.)/(1024^2) as average, max(ingestion_date) as last , min(ingestion_date) as first , sum(uncompressed_file_size)/1024^4 as volume from ngas_files where ingestion_date between {0} and {1}""" try: t=dbpass except NameError: dbpass = getpass.getpass('%s DB password: '******'%s',max(ingestion_date))- strftime('%s',min(ingestion_date)))/1024./1024. as average, max(ingestion_date) as last , min(ingestion_date) as first , sum(uncompressed_file_size)/1024/1024/1024./1024. as volume from ngas_files where ingestion_date between {0} and {1}""" dbconn = dbdrv.connect(self.db) if (file_version): hsql += " and file_version = %d" % file_version cur = dbconn.cursor() res = [] for ii in range(1,self.loop+1): if self.mode[0] != 'Weekly': ssql = hsql.format(self.fdate % (self.date, (ii-1)), self.fdate % (self.date,ii)) else: ssql = hsql.format(self.drange[ii-1][0], self.drange[ii-1][1]) cur.execute(ssql) r = cur.fetchall() res.append(r) self.ssql = ssql res = pylab.array(res) y = res[:,:,1].reshape(len(res)) y[where(y < -1)]=0 n = res[:,:,0].reshape(len(res)) n[where(n < -1)]=0 self.y = pylab.float16(y) self.n = pylab.float16(n) vol = pylab.float16(res[:,:,4]) self.tvol = pylab.float64(vol)[vol>0].sum() self.tfils = pylab.int32(self.n.sum()) self.res=res dbconn.close() del(dbconn) print self.tfils, pylab.int32(self.n.sum()) return
# Set up device variables to run the patch-based HoG GPU code to extract a feature for this image. # patch_hog_kernel(float* input_image, int im_width, int im_height, int patch_top, int patch_bot, int patch_left, int patch_right, # float* gaussian_array, float* x_gradient, float* y_gradient, float* gradient_mag, float* angles, float* output_array) if len(t_image.shape) == 3: t_image = clr.rgb2gray(t_image) gpu_image = t_image.ravel().astype(np.float32) t_patch_im = 255 * t_image[t_patch[0]:t_patch[1],t_patch[2]:t_patch[3]] im_out = open(cur_poselet_dir+"training_patch_"+str(close_poselet_indx)+".png",'w') im_writer = png.Writer(t_patch_im.shape[1], t_patch_im.shape[0], greyscale=True) im_writer.write(im_out, np.asarray(t_patch_im)) im_out.close() gpu_image = t_image.ravel().astype(np.float32) height = pyl.int32( t_image.shape[0] ) width = pyl.int32( t_image.shape[1] ) num_pixels = width * height # Variables needed during the GPU computation gaussian_array = np.zeros((t_image.shape[0],t_image.shape[1])).ravel().astype(np.float32) x_gradient = np.zeros((t_image.shape[0],t_image.shape[1])).ravel().astype(np.float32) y_gradient = np.zeros((t_image.shape[0],t_image.shape[1])).ravel().astype(np.float32) gradient_mag = np.zeros((t_image.shape[0],t_image.shape[1])).ravel().astype(np.float32) angles = np.zeros((t_image.shape[0],t_image.shape[1])).ravel().astype(np.float32) hog_output = np.zeros((1,81)).ravel().astype(np.float32) # Number of threads per block n_TPB_x = int(16) n_TPB_y = int(16)
cu.memcpy_htod( x_gradient_device, x_gradient ) y_gradient_device = cu.mem_alloc(y_gradient.nbytes) cu.memcpy_htod( y_gradient_device, y_gradient ) gradient_mag_device = cu.mem_alloc(gradient_mag.nbytes) cu.memcpy_htod( gradient_mag_device, gradient_mag ) angles_device = cu.mem_alloc(angles.nbytes) cu.memcpy_htod( angles_device, angles ) output_hog_device = cu.mem_alloc(hog_output.nbytes) cu.memcpy_htod( output_hog_device, hog_output) # Execute the kernel patch_hog_kernel(input_im_device, pyl.int32(width), pyl.int32(height), pyl.int32(0), pyl.int32(height), pyl.int32(0), pyl.int32(width), x_gradient_device, y_gradient_device, gradient_mag_device, angles_device, output_hog_device, block=(n_TPB_x,n_TPB_y,1), grid=(n_blocks_x,n_blocks_y)) cu.memcpy_dtoh(hog_output,output_hog_device) # Record the times gpu_end_time.record(); gpu_end_time.synchronize(); gpu_time_delta_seconds = gpu_start_time.time_till(gpu_end_time) * 1e-3 gpu_patch_times.append(gpu_time_delta_seconds) ### # Keypoint-based HoG kernel ###
y_gradient_device = cu.mem_alloc(y_gradient.nbytes) cu.memcpy_htod(y_gradient_device, y_gradient) gradient_mag_device = cu.mem_alloc(gradient_mag.nbytes) cu.memcpy_htod(gradient_mag_device, gradient_mag) angles_device = cu.mem_alloc(angles.nbytes) cu.memcpy_htod(angles_device, angles) output_hog_device = cu.mem_alloc(hog_output.nbytes) cu.memcpy_htod(output_hog_device, hog_output) # Execute the kernel patch_hog_kernel(input_im_device, pyl.int32(width), pyl.int32(height), pyl.int32(0), pyl.int32(height), pyl.int32(0), pyl.int32(width), x_gradient_device, y_gradient_device, gradient_mag_device, angles_device, output_hog_device, block=(n_TPB_x, n_TPB_y, 1), grid=(n_blocks_x, n_blocks_y)) cu.memcpy_dtoh(hog_output, output_hog_device)