Reordered functions in cli module
Signed-off-by: Jim Martens <github@2martens.de>
This commit is contained in:
@ -40,6 +40,21 @@ def config(args: argparse.Namespace) -> None:
|
||||
conf.list_property_values()
|
||||
|
||||
|
||||
def prepare(args: argparse.Namespace) -> None:
|
||||
import pickle
|
||||
|
||||
from twomartens.masterthesis import data
|
||||
|
||||
file_names_photos, file_names_instances, instances = data.prepare_scenenet_data(args.scenenet_path,
|
||||
args.protobuf_path)
|
||||
with open(f"{args.ground_truth_path}/photo_paths.bin", "wb") as file:
|
||||
pickle.dump(file_names_photos, file)
|
||||
with open(f"{args.ground_truth_path}/instance_paths.bin", "wb") as file:
|
||||
pickle.dump(file_names_instances, file)
|
||||
with open(f"{args.ground_truth_path}/instances.bin", "wb") as file:
|
||||
pickle.dump(instances, file)
|
||||
|
||||
|
||||
def train(args: argparse.Namespace) -> None:
|
||||
if args.network == "ssd" or args.network == "bayesian_ssd":
|
||||
_ssd_train(args)
|
||||
@ -47,6 +62,96 @@ def train(args: argparse.Namespace) -> None:
|
||||
_auto_encoder_train(args)
|
||||
|
||||
|
||||
def test(args: argparse.Namespace) -> None:
|
||||
if args.network == "ssd" or args.network == "bayesian_ssd":
|
||||
_ssd_test(args)
|
||||
elif args.network == "auto_encoder":
|
||||
_auto_encoder_test(args)
|
||||
|
||||
|
||||
def evaluate(args: argparse.Namespace) -> None:
|
||||
if args.network == "ssd":
|
||||
_ssd_evaluate(args)
|
||||
else:
|
||||
raise NotImplementedError
|
||||
|
||||
|
||||
def visualise(args: argparse.Namespace) -> None:
|
||||
import pickle
|
||||
|
||||
from matplotlib import pyplot
|
||||
import numpy as np
|
||||
from PIL import Image
|
||||
|
||||
from twomartens.masterthesis.ssd_keras.eval_utils import coco_utils
|
||||
|
||||
with open(f"{args.ground_truth_path}/photo_paths.bin", "rb") as file:
|
||||
file_names = pickle.load(file)
|
||||
with open(f"{args.ground_truth_path}/instances.bin", "rb") as file:
|
||||
instances = pickle.load(file)
|
||||
|
||||
output_path = f"{args.output_path}/visualise/{args.trajectory}"
|
||||
annotation_file_train = f"{args.coco_path}/annotations/instances_train2014.json"
|
||||
cats_to_classes, _, cats_to_names, _ = coco_utils.get_coco_category_maps(annotation_file_train)
|
||||
|
||||
colors = pyplot.cm.hsv(np.linspace(0, 1, 81)).tolist()
|
||||
|
||||
i = 0
|
||||
nr_images = len(file_names[args.trajectory])
|
||||
nr_digits = math.ceil(math.log10(nr_images))
|
||||
for file_name, labels in zip(file_names[args.trajectory], instances[args.trajectory]):
|
||||
if not labels:
|
||||
continue
|
||||
|
||||
# only loop through selected trajectory
|
||||
with Image.open(file_name) as image:
|
||||
figure = pyplot.figure(figsize=(20, 12))
|
||||
pyplot.imshow(image)
|
||||
|
||||
current_axis = pyplot.gca()
|
||||
|
||||
for instance in labels:
|
||||
bbox = instance['bbox']
|
||||
# Transform the predicted bounding boxes for the 300x300 image to the original image dimensions.
|
||||
xmin = bbox[0]
|
||||
ymin = bbox[1]
|
||||
xmax = bbox[2]
|
||||
ymax = bbox[3]
|
||||
color = colors[cats_to_classes[int(instance['coco_id'])]]
|
||||
label = f"{cats_to_names[int(instance['coco_id'])]}: {instance['wordnet_class_name']}, " \
|
||||
f"{instance['wordnet_id']}"
|
||||
current_axis.add_patch(
|
||||
pyplot.Rectangle((xmin, ymin), xmax - xmin, ymax - ymin, color=color, fill=False, linewidth=2))
|
||||
current_axis.text(xmin, ymin, label, size='x-large', color='white',
|
||||
bbox={'facecolor': color, 'alpha': 1.0})
|
||||
pyplot.savefig(f"{output_path}/{str(i).zfill(nr_digits)}")
|
||||
pyplot.close(figure)
|
||||
|
||||
i += 1
|
||||
|
||||
|
||||
def measure_mapping(args: argparse.Namespace) -> None:
|
||||
import pickle
|
||||
|
||||
from twomartens.masterthesis.ssd_keras.eval_utils import coco_utils
|
||||
|
||||
with open(f"{args.ground_truth_path}/instances.bin", "rb") as file:
|
||||
instances = pickle.load(file)
|
||||
|
||||
output_path = f"{args.output_path}/measure/{args.tarball_id}"
|
||||
annotation_file_train = f"{args.coco_path}/annotations/instances_train2014.json"
|
||||
cats_to_classes, _, _, _ = coco_utils.get_coco_category_maps(annotation_file_train)
|
||||
|
||||
for i, trajectory in enumerate(instances):
|
||||
counts = {cat_id: 0 for cat_id in cats_to_classes.keys()}
|
||||
for labels in trajectory:
|
||||
for instance in labels:
|
||||
counts[instance['coco_id']] += 1
|
||||
|
||||
with open(f"{output_path}/{i}.bin", "wb") as file:
|
||||
pickle.dump(counts, file)
|
||||
|
||||
|
||||
def _ssd_train(args: argparse.Namespace) -> None:
|
||||
import os
|
||||
import pickle
|
||||
@ -200,11 +305,87 @@ def _auto_encoder_train(args: argparse.Namespace) -> None:
|
||||
channels=3, train_epoch=args.num_epochs, batch_size=batch_size)
|
||||
|
||||
|
||||
def evaluate(args: argparse.Namespace) -> None:
|
||||
if args.network == "ssd":
|
||||
_ssd_evaluate(args)
|
||||
def _ssd_test(args: argparse.Namespace) -> None:
|
||||
import pickle
|
||||
import os
|
||||
|
||||
import tensorflow as tf
|
||||
|
||||
from twomartens.masterthesis import data
|
||||
from twomartens.masterthesis import ssd
|
||||
|
||||
config = tf.ConfigProto()
|
||||
config.log_device_placement = False
|
||||
config.gpu_options.allow_growth = False
|
||||
tf.enable_eager_execution(config=config)
|
||||
|
||||
batch_size = 16
|
||||
image_size = (300, 300)
|
||||
forward_passes_per_image = 10
|
||||
use_dropout = False if args.network == "ssd" else True
|
||||
|
||||
checkpoint_path = f"{args.weights_path}/train/{args.network}/{args.train_iteration}"
|
||||
model_file = f"{checkpoint_path}/ssd300.h5"
|
||||
output_path = f"{args.output_path}/val/{args.network}/{args.iteration}/"
|
||||
os.makedirs(output_path, exist_ok=True)
|
||||
|
||||
# load prepared ground truth
|
||||
with open(f"{args.ground_truth_path}/photo_paths.bin", "rb") as file:
|
||||
file_names_photos = pickle.load(file)
|
||||
with open(f"{args.ground_truth_path}/instances.bin", "rb") as file:
|
||||
instances = pickle.load(file)
|
||||
|
||||
# model
|
||||
ssd_model = tf.keras.models.load_model(model_file)
|
||||
|
||||
test_generator, length_dataset = \
|
||||
data.load_scenenet_data(file_names_photos, instances, args.coco_path,
|
||||
predictor_sizes=ssd_model.predictor_sizes,
|
||||
batch_size=batch_size,
|
||||
resized_shape=image_size,
|
||||
training=False, evaluation=True)
|
||||
del file_names_photos, instances
|
||||
|
||||
nr_digits = math.ceil(math.log10(math.ceil(length_dataset / batch_size)))
|
||||
steps_per_epoch = int(math.ceil(length_dataset / batch_size))
|
||||
ssd.predict_keras(test_generator,
|
||||
steps_per_epoch,
|
||||
ssd_model,
|
||||
use_dropout,
|
||||
forward_passes_per_image,
|
||||
image_size,
|
||||
output_path,
|
||||
nr_digits)
|
||||
|
||||
|
||||
def _auto_encoder_test(args: argparse.Namespace) -> None:
|
||||
from twomartens.masterthesis import data
|
||||
from twomartens.masterthesis.aae import run
|
||||
import tensorflow as tf
|
||||
from tensorflow.python.ops import summary_ops_v2
|
||||
|
||||
tf.enable_eager_execution()
|
||||
coco_path = args.coco_path
|
||||
category = args.category
|
||||
category_trained = args.category_trained
|
||||
batch_size = 16
|
||||
image_size = 256
|
||||
coco_data = data.load_coco_val(coco_path, category, num_epochs=1,
|
||||
batch_size=batch_size, resized_shape=(image_size, image_size))
|
||||
use_summary_writer = summary_ops_v2.create_file_writer(
|
||||
f"{args.summary_path}/val/category-{category}/{args.iteration}"
|
||||
)
|
||||
if args.debug:
|
||||
with use_summary_writer.as_default():
|
||||
run.run_simple(coco_data, iteration=args.iteration_trained,
|
||||
weights_prefix=f"{args.weights_path}/category-{category_trained}",
|
||||
zsize=16, verbose=args.verbose, channels=3, batch_size=batch_size,
|
||||
image_size=image_size)
|
||||
else:
|
||||
raise NotImplementedError
|
||||
run.run_simple(coco_data, iteration=args.iteration_trained,
|
||||
weights_prefix=f"{args.weights_path}/category-{category_trained}",
|
||||
zsize=16, verbose=args.verbose, channels=3, batch_size=batch_size,
|
||||
image_size=image_size)
|
||||
|
||||
|
||||
def _ssd_evaluate(args: argparse.Namespace) -> None:
|
||||
@ -294,183 +475,3 @@ def _ssd_evaluate(args: argparse.Namespace) -> None:
|
||||
|
||||
with open(result_file, "wb") as file:
|
||||
pickle.dump(results, file)
|
||||
|
||||
|
||||
def test(args: argparse.Namespace) -> None:
|
||||
if args.network == "ssd" or args.network == "bayesian_ssd":
|
||||
_ssd_test(args)
|
||||
elif args.network == "auto_encoder":
|
||||
_auto_encoder_test(args)
|
||||
|
||||
|
||||
def _ssd_test(args: argparse.Namespace) -> None:
|
||||
import pickle
|
||||
import os
|
||||
|
||||
import tensorflow as tf
|
||||
|
||||
from twomartens.masterthesis import data
|
||||
from twomartens.masterthesis import ssd
|
||||
|
||||
config = tf.ConfigProto()
|
||||
config.log_device_placement = False
|
||||
config.gpu_options.allow_growth = False
|
||||
tf.enable_eager_execution(config=config)
|
||||
|
||||
batch_size = 16
|
||||
image_size = (300, 300)
|
||||
forward_passes_per_image = 10
|
||||
use_dropout = False if args.network == "ssd" else True
|
||||
|
||||
checkpoint_path = f"{args.weights_path}/train/{args.network}/{args.train_iteration}"
|
||||
model_file = f"{checkpoint_path}/ssd300.h5"
|
||||
output_path = f"{args.output_path}/val/{args.network}/{args.iteration}/"
|
||||
os.makedirs(output_path, exist_ok=True)
|
||||
|
||||
# load prepared ground truth
|
||||
with open(f"{args.ground_truth_path}/photo_paths.bin", "rb") as file:
|
||||
file_names_photos = pickle.load(file)
|
||||
with open(f"{args.ground_truth_path}/instances.bin", "rb") as file:
|
||||
instances = pickle.load(file)
|
||||
|
||||
# model
|
||||
ssd_model = tf.keras.models.load_model(model_file)
|
||||
|
||||
test_generator, length_dataset = \
|
||||
data.load_scenenet_data(file_names_photos, instances, args.coco_path,
|
||||
predictor_sizes=ssd_model.predictor_sizes,
|
||||
batch_size=batch_size,
|
||||
resized_shape=image_size,
|
||||
training=False, evaluation=True)
|
||||
del file_names_photos, instances
|
||||
|
||||
nr_digits = math.ceil(math.log10(math.ceil(length_dataset / batch_size)))
|
||||
steps_per_epoch = int(math.ceil(length_dataset / batch_size))
|
||||
ssd.predict_keras(test_generator,
|
||||
steps_per_epoch,
|
||||
ssd_model,
|
||||
use_dropout,
|
||||
forward_passes_per_image,
|
||||
image_size,
|
||||
output_path,
|
||||
nr_digits)
|
||||
|
||||
|
||||
def _auto_encoder_test(args: argparse.Namespace) -> None:
|
||||
from twomartens.masterthesis import data
|
||||
from twomartens.masterthesis.aae import run
|
||||
import tensorflow as tf
|
||||
from tensorflow.python.ops import summary_ops_v2
|
||||
|
||||
tf.enable_eager_execution()
|
||||
coco_path = args.coco_path
|
||||
category = args.category
|
||||
category_trained = args.category_trained
|
||||
batch_size = 16
|
||||
image_size = 256
|
||||
coco_data = data.load_coco_val(coco_path, category, num_epochs=1,
|
||||
batch_size=batch_size, resized_shape=(image_size, image_size))
|
||||
use_summary_writer = summary_ops_v2.create_file_writer(
|
||||
f"{args.summary_path}/val/category-{category}/{args.iteration}"
|
||||
)
|
||||
if args.debug:
|
||||
with use_summary_writer.as_default():
|
||||
run.run_simple(coco_data, iteration=args.iteration_trained,
|
||||
weights_prefix=f"{args.weights_path}/category-{category_trained}",
|
||||
zsize=16, verbose=args.verbose, channels=3, batch_size=batch_size,
|
||||
image_size=image_size)
|
||||
else:
|
||||
run.run_simple(coco_data, iteration=args.iteration_trained,
|
||||
weights_prefix=f"{args.weights_path}/category-{category_trained}",
|
||||
zsize=16, verbose=args.verbose, channels=3, batch_size=batch_size,
|
||||
image_size=image_size)
|
||||
|
||||
|
||||
def prepare(args: argparse.Namespace) -> None:
|
||||
import pickle
|
||||
|
||||
from twomartens.masterthesis import data
|
||||
|
||||
file_names_photos, file_names_instances, instances = data.prepare_scenenet_data(args.scenenet_path,
|
||||
args.protobuf_path)
|
||||
with open(f"{args.ground_truth_path}/photo_paths.bin", "wb") as file:
|
||||
pickle.dump(file_names_photos, file)
|
||||
with open(f"{args.ground_truth_path}/instance_paths.bin", "wb") as file:
|
||||
pickle.dump(file_names_instances, file)
|
||||
with open(f"{args.ground_truth_path}/instances.bin", "wb") as file:
|
||||
pickle.dump(instances, file)
|
||||
|
||||
|
||||
def visualise(args: argparse.Namespace) -> None:
|
||||
import pickle
|
||||
|
||||
from matplotlib import pyplot
|
||||
import numpy as np
|
||||
from PIL import Image
|
||||
|
||||
from twomartens.masterthesis.ssd_keras.eval_utils import coco_utils
|
||||
|
||||
with open(f"{args.ground_truth_path}/photo_paths.bin", "rb") as file:
|
||||
file_names = pickle.load(file)
|
||||
with open(f"{args.ground_truth_path}/instances.bin", "rb") as file:
|
||||
instances = pickle.load(file)
|
||||
|
||||
output_path = f"{args.output_path}/visualise/{args.trajectory}"
|
||||
annotation_file_train = f"{args.coco_path}/annotations/instances_train2014.json"
|
||||
cats_to_classes, _, cats_to_names, _ = coco_utils.get_coco_category_maps(annotation_file_train)
|
||||
|
||||
colors = pyplot.cm.hsv(np.linspace(0, 1, 81)).tolist()
|
||||
|
||||
i = 0
|
||||
nr_images = len(file_names[args.trajectory])
|
||||
nr_digits = math.ceil(math.log10(nr_images))
|
||||
for file_name, labels in zip(file_names[args.trajectory], instances[args.trajectory]):
|
||||
if not labels:
|
||||
continue
|
||||
|
||||
# only loop through selected trajectory
|
||||
with Image.open(file_name) as image:
|
||||
figure = pyplot.figure(figsize=(20, 12))
|
||||
pyplot.imshow(image)
|
||||
|
||||
current_axis = pyplot.gca()
|
||||
|
||||
for instance in labels:
|
||||
bbox = instance['bbox']
|
||||
# Transform the predicted bounding boxes for the 300x300 image to the original image dimensions.
|
||||
xmin = bbox[0]
|
||||
ymin = bbox[1]
|
||||
xmax = bbox[2]
|
||||
ymax = bbox[3]
|
||||
color = colors[cats_to_classes[int(instance['coco_id'])]]
|
||||
label = f"{cats_to_names[int(instance['coco_id'])]}: {instance['wordnet_class_name']}, {instance['wordnet_id']}"
|
||||
current_axis.add_patch(
|
||||
pyplot.Rectangle((xmin, ymin), xmax - xmin, ymax - ymin, color=color, fill=False, linewidth=2))
|
||||
current_axis.text(xmin, ymin, label, size='x-large', color='white',
|
||||
bbox={'facecolor': color, 'alpha': 1.0})
|
||||
pyplot.savefig(f"{output_path}/{str(i).zfill(nr_digits)}")
|
||||
pyplot.close(figure)
|
||||
|
||||
i += 1
|
||||
|
||||
|
||||
def measure_mapping(args: argparse.Namespace) -> None:
|
||||
import pickle
|
||||
|
||||
from twomartens.masterthesis.ssd_keras.eval_utils import coco_utils
|
||||
|
||||
with open(f"{args.ground_truth_path}/instances.bin", "rb") as file:
|
||||
instances = pickle.load(file)
|
||||
|
||||
output_path = f"{args.output_path}/measure/{args.tarball_id}"
|
||||
annotation_file_train = f"{args.coco_path}/annotations/instances_train2014.json"
|
||||
cats_to_classes, _, _, _ = coco_utils.get_coco_category_maps(annotation_file_train)
|
||||
|
||||
for i, trajectory in enumerate(instances):
|
||||
counts = {cat_id: 0 for cat_id in cats_to_classes.keys()}
|
||||
for labels in trajectory:
|
||||
for instance in labels:
|
||||
counts[instance['coco_id']] += 1
|
||||
|
||||
with open(f"{output_path}/{i}.bin", "wb") as file:
|
||||
pickle.dump(counts, file)
|
||||
|
||||
Reference in New Issue
Block a user