示例#1
0
    def __init__(self):
        # im, all_coordinates = demo.detection_demo(im_names)
        global sess, net, keras_model_antnet,keras_model_back,keras_model_face,\
            classes_cows_back,classes_cows_antnet,classes_cows_face, graph_face, graph_back,graph_antnet,graph_objdetect
        sess, net = demo.restore_model('60000')
        graph_objdetect = tf.get_default_graph()
        # tf.reset_default_graph()
        # sess_id, net_id = demo_id.restore_model('100000')

        # set the backend whether tf or theano
        util.set_img_format()

        config.model = 'xception_face'
        # get the keras original model object
        keras_module_face = util.get_model_class_instance()
        # load the best trained model
        keras_model_face = keras_module_face.load()
        graph_face = tf.get_default_graph()
        # get the classes dict
        classes_cows_face = util.get_classes_in_keras_format()

        config.model = 'xception_back'
        # get the keras original model object
        keras_module_back = util.get_model_class_instance()
        # load the best trained model
        keras_model_back = keras_module_back.load()
        graph_back = tf.get_default_graph()
        # get the classes dict
        classes_cows_back = util.get_classes_in_keras_format()

        config.model = 'antnet'
        # get the keras original model object
        keras_module_antnet = util.get_model_class_instance()
        # load the best trained model
        keras_model_antnet = keras_module_antnet.load()
        graph_antnet = tf.get_default_graph()
        # get the classes dict
        classes_cows_antnet = util.get_classes_in_keras_format()

        # warm up the model
        print('Warming up the keras model face')
        input_shape = (1, ) + keras_module_face.img_size + (3, )
        dummpy_img = numpy.ones(input_shape)
        dummpy_img = preprocess_input(dummpy_img)
        keras_model_face.predict(dummpy_img)

        # warm up the model
        print('Warming up the keras model back')
        input_shape = (1, ) + keras_module_back.img_size + (3, )
        dummpy_img = numpy.ones(input_shape)
        dummpy_img = preprocess_input(dummpy_img)
        keras_model_back.predict(dummpy_img)

        # warm up the model
        print('Warming up the keras model antnet')
        input_shape = (1, ) + keras_module_antnet.img_size + (3, )
        dummpy_img = numpy.ones(input_shape)
        dummpy_img = preprocess_input(dummpy_img)
        keras_model_antnet.predict([dummpy_img, np.random.rand(1)])
def train_nn():
    df = pd.read_csv(config.activations_path)
    df, y, classes = encode(df)
    X_train, X_test, y_train, y_test = train_test_split(df.values, y, test_size=0.2, random_state=17)

    model_module = util.get_model_class_instance()

    model = Sequential()
    model.add(Dense(48, input_dim=model_module.noveltyDetectionLayerSize, activation='elu', init='uniform'))
    model.add(Dropout(0.5))
    model.add(Dense(32, activation='elu', init='uniform'))
    model.add(Dropout(0.5))
    model.add(Dense(len(classes), activation='softmax', init='uniform'))

    model.compile(loss='sparse_categorical_crossentropy', optimizer='sgd', metrics=['accuracy'])

    early_stopping = EarlyStopping(verbose=1, patience=40, monitor='val_loss')
    model_checkpoint = ModelCheckpoint(config.get_novelty_detection_model_path(), save_best_only=True,
                                       save_weights_only=True, monitor='val_loss')
    callbacks_list = [early_stopping, model_checkpoint]

    model.fit(
        X_train,
        y_train,
        nb_epoch=300,
        validation_data=(X_test, y_test),
        batch_size=16,
        callbacks=callbacks_list)

    out = model.predict(X_test)
    predictions = np.argmax(out, axis=1)
    print("Accuracy: ", accuracy_score(y_test, predictions))
示例#3
0
def train(nb_epoch, freeze_layers_number):
    model = util.get_model_class_instance(
        class_weight=util.get_class_weight(config.train_dir),
        nb_epoch=nb_epoch,
        freeze_layers_number=freeze_layers_number)
    model.train()
    print('Training is finished!')
示例#4
0
def train_nn():
    df = pd.read_csv(config.activations_path)
    df, y, classes = encode(df)
    X_train, X_test, y_train, y_test = train_test_split(df.values, y, test_size=0.2, random_state=17)

    model_module = util.get_model_class_instance()

    model = Sequential()
    model.add(Dense(48, input_dim=model_module.noveltyDetectionLayerSize, activation='elu', init='uniform'))
    model.add(Dropout(0.5))
    model.add(Dense(32, activation='elu', init='uniform'))
    model.add(Dropout(0.5))
    model.add(Dense(len(classes), activation='softmax', init='uniform'))

    model.compile(loss='sparse_categorical_crossentropy', optimizer='sgd', metrics=['accuracy'])

    early_stopping = EarlyStopping(verbose=1, patience=40, monitor='val_loss')
    model_checkpoint = ModelCheckpoint(config.get_novelty_detection_model_path(), save_best_only=True,
                                       save_weights_only=True, monitor='val_loss')
    callbacks_list = [early_stopping, model_checkpoint]

    model.fit(
        X_train,
        y_train,
        nb_epoch=300,
        validation_data=(X_test, y_test),
        batch_size=1,
        callbacks=callbacks_list)

    out = model.predict(X_test)
    predictions = np.argmax(out, axis=1)
    print("Accuracy: ", accuracy_score(y_test, predictions))
示例#5
0
def train():
    model = util.get_model_class_instance(
        class_weight=util.get_class_weight(config.train_dir),
        nb_epoch=args.nb_epoch,
        batch_size=args.batch_size,
        freeze_layers_number=args.freeze_layers_number)
    model.train()
    print('Training is finished!')
def train():
    model = util.get_model_class_instance(
        class_weight=util.get_class_weight(config.train_dir),
        nb_epoch=args.nb_epoch,
        batch_size=args.batch_size,
        freeze_layers_number=args.freeze_layers_number)
    model.train()
    print('Training is finished!')
示例#7
0
def train(nb_epoch,
          freeze_layers_number,
          auto_load_finetune=False,
          visual=False):
    model = util.get_model_class_instance(
        class_weight=util.get_class_weight(config.train_dir),
        nb_epoch=nb_epoch,
        freeze_layers_number=freeze_layers_number)
    model.train(auto_load_fine_tune=auto_load_finetune, visual=visual)
    print('Training is finished!')
示例#8
0
def predict_target_by_paths(paths, augment_times=1):
    util.set_img_format()

    # sys.argv[0] = "--model=resnet50"
    predict.args = predict.parse_args()
    predict.model_module = util.get_model_class_instance()
    predict.model = predict.model_module.load()
    predict.classes_in_keras_format = util.get_classes_in_keras_format()

    for path in paths:
        predict.predict(dir=path, augment_times=augment_times)
示例#9
0
def run():
    model = util.get_model_class_instance(
        class_weight=util.get_class_weight(config.train_dir),
        nb_epoch=args.nb_epoch,
        batch_size=args.batch_size,
        freeze_layers_number=args.freeze_layers_number)

    #config.train_dir = 'data/sorted/test/'
    #model.load()
    #model.test()
    model.train()
    #model.extract()
    model.evaluate()

    print('Training is finished!')
示例#10
0
def predict_target_by_dir(main_dir, augment_times=1):
    util.set_img_format()

    # sys.argv[0] = "--model=resnet50"
    predict.args = predict.parse_args()
    predict.model_module = util.get_model_class_instance()
    predict.model = predict.model_module.load()
    predict.classes_in_keras_format = util.get_classes_in_keras_format()

    dirs = glob.glob(main_dir + "*")

    global_summary = {"total": 0, "trues": 0, "falses": 0, "acc": 0}
    iter = 0
    for dir in dirs:
        stats = predict.predict(dir + os.sep, iter, augment_times, False)
        iter += 1
        global_summary["total"] += stats["summary"]["total"]
        global_summary["trues"] += stats["summary"]["trues"]
        global_summary["falses"] += stats["summary"]["falses"]
    global_summary["acc"] = float(
        global_summary["trues"]) / global_summary["total"]
    print("Global summary: {0}".format(global_summary))
示例#11
0
if __name__ == '__main__':
    tic = time.clock()

    args = parse_args()
    print('=' * 50)
    print('Called with args:')
    print(args)

    if args.data_dir:
        config.data_dir = args.data_dir
        config.set_paths()
    if args.model:
        config.model = args.model

    util.set_img_format()
    model_module = util.get_model_class_instance()
    model = model_module.load()

    classes_in_keras_format = util.get_classes_in_keras_format()

    all_metrix = []
    print('args.path:', os.listdir(args.path))
    for cow_dir in os.listdir(args.path):
        root = args.path + str(cow_dir)
        store_label = predict(root)
        # print(store_label)
        all_metrix.append(metrix(store_label))

    total = 0
    for i in range(len(all_metrix)):
        total += all_metrix[i]
示例#12
0
    try:
        args = parse_args()

        if args.data_dir:
            config.data_dir = args.data_dir
            config.set_paths()
        if args.model:
            config.model = args.model

        util.lock()
        util.override_keras_directory_iterator_next()
        util.set_classes_from_train_dir()
        util.set_samples_info()
        if not os.path.exists(config.trained_dir):
            os.mkdir(config.trained_dir)

        class_weight = util.get_class_weight(config.train_dir)
        # TODO: create class instance without dynamic module import
        model = util.get_model_class_instance(
            class_weight=class_weight,
            nb_epoch=args.nb_epoch,
            freeze_layers_number=args.freeze_layers_number)
        model.train()
        print('Training is finished!')
    except (KeyboardInterrupt, SystemExit):
        util.unlock()
    except Exception as e:
        print(e)
        print(traceback.format_exc())
    util.unlock()
示例#13
0
                                       normalize=True)


if __name__ == '__main__':
    tic = time.clock()  #return the current cpu time

    args = parse_args()
    print('=' * 50)
    print('Called with args:')
    print(args)

    if args.data_dir:  #user defined directory
        config.data_dir = args.data_dir  #~
        config.set_paths()  #~
    if args.model:  #user defined model
        config.model = args.model  #~

    util.set_img_format()  #channel_first or channels_last
    model_module = util.get_model_class_instance()  #class model.resnet50
    model = model_module.load(
    )  #creat base_model and load trained weight!(ResNet50) "G:\keras-transfer-learning-for-oxford102\trained\fine-tuned-resnet50-weights.h5"

    classes_in_keras_format = util.get_classes_in_keras_format(
    )  #get a dictory of classes

    predict(args.path)  #we must input a path of directory including pictures

    if args.execution_time:  #
        toc = time.clock()  #record current time
        print('Time: %s' % (toc - tic))  #print execution time
            cnf_matrix = confusion_matrix(y_trues, predictions)
            util.plot_confusion_matrix(cnf_matrix, config.classes, normalize=False)
            util.plot_confusion_matrix(cnf_matrix, config.classes, normalize=True)


if __name__ == '__main__':
    tic = time.clock()

    args = parse_args()
    print('=' * 50)
    print('Called with args:')
    print(args)

    if args.data_dir:
        config.data_dir = args.data_dir
        config.set_paths()
    if args.model:
        config.model = args.model

    util.set_img_format()
    model_module = util.get_model_class_instance()
    model = model_module.load()

    classes_in_keras_format = util.get_classes_in_keras_format()

    predict(args.path)

    if args.execution_time:
        toc = time.clock()
        print('Time: %s' % (toc - tic))
示例#15
0
    print("Test size: ", len(test_data))
    v = (len(test_data) // batch_size)
    print("Loaded images paths and labels!")
    try:
        args = parse_args()
        if args.data_dir:
            config.data_dir = args.data_dir
            config.set_paths()
        if args.model:
            config.model = args.model

        init()

        model = util.get_model_class_instance(
            class_weight=util.get_class_weight(config.train_dir),
            nb_epoch=args.nb_epoch,
            batch_size=args.batch_size,
            freeze_layers_number=args.freeze_layers_number)

        model.load()
        print("Loaded Model! Now compiling...")
        model.model.compile(loss='categorical_crossentropy',
                            optimizer=Adam(lr=1e-5),
                            metrics=['accuracy'])
        model.model.summary()
        print("Compiled! now evaluating")
        model.model.evaluate_generator(imageLoader(test_data, batch_size),
                                       steps=v)
    except Exception as e:
        print(e)
        traceback.print_exc()