Skip to content

Commit

Permalink
Returning confidence of the prediction.
Browse files Browse the repository at this point in the history
  • Loading branch information
Benteng Ma committed Jun 25, 2024
1 parent 90d8c3c commit 2864f87
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 167 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -563,8 +563,8 @@ def predict_frame(
).describe()

result = {
"attributes": {**rst_person["attributes"], **rst_cloth["attributes"]},
"description": rst_person["description"] + rst_cloth["description"],
**rst_person,
**rst_cloth,
}

result = json.dumps(result, indent=4)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,116 +91,20 @@ def from_parent_instance(
)

def describe(self) -> dict:
male = (
self.attributes["Male"]
> self.categories_and_attributes.thresholds_pred["Male"],
self.attributes["Male"],
)
has_hair = (
self.attributes["hair"]
> self.categories_and_attributes.thresholds_pred["hair"],
self.attributes["hair"],
)
hair_colour = _max_value_tuple(self.selective_attribute_dict["hair_colour"])
hair_shape = _max_value_tuple(self.selective_attribute_dict["hair_shape"])
facial_hair = _max_value_tuple(self.selective_attribute_dict["facial_hair"])
# bangs = (
# self.attributes['Bangs'] > self.categories_and_attributes.thresholds_pred['Bangs'],
# self.attributes['Bangs'])
hat = (
self.attributes["Wearing_Hat"]
> self.categories_and_attributes.thresholds_pred["Wearing_Hat"],
self.attributes["Wearing_Hat"],
)
glasses = (
self.attributes["Eyeglasses"]
> self.categories_and_attributes.thresholds_pred["Eyeglasses"],
self.attributes["Eyeglasses"],
)
earrings = (
self.attributes["Wearing_Earrings"]
> self.categories_and_attributes.thresholds_pred["Wearing_Earrings"],
self.attributes["Wearing_Earrings"],
)
necklace = (
self.attributes["Wearing_Necklace"]
> self.categories_and_attributes.thresholds_pred["Wearing_Necklace"],
self.attributes["Wearing_Necklace"],
)
necktie = (
self.attributes["Wearing_Necktie"]
> self.categories_and_attributes.thresholds_pred["Wearing_Necktie"],
self.attributes["Wearing_Necktie"],
)

description = "This customer has "
hair_colour_str = "None"
hair_shape_str = "None"
if has_hair[0]:
hair_shape_str = ""
if hair_shape[0] == "Straight_Hair":
hair_shape_str = "straight"
elif hair_shape[0] == "Wavy_Hair":
hair_shape_str = "wavy"
if hair_colour[0] == "Black_Hair":
description += "black %s hair, " % hair_shape_str
hair_colour_str = "black"
elif hair_colour[0] == "Blond_Hair":
description += "blond %s hair, " % hair_shape_str
hair_colour_str = "blond"
elif hair_colour[0] == "Brown_Hair":
description += "brown %s hair, " % hair_shape_str
hair_colour_str = "brown"
elif hair_colour[0] == "Gray_Hair":
description += "gray %s hair, " % hair_shape_str
hair_colour_str = "gray"

if (
male
): # here 'male' is only used to determine whether it is confident to decide whether the person has beard
if not facial_hair[0] == "No_Beard":
description += "and has beard. "

if hat[0] or glasses[0]:
description += "I can also see this customer wearing "
if hat[0] and glasses[0]:
description += "a hat and a pair of glasses. "
elif hat[0]:
description += "a hat. "
else:
description += "a pair of glasses. "

if earrings[0] or necklace[0] or necktie[0]:
description += "This customer might also wear "
wearables = []
if earrings[0]:
wearables.append("a pair of earrings")
if necklace[0]:
wearables.append("a necklace")
if necktie[0]:
wearables.append("a necktie")
description += (
", ".join(wearables[:-2] + [" and ".join(wearables[-2:])]) + ". "
)

if description == "This customer has ":
description = (
"I didn't manage to get any attributes from this customer, I'm sorry."
)
has_hair = self.attributes['hair'] - 0.5
hair_colour = _max_value_tuple(self.selective_attribute_dict['hair_colour']) - 0.5
hair_shape = _max_value_tuple(self.selective_attribute_dict['hair_shape']) - 0.5
facial_hair = 1 - self.attributes['No_Beard'] - 0.5
glasses = self.attributes['Eyeglasses'] - 0.5
hat = self.attributes['Wearing_Hat'] - 0.5

result = {
"attributes": {
"has_hair": has_hair[0],
"hair_colour": hair_colour_str,
"hair_shape": hair_shape_str,
"facial_hair": facial_hair[0] != "No_Beard",
"hat": hat[0],
"glasses": glasses[0],
"earrings": earrings[0],
"necklace": necklace[0],
"necktie": necktie[0],
},
"description": description,
'has_hair': has_hair,
'hair_colour': hair_colour,
'hair_shape': hair_shape,
'facial_hair': facial_hair,
'glasses': glasses,
'hat': hat,
}

return result
Expand Down Expand Up @@ -232,27 +136,10 @@ def from_parent_instance(
)

def describe(self) -> dict:
top = (
self.attributes["top"]
> self.categories_and_attributes.thresholds_pred["top"]
)
down = (
self.attributes["down"]
> self.categories_and_attributes.thresholds_pred["down"]
)
dress = (
self.attributes["dress"]
> self.categories_and_attributes.thresholds_pred["dress"]
)
outwear = (
self.attributes["outwear"]
> self.categories_and_attributes.thresholds_pred["outwear"]
)

result = {
# not in a loop for now, likely to add more logic combined with a classifier of more specific cloth classes.
"attributes": {},
"description": "this descrcription will be completed if we find out it is better to do it here.",
'top': self.attributes['top'] / 2,
'outwear': self.attributes['outwear'] / 2,
'dress': self.attributes['dress'] / 2,
}

for attribute in [
Expand All @@ -266,43 +153,42 @@ def describe(self) -> dict:
"sling dress",
"sleeveless top",
]:
result["attributes"][attribute] = False

if top:
max_prob = 0.0
max_attribute = "short sleeve top"
for attribute in ["short sleeve top", "long sleeve top", "vest", "sling"]:
if self.attributes[attribute] > max_prob:
max_prob = self.attributes[attribute]
max_attribute = attribute
if max_attribute in ["vest", "sling"]:
max_attribute = "sleeveless top"
result["attributes"][max_attribute] = True

if outwear:
max_prob = 0.0
max_attribute = "short sleeve outwear"
for attribute in [
"short sleeve outwear",
"long sleeve outwear",
]:
if self.attributes[attribute] > max_prob:
max_prob = self.attributes[attribute]
max_attribute = attribute
result["attributes"][max_attribute] = True
result[attribute] = False

max_prob = 0.0
max_attribute = "short sleeve top"
for attribute in ["short sleeve top", "long sleeve top", "vest", "sling"]:
if self.attributes[attribute] > max_prob:
max_prob = self.attributes[attribute]
max_attribute = attribute
if max_attribute in ["vest", "sling"]:
max_attribute = "sleeveless top"
result[max_attribute] = True

max_prob = 0.0
max_attribute = "short sleeve outwear"
for attribute in [
"short sleeve outwear",
"long sleeve outwear",
]:
if self.attributes[attribute] > max_prob:
max_prob = self.attributes[attribute]
max_attribute = attribute
result[max_attribute] = True

if dress:
max_prob = 0.0
max_attribute = "short sleeve dress"
for attribute in [
"short sleeve dress",
"long sleeve dress",
"vest dress",
"sling dress",
]:
if self.attributes[attribute] > max_prob:
max_prob = self.attributes[attribute]
max_attribute = attribute
result["attributes"][max_attribute] = True
max_prob = 0.0
max_attribute = "short sleeve dress"
for attribute in [
"short sleeve dress",
"long sleeve dress",
"vest dress",
"sling dress",
]:
if self.attributes[attribute] > max_prob:
max_prob = self.attributes[attribute]
max_attribute = attribute
if max_attribute in ["vest dress", "sling dress"]:
max_attribute = "sleeveless dress"
result[max_attribute] = True

return result

0 comments on commit 2864f87

Please sign in to comment.