def calculate_active_regions(self): """ Find projection of sample through entrance apertures onto xtals. FIXME: This currently assumes rectangular projections onto crytals. Returns: List of active regions for each crystal. Each entry in list is also a list (since multiple apertures may illuminate same xtal). Finally, each subentry is a list of 4 points. """ if len(self.entrance_aperture) != len(self.xtals): raise Exception( "Number of entrance apertures and crystals must be same") # run through apertures and crystals active_regions = [] for i, xtal_rect in enumerate(self.xtal_rects): active_regions.append([]) for aperture in self.entrance_aperture: ap_rect = geom.Rectangle(aperture[0], aperture[1], aperture[3]) projection = geom.project_point_through_rect_onto_rect( self.sample, ap_rect, xtal_rect) if projection is not None: active_regions[i].append(projection) return active_regions
def bounds(self): r = None for vertex in self.vertices: if r is None: r = geom.Rectangle(vertex.point, vertex.point) else: r = r.extend(vertex.point) return r
def load_rect(region, rect, load_func=load_tile, mode='all'): # special case for fast load: rect is single tile if rect.start.x % tile_size == 0 and rect.start.y % tile_size == 0 and rect.end.x % tile_size == 0 and rect.end.y % tile_size == 0 and rect.end.x - rect.start.x == tile_size and rect.end.y - rect.start.y == tile_size: return load_func(region, rect.start.x / tile_size, rect.start.y / tile_size, mode=mode) tile_rect = geom.Rectangle( geom.Point(rect.start.x / tile_size, rect.start.y / tile_size), geom.Point((rect.end.x - 1) / tile_size + 1, (rect.end.y - 1) / tile_size + 1)) full_rect = geom.Rectangle(tile_rect.start.scale(tile_size), tile_rect.end.scale(tile_size)) full_ims = {} for i in range(tile_rect.start.x, tile_rect.end.x): for j in range(tile_rect.start.y, tile_rect.end.y): p = geom.Point(i - tile_rect.start.x, j - tile_rect.start.y).scale(tile_size) tile_ims = load_func(region, i, j, mode=mode) for k, im in tile_ims.items(): scale = tile_size / im.shape[0] if k not in full_ims: full_ims[k] = numpy.zeros( (int(full_rect.lengths().x / scale), int(full_rect.lengths().y / scale), im.shape[2]), dtype='uint8') full_ims[k][int(p.x / scale):int((p.x + tile_size) / scale), int(p.y / scale):int((p.y + tile_size) / scale), :] = im crop_rect = geom.Rectangle(rect.start.sub(full_rect.start), rect.end.sub(full_rect.start)) for k in full_ims: scale = (full_rect.end.x - full_rect.start.x) / full_ims[k].shape[0] full_ims[k] = full_ims[k][int(crop_rect.start.x / scale):int(crop_rect.end.x / scale), int(crop_rect.start.y / scale):int(crop_rect.end.y / scale), :] return full_ims
def tile_filter(tile): # find starting points in different tiles if tile.region not in REGIONS: return False rect = geom.Rectangle(tile.scale(tile_size), tile.add(geom.Point(1, 1)).scale(tile_size)) starting_locations = self.all_starting_locations['{}_{}_{}'.format( tile.region, tile.x, tile.y)] starting_locations = [ loc for loc in starting_locations if rect.add_tol(-window_size).contains(loc[0]['point']) ] return len(starting_locations) > 0
def zip_frame_info(detections, label, frame_idx): if not detections: return [] frame_path = FRAME_PATH.format(label) im = skimage.io.imread('{}/{}'.format(frame_path, get_frame_fname(frame_idx))) im_bounds = geom.Rectangle(geom.Point(0, 0), geom.Point(im.shape[0], im.shape[1])) info = [] for idx, detection in enumerate(detections): rect = geom.Rectangle( geom.Point(detection['top'] // FRAME_SCALE, detection['left'] // FRAME_SCALE), geom.Point(detection['bottom'] // FRAME_SCALE, detection['right'] // FRAME_SCALE)) rect = im_bounds.clip_rect(rect) if rect.lengths().x < 4 or rect.lengths().y < 4: continue crop = im[rect.start.x:rect.end.x, rect.start.y:rect.end.y, :] resize_factor = min([ float(CROP_SIZE) / crop.shape[0], float(CROP_SIZE) / crop.shape[1] ]) resize_shape = [ int(crop.shape[0] * resize_factor), int(crop.shape[1] * resize_factor) ] if resize_shape[0] == 0 or resize_shape[1] == 0: continue crop = (skimage.transform.resize(crop, resize_shape) * 255).astype('uint8') fix_crop = numpy.zeros((CROP_SIZE, CROP_SIZE, 3), dtype='uint8') fix_crop[0:crop.shape[0], 0:crop.shape[1], :] = crop detection['width'] = float(detection['right'] - detection['left']) / ORIG_WIDTH detection['height'] = float(detection['bottom'] - detection['top']) / ORIG_HEIGHT info.append((detection, fix_crop, idx)) return info
def get_test_tile_data(self): if 'd' not in REGIONS: print("d is not in regions") return None rect = geom.Rectangle( geom.Point(-4096, -4096), geom.Point(4096, 4096), ) starting_locations = self.all_starting_locations['d_0_-1'] starting_locations = [ loc for loc in starting_locations if rect.add_tol(-window_size).contains(loc[0]['point']) ] return { 'region': 'd', 'rect': rect, 'search_rect': rect.add_tol(-window_size / 2), 'cache': self.cache, 'starting_locations': starting_locations, 'gc': self.gcs['d'], }
def get_training_tile_data_normal(self, tile, tries=0): rect = geom.Rectangle(tile.scale(tile_size), tile.add(geom.Point(1, 1)).scale(tile_size)) if tries < 3: search_rect_x = random.randint( window_size / 2, tile_size - window_size / 2 - self.search_rect_size) search_rect_y = random.randint( window_size / 2, tile_size - window_size / 2 - self.search_rect_size) search_rect = geom.Rectangle( rect.start.add(geom.Point(search_rect_x, search_rect_y)), rect.start.add(geom.Point(search_rect_x, search_rect_y)).add( geom.Point(self.search_rect_size, self.search_rect_size)), ) starting_locations = self.all_starting_locations['{}_{}_{}'.format( tile.region, tile.x, tile.y)] starting_locations = [ loc for loc in starting_locations if search_rect.add_tol(-window_size / 4).contains(loc[0]['point']) ] else: starting_locations = self.all_starting_locations['{}_{}_{}'.format( tile.region, tile.x, tile.y)] starting_locations = [ loc for loc in starting_locations if rect.add_tol(-window_size).contains(loc[0]['point']) ] starting_location = random.choice(starting_locations) search_rect_min = starting_location[0]['point'].sub( geom.Point(self.search_rect_size / 2, self.search_rect_size / 2)).sub(rect.start) if search_rect_min.x < window_size / 2: search_rect_min.x = window_size / 2 elif search_rect_min.x > tile_size - window_size / 2 - self.search_rect_size: search_rect_min.x = tile_size - window_size / 2 - self.search_rect_size if search_rect_min.y < window_size / 2: search_rect_min.y = window_size / 2 elif search_rect_min.y > tile_size - window_size / 2 - self.search_rect_size: search_rect_min.y = tile_size - window_size / 2 - self.search_rect_size search_rect = geom.Rectangle( rect.start.add(search_rect_min), rect.start.add(search_rect_min).add( geom.Point(self.search_rect_size, self.search_rect_size)), ) starting_locations = [starting_location] if not starting_locations: return self.get_training_tile_data_normal(tile, tries + 1) return { 'region': tile.region, 'rect': rect, 'search_rect': search_rect, 'cache': self.cache, 'starting_locations': starting_locations, 'gc': self.gcs[tile.region], }
import sys import pprint pprint.pprint(sys.path) import geom r = geom.Rectangle(2.0, 5.0) print(r.area)
export_path = sys.argv[1] yolo_path = sys.argv[2] LABEL_WIDTH = 1280 LABEL_HEIGHT = 720 examples = [] for fname in os.listdir(export_path): if not fname.endswith('_0.jpg'): continue label = fname.split('_0.jpg')[0] example_path = yolo_path + 'images/' + label rect = geom.Rectangle( geom.Point(0, 0), geom.Point(LABEL_WIDTH, LABEL_HEIGHT), ) with open(export_path+label+'_1.json', 'r') as f: detections = json.load(f)[0] objects = [geom.Point(d['left'], d['top']).bounds().add_tol(30) for d in detections] objects = [rect.clip_rect(obj_rect) for obj_rect in objects] crop_objects = [] for obj_rect in objects: start = geom.FPoint(float(obj_rect.start.x) / LABEL_WIDTH, float(obj_rect.start.y) / LABEL_HEIGHT) end = geom.FPoint(float(obj_rect.end.x) / LABEL_WIDTH, float(obj_rect.end.y) / LABEL_HEIGHT) crop_objects.append((start.add(end).scale(0.5), end.sub(start))) crop_lines = ['0 {} {} {} {}'.format(center.x, center.y, size.x, size.y) for center, size in crop_objects] subprocess.call(['cp', export_path+label+'_0.jpg', example_path+'.jpg']) with open(example_path+'.txt', 'w') as f: f.write("\n".join(crop_lines) + "\n")
import geom r = geom.Rectangle(10, 20) print(r.area)
print('reading tiles') tiles = tileloader.Tiles(PATHS_PER_TILE_AXIS, SEGMENT_LENGTH, 16, TILE_MODE) print('initializing model') model.BATCH_SIZE = 1 m = model.Model(tiles.num_input_channels()) session = tf.Session() m.saver.restore(session, model_path) node_list = [] # save all nodes already explored if EXISTING_GRAPH_FNAME is None: # read rect = geom.Rectangle(TILE_START, TILE_END) tile_data = tiles.get_tile_data(REGION, rect) # generate path list path_list = [] start_points = [] with open(corner_file, "r") as f: for line in f.readlines(): temp = line.strip().split(",") start_points.append([int(temp[0]), int(temp[1])]) graph_num = 0 # s_pt=start_points[4] s_pt = [3003, 2695] pos3_point = geom.Point(s_pt[0] - 4096,
best_path = MODEL_BASE + '/model_best00/model' if os.path.isfile(model_path + '.meta'): print('... loading existing model') print('... loading existing model', file=mylogs) m.saver.restore(session, model_path) else: print('... initializing a new model') print('... initializing a new model', file=mylogs) session.run(m.init_op) init = tf.global_variables_initializer() session.run(init) # initialize subtiles subtiles = [] for tile in tiles.train_tiles: big_rect = geom.Rectangle(tile.scale(1024), tile.add(geom.Point(1, 1)).scale(1024)) for offset in [geom.Point(0, 0)]: start = big_rect.start.add(offset) search_rect = geom.Rectangle(start, start.add(geom.Point(1024, 1024))) search_rect = search_rect.add_tol(-WINDOW_SIZE / 2) starting_locations = tiles.all_starting_locations['{}_{}_{}'.format( tile.region, tile.x, tile.y)] starting_locations = [ loc for loc in starting_locations if search_rect.add_tol(-WINDOW_SIZE / 2).contains(loc[0]['point']) ] if len(starting_locations) < 5: continue subtiles.append({
def setUp(self): self.rectangle = geom.Rectangle(2.0, 5.0)
def load(self, filename): self.load_errors = [] with open(filename) as f: point_list = (LIST, (FLOAT, FLOAT, FLOAT)) p = Parser({ 'Name': STRING, 'Element': STRING, 'Line': STRING, 'Xtal': (STRING, INT, INT, INT), 'Num Xtals': INT, 'Dispersive Direction': STRING, 'Energy Range': (FLOAT, FLOAT), 'Exit Aperture': point_list, 'Entrance Aperture': point_list, 'Xtals': point_list, 'Sample': point_list, 'Camera': point_list, 'Beam': point_list }) self.filename = filename info = p.parse(f.readlines()) self.load_errors += p.errors self.name = info.get('Name') self.element = info.get('Element') self.line = info.get('Line') self.energy_range = info.get('Energy Range') xtal = info.get('Xtal') if xtal: self.xtal_type = info.get('Xtal')[0] self.xtal_cut = info.get('Xtal')[1:4] if self.xtal_type in lattice_constants: d0 = lattice_constants[self.xtal_type] self.E0 = HC * norm(self.xtal_cut) / (2 * d0) else: self.E0 = None else: self.load_errors.append( 'Missing crystal type information ("Xtal" line).') exit_aperture = info.get('Exit Aperture') if exit_aperture: if len(exit_aperture) == 4: self.exit_aperture = [ geom.Point(*p) for p in exit_aperture ] else: self.load_errors.append( 'Aperture contains %d points instead of 4.' % len(self.exit_aperture)) else: self.load_errors.append("Missing Exit Aperture") entrance_aperture = info.get('Entrance Aperture') if entrance_aperture: if len(entrance_aperture) % 4 == 0: self.entrance_aperture = [[ geom.Point(*p) for p in entrance_aperture[4 * i:4 * (i + 1)] ] for i in range(len(entrance_aperture) / 4)] else: self.load_errors.append( 'Aperture contains %d points instead of 4.' % len(self.entrance_aperture)) else: self.load_errors.append('Missing Entrance Apertures') self.num_xtals = 0 xtals = info.get('Xtals') if xtals: if len(xtals) % 4 == 0: self.xtals = [[ geom.Point(*p) for p in xtals[4 * i:4 * (i + 1)] ] for i in range(len(xtals) / 4)] self.xtal_rects = [ geom.Rectangle(x[0], x[1], x[3]) for x in self.xtals ] self.num_xtals = len(self.xtals) else: self.load_errors.append( 'Xtal list contains %d points, which is not a multiple of 4.' % len(xtals)) else: self.load_errors.append('Missing analyzer crystal geometry.') num_xtals = info.get('Num Xtals') if num_xtals: if self.num_xtals > 0 and num_xtals != self.num_xtals: self.load_errors.append( 'Geometry specified for %d xtals, but \'Num Xtals\' key also included with value: %d.' % (self.num_xtals, num_xtals)) else: self.num_xtals = num_xtals dirname = info.get('Dispersive Direction') if dirname: if dirname in DIRECTION_NAMES: self.dispersive_direction = DIRECTION_NAMES.index(dirname) else: self.load_errors.append( "Unknown dispersive direction: %s. Should be one of 'Up', 'Down', 'Left' or 'Right'" ) sample = info.get('Sample') if sample: if len(sample) == 1: self.sample = geom.Point(*sample[0]) else: self.load_errors.append( 'One sample point must be specified, not %d.' % len(sample)) else: self.load_errors.append('Missing sample point.') camera = info.get('Camera') if camera: if len(camera) == 4: self.camera = [geom.Point(*p) for p in camera] self.camera_rect = geom.Rectangle(self.camera[0], self.camera[1], self.camera[3]) else: self.load_errors.append( 'Camera contains %d points instead of 4.' % len(self.camera)) else: self.load_errors.append('Missing camera geometry') beam = info.get('Beam') if beam: if len(beam) == 1: self.beam = geom.Point(*beam[0]) else: self.load_errors.append( 'One beam direction must be specified, not %d.' % len(beam)) else: self.load_errors.append('Beam direction not specified')