array bagging(array &train_feats, array &test_feats, array &train_labels, int num_classes, int num_models, int sample_size) { int num_train = train_feats.dims(0); int num_test = test_feats.dims(0); array idx = floor(randu(sample_size, num_models) * num_train); array labels_all = constant(0, num_test, num_classes); array off = seq(num_test); for (int i = 0; i < num_models; i++) { array ii = idx(span, i); array train_feats_ii = lookup(train_feats, ii, 0); array train_labels_ii = train_labels(ii); // Get the predicted results array labels_ii = knn(train_feats_ii, test_feats, train_labels_ii); array lidx = labels_ii * num_test + off; labels_all(lidx) = labels_all(lidx) + 1; } array val, labels; max(val, labels, labels_all, 1); return labels; }
array train(const array &X, const array &Y, double alpha = 0.1, double lambda = 1.0, double maxerr = 0.01, int maxiter = 1000, bool verbose = false) { // Initialize parameters to 0 array Weights = constant(0, X.dims(1), Y.dims(1)); array J, dJ; float err = 0; for (int i = 0; i < maxiter; i++) { // Get the cost and gradient cost(J, dJ, Weights, X, Y, lambda); err = max<float>(abs(J)); if (err < maxerr) { printf("Iteration %4d Err: %.4f\n", i + 1, err); printf("Training converged\n"); return Weights; } if (verbose && ((i + 1) % 10 == 0)) { printf("Iteration %4d Err: %.4f\n", i + 1, err); } // Update the parameters via gradient descent Weights = Weights - alpha * dJ; } printf("Training stopped after %d iterations\n", maxiter); return Weights; }
array train(const array &X, const array &Y, double alpha = 0.1, double maxerr = 0.05, int maxiter = 1000, bool verbose = false) { // Initialize parameters to 0 array Weights = constant(0, X.dims(1), Y.dims(1)); for (int i = 0; i < maxiter; i++) { array P = predict(X, Weights); array err = Y - P; float mean_abs_err = mean<float>(abs(err)); if (mean_abs_err < maxerr) break; if (verbose && (i + 1) % 25 == 0) { printf("Iter: %d, Err: %.4f\n", i + 1, mean_abs_err); } Weights = Weights + alpha * matmulTN(X, err); } return Weights; }
void cost(array &J, array &dJ, const array &Weights, const array &X, const array &Y, double lambda = 1.0) { // Number of samples int m = Y.dims(0); // Make the lambda corresponding to Weights(0) == 0 array lambdat = constant(lambda, Weights.dims()); // No regularization for bias weights lambdat(0, span) = 0; // Get the prediction array H = predict(X, Weights); // Cost of misprediction array Jerr = -sum(Y * log(H) + (1 - Y) * log(1 - H)); // Regularization cost array Jreg = 0.5 * sum(lambdat * Weights * Weights); // Total cost J = (Jerr + Jreg) / m; // Find the gradient of cost array D = (H - Y); dJ = (matmulTN(X, D) + lambdat * Weights) / m; }
// Calculate all the distances from testing set to training set array distance(array train, array test) { const int feat_len = train.dims(1); const int num_train = train.dims(0); const int num_test = test.dims(0); array dist = constant(0, num_train, num_test); // Iterate over each attribute for (int ii = 0; ii < feat_len; ii++) { // Get a attribute vectors array train_i = train(span, ii); array test_i = test (span, ii).T(); // Tile the vectors to generate matrices array train_tiled = tile(train_i, 1, num_test); array test_tiled = tile( test_i, num_train, 1 ); // Add the distance for this attribute dist = dist + abs(train_tiled - test_tiled); dist.eval(); // Necessary to free up train_i, test_i } return dist; }
/** * a - foregound image * b - background image * mask - mask map * */ array alphaBlend(const array &a, const array &b, const array &mask) { array tiledMask; if (mask.dims(2) != a.dims(2)) tiledMask = tile(mask, 1, 1, a.dims(2)); return a*tiledMask + (1.0f - tiledMask)*b; }
array matmul(const array &a, const array &b, const array &c) { int tmp1 = a.dims(0) * b.dims(1); int tmp2 = b.dims(0) * c.dims(1); if (tmp1 < tmp2) { return matmul(matmul(a, b), c); } else { return matmul(a, matmul(b, c)); } }
array matmul(const array &a, const array &b, const array &c, const array &d) { int tmp1 = a.dims(0) * c.dims(1); int tmp2 = b.dims(0) * d.dims(1); if (tmp1 < tmp2) { return matmul(matmul(a, b, c), d); } else { return matmul(a, matmul(b, c, d)); } }
array border(const array& img, const int left, const int right, const int top, const int bottom, const float value = 0.0) { if((int)img.dims(0) < (top + bottom)) std::cerr << "input does not have enough rows" << std::endl; if((int)img.dims(1) < (left + right)) std::cerr << "input does not have enough columns" << std::endl; dim4 imgDims = img.dims(); array ret = constant(value, imgDims); ret(seq(top, imgDims[0]-bottom), seq(left, imgDims[1]-right), span, span) = img(seq(top, imgDims[0]-bottom), seq(left, imgDims[1]-right), span, span); return ret; }
array blur(const array& img, const array mask = gaussianKernel(3,3)) { array blurred = array(img.dims(), img.type()); for(int i = 0; i < (int)blurred.dims(2); i++) blurred(span, span, i) = convolve(img(span, span, i), mask); return blurred; }
void max(array &val, array &idx, const array &in, const int dim) { af_array out = 0; af_array loc = 0; AF_THROW(af_imax(&out, &loc, in.get(), getFNSD(dim, in.dims()))); val = array(out); idx = array(loc); }
array medianfilter(const array &in, int window_width, int window_height) { array ret_val(in.dims()); ret_val(span, span, 0) = medfilt(in(span, span, 0), window_width, window_height); ret_val(span, span, 1) = medfilt(in(span, span, 1), window_width, window_height); ret_val(span, span, 2) = medfilt(in(span, span, 2), window_width, window_height); return ret_val; }
/** * randomization - controls % of total number of pixels in the image * that will be effected by random noise * repeat - # of times the process is carried out on the previous steps output */ array pick(const array &in, int randomization, int repeat) { int w = in.dims(0); int h = in.dims(1); float f = randomization / 100.0f; int dim = (int)(f*w*h); array ret_val = in.copy(); for (int i = 0; i<repeat; ++i) { array idxs = (w*h) * randu(dim); array rnd = getRandomNeighbor(ret_val, 1, 1); array temp_src = moddims(rnd, w*h, 3); array temp_dst = moddims(ret_val, w*h, 3); temp_dst(idxs, span) = temp_src(idxs, span); ret_val = moddims(temp_dst, in.dims()); } return ret_val; }
/** * Retrieve a new image of same dimensions of the original image * where each original image's pixel is replaced by randomly picked * neighbor in the provided local neighborhood window */ array getRandomNeighbor(const array &in, int windW, int windH) { array rnd = 2.0f*randu(in.dims(0), in.dims(1)) - 1.0f; array sx = seq(in.dims(0)); array sy = seq(in.dims(1)); array vx = tile(sx, 1, in.dims(1)) + floor(rnd*windW); array vy = tile(sy.T(), in.dims(0), 1) + floor(rnd*windH); array vxx = clamp(vx, 0, in.dims(0)); array vyy = clamp(vy, 0, in.dims(1)); array in2 = moddims(in, vx.elements(), 3); return moddims(in2(vyy*in.dims(0) + vxx, span), in.dims()); }
/** * randomization - controls % of total number of pixels in the image * that will be effected by random noise * repeat - # of times the process is carried out on the previous steps output */ array hurl(const array &in, int randomization, int repeat) { int w = in.dims(0); int h = in.dims(1); float f = randomization / 100.0f; int dim = (int)(f*w*h); array ret_val = in.copy(); array temp = moddims(ret_val, w*h, 3); for (int i = 0; i<repeat; ++i) { array idxs = (w*h) * randu(dim); array rndR = 255.0f * randu(dim); array rndG = 255.0f * randu(dim); array rndB = 255.0f * randu(dim); temp(idxs, 0) = rndR; temp(idxs, 1) = rndG; temp(idxs, 2) = rndB; } ret_val = moddims(temp, in.dims()); return ret_val; }
array segment_volume(array A, int k) { array I1 = A(span, span, k); float mx = max<float>(I1); float mn = min<float>(I1); float u0 = 0.9 * mx; float s0 = (mx - mn) / 2; float u1 = 1.1 * mn; float s1 = (mx - mn) / 2; array L0 = gauss(I1, u0, s0); array L11 = gauss(I1, u1, s1); array L10; array L12; static array kernel = constant(1, 3, 3) / 9; static array L11_old; static array L12_old; if (k == 0) { L11 = convolve(L11, kernel); L10 = L11; } else { L10 = L11_old; L11 = L12_old; } if (k < A.dims(2) - 1) { L12 = gauss(A(span, span, k + 1), u1, s1); L12 = convolve(L12, kernel); } else { L12 = L11; } L11_old = L11; L12_old = L12; array L1 = (L10 + L11 + L12) / 3; array S = (L0 > L1); return S.as(A.type()); }
array anyTrue(const array &in, const int dim) { af_array out = 0; AF_THROW(af_any_true(&out, in.get(), getFNSD(dim, in.dims()))); return array(out); }
array var(const array& in, const array weights, dim_type dim) { af_array temp = 0; AF_THROW(af_var_weighted(&temp, in.get(), weights.get(), getFNSD(dim, in.dims()))); return array(temp); }
array var(const array& in, bool isbiased, dim_type dim) { af_array temp = 0; AF_THROW(af_var(&temp, in.get(), isbiased, getFNSD(dim, in.dims()))); return array(temp); }
array resize(const array &in, const float scale, const interpType method) { af_array out = 0; AF_THROW(af_resize(&out, in.get(), in.dims(0) * scale, in.dims(1) * scale, method)); return array(out); }
inline auto total_size(array const& a) { return calc_total_size(a.dims()); }
AFAPI array median(const array& in, const dim_t dim) { af_array temp = 0; AF_THROW(af_median(&temp, in.get(), getFNSD(dim, in.dims()))); return array(temp); }
array count(const array &in, const int dim) { af_array out = 0; AF_THROW(af_count(&out, in.get(), getFNSD(dim, in.dims()))); return array(out); }
void Window::surface(const array& S, const char* const title) { af::array xVals = range(S.dims(0)); af::array yVals = range(S.dims(1)); af_cell temp{_r, _c, title, AF_COLORMAP_DEFAULT}; AF_THROW(af_draw_surface(get(), xVals.get(), yVals.get(), S.get(), &temp)); }
/** * x,y - starting position of zoom * width, height - dimensions of the rectangle to where we have to zoom in * */ array digZoom(const array &in, int x, int y, int width, int height) { array cropped = in(seq(x, width - 1), seq(y, height - 1), span); return resize(cropped, (unsigned)in.dims(0), (unsigned)in.dims(1)); }