def _create_callbacks(): os.makedirs(ccp.output('weights'), exist_ok=True) return [ CSVLogger(ccp.output('keras_history.csv'), append=True), ModelCheckpoint( ccp.output("weights/weights.{epoch:02d}-{val_loss:.2f}.hdf5")), TensorBoard(log_dir=ccp.output('tensorboard')), DensityCheckpoint("data/shakecam/shakeshack-1504543773.jpg") ]
def handle(self, *args, **kwargs): now = int(time()) source = "https://cdn.shakeshack.com/camera.jpg?{}".format(now) destination = ccp.output("shakeshack-{}.jpg".format(now)) with _fetch_image(source) as data: if len(data) > 0: _upload(data, "shakeshack/{}".format(basename(destination)))
def _predict_images(self, image_keys, save=False): for image_key in image_keys: prediction = self.predictor.predict_line(ml.load_img(image_key)) truth = self._get_truth(image_key) if save: dest = ccp.output("predictions/{}".format(os.path.basename(image_key))) self.previewer.save(dest, image_key, prediction, truth) else: self.previewer.show(image_key, prediction, truth) if input("Continue? [y]/n: ") == 'n': break
class DensityCheckpoint(Callback): image_key = attr.ib() output_dir = attr.ib(default=ccp.output("prediction_checkpoint/{}".format( int(time.time())))) def on_train_begin(self, logs=None): self._save_prediction("begin") def on_epoch_end(self, epoch, logs=None): self._save_prediction("epoch_{:03}".format(epoch)) def _save_prediction(self, label): x = ml.image_to_batch(ml.load_img(self.image_key)) y = self.model.predict(x, batch_size=1) self._save_image(label, y) def _save_image(self, label, y): os.makedirs(self.output_dir, exist_ok=True) destination = "{}.jpg".format(os.path.join(self.output_dir, label)) previewer.save(destination, self.image_key, Prediction(y, y.sum()))
class LineCountCheckpoint(Callback): image_key = attr.ib() output_dir = attr.ib(default=ccp.output("prediction_checkpoint/{}".format( int(time.time())))) def on_train_begin(self, logs=None): self._save_prediction("begin") def on_epoch_end(self, epoch, logs=None): self._save_prediction("epoch_{:03}".format(epoch)) def _save_prediction(self, name): truth = dm.generate_truth_batch(self.image_key, True) inline = float(self.model.predict(truth, batch_size=1)) print("checkpoint linecount prediction: {}".format(inline)) self._save_result(name, truth, inline) def _save_result(self, name, y, inline): os.makedirs(self.output_dir, exist_ok=True) destination = "{}.jpg".format(os.path.join(self.output_dir, name)) previewer.save(destination, self.image_key, Prediction(y, line=inline))
def _write_images_to_tmp(self, anns): os.makedirs(ccp.output("previews/"), exist_ok=True) for key in anns.keys(): previewer.save(ccp.output("previews/{}.jpg".format(_index_from_key(key))), key)
def _create_callbacks(): os.makedirs(ccp.output('weights/linecount'), exist_ok=True) return [CSVLogger(ccp.output('keras_history.csv'), append=True), ModelCheckpoint(ccp.output("weights/linecount/weights.{epoch:03d}-{val_loss:.2f}.hdf5")), TensorBoard(log_dir=ccp.output('tensorboard')), callbacks.LineCountCheckpoint("data/shakecam/shakeshack-1500859164.jpg")]