/** * @brief Convolve a first array of floats to a second one * @param samples_across The number of units across in the second array of floats (sampling grid resolution) * @param samples_down The number of units down in the second array of floats (sampling grid resolution) * @param patch_radius The radius of the patch within the first float array * @param floats_width Width of the image * @param floats_height Height of the image * @param floats_depth Depth of the image (mono=1, RGB=3) * @param layer0 First array of floats * @param layer1_units Number of units in the second float array * @param layer1 Second float array * @param feature_autocoder An autocoder containing learned features * @param use_dropouts non-zero if dropouts are to be used * @returns zero on success */ int features_conv_flt_to_flt(int samples_across, int samples_down, int patch_radius, int floats_width, int floats_height, int floats_depth, float layer0[], int layer1_units, float layer1[], ac * feature_autocoder, unsigned char use_dropouts) { int no_of_learned_features = feature_autocoder->NoOfHiddens; if (samples_across * samples_down * no_of_learned_features != layer1_units) { /* across*down doesn't equal the second layer units */ return -1; } if (feature_autocoder->NoOfInputs != patch_radius*patch_radius*4*floats_depth) { /* the patch size doesn't match the feature learner inputs */ return -2; } for (int fy = 0; fy < samples_down; fy++) { for (int fx = 0; fx < samples_across; fx++) { int index_layer1 = (fy * samples_across + fx) * no_of_learned_features; int tx=0, ty=0, bx=0, by=0; if (features_patch_coords(fx, fy, samples_across, samples_down, patch_radius, floats_width, floats_height, &tx, &ty, &bx, &by) != 0) { for (int f = 0; f < no_of_learned_features; f++) { layer1[index_layer1+f] = 0; } continue; } if (scan_floats_patch(layer0, floats_width, floats_depth, tx, ty, bx, by, feature_autocoder) != 0) { return -4; } autocoder_encode(feature_autocoder, &layer1[index_layer1], use_dropouts); } } return 0; }
/** * @brief Convolve an image with learned features and output * the results to the input layer of a neural net * @param samples_across The number of units across in the input layer * (sampling grid resolution) * @param samples_down The number of units down in the input layer * (sampling grid resolution) * @param patch_radius The radius of the patch within the image * @param img_width Width of the image * @param img_height Height of the image * @param img_depth Depth of the image (mono=1, RGB=3) * @param img Image buffer * @param layer0 Neural net * @param feature_autocoder An autocoder containing learned features * @param use_dropouts non-zero if dropouts are used * @returns zero on success */ int features_conv_img_to_neurons(int samples_across, int samples_down, int patch_radius, int img_width, int img_height, int img_depth, unsigned char img[], bp * net, ac * feature_autocoder, unsigned char use_dropouts) { int no_of_learned_features = feature_autocoder->NoOfHiddens; if (samples_across * samples_down * no_of_learned_features != net->NoOfInputs) { /* across*down doesn't equal the second layer units */ return -1; } if (feature_autocoder->NoOfInputs != patch_radius*patch_radius*4*img_depth) { /* the patch size doesn't match the feature learner inputs */ return -2; } for (int fy = 0; fy < samples_down; fy++) { for (int fx = 0; fx < samples_across; fx++) { int tx=0, ty=0, bx=0, by=0; if (features_patch_coords(fx, fy, samples_across, samples_down, patch_radius, img_width, img_height, &tx, &ty, &bx, &by) != 0) { continue; } if (scan_img_patch(img, img_width, img_depth, tx, ty, bx, by, feature_autocoder) != 0) { return -4; } int index_input_layer = (fy * samples_across + fx) * no_of_learned_features; autocoder_encode(feature_autocoder, feature_autocoder->hiddens, use_dropouts); for (int f = 0; f < no_of_learned_features; f++) { bp_set_input(net, index_input_layer+f, autocoder_get_hidden(feature_autocoder, f)); } } } return 0; }
/** * @brief Convolve an image with learned features and output * the results to an array of floats * @param samples_across The number of units across in the array of floats * (sampling grid resolution) * @param samples_down The number of units down in the array of floats * (sampling grid resolution) * @param patch_radius The radius of the patch within the float array * @param img_width Width of the image * @param img_height Height of the image * @param img_depth Depth of the image (mono=1, RGB=3) * @param img Image buffer * @param layer0_units Number of units in the float array * @param layer0 float array * @param feature_autocoder An autocoder containing learned features * @param use_dropouts Non-zero if dropouts are to be used * @returns zero on success */ int features_conv_img_to_flt(int samples_across, int samples_down, int patch_radius, int img_width, int img_height, int img_depth, unsigned char img[], int layer0_units, float layer0[], ac * feature_autocoder, unsigned char use_dropouts) { int no_of_learned_features = feature_autocoder->NoOfHiddens; if (samples_across * samples_down * no_of_learned_features != layer0_units) { /* across*down doesn't equal the second layer units */ return -1; } if (feature_autocoder->NoOfInputs != patch_radius*patch_radius*4*img_depth) { /* the patch size doesn't match the feature learner inputs */ return -2; } /* for each input image sample */ for (int fy = 0; fy < samples_down; fy++) { for (int fx = 0; fx < samples_across; fx++) { /* starting position in the first layer, where the depth is the number of encoded features */ int index_layer0 = (fy * samples_across + fx) * no_of_learned_features; /* coordinates of the patch in the input image */ int tx=0, ty=0, bx=0, by=0; if (features_patch_coords(fx, fy, samples_across, samples_down, patch_radius, img_width, img_height, &tx, &ty, &bx, &by) != 0) { for (int f = 0; f < no_of_learned_features; f++) { layer0[index_layer0+f] = 0; } continue; } /* scan the patch from the input image and get the feature responses */ if (scan_img_patch(img, img_width, img_depth, tx, ty, bx, by, feature_autocoder) != 0) { return -4; } /* set the first layer at this position to the feature responses */ autocoder_encode(feature_autocoder, &layer0[index_layer0], use_dropouts); } } return 0; }
/** * @brief Feed forward * @param autocoder Autocoder object */ void autocoder_feed_forward(ac * autocoder) { autocoder_encode(autocoder, autocoder->hiddens,1); autocoder_decode(autocoder, autocoder->outputs); }