allanzelener / YAD2K

YAD2K: Yet Another Darknet 2 Keras
Other
2.71k stars 877 forks source link

YOLO implementation giving wrong predictions #126

Open Flock1 opened 6 years ago

Flock1 commented 6 years ago

Hi,

I am trying to implement this YOLO implementation for a purpose and it's giving wrong predictions. I am loading the same model as proposed by the author and following almost the same steps (as shown):

def _main(args):
    yolo_model1 = load_model(model1)  ###########LOADS YOLO
    sess = K.get_session()

    '''################ FUNCTION TO LOAD MY MODEL ############################'''
    def get_model():
        # load json and create model
        json_file = open('/home/sarvagya/Desktop/RBC/Python/time-to-collision/trucks/model.json', 'r')
        loaded_model_json = json_file.read()
        json_file.close()

        loaded_model = model_from_json(loaded_model_json)
        # load weights into new model
        config = tf.ConfigProto()
        config.gpu_options.per_process_gpu_memory_fraction = 0.5
        config.gpu_options.visible_device_list = "0"
        tf.Session(config=config)
        loaded_model.load_weights("/home/sarvagya/Desktop/RBC/Python/time-to-collision/trucks/model.h5")

        loaded_model.compile(loss='binary_crossentropy',
                             optimizer='rmsprop',
                             metrics=['accuracy'])
        return loaded_model

    loaded_model = get_model() ############LOADS MY MODEL
    anchors_path = os.path.expanduser(args.anchors_path)
    classes_path = os.path.expanduser(args.classes_path)
    print(classes_path)

    with open(classes_path) as f:
        class_names = f.readlines()
    class_names = [c.strip() for c in class_names]
    # print(class_names)

    with open(anchors_path) as f:
        anchors = f.readline()
        anchors = [float(x) for x in anchors.split(',')]
        anchors = np.array(anchors).reshape(-1, 2)

    num_classes = len(class_names)
    num_anchors = len(anchors)
    yolo_outputs = yolo_head(yolo_model1.output, anchors, len(class_names))
    # print("class names: ")
    # print(yolo_outputs)
    input_image_shape = K.placeholder(shape=(2, ))
    boxes, scores, classes = yolo_eval(
        yolo_outputs,
        input_image_shape,
        score_threshold=args.score_threshold,
        iou_threshold=args.iou_threshold)

    img = cv2.imread(input_img)
    x1 = 416/img.shape[1]
    y1 = 416/img.shape[0]
    height, width = img.shape[0],img.shape[1]
    new_image_size = (width - (width % 32),
                      height - (height % 32))
    resized_image = cv2.resize(img, None, fx = x1, fy = y1, interpolation = cv2.INTER_AREA)
    # print(type(resized_image))
    resized_image = np.expand_dims(resized_image, 0)
    out_boxes, out_scores, out_classes = sess.run(
        [boxes, scores, classes],
        feed_dict={
            yolo_model1.input: resized_image,
            input_image_shape: [width, height],
            K.learning_phase(): 0
        })

    print('Found {} boxes for {}'.format(len(out_boxes), input_img))
    print(out_classes)
    for i, c in reversed(list(enumerate(out_classes))):

        predicted_class = class_names[c]
        box = out_boxes[i]
        score = out_scores[i]

        label = '{} {:.2f}'.format(predicted_class, score)

        top, left, bottom, right = box
        top = max(0, np.floor(top + 0.5).astype('int32'))
        left = max(0, np.floor(left + 0.5).astype('int32'))
        bottom = min(resized_image.shape[1], np.floor(bottom + 0.5).astype('int32'))
        right = min(resized_image.shape[0], np.floor(right + 0.5).astype('int32'))
        print(label, (left, top), (right, bottom))

Yet, I am getting wrong predictions. Can someone help me?