Beispiel #1
0
 def train(self):
     # Check if the given dataset exists:
     if not os.path.exists(self.dataset):
         print ">> [Error] No Dataset Found at '%s'." % self.dataset
         sys.exit(1)
     # Reads the images, labels and folder_names from a given dataset. Images
     # are resized to given size on the fly:
     print ">> Loading Dataset <-- " + self.dataset
     [images, labels,
      subject_names] = self.read_images(self.dataset, self.image_size)
     # Zip us a {label, name} dict from the given data:
     list_of_labels = list(xrange(max(labels) + 1))
     subject_dictionary = dict(zip(list_of_labels, subject_names))
     # Get the model we want to compute:
     model = self.get_model(image_size=self.image_size,
                            subject_names=subject_dictionary)
     # Sometimes you want to know how good the model may perform on the data
     # given, the script allows you to perform a k-fold Cross Validation before
     # the Detection & Recognition part starts:
     if self.numfolds is not None:
         print ">> Validating Model With %s Folds.." % self.numfolds
         # We want to have some log output, so set up a new logging handler
         # and point it to stdout:
         handler = logging.StreamHandler(sys.stdout)
         formatter = logging.Formatter(
             '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
         handler.setFormatter(formatter)
         # Add a handler to facerec modules, so we see what's going on inside:
         logger = logging.getLogger("facerec")
         logger.addHandler(handler)
         logger.setLevel(logging.DEBUG)
         # Perform the validation & print results:
         crossval = KFoldCrossValidation(model, k=self.numfolds)
         crossval.validate(images, labels)
         crossval.print_results()
     # Compute the model:
     print ">> Computing Model.."
     model.compute(images, labels)
     # And save the model, which uses Pythons pickle module:
     print ">> Saving Model.."
     save_model(self.model_filename, model)
 def train(self):
     # Check if the given dataset exists:
     if not os.path.exists(self.dataset):
         print ">> [Error] No Dataset Found at '%s'." % self.dataset
         sys.exit(1)
     # Reads the images, labels and folder_names from a given dataset. Images
     # are resized to given size on the fly:
     print ">> Loading Dataset <-- " + self.dataset
     [images, labels, subject_names] = self.read_images(self.dataset, self.image_size)
     # Zip us a {label, name} dict from the given data:
     list_of_labels = list(xrange(max(labels) + 1))
     subject_dictionary = dict(zip(list_of_labels, subject_names))
     # Get the model we want to compute:
     model = self.get_model(image_size=self.image_size, subject_names=subject_dictionary)
     # Sometimes you want to know how good the model may perform on the data
     # given, the script allows you to perform a k-fold Cross Validation before
     # the Detection & Recognition part starts:
     if self.numfolds is not None:
         print ">> Validating Model With %s Folds.." % self.numfolds
         # We want to have some log output, so set up a new logging handler
         # and point it to stdout:
         handler = logging.StreamHandler(sys.stdout)
         formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
         handler.setFormatter(formatter)
         # Add a handler to facerec modules, so we see what's going on inside:
         logger = logging.getLogger("facerec")
         logger.addHandler(handler)
         logger.setLevel(logging.DEBUG)
         # Perform the validation & print results:
         crossval = KFoldCrossValidation(model, k=self.numfolds)
         crossval.validate(images, labels)
         crossval.print_results()
     # Compute the model:
     print ">> Computing Model.."
     model.compute(images, labels)
     # And save the model, which uses Pythons pickle module:
     print ">> Saving Model.."
     save_model(self.model_filename, model)