示例#1
0
def calculate_test1_accuracy():
    # testing on the labelled Test1 video
    # vgg_model = final_model.Model().model
    vgg_model = model77().model
    # vgg_model.load_weights('weights-improvement-16-0.87.hdf5')
    # vgg_model.load_weights('weights-improvement-555-0.87.hdf5')
    vgg_model.load_weights(
        r'C:\Users\Rani\Desktop\AI Pycharm Project\vgg_features_model\backup\smaller 77\\'
        + 'weights-improvement-607-0.88.hdf5')

    adam = Adam(lr=0.00001,
                beta_1=0.9,
                beta_2=0.999,
                epsilon=None,
                decay=0.0,
                amsgrad=False)
    vgg_model.compile(loss='categorical_crossentropy',
                      optimizer=adam,
                      metrics=['accuracy'])

    # test_folder = 'C:\\Users\\Rani\\Desktop\\AI Pycharm Project\\labelled data\\test\\'
    test_folder = 'C:\\Users\\Rani\\Desktop\\AI Pycharm Project\\labelled data\\test2\\'
    X_test1 = np.load(test_folder + 'image_array.npy')
    y_test1 = np.load(test_folder + 'labels_array.npy')

    y_test1[y_test1 == 'Real'] = 0
    y_test1[y_test1 == 'Barca'] = 1
    y_test1[y_test1 == 'Other'] = 2
    y_test1 = to_categorical(y_test1, 3)

    # #####################
    # # for layer in vgg_model.layers[:-3]:
    # #     print(layer)
    # #     layer.trainable = False
    #
    # X_train = X_test1[:200]
    # y_train = y_test1[:200]
    # X_val = X_test1[200:250]
    # y_val = y_test1[200:250]
    # vgg = VGG16(include_top=False, input_shape=(288, 512, 3))
    # vgg_model.fit(get_features(X_train, vgg, flip=True), y_train, epochs=200, validation_data=(get_features(X_val, vgg, flip=True), y_val), verbose=2)
    #
    # X_test1 = X_test1[250:]
    # y_test1 = y_test1[250:]
    # #####################

    X_val = X_test1
    y_val = y_test1

    vgg = VGG16(include_top=False, input_shape=(288, 512, 3))
    predictions = get_actual_labels(predict_aux(X_val, vgg_model, vgg))

    # print('normal test accuracy=', vgg_model.evaluate(get_image_features(X_val, vgg), y_val, batch_size=4))
    print('normal test accuracy=',
          vgg_model.evaluate(get_features(X_val, vgg), y_val, batch_size=4))
    print('non_neutral test accuracy=',
          non_neutral_accuracy(get_actual_labels(y_val), predictions))
示例#2
0
def predict_labels():
    vgg = VGG16(include_top=False, input_shape=(288, 512, 3))
    # testing on the labelled Test1 video
    # vgg_model = final_model.Model().model
    vgg_model = model77().model
    # vgg_model.load_weights('weights-improvement-28-0.88.hdf5')
    # vgg_model.load_weights('weights-improvement-555-0.87.hdf5')
    vgg_model.load_weights(
        r'C:\Users\Rani\Desktop\AI Pycharm Project\vgg_features_model\backup\smaller 77\\'
        + 'weights-improvement-607-0.88.hdf5')

    cap = cv2.VideoCapture(
        "C:\\Users\\Rani\\Desktop\\AI project\\data (video)\\Test2.mp4")
    totalFrames = cap.get(cv2.CAP_PROP_FRAME_COUNT)
    i = 0
    labels = []
    while i < totalFrames:
        frames = []
        for j in range(i, i + 100):
            ret, frame = cap.read()
            if ret:
                frames += [frame]
            else:
                print('video read error (or finished reading)')
        predictions = get_actual_labels(predict_aux(frames, vgg_model, vgg))
        labels += list(predictions)
        print(i)
        i += 100
        if (i > 0) and (i % 1000) == 0:
            np.save('Test2_predicted_labels_new77.npy', np.array(labels))

    np.save('Test2_predicted_labels_new77.npy', np.array(labels))
示例#3
0
def predict_labels():
    vgg_model = VGG16()
    # re-structure the model
    vgg_model.layers.pop()
    vgg_model = Model(inputs=vgg_model.inputs,
                      outputs=vgg_model.layers[-1].output)
    # testing on the labelled Test1 video
    vgg_model = final_model.Model().model
    # vgg_model.load_weights('weights-improvement-28-0.88.hdf5')
    vgg_model.load_weights('weights-improvement-06-0.88.hdf5')

    cap = cv2.VideoCapture(
        'C:\\Users\\Rani\\Desktop\\AI project\\data (video)\\Test1.mp4')
    totalFrames = cap.get(cv2.CAP_PROP_FRAME_COUNT)
    i = 0
    labels = []
    while i < totalFrames:
        frames = []
        for j in range(i, i + 100):
            ret, frame = cap.read()
            if ret:
                frames += [cv2.resize(frame, (0, 0), fx=0.4, fy=0.4)]
            else:
                print('video read error (or finished reading)')
        predictions = get_actual_labels(predict_aux(frames, vgg_model, vgg))
        labels += list(predictions)
        print(i)
        i += 100
        if (i > 0) and (i % 1000) == 0:
            np.save('Test1_predicted_labels_new.npy', np.array(labels))

    np.save('Test1_predicted_labels_new.npy', np.array(labels))
示例#4
0
def calculate_test1_accuracy():
    # testing on the labelled Test1 video
    vgg_model = final_model().model
    # vgg_model.load_weights('weights-improvement-16-0.87.hdf5')
    vgg_model.load_weights('weights-improvement-257-0.80.hdf5')

    adam = Adam(lr=0.0001,
                beta_1=0.9,
                beta_2=0.999,
                epsilon=None,
                decay=0.0,
                amsgrad=False)
    vgg_model.compile(loss='categorical_crossentropy',
                      optimizer=adam,
                      metrics=['accuracy'])

    # test_folder = 'C:\\Users\\Rani\\Desktop\\AI Pycharm Project\\labelled data\\test\\'
    test_folder = 'C:\\Users\\Rani\\Desktop\\AI Pycharm Project\\labelled data 2\\test1\\'
    X_test1 = np.load(test_folder + 'image_array.npy')
    y_test1 = np.load(test_folder + 'labels_array.npy')

    y_test1[y_test1 == 'Real'] = 0
    y_test1[y_test1 == 'Barca'] = 1
    y_test1[y_test1 == 'Other'] = 2
    y_test1 = to_categorical(y_test1, 3)

    X_val = X_test1
    y_val = y_test1

    vgg = VGG16()
    # re-structure the model
    vgg.layers.pop()
    vgg.layers.pop()
    vgg = Model(inputs=vgg.inputs, outputs=vgg.layers[-1].output)
    # testing on the labelled Test1 video
    predictions = get_actual_labels(predict_aux(X_val, vgg_model, vgg))

    print('normal test accuracy=',
          vgg_model.evaluate(get_image_features(X_val, vgg), y_val))
    print('non_neutral test accuracy=',
          non_neutral_accuracy(get_actual_labels(y_val), predictions))
示例#5
0
    predicted_class_indices = np.argmax(pred, axis=1)
    labels = train_generator.class_indices
    labels = dict((v, k) for k, v in labels.items())
    predictions = np.array([labels[k] for k in predicted_class_indices])

    print('manual acc calculation: ',
          sum(predictions == y_test) / y_test.shape[0])
    print('sklearn acc calculation: ', accuracy_score(y_test, predictions))

    y_test[y_test == 'Real'] = 0
    y_test[y_test == 'Barca'] = 1
    y_test[y_test == 'Other'] = 2
    y_test = keras.utils.to_categorical(y_test, 3)
    print('model.evaluate: ', model.evaluate(X_test, y_test, batch_size=8))
    print('non_neutral test accuracy=',
          non_neutral_accuracy(get_actual_labels(y_test), predictions))
    # print('non_neutral test accuracy=', non_neutral_accuracy(get_actual_labels(y_test), get_actual_labels(model.predict(X_test, batch_size=8))))
    exit()

    X_test = np.array([
        preprocess_input(x) for x in np.load(test_folder + 'image_array.npy')
    ])

    for image in X_test:
        cv2.rectangle(image, (40, 16), (70, 21), (255, 255, 255), 10)

    y_test = np.load(test_folder + 'labels_array.npy')
    y_test[y_test == 'Real'] = 0
    y_test[y_test == 'Barca'] = 1
    y_test[y_test == 'Other'] = 2
    y_test = keras.utils.to_categorical(y_test, 3)
                        metrics=['accuracy'])

    # print(model.model.summary())
    # exit()

    weights_file_name = "weights-improvement-{epoch:02d}-{val_acc:.2f}.hdf5"
    checkpoint = ModelCheckpoint(saved_weights_path + weights_file_name,
                                 monitor='val_acc',
                                 verbose=1,
                                 save_best_only=True,
                                 mode='max')
    callbacks_list = [checkpoint]
    model.model.load_weights('weights-improvement-06-0.81.hdf5')
    ##############
    # print(model.model.summary())
    # exit()
    print('normal validation accuracy=', model.model.evaluate(X_val, y_val))
    print(
        'non_neutral validation accuracy=',
        non_neutral_accuracy(get_actual_labels(y_val),
                             get_actual_labels(model.model.predict(X_val))))
    exit()
    ##############
    model.model.fit(X_train,
                    y_train,
                    batch_size=64,
                    epochs=50,
                    callbacks=callbacks_list,
                    validation_data=(X_val, y_val),
                    verbose=2)