code: # Copyright (c) 2019 Sony Corporation. All Rights Reserved.
  #
  # Licensed under the Apache License, Version 2.0 (the "License");
  # you may not use this file except in compliance with the License.
  # You may obtain a copy of the License at
  #
  #     http://www.apache.org/licenses/LICENSE-2.0
  #
  # Unless required by applicable law or agreed to in writing, software
  # distributed under the License is distributed on an "AS IS" BASIS,
  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  # See the License for the specific language governing permissions and
  # limitations under the License.
  
  
  import os
  import argparse
  import nnabla as nn
  from nnabla.ext_utils import get_extension_context
  import numpy as np
  import json
  import glob
  import model
  from nnabla.utils.image_utils import imread, imsave, imresize
  import functools
  
  
  def saveimage(path, img):
      img = (img * 0.5) + 0.5
      imsave(path, img, channel_first=True)
  
  
  def save_results(i, args, used_config, img_trg, lbl_trg):
      target_attr_flags = lbl_trg.d[0].reshape(lbl_trg.d[0].size)
      target_domain = "_".join([attr for idx, attr in zip(
          target_attr_flags, used_config["selected_attrs"]) if bool(idx) is True])
      result_x = img_trg.d[0]
      filename = os.path.join(args.result_save_path,
                              "generated_{}_{}.png".format(i, target_domain))
      saveimage(filename, result_x)
      print("Saved {}.".format(filename))
      return
  
  
  def img_preprocess(img_paths, used_config):
  
      image_size = used_config["image_size"]
      images = list()
      image_names = list()
  
      for img_path in img_paths:
          # Load (and resize) image and labels.
          image = imread(img_path, num_channels=3, channel_first=True)
          if image.dtype == np.uint8:
              # Clip image's value from [0, 255] -> [0.0, 1.0]
              image = image / 255.0
          image = (image - 0.5) / 0.5  # Normalize
          image = imresize(image, (image_size, image_size),
                           interpolate='bilinear', channel_first=True)
          images.append(image)
          image_names.append(img_path.split("/")[-1])
  
      return np.asarray(images), np.asarray(image_names)
  
  
  def get_user_input(used_config):
      label = [0 for _ in range(used_config["c_dim"])]
      choice = used_config["selected_attrs"]
      for i, c in enumerate(choice):
          print("Use '{}'?".format(c))
          while 1:
              ans = input("type yes or no: ")
              if ans in ["yes", "no"]:
                  label[i] = 1 if ans == "yes" else 0
                  break
              else:
                  print("type 'yes' or 'no'.")
          #label[i] = int(bool(input("if yes, type 1, if not, just press enter:")))
      return np.array(label)
  
  
  def generate(args):
  
      # Load the config data used for training.
      with open(args.config, "r") as f:
          used_config = json.load(f)
  
      paramfile = args.pretrained_params
      img_paths = glob.glob(os.path.join(args.test_image_path, "*.png"))
      assert os.path.isfile(paramfile) and paramfile.split(
          "/")[-1] == used_config["pretrained_params"], "Corresponding parameter file not found."
  
      print("Learned attributes choice: {}".format(
          used_config["selected_attrs"]))
  
      # Prepare Generator and Discriminator based on user config.
      generator = functools.partial(
          model.generator, conv_dim=used_config["g_conv_dim"], c_dim=used_config["c_dim"], repeat_num=used_config["g_repeat_num"])
  
      x_real = nn.Variable(
          [1, 3, used_config["image_size"], used_config["image_size"]])
      label_trg = nn.Variable([1, used_config["c_dim"], 1, 1])
      with nn.parameter_scope("gen"):
          x_fake = generator(x_real, label_trg)
      x_fake.persistent = True
  
      nn.load_parameters(paramfile)  # load learned parameters.
  
      images, image_names = img_preprocess(img_paths, used_config)
  
      for i, (image, image_name) in enumerate(zip(images, image_names)):
          # Get real images.
          print("Source image: {}".format(image_name))
          x_real.d = image
  
          # Generate target domain based on user input.
          label_trg.d = np.reshape(get_user_input(used_config), label_trg.shape)
  
          # Execute image translation.
          x_fake.forward(clear_no_need_grad=True)
  
          save_results(i, args, used_config, x_fake, label_trg)
  
  
  def get_args():
  
      parser = argparse.ArgumentParser()
  
      # Generation
      parser.add_argument('--context', '-c', type=str,
                          default='cudnn', help="Extension path. ex) cpu, cudnn.")
      parser.add_argument("--device-id", "-d", type=str, default='0',
                          help='Device ID the training run on. This is only valid if you specify `-c cudnn`.')
      parser.add_argument("--type-config", "-t", type=str, default='float',
                          help='Type of computation. e.g. "float", "half".')
      parser.add_argument('--test-image-path', type=str,
                          help='a directory containing images used for image translation')
      parser.add_argument('--result-save-path', type=str,
                          default="tmp.results", help='a directory to save generated images')
      parser.add_argument('--pretrained-params', type=str, required=True,
                          help='path to the parameters used for generation.')
      parser.add_argument('--config', type=str, required=True,
                          help='path to the config file used for generation.')
  
      args = parser.parse_args()
      if not os.path.isdir(args.result_save_path):
          os.makedirs(args.result_save_path)
  
      return args
  
  
  def main():
      args = get_args()
      ctx = get_extension_context(
          args.context, device_id=args.device_id, type_config=args.type_config)
      nn.set_default_context(ctx)
      generate(args)
  
  
  if __name__ == '__main__':
      main()
   # Copyright (c) 2017 Sony Corporation. All Rights Reserved.
  #
  # Licensed under the Apache License, Version 2.0 (the "License");
  # you may not use this file except in compliance with the License.
  # You may obtain a copy of the License at
  #
  #     http://www.apache.org/licenses/LICENSE-2.0
  #
  # Unless required by applicable law or agreed to in writing, software
  # distributed under the License is distributed on an "AS IS" BASIS,
  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  # See the License for the specific language governing permissions and
  # limitations under the License.
  
  from __future__ import absolute_import
  from six.moves import range
  
  import numpy as np
  
  import nnabla as nn
  import nnabla.logger as logger
  import nnabla.functions as F
  import nnabla.parametric_functions as PF
  import nnabla.solvers as S
  import nnabla.utils.save as save
  
  from args import get_args
  from mnist_data import data_iterator_mnist
  from _checkpoint_nnp_util import save_checkpoint, load_checkpoint, save_nnp
  
  import os
  
  
  def generator(z, maxh=256, test=False, output_hidden=False):
      """
      Building generator network which takes (B, Z, 1, 1) inputs and generates
      (B, 1, 28, 28) outputs.
      """
      # Define shortcut functions
      def bn(x):
          # Batch normalization
          return PF.batch_normalization(x, batch_stat=not test)
  
      def upsample2(x, c):
          # Twice upsampling with deconvolution.
          return PF.deconvolution(x, c, kernel=(4, 4), pad=(1, 1), stride=(2, 2), with_bias=False)
  
      assert maxh / 4 > 0
      with nn.parameter_scope("gen"):
          # (Z, 1, 1) --> (256, 4, 4)
          with nn.parameter_scope("deconv1"):
              d1 = F.elu(bn(PF.deconvolution(z, maxh, (4, 4), with_bias=False)))
          # (256, 4, 4) --> (128, 8, 8)
          with nn.parameter_scope("deconv2"):
              d2 = F.elu(bn(upsample2(d1, maxh / 2)))
          # (128, 8, 8) --> (64, 16, 16)
          with nn.parameter_scope("deconv3"):
              d3 = F.elu(bn(upsample2(d2, maxh / 4)))
          # (64, 16, 16) --> (32, 28, 28)
          with nn.parameter_scope("deconv4"):
              # Convolution with kernel=4, pad=3 and stride=2 transforms a 28 x 28 map
              # to a 16 x 16 map. Deconvolution with those parameters behaves like an
              # inverse operation, i.e. maps 16 x 16 to 28 x 28.
              d4 = F.elu(bn(PF.deconvolution(
                  d3, maxh / 8, (4, 4), pad=(3, 3), stride=(2, 2), with_bias=False)))
          # (32, 28, 28) --> (1, 28, 28)
          with nn.parameter_scope("conv5"):
              x = F.tanh(PF.convolution(d4, 1, (3, 3), pad=(1, 1)))
      if output_hidden:
          return x, [d1, d2, d3, d4]
      return x
  
  
  def discriminator(x, maxh=256, test=False, output_hidden=False):
      """
      Building discriminator network which maps a (B, 1, 28, 28) input to
      a (B, 1).
      """
      # Define shortcut functions
      def bn(xx):
          # Batch normalization
          return PF.batch_normalization(xx, batch_stat=not test)
  
      def downsample2(xx, c):
          return PF.convolution(xx, c, (3, 3), pad=(1, 1), stride=(2, 2), with_bias=False)
  
      assert maxh / 8 > 0
      with nn.parameter_scope("dis"):
          # (1, 28, 28) --> (32, 16, 16)
          with nn.parameter_scope("conv1"):
              c1 = F.elu(bn(PF.convolution(x, maxh / 8,
                                           (3, 3), pad=(3, 3), stride=(2, 2), with_bias=False)))
          # (32, 16, 16) --> (64, 8, 8)
          with nn.parameter_scope("conv2"):
              c2 = F.elu(bn(downsample2(c1, maxh / 4)))
          # (64, 8, 8) --> (128, 4, 4)
          with nn.parameter_scope("conv3"):
              c3 = F.elu(bn(downsample2(c2, maxh / 2)))
          # (128, 4, 4) --> (256, 4, 4)
          with nn.parameter_scope("conv4"):
              c4 = bn(PF.convolution(c3, maxh, (3, 3),
                                     pad=(1, 1), with_bias=False))
          # (256, 4, 4) --> (1,)
          with nn.parameter_scope("fc1"):
              f = PF.affine(c4, 1)
      if output_hidden:
          return f, [c1, c2, c3, c4]
      return f
  
  
  def train(args):
      """
      Main script.
      """
  
      # Get context.
      from nnabla.ext_utils import get_extension_context
      logger.info("Running in %s" % args.context)
      ctx = get_extension_context(
          args.context, device_id=args.device_id, type_config=args.type_config)
      nn.set_default_context(ctx)
  
      # Create CNN network for both training and testing.
      # TRAIN
  
      # Fake path
      z = nn.Variable([args.batch_size, 100, 1, 1])
      fake = generator(z)
      fake.persistent = True  # Not to clear at backward
      pred_fake = discriminator(fake)
      loss_gen = F.mean(F.sigmoid_cross_entropy(
          pred_fake, F.constant(1, pred_fake.shape)))
      fake_dis = fake.get_unlinked_variable(need_grad=True)
      fake_dis.need_grad = True  # TODO: Workaround until v1.0.2
      pred_fake_dis = discriminator(fake_dis)
      loss_dis = F.mean(F.sigmoid_cross_entropy(
          pred_fake_dis, F.constant(0, pred_fake_dis.shape)))
  
      # Real path
      x = nn.Variable([args.batch_size, 1, 28, 28])
      pred_real = discriminator(x)
      loss_dis += F.mean(F.sigmoid_cross_entropy(pred_real,
                                                 F.constant(1, pred_real.shape)))
  
      # Create Solver.
      solver_gen = S.Adam(args.learning_rate, beta1=0.5)
      solver_dis = S.Adam(args.learning_rate, beta1=0.5)
      with nn.parameter_scope("gen"):
          solver_gen.set_parameters(nn.get_parameters())
      with nn.parameter_scope("dis"):
          solver_dis.set_parameters(nn.get_parameters())
      start_point = 0
  
      if args.checkpoint is not None:
          # load weights and solver state info from specified checkpoint files.
          start_point = load_checkpoint(
              args.checkpoint, {"gen": solver_gen, "dis": solver_dis})
  
      # Create monitor.
      import nnabla.monitor as M
      monitor = M.Monitor(args.monitor_path)
      monitor_loss_gen = M.MonitorSeries("Generator loss", monitor, interval=10)
      monitor_loss_dis = M.MonitorSeries(
          "Discriminator loss", monitor, interval=10)
      monitor_time = M.MonitorTimeElapsed("Time", monitor, interval=100)
      monitor_fake = M.MonitorImageTile(
          "Fake images", monitor, normalize_method=lambda x: (x + 1) / 2.)
  
      data = data_iterator_mnist(args.batch_size, True)
  
      # Save_nnp
      contents = save_nnp({'x': z}, {'y': fake}, args.batch_size)
      save.save(os.path.join(args.model_save_path,
                             'Generator_result_epoch0.nnp'), contents)
      contents = save_nnp({'x': x}, {'y': pred_real}, args.batch_size)
      save.save(os.path.join(args.model_save_path,
                             'Discriminator_result_epoch0.nnp'), contents)
  
      # Training loop.
      for i in range(start_point, args.max_iter):
          if i % args.model_save_interval == 0:
              save_checkpoint(args.model_save_path, i, {
                              "gen": solver_gen, "dis": solver_dis})
  
          # Training forward
          image, _ = data.next()
          x.d = image / 255. - 0.5  # [0, 255] to [-1, 1]
          z.d = np.random.randn(*z.shape)
  
          # Generator update.
          solver_gen.zero_grad()
          loss_gen.forward(clear_no_need_grad=True)
          loss_gen.backward(clear_buffer=True)
          solver_gen.weight_decay(args.weight_decay)
          solver_gen.update()
          monitor_fake.add(i, fake)
          monitor_loss_gen.add(i, loss_gen.d.copy())
  
          # Discriminator update.
          solver_dis.zero_grad()
          loss_dis.forward(clear_no_need_grad=True)
          loss_dis.backward(clear_buffer=True)
          solver_dis.weight_decay(args.weight_decay)
          solver_dis.update()
          monitor_loss_dis.add(i, loss_dis.d.copy())
          monitor_time.add(i)
  
      with nn.parameter_scope("gen"):
          nn.save_parameters(os.path.join(
              args.model_save_path, "generator_param_%06d.h5" % i))
      with nn.parameter_scope("dis"):
          nn.save_parameters(os.path.join(
              args.model_save_path, "discriminator_param_%06d.h5" % i))
  
      # Save_nnp
      contents = save_nnp({'x': z}, {'y': fake}, args.batch_size)
      save.save(os.path.join(args.model_save_path,
                             'Generator_result.nnp'), contents)
      contents = save_nnp({'x': x}, {'y': pred_real}, args.batch_size)
      save.save(os.path.join(args.model_save_path,
                             'Discriminator_result.nnp'), contents)
  
  
  if __name__ == '__main__':
      monitor_path = 'tmp.monitor.dcgan'
      args = get_args(monitor_path=monitor_path, model_save_path=monitor_path,
                      max_iter=20000, learning_rate=0.0002, batch_size=64,
                      weight_decay=0.0001)
      train(args)
   # Copyright (c) 2017-2020 Sony Corporation. All Rights Reserved.
  #
  # Licensed under the Apache License, Version 2.0 (the "License");
  # you may not use this file except in compliance with the License.
  # You may obtain a copy of the License at
  #
  #     http://www.apache.org/licenses/LICENSE-2.0
  #
  # Unless required by applicable law or agreed to in writing, software
  # distributed under the License is distributed on an "AS IS" BASIS,
  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  # See the License for the specific language governing permissions and
  # limitations under the License.
  
  from __future__ import division
  
  import glob
  import os
  import numpy as np
  from scipy import spatial
  import open3d as o3d
  
  import nnabla as nn
  import nnabla.logger as logger
  import nnabla.monitor as M
  import nnabla.functions as F
  import nnabla.parametric_functions as PF
  from nnabla.utils.data_iterator import data_iterator, data_iterator_simple
  from nnabla.utils.data_source import DataSource
  import utils
  
  from args import get_args
  
  
  class PointCloudDataSource(DataSource):
  
      def __init__(self, fpath, knn=50, test_rate=0.25, test=False, shuffle=True, rng=None):
          super(PointCloudDataSource, self).__init__(shuffle=shuffle)
          self.knn = knn
          self.test_rate = 0.25
          self.rng = np.random.RandomState(313) if rng is None else rng
  
          # Split info
          pcd = self._read_dataset(fpath)
          total_size = len(pcd.points)
          test_size = int(total_size * test_rate)
          indices = self.rng.permutation(total_size)
          test_indices = indices[:test_size]
          train_indices = indices[test_size:]
          indices = test_indices if test else train_indices
          self._size = test_size if test else total_size - test_size
          # Points
          points = np.asarray(pcd.points)
          self._points = self._preprocess(points)[indices]
          # Normals
          normals = np.asarray(pcd.normals)
          self._normals = normals[indices] if self.has_normals(
              normals) else normals
          # Radius
          self._radius = self._compute_radius(self._points, self.knn)
          self._variables = ('points', 'normals', 'radius')
          self.reset()
  
          logger.info("Data size = {}".format(self._size))
  
      def has_normals(self, normals):
          return False if normals.shape[0] == 0 else True
  
      def _preprocess(self, points):
          return utils.normalize(points)
  
      def _compute_radius(self, points, knn):
          if knn < 0:
              logger.info("Radius is not computed.")
              return
          # KDTree
          logger.info(
              "Constructing KDTree and querying {}-nearest neighbors".format(self.knn))
          tree = spatial.cKDTree(points, compact_nodes=True)
          # exclude self by adding 1
          dists, indices = tree.query(points, k=knn + 1)
          return dists[:, -1].reshape(dists.shape[0], 1)
  
      def _read_dataset(self, fpath):
          pcd = utils.read_pcd(fpath)
          return pcd
  
      def _get_data(self, position):
          points = self._points[self._indices[position]]
          normals = self._normals[self._indices[position]
                                  ] if self.has_normals(self._normals) else [0.0]
          radius = self._radius[self._indices[position]]
          return points, normals, radius
  
      @property
      def points(self):
          return self._points
  
      @property
      def normals(self):
          return self._normals
  
      @property
      def radius(self):
          return self._radius
  
      def reset(self):
          self._indices = self.rng.permutation(self._size) \
              if self._shuffle else np.arange(self._size)
          return super(PointCloudDataSource, self).reset()
  
  
  def point_cloud_data_source(fpath, knn=50, test_rate=0.25, test=False, shuffle=True, rng=None):
      return PointCloudDataSource(fpath, knn, test_rate, test, shuffle, rng)
  
  
  def point_cloud_data_iterator(data_source, batch_size):
      return data_iterator(data_source, batch_size=batch_size,
                           with_memory_cache=False,
                           with_file_cache=False)
  
  
  def create_pcd_dataset_from_mesh(fpath):
      mesh = utils.read_mesh(fpath)
      pcd = mesh.sample_points_poisson_disk(len(mesh.vertices),
                                            use_triangle_normal=True,
                                            seed=412)
      dpath = "/".join(fpath.split("/")[:-1])
      fname = fpath.split("/")[-1]
      fname = "{}_pcd.ply".format(os.path.splitext(fname)[0])
      fpath = os.path.join(dpath, fname)
      logger.info("PointCloud data ({}) is being created.".format(fpath))
      utils.write_pcd(fpath, pcd)
  
  
  def main():
      args = get_args()
      if args.command == "create":
          create_pcd_dataset_from_mesh(args.mesh_data_path)
  
  
  if __name__ == '__main__':
      main()
  
apis: numpy.array numpy.asarray
apis: numpy.random.randn
apis: numpy.random.RandomState numpy.arange scipy.spatial.cKDTree numpy.asarray