91 printf(
"neural_layer_upsample_copy(): incorrect source layer type\n");
94 struct Layer *l = malloc(
sizeof(
struct Layer));
133 const int w = l->
width;
137 for (
int k = 0; k <
c; ++k) {
138 for (
int j = 0; j <
h * s; ++j) {
139 for (
int i = 0;
i < w * s; ++
i) {
140 const int in_index = k * w *
h + (j / s) * w +
i / s;
141 const int out_index = k * w *
h * s * s + j * w * s +
i;
142 l->
output[out_index] = input[in_index];
157 const double *input,
double *
delta)
164 const int w = l->
width;
168 for (
int k = 0; k <
c; ++k) {
169 for (
int j = 0; j <
h * s; ++j) {
170 for (
int i = 0;
i < w * s; ++
i) {
171 const int in_index = k * w *
h + (j / s) * w +
i / s;
172 const int out_index = k * w *
h * s * s + j * w * s +
i;
242 printf(
"%s\n", json_str);
255 const bool return_weights)
257 (void) return_weights;
258 cJSON *json = cJSON_CreateObject();
259 cJSON_AddStringToObject(json,
"type",
"upsample");
260 cJSON_AddNumberToObject(json,
"n_inputs", l->
n_inputs);
261 cJSON_AddNumberToObject(json,
"n_outputs", l->
n_outputs);
262 cJSON_AddNumberToObject(json,
"height", l->
height);
263 cJSON_AddNumberToObject(json,
"width", l->
width);
264 cJSON_AddNumberToObject(json,
"channels", l->
channels);
265 cJSON_AddNumberToObject(json,
"stride", l->
stride);
266 char *
string = cJSON_Print(json);
281 s += fwrite(&l->
height,
sizeof(
int), 1, fp);
282 s += fwrite(&l->
width,
sizeof(
int), 1, fp);
283 s += fwrite(&l->
channels,
sizeof(
int), 1, fp);
284 s += fwrite(&l->
out_w,
sizeof(
int), 1, fp);
285 s += fwrite(&l->
out_h,
sizeof(
int), 1, fp);
286 s += fwrite(&l->
out_c,
sizeof(
int), 1, fp);
287 s += fwrite(&l->
n_outputs,
sizeof(
int), 1, fp);
289 s += fwrite(&l->
n_inputs,
sizeof(
int), 1, fp);
290 s += fwrite(&l->
stride,
sizeof(
int), 1, fp);
304 s += fread(&l->
height,
sizeof(
int), 1, fp);
305 s += fread(&l->
width,
sizeof(
int), 1, fp);
306 s += fread(&l->
channels,
sizeof(
int), 1, fp);
307 s += fread(&l->
out_w,
sizeof(
int), 1, fp);
308 s += fread(&l->
out_h,
sizeof(
int), 1, fp);
309 s += fread(&l->
out_c,
sizeof(
int), 1, fp);
310 s += fread(&l->
n_outputs,
sizeof(
int), 1, fp);
312 s += fread(&l->
n_inputs,
sizeof(
int), 1, fp);
313 s += fread(&l->
stride,
sizeof(
int), 1, fp);
Neural network activation functions.
void layer_defaults(struct Layer *l)
Initialises a layer to default values.
void layer_guard_outputs(const struct Layer *l)
Check number of outputs is within bounds.
#define UPSAMPLE
Layer type upsample.
void neural_layer_upsample_print(const struct Layer *l, const bool print_weights)
Prints an upsampling layer.
void neural_layer_upsample_resize(struct Layer *l, const struct Layer *prev)
Resizes an upsampling layer if the previous layer has changed size.
double * neural_layer_upsample_output(const struct Layer *l)
Returns the output from an upsampling layer.
size_t neural_layer_upsample_save(const struct Layer *l, FILE *fp)
Writes an upsampling layer to a file.
void neural_layer_upsample_backward(const struct Layer *l, const struct Net *net, const double *input, double *delta)
Backward propagates an upsampling layer.
void neural_layer_upsample_free(const struct Layer *l)
Free memory used by an upsampling layer.
void neural_layer_upsample_forward(const struct Layer *l, const struct Net *net, const double *input)
Forward propagates an upsampling layer.
static void free_layer_arrays(const struct Layer *l)
Free memory used by an upsampling layer.
static void malloc_layer_arrays(struct Layer *l)
Allocate memory used by an upsampling layer.
void neural_layer_upsample_rand(struct Layer *l)
Dummy function since upsampling layers have no weights.
bool neural_layer_upsample_mutate(struct Layer *l)
Dummy function since upsampling layers cannot be mutated.
void neural_layer_upsample_update(const struct Layer *l)
Dummy function since upsampling layers have no weights.
void neural_layer_upsample_init(struct Layer *l, const struct ArgsLayer *args)
Initialises a 2D upsampling layer.
size_t neural_layer_upsample_load(struct Layer *l, FILE *fp)
Reads an upsampling layer from a file.
char * neural_layer_upsample_json_export(const struct Layer *l, const bool return_weights)
Returns a json formatted string representation of an upsample layer.
struct Layer * neural_layer_upsample_copy(const struct Layer *src)
Initialises and copies one upsampling layer from another.
An implementation of a 2D upsampling layer.
Parameters for initialising a neural network layer.
int channels
Pool, Conv, and Upsample.
int width
Pool, Conv, and Upsample.
int height
Pool, Conv, and Upsample.
int stride
Pool, Conv, and Upsample.
Neural network layer data structure.
double * output
Current neuron outputs (after activation function)
int stride
Pool, Conv, and Upsample.
int n_inputs
Number of layer inputs.
int channels
Pool, Conv, and Upsample.
int height
Pool, Conv, and Upsample.
struct LayerVtbl const * layer_vptr
Functions acting on layers.
int max_outputs
Maximum number of neurons in the layer.
int width
Pool, Conv, and Upsample.
int n_outputs
Number of layer outputs.
int out_w
Pool, Conv, and Upsample.
int type
Layer type: CONNECTED, DROPOUT, etc.
int out_c
Pool, Conv, and Upsample.
double * delta
Delta for updating weights.
int out_h
Pool, Conv, and Upsample.
Neural network data structure.
Utility functions for random number handling, etc.