def predict(self, images: List[Url], simple: bool) -> Prediction: """ Predicts using the trained model """ filepath = self._names.prediction_cache(images) mkdirname(filepath) if os.path.exists(filepath): print("LOADING: %s" % filepath) with open(filepath, 'rb') as f: return dill.load(f) x = asarray(images) seq = Sequence1(x, x, self._res, self._batch) predictions: ndarray = self._kmodel.predict_generator(generator=seq, verbose=1) if simple: if predictions.ndim == 2 and predictions.shape[1] == 1: predictions = predictions.flatten() y = predictions.tolist() if simple: y = self._data.translate_predictions(y) results = Prediction(images, y) print("SAVING: %s" % filepath) with open(filepath, 'wb') as f: dill.dump(results, f) return results
def run_cached(self, images: List[Url], **kwargs) -> ClusterResults: filepath = self._cache_path(images, **kwargs) mkdirname(filepath) if os.path.exists(filepath): print('LOADING: %s' % filepath) with open(filepath, 'rb') as f: return dill.load(f) results = self.run(images, **kwargs) print('SAVING: %s' % filepath) with open(filepath, 'wb') as f: dill.dump(results, f) return results
def evaluate_test_set(self) -> Dict[str, float]: """ Evaluates the model using the test set """ filepath = self._names.test_set_evaluation_cache() if os.path.exists(filepath): print("LOADING: %s" % filepath) with open(filepath, 'rb') as f: return dill.load(f) x = self._data.test().x().load() y = self._data.test().y().load() results = self.evaluate(x, y) mkdirname(filepath) print("SAVING: %s" % filepath) with open(filepath, 'wb') as f: dill.dump(results, f) return results