Kaggle competition: Predict Future Sales
<SOS> metric: rmse Kaggle data source: predict-future-sales<set_options>
warnings.filterwarnings('ignore')
!nvidia-smi<define_variables>
yolo = pd.read_csv('.. /input/vinbigdatastack/yolov5.csv')
 detectron = pd.read_csv('.. /input/vinbigdatastack/detectron2.csv')
 fasterrcnn = pd.read_csv('.. /input/vinbigdatastack/fasterrcnn.csv')
 image_ids = yolo.image_id.values
USE_COLAB, TRAIN_MODE, USE_TPU = 0, 1, 1
version_name = 'v6_tpu'
network_name = 'efficientnet_b4'<load_from_url>
def getitem(dataframe, img_id):

 pred = list(dataframe.loc[dataframe.image_id == img_id, "PredictionString"])[0].split(' ')
 nb_elm = len(pred)//6
output = {}
for elm in range(nb_elm):
output[f'elm_{elm}'] = pred[elm*6 :(elm+1)*6]
return output
def sortDictByProba(dict_):

 for key in dict_.keys() :
dict_[key] = list(map(lambda x: float(x), dict_[key]))
return {k: v for k, v in sorted(dict_.items() , key=lambda item: item[1][1], reverse = True)}
def getHighestProba(*list_of_dicts, n=3):

 output = {}
for index, dict_ in enumerate(list_of_dicts):
dict_length = len(dict_)
 for i in range(dict_length):
if i < n:
output[f"elm_{i}_dict_{index}"] =list(dict_.values())[i]
return output
def getUnique(dict_):

 dict_length = len(dict_)
 classes_non_unique = [list(dict_.values())[index][0] for index in range(dict_length)]
classes_unique = list(set(classes_non_unique))
uniques, counts = np.unique(classes_non_unique, return_counts=True)
 duplicates = uniques[counts > 1]
singles = np.setdiff1d(classes_unique, duplicates)
 return singles, duplicates
def getKeysByValue(dictOfElements, valueToFind):

 output = list()
listOfItems = dictOfElements.items()
for item in listOfItems:
if item[1][0] == valueToFind:
output.append(item[0])
 return output
def getListKeysByValue(dictOfElements, valuesToFind):

 output = []
for value in valuesToFind:
output.append(getKeysByValue(dictOfElements, value))
return output
def averaging(from_dict, single_keys, dupl_keys):

 output = {}
if len(np.ravel(single_keys)) != 0:
for single in np.ravel(single_keys):
output[single] = from_dict[single]
if len(np.ravel(dupl_keys)) != 0:
for index, list_of_duplicate_class in enumerate(dupl_keys):
probs = []
boxing1 = []
boxing2 = []
boxing3 = []
boxing4 = []
for elm in list_of_duplicate_class:
probs.append(from_dict[elm][1])
 boxing1.append(from_dict[elm][2])
 boxing2.append(from_dict[elm][3])
 boxing3.append(from_dict[elm][4])
 boxing4.append(from_dict[elm][5])
 output[f"elm_{index}"] = [from_dict[list_of_duplicate_class[0]][0],
np.mean(probs),
np.mean(boxing1),
np.mean(boxing2),
np.mean(boxing3),
np.mean(boxing4)]
return output
def toString(pred_list):

 castedList = []
for index, elm in enumerate(pred_list):
if index%6 == 0:
castedList.append(str(int(elm)))
 else:
castedList.append(str(elm))
output = " ".join(castedList)
 return output
if USE_TPU:
!curl https://raw.githubusercontent.com/pytorch/xla/master/contrib/scripts/env-setup.py -o pytorch-xla-env-setup.py
!python pytorch-xla-env-setup.py --apt-packages libomp5 libopenblas-dev 
<define_variables>
def main() :
output = pd.DataFrame(columns = ["image_id", "PredictionString"])
 for image_id in tqdm(image_ids):
fasterrcnn_pred = getitem(fasterrcnn, image_id)
 detectron_pred = getitem(detectron, image_id)
 yolo_pred = getitem(yolo, image_id)
 sorted_fasterrcnn = sortDictByProba(fasterrcnn_pred)
 sorted_detectron = sortDictByProba(detectron_pred)
 sorted_yolo = sortDictByProba(yolo_pred)
 highest_probs = getHighestProba(sorted_fasterrcnn,
sorted_detectron,
sorted_yolo,
n = 3)
 singles, duplicates = getUnique(highest_probs)
 single_keys = getListKeysByValue(highest_probs, singles)
 dupl_keys = getListKeysByValue(highest_probs, duplicates)
 stacked_dict = averaging(highest_probs, single_keys, dupl_keys)
 prediction_int = np.ravel(list(stacked_dict.values()))
prediction_string = toString(prediction_int)
 output = output.append({"image_id": image_id,
"PredictionString": prediction_string},
ignore_index=True)
 return output
<set_options><EOS> 
final_sub = main()
final_sub.to_csv("submission.csv", index=False )