def __init__(self, width, height, wt_file): self.width = int(round(width / self.base) * self.base) self.height = int(round(height / self.base) * self.base) self.weight_file = wt_file self.model = get_yolo_model(self.width, self.height, num_class=1, features=True) self.model.load_weights(self.weight_file, by_name=True)
def __init__(self, width, height, wt_file, obj_threshold=0.001, nms_threshold=0.5, max_length=256): # YOLO dimensions have to be a multiple of 32 so we'll choose the closest multiple to the image self.width = int(round(width / self.base) * self.base) self.height = int(round(height / self.base) * self.base) self.weight_file = wt_file self.obj_threshold = obj_threshold self.nms_threshold = nms_threshold self.max_length = max_length self.model = get_yolo_model(self.width, self.height, num_class=1) self.model.load_weights(self.weight_file,by_name=True)
#Open the video file which needs to be processed root = tk.Tk() movieName = askopenfilename( initialdir= '/media/aakanksha/f41d5ac2-703c-4b56-a960-cd3a54f21cfb/aakanksha/Documents/Backup/Phd/Analysis/Videos/', filetypes=[("Video files", "*")]) cap = cv2.VideoCapture(movieName) nframe = cap.get(cv2.CAP_PROP_FRAME_COUNT) step = 500 im_width = 3840 #1920#864 im_height = 2176 #1088#864 obj_threshold = 0.5 max_length = 256 weight_file = '/media/aakanksha/f41d5ac2-703c-4b56-a960-cd3a54f21cfb/aakanksha/Documents/Backup/Phd/Analysis/blackbuckML/yoloTracker/weights/compare-blackbucks-yolo.h5' model = get_yolo_model(im_width, im_height, num_class=1) model.load_weights(weight_file, by_name=True) im_num = 0 width = 3840 #1920 height = 2176 #1080 count = 0 #video = cv2.VideoWriter('/media/aakanksha/f41d5ac2-703c-4b56-a960-cd3a54f21cfb/aakanksha/Documents/Backup/Phd/Analysis/blackbuckML/testOut/video.avi',-1,1,(im_width,im_height)) while (cap.isOpened()): if (cv2.waitKey(1) & 0xFF == ord('q')) | (count > 32): break cap.set(cv2.CAP_PROP_POS_FRAMES, im_num) ret, img = cap.read() im_num += step image_h, image_w, _ = img.shape
def main(argv): if (len(sys.argv) != 3): print('Usage ./train.py [root_dir] [config.yml]') sys.exit(1) #Load data root_dir = argv[1] + '/' #in case we forgot print('Opening file' + root_dir + argv[2]) with open(root_dir + argv[2], 'r') as configfile: config = yaml.safe_load(configfile) image_dir = root_dir + config['data_dir'] train_dir = root_dir + config['data_dir'] train_image_folder = root_dir + config['data_dir'] weights_dir = root_dir + config['weights_dir'] your_weights = weights_dir + config['specific_weights'] generic_weights = weights_dir + config['generic_weights'] trained_weights = weights_dir + config['trained_weights'] list_of_train_file = train_dir + config['checked_annotations_fname'] list_of_train_files = '/annotations-checked.yml' train_files_regex = config['generic_train_files_regex'] FINE_TUNE = config['FINE_TUNE'] TEST_RUN = config['TEST_RUN'] LABELS = config['LABELS'] IMAGE_H = config['IMAGE_H'] IMAGE_W = config['IMAGE_W'] NO_OBJECT_SCALE = config['NO_OBJECT_SCALE'] OBJECT_SCALE = config['OBJECT_SCALE'] COORD_SCALE = config['COORD_SCALE'] CLASS_SCALE = config['CLASS_SCALE'] valid_image_folder = train_image_folder valid_annot_folder = train_image_folder if FINE_TUNE: BATCH_SIZE = 4 if TEST_RUN: EPOCHS = 1 else: EPOCHS = 100 model = get_yolo_model(IMAGE_W, IMAGE_H, num_class=1, headtrainable=True, trainable=True) model.load_weights(your_weights) else: BATCH_SIZE = 32 EPOCHS = 500 model = get_yolo_model(IMAGE_W, IMAGE_H, num_class=1, headtrainable=True) model.load_weights(generic_weights, by_name=True) ### read saved pickle of parsed annotations with open(train_image_folder + list_of_train_files, 'r') as fp: all_imgs = yaml.load(fp) print('Reading YaML file finished. Time to luck and load!\n') num_ims = len(all_imgs) indexes = np.arange(num_ims) random.shuffle(indexes) num_val = 0 #num_ims//10 #valid_imgs = list(itemgetter(*indexes[:num_val].tolist())(all_imgs)) train_imgs = list(itemgetter(*indexes[num_val:].tolist())(all_imgs)) train_batch = BatchGenerator(instances=train_imgs, labels=LABELS, batch_size=BATCH_SIZE, shuffle=True, jitter=0.0, im_dir=train_image_folder) #valid_batch = BatchGenerator(valid_imgs, generator_config, norm=normalize, jitter=False) def yolo_loss(y_true, y_pred): # compute grid factor and net factor grid_h = tf.shape(y_true)[1] grid_w = tf.shape(y_true)[2] grid_factor = tf.reshape(tf.cast([grid_w, grid_h], tf.float32), [1, 1, 1, 1, 2]) net_h = IMAGE_H / grid_h net_w = IMAGE_W / grid_w net_factor = tf.reshape(tf.cast([net_w, net_h], tf.float32), [1, 1, 1, 1, 2]) pred_box_xy = y_pred[..., 0:2] # t_wh pred_box_wh = tf.log(y_pred[..., 2:4]) # t_wh pred_box_conf = tf.expand_dims(y_pred[..., 4], 4) pred_box_class = y_pred[..., 5:] # adjust class probabilities # initialize the masks object_mask = tf.expand_dims(y_true[..., 4], 4) true_box_xy = y_true[..., 0:2] # (sigma(t_xy) + c_xy) true_box_wh = tf.where(y_true[..., 2:4] > 0, tf.log(tf.cast(y_true[..., 2:4], tf.float32)), y_true[..., 2:4]) true_box_conf = tf.expand_dims(y_true[..., 4], 4) true_box_class = y_true[..., 5:] xy_delta = COORD_SCALE * object_mask * (pred_box_xy - true_box_xy ) #/net_factor #* xywh_scale wh_delta = COORD_SCALE * object_mask * (pred_box_wh - true_box_wh ) #/ net_factor #* xywh_scale obj_delta = OBJECT_SCALE * object_mask * (pred_box_conf - true_box_conf) no_obj_delta = NO_OBJECT_SCALE * (1 - object_mask) * pred_box_conf class_delta = CLASS_SCALE * object_mask * (pred_box_class - true_box_class) loss_xy = tf.reduce_sum(tf.square(xy_delta), list(range(1, 5))) loss_wh = tf.reduce_sum(tf.square(wh_delta), list(range(1, 5))) loss_obj = tf.reduce_sum(tf.square(obj_delta), list(range(1, 5))) lossnobj = tf.reduce_sum(tf.square(no_obj_delta), list(range(1, 5))) loss_cls = tf.reduce_sum(tf.square(class_delta), list(range(1, 5))) loss = loss_xy + loss_wh + loss_obj + lossnobj + loss_cls return loss print('Prepared bathes now we will load weights') wt_file = your_weights optimizer = Adam(lr=0.5e-4, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.0) model.compile(loss=yolo_loss, optimizer=optimizer) early_stop = EarlyStopping(monitor='loss', min_delta=0.001, patience=5, mode='min', verbose=1) checkpoint = ModelCheckpoint(wt_file, monitor='loss', verbose=1, save_best_only=True, mode='min', period=1) print('Training starts.') start = time.time() model.fit_generator( generator=train_batch, steps_per_epoch=len(train_batch), epochs=EPOCHS, verbose=1, # validation_data = valid_batch, # validation_steps = len(valid_batch), callbacks=[checkpoint, early_stop], #, tensorboard], max_queue_size=3) model.save_weights(trained_weights) end = time.time() print('Training took ' + str(end - start) + ' seconds') print('Weights saved to ' + trained_weights) print("Finished! :o)")
def main(argv): if (len(sys.argv) != 3): print('Usage ./makeTrainCoco.py [root_dir] [config.yml]') sys.exit(1) #Load data root_dir = argv[1] + '/' #in case we forgot print('Opening file' + root_dir + argv[2]) with open(root_dir + argv[2], 'r') as configfile: config = yaml.safe_load(configfile) image_dir = root_dir + config['data_dir'] train_dir = root_dir + config['data_dir'] weights_dir = root_dir + config['weights_dir'] your_weights = weights_dir + config['generic_weights'] annotations_file = train_dir + config['untrained_annotations_fname'] train_files_regex = config['generic_train_files_regex'] train_images = glob.glob(image_dir + train_files_regex) max_l = 100 min_l = 10 im_size = 864 #size of training imageas for yolo ################################################## #im_size=416 #size of training imageas for yolo yolov3 = get_yolo_model(im_size, im_size, trainable=False) yolov3.load_weights(your_weights, by_name=True) ######################################## im_num = 1 all_imgs = [] for imagename in train_images: im = cv2.imread(imagename) print('processing image ' + imagename + ', ' + str(im_num) + ' of ' + str(len(train_images)) + '...') height, width = im.shape[:2] im_num += 1 n_count = 0 for x in np.arange(0, width - im_size, im_size): for y in np.arange(0, height - im_size, im_size): img_data = { 'object': [] } #dictionary? key-value pair to store image data head, tail = os.path.split(imagename) noext, ext = os.path.splitext(tail) save_name = train_dir + '/TR_' + noext + '-' + str( n_count) + '.png' box_name = train_dir + '/bbox/' + noext + '-' + str( n_count) + '.png' img = im[y:y + im_size, x:x + im_size, :] cv2.imwrite(save_name, img) img_data['filename'] = save_name img_data['width'] = im_size img_data['height'] = im_size n_count += 1 # use the yolov3 model to predict 80 classes on COCO # preprocess the image image_h, image_w, _ = img.shape new_image = img[:, :, ::-1] / 255. new_image = np.expand_dims(new_image, 0) # run the prediction yolos = yolov3.predict(new_image) boxes = decode(yolos, obj_thresh=0.005, nms_thresh=0.5) for b in boxes: xmin = int(b[0]) xmax = int(b[2]) ymin = int(b[1]) ymax = int(b[3]) obj = {} obj['name'] = 'aoi' if xmin < 0: continue if ymin < 0: continue if xmax > im_size: continue if ymax > im_size: continue if (xmax - xmin) < min_l: continue if (xmax - xmin) > max_l: continue if (ymax - ymin) < min_l: continue if (ymax - ymin) > max_l: continue obj['xmin'] = xmin obj['ymin'] = ymin obj['xmax'] = xmax obj['ymax'] = ymax img_data['object'] += [obj] cv2.rectangle(img, (xmin, ymin), (xmax, ymax), (0, 255, 0), 2) cv2.imwrite(box_name, img) all_imgs += [img_data] #print(all_imgs) print('Saving data to ' + annotations_file) with open(annotations_file, 'w') as handle: yaml.dump(all_imgs, handle) print('Finished! :o)')
def main(argv): if (len(sys.argv) != 3): print('Usage ./makeTrain.py [root_dir] [config.yml]') sys.exit(1) #Load data root_dir = argv[1] + '/' #in case we forgot print('Opening file' + root_dir + argv[2]) with open(root_dir + argv[2], 'r') as configfile: config = yaml.safe_load(configfile) image_dir = root_dir + config['data_dir'] train_dir = root_dir + config['data_dir'] your_weights = root_dir + config['weights_dir'] + config['specific_weights'] trained_annotations_fname = train_dir + config['trained_annotations_fname'] train_files_regex = config['specific_train_files_regex'] train_images = glob.glob(train_dir + train_files_regex) shuffle(train_images) max_l = 100 min_l = 10 im_size = 864 #size of training imageas for yolo ################################################## #im_size=416 #size of training imageas for yolo yolov3 = get_yolo_model(im_size, im_size, num_class=1, trainable=False) yolov3.load_weights(your_weights) ######################################## im_num = 1 all_imgs = [] for imagename in train_images: img = cv2.imread(imagename) print('processing image ' + imagename + ', ' + str(im_num) + ' of ' + str(len(train_images)) + '...') im_num += 1 img_data = { 'object': [] } #dictionary? key-value pair to store image data head, tail = os.path.split(imagename) noext, ext = os.path.splitext(tail) box_name = train_dir + '/bbox/' + tail img_data['filename'] = tail img_data['width'] = im_size img_data['height'] = im_size # use the trained yolov3 model to predict # preprocess the image image_h, image_w, _ = img.shape new_image = img[:, :, ::-1] / 255. new_image = np.expand_dims(new_image, 0) # run the prediction yolos = yolov3.predict(new_image) boxes = decode(yolos, obj_thresh=0.2, nms_thresh=0.3) for b in boxes: xmin = int(b[0]) xmax = int(b[2]) ymin = int(b[1]) ymax = int(b[3]) obj = {} obj['name'] = 'aoi' if xmin < 0: continue if ymin < 0: continue if xmax > im_size: continue if ymax > im_size: continue if (xmax - xmin) < min_l: continue if (xmax - xmin) > max_l: continue if (ymax - ymin) < min_l: continue if (ymax - ymin) > max_l: continue obj['xmin'] = xmin obj['ymin'] = ymin obj['xmax'] = xmax obj['ymax'] = ymax img_data['object'] += [obj] cv2.rectangle(img, (xmin, ymin), (xmax, ymax), (0, 255, 0), 2) cv2.imwrite(box_name, img) all_imgs += [img_data] #print(all_imgs) with open(trained_annotations_fname, 'w') as handle: yaml.dump(all_imgs, handle)
CLASS_SCALE = 1.0 if FINE_TUNE: BATCH_SIZE = 4 else: BATCH_SIZE = 32 train_image_folder = 'train_images_1/' #/home/ctorney/data/coco/train2014/' train_annot_folder = 'train_images_1/' valid_image_folder = train_image_folder #'/home/ctorney/data/coco/val2014/' valid_annot_folder = train_annot_folder #'/home/ctorney/data/coco/val2014ann/' if FINE_TUNE: model = get_yolo_model(IMAGE_W, IMAGE_H, num_class=1, headtrainable=True, trainable=True) model.load_weights('../weights/balloon-yolo.h5') else: model = get_yolo_model(IMAGE_W, IMAGE_H, num_class=1, headtrainable=True) model.load_weights('../weights/yolo-v3-coco.h5', by_name=True) #model = get_yolo_model(IMAGE_W,IMAGE_H, num_class=1,trainable=True) def yolo_loss(y_true, y_pred): # compute grid factor and net factor grid_h = tf.shape(y_true)[1] grid_w = tf.shape(y_true)[2] grid_factor = tf.reshape(tf.cast([grid_w, grid_h], tf.float32),
width=4096 height=2160 width=865 height=865 im_size=864 #size of training imageas for yolo nx = width//im_size ny = height//im_size ################################################## #im_size=416 #size of training imageas for yolo #yolov3 = get_yolo_model(im_size,im_size,trainable=False) #yolov3.load_weights('../../weights/yolo-v3-coco.h5',by_name=True) yolov3 = get_yolo_model(im_size,im_size,num_class=1,trainable=False) yolov3.load_weights('../../weights/balloon-yolo.h5',by_name=False) print(yolov3.summary()) #im = cv2.imread('balloon.png') #new_image = im[:,:,::-1]/255. #new_image = np.expand_dims(new_image, 0) #aa = np.tile(new_image, (2,1,1,1)) # run the prediction #yolos = yolov3.predict(aa) #boxes = decode(yolos, im_size, 'predictions.png', im) #sys.exit('bye!')
def main(argv): if(len(sys.argv) != 3): print('Usage ./postTrainTest.py [data_dir] [config.yml]') sys.exit(1) #Load data data_dir = argv[1] + '/' #in case we forgot '/' print('Opening file' + argv[2]) with open(argv[2], 'r') as configfile: config = yaml.safe_load(configfile) image_dir = data_dir train_dir = data_dir weights_dir = data_dir + config['weights_dir'] #Training type dependent trained_weights = weights_dir + config['trained_weights'] #based on get_yolo_model defaults and previous makTrain.py files num_class=config['specific']['num_class'] obj_thresh=config['specific']['obj_thresh'] nms_thresh=config['specific']['nms_thresh'] list_of_train_files = config['checked_annotations_fname'] annotations_file = train_dir + config['untrained_annotations_fname'] with open (annotations_file, 'r') as fp: all_imgs = yaml.load(fp) max_l=config['MAX_L'] #maximal object size in pixels min_l=config['MIN_L'] im_size=config['IMAGE_H'] #size of training imageas for yolo ################################################## print("Loading YOLO models") yolov3 = get_yolo_model(im_size,im_size,num_class,trainable=False) yolov3.load_weights(trained_weights,by_name=True) #TODO is by_name necessary here? print("YOLO models loaded, my dear.") ######################################## #read in all images from checked annotations (GROUND TRUTH) for i in range(len(all_imgs)): basename = os.path.basename(all_imgs[i]['filename']) #remove extension from basename: name_seed_split = basename.split('.')[:-1] name_seed = '.'.join(name_seed_split) fname_gt = image_dir + "/groundtruths/" + name_seed + ".txt" fname_pred = image_dir + "/predictions/" + name_seed + ".txt" img_data = {'object':[]} img_data['filename'] = basename img_data['width'] = all_imgs[i]['width'] img_data['height'] = all_imgs[i]['height'] #Reading ground truth boxes_gt=[] for obj in all_imgs[i]['object']: boxes_gt.append([obj['xmin'],obj['ymin'],obj['xmax'],obj['ymax']]) sys.stdout.write('GT objects:') sys.stdout.write(str(len(boxes_gt))) sys.stdout.flush() #do box processing img = cv2.imread(image_dir + basename) with open(fname_gt, 'w') as file_gt: #left top righ bottom for b in boxes_gt: obj = {} if ((b[2]-b[0])*(b[3]-b[1]))<10: continue obj['name'] = 'aoi' obj['xmin'] = int(b[0]) obj['ymin'] = int(b[1]) obj['xmax'] = int(b[2]) obj['ymax'] = int(b[3]) img_data['object'] += [obj] file_gt.write(obj['name'] + " " ) file_gt.write(str(obj['xmin']) + " " ) file_gt.write(str(obj['ymin']) + " " ) file_gt.write(str(obj['xmax']) + " " ) file_gt.write(str(obj['ymax'])) file_gt.write('\n') # preprocess the image image_h, image_w, _ = img.shape new_image = img[:,:,::-1]/255. new_image = np.expand_dims(new_image, 0) # run the prediction sys.stdout.write('Yolo predicting...') sys.stdout.flush() yolos = yolov3.predict(new_image) sys.stdout.write('decoding...') sys.stdout.flush() boxes_predict = decode(yolos, obj_thresh, nms_thresh) sys.stdout.write('done!#of boxes_predict:') sys.stdout.write(str(len(boxes_predict))) sys.stdout.write('\n') sys.stdout.flush() with open(fname_pred, 'w') as file_pred: #left top righ bottom for b in boxes_predict: xmin=int(b[0]) xmax=int(b[2]) ymin=int(b[1]) ymax=int(b[3]) confidence=float(b[4]) objpred = {} objpred['name'] = 'aoi' if xmin<0: continue if ymin<0: continue if xmax>im_size: continue if ymax>im_size: continue if (xmax-xmin)<min_l: continue if (xmax-xmin)>max_l: continue if (ymax-ymin)<min_l: continue if (ymax-ymin)>max_l: continue objpred['xmin'] = xmin objpred['ymin'] = ymin objpred['xmax'] = xmax objpred['ymax'] = ymax objpred['confidence'] = confidence file_pred.write(objpred['name'] + " " ) file_pred.write(str(objpred['confidence']) + " " ) file_pred.write(str(objpred['xmin']) + " " ) file_pred.write(str(objpred['ymin']) + " " ) file_pred.write(str(objpred['xmax']) + " " ) file_pred.write(str(objpred['ymax'])) file_pred.write('\n') #precision = tp / (tp + fp) # for box_gt in boxes_gt: # for box_predict in boxes_predict: # iou_val = bbox_iou(box_predict,box_gt) # print(iou_val) #count prediction which reache a threshold of let's say 0.5 # if we cahnge the dection threshold I think we'll get ROC curve - that'd be cute. print('Finished! :o)')
def main(argv): if(len(sys.argv) != 3): print('Usage ./train.py [data_dir] [config.yml]') sys.exit(1) #Load data data_dir = argv[1] + '/' #in case we forgot '/' print('Opening file' + argv[2]) with open(argv[2], 'r') as configfile: config = yaml.safe_load(configfile) #logging and debugging setup DEBUG = config['DEBUG'] TEST_RUN = config['TEST_RUN'] print(config) image_dir = data_dir train_dir = data_dir weights_dir = data_dir + config['weights_dir'] train_image_folder = data_dir training_type = config['training_type'] your_weights = weights_dir + config['specific']['weights'] md5check(config['specific']['weights_md5'],your_weights) generic_weights = weights_dir + config['generic']['weights'] md5check(config['generic']['weights_md5'],generic_weights) trained_weights = weights_dir + config['trained_weights'] list_of_train_files = config['checked_annotations_fname'] #list_of_train_files = '/annotations-checked.yml' train_files_regex = config['generic']['train_files_regex'] training_phase = config['FINE_TUNE_PHASE'] LABELS = config['LABELS'] IMAGE_H = config['IMAGE_H'] IMAGE_W = config['IMAGE_W'] NO_OBJECT_SCALE = config['NO_OBJECT_SCALE'] OBJECT_SCALE = config['OBJECT_SCALE'] COORD_SCALE = config['COORD_SCALE'] CLASS_SCALE = config['CLASS_SCALE'] valid_image_folder = train_image_folder valid_annot_folder = train_image_folder BATCH_SIZE = config[training_phase]['BATCH_SIZE'] EPOCHS = config[training_phase]['EPOCHS'] LR = config[training_phase]['LR'] input_weights = weights_dir + config[training_type]['weights'] if TEST_RUN: EPOCHS=1 BATCH_SIZE=4 if training_phase=='phase_one': print("Fine tuning phase 1. Training top layers.") model = get_yolo_model(IMAGE_W,IMAGE_H, num_class=1,headtrainable=True) print("Loading weights %s",input_weights) model.load_weights(input_weights, by_name=True) else: print("Fine tuning phase 2. We retrain all layers with small learning rate") model = get_yolo_model(IMAGE_W,IMAGE_H, num_class=1,headtrainable=True, trainable=True) print("Loading weights %s",input_weights) model.load_weights(input_weights) if DEBUG: print(model.summary()) ### read saved pickle of parsed annotations print("Loading images from %s",train_image_folder + list_of_train_files) with open (train_image_folder + list_of_train_files, 'r') as fp: all_imgs = yaml.load(fp) print('Reading YaML file finished. Time to lock and load!\n') num_ims = len(all_imgs) indexes = np.arange(num_ims) random.shuffle(indexes) num_val = 0 #num_ims//10 # valid_imgs = list(itemgetter(*indexes[:num_val].tolist())(all_imgs)) # valid_batch = BatchGenerator(valid_imgs, labels= LABELS, jitter=False, im_dir= train_image_folder) train_imgs = list(itemgetter(*indexes[num_val:].tolist())(all_imgs)) train_batch = BatchGenerator(instances= train_imgs,labels= LABELS,batch_size= BATCH_SIZE,shuffle= True,jitter= 0.0,im_dir= train_image_folder) def yolo_loss(y_true, y_pred): # compute grid factor and net factor grid_h = tf.shape(y_true)[1] grid_w = tf.shape(y_true)[2] grid_factor = tf.reshape(tf.cast([grid_w, grid_h], tf.float32), [1,1,1,1,2]) net_h = IMAGE_H/grid_h net_w = IMAGE_W/grid_w net_factor = tf.reshape(tf.cast([net_w, net_h], tf.float32), [1,1,1,1,2]) pred_box_xy = y_pred[..., 0:2] # t_wh pred_box_wh = tf.log(y_pred[..., 2:4]) # t_wh pred_box_conf = tf.expand_dims(y_pred[..., 4], 4) pred_box_class = y_pred[..., 5:] # adjust class probabilities # initialize the masks object_mask = tf.expand_dims(y_true[..., 4], 4) true_box_xy = y_true[..., 0:2] # (sigma(t_xy) + c_xy) true_box_wh = tf.where(y_true[...,2:4]>0, tf.log(tf.cast(y_true[..., 2:4],tf.float32)), y_true[...,2:4]) true_box_conf = tf.expand_dims(y_true[..., 4], 4) true_box_class = y_true[..., 5:] xy_delta = COORD_SCALE * object_mask * (pred_box_xy-true_box_xy) #/net_factor #* xywh_scale wh_delta = COORD_SCALE * object_mask * (pred_box_wh-true_box_wh) #/ net_factor #* xywh_scale obj_delta = OBJECT_SCALE * object_mask * (pred_box_conf-true_box_conf) no_obj_delta = NO_OBJECT_SCALE * (1-object_mask) * pred_box_conf class_delta = CLASS_SCALE * object_mask * (pred_box_class-true_box_class) loss_xy = tf.reduce_sum(tf.square(xy_delta), list(range(1,5))) loss_wh = tf.reduce_sum(tf.square(wh_delta), list(range(1,5))) loss_obj= tf.reduce_sum(tf.square(obj_delta), list(range(1,5))) lossnobj= tf.reduce_sum(tf.square(no_obj_delta), list(range(1,5))) loss_cls= tf.reduce_sum(tf.square(class_delta), list(range(1,5))) loss = loss_xy + loss_wh + loss_obj + lossnobj + loss_cls return loss print('Prepared batches now we will load weights') wt_file=input_weights optimizer = Adam(lr=LR, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.0) model.compile(loss=yolo_loss, optimizer=optimizer, metrics=['accuracy']) early_stop = EarlyStopping(monitor='loss', min_delta=0.001,patience=5,mode='min',verbose=1) checkpoint = ModelCheckpoint(trained_weights + '_checkpoint' ,monitor='loss',verbose=1,save_best_only=True,mode='min',period=1) print('Training starts.') start = time.time() tensorboard = TensorBoard(log_dir="logs/{}".format(time.time())) model.fit_generator(generator = train_batch, steps_per_epoch = len(train_batch), epochs = EPOCHS, verbose = 1, # validation_data = valid_batch, # validation_steps = len(valid_batch), callbacks = [checkpoint, early_stop, tensorboard], max_queue_size = 3) model.save_weights(trained_weights) end = time.time() print('Training took ' + str(end - start) + ' seconds') print('Weights saved to ' + trained_weights) print("Finished! :o)")
def main(argv): if(len(sys.argv) != 3): print('Usage ./prepTrain.py [data_dir] [config.yml]') sys.exit(1) #Load data data_dir = argv[1] + '/' #in case we forgot '/' print('Opening file' + argv[2]) with open(argv[2], 'r') as configfile: config = yaml.safe_load(configfile) #TODO: since this is the first file to use, maybe add a check if all directories exist? image_dir = data_dir train_dir = data_dir weights_dir = data_dir + config['weights_dir'] #Training type dependent training_type = config['training_type'] print("Training type is " + training_type) print(config[training_type]) your_weights = weights_dir + config[training_type]['weights'] #check md5 of a weights file if available md5check(config[training_type]['weights_md5'],your_weights) train_files_regex = config[training_type]['train_files_regex'] #based on get_yolo_model defaults and previous makTrain.py files num_class=config[training_type]['num_class'] obj_thresh=config[training_type]['obj_thresh'] nms_thresh=config[training_type]['nms_thresh'] train_images = glob.glob( image_dir + train_files_regex ) annotations_file = train_dir + config['untrained_annotations_fname'] max_l=config['MAX_L'] #maximal object size in pixels min_l=config['MIN_L'] im_width=config['IMAGE_W'] #size of training imageas for yolo im_height=config['IMAGE_H'] ################################################## print("Loading YOLO models") yolov3 = get_yolo_model(im_width,im_height,num_class,trainable=False) yolov3.load_weights(your_weights,by_name=True) #TODO is by_name necessary here? # Creating another model to provide visualisation and/or extraction of high level features yolov3_feats = get_yolo_model_feats(im_width,im_height,num_class,trainable=False) yolov3_feats.load_weights(your_weights,by_name=True) #TODO is by_name necessary here? print("YOLO models loaded, my dear.") ######################################## im_num=1 all_imgs = [] for imagename in train_images: im = cv2.imread(imagename) print('processing image ' + imagename + ', ' + str(im_num) + ' of ' + str(len(train_images)) + '...') im_yolo = makeYoloCompatible(im) height, width = im_yolo.shape[:2] im_num+=1 n_count=0 for x in np.arange(0,1+width-im_width,im_width):#'1+' added to allow case when image has exactly size of one window for y in np.arange(0,1+height-im_height,im_height): img_data = {'object':[]} #dictionary? key-value pair to store image data head, tail = os.path.split(imagename) noext, ext = os.path.splitext(tail) save_name = train_dir + '/TR_' + noext + '-' + str(n_count) + '.png' box_name = train_dir + '/bbox/' + noext + '-' + str(n_count) + '.png' img = im[y:y+im_height,x:x+im_width,:] cv2.imwrite(save_name, img) img_data['filename'] = save_name img_data['width'] = im_width img_data['height'] = im_height n_count+=1 # use the yolov3 model to predict 80 classes on COCO # preprocess the image image_h, image_w, _ = img.shape new_image = img[:,:,::-1]/255. new_image = np.expand_dims(new_image, 0) # run the prediction sys.stdout.write('Yolo predicting...') sys.stdout.flush() yolos = yolov3.predict(new_image) # yolo_feats = yolov3_feats.predict(new_image) # print(type(yolo_feats)) # print(type(yolo_feats[1])) # print(yolo_feats[1].shape) # print(yolo_feats[1].dtype) # fileObject = open("feats.pickle",'wb') # pickle.dump(yolo_feats[1],fileObject) # fileObject.close() # print("pickedleeee") # cv2.imshow("heatmap",yolo_feats[1][:,:,1]) # k = cv2.waitKey(0) sys.stdout.write('Decoding...') sys.stdout.flush() boxes = decode(yolos, obj_thresh, nms_thresh) sys.stdout.write('Done!#of boxes:') sys.stdout.write(str(len(boxes))) sys.stdout.flush() for b in boxes: xmin=int(b[0]) xmax=int(b[2]) ymin=int(b[1]) ymax=int(b[3]) obj = {} obj['name'] = 'aoi' if xmin<0: continue if ymin<0: continue if xmax>im_width: continue if ymax>im_height: continue if (xmax-xmin)<min_l: continue if (xmax-xmin)>max_l: continue if (ymax-ymin)<min_l: continue if (ymax-ymin)>max_l: continue obj['xmin'] = xmin obj['ymin'] = ymin obj['xmax'] = xmax obj['ymax'] = ymax img_data['object'] += [obj] cv2.rectangle(img, (xmin,ymin), (xmax,ymax), (0,255,0), 2) cv2.imwrite(box_name, img) all_imgs += [img_data] #print(all_imgs) print('Saving data to ' + annotations_file) with open(annotations_file, 'w') as handle: yaml.dump(all_imgs, handle) print('Finished! :o)')