def __init__(self, batch_size, input_shape, json_path, classes, num_parallel_calls=2, prefetch_size=2): self.batch_size = batch_size self.input_shape = input_shape self.json_path = json_path self.num_parallel_calls = num_parallel_calls self.prefetch_size = prefetch_size ObjectDetectorJson.init_cache(self.json_path, cache_type='NONE', classes=classes) dataset, self.dataset_size = ObjectDetectorJson.create_dataset( self.json_path, classes=classes) _, self.transform_param = MobileNetSSD.create_transform_parameters( *input_shape[:2]) self.transformer = AnnotatedDataTransformer(self.transform_param, is_training=False) print('Total evaluation steps: {}'.format( math.ceil(self.dataset_size / self.batch_size))) transform_fn = lambda value: ObjectDetectorJson.transform_fn( value, self.transformer) map_fn = lambda value: tf.py_func(transform_fn, [value], (tf.float32, tf.string)) self.dataset = dataset.map(map_fn, num_parallel_calls=num_parallel_calls) self.dataset = self.dataset.batch( self.batch_size).prefetch(prefetch_size)
def __init__(self, batch_size, input_shape, json_path, cache_type='NONE', classes=['bg'], fill_with_current_image_mean=True, num_parallel_calls=4, prefetch_size=16): self.batch_size = batch_size self.input_shape = input_shape self.json_path = json_path self.cache_type = cache_type self.num_parallel_calls = num_parallel_calls self.prefetch_size = prefetch_size self.classes = classes ObjectDetectorJson.init_cache(self.json_path, cache_type, classes=classes) self.train_dataset, self.dataset_size = ObjectDetectorJson.create_dataset( self.json_path, classes) self.train_transform_param, _ = MobileNetSSD.create_transform_parameters( input_shape[0], input_shape[1], fill_with_current_image_mean) self.train_transformer = AnnotatedDataTransformer( self.train_transform_param, is_training=True)
def __init__(self, input_type, input): self.input_type = input_type self.item_counter = 0 if input_type == 'json': coco_annotation = COCO(input) annotation_directory = os.path.join(os.getcwd(), os.path.dirname(input)) classes = ObjectDetectorJson.get_classes_from_coco_annotation(input) self.json_data = ObjectDetectorJson.convert_coco_to_toolbox_format(coco_annotation, classes, annotation_directory) if input_type == "video": self.cap = cv2.VideoCapture(input) if input_type == "cam": self.cap = cv2.VideoCapture(0)
def predict_on_json(predictions, annotation_path, classes, show=False, conf_threshold=None, dump_output_video=False, path_to_output_video='output.avi', width=640, height=480, fps=30): annotation_generator, _ = ObjectDetectorJson.json_iterator(annotation_path, classes) annotation_data = [pickle.loads(x) for x in annotation_generator()] output = [] if dump_output_video: fourcc = cv2.VideoWriter_fourcc(*'XVID') out = cv2.VideoWriter(path_to_output_video, fourcc, fps, (int(width), int(height))) for i, pred in enumerate(predictions): annotation = annotation_data[i] image_size = annotation['image_size'] img_path = annotation['image'] img_id = annotation['image_id'] det = process_image(pred[:, 1:], image_size, img_id, conf_threshold, classes) output.extend(det) frame = cv2.imread(img_path) frame = cv2.resize(frame, tuple(image_size)) img = draw_detections(frame, det) if show: cv2.imshow('detections', img) key = cv2.waitKey(10) if key == 27: break if dump_output_video: img_resized = cv2.resize(img, (width, height)) out.write(img_resized) if dump_output_video: out.release() return output
def input_fn(self): transform_fn = lambda value: ObjectDetectorJson.transform_fn( value, self.train_transformer, cache_type=self.cache_type) def transform_batch_fn(value): images = [] annotations = [] for val in value: img, annot = transform_fn(val) images.append(img) annotations.append(annot) return images, annotations map_fn_batch = lambda value: tf.py_func(transform_batch_fn, [value], (tf.float32, tf.string)) dataset = self.train_dataset.apply( tf.contrib.data.shuffle_and_repeat(buffer_size=self.dataset_size)) dataset = dataset.batch(self.batch_size).map( map_fn_batch, num_parallel_calls=self.num_parallel_calls) dataset = dataset.prefetch(self.prefetch_size) images, annotation = dataset.make_one_shot_iterator().get_next() images.set_shape([self.batch_size] + list(self.input_shape)) return images, annotation
def sample_data(json_path, num_samples, input_shape, classes, seed=666): if num_samples == 0: return None data, _ = ObjectDetectorJson.json_iterator(json_path, classes) data = [x for x in data()] # data = ObjectDetectorJson.convert_coco_to_toolbox_format(COCO(json_path), classes) ObjectDetectorJson.init_cache(json_path, cache_type='NONE', classes=classes) rng = random.Random(seed) selected_items = rng.sample(range(len(data)), num_samples) _, transform_param = MobileNetSSD.create_transform_parameters(*input_shape[:2]) transformer = AnnotatedDataTransformer(transform_param, is_training=False) transform_fn = lambda value: ObjectDetectorJson.transform_fn(value, transformer, add_original_image=True) return [transform_fn(data[i]) for i in selected_items]
def calc_coco_metrics(coco_annotations, predictions, classes): annotations = ObjectDetectorJson.convert_coco_to_toolbox_format( coco_annotations, classes) detections = [] for annotation, prediction in zip(annotations, predictions): width, height = annotation['image_size'] image_id = annotation['image_id'] for obj_id, obj in enumerate(prediction): label = int(obj[1]) score = float(obj[2]) if obj_id != 0 and score == 0: # At least one prediction must be (COCO API issue) continue bbox = (obj[3:]).tolist() bbox[::2] = [width * i for i in bbox[::2]] bbox[1::2] = [height * i for i in bbox[1::2]] xmin, ymin, xmax, ymax = bbox w_bbox = round(xmax - xmin, 1) h_bbox = round(ymax - ymin, 1) xmin, ymin = round(xmin, 1), round(ymin, 1) coco_det = {} coco_det['image_id'] = image_id coco_det['category_id'] = label coco_det['bbox'] = [xmin, ymin, w_bbox, h_bbox] coco_det['score'] = score detections.append(coco_det) coco_dt = coco_annotations.loadRes(detections) img_ids = sorted(coco_annotations.getImgIds()) coco_eval = COCOeval(coco_annotations, coco_dt, 'bbox') coco_eval.params.imgIds = img_ids coco_eval.evaluate() coco_eval.accumulate() coco_eval.summarize() metrics = {} for metric_name, value in zip(METRICS_NAMES, coco_eval.stats): metrics[metric_name] = value return metrics
def predict_on_json(predictions, annotation_path, classes, show=False, conf_threshold=None, dump_output_video=False, path_to_output_video='output.avi', width=640, height=480, fps=30): annotation_generator, _ = ObjectDetectorJson.json_iterator( annotation_path, classes) annotation_data = [pickle.loads(x) for x in annotation_generator()] output = [] if dump_output_video: fourcc = cv2.VideoWriter_fourcc(*'XVID') out = cv2.VideoWriter(path_to_output_video, fourcc, fps, (int(width), int(height))) for i, pred in enumerate(predictions): annotation = annotation_data[i] image_size = annotation['image_size'] img_path = annotation['image'] img_id = annotation['image_id'] det = process_image(pred[:, 1:], image_size, img_id, conf_threshold, classes) output.extend(det) frame = cv2.imread(img_path) frame = cv2.resize(frame, tuple(image_size)) img = draw_detections(frame, det) if show: cv2.imshow('detections', img) key = cv2.waitKey(10) if key == 27: break if dump_output_video: img_resized = cv2.resize(img, (width, height)) out.write(img_resized) if dump_output_video: out.release() return output
def calc_coco_metrics(coco_annotations, predictions, classes): annotations = ObjectDetectorJson.convert_coco_to_toolbox_format(coco_annotations, classes) detections = [] for annotation, prediction in zip(annotations, predictions): width, height = annotation['image_size'] image_id = annotation['image_id'] for obj_id, obj in enumerate(prediction): label = int(obj[1]) score = float(obj[2]) if obj_id != 0 and score == 0: # At least one prediction must be (COCO API issue) continue bbox = (obj[3:]).tolist() bbox[::2] = [width * i for i in bbox[::2]] bbox[1::2] = [height * i for i in bbox[1::2]] xmin, ymin, xmax, ymax = bbox w_bbox = round(xmax - xmin, 1) h_bbox = round(ymax - ymin, 1) xmin, ymin = round(xmin, 1), round(ymin, 1) coco_det = {} coco_det['image_id'] = image_id coco_det['category_id'] = label coco_det['bbox'] = [xmin, ymin, w_bbox, h_bbox] coco_det['score'] = score detections.append(coco_det) coco_dt = coco_annotations.loadRes(detections) img_ids = sorted(coco_annotations.getImgIds()) coco_eval = COCOeval(coco_annotations, coco_dt, 'bbox') coco_eval.params.imgIds = img_ids coco_eval.evaluate() coco_eval.accumulate() coco_eval.summarize() metrics = {} for metric_name, value in zip(METRICS_NAMES, coco_eval.stats): metrics[metric_name] = value return metrics
batch_size = 32 # Number of images in the batch class execution: CUDA_VISIBLE_DEVICES = "0" # Environment variable to control cuda device used for training per_process_gpu_memory_fraction = 0.5 # Fix extra memory allocation issue allow_growth = True # Option which attempts to allocate only as much GPU memory based on runtime allocations intra_op_parallelism_threads = 2 inter_op_parallelism_threads = 8 transformer_parallel_calls = 4 # Number of parallel threads in data transformer/augmentation transformer_prefetch_size = 8 # Number of batches to prefetch input_shape = (256, 256, 3 ) # Input shape of the model (width, height, channels) classes = ObjectDetectorJson.get_classes_from_coco_annotation( os.path.join(current_dir, train.annotation_path)) model_dir = os.path.join( os.path.dirname(os.path.abspath(__file__)), 'model' ) # Path to the folder where all training and evaluation artifacts will be located if not os.path.exists(model_dir): os.makedirs(model_dir) def learning_rate_schedule( ): # Function which controls learning rate during training import tensorflow as tf step = tf.train.get_or_create_global_step() lr = tf.case([(tf.less(step, 1000), lambda: tf.constant(0.0004)), (tf.less(step, 10000), lambda: tf.constant(0.01)), (tf.less(step, 40000), lambda: tf.constant(0.005)), (tf.less(step, 55000), lambda: tf.constant(0.0005)),
transformer_prefetch_size = 1 class infer: batch_size = 1 out_subdir = "predictions" class execution: CUDA_VISIBLE_DEVICES = "" intra_op_parallelism_threads = 1 inter_op_parallelism_threads = 1 transformer_parallel_calls = 1 transformer_prefetch_size = 1 classes = ObjectDetectorJson.get_classes_from_coco_annotation(train.annotation_path) input_shape = [128, 128, 3] def optimizer(learning_rate): import tensorflow as tf optimizer = tf.train.MomentumOptimizer(learning_rate=learning_rate, momentum=0.95) return optimizer detector_params = { "data_format": "NHWC", "depth_multiplier": 0.35, "initial_weights_path": path.join(root_dir, "data/test/model_ckpt/model.ckpt"), "learning_rate": 0.001, "mobilenet_version": "v2",
class infer: batch_size = 1 out_subdir = "predictions" class execution: CUDA_VISIBLE_DEVICES = "" intra_op_parallelism_threads = 1 inter_op_parallelism_threads = 1 transformer_parallel_calls = 1 transformer_prefetch_size = 1 classes = ObjectDetectorJson.get_classes_from_coco_annotation( train.annotation_path) input_shape = [128, 128, 3] def optimizer(learning_rate): import tensorflow as tf optimizer = tf.train.MomentumOptimizer(learning_rate=learning_rate, momentum=0.95) return optimizer detector_params = { "data_format": "NHWC", "depth_multiplier": 0.35, "initial_weights_path": path.join(root_dir, "data/test/model_ckpt/model.ckpt"),
out_subdir = "predictions" # Name of folder in model directory where output json files with detections will be saved batch_size = 32 # Number of images in the batch class execution: CUDA_VISIBLE_DEVICES = "0" # Environment variable to control cuda device used for training per_process_gpu_memory_fraction = 0.5 # Fix extra memory allocation issue allow_growth = True # Option which attempts to allocate only as much GPU memory based on runtime allocations intra_op_parallelism_threads = 2 inter_op_parallelism_threads = 8 transformer_parallel_calls = 4 # Number of parallel threads in data transformer/augmentation transformer_prefetch_size = 8 # Number of batches to prefetch input_shape = (256, 256, 3) # Input shape of the model (width, height, channels) classes = ObjectDetectorJson.get_classes_from_coco_annotation(os.path.join(current_dir, train.annotation_path)) model_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'model') # Path to the folder where all training and evaluation artifacts will be located if not os.path.exists(model_dir): os.makedirs(model_dir) def learning_rate_schedule(): # Function which controls learning rate during training import tensorflow as tf step = tf.train.get_or_create_global_step() lr = tf.case([(tf.less(step, 1000), lambda: tf.constant(0.0004)), (tf.less(step, 10000), lambda: tf.constant(0.01)), (tf.less(step, 40000), lambda: tf.constant(0.005)), (tf.less(step, 55000), lambda: tf.constant(0.0005)), (tf.less(step, 65000), lambda: tf.constant(0.00005))]) return lr
batch_size = 32 # Number of images in the batch class execution: CUDA_VISIBLE_DEVICES = "0" # Environment variable to control cuda device used for training per_process_gpu_memory_fraction = 0.5 # Fix extra memory allocation issue allow_growth = True # Option which attempts to allocate only as much GPU memory based on runtime allocations intra_op_parallelism_threads = 2 inter_op_parallelism_threads = 8 transformer_parallel_calls = 4 # Number of parallel threads in data transformer/augmentation transformer_prefetch_size = 8 # Number of batches to prefetch input_shape = (256, 256, 3 ) # Input shape of the model (width, height, channels) classes = ObjectDetectorJson.get_classes_from_coco_annotation( os.path.join(CURRENT_DIR, train.annotation_path)) MODEL_DIR = os.path.join( os.path.dirname(os.path.abspath(__file__)), 'model' ) # Path to the folder where all training and evaluation artifacts will be located if not os.path.exists(MODEL_DIR): os.makedirs(MODEL_DIR) def learning_rate_schedule( ): # Function which controls learning rate during training import tensorflow as tf step = tf.train.get_or_create_global_step() lr = tf.case([(tf.less(step, 1000), lambda: tf.constant(0.0004)), (tf.less(step, 10000), lambda: tf.constant(0.01)), (tf.less(step, 40000), lambda: tf.constant(0.005)), (tf.less(step, 55000), lambda: tf.constant(0.0005)),