def compute_accuracy( pred_dir: str, input_fn: str, classes: int = 5, hr_label_key: str = "data/cheaseapeake_to_hr_labels.txt", lr_label_key: str = "data/nlcd_to_lr_labels.txt", ): data_dir = os.path.dirname(input_fn) logger.info("Starting %s at %s" % ("Accuracy computing script", str(datetime.datetime.now()))) try: df = pd.read_csv(input_fn) fns = df[["naip-new_fn", "lc_fn", "nlcd_fn"]].values except Exception as e: logger.error("Could not load the input file") logger.error(e) return cm = np.zeros(( classes - 1, classes - 1, ), dtype=np.float32) cm_dev = np.zeros(( classes - 1, classes - 1, ), dtype=np.float32) acc_sum = 1e-6 acc_num = 1e-6 for i in range(len(fns)): naip_fn = os.path.join(data_dir, fns[i][0]) lc_fn = os.path.join(data_dir, fns[i][1]) nlcd_fn = os.path.join(data_dir, fns[i][2]) pred_fn = os.path.join(pred_dir, os.path.basename(naip_fn)[:-4] + "_class.tif") pred_f = rasterio.open(pred_fn, "r") pred = pred_f.read() pred_f.close() lc_f = rasterio.open(lc_fn, "r") lc = lc_f.read() lc_f.close() nlcd_f = rasterio.open(nlcd_fn, "r") nlcd = nlcd_f.read() nlcd_f.close() nlcd = nlcd.squeeze().astype(int) if lr_label_key: nlcd = handle_labels(nlcd, lr_label_key) lc = np.squeeze(lc).astype(int) pred = np.squeeze(pred).astype(int) if hr_label_key: lc = handle_labels(lc, hr_label_key) pred = handle_labels(pred, hr_label_key) roi = (lc > 0) & (pred > 0) roi_dev = (lc > 0) & (pred > 0) & (nlcd >= 21) & (nlcd <= 24) if np.sum(roi) > 0: if np.sum(roi_dev) > 0: cm_dev += get_confusion_matrix(lc[roi_dev > 0].flatten(), pred[roi_dev > 0].flatten(), classes) cm += get_confusion_matrix(lc[roi > 0].flatten(), pred[roi > 0].flatten(), classes) accuracy = np.sum(lc[roi > 0] == pred[roi > 0]) / np.sum(roi) acc_sum += np.sum(lc[roi > 0] == pred[roi > 0]) acc_num += np.sum(roi) else: accuracy = -1 logger.info("Accuracy %f %s\t%d/%d %f" % (accuracy, lc_fn, i + 1, len(fns), acc_sum / acc_num)) return acc_sum / acc_num, cm, cm_dev
def __getitem__(self, index): """Generate one batch of data""" indices = self.indices[index * self.batch_size:(index + 1) * self.batch_size] fns = [self.patches[i] for i in indices] x_batch = np.zeros( (self.batch_size, self.input_size, self.input_size, self.num_channels), dtype=np.float32, ) y_hr_batch = np.zeros( (self.batch_size, self.output_size, self.output_size, self.num_classes), dtype=np.float32, ) y_sr_batch = None if self.do_superres: y_sr_batch = np.zeros( ( self.batch_size, self.output_size, self.output_size, self.lr_num_classes, ), dtype=np.float32, ) for i, (fn, state) in enumerate(fns): if fn.endswith(".npz"): dl = np.load(fn) data = dl["arr_0"].squeeze() dl.close() elif fn.endswith(".npy"): data = np.load(fn).squeeze() data = np.rollaxis(data, 0, 3) # do a random crop if input_size is less than the prescribed size assert data.shape[0] == data.shape[1] data_size = data.shape[0] if self.input_size < data_size: x_idx = np.random.randint(0, data_size - self.input_size) y_idx = np.random.randint(0, data_size - self.input_size) data = data[y_idx:y_idx + self.input_size, x_idx:x_idx + self.input_size, :] x_batch[i] = to_float(data[:, :, :self.num_channels], self.data_type) # setup x if self.do_color_aug: x_batch[i] = color_aug(x_batch[i]) # setup y_highres if self.hr_label_key: y_train_hr = handle_labels(data[:, :, self.hr_labels_index], self.hr_label_key) else: y_train_hr = data[:, :, self.hr_labels_index] y_train_hr = keras.utils.to_categorical(y_train_hr, self.num_classes) if self.do_superres: if state in self.superres_only_states: y_train_hr[:, :, 0] = 0 else: y_train_hr[:, :, 0] = 1 else: y_train_hr[:, :, 0] = 0 y_hr_batch[i] = y_train_hr # setup y_superres if self.do_superres: if self.lr_label_key: y_train_nlcd = handle_labels( data[:, :, self.lr_labels_index], self.lr_label_key) else: y_train_nlcd = data[:, :, self.lr_labels_index] y_train_nlcd = keras.utils.to_categorical( y_train_nlcd, self.lr_num_classes) y_sr_batch[i] = y_train_nlcd if self.do_superres: return x_batch.copy(), { "outputs_hr": y_hr_batch, "outputs_sr": y_sr_batch } else: return x_batch.copy(), y_hr_batch