def _make_data_gen(hypes, phase, data_dir): """Return a data generator that outputs image samples. @ Returns image: integer array of shape [width, height, 3]. Representing RGB value of each pixel. gt_image: boolean array of shape [width, height, num_classes]. Set `gt_image[i,j,k] == 1` if and only if pixel i,j is assigned class k. `gt_image[i,j,k] == 0` otherwise. [Alternativly make gt_image[i,j,*] a valid propability distribution.] """ if phase == 'train': data_file = hypes['data']["train_file"] elif phase == 'val': data_file = hypes['data']["val_file"] else: assert False, "Unknown Phase %s" % phase data_file = os.path.join(data_dir, data_file) data = _load_gt_file(hypes, data_file) for image, gt_image in data: if phase == 'val': yield image, utils.load_segmentation_mask(hypes, gt_image)[:, :, :-1] elif phase == 'train': image, gt_image = jitter_input(hypes, image, gt_image) yield image, utils.load_segmentation_mask(hypes, gt_image)[:, :, :-1] image, gt_image = jitter_input(hypes, np.fliplr(image), np.fliplr(gt_image)) yield image, utils.load_segmentation_mask(hypes, gt_image)[:, :, :-1]
def get_class_distribution(hypes, labeled_dataset): """ Get the distribution of classes in a labeled dataset. Parameters ---------- hypes : dict The hyperparameters have to specify 'classes'. labeled_dataset : list of dicts Each dict has to have the keys 'raw' and 'mask' which have the absolute path to image files. Returns ------- dict Mapping class indices according to hypes['classes'] to pixel counts. """ classes = {} for item in labeled_dataset: im = utils.load_segmentation_mask(hypes, item['mask']) for y in range(im.shape[0]): for x in range(im.shape[1]): cl = im[y][x] if cl in classes: classes[cl] += 1 else: classes[cl] = 1 return classes
def main(hypes_file, output_dir, override): """ Orchestrate. Parameters ---------- hypes_file : str Path to a JSON file output_dir : str Path where the output should be stored override : bool If True, then override the model if it exists. """ # Load hyperparameters with open(hypes_file, 'r') as f: hypes = json.load(f) # Set serialization path base = os.path.dirname(hypes_file) model_file_path = os.path.join(base, '%s.json' % hypes['model']['name']) model_file_path = os.path.abspath(model_file_path) if not os.path.isfile(model_file_path) or override: if not os.path.isfile(model_file_path): logging.info("Did not find '%s'. Start training...", model_file_path) else: logging.info("Override '%s'. Start training...", model_file_path) # Get training data x_files, y_files = get_file_list(hypes, 'train') # "Train" "classifier" (it just counts the classes) model = {} for i in range(len(hypes['classes'])): model[i] = 0 for y_file in y_files: logging.info("Read '%s'...", y_file) mask = load_segmentation_mask(hypes, y_file) for row in mask: for pixel in row: model[pixel] += 1 # save model as json file with open(model_file_path, 'w') as f: json.dump(model, f) else: # load model from json file with open(model_file_path) as f: model = json.load(f) # Evaluate data = get_file_list(hypes, 'test') analyze.evaluate(hypes, data, output_dir, model, elements=[0, 1], get_segmentation=get_segmentation)
def eval_image(hypes, gt_image, cnn_image): """.""" thresh = np.array(range(0, 256))/255.0 valid_gt = utils.load_segmentation_mask(hypes, gt_image)[:, :, :-1] return seg.evalExp(hypes, valid_gt, cnn_image)
def main(hypes_file, output_dir, override): """ Orchestrate. Parameters ---------- hypes_file : str Path to a JSON file output_dir : str Path where the output should be stored override : bool If True, then override the model if it exists. """ # Load hyperparameters with open(hypes_file, 'r') as f: hypes = json.load(f) # Set serialization path base = os.path.dirname(hypes_file) model_file_path = os.path.join(base, '%s.pickle' % hypes['model']['name']) model_file_path = os.path.abspath(model_file_path) if not os.path.isfile(model_file_path) or override: if not os.path.isfile(model_file_path): logging.info("Did not find '%s'. Start training...", model_file_path) else: logging.info("Override '%s'. Start training...", model_file_path) # Get training data x_files, y_files = get_file_list(hypes, 'train') # "Train" "classifier" (it just counts the classes) model = {'positions': None, 'files': 0} for y_file in y_files: logging.info("Read '%s'...", y_file) mask = load_segmentation_mask(hypes, y_file) if model['positions'] is None: model['positions'] = mask else: model['positions'] += mask model['files'] += 1 # save model as pickle file scipy.misc.imsave("instruments.png", model['positions']) with open(model_file_path, 'wb') as handle: pickle.dump(model, handle, protocol=pickle.HIGHEST_PROTOCOL) else: # load model from pickle file with open(model_file_path, 'rb') as handle: model = pickle.load(handle) # Evaluate data = get_file_list(hypes, 'test') analyze.evaluate(hypes, data, output_dir, model, elements=[0, 1], get_segmentation=get_segmentation)
def get_traindata_single_file(hypes, x, y): """Get trainingdata for a single file x with segmentation file y.""" xs, ys = [], [] logging.info("Read '%s' for data...", x) label = load_segmentation_mask(hypes, y) im = Image.open(x, 'RGB') width, height, _ = im.size for x in range(width): for y in range(height): image_val = get_features(hypes, im, 'data') label_val = label[y][x] xs.append(image_val) ys.append(label_val) return numpy.array(xs), numpy.array(ys, dtype=int)
def get_traindata_single_file(hypes, x, y): """Get trainingdata for a single file x with segmentation file y.""" xs, ys = [], [] logging.info("Read '%s' for data...", x) label = load_segmentation_mask(hypes, y) im = Image.open(x, 'RGB') width, height, _ = im.size for x in range(width): for y in range(height): image_val = get_features(hypes, im, 'data') label_val = label[y][x] xs.append(image_val) ys.append(label_val) return numpy.array(xs), numpy.array(ys, dtype=int)
def test_load_segmentation_mask(): """Test load_segmentation_mask.""" from tensorvision.utils import load_segmentation_mask import json import scipy.misc import numpy import os hypes_path = os.path.abspath('tensorvision/tests/croco.json') seg_image = os.path.abspath('tensorvision/tests/croco-mask.png') with open(hypes_path) as f: hypes = json.load(f) gt = load_segmentation_mask(hypes, seg_image) # scipy.misc.imsave("tensorvision/tests/test-generated-mask.png", gt) seg_image = 'tensorvision/tests/Crocodylus-johnsoni-3-mask.png' true_gt = (scipy.misc.imread(seg_image) / 255.).astype(int) numpy.testing.assert_array_equal(true_gt, gt) assert gt.shape == (480, 640)
def test_load_segmentation_mask(): """Test load_segmentation_mask.""" from tensorvision.utils import load_segmentation_mask import json import scipy.misc import numpy import os hypes_path = os.path.abspath('tensorvision/tests/croco.json') seg_image = os.path.abspath('tensorvision/tests/croco-mask.png') with open(hypes_path) as f: hypes = json.load(f) gt = load_segmentation_mask(hypes, seg_image) # scipy.misc.imsave("tensorvision/tests/test-generated-mask.png", gt) seg_image = 'tensorvision/tests/Crocodylus-johnsoni-3-mask.png' true_gt = (scipy.misc.imread(seg_image) / 255.).astype(int) numpy.testing.assert_array_equal(true_gt, gt) assert gt.shape == (480, 640)
def generate_batch(hypes, phase): """ Generate patches. Parameters ---------- hypes : dict phase : 'train' or 'test' """ x_files, y_files = get_file_list(hypes, phase) x_files, y_files = sklearn.utils.shuffle(x_files, y_files, random_state=0) batch_x, batch_y = [], [] while True: for x, y in zip(x_files, y_files): logging.info("Read '%s' for data...", x) image = get_image(x, 'RGB') label = load_segmentation_mask(hypes, y) im = Image.open(x, 'r') width, height = im.size image_vals = get_features(hypes, image, 'data') label_vals = get_features(hypes, label, 'label') # print("image_vals = %s" % str(list(image_vals))) for patch, label_ in zip(image_vals, label_vals): patch = img_to_array(patch) label_ = img_to_array(label_) _, w, h = label_.shape label_ = label_.reshape((w, h)) if phase == 'val' and 1.0 not in label_: print("continue") continue # scipy.misc.imshow(patch) # scipy.misc.imshow(label_) batch_x.append(patch) batch_y.append(label_) # .flatten() if len(batch_x) == hypes['solver']['batch_size']: yield (np.array(batch_x), np.array(batch_y)) batch_x, batch_y = [], []
def inputs(hypes, _, phase, data_dir): """ Get data. Parameters ---------- hypes : dict _ : ignore this phase : {'train', 'val'} data_dir : str Returns ------- tuple (xs, ys), where xs and ys are lists of the same length. xs are paths to the input images and ys are paths to the expected output """ x_files, y_files = get_file_list(hypes, 'train') x_files, y_files = sklearn.utils.shuffle(x_files, y_files, random_state=0) xs, ys = [], [] for x, y in zip(x_files, y_files): logging.info("Read '%s' for data...", x) image = get_image(x, 'RGB') label = load_segmentation_mask(hypes, y) im = Image.open(x, 'r') width, height = im.size for x in range(width): for y in range(height): image_val = get_features(x, y, image, hypes['model_nr']) label_val = label[y][x] xs.append(image_val) ys.append(label_val) return xs, np.array(ys, dtype=int)
def generate_batch(hypes, phase): """ Generate patches. Parameters ---------- hypes : dict phase : 'train' or 'test' """ x_files, y_files = get_file_list(hypes, phase) x_files, y_files = sklearn.utils.shuffle(x_files, y_files, random_state=0) batch_x, batch_y = [], [] while True: for x, y in zip(x_files, y_files): logging.info("Read '%s' for data...", x) image = get_image(x, 'RGB') label = load_segmentation_mask(hypes, y) im = Image.open(x, 'r') width, height = im.size image_vals = get_features(hypes, image, 'data') label_vals = get_features(hypes, label, 'label') # print("image_vals = %s" % str(list(image_vals))) for patch, label_ in zip(image_vals, label_vals): patch = img_to_array(patch) label_ = img_to_array(label_) _, w, h = label_.shape label_ = label_.reshape((w, h)) if phase == 'val' and 1.0 not in label_: print("continue") continue # scipy.misc.imshow(patch) # scipy.misc.imshow(label_) batch_x.append(patch) batch_y.append(label_) # .flatten() if len(batch_x) == hypes['solver']['batch_size']: yield (np.array(batch_x), np.array(batch_y)) batch_x, batch_y = [], []
def main(hypes_file, output_dir, override): """ Orchestrate. Parameters ---------- hypes_file : str Path to a JSON file output_dir : str Path where the output should be stored override : bool If True, then override the model if it exists. """ # Load hyperparameters with open(hypes_file, 'r') as f: hypes = json.load(f) # Set serialization path base = os.path.dirname(hypes_file) model_file_path = os.path.join(base, '%s.pickle' % hypes['model']['name']) model_file_path = os.path.abspath(model_file_path) if not os.path.isfile(model_file_path) or override: if not os.path.isfile(model_file_path): logging.info("Did not find '%s'. Start training...", model_file_path) else: logging.info("Override '%s'. Start training...", model_file_path) # Get training data x_files, y_files = get_file_list(hypes, 'train') # "Train" "classifier" (it just counts the classes) model = {'positions': None, 'files': 0} for y_file in y_files: logging.info("Read '%s'...", y_file) mask = load_segmentation_mask(hypes, y_file) if model['positions'] is None: model['positions'] = mask else: model['positions'] += mask model['files'] += 1 # save model as pickle file scipy.misc.imsave("instruments.png", model['positions']) with open(model_file_path, 'wb') as handle: pickle.dump(model, handle, protocol=pickle.HIGHEST_PROTOCOL) else: # load model from pickle file with open(model_file_path, 'rb') as handle: model = pickle.load(handle) # Evaluate data = get_file_list(hypes, 'test') analyze.evaluate(hypes, data, output_dir, model, elements=[0, 1], get_segmentation=get_segmentation)
def evaluate(hypes, data, out_dir, model, elements, get_segmentation, verbose=True): """ Analyze how well a model does on given data. Parameters ---------- hypes : dict Hyperparameters (model specific information) data : tuple (x_files, y_files) where x_files and y_files are lists of strings out_dir : str Path to the directory where the output gets stored model : object elements : iterable A list / set or another iterable which contains the possible segmentation classes (commonly 0 and 1) get_segmentation : function Takes a string and a model the model as input and returns a numpy array. The string is the path to an image file. load_label_seg : function Takes a path as a string and returns a segmentation mask as a numpy array. verbose : bool Print messages when in verbose mode. """ # Initialize confusion matrix cm = {} for i in elements: cm[i] = {} for j in elements: cm[i][j] = 0 # Initialize timings for segmentation times = [] i = 1 for xfile, yfile in zip(*data): if verbose: logging.info("Segmentation of '%s'...", xfile) logging.info(" label='%s'...", yfile) t0 = time.time() segmentation = get_segmentation(hypes, xfile, model) t1 = time.time() times.append(t1 - t0) correct_seg = load_segmentation_mask(hypes, yfile) cm_tmp = get_confusion_matrix(correct_seg, segmentation, elements) color_changes = get_color_changes_dict(hypes) gen_img_and_overlay(xfile, segmentation, out_dir, color_changes, i) cm = merge_cms(cm, cm_tmp) if verbose: show_metrics(cm, indentation=4) print(" time: %0.4fs" % (t1 - t0)) i += 1 if verbose: show_metrics(cm) print("Average time: %0.4f" % (sum(times) / len(times))) return {'cm': cm, 'times': times}