Exemplo n.º 1
0
def create_model(input_shape, anchors, num_classes, load_pretrained=True, freeze_body=2,
            weights_path='model_data/yolo_weights.h5'):
    '''create the training model'''
    K.clear_session() # get a new session
    image_input = Input(shape=(None, None, 3))
    h, w = input_shape
    num_anchors = len(anchors)

    y_true = [Input(shape=(h//{0:32, 1:16, 2:8}[l], w//{0:32, 1:16, 2:8}[l], \
        num_anchors//3, num_classes+5)) for l in range(3)]

    model_body = yolo_body(image_input, num_anchors//3, num_classes)
    print('Create YOLOv3 model with {} anchors and {} classes.'.format(num_anchors, num_classes))

    if load_pretrained:
        model_body.load_weights(weights_path, by_name=True, skip_mismatch=True)
        print('Load weights {}.'.format(weights_path))
        if freeze_body in [1, 2]:
            # Freeze darknet53 body or freeze all but 3 output layers.
            num = (185, len(model_body.layers)-3)[freeze_body-1]
            for i in range(num): model_body.layers[i].trainable = False
            print('Freeze the first {} layers of total {} layers.'.format(num, len(model_body.layers)))
 
    for y in range(-3, 0):
        model_body.layers[y].name = "conv2d_output_" + str(h//{-3:32, -2:16, -1:8}[y])

    model_loss = Lambda(yolo_loss, output_shape=(1,), name='yolo_loss',
        arguments={'anchors': anchors, 'num_classes': num_classes, 'ignore_thresh': 0.5})(
        [*model_body.output, *y_true])
    model = Model([model_body.input, *y_true], model_loss)


    return model
Exemplo n.º 2
0
    def generate(self):
        model_path = os.path.expanduser(self.model_path)
        assert model_path.endswith(
            '.h5'), 'Keras model or weights must be a .h5 file.'

        # Load model, or construct model and load weights.
        num_anchors = len(self.anchors)
        num_classes = len(self.class_names)
        is_tiny_version = num_anchors == 6  # default setting
        try:
            self.yolo_model = load_model(model_path, compile=False)
        except:
            self.yolo_model = tiny_yolo_body(Input(shape=(None,None,3)), num_anchors//2, num_classes) \
                if is_tiny_version else yolo_body(Input(shape=(None,None,3)), num_anchors//3, num_classes)
            self.yolo_model.load_weights(
                self.model_path)  # make sure model, anchors and classes match
        else:
            assert self.yolo_model.layers[-1].output_shape[-1] == \
                num_anchors/len(self.yolo_model.output) * (num_classes + 5), \
                'Mismatch between model and given anchor and class sizes'

        #self.yolo_model = reduce_keras_model(self.yolo_model)

        print('{} model, anchors, and classes loaded.'.format(model_path))

        # Generate colors for drawing bounding boxes.
        hsv_tuples = [(x / len(self.class_names), 1., 1.)
                      for x in range(len(self.class_names))]
        self.colors = list(map(lambda x: colorsys.hsv_to_rgb(*x), hsv_tuples))
        self.colors = list(
            map(lambda x: (int(x[0] * 255), int(x[1] * 255), int(x[2] * 255)),
                self.colors))
        np.random.seed(10101)  # Fixed seed for consistent colors across runs.
        np.random.shuffle(
            self.colors)  # Shuffle colors to decorrelate adjacent classes.
        np.random.seed(None)  # Reset seed to default.

        # Generate output tensor targets for filtered bounding boxes.
        self.input_image_shape = K.placeholder(shape=(2, ))
        if self.gpu_num >= 2:
            self.yolo_model = multi_gpu_model(self.yolo_model,
                                              gpus=self.gpu_num)
        boxes, scores, classes = yolo_eval(self.yolo_model.output,
                                           self.anchors,
                                           len(self.class_names),
                                           self.input_image_shape,
                                           score_threshold=self.score,
                                           iou_threshold=self.iou,
                                           yolo_one=0)
        return boxes, scores, classes
Exemplo n.º 3
0
def _main():
    #weights_path = 'model_data/trained_weights_final_mobilenet.h5'
    weights_path = 'model_data/small_mobilenets2_trained_weights_final.h5'
    #weights_path = 'logs/squeezenet_000/squeezenet_trained_weights_final.h5'

    train_path = '2007_train.txt'
    val_path = '2007_val.txt'
    test_path = '2007_test.txt'
    #log_dir = 'logs/logits_only_000/'
    classes_path = 'class/voc_classes.txt'
    anchors_path = 'anchors/yolo_anchors.txt'

    class_names = get_classes(classes_path)
    anchors = get_anchors(anchors_path)

    num_classes = len(class_names)
    num_anchors = len(anchors)  #9

    shape_size = 416
    input_shape = (shape_size, shape_size)  # multiple of 32, hw

    num_layers = num_anchors // 3  #9//3

    #with open(train_path) as f:
    #    train_lines = f.readlines()
    #train_lines = train_lines[:200]

    #with open(val_path) as f:
    #    val_lines = f.readlines()
    #val_lines = val_lines[:150]

    with open(test_path) as f:
        test_lines = f.readlines()
    #test_lines = test_lines[:2]

    #num_train = int(len(train_lines))
    #num_val = int(len(val_lines))
    num_test = int(len(test_lines))

    #declare model

    image_input = Input(shape=(shape_size, shape_size, 3))

    try:
        eval_model = load_model(model_path, compile=False)
    except:
        eval_model = yolo_body(image_input, num_anchors // 3,
                               num_classes)  #9//3
        eval_model.load_weights(weights_path)

    yolo_out = []
    fmap = shape_size // 32
    mapsize = [1, 2, 4]

    if num_layers == 3:
        ly_out = [-3, -2, -1]
    elif num_layers == 2:
        ly_out = [-2, -1]
    else:
        ly_out = [-1]

    # return the constructed network architecture
    # class+5
    for ly in range(num_layers):
        yolo_layer = Reshape(
            (fmap * mapsize[ly], fmap * mapsize[ly], 3,
             (num_classes + 5)))(eval_model.layers[ly_out[ly]].output)

        yolo_out.append(yolo_layer)

    eval_model = Model(inputs=eval_model.input, outputs=yolo_out)
    eval_model._make_predict_function()

    batch_size = 1

    all_detections = [[] for i in range(num_classes)]
    all_annotations = [[] for i in range(num_classes)]

    count_detections = [[0 for i in range(num_classes)]
                        for i in range(num_layers)]
    total_object = 0

    datagen = data_generator_wrapper_eval(test_lines, batch_size, input_shape,
                                          anchors, num_classes, eval_model)

    print("{} test data".format(num_test))
    for n in tqdm(range(num_test)):  #num_test
        img, flogits, mlogits = next(datagen)

        for l in range(num_layers):
            #print( "layer" + str(l) )
            arrp = flogits[l]
            box = np.where(arrp[..., 4] > 0)
            box = np.transpose(box)

            for i in range(len(box)):
                #print("obj" + str(i) )
                #print( tuple(box[i]) )
                #detection_label =  np.argmax( flogits[l][tuple(box[i])][5:])
                annotation_label = np.argmax(flogits[l][tuple(box[i])][5:])

                #print( "{} ({}) {} == ({}) {} ".format(l, detection_label, class_names[  detection_label ] ,annotation_label, class_names[  annotation_label ] ) )

                all_detections[annotation_label].append(mlogits[l][tuple(
                    box[i])])
                all_annotations[annotation_label].append(flogits[l][tuple(
                    box[i])])

                count_detections[l][annotation_label] += 1
                total_object += 1

    print(len(all_detections))
    print(len(all_annotations))
    print(count_detections)
    print(total_object)

    conf_thres = 0.5
    iou_thres = 0.45

    average_precisions = {}

    for label in tqdm(range(num_classes)):

        false_positives = np.zeros((0, ))
        true_positives = np.zeros((0, ))
        scores = np.zeros((0, ))

        num_detect = len(all_detections[label])
        for det in range(num_detect):

            detect_box = all_detections[label][det][..., 0:4]
            detect_conf = all_detections[label][det][..., 4]
            detect_label = np.argmax(all_detections[label][det][..., 5:])

            annot_box = all_annotations[label][det][..., 0:4]
            annot_conf = all_annotations[label][det][..., 4]
            detect_label = np.argmax(all_detections[label][det][..., 5:])

            iou = numpy_box_iou(detect_box, annot_box)

            scores = np.append(scores, detect_conf)

            if (iou > iou_thres and detect_conf > conf_thres
                    and (label == detect_label)):
                #print( best_iou[tuple(box[i])] )
                #print("pos")
                false_positives = np.append(false_positives, 0)
                true_positives = np.append(true_positives, 1)
            else:
                #print("neg")
                false_positives = np.append(false_positives, 1)
                true_positives = np.append(true_positives, 0)

        indices = np.argsort(-scores)
        false_positives = false_positives[indices]
        true_positives = true_positives[indices]
        #print(true_positives)

        false_positives = np.cumsum(false_positives)
        true_positives = np.cumsum(true_positives)
        #print(true_positives)

        recall = true_positives / num_detect
        #print( recall )
        precision = true_positives / np.maximum(
            true_positives + false_positives,
            np.finfo(np.float64).eps)
        #print( precision )

        average_precision = compute_ap(recall, precision)
        average_precisions[label] = average_precision

    print("loaded weights {}".format(weights_path))

    #print(average_precisions)

    for label, average_precision in average_precisions.items():
        print(class_names[label] + ': {:.4f}'.format(average_precision))
    print('mAP: {:.4f}'.format(
        sum(average_precisions.values()) / len(average_precisions)))
Exemplo n.º 4
0
from keras.models import Model
from keras.layers import Input
from utils.setup_tool import get_classes, get_anchors
from model.small_mobilenets2 import yolo_body
#from model.mobilenetv2 import yolo_body

import os
os.environ["CUDA_VISIBLE_DEVICES"] = "0"

#model_path = 'model_data/small_mobilenet_trained_weights_final.h5'
model_path = 'model_data/small_mobilenets2_trained_weights_final.h5'
classes_path = 'class/voc_classes.txt'
anchors_path = 'anchors/yolo_anchors.txt'

class_names = get_classes(classes_path)
anchors = get_anchors(anchors_path)

num_classes = len(class_names)
num_anchors = len(anchors)

yolo_model = yolo_body(Input(shape=(416, 416, 3)), num_anchors // 3,
                       num_classes)
yolo_model.load_weights(model_path)

yolo_model.save('model_data/small_mobilenets2_trained_model.h5')
Exemplo n.º 5
0
#log_dir = 'logs/logits_only_000/'
#classes_path = 'class/voc_classes.txt'
#anchors_path = 'anchors/tiny_yolo_anchors.txt'
classes_path = 'class/elderly_classes.txt'
anchors_path = 'anchors/elderly_anchors.txt'
class_names = get_classes(classes_path)
num_classes = len(class_names)
anchors = get_anchors(anchors_path)

#input_shape = (416,416) # multiple of 32, hw

#ima#ge_input = Input(shape=(416,416, 3))
image_input = Input(shape=(None, None, 3))
#h, w = input_shape
num_anchors = len(anchors)

model = yolo_body(image_input, 3, num_classes)
model.load_weights(model_path)
model.summary()
print(len(model.layers))

model_reduced = reduce_keras_model(model)
#model_reduced.save('model_data/416bnfuse_small_mobilenets2_trained_model.h5')
#model_reduced.save('model_data/416bnfuse_tiny_yolo.h5')
#model_reduced.save('model_data/bnfuse_med_tiny_yolo.h5')
#model_reduced.save('model_data/bnfuse_a2_ds_small_mobilenets2_trained_model.h5')
model_reduced.save(
    'model_data/bnfuse_eld_small_mobilenets2_trained_weights_final.h5')
model_reduced.summary()
print(len(model_reduced.layers))
Exemplo n.º 6
0
def create_model(input_shape,
                 anchors,
                 num_classes,
                 load_pretrained=True,
                 freeze_body=2,
                 weights_path='model_data/yolo_weights.h5',
                 teacher_weights_path="model_data/trained_weights_final.h5"):
    '''create the training model'''
    K.clear_session()  # get a new session
    image_input = Input(shape=(None, None, 3))
    h, w = input_shape
    num_anchors = len(anchors)

    y_true = [Input(shape=(h//{0:32, 1:16, 2:8}[l], w//{0:32, 1:16, 2:8}[l], \
        num_anchors//3, num_classes+5)) for l in range(3)]

    teacher = teacher_body(image_input, num_anchors // 3, num_classes)
    teacher.load_weights(teacher_weights_path)

    #teacher_output = []
    #for ty in range(-3, 0):#13,26,52
    #    out_ty = Reshape( teacher.layers[ty].output_shape[:-1] + ( 3, num_classes+5 )  )(teacher.layers[ty].output)

    #yolo13 = Reshape( ( teacher.layers[-3].output_shape[:-1] + ( 3, num_classes+5 ) ) )(teacher.layers[-3].output)
    #yolo26 = Reshape( ( teacher.layers[-3].output_shape[:-1] + ( 3, num_classes+5 ) ) )(teacher.layers[-2].output)
    #yolo52 = Reshape( ( teacher.layers[-3].output_shape[:-1] + (3, num_classes+5 ) ) )(teacher.layers[-1].output)

    #teacher = Model( inputs= teacher.input , outputs= teacher_output )
    #print("teacher before freeze" , len(teacher.trainable_weights) )
    for i in range(len(teacher.layers)):
        teacher.layers[i].trainable = False
    #print("teacher after freeze" , len(teacher.trainable_weights) )

    student = yolo_body(image_input, num_anchors // 3, num_classes)
    print('Create YOLOv3 model with {} anchors and {} classes.'.format(
        num_anchors, num_classes))

    #print("student before freeze" ,  len(student.trainable_weights) )

    if load_pretrained:
        student.load_weights(weights_path, by_name=True, skip_mismatch=True)
        print('Load weights {}.'.format(weights_path))
        if freeze_body in [1, 2]:
            # Freeze darknet53 body or freeze all but 3 output layers.
            num = (185, len(student.layers) - 3)[freeze_body - 1]
            for i in range(num):
                student.layers[i].trainable = False
            print('Freeze the first {} layers of total {} layers.'.format(
                num, len(student.layers)))

    for y in range(-3, 0):
        student.layers[y].name = "conv2d_output_" + str(h // {
            -3: 32,
            -2: 16,
            -1: 8
        }[y])

    model_loss = Lambda(yolo_custom_loss,
                        output_shape=(1, ),
                        name='yolo_custom_loss',
                        arguments={
                            'anchors': anchors,
                            'num_classes': num_classes,
                            'ignore_thresh': 0.5
                        })([*student.output, *y_true, *teacher.output])
    model = Model([image_input, *y_true], model_loss)

    #print("student after freeze" ,  len(student.trainable_weights) )
    #print("model" , len( model.trainable_weights) )

    #for i in range(len( student.layers ) ): student.layers[i].trainable = True

    #print("student after unfreeze" ,  len(student.trainable_weights) )

    #print("model after" ,  len(model.trainable_weights) )

    #from keras.utils.vis_utils import plot_model as plot
    #plot(model, to_file='{}.png'.format("train_together"), show_shapes=True)
    #print("stop")

    return model, student, teacher