Пример #1
0
	std::vector<size_t> operator()(const D& data) const
	{
		std::vector<size_t> idx(data.size());
		std::iota(idx.begin(), idx.end(), 0);

		if (data.size() < 2)
			return idx;

		View view;
		using R = decltype(data[0]);

		size_t width = Var ?
			view.size(*std::max_element(data.begin(), data.end(),
				[view](R a, R b) { return view.size(a) < view.size(b); }
			)) :
			view.size(data[0]);

		for (size_t d = 0; d < width; ++d)
		{
			size_t digit = Flip ? width - d - 1 : d;
			sort(idx, [&view, &data, digit] (size_t i) {
				return size_t(view.at(expr<Var>(), data[i], digit));
			});
		}

		return idx;
	}
	double test_accuracy(const Eigen::VectorXd& parameters, View& test_set)
	{
		// Loop through test set and count how many predictions are correct
		size_t correct = 0;
		for (size_t element_idx = 0; element_idx < test_set.size(); element_idx++) {
			if (predict(parameters, test_set.first(element_idx)) == get_nonzero_idx(test_set.second(element_idx)))
				correct++;
		}

		return static_cast<double>(correct) / test_set.size();
	}
	double objective(View& view, const Eigen::VectorXd& parameters)
	{
		double total_error = 0, scaling_factor = 1. / view.size();

		// TODO: Let data object be used in range based for
		for (unsigned int i = 0; i < view.size(); i++) {
			forward_propagation(parameters, view.first(i), outputs());
			total_error += error_policy.error(outputs(), view.second(i), scaling_factor);
		}

		return total_error;
	}
Пример #4
0
	static
	mizuiro::image::indexed_iterator<
		typename View::dim,
		typename View::iterator
	>
	begin(
		View const &_view
	)
	{
		typedef
		mizuiro::image::indexed_iterator<
			typename View::dim,
			typename View::iterator
		>
		result_type;

		return
			result_type(
				_view.size(),
				mizuiro::image::dimension_null<
					typename
					View::dim
				>(),
				_view.begin()
			);
	}
Пример #5
0
	static
	mizuiro::image::indexed_iterator<
		typename View::dim,
		typename View::iterator
	>
	end(
		View const &_view
	)
	{
		typedef
		mizuiro::image::indexed_iterator<
			typename View::dim,
			typename View::iterator
		>
		result_type;

		return
			result_type(
				_view.size(),
				mizuiro::image::iterator_position(
					_view,
					_view.end()
				),
				_view.end()
			);
	}
Пример #6
0
void View::transform(
    Transformer& t, const Allocation& a, const Allocation&
) const {
    scene2view(a);
    const Allotment& ax = a.x_allotment();
    const Allotment& ay = a.y_allotment();
    csize(ax.begin(), ax.span(), ay.begin(), ay.span());
    float sx = ax.span()/XYView::width();
    float sy = ay.span()/XYView::height();
//	if (sx > sy) sx = sy;
    t.translate( -x(), -y());
    t.scale(sx, sx);
    View* v = (View*)this;
    v->x_pick_epsilon_ = pick_epsilon/sx;
    v->y_pick_epsilon_ = pick_epsilon/sx;
    t.translate((ax.begin() + ax.end())/2,(ay.begin() + ay.end())/2);
//printf("\nx origin=%g span=%g alignment=%g begin=%g end=%g\n", ax.origin(), ax.span(), ax.alignment(), ax.begin(), ax.end());
//printf("\ny origin=%g span=%g alignment=%g begin=%g end=%g\n", ay.origin(), ay.span(), ay.alignment(), ay.begin(), ay.end());
    Coord x1,y1;
    t.transform(x() - x_span_/2, y() - y_span_/2, x1, y1);
    if (!Math::equal(ax.begin(), x1, 1) || !Math::equal(ay.begin(), y1, 1)) {
        t.inverse_transform(ax.begin(), ay.begin(), x1, y1);
        v->x_span_ = 2*(x() - x1);
        v->y_span_ = 2*(y() - y1);
        v->size(x1,y1,x1+v->x_span_, y1+v->y_span_);
    }

}
Пример #7
0
 ExecStatus
 Watch<View>::post(Home home, View x0, View x1, unsigned int _x0Size) {
   if (x0.size() != _x0Size) {
     return ES_FAILED;
   } else if (x1.assigned()) {
     GECODE_ME_CHECK(x0.eq(home,x1.val()));
   } else {
     assert(!same(x0,x1));
     GECODE_ME_CHECK(x0.lq(home,x1.max()));
     GECODE_ME_CHECK(x1.lq(home,x0.max()));
     GECODE_ME_CHECK(x0.gq(home,x1.min()));
     GECODE_ME_CHECK(x1.gq(home,x0.min()));
     assert(x0.size() == x1.size());
     (void) new (home) Watch<View>(home,x0,x1,_x0Size);
   }
   return ES_OK;
 }
	void gradient(View& view, const Eigen::VectorXd& parameters, Eigen::VectorXd& gradient_vector)
	{
		// TODO: Check concept for InputIterator

		//double N = std::distance(first_input, last_input);
		double scaling_factor = 1. / view.size();
		gradient_vector.setZero();

		// DEBUG
		//std::cout << gradient_ << std::endl;

		for (unsigned int i = 0; i < view.size(); i++) {
			forward_propagation(parameters, view.first(i), outputs());
			back_propagation_error(parameters, view.first(i), outputs(), view.second(i), gradient_vector, scaling_factor);
		}

		// DEBUG
		//std::cout << gradient_ << std::endl;
	}
Пример #9
0
 void operator()() const {
     T* first=(T*)_v.row_begin(0);
     T* last=first+_v.size()*3;
     while(first!=last) {
         first[0]=at_c<0>(_p);
         first[1]=at_c<1>(_p);
         first[2]=at_c<2>(_p);
         first+=3;
     }
 }
Пример #10
0
 forceinline int
 ValSelRnd<View>::val(const Space&, View x, int) {
   unsigned int p = r(x.size());
   for (ViewRanges<View> i(x); i(); ++i) {
     if (i.width() > p)
       return i.min() + static_cast<int>(p);
     p -= i.width();
   }
   GECODE_NEVER;
   return 0;
 }
Пример #11
0
void lighten(View &a, const ConstView &b) {
  using namespace boost::gil;
  using dst_pixel = typename View::value_type;
  using channel_t = typename channel_type<dst_pixel>::type;
  auto max = channel_traits<channel_t>::max_value();

  for(size_t i = 0; i != a.size(); i++) {
    for(size_t chan = 0; chan != boost::gil::size<dst_pixel>::value; chan++)
      a[i][chan] = max - (max - a[i][chan]) * (max - b[i][chan]) / max;
  }
}
Пример #12
0
typename
View::iterator
move_iterator(
	View const &_view,
	typename View::dim const _pos
)
{
	typename View::iterator ret(
		_view.begin()
	);

	typedef typename View::dim dim;

	dim const size(
		_view.size()
	);

	typedef
	typename
	View::iterator::difference_type
	difference_type;

	difference_type
		add = 0,
		multiplier = 1;

	for(
		typename dim::size_type i = 0;
		i < dim::static_size;
		++i
	)
	{
		add +=
			static_cast<
				difference_type
			>(
				_pos[i]
			)
			* multiplier;

		multiplier *=
			static_cast<
				difference_type
			>(
				size[i]
			);
	}

	ret += add;

	return ret;
}
Пример #13
0
void render(View &dst, const cooked_image_data<ColorPixel> &src,
            double gamma = 1.0) {
  using namespace boost::gil;

  auto color = const_view(src.color);
  auto alpha = const_view(src.alpha);

  double inv_gamma = 1/gamma;
  for(size_t i = 0; i != dst.size(); i++) {
    assert(alpha[i] <= 1);
    auto a = std::pow(alpha[i], inv_gamma);
    auto &c = color[i];
    dst[i] = typename View::value_type(c[0] * a, c[1] * a, c[2] * a);
  }
}
Пример #14
0
static bool
check_elem_view (const View &view, const Elem &e)
{
  if (view.empty ())
    return false;
  if (view.size () != 1)
    return false;
  if (view.data () != &e)
    return false;
  if (&view[0] != &e)
    return false;
  if (view[0] != e)
    return false;
  return true;
}
Пример #15
0
void render_monochrome(View &dst, const cooked_image_data<ColorPixel> &src,
                       const ColorPixel &color, double gamma = 1.0) {
  using namespace boost::gil;
  using dst_pixel = typename View::value_type;
  using channel_t = typename channel_type<dst_pixel>::type;

  auto alpha = const_view(src.alpha);

  double inv_gamma = 1/gamma;
  for(size_t i = 0; i != dst.size(); i++) {
    assert(alpha[i] <= 1);
    auto a = std::pow(alpha[i], inv_gamma);
    for(size_t chan = 0; chan != boost::gil::size<ColorPixel>::value; chan++)
      dst[i][chan] = a * color[chan];
  }
}
 forceinline
 PosValuesChoice<ViewSel,View>::
 PosValuesChoice(const Brancher& b, const Pos& p,
               const typename ViewSel::Choice& viewc, View x)
   : PosChoice<ViewSel>(b,x.size(),p,viewc), n(0) {
   for (ViewRanges<View> r(x); r(); ++r)
     n++;
   pm = heap.alloc<PosMin>(n+1);
   unsigned int w=0;
   int i=0;
   for (ViewRanges<View> r(x); r(); ++r) {
     pm[i].min = r.min();
     pm[i].pos = w;
     w += r.width(); i++;
   }
   pm[i].pos = w;
 }
void view_test(View<T,Block0> view)
{
  dimension_type const View_dim = View<T,Block0>::dim;
  vsip::impl::Length<View_dim> v_extent = extent(view);
  Index<View_dim> my_index;
  T view_data,comp_data;

  comp_data = 0;
  for(index_type i=0;i<view.size();i++)
  {
    comp_data=increment(comp_data);
    view_data = get(view,my_index);
    
    test_assert(comp_data == view_data);
      
    my_index = next(v_extent,my_index);
  }
}	
Пример #18
0
static bool
check_container_view (const View &view, const Container &c)
{
  if (view.empty ())
    return false;
  if (view.size () != c.size ())
    return false;
  if (view.data () != c.data ())
    return false;
  for (size_t i = 0; i < c.size (); i++)
    {
      if (&view[i] != &c[i])
	return false;
      if (view[i] != c[i])
	return false;
    }
  return true;
}
Пример #19
0
void test_image_views(const View& img_view, const string& prefix) {
    check_image(img_view,prefix+"original.jpg");

    check_image(subimage_view(img_view, iround(img_view.dimensions()/4), iround(img_view.dimensions()/2)),prefix+"cropped.jpg");
    check_image(color_converted_view<gray8_pixel_t>(img_view),prefix+"gray8.jpg");
    check_image(color_converted_view<gray8_pixel_t>(img_view,my_color_converter()),prefix+"my_gray8.jpg");
    check_image(transposed_view(img_view),prefix+"transpose.jpg");
    check_image(rotated180_view(img_view),prefix+"rot180.jpg");
    check_image(rotated90cw_view(img_view),prefix+"90cw.jpg");
    check_image(rotated90ccw_view(img_view),prefix+"90ccw.jpg");
    check_image(flipped_up_down_view(img_view),prefix+"flipped_ud.jpg");
    check_image(flipped_left_right_view(img_view),prefix+"flipped_lr.jpg");
    check_image(subsampled_view(img_view,typename View::point_t(2,1)),prefix+"subsampled.jpg");
    check_image(nth_channel_view(img_view,0),prefix+"0th_channel.jpg");

    // some iterator math
    ptrdiff_t dst=(img_view.end()-(img_view.begin()+4500))+4500; ignore_unused_variable_warning(dst);
    ptrdiff_t dst2=img_view.end()-img_view.begin(); ignore_unused_variable_warning(dst2);
    ptrdiff_t sz=img_view.size(); ignore_unused_variable_warning(sz);
    io_error_if(sz!=dst2);
}
	void potential_and_gradient(const Eigen::VectorXd& parameters, const Eigen::VectorXd& hyperparameters, View& view, double& potential, Eigen::VectorXd& gradient)
	{
		// Loop over layers to calculate weights part of potential, and non-data part of gradient
		potential = 0;
		for (size_t layer_idx = 0; layer_idx < count_weights_layers(); layer_idx++) {
			//potential -= 0.5 * (hyperparameters[layer_idx * 2] * weights(parameters, layer_idx).squaredNorm() + hyperparameters[layer_idx * 2 + 1] * biases(parameters, layer_idx).squaredNorm());
			potential -= 0.5 * (hyperparameters[0] * weights(parameters, layer_idx).squaredNorm() + hyperparameters[1] * biases(parameters, layer_idx).squaredNorm());

			// TODO: Debugging here!
			//weights(gradient, layer_idx) = (weights(parameters, layer_idx).array() * -hyperparameters[layer_idx * 2]).matrix();
			//biases(gradient, layer_idx) = (biases(parameters, layer_idx).array() * -hyperparameters[layer_idx * 2 + 1]).matrix();
			weights(gradient, layer_idx) = (weights(parameters, layer_idx).array() * -hyperparameters[0]).matrix();
			biases(gradient, layer_idx) = (biases(parameters, layer_idx).array() * -hyperparameters[1]).matrix();
		}

		/*if (std::isnan(gradient[0])) {
			std::cout << gradient[0] << std::endl;
		}*/

		// Calculate output part of potential and gradient
		for (size_t data_idx = 0; data_idx < view.size(); data_idx++) {
			// Get the class label for this observation
			size_t class_idx = get_nonzero_idx(view.second(data_idx));

			// Calculate the output for this sample, and the gradient of the output with respect to the parameters
			// gradient_and_output(size_t variable_idx, const Eigen::VectorXd& inputs, const Eigen::VectorXd& parameters, Eigen::VectorXd& outputs, Eigen::VectorXd& gradient_vector)

			/*if (std::isnan(temp_gradient_[0])) {
				std::cout << temp_gradient_[0] << std::endl;
			}*/

			log_gradient_and_output(class_idx, view.first(data_idx), parameters, outputs(), temp_gradient_);

			//if (outputs()[class_idx] != 0.)
				gradient = gradient + temp_gradient_;

			
			/*if (std::isnan(temp_gradient_[0])) {
				std::cout << temp_gradient_[0] << std::endl;
			}

			if (std::isnan(gradient[0])) {
				std::cout << gradient[0] << std::endl;
			}*/

			//if ()
			// NOTE: Does it matter here when -E[theta] = -INF?
			//potential += log(outputs()[class_idx]);
			potential += outputs()[class_idx];
		}

		// DEBUG: Check that all entries are finite and not NaN
		/*if (!std::isfinite(potential)) {
			if (std::isnan(potential))
				std::cout << "NaN: Potential" << std::endl;
			else if (std::isinf(potential))
				std::cout << "INF: Potential" << std::endl;
		}
		for (size_t idx = 0; idx < static_cast<size_t>(gradient.size()); idx++) {
			if (!std::isfinite(gradient[idx])) {
				if (std::isnan(gradient[idx]))
					std::cout << "NaN: Gradient[" << idx << "]" << std::endl;
				else if (std::isinf(gradient[idx]))
					std::cout << "NaN: Gradient[" << idx << "]" << std::endl;
			}
		}*/
	}
Пример #21
0
void Player::publish() {
    options_.check();

    // Open all the bag files
    foreach(string const& filename, options_.bags) {
        ROS_INFO("Opening %s", filename.c_str());

        try
        {
            shared_ptr<Bag> bag(new Bag);
            bag->open(filename, bagmode::Read);
            bags_.push_back(bag);
        }
        catch (BagUnindexedException ex) {
            std::cerr << "Bag file " << filename << " is unindexed.  Run rosbag reindex." << std::endl;
            return;
        }
    }

    setupTerminal();

    if (!node_handle_.ok())
      return;

    if (!options_.quiet)
      puts("");
    
    // Publish all messages in the bags
    View full_view;
    foreach(shared_ptr<Bag> bag, bags_)
        full_view.addQuery(*bag);

    ros::Time initial_time = full_view.getBeginTime();

    initial_time += ros::Duration(options_.time);


    View view;
    TopicQuery topics(options_.topics);

    if (options_.topics.empty())
    {
      foreach(shared_ptr<Bag> bag, bags_)
        view.addQuery(*bag, initial_time, ros::TIME_MAX);
    } else {
      foreach(shared_ptr<Bag> bag, bags_)
        view.addQuery(*bag, topics, initial_time, ros::TIME_MAX);
    }

    if (view.size() == 0)
    {
      std::cerr << "No messages to play on specified topics.  Exiting." << std::endl;
      ros::shutdown();
      return;
    }

    // Advertise all of our messages
    foreach(const ConnectionInfo* c, view.getConnections())
    {
        ros::M_string::const_iterator header_iter = c->header->find("callerid");
        std::string callerid = (header_iter != c->header->end() ? header_iter->second : string(""));

        string callerid_topic = callerid + c->topic;

        map<string, ros::Publisher>::iterator pub_iter = publishers_.find(callerid_topic);
        if (pub_iter == publishers_.end()) {

            ros::AdvertiseOptions opts = createAdvertiseOptions(c, options_.queue_size);

            ros::Publisher pub = node_handle_.advertise(opts);
            publishers_.insert(publishers_.begin(), pair<string, ros::Publisher>(callerid_topic, pub));

            pub_iter = publishers_.find(callerid_topic);
        }
    }

    std::cout << "Waiting " << options_.advertise_sleep.toSec() << " seconds after advertising topics..." << std::flush;
    options_.advertise_sleep.sleep();
    std::cout << " done." << std::endl;

    std::cout << std::endl << "Hit space to toggle paused, or 's' to step." << std::endl;

    paused_ = options_.start_paused;

    while (true) {
        // Set up our time_translator and publishers

        time_translator_.setTimeScale(options_.time_scale);

        start_time_ = view.begin()->getTime();
        time_translator_.setRealStartTime(start_time_);
        bag_length_ = view.getEndTime() - view.getBeginTime();

        time_publisher_.setTime(start_time_);

        ros::WallTime now_wt = ros::WallTime::now();
        time_translator_.setTranslatedStartTime(ros::Time(now_wt.sec, now_wt.nsec));


        time_publisher_.setTimeScale(options_.time_scale);
        if (options_.bag_time)
            time_publisher_.setPublishFrequency(options_.bag_time_frequency);
        else
            time_publisher_.setPublishFrequency(-1.0);

        paused_time_ = now_wt;

        // Call do-publish for each message
        foreach(MessageInstance m, view) {
            if (!node_handle_.ok())
                break;

            doPublish(m);
        }

        if (options_.keep_alive)
            while (node_handle_.ok())
                doKeepAlive();

        if (!node_handle_.ok()) {
            std::cout << std::endl;
            break;
        }
        if (!options_.loop) {
            std::cout << std::endl << "Done." << std::endl;
            break;
        }
    }

    ros::shutdown();
}
Teuchos::ArrayView<typename View::const_value_type> toArray( View const &v )
{
    return Teuchos::ArrayView<typename View::const_value_type>( v.data(),
                                                                v.size() );
}
Пример #23
0
 ConstView(const View &v) : data_(v.data()),size_(v.size()){}
Пример #24
0
//a set of callbacks that provides a uniform way of trying to resize containers of contiguous bytes. 
//In the case of containers that allow for resizing (vector,string), the resize actually occurs
//In the case of containers that aren't resizeable, if the resize attempt is shrinkage, it will succeed, or else it will fail
inline bool default_resize(size_t size,byte_t byte,View &view) { return view.size() == size; }
Пример #25
0
typename
View::pitch_type
subview_pitch(
	View const &_view,
	typename View::bound_type const &_bound
)
{
	typedef typename View::pitch_type pitch_type;

	pitch_type ret{
		mizuiro::no_init{}
	};

	for(
		typename pitch_type::size_type index = 0;
		index < pitch_type::static_size;
		++index
	)
	{
		typename View::dim const edge_pos_end(
			mizuiro::image::detail::edge_pos_end(
				_bound,
				index
			)
		);

		ret[
			index
		] =
			_view.size()[
				index
			] > 1
			?
				mizuiro::image::detail::pitch_difference(
					mizuiro::image::move_iterator(
						_view,
						edge_pos_end
					),
					mizuiro::image::move_iterator(
						_view,
						mizuiro::image::detail::edge_pos_begin(
							_bound,
							index
						)
					)
				)
			:
				0
			;

		// if the end is one past the parent view's dim,
		// the pitch will be skipped by the iterator, so readd it
		if(
			edge_pos_end[
				index
			]
			==
			_view.size()[
				index
			]
		)
			ret[
				index
			]
				+=
				_view.pitch()[
					index
				];
	}

	return ret;
}
Пример #26
0
 forceinline
 ViewNode<View>::ViewNode(View x)
   : _size(x.size()), _view(x) {}