def save_tiles(self, path, rows=None, cols=None, overlap=16): if rows is None: rows = self.nrows if cols is None: cols = rows tiles = split_tiles(channels_first(self.data), (rows, cols), overlap) for i, tile in enumerate(tiles): fn = '{}_tile{:04}.npy'.format(path, i+1) np.save(fn, tile)
def rectify_building(image, meta=None): labels = pyfacades.models.driving_12x360x480.segment.process_strip( channels_first(image)) non_rectangular = building = np.argmax( labels, 0) == pyfacades.models.driving_12x360x480.model.BUILDING h = pyfacades.rectify.Homography(image, mask=non_rectangular) if meta is not None: meta['labels'] = labels meta['building'] = building meta['homography'] = h return h.rectified
def load_image(self, path): self.data = imread(path) self.path = path self._load_image_mask() self._mask_out_common_obstructions() self._rectify_image() self.driving_layers = driving.process_strip( channels_first(self.rectified * 255)) self.facade_layers = i12.process_strip( channels_first(self.rectified * 255)) self._create_sky_mask() self._segment_windows() self._segment_facade_edges() facade_cuts = self._split_at_facade_edges() facade_mask = self._create_facade_mask() wall_colors = self._mask_out_wall_colors(facade_mask) self.wall_colors = wall_colors self.facade_candidates = self._find_facade_candidates( wall_colors, facade_cuts)
def _mask_out_common_obstructions(self): """Mask out the sky and some common objects that can obstruct a facade This is intended to be run prior to rectifications, since these things can prevent us from correctly identifying the rectilinear features of a facade """ features = driving.process_strip( channels_first(img_as_ubyte(self.data))) if self.data_mask is not None: occlusions = ~self.data_mask else: occlusions = np.zeros(self.data.shape[:2], dtype=np.bool) occlusions |= driving.occlusion(features) self.data_mask = ~occlusions
def eval(): p = ArgumentParser(default_config_files=[join(dirname(__file__), '.eval.cfg')]) p.add('--config', '-c', is_config_file=True, help="config file path") p.add('--weights', type=str, help="path to the classifier weights") p.add('--data', type=str, help="test data, a file with each line as an .npy filename") p.add('--output', '-o', type=str, help="folder to hold outputs", default='.') p.add('--path', type=str, action='append', help='search paths for files') args = p.parse_args() try: os.makedirs(args.output) except OSError: pass # If weights is None, this will default to the weights in the models folder, # or the ones indicated by the environment variable net = i12.net(args.weights) files = [complete(p, args.path) for p in open(args.data).readlines()] summary = OrderedDict() for label in i12.LABELS: summary[label] = OrderedDict() summary[label].update(TP=0) summary[label].update(TN=0) summary[label].update(FP=0) summary[label].update(FN=0) summary[label].update(targets=0) summary[label].update(candidates=0) summary[label].update(hits=0) for file_index, file in enumerate(files): print(file_index+1, "of", len(files), ":", file) cached_file = os.path.join(args.output, "metrics-{:04}.yml".format(file_index)) data = np.load(file) rgb = channels_last(data[:3].astype(np.uint8)) if not os.path.isfile(os.path.join(args.output, 'file-{}.jpg'.format(file_index))): skimage.io.imsave(os.path.join(args.output, 'file-{}.jpg'.format(file_index)), rgb) all_expected = data[3:] if not os.path.isfile(os.path.join(args.output, 'file-{}-windows.jpg'.format(file_index))): skimage.io.imsave(os.path.join(args.output, 'file-{}-windows.jpg'.format(file_index)), skimage.color.gray2rgb((all_expected[ pyfacades.models.independant_12_layers.model.WINDOW] == 2).astype(float))) if not os.path.isfile(cached_file): print("Calculating metrics for file", file) #data = np.load(file) #rgb = channels_last(data[:3].astype(np.uint8)) #all_expected = data[3:] all_predicted = i12.process_strip(channels_first(rgb)) results = OrderedDict() for label_index, label in enumerate(i12.LABELS): if label == 'background': continue # Skip the top row and the bottom row -- I gave those dummy labels to work around a caffe error expected = all_expected[label_index][1:-1] # Uggh! Since the 'UNKNOWN' label is removed, argmax gives the wrong values; I need 0->0, 1->2, 2->3 predicted = all_predicted.features[label_index].argmax(0)[1:-1]+1 predicted[predicted==LABEL_UNKNOWN] = LABEL_NEGATIVE metrics = Metrics(expected, predicted, source=file, feature=label, threshold=0.5) results[label] = metrics.as_dict() viz = viz_pixel_labels(expected, predicted, rgb[1:-1], label_negative=LABEL_NEGATIVE, label_positive=LABEL_POSITIVE) skimage.io.imsave(os.path.join(args.output, 'file-{}-viz-{}.jpg'.format(file_index, label)), viz) with open(cached_file, 'w') as f: yaml.dump(results, f, Dumper=yamlordereddictloader.Dumper) cached = yaml.load(open(cached_file)) print("Cumulative:") for label in i12.LABELS: if label == 'background': continue metrics = Metrics(**cached[label]) assert metrics.source == file summary[label]['TP'] += metrics.TP summary[label]['FP'] += metrics.FP summary[label]['TN'] += metrics.TN summary[label]['FN'] += metrics.FN summary[label]['targets'] += metrics.targets summary[label]['candidates'] += metrics.candidates summary[label]['hits'] += metrics.hits cum = Metrics(**summary[label]) print("{:10}: pix(P:{:2.5f}, R:{:2.5f},F:{:2.5f}), obj:(P:{:2.5f}, R:{:2.5f},F:{:2.5f})".format( label, cum.pixel_precision, cum.pixel_recall, cum.pixel_f1, cum.object_precision, cum.object_recall, cum.object_f1))