Esempio n. 1
0
    def __init__(self,
                 shared_value,
                 final_value,
                 epochs_to_saturation):
        assert_is_instance(shared_value,
                           theano.tensor.sharedvar.SharedVariable)
        assert_is_subdtype(shared_value.dtype, numpy.floating)

        assert_equal(shared_value.ndim == 0, numpy.isscalar(final_value))

        if numpy.isscalar(final_value):
            assert_floating(final_value)
        else:
            assert_is_subdtype(final_value.dtype, numpy.floating)
            assert_equal(final_value.shape,
                         shared_value.get_value().shape)

        assert_integer(epochs_to_saturation)
        assert_greater(epochs_to_saturation, 0)

        self.shared_value = shared_value

        cast = numpy.cast[shared_value.dtype]
        self._final_value = cast(final_value)

        self._epochs_to_saturation = epochs_to_saturation

        self._num_epochs_seen = None
        self._initial_value = None
Esempio n. 2
0
    def __init__(self, dataset, batch_size):
        super(DatasetIterator, self).__init__()

        assert_is_instance(dataset, Dataset)
        assert_greater(len(dataset.tensors), 0)

        assert_greater(batch_size, 0)
        assert_integer(batch_size)

        self.dataset = dataset
        self.batch_size = batch_size
Esempio n. 3
0
 def _getitem_arg_to_slice(arg):
     '''
     Turns arg into a slice, if it isn't one.
     '''
     if numpy.isscalar(arg):
         assert_integer(arg)
         if arg == -1:
             return slice(arg, None)
         else:
             return slice(arg, arg + 1)
     else:
         assert_is_instance(arg, slice)
         return arg
    def get_expected_values(start_row, end_row=None):
        if end_row is None:
            assert_is_instance(start_row, numpy.ndarray)
            values = start_row
        else:
            assert_integer(start_row)
            assert_integer(end_row)
            values = numpy.arange(start_row, end_row)

        values = values % dtype_max
        values = values.reshape((values.shape[0], ) +
                                ((1, ) * (len(shape) - 1)))

        return numpy.tile(values, shape[1:])
def get_iters(yaml_dict, seed):
    assert_is_instance(yaml_dict, dict)
    assert_integer(seed)

    training_set, validation_set = _get_datasets(yaml_dict)

    hyperparams_dict = yaml_dict['hyperparams']

    training_iter = training_set.iterator(
        'random_vec3',
        hyperparams_dict['training_batch_size'],
        rng=RandomState(seed))

    validation_iter = validation_set.iterator(
        'random_vec3',
        hyperparams_dict['validation_batch_size'],
        rng=RandomState(seed))

    return training_iter, validation_iter
Esempio n. 6
0
    def __init__(self,
                 shared_value,
                 final_scale,
                 epochs_to_saturation):
        assert_is_instance(shared_value,
                           theano.tensor.sharedvar.SharedVariable)
        assert_floating(final_scale)
        assert_greater_equal(final_scale, 0.0)
        assert_less_equal(final_scale, 1.0)
        assert_integer(epochs_to_saturation)
        assert_greater(epochs_to_saturation, 0)

        self.shared_value = shared_value
        self._final_scale = final_scale
        self._epochs_to_saturation = epochs_to_saturation

        # initialized in on_start_training()
        self._initial_value = None
        self._num_epochs_seen = None
    def get_example_image(self, object_id, camera_dir):
        assert_integer(object_id)
        assert_equal(len(camera_dir.shape), 1)

        def normalize(vec):
            return vec / numpy.sqrt((vec * vec).sum())

        camera_dir = normalize(camera_dir)

        xy_projection_length = numpy.sqrt((camera_dir[:2] ** 2).sum())
        elevation = numpy.arctan2(camera_dir[2],  # z
                                  xy_projection_length)

        # atan2 args need not be on the unit circle; otherwise the following
        # would be wrong.
        azimuth = numpy.arctan2(camera_dir[1],  # y
                                camera_dir[0])  # x
        if azimuth < 0.0:
            azimuth += (2 * numpy.pi)

        assert_greater_equal(azimuth, 0.0)

        def rad_to_deg(rad):
            return rad / numpy.pi * 180.0

        elevation = rad_to_deg(elevation)
        azimuth = rad_to_deg(azimuth)

        def round_to_int(floating):
            return int(numpy.round(floating))

        elevation_label = round_to_int((elevation - 30.0) / 5.0)
        azimuth_label = round_to_int(azimuth / 20.0) * 2

        result = self.id_elev_azim_to_example.get((object_id,
                                                   elevation_label,
                                                   azimuth_label),
                                                  None)

        return result
Esempio n. 8
0
def __check_arg_types(input_node,
                      yaml_dict,
                      use_dropout,
                      numpy_rng,
                      theano_rng,
                      output_list,
                      output_size=None):
    assert_is_instance(input_node, Node)
    assert_is_instance(yaml_dict, dict)
    assert_is_instance(use_dropout, bool)
    assert_is_instance(numpy_rng, numpy.random.RandomState)

    if use_dropout:
        assert_is_instance(theano_rng, RandomStreams)
    else:
        assert_true(theano_rng is None or isinstance(theano_rng, RandomStreams))

    assert_is_instance(output_list, list)
    assert_all_is_instance(output_list, Node)

    if output_size is not None:
        assert_integer(output_size)
    def __init__(self,
                 model_id_node,
                 model_cam_dir_node,
                 # label_id_node,
                 # label_cam_dir_node,
                 iterator_input_nodes,
                 blank_id):
        assert_equal(model_id_node.output_format.axes, ('b', 'f'))
        assert_equal(model_id_node.output_symbol.dtype, floatX)

        assert_equal(model_cam_dir_node.output_format.axes, ('b', 'f'))
        assert_equal(model_cam_dir_node.output_format.shape[1], 3)
        assert_equal(model_cam_dir_node.output_symbol.dtype, floatX)

        label_id_node = iterator_input_nodes.id
        label_cam_dir_node = iterator_input_nodes.camera_dir

        assert_equal(label_id_node.output_format.axes, ('b',))
        assert_equal(str(label_id_node.output_format.dtype), 'int32')

        assert_equal(label_cam_dir_node.output_format.axes, ('b', 'f'))
        assert_equal(label_cam_dir_node.output_format.dtype, floatX)

        # assert_in('id_layers', model.__dict__)
        # assert_in('cam_dir_layers', model.__dict__)
        # assert_is_instance(model, IdAndCameraDirModel)
        assert_integer(blank_id)

        # def assert_formats_equal(fmt_0, fmt_1):
        #     assert_equal(fmt_0.axes, fmt_1.axes)
        #     assert_equal(fmt_0.shape, fmt_1.shape)
        #     assert_equal(fmt_0.dtype, fmt_1.dtype)

        # assert_formats_equal(model.input_node.output_format,
        #                      iterator_input_nodes[0].output_format)

        assert_equal(blank_id, model_id_node.output_format.shape[1] - 1)

        id_loss = CrossEntropy(model_id_node, label_id_node) #model.id_layers[-1], iterator_input_nodes.id)

        def to_floatX(arg):
            return numpy.cast[floatX](arg)

        # This is a bit different from the other scales, in that it
        # normalizes the initial expected loss to 1.0, whereas
        # the regression scales normalize the initial max loss to 1.0.
        # So id loss will initially be ~ 2x the regression losses, which
        # suits me fine.
        def get_id_scale(model_id_node):
            # id_node = model.id_layers[-1]
            assert_equal(model_id_node.output_format.axes, ('b', 'f'))
            num_ids = model_id_node.output_format.shape[1]
            assert_equal(num_ids, 7)

            return to_floatX((1.0 / -numpy.log(1.0 / num_ids)) ** 2.0)

        id_scale = get_id_scale(model_id_node)

        cam_dir_loss = UnitVectorSqNorm(model_cam_dir_node,  #model.cam_dir_layers[-1],
                                        label_cam_dir_node)  #iterator_input_nodes.camera_dir)
        cam_dir_scale = to_floatX((1.0 / 2.0) ** 2.0)

        id_loss_symbol = id_loss.output_symbol * id_scale
        cam_dir_loss_symbol = cam_dir_loss.output_symbol * cam_dir_scale

        id_label_symbol = label_id_node.output_symbol  #iterator_input_nodes.id.output_symbol

        # A vector along the 'b' axis. Has 0.0 for bkg images and 1.0 elsewhere
        self.non_bkg_mask = T.cast(T.neq(id_label_symbol, blank_id), floatX)

        total_loss_symbol = (id_loss_symbol +
                             self.non_bkg_mask * cam_dir_loss_symbol)
        # total_loss_symbol = T.switch(T.eq(id_label_symbol, blank_id),
        #                              id_loss_symbol,
        #                              id_loss_symbol + cam_dir_loss_symbol)

        # loss_symbol = (id_loss.output_symbol * to_floatX(id_scale) +
        #                cam_dir_loss.output_symbol * to_floatX(cam_dir_scale))
        assert_equal(str(total_loss_symbol.dtype), floatX)

        super(IdAndCameraDirLoss, self).__init__(
            input_nodes=iterator_input_nodes,  # not all of these are used
            output_symbol=total_loss_symbol,
            output_format=copy.deepcopy(id_loss.output_format))

        self.id_loss = id_loss
        self.cam_dir_loss = cam_dir_loss
    def __init__(self,
                 model,
                 foreground_dataset,
                 video_frame,
                 smoothing,
                 input_scale,
                 output_dir=None):
        '''
        model: simplelearn.io.SerializeableModel

        foreground_dataset: simplelearn.data.Dataset
          A Dataset with NORB-style label vectors. Used to compute a mapping
          from object ID to example images.

        video_frame: numpy.ndarray
          A frame from the input video stream. Used to initialize display pixel
          buffer's size and dtype.

        smoothing: float
          A float in the range [0.0, 1.0).
          The displayed pose and softmax z_t is computed as:

            z_t = k * z_(t-1) + (1.0 - k) * y(t)

          Where:
            t is the timestep
            z is the displayed value
            y is the model output value
            k is this smoothing argument

        input_scale: int
          The ratio of the input reticule to the model's actual input size.

        output_dir: string or None
          The directory to save frame images to. If omitted, no frame images
          will be saved.
        '''

        def get_input_size(model):
            image_format = model.input_node.output_format
            assert_equal(image_format.axes, ('b', '0', '1', 'c'))
            return image_format.shape[1:3]

        super(ReticuleDisplay, self).__init__(model,
                                              foreground_dataset,
                                              video_frame,
                                              get_input_size(model),
                                              output_dir)

        assert_is_instance(model, IdAndCameraDirModel)

        assert_integer(input_scale)
        assert_greater_equal(input_scale, 1)

        assert_greater_equal(smoothing, 0.0)
        assert_less(smoothing, 1.0)
        self.smoothing = smoothing
        self._displayed_camera_dir = None
        self._displayed_softmax = None

        input_format = model.input_node.output_format
        input_shape = numpy.asarray(
            [input_format.shape[input_format.axes.index(a)]
             for a in ('0', '1')])

        reticule_shape = input_shape * input_scale

        self.reticule_min_corner = (numpy.asarray(self.video_pixels.shape[:2])
                                    - reticule_shape) // 2

        self.reticule_max_corner = self.reticule_min_corner + reticule_shape

        self.input_pixels = self._get_example_window(0, 0)
        self.preprocessed_pixels = self._get_example_window(0, 1)
        model_output_nodes = [model.id_layers[-1],
                              model.cam_dir_layers[-1]]

        self.model_function = theano.function(
            [model.input_node.output_symbol],
            [n.output_symbol for n in model_output_nodes])

        # outputs bc01
        self.preprocess_function = theano.function(
            [model.input_node.output_symbol],
            _find_lcn_node(model).output_symbol)

        num_candidates = 3

        self.example_pixels_windows = [self._get_example_window(1, c)
                                       for c in range(num_candidates)]
        self.caption_pixels_windows = [self._get_example_window(2, c)
                                       for c in range(num_candidates)]
def make_instance_dataset(norb_name,
                          a_norb,
                          b_norb,
                          test_elevation_stride,
                          test_azimuth_stride,
                          objects=None):
    '''
    Creates instance recognition datasets from category recognition datasets.

    Merges two category recognition datasets (with disjoint object instances),
    and re-partitions them into instance recognition datasets (with disjoint
    camera views).

    The instance recognition dataset consists of a train and test set.

    All objects not selected by <objects> are ignored.

    Of the remaining images, he test set consists of all images that satisfy
    both the test_elevation_stride and test_azimuth_stride. The other
    images are used for the training set.

    If the category datset is in stereo, only the left stereo images are used.

    Parameters
    ----------
    norb_name: str
      The name of the category recognition dataset (e.g. 'big_norb'). Used to
      build the name of the instance recognition dataset. Alphanumeric
      characters and '_' only.

    a_norb: NORB Dataset
      One of the category recognition datasets (i.e. training set).

    b_norb: NORB Dataset
      The other category recognition dataset (i.e. testing set).

    test_elevation_stride: int
      Use every M'th elevation as a test image.

    test_azimuth_stride: int
      Use every N'th azimuth as a test image.

    objects: Sequence
      [(c0, i0), (c1, i1), ..., (cN, iN)]
      Each (cx, ix) pair specifies an object to include, by their
      class and instance labels cx and ix.

    Returns
    -------
    rval: str
      The path to the newly created .h5 file.
    '''

    assert_is_instance(norb_name, basestring)
    assert_all_true(c.isalnum() or c == '_' for c in norb_name)

    assert_is_instance(a_norb, Dataset)
    assert_is_instance(b_norb, Dataset)
    assert_all_equal(a_norb.names, b_norb.names)
    assert_all_equal(a_norb.formats, b_norb.formats)

    assert_integer(test_elevation_stride)
    assert_greater(test_elevation_stride, 0)

    assert_integer(test_azimuth_stride)
    assert_greater(test_azimuth_stride, 0)

    if objects is not None:
        assert_is_instance(objects, Sequence)
        for id_pair in objects:
            assert_equal(len(id_pair), 2)
            assert_all_integer(id_pair)
            assert_all_greater_equal(id_pair, 0)

    #
    # Done sanity-checking args
    #

    (category_index,
     instance_index,
     azimuth_index,
     elevation_index) = range(4)  # no need for lighting_index (= 4)

    def get_row_indices(labels,
                        test_elevation_stride,
                        test_azimuth_stride,
                        objects):
        '''
        Returns row indices or training and testing sets.
        '''

        logical_and = numpy.logical_and

        if objects is not None:
            objects = numpy.asarray(objects)
            obj_cols = (category_index, instance_index)
            object_mask = (labels[:, obj_cols] == objects).all(axis=1)
        else:
            object_mask = numpy.ones(labels.shape[0], dtype=bool)

        test_mask = logical_and(
            object_mask,
            (labels[:, elevation_index] % test_elevation_stride) == 0)

        test_mask = logical_and(
            test_mask,
            (labels[:, azimuth_index] % (test_azimuth_stride * 2)) == 0)

        train_mask = logical_and(object_mask, numpy.logical_not(test_mask))

        return tuple(numpy.nonzero(m)[0] for m in (train_mask, test_mask))

    a_train_indices, a_test_indices = get_row_indices(a_norb.tensors[1],
                                                      test_elevation_stride,
                                                      test_azimuth_stride,
                                                      objects)

    b_train_indices, b_test_indices = get_row_indices(b_norb.tensors[1],
                                                      test_elevation_stride,
                                                      test_azimuth_stride,
                                                      objects)

    def create_h5_filepath(norb_name,
                           test_elevation_stride,
                           test_azimuth_stride,
                           objects):
        '''
        Creates an hdf filepath based on the args.

        For which-norb: "big_norb", elevation_stride: 2, azimuth_stride: 1:
          <data_dir>/big_norb_instance/e2_a1_all.h5

        For same as above, but with objects: [[1, 2], [3, 7], [4, 1]]
          <data_dir>/big_norb_instance/e2_a1_1-2_3-7_4-1.h5
        '''
        output_dir = os.path.join(simplelearn.data.data_path,
                                  '{}_instance'.format(norb_name))

        if not os.path.isdir(output_dir):
            os.mkdir(output_dir)

        filename = "e{:02d}_a{:02d}_o_".format(test_elevation_stride,
                                               test_azimuth_stride)

        if objects is None:
            filename = filename + 'all'
        else:
            for id_pair in objects:
                filename = filename + "%d-%d" % tuple(id_pair)

        filename = filename + '.h5'

        return os.path.join(output_dir, filename)

    h5_path = create_h5_filepath(norb_name,
                                 test_elevation_stride,
                                 test_azimuth_stride,
                                 objects)

    def get_mono_format(input_image_format):
        axes = input_image_format.axes
        shape = input_image_format.shape

        if 's' in axes:
            s_index = axes.index('s')
            axes = list(axes)
            del axes[s_index]

            shape = list(shape)
            del shape[s_index]

        return DenseFormat(axes=axes,
                           shape=shape,
                           dtype=input_image_format.dtype)

    mono_image_format = get_mono_format(a_norb.formats[0])
    label_format = a_norb.formats[1]
    partition_names = ['train', 'test']
    partition_sizes = [len(a_train_indices) + len(b_train_indices),
                       len(a_test_indices) + len(b_test_indices)]
    train_indices = (a_train_indices, b_train_indices)
    test_indices = (a_test_indices, b_test_indices)

    # Creates a .h5 file and copies repartitioned data into it.
    with make_h5_file(h5_path,
                      partition_names,
                      partition_sizes,
                      a_norb.names,
                      [mono_image_format, label_format]) as h5_file:
        partitions = h5_file['partitions']

        for partition_name, (a_indices, b_indices) \
            in safe_izip(partition_names, [train_indices, test_indices]):

            partition = partitions[partition_name]

            a_images = a_norb.tensors[0]
            b_images = b_norb.tensors[0]
            out_images = partition['images']

            print("Copying {} partition.".format(partition_name))

            if 's' in a_norb.formats[0].axes:
                assert_equal(a_norb.formats[0].axes.index('s'), 1)

                out_images[:len(a_indices), ...] = a_images[a_indices, 0, ...]
                out_images[len(a_indices):, ...] = b_images[b_indices, 0, ...]
            else:
                out_images[:len(a_indices), ...] = a_images[a_indices, ...]
                out_images[len(a_indices):, ...] = b_images[b_indices, ...]

            a_labels = a_norb.tensors[1]
            b_labels = b_norb.tensors[1]
            out_labels = partition['labels']

            out_labels[:len(a_indices), :] = a_labels[a_indices, :]
            out_labels[len(a_indices):, :] = b_labels[b_indices, :]

            # Don't shuffle the data; that's the iterator's job.

    return h5_path
Esempio n. 12
0
    def make_batch(self, is_symbolic, batch_size=None, dtype=None, name=None):
        '''
        Makes a numeric or symbolic batch.

        May later split this into two functions: make_numeric_batch and
        make_symbolic_batch. Keep it like this for now. If implementations of
        _make_batch end up not sharing much logic between is_symbolic=True
        and is_symbolic=False, then maybe split it.

        Parameters
        ----------
        is_symbolic: bool
          if True, return a symbolic batch. Otherwise return a numeric batch.

        batch_size: int
          Number of examples in numeric batch. Omit if is_symbolic is True or
          if there is no batch axis.

        dtype: str/numpy.dtype, or NoneType
          A numpy/theano dtype. Required if self.dtype is None.
          Prohibited otherwise.

        name: str, or NoneType
          Name of the symbolic batch.
          Optional when is_symbolic is True.
          Omit if is_symbolic is False.
        '''

        assert_is_instance(is_symbolic, bool)

        # checks is_symbolic vs batch_size
        assert_equal(is_symbolic, batch_size is None,
                     "Must not supply a batch_size when is_symbolic is True."
                     if is_symbolic
                     else
                     "Must supply a batch_size when is_symbolic is False.")

        # Sanity-checks batch_size
        if not is_symbolic:
            assert_integer(batch_size)
            assert_greater_equal(batch_size, 0)

        # checks is_symbolic vs name
        if not is_symbolic and name is not None:
            raise ValueError("Can't supply a name when is_symbolic is False.")

        # Checks dtype vs self.dtype
        if dtype is None:
            if self.dtype is None:
                raise TypeError("Must supply a dtype argument, because this "
                                "%s has no dtype of its own." % type(self))
            else:
                dtype = self.dtype
        elif self.dtype is not None:
            raise TypeError("Can't supply a dtype argument because this %s's "
                            "dtype is not None (it's %s)." %
                            (type(self), self.dtype))
        elif dtype == 'floatX':
            dtype = theano.config.floatX
        else:
            dtype = numpy.dtype(dtype)

        result = self._make_batch(is_symbolic, batch_size, dtype, name)

        self.check(result)
        return result