def test_train_yolo_framework(setup_model_config, setup_weights_file, setup_dataset_folder, setup_image_and_its_boxes): model_config = setup_model_config pretrained_feature_file, weight_file = setup_weights_file img_folder, ann_folder = setup_dataset_folder # 1. Construct the model yolo = create_yolo(model_config['architecture'], model_config['labels'], model_config['input_size'], model_config['anchors'], pretrained_feature_file) # 2. warmup training yolo.train(img_folder, ann_folder, 2, weight_file, 2, False, 1e-4, 10, 1, 2, img_folder, ann_folder) # 3. Load the warmup trained weights yolo.load_weights(weight_file) # 4. actual training yolo.train(img_folder, ann_folder, 12, weight_file, 2, False, 1e-4, 10, 1, 0, img_folder, ann_folder) # 5. Load training image & predict objects image, true_boxes = setup_image_and_its_boxes boxes, probs = yolo.predict(image) boxes = to_centroid(boxes) assert len(boxes) == 2 assert len(probs) == 2 assert np.allclose(np.argmax(probs, axis=1), [0, 3]) for box, true_box in zip(boxes, true_boxes): iou = centroid_box_iou(box, true_box) assert iou > 0.4
def detector(image_loc): # 1. create yolo instance yolo_detector = create_yolo( "ResNet50", ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"], 416) # 2. load pretrained weighted file DEFAULT_WEIGHT_FILE = os.path.join(yolo.PROJECT_ROOT, "weights.h5") yolo_detector.load_weights(DEFAULT_WEIGHT_FILE) # 3. Load images DEFAULT_IMAGE_FOLDER = "Machine_Learning\\imgs" img_files = [os.path.join(DEFAULT_IMAGE_FOLDER, image_loc)] print(img_files) imgs = [] for fname in img_files: img = cv2.imread(fname) img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) imgs.append(img) #plt.imshow(img) #plt.show() # 4. Predict digit region THRESHOLD = 0.3 for img in imgs: boxes, probs = yolo_detector.predict(img, THRESHOLD) return probs
def initYolo(): # laddar in modellen och Konfigurerar yolo. with open(DEFAULT_CONFIG_FILE) as config_buffer: config = json.loads(config_buffer.read()) if config['train']['is_only_detect']: labels = [''] else: if config['model']['labels']: labels = config['model']['labels'] else: labels = get_object_labels(config['train']['train_annot_folder']) print(labels) # 2. create yolo instance & predict yolo = create_yolo(config['model']['architecture'], labels, config['model']['input_size'], config['model']['anchors']) yolo.load_weights(DEFAULT_WEIGHT_FILE) return yolo,labels
def test_predict(setup_image_and_its_boxes, setup_outputs, setup_config): # 1. Given image, true_boxes = setup_image_and_its_boxes model_config = setup_config desired_boxes, desired_probs = setup_outputs # 2. When run # 1. Construct the model yolo = create_yolo(model_config['architecture'], model_config['labels'], model_config['input_size'], model_config['anchors']) yolo.load_weights(os.path.join(TEST_SAMPLE_DIR, "mobile_288_weights.h5")) boxes, probs = yolo.predict(image) boxes = to_centroid(boxes) assert len(boxes) == 2 assert len(probs) == 2 for box, true_box in zip(boxes, true_boxes): iou = centroid_box_iou(box, true_box) assert iou > 0.5
config, weight_file = setup_training(config_file) if config['train']['is_only_detect']: labels = ["object"] else: if config['model']['labels']: labels = config['model']['labels'] else: labels = get_object_labels(config['train']['train_annot_folder']) print(labels) # 1. Construct the model yolo = create_yolo( config['model']['architecture'], labels, config['model']['input_size'], config['model']['anchors'], config['model']['coord_scale'], config['model']['class_scale'], config['model']['object_scale'], config['model']['no_object_scale']) # 2. Load the pretrained weights (if any) yolo.load_weights(config['pretrained']['full'], by_name=True) with warnings.catch_warnings(): warnings.simplefilter("ignore") # 3. actual training yolo.train(config['train']['train_image_folder'], config['train']['train_annot_folder'], config['train']['actual_epoch'], weight_file, config["train"]["batch_size"], config["train"]["jitter"], config['train']['learning_rate'], config['train']['train_times'],
config, weight_file = setup_training(args.conf) if config['train']['is_only_detect']: labels = ["object"] else: if config['model']['labels']: labels = config['model']['labels'] else: labels = get_object_labels(config['train']['train_annot_folder']) print(labels) # 1. Construct the model yolo = create_yolo(config['model']['architecture'], labels, config['model']['input_size'], config['model']['anchors'], config['model']['coord_scale'], config['model']['class_scale'], config['model']['object_scale'], config['model']['no_object_scale']) # 2. Load the pretrained weights (if any) yolo.load_weights(config['pretrained']['full'], by_name=True) if config['train']['warmup_epoch'] > 0: # 3. warmup training yolo.train(config['train']['train_image_folder'], config['train']['train_annot_folder'], config['train']['warmup_epoch'], weight_file, config["train"]["batch_size"], config["train"]["jitter"],
help='detection threshold') argparser.add_argument('-w', '--weights', default=DEFAULT_WEIGHT_FILE, help='trained weight files') if __name__ == '__main__': # 1. extract arguments args = argparser.parse_args() with open(args.conf) as config_buffer: config = json.loads(config_buffer.read()) # 2. create yolo instance & predict yolo = create_yolo(config['model']['architecture'], config['model']['labels'], config['model']['input_size'], config['model']['anchors']) yolo.load_weights(args.weights) # 3. read image write_dname = "detected" if not os.path.exists(write_dname): os.makedirs(write_dname) annotations = parse_annotation( config['train']['valid_annot_folder'], config['train']['valid_image_folder'], config['model']['labels'], is_only_detect=config['train']['is_only_detect']) n_true_positives = 0 n_truth = 0 n_pred = 0
def test(img_to_predict): # 1. create yolo instance yolo_detector = create_yolo( "ResNet50", ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"], 416) # 2. load pretrained weighted file yolo_detector.load_weights(svhn.DETECTOR_WEIGHT) # 3. load images img_files = [os.path.join(svhn.DEFAULT_IMAGE_FOLDER, img_to_predict)] imgs = [] for fname in img_files: try: img = cv2.imread(fname) img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) imgs.append(img) except cv2.error: print("NO IMAGE FOUND! please place image to be predicted here:{}". format(svhn.DEFAULT_IMAGE_FOLDER)) #4 create Image Detector model input_shape = (32, 32, 3) num_classes = 10 model = Sequential() model.add( Conv2D(32, kernel_size=3, input_shape=input_shape, padding="same")) model.add(BatchNormalization()) model.add(Activation('relu')) model.add(Conv2D(32, 3, padding="same")) model.add(BatchNormalization()) model.add(Activation('relu')) model.add(MaxPooling2D(pool_size=2)) model.add(Dropout(0.3)) model.add(Conv2D(64, 3)) model.add(BatchNormalization()) model.add(Activation('relu')) model.add(Conv2D(64, 3, padding="same")) model.add(BatchNormalization()) model.add(Activation('relu')) model.add(MaxPooling2D(pool_size=2)) model.add(Dropout(0.3)) model.add(Conv2D(128, 3)) model.add(BatchNormalization()) model.add(Activation('relu')) model.add(Conv2D(128, 3, padding="same")) model.add(BatchNormalization()) model.add(Activation('relu')) model.add(MaxPooling2D(pool_size=2)) model.add(Dropout(0.3)) model.add(Flatten()) model.add(Dense(512, activation='relu')) model.add(Dropout(0.3)) model.add(Dense(num_classes, activation='softmax')) model.load_weights('predictor.h5') # 4. Predict digit region for img in imgs: boxes, probs = yolo_detector.predict(img, svhn.THRESHOLD) numbers_predicted = [] for box in sorted(boxes, key=getKey): (x1, y1, x2, y2) = box image = array_to_img(img) stc = image.crop((x1, y1, x2, y2)) stc = stc.resize((32, 32)) stc = img_to_array(stc) stc = stc.astype('float32') stc /= 255 pred = model.predict(stc.reshape(1, 32, 32, 3)) numbers_predicted.append(np.argmax(pred) + 1) return 'number predicted is: ' + ''.join( str(x) for x in numbers_predicted)
os.environ["CUDA_VISIBLE_DEVICES"] = "1" import warnings warnings.filterwarnings("ignore") import numpy as np import cv2 from yolo.backend.utils.box import draw_scaled_boxes import yolo from yolo.frontend import create_yolo # 1. create yolo instance yolo_detector = create_yolo( "ResNet50", ["10", "1", "2", "3", "4", "5", "6", "7", "8", "9"], 416) # 2. load pretrained weighted file # Pretrained weight file is at https://drive.google.com/drive/folders/1Lg3eAPC39G9GwVTCH3XzF73Eok-N-dER DEFAULT_WEIGHT_FILE = os.path.join(yolo.PROJECT_ROOT, "weight/weights.h5") yolo_detector.load_weights(DEFAULT_WEIGHT_FILE) # 3. predict testing images DEFAULT_IMAGE_FOLDER = os.path.join(yolo.PROJECT_ROOT, "data_svhn", "test") THRESHOLD = 0.25 result_list = [] def bbox_format(boxes): '''
def train( self, epochs=100, progress_cb=None, weights=os.path.join(curr_file_dir, "weights", "mobilenet_7_5_224_tf_no_top.h5"), batch_size=5, train_times=5, valid_times=2, learning_rate=1e-4, jitter=False, is_only_detect=False, save_best_weights_path="out/best_weights.h5", save_final_weights_path="out/final_weights.h5", ): import tensorflow as tf from yolo.frontend import create_yolo self.log.i("train, labels:{}".format(self.labels)) self.log.d("train, datasets dir:{}".format(self.datasets_list)) # param check # TODO: check more param if len(self.labels) == 1: is_only_detect = True self.save_best_weights_path = save_best_weights_path self.save_final_weights_path = save_final_weights_path # create yolo model strip_size = 32 if min(self.input_shape[:2]) % 32 == 0 else 16 # get anchors self.anchors = self._get_anchors(self.datasets_ann, self.input_shape[:2], strip_size=strip_size) # create network yolo = create_yolo(architecture="MobileNet", labels=self.labels, input_size=self.input_shape[:2], anchors=self.anchors, coord_scale=1.0, class_scale=1.0, object_scale=5.0, no_object_scale=1.0, weights=weights, strip_size=strip_size) # train self.history = yolo.train( img_folder=None, ann_folder=None, img_in_mem=self.datasets_img, # datasets in mem, format: list ann_in_mem=self. datasets_ann, # datasets's annotation in mem, format: list nb_epoch=epochs, save_best_weights_path=save_best_weights_path, save_final_weights_path=save_final_weights_path, batch_size=batch_size, jitter=jitter, learning_rate=learning_rate, train_times=train_times, valid_times=valid_times, valid_img_folder="", valid_ann_folder="", valid_img_in_mem=self.datasets_val_img, valid_ann_in_mem=self.datasets_val_ann, first_trainable_layer=None, is_only_detect=is_only_detect, progress_callbacks=[ self.Train_progress_cb(epochs, progress_cb, self.log) ])
'--weights', default=DEFAULT_WEIGHT_FILE, help='trained weight files') if __name__ == '__main__': # 1. extract arguments args = argparser.parse_args() with open(args.conf) as config_buffer: config = json.loads(config_buffer.read()) model_config = config['model'] # Todo : pretrained feature message # 2. create yolo instance & predict yolo = create_yolo(model_config['architecture'], model_config['labels'], model_config['input_size'], model_config['anchors'], feature_weights_path=None) yolo.load_weights(args.weights) # 3. read image write_dname = "detected" if not os.path.exists(write_dname): os.makedirs(write_dname) # 4. files = os.listdir(config['train']['valid_annot_folder']) for fname in files: fname_ = os.path.splitext(fname)[0] img_fname = fname_ + ".png" img_path = os.path.join(config['train']['valid_image_folder'],
argparser.add_argument( '-w', '--weights', default=DEFAULT_WEIGHT_FILE, help='trained weight files') if __name__ == '__main__': # 1. extract arguments args = argparser.parse_args() with open(args.conf) as config_buffer: config = json.loads(config_buffer.read()) # 2. create yolo instance & predict yolo = create_yolo(config['model']['architecture'], config['model']['labels'], config['model']['input_size'], config['model']['anchors']) yolo.load_weights(args.weights) # 3. read image write_dname = "detected" if not os.path.exists(write_dname): os.makedirs(write_dname) annotations = parse_annotation(config['train']['valid_annot_folder'], config['train']['valid_image_folder'], config['model']['labels'], is_only_detect=config['train']['is_only_detect']) n_true_positives = 0 n_truth = 0 n_pred = 0 for i in range(len(annotations)):
import cv2 from yolo.backend.utils.box import draw_scaled_boxes from yolo.backend.loss import YoloLoss import os import yolo from keras.models import load_model # %% from yolo.frontend import create_exist_yolo from yolo.frontend import create_yolo # 1. create yolo instance yolo_detector = create_yolo("Full Yolo", ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"], 416) # yolo_loss = YoloLoss(nb_class=10) # custom_loss = yolo_loss.custom_loss(16) # model = load_model('model.h5', custom_objects={'loss_func': custom_loss}) # yolo_detector = create_exist_yolo("Full Yolo", model, ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"], 416) # yolo_detector.summary() # %% # 2. load pretrained weighted file # Pretrained weight file is at https://drive.google.com/drive/folders/1Lg3eAPC39G9GwVTCH3XzF73Eok-N-dER import json DEFAULT_WEIGHT_FILE = os.path.join('svhn', "weights.h5") print(DEFAULT_WEIGHT_FILE)
def __init__(self): self.yolo_detector = create_yolo( "ResNet50", ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"], 416) DEFAULT_WEIGHT_FILE = 'C:\\Users\\Simas\\Desktop\\Insight\\Data Challenges\\House-Number-Detection\\HND\\Yolo\\Yolo-digit-detector\\weights.h5' self.yolo_detector.load_weights(DEFAULT_WEIGHT_FILE)