def main(args):
    config_path = 'config.json'
    config_path = args.conf
    num_anchors = 5

    with open(config_path) as config_buffer:
        config = json.loads(config_buffer.read())

    train_imgs, train_labels = parse_annotation(config['train']['train_annot_folder'],
                                                config['train']['train_image_folder'],
                                                config['model']['labels'])

    grid_w = config['model']['input_size']/32
    grid_h = config['model']['input_size']/32

    # run k_mean to find the anchors
    annotation_dims = []
    for image in train_imgs:
        cell_w = image['width']/grid_w
        cell_h = image['height']/grid_h

        for obj in image['object']:
            relative_w = (float(obj['xmax']) - float(obj['xmin']))/cell_w
            relatice_h = (float(obj["ymax"]) - float(obj['ymin']))/cell_h
            annotation_dims.append(map(float, (relative_w,relatice_h)))
    annotation_dims = np.array(annotation_dims)

    centroids = run_kmeans(annotation_dims, num_anchors)

    # write anchors to file
    print '\naverage IOU for', num_anchors, 'anchors:', '%0.2f' % avg_IOU(annotation_dims, centroids)
    print_anchors(centroids)
Ejemplo n.º 2
0
def main(argv):
    config_path = args.conf
    num_anchors = args.anchors

    with open(config_path) as config_buffer:
        config = json.loads(config_buffer.read())

    train_imgs, train_labels = parse_annotation(config['train']['train_annot_folder'],
                                                config['train']['train_image_folder'],
                                                config['model']['labels'])

    grid_w = config['model']['input_size']/32
    grid_h = config['model']['input_size']/32

    # run k_mean to find the anchors
    annotation_dims = []
    for image in train_imgs:
        cell_w = image['width']/grid_w
        cell_h = image['height']/grid_h

        for obj in image['object']:
            relative_w = (float(obj['xmax']) - float(obj['xmin']))/cell_w
            relatice_h = (float(obj["ymax"]) - float(obj['ymin']))/cell_h
            annotation_dims.append(tuple(map(float, (relative_w,relatice_h))))

    annotation_dims = np.array(annotation_dims)
    centroids = run_kmeans(annotation_dims, num_anchors)

    # write anchors to file
    print('\naverage IOU for', num_anchors, 'anchors:', '%0.2f' % avg_IOU(annotation_dims, centroids))
    print_anchors(centroids)
Ejemplo n.º 3
0
def load_data_generators(generator_config, args):
    pickle_train = 'data/TrainAnn_hydr.pickle'
    pickle_val = 'data/ValAnn_hydr.pickle'
    train_image_folder = os.path.join(args.input, "image/")
    train_annot_folder = os.path.join(args.input, "labels/")
    valid_image_folder = train_image_folder
    valid_annot_folder = train_annot_folder
    if os.path.isfile(pickle_train):
        print("\n=== WARNING!!! Opening an old pickled file!!! ===")
        with open(pickle_train, 'rb') as fp:
            train_imgs = pickle.load(fp)
    else:
        train_imgs, seen_train_labels = parse_annotation(
            train_annot_folder,
            train_image_folder,
            labels=generator_config['LABELS'])
        with open(pickle_train, 'wb') as fp:
            pickle.dump(train_imgs, fp)

    if os.path.isfile(pickle_val):
        with open(pickle_val, 'rb') as fp:
            valid_imgs = pickle.load(fp)
    else:
        valid_imgs, seen_valid_labels = parse_annotation(
            valid_annot_folder,
            valid_image_folder,
            labels=generator_config['LABELS'])
        with open(pickle_val, 'wb') as fp:
            pickle.dump(valid_imgs, fp)

    train_batch = BatchSequenceGenerator(train_imgs,
                                         generator_config,
                                         norm=None,
                                         shuffle=True,
                                         augment=False)
    valid_batch = BatchSequenceGenerator(valid_imgs,
                                         generator_config,
                                         norm=None,
                                         augment=False)
    return train_batch, valid_batch
def main(argv):
    config_path = args.conf
    initial_num_anchors = args.anchors
    print("Generating %d anchors..." % initial_num_anchors)

    with open(config_path) as config_buffer:
        config = json.loads(config_buffer.read())

    print('labels:\n')
    print(config['model']['labels'])

    train_imgs, train_labels = parse_annotation(
        config['train']['train_annot_folder'],
        config['train']['train_image_folder'], config['model']['labels'])

    grid_w = config['model']['input_size'] / 32
    grid_h = config['model']['input_size'] / 32

    #anchorsFinalAcceptableIou = 0.9
    #anchorsAvgIou = 0
    #num_anchors = initial_num_anchors
    #hsItCounter = 0

    #while anchorsAvgIou < anchorsFinalAcceptableIou:
    # run k_mean to find the anchors
    annotation_dims = []
    num_wrong = 0
    for image in train_imgs:
        cell_w = image['width'] / grid_w
        cell_h = image['height'] / grid_h

        for obj in image['object']:
            relative_w = (float(obj['xmax']) - float(obj['xmin'])) / cell_w
            relative_h = (float(obj["ymax"]) - float(obj['ymin'])) / cell_h
            if not (relative_w < 0 or relative_h < 0):
                annotation_dims.append(
                    tuple(map(float, (relative_w, relative_h))))
            else:
                num_wrong += 1
                print("WRONG! ", num_wrong)

    annotation_dims = np.array(annotation_dims)
    centroids = run_kmeans(annotation_dims, initial_num_anchors)

    # write anchors to file
    anchorsAvgIou = avg_IOU(annotation_dims, centroids)
    print('\naverage IOU for', initial_num_anchors, 'anchors:',
          '%0.2f' % anchorsAvgIou)
    print_anchors(centroids)
Ejemplo n.º 5
0
def main(argv):
    config_path = args.conf
    num_anchors = args.anchors

    with open(config_path) as config_buffer:
        config = json.loads(config_buffer.read())

    if config['parser_annotation_type'] == 'xml':
        # parse annotations of the training set
        train_imgs, train_labels = parse_annotation(
            config['train']['train_annot_folder'],
            config['train']['train_image_folder'], config['model']['labels'])
    elif config['parser_annotation_type'] == 'csv':
        # parse annotations of the training set
        train_imgs, train_labels = parse_annotation_csv(
            config['train']['train_csv_file'], config['model']['labels'],
            config['train']['train_csv_base_path'])
    input_size = (config['model']['input_size_h'],
                  config['model']['input_size_w'], 1)
    feature_extractor = import_feature_extractor(config['model']['backend'],
                                                 input_size)
    grid_w = config['model'][
        'input_size_w'] / feature_extractor.get_output_shape()[1]
    grid_h = config['model'][
        'input_size_h'] / feature_extractor.get_output_shape()[0]

    # run k_mean to find the anchors
    annotation_dims = []
    for image in train_imgs:
        cell_w = image['width'] / grid_w
        cell_h = image['height'] / grid_h

        for obj in image['object']:
            relative_w = (float(obj['xmax']) - float(obj['xmin'])) / cell_w
            relatice_h = (float(obj["ymax"]) - float(obj['ymin'])) / cell_h
            annotation_dims.append(tuple(map(float, (relative_w, relatice_h))))

    annotation_dims = np.array(annotation_dims)
    centroids = run_kmeans(annotation_dims, num_anchors)

    # write anchors to file
    print('\naverage IOU for', num_anchors, 'anchors:',
          '%0.2f' % avg_IOU(annotation_dims, centroids))
    print_anchors(centroids)
def _main_(args):
    config_path = args.conf
    weights_path = args.weights

    with open(config_path) as config_buffer:
        config = json.load(config_buffer)

    yolo = YOLO(backend=config['model']['backend'],
                input_size=config['model']['input_size'],
                labels=config['model']['labels'],
                max_box_per_image=config['model']['max_box_per_image'],
                anchors=config['model']['anchors'])

    yolo.load_weights(weights_path)

    valid_imgs, valid_labels = parse_annotation(
        config['valid']['valid_annot_folder'],
        config['valid']['valid_image_folder'], config['model']['labels'])
    yolo.batch_size = config['train']['batch_size']
    yolo.sequence_length = 1
    generator_config = {
        'IMAGE_H': yolo.input_size,
        'IMAGE_W': yolo.input_size,
        'GRID_H': yolo.grid_h,
        'GRID_W': yolo.grid_w,
        'BOX': yolo.nb_box,
        'LABELS': yolo.labels,
        'CLASS': len(yolo.labels),
        'ANCHORS': yolo.anchors,
        'BATCH_SIZE': yolo.batch_size,
        'TRUE_BOX_BUFFER': yolo.max_box_per_image,
        'SEQUENCE_LENGTH': yolo.sequence_length
    }
    valid_generator = BatchGenerator(valid_imgs,
                                     generator_config,
                                     norm=yolo.feature_extractor.normalize,
                                     jitter=False)
    ave_precisions = yolo.evaluate(valid_generator,
                                   iou_threshold=0.3,
                                   score_threshold=0.2)
    print("ave precisions: ", ave_precisions)
    print('mAP: {:.4f}'.format(
        sum(ave_precisions.values()) / len(ave_precisions)))
Ejemplo n.º 7
0
def main(args):
    config_path = args.conf
    num_anchors = args.anchors

    with open(config_path) as config_buffer:
        config = json.loads(config_buffer.read())

    train_imgs, train_labels = parse_annotation(
        config['train']['train_annot_folder'],
        config['train']['train_image_folder'], config['model']['labels'])

    if not train_imgs:
        print('ERROR: No images found for labels {}'.format(
            config['model']['labels']))
        return

    grid_w = config['model']['input_size'] / 32
    grid_h = config['model']['input_size'] / 32

    # run k_mean to find the anchors
    annotation_dims = []
    for image in train_imgs:
        cell_w = image['width'] / grid_w
        cell_h = image['height'] / grid_h

        for obj in image['object']:
            relative_w = (float(obj['xmax']) - float(obj['xmin'])) / cell_w
            relatice_h = (float(obj["ymax"]) - float(obj['ymin'])) / cell_h
            annotation_dims.append(tuple(map(float, (relative_w, relatice_h))))

    annotation_dims = np.array(annotation_dims)
    centroids = run_kmeans(annotation_dims, num_anchors)

    # write anchors to file
    print('\naverage IOU for', num_anchors, 'anchors:',
          '%0.2f' % avg_IOU(annotation_dims, centroids))
    print_anchors(centroids)
    config['model']['anchors'] = np.array(sorted(
        centroids.tolist())).flatten().tolist()

    with open(config_path, 'w') as config_buffer:
        json.dump(config, config_buffer)
Ejemplo n.º 8
0
def get_anchors(config, num_anchors=5):
    train_imgs, train_labels = parse_annotation(config['train_annot_folder'],
                                                config['train_image_folder'],
                                                config['LABELS'])
    grid_w = config['GRID_W']
    grid_h = config['GRID_H']
    annotation_dims = []
    for image in train_imgs:
        cell_w = image['width'] / grid_w
        cell_h = image['height'] / grid_h

        for obj in image['object']:
            relative_w = (float(obj['xmax']) - float(obj['xmin'])) / cell_w
            relatice_h = (float(obj["ymax"]) - float(obj['ymin'])) / cell_h
            annotation_dims.append(tuple(map(float, (relative_w, relatice_h))))

    annotation_dims = np.array(annotation_dims)
    centroids = run_kmeans(annotation_dims, num_anchors)

    return centroids
Ejemplo n.º 9
0
 def write_all_missing_fields(self):
   self.config['wt_path'] = self.wt_path
   self.config['train_annot_folder'] = self.train_annot_folder
   self.config['train_image_folder'] = self.train_image_folder
   self.config['valid_annot_folder'] = self.valid_annot_folder
   self.config['valid_image_folder'] = self.valid_image_folder
   self.config['model_save_path'] = self.model_save_path
   _, seen_labels = parse_annotation(
       self.train_annot_folder,
       self.train_image_folder,
       labels=[],
       onlyInLabels=False
   )
   LABELS = sorted(seen_labels.keys())
   self.config['LABELS'] = LABELS
   self.config['CLASS'] = len(LABELS)
   self.config['CLASS_WEIGHTS'] = [1.0] * len(LABELS)
   random.seed(self.config['random_seed'])
   anchors = sorted_anchors(get_anchors(self.config))
   self.config['ANCHORS'] = anchors
Ejemplo n.º 10
0
def main(argv):
    config_path = args.conf
    num_anchors = int(args.anchors)
    class_name = args.name

    with open(config_path) as config_buffer:
        config = json.loads(config_buffer.read())

    train_imgs, train_labels = parse_annotation(
        config['train']['train_annot_folder'],
        config['train']['train_image_folder'], config['model']['labels'])

    grid_w = config['model']['input_size'] / 32
    grid_h = config['model']['input_size'] / 32

    # run k_mean to find the anchors
    annotation_dims = []
    for image in train_imgs:
        cell_w = image['width'] / grid_w
        cell_h = image['height'] / grid_h

        for obj in image['object']:
            if obj['name'] == class_name or not class_name:
                relative_w = (float(obj['xmax']) - float(obj['xmin'])) / cell_w
                relatice_h = (float(obj["ymax"]) - float(obj['ymin'])) / cell_h
                annotation_dims.append(map(float, (relative_w, relatice_h)))
    annotation_dims = np.array(annotation_dims)

    centroids = run_kmeans(annotation_dims, num_anchors)

    # write anchors to file
    print('\naverage IOU for', num_anchors, 'anchors:',
          '%0.2f' % avg_IOU(annotation_dims, centroids))
    print_anchors(centroids)

    centroids_real_value = np.zeros(centroids.shape)
    centroids_real_value[:, 0] = centroids[:, 0] * cell_w
    centroids_real_value[:, 1] = centroids[:, 1] * cell_h

    print('Real value:')
    print_anchors(centroids_real_value)
Ejemplo n.º 11
0
def main():
    #LABELS = ['traffic_sign']
    LABELS = [
        "aeroplane", "bicycle", "bird", "boat", "bottle", "bus", "car", "cat",
        "chair", "cow", "diningtable", "dog", "horse", "motorbike",
        "pottedplant", "sheep", "sofa", "train", "tvmonitor", "person"
    ]
    #train_imgs_GTSDB,train_labels_GTSDB = parse_GTSDB_dataset()
    # train_imgs, train_labels = parse_annotation('/data4/sunsiyuan/data/LISA/Annotations/',
    #                                             '/data4/sunsiyuan/data/LISA/JPEGImages/',
    #                                              LABELS)
    train_imgs, train_labels = parse_annotation(
        '/data4/sunsiyuan/data/VOC/VOCdevkit/VOC2012/Annotations/',
        '/data4/sunsiyuan/data/VOC/VOCdevkit/VOC2012/JPEGImages/', LABELS)
    print len(train_imgs)
    print train_labels
    #train_imgs.extend(train_imgs_GTSDB)
    #train_labels['traffic_sign'] =  train_labels['traffic_sign'] + train_labels_GTSDB['traffic_sign']
    num_anchors = 5
    grid_w = 13
    grid_h = 13

    # run k_mean to find the anchors
    annotation_dims = []
    for image in train_imgs:
        cell_w = image['width'] / grid_w
        cell_h = image['height'] / grid_h

        for obj in image['object']:
            relative_w = (float(obj['xmax']) - float(obj['xmin'])) / cell_w
            relatice_h = (float(obj["ymax"]) - float(obj['ymin'])) / cell_h
            #print (relatice_h,relative_w)
            annotation_dims.append(map(float, (relative_w, relatice_h)))
    annotation_dims = np.array(annotation_dims)
    centroids = run_kmeans(annotation_dims, num_anchors)

    # write anchors to file
    print '\naverage IOU for', num_anchors, 'anchors:', '%0.2f' % avg_IOU(
        annotation_dims, centroids)
    print_anchors(centroids)
Ejemplo n.º 12
0
def main(argv):
    config_path = args.conf
    num_anchors = args.anchors
    train_annot_folder = '/home/sathish/gitRepos/YOLO/darkflow/custom_dataset/BT/Train/Annotation/'
    train_image_folder = '/home/sathish/gitRepos/YOLO/darkflow/custom_dataset/BT/Train/Img/'
    labels = ['gooddayOrangeCashew', 'parleg']
    #with open(config_path) as config_buffer:
        #config = json.loads(config_buffer.read())

    train_imgs, train_labels = parse_annotation(train_annot_folder, train_image_folder, labels)
    

    grid_w = int(256/32)
    grid_h = int(256/32)

    # run k_mean to find the anchors
    annotation_dims = []
    for image in train_imgs:
        cell_w = image['width']/grid_w
        #if image['width'] == 0:
          #print(image)
        cell_h = image['height']/grid_h
        #print(cell_h)

        for obj in image['object']:
            print(obj)
            relative_w = (float(obj['xmax']) - float(obj['xmin']))/cell_w
            relatice_h = (float(obj["ymax"]) - float(obj['ymin']))/cell_h
            annotation_dims.append(tuple(map(float, (relative_w, relatice_h))))
            print('shape of ann:--', annotation_dims[0])
    annotation_dims = np.array(annotation_dims)
    print(annotation_dims)

    centroids = run_kmeans(annotation_dims, num_anchors)

    # write anchors to file
    print('\naverage IOU for', num_anchors, 'anchors:', '%0.2f' % avg_IOU(annotation_dims, centroids))
    print_anchors(centroids)
Ejemplo n.º 13
0
def mymain(config_path, num_anchors):
    #config_path = args.conf
    #num_anchors = args.anchors

    with open(config_path) as config_buffer:
        config = json.loads(config_buffer.read())

    train_imgs, train_labels = parse_annotation(
        config['train']['train_annot_folder'],
        config['train']['train_image_folder'], config['model']['labels'])

    print("Anno processed")

    inp_size = config['model']['input_size']
    grid_w = inp_size[0] / 32
    grid_h = inp_size[1] / 32

    # run k_mean to find the anchors
    annotation_dims = []
    for image in train_imgs:
        cell_w = image['width'] / grid_w
        cell_h = image['height'] / grid_h

        for obj in image['object']:
            relative_w = (float(obj['xmax']) - float(obj['xmin'])) / cell_w
            relative_h = (float(obj["ymax"]) - float(obj['ymin'])) / cell_h
            annotation_dims.append([relative_w, relative_h])
    annotation_dims = np.array(annotation_dims)

    centroids = run_kmeans(annotation_dims, num_anchors)

    # write anchors to file
    anchors = avg_IOU(annotation_dims, centroids)
    print('\naverage IOU for {} anchors: \n'.format(num_anchors))
    print(anchors)
    print_anchors(centroids)
no_object_scale = 1.0
coord_scale = 1.0
class_scale = 1.0

train_times = 5
valid_times = 1

architecture = "Tiny Yolo"
input_size = 416
max_box_per_img = 10
anchors = [
    0.57273, 0.677385, 1.87446, 2.06253, 3.33843, 5.47434, 7.88282, 3.52778,
    9.77052, 9.16828
]

imgs, labels = parse_annotation(train_annot_folder, train_image_folder, labels)

train_valid_split = int(0.8 * len(imgs))
np.random.shuffle(imgs)

valid_imgs = imgs[train_valid_split:]
train_imgs = imgs[:train_valid_split]

overlap_labels = set(labels).intersection(set(labels.keys()))

# print("Seen labels: "+str(labels))
# print("Given labels: "+str(labels)
print("Overelap labels: " + str(overlap_labels))

if len(overlap_labels) < len(labels):
    print("Some labels have no image! Please check it.")
Ejemplo n.º 15
0
def main():
    argparser = argparse.ArgumentParser(description=PARSER_HELP_STR)

    argparser.add_argument('-c',
                           '--conf',
                           default='config.json',
                           help='path to configuration file')
    argparser.add_argument('-d',
                           '--imgdir',
                           default='.',
                           help='path to image directory')
    argparser.add_argument('-s',
                           '--suffix',
                           default='',
                           help='image file suffix')
    argparser.add_argument('-m',
                           '--multi_image',
                           action='store_true',
                           help='show multi-band images in separate plots')

    args = argparser.parse_args()
    conf_type = os.path.splitext(args.conf)[1]
    if conf_type == '.json':
        from preprocessing import parse_annotation
        with open(args.conf) as cfb:
            config = json.loads(cfb.read())
        train_imgs, _ = parse_annotation(config['train']['train_annot_folder'],
                                         config['train']['train_image_folder'],
                                         config['model']['labels'])
    elif conf_type == '.txt':
        filenames = [x for x in os.listdir(args.imgdir) if is_image(x)]
        if not filenames:
            print('No images found in {}'.format(args.imgdir))
            return
        train_imgs = []
        with open(args.conf) as txt:
            for line in txt.readlines():
                item = {}
                data = line.strip().split(' ')
                poss_imgs = [f for f in filenames if data[0] in f]
                print(len(poss_imgs), 'found for', data[0])
                if args.suffix:
                    poss_imgs = [f for f in poss_imgs if args.suffix in f]
                    if poss_imgs:
                        item['filename'] = os.path.join(
                            args.imgdir, poss_imgs[0])
                    else:
                        print('No images found with suffix {}'.format(
                            args.suffix))
                else:
                    item['filename'] = os.path.join(args.imgdir, poss_imgs[0])
                item['object'] = [
                    zip(['xmin', 'ymax', 'xmax', 'ymin'],
                        [float(x) for x in data[5:]])
                ]
                train_imgs.append(item)
        print(train_imgs)
    else:
        print('Invalid config file {}'.format(args.conf))
        return

    for imobj in train_imgs:
        plot_image(imobj['filename'],
                   imobj['object'][0],
                   separate=args.multi_image)
Ejemplo n.º 16
0
generator_config = {
    'IMAGE_H': IMAGE_H,
    'IMAGE_W': IMAGE_W,
    'GRID_H': GRID_H,
    'GRID_W': GRID_W,
    'BOX': BOX,
    'LABELS': LABELS,
    'CLASS': len(LABELS),
    'ANCHORS': ANCHORS,
    'BATCH_SIZE': BATCH_SIZE,
    'TRUE_BOX_BUFFER': 50,
}
image_path = './raccoon_dataset-master/images/'
annot_path = './raccoon_dataset-master/annotations/'

all_imgs, seen_labels = parse_annotation(annot_path, image_path)  #no labels?

# from random import shuffle
# shuffle(all_imgs)
#
# with open('imgs_racoon_shfld', 'wb') as fp:
#    pickle.dump(all_imgs, fp)
#
# with open('imgs_racoon_shfld', 'rb') as fp:
#     all_imgs = pickle.load(fp)


def normalize(image):
    return image / 255.

Ejemplo n.º 17
0
with open(config_path) as config_buffer:    
    config = json.loads(config_buffer.read())

yolo = YOLO(backend             = None,
            input_size          = config['model']['input_size'], 
            labels              = config['model']['labels'], 
            max_box_per_image   = config['model']['max_box_per_image'],
            anchors             = config['model']['anchors'], 
            load_from_json      = model_path,
            trained_weights     = weights_path)
yolo.set_batch(config['train']['batch_size'])

#TODO insert bactch generator here
if os.path.exists(config['valid']['valid_annot_folder']):
    valid_imgs, valid_labels = parse_annotation(config['valid']['valid_annot_folder'], 
                                                config['valid']['valid_image_folder'], 
                                                config['model']['labels'])
else:
    valid_imgs, valid_labels = parse_annotation(config['train']['train_annot_folder'], 
                                            config['train']['train_image_folder'], 
                                            config['model']['labels'])

valid_generator = yolo.get_generator_from_data(valid_imgs)

average_precisions, average_speed = yolo.evaluate(valid_generator)


# print evaluation
for label, average_precision in average_precisions.items():
    print(yolo.get_label(label), '{:.4f}'.format(average_precision))
print('mAP: {:.4f}'.format(sum(average_precisions.values()) / len(average_precisions)))
Ejemplo n.º 18
0
def makemodel():
    # Layer 1
    x = Conv2D(32, (3,3), strides=(1,1), padding='same', name='conv_1', use_bias=False)(input_image)
    x = BatchNormalization(name='norm_1')(x)
    x = LeakyReLU(alpha=0.1)(x)
    x = MaxPooling2D(pool_size=(2, 2))(x)

    # Layer 2
    x = Conv2D(64, (3,3), strides=(1,1), padding='same', name='conv_2', use_bias=False)(x)
    x = BatchNormalization(name='norm_2')(x)
    x = LeakyReLU(alpha=0.1)(x)
    x = MaxPooling2D(pool_size=(2, 2))(x)

    # Layer 3
    x = Conv2D(128, (3,3), strides=(1,1), padding='same', name='conv_3', use_bias=False)(x)
    x = BatchNormalization(name='norm_3')(x)
    x = LeakyReLU(alpha=0.1)(x)

    # Layer 4
    x = Conv2D(64, (1,1), strides=(1,1), padding='same', name='conv_4', use_bias=False)(x)
    x = BatchNormalization(name='norm_4')(x)
    x = LeakyReLU(alpha=0.1)(x)

    # Layer 5
    x = Conv2D(128, (3,3), strides=(1,1), padding='same', name='conv_5', use_bias=False)(x)
    x = BatchNormalization(name='norm_5')(x)
    x = LeakyReLU(alpha=0.1)(x)
    x = MaxPooling2D(pool_size=(2, 2))(x)

    # Layer 6
    x = Conv2D(256, (3,3), strides=(1,1), padding='same', name='conv_6', use_bias=False)(x)
    x = BatchNormalization(name='norm_6')(x)
    x = LeakyReLU(alpha=0.1)(x)

    # Layer 7
    x = Conv2D(128, (1,1), strides=(1,1), padding='same', name='conv_7', use_bias=False)(x)
    x = BatchNormalization(name='norm_7')(x)
    x = LeakyReLU(alpha=0.1)(x)

    # Layer 8
    x = Conv2D(256, (3,3), strides=(1,1), padding='same', name='conv_8', use_bias=False)(x)
    x = BatchNormalization(name='norm_8')(x)
    x = LeakyReLU(alpha=0.1)(x)
    x = MaxPooling2D(pool_size=(2, 2))(x)

    # Layer 9
    x = Conv2D(512, (3,3), strides=(1,1), padding='same', name='conv_9', use_bias=False)(x)
    x = BatchNormalization(name='norm_9')(x)
    x = LeakyReLU(alpha=0.1)(x)

    # Layer 10
    x = Conv2D(256, (1,1), strides=(1,1), padding='same', name='conv_10', use_bias=False)(x)
    x = BatchNormalization(name='norm_10')(x)
    x = LeakyReLU(alpha=0.1)(x)

    # Layer 11
    x = Conv2D(512, (3,3), strides=(1,1), padding='same', name='conv_11', use_bias=False)(x)
    x = BatchNormalization(name='norm_11')(x)
    x = LeakyReLU(alpha=0.1)(x)

    # Layer 12
    x = Conv2D(256, (1,1), strides=(1,1), padding='same', name='conv_12', use_bias=False)(x)
    x = BatchNormalization(name='norm_12')(x)
    x = LeakyReLU(alpha=0.1)(x)

    # Layer 13
    x = Conv2D(512, (3,3), strides=(1,1), padding='same', name='conv_13', use_bias=False)(x)
    x = BatchNormalization(name='norm_13')(x)
    x = LeakyReLU(alpha=0.1)(x)

    skip_connection = x

    x = MaxPooling2D(pool_size=(2, 2))(x)

    # Layer 14
    x = Conv2D(1024, (3,3), strides=(1,1), padding='same', name='conv_14', use_bias=False)(x)
    x = BatchNormalization(name='norm_14')(x)
    x = LeakyReLU(alpha=0.1)(x)

    # Layer 15
    x = Conv2D(512, (1,1), strides=(1,1), padding='same', name='conv_15', use_bias=False)(x)
    x = BatchNormalization(name='norm_15')(x)
    x = LeakyReLU(alpha=0.1)(x)

    # Layer 16
    x = Conv2D(1024, (3,3), strides=(1,1), padding='same', name='conv_16', use_bias=False)(x)
    x = BatchNormalization(name='norm_16')(x)
    x = LeakyReLU(alpha=0.1)(x)

    # Layer 17
    x = Conv2D(512, (1,1), strides=(1,1), padding='same', name='conv_17', use_bias=False)(x)
    x = BatchNormalization(name='norm_17')(x)
    x = LeakyReLU(alpha=0.1)(x)

    # Layer 18
    x = Conv2D(1024, (3,3), strides=(1,1), padding='same', name='conv_18', use_bias=False)(x)
    x = BatchNormalization(name='norm_18')(x)
    x = LeakyReLU(alpha=0.1)(x)

    # Layer 19
    x = Conv2D(1024, (3,3), strides=(1,1), padding='same', name='conv_19', use_bias=False)(x)
    x = BatchNormalization(name='norm_19')(x)
    x = LeakyReLU(alpha=0.1)(x)

    # Layer 20
    x = Conv2D(1024, (3,3), strides=(1,1), padding='same', name='conv_20', use_bias=False)(x)
    x = BatchNormalization(name='norm_20')(x)
    x = LeakyReLU(alpha=0.1)(x)

    # Layer 21
    skip_connection = Conv2D(64, (1,1), strides=(1,1), padding='same', name='conv_21', use_bias=False)(skip_connection)
    skip_connection = BatchNormalization(name='norm_21')(skip_connection)
    skip_connection = LeakyReLU(alpha=0.1)(skip_connection)
    skip_connection = Lambda(space_to_depth_x2)(skip_connection)

    x = concatenate([skip_connection, x])

    # Layer 22
    x = Conv2D(1024, (3,3), strides=(1,1), padding='same', name='conv_22', use_bias=False)(x)
    x = BatchNormalization(name='norm_22')(x)
    x = LeakyReLU(alpha=0.1)(x)

    # Layer 23
    x = Conv2D(BOX * (4 + 1 + CLASS), (1,1), strides=(1,1), padding='same', name='conv_23')(x)
    output = Reshape((GRID_H, GRID_W, BOX, 4 + 1 + CLASS))(x)

    # small hack to allow true_boxes to be registered when Keras build the model 
    # for more information: https://github.com/fchollet/keras/issues/2790
    output = Lambda(lambda args: args[0])([output, true_boxes])

    model = Model([input_image, true_boxes], output)


    train_imgs, seen_train_labels = parse_annotation(train_annot_folder, train_image_folder, labels=LABELS)
    ### write parsed annotations to pickle for fast retrieval next time
    #with open('train_imgs', 'wb') as fp:
    #    pickle.dump(train_imgs, fp)

    ### read saved pickle of parsed annotations
    #with open ('train_imgs', 'rb') as fp:
    #    train_imgs = pickle.load(fp)
    train_batch = BatchGenerator(train_imgs, generator_config, norm=normalize)
    
    valid_imgs, seen_valid_labels = parse_annotation(valid_annot_folder, valid_image_folder, labels=LABELS)
    ### write parsed annotations to pickle for fast retrieval next time
    #with open('valid_imgs', 'wb') as fp:
    #    pickle.dump(valid_imgs, fp)

    ### read saved pickle of parsed annotations
    #with open ('valid_imgs', 'rb') as fp:
    #    valid_imgs = pickle.load(fp)

    optimizer = Adam(lr=0.5e-4, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.0)
    #optimizer = SGD(lr=1e-4, decay=0.0005, momentum=0.9)
    #optimizer = RMSprop(lr=1e-4, rho=0.9, epsilon=1e-08, decay=0.0)

    model.compile(loss=custom_loss, optimizer=optimizer)

    return model
Ejemplo n.º 19
0
def _main_(args):

    config_path = args.conf

    with open(config_path) as config_buffer:    
        config = json.loads(config_buffer.read())

    ###############################
    #   Parse the annotations 
    ###############################

    # parse annotations of the training set
    train_imgs, train_labels = parse_annotation(config['train']['train_annot_folder'], 
                                                config['train']['train_image_folder'], 
                                                config['model']['labels'])

    # parse annotations of the validation set, if any, otherwise split the training set
    if os.path.exists(config['valid']['valid_annot_folder']):
        valid_imgs, valid_labels = parse_annotation(config['valid']['valid_annot_folder'], 
                                                    config['valid']['valid_image_folder'], 
                                                    config['model']['labels'])
    else:
        train_valid_split = int(0.8*len(train_imgs))
        np.random.shuffle(train_imgs)

        valid_imgs = train_imgs[train_valid_split:]
        train_imgs = train_imgs[:train_valid_split]

    print train_labels

    if len(set(config['model']['labels']).intersection(set(train_labels.keys()))) == 0:
        print "Labels to be detected are not present in the dataset! Please revise the list of labels in the config.json file!"
        return

    ###############################
    #   Construct the model 
    ###############################

    yolo = YOLO(architecture        = config['model']['architecture'],
                input_size          = config['model']['input_size'], 
                labels              = config['model']['labels'], 
                max_box_per_image   = config['model']['max_box_per_image'],
                anchors             = config['model']['anchors'])

    ###############################
    #   Load the pretrained weights (if any) 
    ###############################    

    if os.path.exists(config['train']['pretrained_weights']):
        print "Loading pre-trained weights in", config['train']['pretrained_weights']
        yolo.load_weights(config['train']['pretrained_weights'])

    ###############################
    #   Start the training process 
    ###############################

    yolo.train(train_imgs         = train_imgs,
               valid_imgs         = valid_imgs,
               train_times        = config['train']['train_times'],
               valid_times        = config['valid']['valid_times'],
               nb_epoch           = config['train']['nb_epoch'], 
               learning_rate      = config['train']['learning_rate'], 
               batch_size         = config['train']['batch_size'],
               warmup_bs          = config['train']['warmup_batches'],
               object_scale       = config['train']['object_scale'],
               no_object_scale    = config['train']['no_object_scale'],
               coord_scale        = config['train']['coord_scale'],
               class_scale        = config['train']['class_scale'],
               saved_weights_name = config['train']['saved_weights_name'],
               debug              = config['train']['debug'])
Ejemplo n.º 20
0
def _main_(args):
    config_path = args.conf

    with open(config_path) as config_buffer:
        config = json.loads(config_buffer.read())

    ###############################
    #   Parse the annotations
    #   预处理部分
    ###############################

    # parse annotations of the training set
    """
    train_imgs是一个list,每个元素是一个dict,里面包括了每个图片的信息:
    img['filename']: str, 图片完整路径
    img['width']: int, 图片宽度
    img['height']: int, 图片长度
    img['object']: list, 所有标注object的list,每个object是一个dict
        obj['name']: str object的label名字
        obj['xmin']: int
        obj['xmax']: int
        obj['ymin']: int
        obj['ymax']: int
    
    train label是一个dictionary,key为label,value为出现的次数
    """

    train_imgs, train_labels = parse_annotation(
        config['train']['train_annot_folder'],
        config['train']['train_image_folder'], config['model']['labels'])

    # parse annotations of the validation set, if any, otherwise split the training set
    np.random.shuffle(train_imgs)
    if os.path.exists(config['valid']['valid_annot_folder']):
        valid_imgs, valid_labels = parse_annotation(
            config['valid']['valid_annot_folder'],
            config['valid']['valid_image_folder'], config['model']['labels'])
    else:
        train_valid_split = int(0.8 * len(train_imgs))

        valid_imgs = train_imgs[train_valid_split:]
        train_imgs = train_imgs[:train_valid_split]

    if len(config['model']['labels']) > 0:
        overlap_labels = set(config['model']['labels']).intersection(
            set(train_labels.keys()))

        print('Seen labels:\t', train_labels)
        print('Given labels:\t', config['model']['labels'])
        print('Overlap labels:\t', overlap_labels)

        if len(overlap_labels) < len(config['model']['labels']):
            print(
                'Some labels have no annotations! Please revise the list of labels in the config.json file!'
            )
            return
    else:
        print('No labels are provided. Train on all seen labels.')
        print(f"labels:{train_labels}")
        config['model']['labels'] = train_labels.keys()

    ###############################
    #   Construct the model
    ###############################
    """
    这里创建了一个yolo类型的对象
    模型特征提取部分使用config里backend的设置
    注意这里的模型建立只对模型的特征提取部分,即卷积部分做了构建,没有对后面的分类部分,即全连接层做构建
    labels是config中定义的labels,当空时为全部找到的label(看preprocessing的代码)
    max_box_per_image顾名思义,但是注意在config中的anchors的设置数量一定要一样,不然没有意义
    anchors到底是什么可以看论文中对anchor权重的说明。
    """
    yolo = YOLO(backend=config['model']['backend'],
                input_size=config['model']['input_size'],
                labels=config['model']['labels'],
                max_box_per_image=config['model']['max_box_per_image'],
                anchors=config['model']['anchors'])

    ###############################
    #   Load the pretrained weights (if any)
    """
    注意这里load的weight是load之前训练好的
    也就是说,之前跑过一遍这个代码训练出来的weight才能fit上去。
    因为其实这里结构比较特殊,特征提取部分被独立成一个layer了
    """
    ###############################

    if os.path.exists(config['train']['pretrained_weights']):
        print("Loading pre-trained weights in",
              config['train']['pretrained_weights'])
        yolo.load_weights(config['train']['pretrained_weights'])

    ###############################
    #   Start the training process
    ###############################

    yolo.train(train_imgs=train_imgs,
               valid_imgs=valid_imgs,
               train_times=config['train']['train_times'],
               valid_times=config['valid']['valid_times'],
               nb_epochs=config['train']['nb_epochs'],
               learning_rate=config['train']['learning_rate'],
               batch_size=config['train']['batch_size'],
               warmup_epochs=config['train']['warmup_epochs'],
               object_scale=config['train']['object_scale'],
               no_object_scale=config['train']['no_object_scale'],
               coord_scale=config['train']['coord_scale'],
               class_scale=config['train']['class_scale'],
               saved_weights_name=config['train']['saved_weights_name'],
               debug=config['train']['debug'])
Ejemplo n.º 21
0
import json
import pandas as pd

manual_check = pd.read_csv(
    "../large_dataset/whale_box/yolo_box/manual_check_train.csv")
df = pd.read_csv("../large_dataset/whale_box/yolo_box/train_selected.csv")
with open('config.json') as config_buffer:
    config = json.loads(config_buffer.read())

###############################
#   Parse the annotations
###############################

# parse annotations of the training set
train_imgs, train_labels = parse_annotation(
    config['train']['missed_annot_folder'],
    config['train']['missed_image_folder'], config['model']['labels'])

for i in range(len(train_imgs)):
    head, tail = os.path.split(train_imgs[i]['filename'])
    manual_check.at[manual_check.Image == tail,
                    'Xmin'] = train_imgs[i]['object'][0]['xmin']
    manual_check.at[manual_check.Image == tail,
                    'Ymin'] = train_imgs[i]['object'][0]['ymin']
    manual_check.at[manual_check.Image == tail,
                    'Xmax'] = train_imgs[i]['object'][0]['xmax']
    manual_check.at[manual_check.Image == tail,
                    'Ymax'] = train_imgs[i]['object'][0]['ymax']

manual_check_final = manual_check.dropna()
#manual_check_final.to_csv('manual_check_final.csv', encoding='utf-8', index=False)
Ejemplo n.º 22
0
def _main_(args):
    print('Hello World! This is {:s}'.format(args.desc))
    '''**************************************************************
    I. Set parameters
    '''
    config_path = args.conf
    with open(config_path) as config_buffer:
        config = json.loads(config_buffer.read())
    '''**************************************************************
    II. Prepare the data
    '''
    # 1: Parse dataset
    train_imgs, train_labels = parse_annotation(
        ann_dir=config['train']['train_annot_folder'],
        img_dir=config['train']['train_image_folder'],
        labels=config['model']['labels'])
    if os.path.exists(config['valid']['valid_annot_folder']):
        valid_imgs, valid_labels = parse_annotation(
            ann_dir=config['valid']['valid_annot_folder'],
            img_dir=config['valid']['valid_image_folder'],
            labels=config['model']['labels'])
    else:
        train_valid_split = int(0.8 * len(train_imgs))
        np.random.shuffle(train_imgs)

        valid_imgs = train_imgs[train_valid_split:]
        train_imgs = train_imgs[:train_valid_split]

    if len(config['model']['labels']) > 0:
        overlap_labels = set(config['model']['labels']).intersection(
            set(train_labels.keys()))

        print('Seen labels:\t', train_labels)
        print('Given labels:\t', config['model']['labels'])
        print('Overlap labels:\t', overlap_labels)

        if len(overlap_labels) < len(config['model']['labels']):
            print(
                'Some labels have no annotations! Please revise the list of labels in the config.json file!'
            )
            return
    else:
        print('No labels are provided. Train on all seen labels.')
        config['model']['labels'] = train_labels.keys()
    '''**************************************************************
    III. Create the model
    '''
    # 1: Build the model architecture.
    yolo = YOLO(backend=config['model']['backend'],
                input_size=config['model']['input_size'],
                labels=config['model']['labels'],
                max_box_per_image=config['model']['max_box_per_image'],
                anchors=config['model']['anchors'])

    # 2: Load some weights into the model.
    if os.path.exists(config['train']['pretrained_weights']):
        print("Loading pre-trained weights in",
              config['train']['pretrained_weights'])
        yolo.load_weights(config['train']['pretrained_weights'])
    '''**************************************************************
    IV. Kick off the training
    '''
    yolo.train(train_imgs=train_imgs,
               valid_imgs=valid_imgs,
               train_times=config['train']['train_times'],
               valid_times=config['valid']['valid_times'],
               nb_epoch=config['train']['nb_epochs'],
               learning_rate=config['train']['learning_rate'],
               batch_size=config['train']['batch_size'],
               warmup_epochs=config['train']['warmup_epochs'],
               object_scale=config['train']['object_scale'],
               no_object_scale=config['train']['no_object_scale'],
               coord_scale=config['train']['coord_scale'],
               class_scale=config['train']['class_scale'],
               saved_weights_name=config['train']['saved_weights_name'],
               debug=config['train']['debug'])
    '''**************************************************************
    V. Test & Evaluate
    '''
    # 1: Set test parameters.
    input_path = args.input
    output_path = args.output
    weights_path = args.weights

    # 2: Obtain test image paths.
    image_paths = []
    if os.path.isdir(input_path):
        for inp_file in os.listdir(input_path):
            image_paths += [input_path + inp_file]
    else:
        image_paths += [input_path]
    image_paths = [
        inp_file for inp_file in image_paths
        if (inp_file[-4:] in ['.jpg', '.png', 'jpeg'])
    ]

    # 3: Load model.
    yolo = YOLO(backend=config['model']['backend'],
                input_size=config['model']['input_size'],
                labels=config['model']['labels'],
                max_box_per_image=config['model']['max_box_per_image'],
                anchors=config['model']['anchors'])
    yolo.load_weights(weights_path)

    # 4: Make predictions.
    for image_path in image_paths:
        image = cv2.imread(image_path)
        print(image_path)

        # predict the bounding boxes
        boxes = yolo.predict(image)

        # draw bounding boxes on the image using labels
        image = draw_boxes(image, boxes, config['model']['labels'])

        # display predicted results
        cv2.imshow("A", np.uint8(image))
        cv2.waitKey(0)

        # write the image with bounding boxes to file
        cv2.imwrite(output_path + image_path.split('/')[-1], np.uint8(image))
Ejemplo n.º 23
0
def _main_(args):
    config_path = args.conf
    weights_path = args.weights

    with open(config_path) as config_buffer:
        config = json.load(config_buffer)

    ###############################
    #   Make the model
    ###############################

    yolo = YOLO(backend=config['model']['backend'],
                input_size=config['model']['input_size'],
                labels=config['model']['labels'],
                max_box_per_image=config['model']['max_box_per_image'],
                anchors=config['model']['anchors'])

    ###############################
    #   Load trained weights
    ###############################

    yolo.load_weights(weights_path)

    ###############################
    #   Parse the annotations
    ###############################
    # parse annotations of the validation set, if any, otherwise split the training set
    if os.path.exists(config['valid']['valid_annot_folder']):
        valid_imgs, valid_labels = parse_annotation(
            config['valid']['valid_annot_folder'],
            config['valid']['valid_image_folder'], config['model']['labels'])
    else:
        raise ValueError(
            'Validation folder does not exist or is not specified')

    ############################################
    # Make validation generators
    ############################################
    generator_config = {
        'IMAGE_H': yolo.input_size,
        'IMAGE_W': yolo.input_size,
        'GRID_H': yolo.grid_h,
        'GRID_W': yolo.grid_w,
        'BOX': yolo.nb_box,
        'LABELS': yolo.labels,
        'CLASS': len(yolo.labels),
        'ANCHORS': yolo.anchors,
        'BATCH_SIZE': config['train']['batch_size'],
        'TRUE_BOX_BUFFER': yolo.max_box_per_image,
    }

    generator = BatchGenerator(valid_imgs,
                               generator_config,
                               norm=yolo.feature_extractor.normalize,
                               jitter=False)

    y_true = []
    y_predicted = []
    for i in range(generator.size()):
        raw_image = generator.load_image(i)
        raw_height, raw_width, raw_channels = raw_image.shape

        # make the boxes and the labels
        pred_boxes = yolo.predict(raw_image)

        score = np.array([box.score for box in pred_boxes])
        pred_labels = np.array([box.label for box in pred_boxes])

        if len(pred_boxes) > 0:
            pred_boxes = np.array([[
                box.xmin * raw_width, box.ymin * raw_height,
                box.xmax * raw_width, box.ymax * raw_height, box.score
            ] for box in pred_boxes])
        else:
            pred_boxes = np.array([[]])

        # sort the boxes and the labels according to scores
        score_sort = np.argsort(-score)
        pred_labels = pred_labels[score_sort]
        pred_boxes = pred_boxes[score_sort]

        # store predicted label for the image i.
        # since multiple boxes may be predicted, choose the one with the highest score
        # TODO: find out why there are no predictions at all for certain images
        if pred_labels.any():
            y_predicted.append(pred_labels[0])
        else:
            y_predicted.append(4)

        # load true image annotations
        annotations = generator.load_annotation(i)

        if annotations.shape[0] > 1:
            raise ValueError('Multiple objects exist per image not supported')

        ### store the true label for the image i
        y_true.append(annotations[0, 4])

    print('Processed ' + str(len(y_true)) + 'imgaes')

    print('Confusion Matrix')
    print(confusion_matrix(y_true, y_predicted))
    print('Classification Report')

    # added NoPrediction label to number of classes as yolo model returned null prediction for some images
    target_names = config['model']['labels'] + ['NoPrediction']
    print(classification_report(y_true, y_predicted,
                                target_names=target_names))
Ejemplo n.º 24
0
def _main_(args):
    config_path = args.conf

    with open(config_path) as config_buffer:
        config = json.loads(config_buffer.read())
    pp.pprint(config)

    train_imgs, train_labels = parse_annotation(
        config['train']['train_annot_folder'],
        config['train']['train_image_folder'], config['model']['labels'])

    # parse annotations of the validation set, if any, otherwise split the training set
    if os.path.exists(str(config['valid']['valid_annot_folder'])):
        valid_imgs, valid_labels = parse_annotation(
            config['valid']['valid_annot_folder'],
            config['valid']['valid_image_folder'], config['model']['labels'])
    else:
        print("Splitting into train and validation")
        train_valid_split = int(0.99 * len(train_imgs))
        np.random.shuffle(train_imgs)

        valid_imgs = train_imgs[train_valid_split:]
        train_imgs = train_imgs[:train_valid_split]

    if len(config['model']['labels']) > 0:
        overlap_labels = set(config['model']['labels']).intersection(
            set(train_labels.keys()))

        print(('Seen labels:\t', train_labels))
        print(('Given labels:\t', config['model']['labels']))
        print(('Overlap labels:\t', overlap_labels))

        if len(overlap_labels) < len(config['model']['labels']):
            print(
                'Some labels have no annotations! Please revise the list of labels in the config.json file!'
            )
            return
    else:
        print('No labels are provided. Train on all seen labels.')
        config['model']['labels'] = list(train_labels.keys())

    yolo = YOLO(backend=config['model']['backend'],
                input_size=config['model']['input_size'],
                labels=config['model']['labels'],
                max_box_per_image=config['model']['max_box_per_image'],
                anchors=config['model']['anchors'])

    if os.path.exists(str(config['train']['pretrained_weights'])):
        print(("Loading pre-trained weights in",
               config['train']['pretrained_weights']))
        if config['model']['backend'] == "MobileNet":
            is_mobilenet = True
        else:
            print((config['model']['backend']))
            is_mobilenet = False
        yolo.load_weights(config['train']['pretrained_weights'])

    print("\nStarting training...")

    yolo.train(train_imgs=train_imgs,
               valid_imgs=valid_imgs,
               train_times=config['train']['train_times'],
               valid_times=config['valid']['valid_times'],
               nb_epochs=config['train']['nb_epochs'],
               learning_rate=config['train']['learning_rate'],
               batch_size=config['train']['batch_size'],
               warmup_epochs=config['train']['warmup_epochs'],
               object_scale=config['train']['object_scale'],
               no_object_scale=config['train']['no_object_scale'],
               coord_scale=config['train']['coord_scale'],
               class_scale=config['train']['class_scale'],
               saved_weights_name=config['train']['saved_weights_name'],
               debug=config['train']['debug'],
               full_log_dir=config['train']['full_log_dir'],
               early_stop_patience=config['train']['early_stop_patience'],
               early_stop_min_delta=config['train']['early_stop_min_delta'],
               learning_rate_decay_factor=config['train']
               ['learning_rate_decay_factor'],
               learning_rate_decay_patience=config['train']
               ['learning_rate_decay_patience'],
               learning_rate_decay_min_lr=config['train']
               ['learning_rate_decay_min_lr'])
Ejemplo n.º 25
0
def _main_(args):
    config_path = args.conf

    keras.backend.tensorflow_backend.set_session(get_session())

    with open(config_path) as config_buffer:
        config = json.loads(config_buffer.read())

    if config['backup']['create_backup']:
        config = create_backup(config)
    ###############################
    #   Parse the annotations
    ###############################

    if config['parser_annotation_type'] == 'xml':
        # parse annotations of the training set
        train_imgs, train_labels = parse_annotation(
            config['train']['train_annot_folder'],
            config['train']['train_image_folder'], config['model']['labels'])

        # parse annotations of the validation set, if any, otherwise split the training set
        if os.path.exists(config['valid']['valid_annot_folder']):
            valid_imgs, valid_labels = parse_annotation(
                config['valid']['valid_annot_folder'],
                config['valid']['valid_image_folder'],
                config['model']['labels'])
            split = False
        else:
            split = True
    elif config['parser_annotation_type'] == 'csv':
        # parse annotations of the training set
        train_imgs, train_labels = parse_annotation_csv(
            config['train']['train_csv_file'], config['model']['labels'],
            config['train']['train_csv_base_path'])

        # parse annotations of the validation set, if any, otherwise split the training set
        if os.path.exists(config['valid']['valid_csv_file']):
            valid_imgs, valid_labels = parse_annotation_csv(
                config['valid']['valid_csv_file'], config['model']['labels'],
                config['valid']['valid_csv_base_path'])
            split = False
        else:
            print("Validation file not found commensing split")
            split = True
    else:
        raise ValueError(
            "'parser_annotations_type' must be 'xml' or 'csv' not {}.".format(
                config['parser_annotations_type']))

    if split:
        train_valid_split = int(0.8 * len(train_imgs))
        np.random.shuffle(train_imgs)

        valid_imgs = train_imgs[train_valid_split:]
        train_imgs = train_imgs[:train_valid_split]

    if len(config['model']['labels']) > 0:
        overlap_labels = set(config['model']['labels']).intersection(
            set(train_labels.keys()))

        print('Seen labels:\t', train_labels)
        print('Given labels:\t', config['model']['labels'])
        print('Overlap labels:\t', overlap_labels)

        if len(overlap_labels) < len(config['model']['labels']):
            print(
                'Some labels have no annotations! Please revise the list of labels in the config.json file!'
            )
            return
    else:
        print('No labels are provided. Train on all seen labels.')
        config['model']['labels'] = train_labels.keys()
        with open("labels.json", 'w') as outfile:
            json.dump({"labels": list(train_labels.keys())}, outfile)

    ###############################
    #   Construct the model
    ###############################

    yolo = YOLO(backend=config['model']['backend'],
                input_size=(config['model']['input_size_h'],
                            config['model']['input_size_w']),
                labels=config['model']['labels'],
                max_box_per_image=config['model']['max_box_per_image'],
                anchors=config['model']['anchors'],
                gray_mode=config['model']['gray_mode'])

    ###############################
    #   Load the pretrained weights (if any)
    ###############################

    if os.path.exists(config['train']['pretrained_weights']):
        print("Loading pre-trained weights in",
              config['train']['pretrained_weights'])
        yolo.load_weights(config['train']['pretrained_weights'])

    ###############################
    #   Start the training process
    ###############################

    yolo.train(train_imgs=train_imgs,
               valid_imgs=valid_imgs,
               train_times=config['train']['train_times'],
               valid_times=config['valid']['valid_times'],
               nb_epochs=config['train']['nb_epochs'],
               learning_rate=config['train']['learning_rate'],
               batch_size=config['train']['batch_size'],
               warmup_epochs=config['train']['warmup_epochs'],
               object_scale=config['train']['object_scale'],
               no_object_scale=config['train']['no_object_scale'],
               coord_scale=config['train']['coord_scale'],
               class_scale=config['train']['class_scale'],
               saved_weights_name=config['train']['saved_weights_name'],
               debug=config['train']['debug'],
               early_stop=config['train']['early_stop'],
               workers=config['train']['workers'],
               max_queue_size=config['train']['max_queue_size'],
               tb_logdir=config['train']['tensorboard_log_dir'])
Ejemplo n.º 26
0
        'GRID_H': GRID_H,
        'GRID_W': GRID_W,
        'BOX': N_BOX,
        'LABELS': LABELS,
        'CLASS': len(LABELS),
        'ANCHORS': ANCHORS,
        'BATCH_SIZE': BATCH_SIZE,
        'TRUE_BOX_BUFFER': TRUE_BOX_BUFFER,
    }

    all_imgs = []
    for i in range(0, len(LABELS)):
        image_path = '/Volumes/JS/UECFOOD100_JS/' + str(i+1) + '/'
        annot_path = '/Volumes/JS/UECFOOD100_JS/' + str(i+1) + '/' + '/annotations_new/'

        folder_imgs, seen_labels = parse_annotation(annot_path, image_path)
        all_imgs.extend(folder_imgs)
    print(np.array(all_imgs).shape)

    # add extensions to image name
    for img in all_imgs:
        img['filename'] = img['filename']

    print('=> Generate BatchGenerator.')
    batches = BatchGenerator(all_imgs, generator_config)

    img = batches[0][0][0][5]
    plt.imshow(img.astype('uint8'))
    # plt_example_batch(batches, BATCH_SIZE)

    ''' Start training '''
Ejemplo n.º 27
0
def main(argstate):
    ###############################
    #   Parse the annotations
    ###############################

    # parse annotations of the training set
    train_imgs, train_labels = parse_annotation(argstate.train.annot_folder,
                                                argstate.train.image_folder,
                                                argstate.labels)

    # parse annotations of the validation set, if any, otherwise split the training set
    if os.path.exists(argstate.valid.annot_folder):
        valid_imgs, valid_labels = parse_annotation(
            argstate.valid.annot_folder, argstate.valid.image_folder,
            argstate.labels)
    else:
        train_valid_split = int(0.8 * len(train_imgs))
        np.random.seed(42)
        np.random.shuffle(train_imgs)

        valid_imgs = train_imgs[train_valid_split:]
        train_imgs = train_imgs[:train_valid_split]

    print(train_labels)

    if len(set(argstate.labels).intersection(set(train_labels.keys()))) == 0:
        print(
            "Labels to be detected are not present in the dataset! Please revise the list of labels in the config.json file!"
        )
        return

    ###############################
    #   Construct the model
    ###############################
    global yolo
    yolo = YOLO(architecture=argstate.architecture,
                input_size=argstate.input_size,
                labels=argstate.labels,
                max_box_per_image=argstate.mbpi,
                anchors=argstate.anchors)

    ###############################
    #   Load the pretrained weights (if any)
    ###############################

    if os.path.exists(argstate.pretrained_weights):
        print("Loading pre-trained weights in", argstate.pretrained_weights)
        yolo.load_weights(argstate.pretrained_weights)

    # Load pretrained weights into feature extractor and then freeze them
    yolo.feature_extractor.feature_extractor.load_weights('yolov2_weights.h5')
    yolo.feature_extractor.feature_extractor.trainable = True

    # Spawn train pauser
    pause_thread = Thread(target=spawn_pause_ui)
    pause_thread.isDaemon = True
    pause_thread.start()

    ###############################
    #   Start the training process
    ###############################

    yolo.train(train_imgs=train_imgs,
               valid_imgs=valid_imgs,
               train_times=argstate.train.times,
               valid_times=argstate.valid.times,
               nb_epoch=argstate.nb_epoch,
               learning_rate=argstate.learning_rate,
               batch_size=argstate.batch_size,
               warmup_bs=argstate.warmup_bs,
               object_scale=argstate.object_scale,
               no_object_scale=argstate.no_object_scale,
               coord_scale=argstate.coord_scale,
               class_scale=argstate.class_scale,
               saved_weights_name=argstate.saved_weights_name,
               debug=argstate.debug)
Ejemplo n.º 28
0
def _main_(args):

    config_path = args.conf

    with open(config_path) as config_buffer:    
        config = json.loads(config_buffer.read())

    ###############################
    #   Parse the annotations 
    ###############################

    # parse annotations of the training set
    train_imgs, train_labels = parse_annotation(config['train']['train_annot_folder'], 
                                                config['train']['train_image_folder'], 
                                                config['model']['labels'])

    # parse annotations of the validation set, if any, otherwise split the training set
    if os.path.exists(config['valid']['valid_annot_folder']):
        valid_imgs, valid_labels = parse_annotation(config['valid']['valid_annot_folder'], 
                                                    config['valid']['valid_image_folder'], 
                                                    config['model']['labels'])
    else:
        train_valid_split = int(0.8*len(train_imgs))
        np.random.shuffle(train_imgs)

        valid_imgs = train_imgs[train_valid_split:]
        train_imgs = train_imgs[:train_valid_split]

    if len(config['model']['labels']) > 0:
        overlap_labels = set(config['model']['labels']).intersection(set(train_labels.keys()))

        print 'Seen labels:\t', train_labels
        print 'Given labels:\t', config['model']['labels']
        print 'Overlap labels:\t', overlap_labels           

        if len(overlap_labels) < len(config['model']['labels']):
            print 'Some labels have no annotations! Please revise the list of labels in the config.json file!'
            return
    else:
        print 'No labels are provided. Train on all seen labels.'
        config['model']['labels'] = train_labels.keys()
        
    ###############################
    #   Construct the model 
    ###############################

    yolo = YOLO(architecture        = config['model']['architecture'],
                input_size          = config['model']['input_size'], 
                labels              = config['model']['labels'], 
                max_box_per_image   = config['model']['max_box_per_image'],
                anchors             = config['model']['anchors'])

    ###############################
    #   Load the pretrained weights (if any) 
    ###############################    

    if os.path.exists(config['train']['pretrained_weights']):
        print "Loading pre-trained weights in", config['train']['pretrained_weights']
        yolo.load_weights(config['train']['pretrained_weights'])

    ###############################
    #   Start the training process 
    ###############################

    yolo.train(train_imgs         = train_imgs,
               valid_imgs         = valid_imgs,
               train_times        = config['train']['train_times'],
               valid_times        = config['valid']['valid_times'],
               nb_epoch           = config['train']['nb_epoch'], 
               learning_rate      = config['train']['learning_rate'], 
               batch_size         = config['train']['batch_size'],
               warmup_epochs      = config['train']['warmup_epochs'],
               object_scale       = config['train']['object_scale'],
               no_object_scale    = config['train']['no_object_scale'],
               coord_scale        = config['train']['coord_scale'],
               class_scale        = config['train']['class_scale'],
               saved_weights_name = config['train']['saved_weights_name'],
               debug              = config['train']['debug'])
Ejemplo n.º 29
0
    'GRID_W': GRID_W,
    'BOX': BOX,
    'LABELS': LABELS,
    'CLASS': len(LABELS),
    'ANCHORS': ANCHORS,
    'BATCH_SIZE': BATCH_SIZE,
    'TRUE_BOX_BUFFER': 50,
}


def normalize(image):
    return image / 255.


train_imgs, seen_train_labels = parse_annotation(train_annot_folder,
                                                 train_image_folder,
                                                 labels=LABELS)
### write parsed annotations to pickle for fast retrieval next time
with open('train_imgs', 'wb') as fp:
    pickle.dump(train_imgs, fp)

### read saved pickle of parsed annotations
with open('train_imgs', 'rb') as fp:
    train_imgs = pickle.load(fp)
train_batch = BatchGenerator(train_imgs, generator_config, norm=normalize)

valid_imgs, seen_valid_labels = parse_annotation(valid_annot_folder,
                                                 valid_image_folder,
                                                 labels=LABELS)
### write parsed annotations to pickle for fast retrieval next time
with open('valid_imgs', 'wb') as fp:
Ejemplo n.º 30
0
def _main_(args):

    config_path = args.conf

    with open(config_path) as config_buffer:    
        config = json.loads(config_buffer.read())

    os.environ["CUDA_VISIBLE_DEVICES"]=config['env']['gpu']    
    gpus = max(1, len(config['env']['gpu'].split(",")))

    #print("{}").format(config)
    ###############################
    #   Parse the annotations 
    ###############################

    # parse annotations of the training set
    train_imgs, train_labels = parse_annotation(config['train']['train_annot_folder'], 
                                                config['train']['train_image_folder'], 
                                                config['model']['labels'], config['train']['image_ext_name'], config['train']['image_prefix'])
   
    # parse annotations of the validation set, if any, otherwise split the training set
    if os.path.exists(config['valid']['valid_annot_folder']):
        valid_imgs, valid_labels = parse_annotation(config['valid']['valid_annot_folder'], 
                                                    config['valid']['valid_image_folder'], 
                                                    config['model']['labels'], config['train']['image_ext_name'], config['train']['image_prefix'])
    else:
        train_valid_split = int(0.8*len(train_imgs))
        np.random.shuffle(train_imgs)

        valid_imgs = train_imgs[train_valid_split:]
        train_imgs = train_imgs[:train_valid_split]

    
    overlap_labels = set(config['model']['labels']).intersection(set(train_labels.keys()))

    print ('Seen labels:{}'.format(train_labels))
    print ('Given labels:{}'.format(config['model']['labels']))
    print ('Overlap labels:{}'.format(overlap_labels))    

    if len(overlap_labels) < len(config['model']['labels']):
        print ('Some labels have no images! Please revise the list of labels in the config.json file!')
        return
        
    ###############################
    #   Construct the model 
    ###############################

    yolo = YOLO(architecture        = config['model']['architecture'],
                input_size          = config['model']['input_size'], 
                labels              = config['model']['labels'], 
                max_box_per_image   = config['model']['max_box_per_image'],
                anchors             = config['model']['anchors']
                gpus                = gpus)

    ###############################
    #   Load the pretrained weights (if any) 
    ###############################    

    if os.path.exists(config['train']['pretrained_weights']):
        print ("Loading pre-trained weights in {}".format(config['train']['pretrained_weights']))
        yolo.load_weights(config['train']['pretrained_weights'])

    ###############################
    #   Start the training process 
    ###############################

    yolo.train(train_imgs         = train_imgs,
               valid_imgs         = valid_imgs,
               train_times        = config['train']['train_times'],
               valid_times        = config['valid']['valid_times'],
               nb_epoch           = config['train']['nb_epoch'], 
               learning_rate      = config['train']['learning_rate'], 
               batch_size         = config['train']['batch_size'],
               warmup_bs          = config['train']['warmup_batches'],
               object_scale       = config['train']['object_scale'],
               no_object_scale    = config['train']['no_object_scale'],
               coord_scale        = config['train']['coord_scale'],
               class_scale        = config['train']['class_scale'],
               saved_weights_name = config['train']['saved_weights_name'],
               debug              = config['train']['debug'])
Ejemplo n.º 31
0
def _main_(args):
    config_path = args.conf

    with open(config_path) as config_buffer:
        config = json.loads(config_buffer.read())

    ###############################
    #   Parse the annotations
    ###############################

    # parse annotations of the training set
    if 'csv' in config['train']['train_annot_folder']:
        train_imgs, train_labels = parse_csv_annotations(
            config['train']['train_annot_folder'],
            config['train']['train_image_folder'], config['model']['labels'])
    else:
        train_imgs, train_labels = parse_annotation(
            config['train']['train_annot_folder'],
            config['train']['train_image_folder'], config['model']['labels'])

    # parse annotations of the validation set, if any, otherwise split the training set
    if os.path.exists(config['valid']['valid_annot_folder']):
        if 'csv' in config['valid']['valid_annot_folder']:
            valid_imgs, valid_labels = parse_csv_annotations(
                config['valid']['valid_annot_folder'],
                config['valid']['valid_image_folder'],
                config['model']['labels'])
        else:
            valid_imgs, valid_labels = parse_annotation(
                config['valid']['valid_annot_folder'],
                config['valid']['valid_image_folder'],
                config['model']['labels'])
    else:
        train_valid_split = int(0.8 * len(train_imgs))
        np.random.shuffle(train_imgs)

        valid_imgs = train_imgs[train_valid_split:]
        train_imgs = train_imgs[:train_valid_split]

    if len(config['model']['labels']) > 0:
        overlap_labels = set(config['model']['labels']).intersection(
            set(train_labels.keys()))

        print('Seen labels:\t', train_labels)
        print('Given labels:\t', config['model']['labels'])
        print('Overlap labels:\t', overlap_labels)

        if len(overlap_labels) < len(config['model']['labels']):
            print(
                'Some labels have no annotations! Please revise the list of labels in the config.json file!'
            )
            return
    else:
        print('No labels are provided. Train on all seen labels.')
        config['model']['labels'] = train_labels.keys()

    ###############################
    #   Construct the model
    ###############################

    yolo = YOLO(backend=config['model']['backend'],
                input_size=config['model']['input_size'],
                labels=config['model']['labels'],
                max_box_per_image=config['model']['max_box_per_image'],
                anchors=config['model']['anchors'])

    ###############################
    #   Load the pretrained weights (if any)
    ###############################

    if os.path.exists(config['train']['pretrained_weights']):
        print("Loading pre-trained weights in",
              config['train']['pretrained_weights'])
        yolo.load_weights(config['train']['pretrained_weights'])

    ###############################
    #   Start the training process
    ###############################

    yolo.train(train_imgs=train_imgs,
               valid_imgs=valid_imgs,
               train_times=config['train']['train_times'],
               valid_times=config['valid']['valid_times'],
               nb_epochs=config['train']['nb_epochs'],
               learning_rate=config['train']['learning_rate'],
               batch_size=config['train']['batch_size'],
               warmup_epochs=config['train']['warmup_epochs'],
               object_scale=config['train']['object_scale'],
               no_object_scale=config['train']['no_object_scale'],
               coord_scale=config['train']['coord_scale'],
               class_scale=config['train']['class_scale'],
               saved_weights_name=config['train']['saved_weights_name'],
               debug=config['train']['debug'])
Ejemplo n.º 32
0
    'BATCH_SIZE': BATCH_SIZE,
    'TRUE_BOX_BUFFER': TRUE_BOX_BUFFER,
}
# define paths for training
valid_image_folder = 'data/Validation_Images/'
valid_annot_folder = 'data/Validation_Annotations/'


# define normalize for images
def normalize(image):
    return image / 255.


# define the image and label datasets for training
valid_imgs, valid_labels = parse_annotation(valid_annot_folder,
                                            valid_image_folder,
                                            labels=LABELS)
valid_generator = BatchGenerator(valid_imgs,
                                 generator_config,
                                 norm=normalize,
                                 jitter=False)

# load new model from training
model = load_model('new_model_6.h5',
                   custom_objects={
                       'tf': tf,
                       'custom_loss': custom_loss
                   })

average_precisions = evaluate(model, valid_generator)