utils_14.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. def train(self, save=False, save_dir=None):
  2. train_img_list = glob.glob(self.path_train + "/*")
  3. print(train_img_list)
  4. train_features = []
  5. for img_file in train_img_list:
  6. img = io.imread(img_file)
  7. img = color.rgb2lab(img)
  8. img_features = self.extract_texton_feature(img, self.fb, self.nb_features)
  9. train_features.extend(img_features)
  10. train_features = np.array(train_features)
  11. print(train_features.shape)
  12. kmeans_cluster = MiniBatchKMeans(n_clusters=self.nb_clusters, verbose=1, max_iter=300)
  13. kmeans_cluster.fit(train_features)
  14. print(kmeans_cluster.cluster_centers_)
  15. print(kmeans_cluster.cluster_centers_.shape)
  16. self.cluster = kmeans_cluster
  17. # save kmeans result
  18. if save is True:
  19. with open(save_dir, 'wb') as f:
  20. pickle.dump(self.cluster, f)
  21. def save(self, event):
  22. if not self.filename:
  23. self.save_as(event)
  24. else:
  25. if self.writefile(self.filename):
  26. self.set_saved(True)
  27. try:
  28. self.editwin.store_file_breaks()
  29. except AttributeError: # may be a PyShell
  30. pass
  31. self.text.focus_set()
  32. return "break"