Commit 409c50c7 authored by Ubuntu's avatar Ubuntu
Browse files

Enabled debug mode, change JSON prediction value order

parent e3746f68
......@@ -5,5 +5,5 @@
"description" : "Food Recognition Challenge Submission",
"license" : "MIT",
"gpu": true,
"debug": false
"debug": true
}
......@@ -28,7 +28,13 @@ def evaluate(test_images_path, predictions_output_path):
# Fix https://github.com/cocodataset/cocoapi/issues/139
# i.e. remove all segments with less than 3 points
prediction["segmentation"] = [s for s in prediction["segmentation"] if len(s) > 4]
predictions.append(prediction)
p = {}
p["image_id"] = prediction["image_id"]
p["category_id"] = prediction["category_id"]
p["score"] = prediction["score"]
p["segmentation"] = prediction["segmentation"]
p["bbox"] = prediction["bbox"]
predictions.append(p)
count += 1
if test and count == 4:
break
......
import random
import json
import numpy as np
import argparse
import base64
import glob
import os
import traceback
from PIL import Image
"""
Expected ENVIRONMENT Variables
* AICROWD_TEST_IMAGES_PATH : abs path to folder containing all the test images
* AICROWD_PREDICTIONS_OUTPUT_PATH : path where you are supposed to write the output predictions.json
"""
# Configuration Variables
padding = 50
SEGMENTATION_LENGTH = 10
MAX_NUMBER_OF_ANNOTATIONS = 10
# List of valid categories to choose from
VALID_CATEGORIES = [2578, 2939, 1085, 1040, 1070, 2022, 2053, 1566, 1151, 2131, 2521, 1068, 1026, 2738, 1565, 2512, 1061, 1311, 1505, 1468, 1967, 1078, 1116, 1022, 2504, 1154, 1163, 2750, 1210, 2620, 1310, 1893, 1069, 1058, 1180, 2618, 1010, 1588, 1879, 3080, 2388, 1108, 1032, 2099, 2530, 1013, 1323, 2634, 1056, 1520, 1788, 1352, 2498, 2711, 2103, 1554, 1556, 1307, 1060, 1220, 2580]
# Helper functions
def bounding_box_from_points(points):
"""
This function only supports the `poly` format.
"""
points = np.array(points).flatten()
even_locations = np.arange(points.shape[0]/2) * 2
odd_locations = even_locations + 1
X = np.take(points, even_locations.tolist())
Y = np.take(points, odd_locations.tolist())
bbox = [X.min(), Y.min(), X.max()-X.min(), Y.max()-Y.min()]
bbox = [int(b) for b in bbox]
return bbox
def single_segmentation(image_width, image_height, number_of_points=10):
points = []
for k in range(number_of_points):
# Choose a random x-coordinate
random_x = int(random.randint(0, image_width))
# Choose a random y-coordinate
random_y = int(random.randint(0, image_height))
#Flatly append them to the list of points
points.append(random_x)
points.append(random_y)
return [points]
def single_annotation(image_id, number_of_points=10):
# width, height = get_image_width_height(image_id)
width = 200
height = 300
_result = {}
_result["image_id"] = image_id
"""
Valid Categories are embedded in the annotations.json of the training set
"""
_result["category_id"] = random.choice(VALID_CATEGORIES)
_result["score"] = np.random.rand() # a random score between 0 and 1
_result["segmentation"] = single_segmentation(width, height, number_of_points=number_of_points)
_result["bbox"] = bounding_box_from_points(_result["segmentation"])
return _result
def get_image_id_from_image_path(image_path):
"""
Returns the image_id from the image_path of a file in the test set
image_path : /home/testsdir/098767.jpg
image_id : 98767
"""
filename = os.path.basename(image_path)
filename = filename.replace(".jpg", "")
image_id = int(filename)
return image_id
def gather_images(test_images_path):
images = glob.glob(os.path.join(
test_images_path, "*.jpg"
))
return images
def gather_image_ids(test_images_path):
images = gather_images(test_images_path)
filenames = [os.path.basename(image_path).replace(".jpg","") for image_path in images]
image_ids = [int(x) for x in filenames]
return image_ids
def get_image_path(image_id):
test_images_path = os.getenv("AICROWD_TEST_IMAGES_PATH", False)
return "{}.jpg".format(os.path.join(test_images_path, str(image_id).zfill(6)))
def get_image_width_height(image_id):
image_path = get_image_path(image_id)
im = Image.open(image_path)
width, height = im.size
im.close()
return width, height
def gather_input_output_path():
test_images_path = os.getenv("AICROWD_TEST_IMAGES_PATH", False)
assert test_images_path != False, "Please provide the path to the test images using the environment variable : AICROWD_TEST_IMAGES_PATH"
predictions_output_path = os.getenv("AICROWD_PREDICTIONS_OUTPUT_PATH", False)
assert predictions_output_path != False, "Please provide the output path (for writing the predictions.json) using the environment variable : AICROWD_PREDICTIONS_OUTPUT_PATH"
return test_images_path, predictions_output_path
predictions = []
image_ids = [123, 124, 125]
for image_id in image_ids:
number_of_annotations = random.randint(0, MAX_NUMBER_OF_ANNOTATIONS)
for _idx in range(number_of_annotations):
_annotation = single_annotation(image_id)
predictions.append(_annotation)
print(json.dumps(predictions))
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment