Skip to content

Commit

Permalink
Applying BLACK formatting
Browse files Browse the repository at this point in the history
Reviewed By: zertosh

Differential Revision: D60771139

fbshipit-source-id: 4d814d0d42cc632a017e26b41a7c6141478a07ec
  • Loading branch information
Ritvij Saxena authored and facebook-github-bot committed Aug 6, 2024
1 parent d19cc89 commit 473ee74
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 84 deletions.
12 changes: 5 additions & 7 deletions flashlight/app/asr/tools/alignment/convertToAudacityLabels.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,16 @@
parser.add_argument(
"-f", "--alignment_file", help="alignment file", default="./alignment.txt"
)
parser.add_argument(
"-o", "--output_dir", help="output_dir", default="."
)
parser.add_argument("-o", "--output_dir", help="output_dir", default=".")
args = parser.parse_args()

with open(args.alignment_file, "r") as f:
for line in f:
(sampleId, segments) = line.split('\t')
segments = segments.split('\\n')
(sampleId, segments) = line.split("\t")
segments = segments.split("\\n")
output_file = os.path.join(args.output_dir, sampleId)
with open(output_file, "w") as ofile:
for segment in segments:
(_, _, start, duration, word) = segment.split(' ')
(_, _, start, duration, word) = segment.split(" ")
end = float(start) + float(duration)
ofile.write('{}\t{}\t{}\n'.format(start, end, word))
ofile.write("{}\t{}\t{}\n".format(start, end, word))
111 changes: 57 additions & 54 deletions flashlight/app/objdet/scripts/eval_coco.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,121 +6,124 @@
"""

import argparse
import arrayfire as af
import glob
import numpy as np
import os

import arrayfire as af
import numpy as np

from pycocotools.coco import COCO
from pycocotools.cocoeval import COCOeval


def convert_to_xywh(boxes):
xmin, ymin, xmax, ymax = np.split(boxes, 4, axis=1)
result = np.concatenate([xmin, ymin, xmax - xmin, ymax - ymin], axis=1);
result = np.concatenate([xmin, ymin, xmax - xmin, ymax - ymin], axis=1)
return result


def softmax(x, axis=0):
"""Compute softmax values for each sets of scores in x."""
max_value = np.max(x, axis=axis, keepdims=True);
max_value = np.max(x, axis=axis, keepdims=True)
e_x = np.exp(x - max_value)
s = np.sum(e_x, axis=axis, keepdims=True)
return e_x / s


def cxcywh_to_xyxy(x):
x_c, y_c, w, h = np.split(x, 4, 2)
b = [(x_c - 0.5 * w), (y_c - 0.5 * h),
(x_c + 0.5 * w), (y_c + 0.5 * h)]
b = [(x_c - 0.5 * w), (y_c - 0.5 * h), (x_c + 0.5 * w), (y_c + 0.5 * h)]
return np.concatenate(b, axis=2)


def postprocess_fn(out_logits, out_bbox, target_sizes):
assert len(out_logits) == len(target_sizes)
assert target_sizes.shape[1] == 2

prob = softmax(out_logits, 2)
labels = np.argmax(prob[..., :-1], axis=2);
labels = np.argmax(prob[..., :-1], axis=2)
scores = np.amax(prob[..., :-1], axis=2)
boxes = cxcywh_to_xyxy(out_bbox)

img_h, img_w = np.split(target_sizes, 2, axis=1)

scale_fct = np.concatenate([img_w, img_h, img_w, img_h], axis=1);
scale_fct = np.concatenate([img_w, img_h, img_w, img_h], axis=1)
boxes = boxes * scale_fct[:, None, :]
results = [{'scores': s, 'labels': l, 'boxes': b} for s, l, b in zip(scores, labels, boxes)]
return results;

results = [
{"scores": s, "labels": l, "boxes": b} for s, l, b in zip(scores, labels, boxes)
]
return results


def prepare_for_coco_detection(predictions):
coco_results = []
for original_id, prediction in predictions.items():
if len(prediction) == 0:
continue

boxes = prediction["boxes"]
boxes = convert_to_xywh(boxes).tolist()
scores = prediction["scores"].tolist()
labels = prediction["labels"].tolist()

coco_results.extend(
[
{
"image_id": original_id,
"category_id": labels[k],
"bbox": box,
"score": scores[k],
}
for k, box in enumerate(boxes)
]
)
return coco_results
coco_results = []
for original_id, prediction in predictions.items():
if len(prediction) == 0:
continue

boxes = prediction["boxes"]
boxes = convert_to_xywh(boxes).tolist()
scores = prediction["scores"].tolist()
labels = prediction["labels"].tolist()

coco_results.extend(
[
{
"image_id": original_id,
"category_id": labels[k],
"bbox": box,
"score": scores[k],
}
for k, box in enumerate(boxes)
]
)
return coco_results


def main(directory, coco_data):
data_type='val2017'
ann_file='{}/annotations/instances_{}.json'.format(coco_data, data_type)
coco=COCO(ann_file)

data_type = "val2017"
ann_file = "{}/annotations/instances_{}.json".format(coco_data, data_type)
coco = COCO(ann_file)

all_results = []
all_image_ids = []

glob_path = os.path.join(directory, '**', 'detection*.array')
glob_path = os.path.join(directory, "**", "detection*.array")
files = glob.glob(glob_path)
assert(len(files) > 0)
assert len(files) > 0
for f in files:

image_sizes = af.read_array(f, key='imageSizes').to_ndarray()
image_sizes = af.read_array(f, key="imageSizes").to_ndarray()
image_sizes = np.transpose(image_sizes, (1, 0))
image_ids = af.read_array(f, key='imageIds').to_ndarray()
scores = af.read_array(f, key='scores').to_ndarray()
image_ids = af.read_array(f, key="imageIds").to_ndarray()
scores = af.read_array(f, key="scores").to_ndarray()
scores = np.transpose(scores, (2, 1, 0))
bboxes = af.read_array(f, key='bboxes').to_ndarray()
bboxes = af.read_array(f, key="bboxes").to_ndarray()
bboxes = np.transpose(bboxes, (2, 1, 0))

results = postprocess_fn(scores, bboxes, image_sizes)

res = { id : output for id, output in zip(image_ids, results) };
res = {id: output for id, output in zip(image_ids, results)}
results = prepare_for_coco_detection(res)


image_ids = [ id for id in image_ids ];
image_ids = [id for id in image_ids]

all_results.extend(results)
all_image_ids.extend(image_ids)

coco_dt = coco.loadRes(all_results)
coco_eval = COCOeval(coco, coco_dt, 'bbox')
coco_eval.params.imgIds = all_image_ids
coco_eval = COCOeval(coco, coco_dt, "bbox")
coco_eval.params.imgIds = all_image_ids
coco_eval.evaluate()
coco_eval.accumulate()
coco_eval.summarize()

if __name__ == '__main__':

if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('--dir', default='/private/home/padentomasello/data/coco/output/')
parser.add_argument('--coco', default='/datasets01/COCO/022719/')
parser.add_argument(
"--dir", default="/private/home/padentomasello/data/coco/output/"
)
parser.add_argument("--coco", default="/datasets01/COCO/022719/")
args = parser.parse_args()
main(args.dir, args.coco)



54 changes: 32 additions & 22 deletions flashlight/app/objdet/scripts/prepare_coco.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,27 @@
LICENSE file in the root directory of this source tree.
"""

from pycocotools.coco import COCO
from PIL import Image
import argparse
import os

import torch
from PIL import Image
from pycocotools.coco import COCO


def create_training_list(img_folder, ann_file, output_file):
coco = COCO(ann_file)
ids = sorted(coco.imgs.keys())

with open(output_file, 'w') as out:
with open(output_file, "w") as out:
for idx in ids:
filepath = coco.loadImgs(idx)[0]['file_name']
filepath = coco.loadImgs(idx)[0]["file_name"]
img = Image.open(os.path.join(img_folder, filepath))
w, h = img.size
ann_ids = coco.getAnnIds(idx)
anns = coco.loadAnns(ann_ids)
bboxes = [ ann['bbox'] for ann in anns]
labels = [ ann['category_id'] for ann in anns]
bboxes = [ann["bbox"] for ann in anns]
labels = [ann["category_id"] for ann in anns]
boxes = torch.as_tensor(bboxes, dtype=torch.float32).reshape(-1, 4)
labels = torch.as_tensor(labels, dtype=torch.int64)
boxes[:, 2:] += boxes[:, :2]
Expand All @@ -33,24 +35,34 @@ def create_training_list(img_folder, ann_file, output_file):
boxes = boxes[keep]
labels = labels[keep]
labels = labels.tolist()
filepath = os.path.join(img_folder, filepath);
filepath = os.path.join(img_folder, filepath)
strings = [filepath]
for (box, label) in zip(boxes, labels):
box_with_label = box.tolist() + [ label ]
box_string = " ".join(map(str, box_with_label));
for box, label in zip(boxes, labels):
box_with_label = box.tolist() + [label]
box_string = " ".join(map(str, box_with_label))
strings.append(box_string)
out.write("\t".join(strings))
out.write('\n')
out.write("\n")


if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('--dataset_file', default='coco')
parser.add_argument('--crowdfree', action='store_true',
help='Remove crowd images from training on COCO')
parser.add_argument('--masks', action='store_true')
parser.add_argument('-r', '--coco_path', help='Root of COCO data', default='/datasets01/COCO/022719')
parser.add_argument('-o', '--output_dir', help='Output dir .lst file', default='/private/home/padentomasello/data/coco-mini/')
parser.add_argument("--dataset_file", default="coco")
parser.add_argument(
"--crowdfree",
action="store_true",
help="Remove crowd images from training on COCO",
)
parser.add_argument("--masks", action="store_true")
parser.add_argument(
"-r", "--coco_path", help="Root of COCO data", default="/datasets01/COCO/022719"
)
parser.add_argument(
"-o",
"--output_dir",
help="Output dir .lst file",
default="/private/home/padentomasello/data/coco-mini/",
)
args = parser.parse_args()

root = args.coco_path
Expand All @@ -61,10 +73,8 @@ def create_training_list(img_folder, ann_file, output_file):
"train": ("train2017", anno_file_template.format("train")),
"val": ("val2017", anno_file_template.format("val")),
}
for (split, (img_folder, ann_file)) in paths.items():
for split, (img_folder, ann_file) in paths.items():
img_folder = os.path.join(root, img_folder)
ann_file = os.path.join(root, 'annotations', ann_file)
output_file = os.path.join(args.output_dir, split + '.lst')
ann_file = os.path.join(root, "annotations", ann_file)
output_file = os.path.join(args.output_dir, split + ".lst")
create_training_list(img_folder, ann_file, output_file)


3 changes: 2 additions & 1 deletion scripts/colab/record.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@
https://air.ghost.io/recording-to-an-audio-file-using-html5-and-js/
https://stackoverflow.com/a/49019356
"""

from base64 import b64decode

import ffmpeg
import sox
from google.colab.output import eval_js
from IPython.display import HTML, display
from IPython.display import display, HTML


AUDIO_HTML = """
Expand Down

0 comments on commit 473ee74

Please sign in to comment.