def main():
    K.set_image_dim_ordering('tf')
    #sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
    #os.getcwd()
    current_path = os.getcwd()
    # from keras_video_classifier.library.recurrent_networks import VGG16LSTMVideoClassifier

    #from keras_video_classifier.library.utility.ucf.UCF101_loader import load_ucf, scan_ucf_with_labels

    vgg16_include_top = True
    data_dir_path = os.path.join(current_path, 'test_video')
    model_dir_path = os.path.join(current_path, 'models', 'p')
    config_file_path = VGG16LSTMVideoClassifier.get_config_file_path(
        model_dir_path, vgg16_include_top=vgg16_include_top)
    weight_file_path = VGG16LSTMVideoClassifier.get_weight_file_path(
        model_dir_path, vgg16_include_top=vgg16_include_top)

    np.random.seed(42)

    #load_ucf(data_dir_path)

    predictor = VGG16LSTMVideoClassifier()
    predictor.load_model(config_file_path, weight_file_path)

    #videos = scan_ucf_with_labels(data_dir_path)

    #video_file_path_list = data_dir_path

    #correct_count = 0
    #count = 0
    labels = []
    videos = []
    outlier = []
    video_list = os.listdir(data_dir_path)
    #video_list_10 = video_list[800:820]
    count = 0
    #for video in data_dir_path:
    for video in video_list:
        print(count)
        video = os.path.join(data_dir_path, video)
        #label = videos[vide o_file_path]
        try:
            predicted_label = predictor.predict(video)
            videos = np.append(videos, video)
            labels = np.append(labels, predicted_label)
        except:
            print(video)
            outlier.append(video)
        count += 1
        #print('predicted: ' + predicted_label + ' actual: ' + label)
        #correct_count = correct_count + 1 if label == predicted_label else correct_count
        #count += 1
        #accuracy = correct_count / count
        #print('accuracy: ', accuracy)
    return videos, labels, outlier
def main():
    K.set_image_dim_ordering('tf')
    sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
    from keras_video_classifier.library.utility.plot_utils import plot_and_save_history,plot_history_2win
    from keras_video_classifier.library.recurrent_networks import VGG16LSTMVideoClassifier
    from keras_video_classifier.library.utility.ucf.UCF101_loader import load_ucf
    
    print("PASS LOAD UCF")
    data_set_name = 'UCF-101'
    input_dir_path = os.path.join(os.path.dirname(__file__), 'very_large_data')
    output_dir_path = os.path.join(os.path.dirname(__file__), 'models', data_set_name)
    report_dir_path = os.path.join(os.path.dirname(__file__), 'reports', data_set_name)

    np.random.seed(42)

    # this line downloads the video files of UCF-101 dataset if they are not available in the very_large_data folder
    load_ucf(input_dir_path)

    classifier = VGG16LSTMVideoClassifier()

    history = classifier.fit(data_dir_path=input_dir_path, model_dir_path=output_dir_path, vgg16_include_top=False,
                             data_set_name=data_set_name)
    print("history = classifier.fit")
    plot_and_save_history(history, VGG16LSTMVideoClassifier.model_name,report_dir_path + '/' + VGG16LSTMVideoClassifier.model_name + '-hi-dim-history.png')
    plot_history_2win(history, VGG16LSTMVideoClassifier.model_name,report_dir_path + '/' + VGG16LSTMVideoClassifier.model_name + '-hi-dim-history2win.png')
def main():

    K.set_image_dim_ordering('tf')
    sys.path.append(os.path.join(os.path.dirname(__file__), '..'))

    from keras_video_classifier.library.utility.plot_utils import plot_and_save_history
    from keras_video_classifier.library.recurrent_networks import VGG16LSTMVideoClassifier
    #from keras_video_classifier.library.utility.ucf.UCF101_loader import load_ucf

    data_set_name = 'AM'
    input_dir_path = os.path.join(os.path.dirname(__file__), 'AM_data')
    output_dir_path = os.path.join(os.path.dirname(__file__), 'models',
                                   data_set_name)
    report_dir_path = os.path.join(os.path.dirname(__file__), 'reports',
                                   data_set_name)
    np.random.seed(35)

    # this line downloads the video files of UCF-101 dataset if they are not available in the very_large_data folder
    #load_ucf(input_dir_path)

    classifier = VGG16LSTMVideoClassifier()

    history = classifier.fit(data_dir_path=input_dir_path,
                             model_dir_path=output_dir_path,
                             data_set_name=data_set_name,
                             from_picture=True)

    plot_and_save_history(
        history, VGG16LSTMVideoClassifier.model_name, report_dir_path + '/' +
        VGG16LSTMVideoClassifier.model_name + '-history.png')
Beispiel #4
0
def main():
    # K.set_image_dim_ordering('tf')
    sys.path.append(os.path.join(os.path.dirname(__file__), '..'))

    from keras_video_classifier.library.recurrent_networks import VGG16LSTMVideoClassifier
    from keras_video_classifier.library.utility.ucf.UCF101_loader import load_ucf, scan_ucf_with_labels

    vgg16_include_top = True
    data_dir_path = os.path.join(os.path.dirname(__file__), 'very_large_data')
    model_dir_path = os.path.join(os.path.dirname(__file__), 'models',
                                  'UCF-101')
    config_file_path = VGG16LSTMVideoClassifier.get_config_file_path(
        model_dir_path, vgg16_include_top=vgg16_include_top)
    weight_file_path = VGG16LSTMVideoClassifier.get_weight_file_path(
        model_dir_path, vgg16_include_top=vgg16_include_top)

    np.random.seed(42)

    load_ucf(data_dir_path)

    predictor = VGG16LSTMVideoClassifier()
    predictor.load_model(config_file_path, weight_file_path)

    #debug
    # print([label for (label, label_index) in list(predictor.labels.items())[:3]]);
    # print([label for (label, label_index) in predictor.labels.items()])
    #debug over
    videos = scan_ucf_with_labels(
        data_dir_path,
        [label for (label, label_index) in list(predictor.labels.items())[:3]])

    video_file_path_list = np.array([file_path for file_path in videos.keys()])
    np.random.shuffle(video_file_path_list)

    correct_count = 0
    count = 0

    for video_file_path in video_file_path_list:
        label = videos[video_file_path]
        predicted_label = predictor.predict(video_file_path)
        print('predicted: ' + predicted_label + ' actual: ' + label)
        correct_count = correct_count + 1 if label == predicted_label else correct_count
        count += 1
        accuracy = correct_count / count
        print('accuracy: ', accuracy)
def main():
    K.set_image_dim_ordering('tf')
    sys.path.append(os.path.join(os.path.dirname(__file__), '..'))

    from keras_video_classifier.library.recurrent_networks import VGG16LSTMVideoClassifier
    from keras_video_classifier.library.utility.crime.UCF_Crime_loader import load_ucf, scan_ucf_with_labels

    vgg16_include_top = False
    data_dir_path = os.path.join(os.path.dirname(__file__), 'very_large_data')
    model_dir_path = os.path.join(os.path.dirname(__file__), 'models',
                                  'UCF-Anomaly-Detection-Dataset')
    config_file_path = VGG16LSTMVideoClassifier.get_config_file_path(
        model_dir_path, vgg16_include_top=vgg16_include_top)
    weight_file_path = VGG16LSTMVideoClassifier.get_weight_file_path(
        model_dir_path, vgg16_include_top=vgg16_include_top)

    np.random.seed(42)

    load_ucf(data_dir_path)

    predictor = VGG16LSTMVideoClassifier()
    predictor.load_model(config_file_path, weight_file_path)

    videos = scan_ucf_with_labels(
        data_dir_path,
        [label for (label, label_index) in predictor.labels.items()])

    video_file_path_list = np.array([file_path for file_path in videos.keys()])
    np.random.shuffle(video_file_path_list)

    correct_count = 0
    count = 0

    print("Begin Predict Using UCF-CRIME-Model: ")
    for video_file_path in video_file_path_list:
        label = videos[video_file_path]
        predicted_label = predictor.predict(video_file_path)
        print('predicted: ' + predicted_label + ' actual: ' + label)
        correct_count = correct_count + 1 if label == predicted_label else correct_count
        count += 1
        accuracy = correct_count / count
        print('correct_count: ' + str(correct_count) + ' count: ' + str(count))
        print('accuracy: ', accuracy)
Beispiel #6
0
def main():
    #K.set_image_dim_ordering('tf')
    K.set_image_data_format('channels_last')
    sys.path.append(os.path.join(os.path.dirname(__file__), '..'))

    from keras_video_classifier.library.utility.plot_utils import plot_and_save_history
    from keras_video_classifier.library.recurrent_networks import VGG16LSTMVideoClassifier
    from keras_video_classifier.library.utility.ucf.UCF101_loader import load_ucf
    #from keras_video_classifier.library.utility.crime.UCF_Crime_loader import load_ucf

    data_set_name = 'UCF-Anomaly-Detection-Dataset'
    input_dir_path = os.path.join(os.path.dirname(__file__),
                                  '/content/drive/My Drive')
    output_dir_path = os.path.join(os.path.dirname(__file__),
                                   '/content/drive/My Drive/models',
                                   data_set_name)
    report_dir_path = os.path.join(os.path.dirname(__file__),
                                   '/content/drive/My Drive/reports',
                                   data_set_name)

    np.random.seed(42)

    # this line downloads the video files of UCF-101 dataset if they are not available in the very_large_data folder
    load_ucf(input_dir_path)

    classifier = VGG16LSTMVideoClassifier()

    history = classifier.fit(data_dir_path=input_dir_path,
                             model_dir_path=output_dir_path,
                             vgg16_include_top=False,
                             data_set_name=data_set_name,
                             test_size=0.1)

    plot_and_save_history(
        history, VGG16LSTMVideoClassifier.model_name, report_dir_path + '/' +
        VGG16LSTMVideoClassifier.model_name + '-hi-dim-history.png')
Beispiel #7
0
def main():
    K.set_image_dim_ordering('tf')
    sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
    from keras_video_classifier.library.recurrent_networks import VGG16LSTMVideoClassifier
    from keras_video_classifier.library.utility.ucf.UCF101_loader import load_ucf, scan_ucf_with_labels
    from keras_video_classifier.library.utility.plot_utils import plot_confusion_matrix
    from sklearn.metrics import confusion_matrix

    vgg16_include_top = False
    data_dir_path = os.path.join(os.path.dirname(__file__), 'very_large_data')
    model_dir_path = os.path.join(os.path.dirname(__file__), 'models', 'UCF-101')
    config_file_path = VGG16LSTMVideoClassifier.get_config_file_path(model_dir_path,
                                                                     vgg16_include_top=vgg16_include_top)
    weight_file_path = VGG16LSTMVideoClassifier.get_weight_file_path(model_dir_path,
                                                                     vgg16_include_top=vgg16_include_top)

    np.random.seed(42)

    load_ucf(data_dir_path)

    predictor = VGG16LSTMVideoClassifier()
    predictor.load_model(config_file_path, weight_file_path)
    
    ### HARD_SPLIT = TRUE MEAN PREDICT IN UCF-101_Train folder ####
    videos = scan_ucf_with_labels(data_dir_path, [label for (label, label_index) in predictor.labels.items()],hard_split=True)
    #print("VIDEO KEY", videos)
    #print("VALUE",videos.values())
    all_label = videos.values()
    #print("TYPE", type(all_label))
    
    Total_label = []
    [Total_label.append(x) for x in all_label if x not in Total_label]
    
    print("Label: ",Total_label)
    #print("Label shape",np.shape(Total_label))
    with open("label.txt", "w") as output:
        output.write(str(Total_label))
    video_file_path_list = np.array([file_path for file_path in videos.keys()])
    
    np.random.shuffle(video_file_path_list)

    correct_count = 0
    count = 0
    label_predicted = []
    label_true = []
    for video_file_path in video_file_path_list:
        label = videos[video_file_path]
        predicted_label = predictor.predict(video_file_path)
        
        label_predicted.append(predicted_label)
        label_true.append(label)
        
        print('predicted: ' + predicted_label + ' actual: ' + label)
        correct_count = correct_count + 1 if label == predicted_label else correct_count
        count += 1
        accuracy = correct_count / count
        print('correct_count: ' + str(correct_count) + ' count: ' + str(count))
        print('accuracy: ', accuracy)
    print("Label_Predicted",np.shape(label_predicted))
    print("True Label",np.shape(label_true))
    
    data_set_name = 'UCF-101'
    report_dir_path = os.path.join(os.path.dirname(__file__), 'reports', data_set_name)
    
    cm = confusion_matrix(label_true,label_predicted)    
    recall = np.diag(cm) / np.sum(cm, axis = 1)
    precision = np.diag(cm) / np.sum(cm, axis = 0)
    import pandas as pd 
    df = pd.DataFrame(cm)
    df.to_csv("CM.csv")
    df = pd.DataFrame(recall)
    df.to_csv("ReCall.csv")
    df = pd.DataFrame(precision)
    df.to_csv("Precision.csv")
    np.set_printoptions(precision=2)
    
    plot_confusion_matrix(cm, Total_label,
                      title='Confusion matrix',normalize=True)
Beispiel #8
0
def main():
    testsetCount = 5
    accuracies = [0, 0, 0, 0, 0]

    for testid in range(0, testsetCount):
        print(testid)

        K.set_image_dim_ordering('tf')
        sys.path.append(os.path.join(os.path.dirname(__file__), '..'))

        from keras_video_classifier.library.utility.plot_utils import plot_and_save_history
        from keras_video_classifier.library.recurrent_networks import VGG16LSTMVideoClassifier
        #from keras_video_classifier.library.utility.ucf.UCF101_loader import load_ucf

        data_set_name = 'AM_pics_' + str(testid)
        input_dir_path = os.path.join(os.path.dirname(__file__), 'AM_data')
        output_dir_path = os.path.join(os.path.dirname(__file__), 'models',
                                       data_set_name)
        report_dir_path = os.path.join(os.path.dirname(__file__), 'reports',
                                       data_set_name)
        print("input_dir_path", input_dir_path)
        print("output_dir_path", output_dir_path)
        print("report_dir_path", report_dir_path)
        np.random.seed(35)

        classifier = VGG16LSTMVideoClassifier()

        history = classifier.fit(data_dir_path=input_dir_path,
                                 model_dir_path=output_dir_path,
                                 data_set_name=data_set_name,
                                 from_picture=True)

        plot_and_save_history(
            history, VGG16LSTMVideoClassifier.model_name,
            report_dir_path + '/' + VGG16LSTMVideoClassifier.model_name +
            '-history_' + str(testid) + '.png')

        K.set_image_dim_ordering('tf')
        sys.path.append(os.path.join(os.path.dirname(__file__), '..'))

        print("predicting now !!!!!")
        data_set_name = 'AM_pics_' + str(testid)
        from keras_video_classifier.library.recurrent_networks import VGG16LSTMVideoClassifier
        from keras_video_classifier.library.utility.ucf.UCF101_loader import load_ucf, scan_ucf_with_labels

        vgg16_include_top = True
        data_dir_path = os.path.join(os.path.dirname(__file__), 'AM_data')
        model_dir_path = os.path.join(os.path.dirname(__file__), 'models',
                                      data_set_name)

        config_file_path = VGG16LSTMVideoClassifier.get_config_file_path(
            model_dir_path, vgg16_include_top=vgg16_include_top)
        weight_file_path = VGG16LSTMVideoClassifier.get_weight_file_path(
            model_dir_path, vgg16_include_top=vgg16_include_top)

        np.random.seed(42)

        # load_ucf(data_dir_path)
        predictor = VGG16LSTMVideoClassifier()
        predictor.load_model(config_file_path, weight_file_path)
        print("Reading weights from :", weight_file_path)
        print("Reading Config from :", config_file_path)
        videos = scan_ucf_with_labels(
            data_dir_path,
            [label
             for (label, label_index) in predictor.labels.items()], testid)
        video_file_path_list = np.array(
            [file_path for file_path in videos.keys()])
        np.random.shuffle(video_file_path_list)
        print(videos)
        correct_count = 0
        count = 0

        for video_file_path in video_file_path_list:
            label = videos[video_file_path]
            predicted_label = predictor.predict(video_file_path,
                                                from_picture=True)
            print('predicted: ' + predicted_label + ' actual: ' + label)
            correct_count = correct_count + 1 if label == predicted_label else correct_count
            count += 1
            accuracy = correct_count / count
            print('accuracy: ', accuracy)
            accuracies[testid] = accuracy

    print(accuracies)
    print(sum(accuracies) / len(accuracies))