def compute_YOLO_output(): """ Load YOLO model and compute the output : Possible model : yolov1 """ path_model ='/media/gonthier/HDD/models/yolo/' model = 'yolo-full' # Version 1 de YOLO trained on VOC12+07 model = 'yolo-voc' # YOLOv2 VOC 2007+2012 2007 model = 'yolo' #YOLOv2 608x608 COCO trainval cfg = path_model + model + ".cfg" weights = path_model + model +".weights" options = {"model": cfg, "load": weights, "threshold": 0.1, "gpu" : 1.0} tfnet = TFNet(options) imgcv = cv2.imread("loulou.jpg") result = tfnet.return_predict_gonthier(imgcv) # Return the best prediction print(result.shape) #print(result) if(model=='yolo-full'): C,B,S = 20,2,7 probs = get_probs(result, C, B, S) probs_per_classe = np.max(probs,axis=0) print(CLASSESVOC[np.argmax(probs_per_classe)]) if(model=='yolo-voc'): C,B= 20,5 H = 13 W = 13 probs = get_probs_v2(result,H,W,C,B) probs_per_classe = np.max(probs,axis=(0,1,2)) print(CLASSESVOC[np.argmax(probs_per_classe)]) if(model=='yolo'): C,B= 80,5 H = 19 W = 19 probs = get_probs_v2(result,H,W,C,B) probs_per_classe = np.max(probs,axis=(0,1,2)) print(CLASSESCOCO[np.argmax(probs_per_classe)]) result = tfnet.return_predict(imgcv) # Return the best prediction print(result)
def compute_YOLO_Perf_Paintings(): classes_paitings = ['aeroplane','bird','boat','chair','cow','diningtable','dog','horse','sheep','train'] path_to_img = '/media/gonthier/HDD/data/Painting_Dataset/' database = 'Paintings' databasetxt = database + '.txt' df_label = pd.read_csv(databasetxt,sep=",") df_test = df_label[df_label['set']=='test'] sLength = len(df_test['name_img']) name_img = df_test['name_img'][0] i = 0 y_test = np.zeros((sLength,10)) path_model ='/media/gonthier/HDD/models/yolo/' for model in NETS_Pretrained: print(model) if model=='yolo-voc' or model=='yolo-full': CLASSES = CLASSES_SET['VOC'] elif model=='yolo': CLASSES = CLASSES_SET['COCO'] nbClasses = len(CLASSES) cfg = path_model + model + ".cfg" weights = path_model + model +".weights" options = {"model": cfg, "load": weights, "threshold": 0.1, "gpu" : 1.0} tfnet = TFNet(options) scores_all_image = np.zeros((len(df_test),nbClasses)) for i,name_img in enumerate(df_test['name_img']): if i%1000==0: print(i,name_img) complet_name = path_to_img + name_img + '.jpg' im = cv2.imread(complet_name) result = tfnet.return_predict_gonthier(im) # Arguments: im (ndarray): a color image in BGR order if(model=='yolo-full'): C,B,S = 20,2,7 probs = get_probs(result, C, B, S) probs_per_classe = np.max(probs,axis=0) if(model=='yolo-voc'): C,B= 20,5 H = 13 W = 13 probs = get_probs_v2(result,H,W,C,B) probs_per_classe = np.max(probs,axis=(0,1,2)) if(model=='yolo'): C,B= 80,5 H = 19 W = 19 probs = get_probs_v2(result,H,W,C,B) probs_per_classe = np.max(probs,axis=(0,1,2)) scores_all_image[i,:] = probs_per_classe for j in range(10): if(classes_paitings[j] in list(df_test['classe'][df_test['name_img']==name_img])[0]): y_test[i,j] = 1 AP_per_class = [] for k,classe in enumerate(classes_paitings): index_classe = np.where(np.array(CLASSES)==classe)[0][0] scores_per_class = scores_all_image[:,index_classe] #print(scores_per_class) #print(y_test[:,k],np.sum(y_test[:,k])) AP = average_precision_score(y_test[:,k],scores_per_class,average=None) AP_per_class += [AP] print("Average Precision for",classe," = ",AP) print(model," mean Average Precision = {0:.3f}".format(np.mean(AP_per_class)))