Example #1
0
def train_classifier(path):
    #user_names = glob(path + "/*")
    metadata = load_metadata(path)
    classifier = Classifier(method='svc')

    '''This is for training'''
    return classifier.trainClassifier(metadata)
Example #2
0
 def grid_search(self, ml_method, classifier_name, grid, splits, n_pca):
     print(
         compose_configuration('Grid Search', self.config['filter_latent'],
                               self.config['standardization'], n_pca,
                               self.name))
     print(ml_method, classifier_name)
     classifier = Classifier(self.config_filepath, ml_method,
                             classifier_name)
     classifier.grid_search(self.name, grid, splits, n_pca)
Example #3
0
 def _fit_all(self, n_pca):
     print(
         compose_configuration('Fitting features',
                               self.config['filter_latent'],
                               self.config['standardization'], n_pca,
                               self.name))
     for ml_method in self.config['ML-method-options']:
         for classifier_name in self.config['Classifier-options']:
             classifier = Classifier(self.config_filepath, ml_method,
                                     classifier_name)
             scores = classifier.fit(self.name, n_pca)
             if ml_method in self.results:
                 self.results[ml_method][classifier_name] = scores
             else:
                 self.results[ml_method] = {classifier_name: scores}
#faceCascade = cv2.CascadeClassifier(cascadePath)
font = cv2.FONT_HERSHEY_SIMPLEX

#path = "/Users/siddhartham/Sid/PycharmProjects/Face-Recognition/dataset"
path = "/Users/siddhartham/Sid/PycharmProjects/Deep-Face-Recognition-Using-Inception-Model-Keras-OpenCV-Dlib/FaceRecognition_dataset"


def assure_path_exists(path):
    dir = os.path.dirname(path)
    if not os.path.exists(dir):
        os.makedirs(dir)


metadata = load_metadata(path)

classifier = Classifier(method='svc')
'''This is for training'''
#classifier.trainClassifier(metadata)

from glob import glob
#img_mask = path + '/**/' + '*.jpg'
user_names = glob(path + "/*")

#cam = cv2.VideoCapture('/Users/siddhartham/Sid/PycharmProjects/CNN-face-recognition/id-face_turn.MOV')
#cam = cv2.VideoCapture(0)
imagePath = '/Users/siddhartham/Sid/PycharmProjects/Deep-Face-Recognition-Using-Inception-Model-Keras-OpenCV-Dlib/Edmund_photos/Edmund_id_2.png'
# Loop
while True:
    # Read the video frame
    #ret, img =cam.read()
num_sents=0
for doc in testing_doc_objs.values():
    for sent in doc.sentence_obj_list:
        num_sents+=1
        for event in sent.set_entities:
            if event.type in Globals.SPECIFIC_CLASSIFIER_TYPES:
                count +=1
print("NUMBER OF SUBSTANCE ABUSE EVENTS:"+ str(count))
print("NUMBER OF SENTENCES: " + str(num_sents))
##########################################
#### EVENT CLASSIFICATION       #########
########################################

# Train classifiers
training_feat_extractor = FeatureExtractor(training_doc_objs)
classifiers, feature_maps, sent_info = Classifier.train_models(training_feat_extractor)

# Classify sentences - Substance abuse general, and abuse type classifications
testing_feat_extractor = FeatureExtractor(testing_doc_objs)
sent_classification_info = Classifier.get_classifications(classifiers, feature_maps, testing_feat_extractor)

# How to use:
print("\nSentence Objects with substance info:\n" + str(sent_classification_info.get_sentences_w_info(Globals.SUBSTANCE)))
print("Sentence Objects with alcohol info:\n" + str(sent_classification_info.get_sentences_w_info(Globals.ALCOHOL)))

results_file = "classifier_results.txt"
sent_classification_info.evaluate_classifications(results_file, TEST_FOLD)

########################################
####   STATUS CLASSIFICATION ##########
######################################
    def __init__(self):
        super(FinalModel, self).__init__()

        # MULTI ATTENTION SUBNET
        self.vgg_features_initial = tf.keras.applications.VGG19(
            input_shape=IMG_SHAPE, include_top=False,
            weights='imagenet')  # VGG_feature()
        self.vgg_features_initial.trainable = False
        self.kmeans = Kmeans(clusters_n=2, iterations=10)
        self.average_pooling_0 = Average_Pooling()
        self.average_pooling_1 = Average_Pooling()
        """
        self.fc_0 = Fc(CHANNELS)
        self.fc_1 = Fc(CHANNELS)
        """
        self.initializer = tf.keras.initializers.glorot_normal()
        self.fc1_1 = tf.keras.layers.Dense(512,
                                           input_shape=(512, ),
                                           activation="relu",
                                           kernel_initializer=self.initializer)
        self.fc1_2 = tf.keras.layers.Dense(512,
                                           input_shape=(512, ),
                                           activation="relu",
                                           kernel_initializer=self.initializer)

        self.fc2_1 = tf.keras.layers.Dense(512,
                                           activation="sigmoid",
                                           kernel_initializer=self.initializer)
        self.fc2_2 = tf.keras.layers.Dense(512,
                                           activation="sigmoid",
                                           kernel_initializer=self.initializer)

        self.bn0 = tf.keras.layers.BatchNormalization()
        self.bn1 = tf.keras.layers.BatchNormalization()

        self.weighted_sum0 = WeightedSum()
        self.weighted_sum1 = WeightedSum()
        self.c_init = tf.random_normal_initializer(0, 0.01)
        self.C = tf.Variable(initial_value=self.c_init(shape=(N_CLASSES,
                                                              SEMANTIC_SIZE),
                                                       dtype=tf.float32),
                             trainable=True)

        # cropping net
        self.crop_net0 = RCN(hidden_unit=14, map_size=14, image_size=448)
        self.crop_net1 = RCN(hidden_unit=14, map_size=14, image_size=448)
        self.crop0 = Crop()
        self.crop1 = Crop()

        # joint feature learning subnet
        self.reshape_global = ReShape224()
        self.reshape_local0 = ReShape224()
        self.reshape_local1 = ReShape224()

        self.vgg_features_global = tf.keras.applications.VGG19(
            input_shape=(224, 224, 3), include_top=False,
            weights='imagenet')  # VGG_feature()
        self.vgg_features_global.trainable = False
        self.vgg_features_local0 = tf.keras.applications.VGG19(
            input_shape=(224, 224, 3), include_top=False,
            weights='imagenet')  # VGG_feature()
        self.vgg_features_local0.trainable = False
        self.vgg_features_local1 = tf.keras.applications.VGG19(
            input_shape=(224, 224, 3), include_top=False,
            weights='imagenet')  # VGG_feature()
        self.vgg_features_local1.trainable = False

        self.average_pooling_global = tf.keras.layers.GlobalAveragePooling2D(
        )  # Average_Pooling()
        self.average_pooling_local0 = tf.keras.layers.GlobalAveragePooling2D(
        )  # Average_Pooling()
        self.average_pooling_local1 = tf.keras.layers.GlobalAveragePooling2D(
        )  # Average_Pooling()

        self.joint_net_global = JFL(N_CLASSES, SEMANTIC_SIZE, CHANNELS)
        self.joint_net_local0 = JFL(N_CLASSES, SEMANTIC_SIZE, CHANNELS)
        self.joint_net_local1 = JFL(N_CLASSES, SEMANTIC_SIZE, CHANNELS)

        self.global_score = Scores()
        self.score0 = Scores()
        self.score1 = Scores()

        self.classifier = Classifier(N_CLASSES)
Example #7
0
from align import AlignDlib
from Preprocessing import convertToGrayscale, align_image, load_metadata, load_image
from Classification import Classifier

path = "/Users/siddhartham/Sid/PycharmProjects/Deep-Face-Recognition-Using-Inception-Model-Keras-OpenCV-Dlib/FaceRecognition_dataset"


def assure_path_exists(path):
    dir = os.path.dirname(path)
    if not os.path.exists(dir):
        os.makedirs(dir)


metadata = load_metadata(path)

classifier = Classifier(method='svc')
classifier.trainClassifier(metadata)

from glob import glob
#img_mask = path + '/**/' + '*.jpg'
user_names = glob(path + "/*")

for user in user_names:
    images = glob(user + "/*.jpg")

    index = random.randrange(len(images))

    img_path = images[index]

    print(img_path)
    img = load_image(img_path)
Example #8
0
    def __init__(self):
        super(FinalModel, self).__init__()

        # MULTI ATTENTION SUBNET
        self.vgg_features_initial = tf.keras.applications.VGG19(
            input_shape=IMG_SHAPE, include_top=False,
            weights='imagenet')  # VGG_feature()
        self.vgg_features_initial.trainable = False
        self.kmeans = Kmeans(clusters_n=2,
                             iterations=10,
                             batch_size=BATCH_SIZE)
        self.average_pooling_0 = tf.keras.layers.GlobalAveragePooling2D()
        self.average_pooling_1 = tf.keras.layers.GlobalAveragePooling2D()
        self.fc1_1 = tf.keras.layers.Dense(CHANNELS,
                                           input_shape=(512, ),
                                           activation="relu")
        self.fc1_2 = tf.keras.layers.Dense(CHANNELS,
                                           input_shape=(512, ),
                                           activation="relu")
        self.fc2_1 = tf.keras.layers.Dense(CHANNELS, activation="sigmoid")
        self.fc2_2 = tf.keras.layers.Dense(CHANNELS, activation="sigmoid")

        self.bn0 = tf.keras.layers.BatchNormalization()
        self.bn1 = tf.keras.layers.BatchNormalization()

        self.weighted_sum0 = WeightedSum()
        self.weighted_sum1 = WeightedSum()
        self.c_init = tf.random_normal_initializer(0, 0.01)
        self.C = tf.Variable(initial_value=self.c_init(shape=(N_CLASSES,
                                                              SEMANTIC_SIZE),
                                                       dtype=tf.float32),
                             trainable=True)

        # cropping net
        self.crop_net0 = RCN(hidden_unit=14, map_size=14, image_size=448)
        self.crop_net1 = RCN(hidden_unit=14, map_size=14, image_size=448)
        self.crop0 = Crop()
        self.crop1 = Crop()

        # joint feature learning subnet
        '''shit'''
        '''
        self.reshape_global = ReShape224()
        self.reshape_local0 = ReShape224()
        self.reshape_local1 = ReShape224()
        self.vgg_features_global = tf.keras.applications.VGG19(input_shape=(224, 224, 3),
                                                               include_top=False,
                                                               weights='imagenet')  # VGG_feature()
        self.vgg_features_global.trainable = False
        self.vgg_features_local0 = tf.keras.applications.VGG19(input_shape=(224, 224, 3),
                                                               include_top=False,
                                                               weights='imagenet')  # VGG_feature()
        self.vgg_features_local0.trainable = False
        self.vgg_features_local1 = tf.keras.applications.VGG19(input_shape=(224, 224, 3),
                                                               include_top=False,
                                                               weights='imagenet')  # VGG_feature()
        self.vgg_features_local1.trainable = False
        self.global_score = Scores()
        self.score0 = Scores()
        self.score1 = Scores()
        '''
        self.vgg_features_global = tf.keras.applications.VGG19(
            input_shape=(224, 224, 3),
            include_top=False,
            weights='imagenet',
            pooling="avg")
        self.W = tf.keras.layers.Dense(SEMANTIC_SIZE, activation="relu")
        self.l2loss = tf.keras.layers.Lambda(lambda x: tf.keras.backend.sum(
            tf.keras.backend.square(x[0] - x[1][:, 0]), 1, keepdims=True))
        #self.fc = tf.keras.layers.Dense(N_CLASSES, activation="softmax")
        # trainable class centers, C={c1, c2, ..., c_{n_classes}}
        c_init = tf.random_normal_initializer(0, 0.01)
        self.C = tf.Variable(initial_value=c_init(shape=(SEMANTIC_SIZE,
                                                         N_CLASSES),
                                                  dtype='float32'),
                             trainable=True,
                             name="C")
        self.classifier = Classifier(N_CLASSES)