def main(): # tf.enable_eager_execution() inputs = keras.layers.Input(shape=(100, 100, 1)) x = SparseConv2D(1, [3, 3], 4, padding='same')([inputs, inputs]) x = SparseConv2D(1, [1, 1], 4, padding='same')([x, x]) # x = SparseConv2DTranspose(1, [2, 2], strides=[2, 2], padding='same')([x, x]) # noqa # x = keras.layers.MaxPool2D()(x) model = keras.Model(inputs, x) model.compile(optimizer=tf.train.AdadeltaOptimizer(0.1), loss='mse', metrics=['mae']) images = np.array([ img.open_as_float('../data/disks_100x100/images/1001.png'), img.open_as_float('../data/disks_100x100/images/1002.png'), img.open_as_float('../data/disks_100x100/images/1003.png'), img.open_as_float('../data/disks_100x100/images/1004.png') ]) images = images[:, :, :, np.newaxis] dataset = tf.data.Dataset.from_tensor_slices((images, images)) dataset = dataset.batch(4).repeat(-1) model.fit(dataset, epochs=5, steps_per_epoch=1000) x = images y = model.predict(images) vis.plot_image(*x, *y, columns=4, vmin=0., vmax=1.) vis.show('../figs/sparse_conv2d_example.pdf')
def vis_predict(self): """Run prediction on the test set and visualize the output.""" history_files = glob(join(self.model_dir, '*history.json')) hists = dict( (fname, utils.json_load(fname)) for fname in history_files) vis.plot_hist(hists) test_set = self._load_test() model = self._load_model() for image, dist_image, prediction in model.predict_visualization( test_set): fig, axes = vis.plot_image(image, image, dist_image, colorbar=True) axes[0, 1].plot(prediction[:, 1], prediction[:, 0], 'rx') axes[0, 2].plot(prediction[:, 1], prediction[:, 0], 'rx') logger.info(f"prediction:\n{prediction}") vis.show(join(self.figs_dir, 'prediction.pdf')) if not self.show: break
def vis_train(self): """Visualize the training set. (Mostly for debugging.)""" train_set = self._load_train() for batch in train_set.training_input(): for b in range(self.batch_size): image = batch[0][b] targets = batch[1] pose = targets[0][b] vis.plot_image(image, None, None, pose[:, :, 1], pose[:, :, 2], None, targets[1][b], targets[2][b], targets[3][b], columns=3) vis.show()
def vis_outputs(self): """Run prediction on the test set and visualize the output.""" test_set = self._load_test() model = self._load_model() for image, outputs in model.predict_outputs(test_set): pose_image = outputs[0] level_outputs = outputs[1:] columns = max(model.num_levels, model.pose_dim + 1) fig, axes = vis.plot_image( image, *[None for _ in range(columns - 1)], *[pose_image[:, :, i] for i in range(model.pose_dim + 1)], *[None for _ in range(columns - model.pose_dim - 1)], *level_outputs, *[None for _ in range(columns - model.num_levels)], colorbar=True, columns=columns) vis.show(join(self.figs_dir, 'model_outputs.pdf')) if not self.show: break
def vis_history(self): # todo: fix this for multiple models in the same model_dir vis.plot_hists_from_dir(self.model_root) vis.show(join(self.figs_dir, 'history.pdf'))