daily pastebin goal
73%
SHARE
TWEET

CS 3630 Lab 1 Boilerplate Modification

abraha2d Jan 21st, 2019 67 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. def main():
  2.     img_clf = ImageClassifier()
  3.  
  4.     # Feature cache files.
  5.     # If you modify img_clf.extract_image_features(),
  6.     # change the filename or delete the cache files.
  7.     train_cache = 'train_data_v1.dat'
  8.     test_cache = 'test_data_v1.dat'
  9.  
  10.     # load images
  11.     print("Loading training images...")
  12.     (train_raw, train_labels) = img_clf.load_data_from_folder('./train/')
  13.     print("Loading testing images...")
  14.     (test_raw, test_labels) = img_clf.load_data_from_folder('./test/')
  15.  
  16.     # convert images into features
  17.     try:
  18.         with open(train_cache, 'rb') as train_data_file:
  19.             print("Loading cached training image features...")
  20.             train_data = pickle.load(train_data_file)
  21.     except FileNotFoundError:
  22.         print("Extracting training image features...")
  23.         train_data = img_clf.extract_image_features(train_raw)
  24.         with open('train_data_v1.dat', 'wb') as train_data_file:
  25.             print("Caching training image features...")
  26.             pickle.dump(train_data, train_data_file)
  27.  
  28.     try:
  29.         with open(test_cache, 'rb') as test_data_file:
  30.             print("Loading cached testing image features...")
  31.             test_data = pickle.load(test_data_file)
  32.     except FileNotFoundError:
  33.         print("Extracting testing image features...")
  34.         test_data = img_clf.extract_image_features(test_raw)
  35.         with open(test_cache, 'wb') as test_data_file:
  36.             print("Caching testing image features...")
  37.             pickle.dump(test_data, test_data_file)
  38.  
  39.     # train model and test on training data
  40.     print("Training classifier...")
  41.     img_clf.train_classifier(train_data, train_labels)
  42.     print("Predicting training image labels...")
  43.     predicted_labels = img_clf.predict_labels(train_data)
  44.     print("\nTraining results")
  45.     print("=============================")
  46.     print("Confusion Matrix:\n", metrics.confusion_matrix(train_labels, predicted_labels))
  47.     print("Accuracy: ", metrics.accuracy_score(train_labels, predicted_labels))
  48.     print("F1 score: ", metrics.f1_score(train_labels, predicted_labels, average='micro'))
  49.  
  50.     # test model
  51.     print("Predicting testing image labels...")
  52.     predicted_labels = img_clf.predict_labels(test_data)
  53.     print("\nTest results")
  54.     print("=============================")
  55.     print("Confusion Matrix:\n", metrics.confusion_matrix(test_labels, predicted_labels))
  56.     print("Accuracy: ", metrics.accuracy_score(test_labels, predicted_labels))
  57.     print("F1 score: ", metrics.f1_score(test_labels, predicted_labels, average='micro'))
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top