class_answer<T, Tds1, Tds2>:: class_answer(uint nclasses, double target_factor, bool binary_target_, t_confidence conf, bool apply_tanh_, const char *name_, int force, int single, idxdim *kerd, double sigma_scale) : answer_module<T, Tds1, Tds2>(binary_target_ ? 1 : nclasses, name_), conf_type(conf), binary_target(binary_target_), resize_output(true), apply_tanh(apply_tanh_), tmp(1, 1, 1), force_class(force), single_output(single) { // create 1-of-n targets with target 1.0 for shown class, -1.0 for the rest targets = create_target_matrix<T>(nclasses, (T)1.0); // binary target if (binary_target) { if (nclasses != 2) eblerror("expecting 2 classes only when binary_target is on"); targets = idx<T>(2, 1, 1); // int neg_id = ds.get_class_id("bg"); // negative class // if (neg_id == 0) { // targets.set(-1.0, 0, 0); // negative: -1.0 // targets.set( 1.0, 1, 0); // positive: 1.0 // } else { targets.sset((T)1.0, 0); // positive: 1.0 targets.sset((T)-1.0, 1); // negative: -1.0 // } } // target factor idx_dotc(targets, target_factor, targets); print_targets(targets); // set min/max of target target_min = idx_min(targets); target_max = idx_max(targets); target_range = target_max - target_min; // set confidence parameters T max_dist; switch (conf_type) { case confidence_sqrdist: max_dist = target_max - target_min; conf_ratio = targets.dim(0) * max_dist * max_dist; // shift value to be subtracted before dividing by conf_ratio conf_shift = target_min; eblprint("Using sqrdist confidence formula with normalization ratio " << conf_ratio << " and shift value " << conf_shift << std::endl); break; case confidence_single: conf_ratio = target_max - target_min; // shift value to be subtracted before dividing by conf_ratio conf_shift = target_min; eblprint("Using single output confidence with normalization ratio " << conf_ratio << " and shift value " << conf_shift << std::endl); break; case confidence_max: if (force >= 0) { conf_ratio = target_max - target_min; conf_shift = -conf_ratio; conf_ratio *= 2; } else { conf_ratio = target_max - target_min; conf_shift = 0; // no shift needed, the difference min is 0. } eblprint("Using max confidence formula with normalization ratio " << conf_ratio << std::endl); break; default: eblerror("confidence type " << conf_type << " undefined"); } if (kerd && kerd->order() == 2) smoothing_kernel = create_mexican_hat2<T>(kerd->dim(0), kerd->dim(1), 1, sigma_scale); else smoothing_kernel = create_mexican_hat2<T>(9, 9, 1, sigma_scale); eblprint("smoothing kernel:" << std::endl); smoothing_kernel.print(); }
int display(list<string>::iterator &ifname, bool signd, bool load, list<string> *mats) { //cout << "displaying " << ifname->c_str() << endl; // conf mode if (conf) return display_net<T>(ifname, signd, load, mats); // mat mode if (is_matrix(ifname->c_str())) { idxdim d = get_matrix_dims(ifname->c_str()); if (interleaved) d.shift_dim(0, 2); if (save_individually || print || !(d.order() == 2 || (d.order() == 3 && (d.dim(2) == 1 || d.dim(2) == 3)))) { // this is probably not an image, just display info and print matrix string type; get_matrix_type(ifname->c_str(), type); idx<T> m = load_matrix<T>(ifname->c_str()); cout << "Matrix " << ifname->c_str() << " is of type " << type << " with dimensions " << d << " (min " << idx_min(m) << ", max " << idx_max(m) << ", mean " << idx_mean(m) << "):" << endl; m.print(); if (has_multiple_matrices(ifname->c_str())) { midx<T> ms = load_matrices<T>(ifname->c_str(), true); // saving sub-matrices if (save_individually) { cout << "Saving each sub-matrix of " << *ifname << " individually..." << endl; save_matrices_individually(ms, *ifname, true); } // printing sub-matrices cout << "This file contains " << m << " matrices: "; if (ms.order() == 1) { for (intg i = 0; i < ms.dim(0); ++i) { idx<T> tmp = ms.mget(i); cout << tmp.info() << " "; } } else if (ms.order() == 2) { for (intg i = 0; i < ms.dim(0); ++i) { for (intg j = 0; j < ms.dim(1); ++j) { idx<T> tmp = ms.mget(i, j); cout << tmp.info() << " "; } cout << endl; } } cout << endl; } else cout << "This is a single-matrix file." << endl; return 0; } } // image mode int loaded = 0; static idx<T> mat; uint h = 0, w = 0, rowh = 0, maxh = 0; list<string>::iterator fname = ifname; #ifdef __GUI__ disable_window_updates(); clear_window(); if (show_filename) { gui << at(h, w) << black_on_white() << ifname->c_str(); h += 16; } #endif maxh = h; for (uint i = 0; i < nh; ++i) { rowh = maxh; for (uint j = 0; j < nw; ++j) { if (fname == mats->end()) fname = mats->begin(); try { // if (load) mat = load_image<T>(*fname); if (print) cout << *fname << ": " << mat << endl << mat.str() << endl; // show only some channels if (chans >= 0) mat = mat.select(2, chans); loaded++; maxh = (std::max)(maxh, (uint) (rowh + mat.dim(0))); T min = 0, max = 0; #ifdef __GUI__ if (autorange || signd) { if (autorange) { min = idx_min(mat); max = idx_max(mat); } else if (signd) { T matmin = idx_min(mat); if ((double)matmin < 0) { min = -1; max = -1; } } draw_matrix(mat, rowh, w, zoom, zoom, min, max); } else draw_matrix(mat, rowh, w, zoom, zoom, (T) range[0], (T) range[1]); #endif w += mat.dim(1) + 1; } catch(string &err) { ERROR_MSG(err.c_str()); } fname++; if (fname == ifname) break ; } if (fname == ifname) break ; maxh++; w = 0; } #ifdef __GUI__ // info if (show_info) { set_text_colors(0, 0, 0, 255, 255, 255, 255, 200); gui << mat; gui << at(15, 0) << *fname; gui << at(29, 0) << "min: " << idx_min(mat) << " max: " << idx_max(mat); } // help if (show_help) { h = 0; w = 0; uint hstep = 14; set_text_colors(0, 0, 255, 255, 255, 255, 255, 200); gui << at(h, w) << "Controls:"; h += hstep; set_text_colors(0, 0, 0, 255, 255, 255, 255, 200); gui << at(h, w) << "Right/Space: next image"; h += hstep; gui << at(h, w) << "Left/Backspace: previous image"; h += hstep; gui << at(h, w) << "i: image info"; h += hstep; gui << at(h, w) << "a: auto-range (use min and max as range)"; h += hstep; gui << at(h, w) << "x/z: show more/less images on width axis"; h += hstep; gui << at(h, w) << "y/t: show more/less images on height axis"; h += hstep; gui << at(h, w) << "0,1,2: show channel 0, 1 or 2 only"; h += hstep; gui << at(h, w) << "9: show alls channels"; h += hstep; gui << at(h, w) << "h: help"; } // update title string title; title << "matshow: " << ebl::basename(ifname->c_str()); set_window_title(title.c_str()); enable_window_updates(); #endif return loaded; }
} }); // // confidence smoothing // idx<T> c = outx.select(0, 1); // uint hpad = (uint) (smoothing_kernel.dim(0) / 2); // uint wpad = (uint) (smoothing_kernel.dim(1) / 2); // idx<T> tmp(c.dim(0) + 2 * hpad, c.dim(1) + 2 * wpad); // idx_clear(tmp); // idx<T> tmp2 = tmp.narrow(0, c.dim(0), hpad); // tmp2 = tmp2.narrow(1, c.dim(1), wpad); // idx_copy(c, tmp2); // idx_2dconvol(tmp, smoothing_kernel, c); // idx_addc(c, (T)1); EDEBUG(this->name() << ": in " << in << " (in min " << idx_min(in) << " max " << idx_max(in) << ") out " << out << " (out min " << idx_min(out) << " max " << idx_max(out) << ")"); #ifdef __DEBUG__ idx<T> ldec = out.select(0, 0); EDEBUG(this->name() << ": class min " << idx_min(ldec) << " max " << idx_max(ldec)); idx<T> lconf = out.select(0, 1); EDEBUG(this->name() << ": confidence min " << idx_min(lconf) << " max " << idx_max(lconf)); #endif } template<typename T, typename Tds1, typename Tds2> void class_answer<T, Tds1, Tds2>:: fprop_ds2(labeled_datasource<T, Tds1, Tds2> &ds, state<T> &out) {