-
-
Notifications
You must be signed in to change notification settings - Fork 234
/
import_model.hpp
1185 lines (1049 loc) · 46.1 KB
/
import_model.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2016, Tobias Hermann.
// https://github.com/Dobiasd/frugally-deep
// Distributed under the MIT License.
// (See accompanying LICENSE file or at
// https://opensource.org/licenses/MIT)
#pragma once
#include "fdeep/base64.hpp"
#if defined(__GNUC__) || defined(__GNUG__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wctor-dtor-privacy"
#endif
#if defined _MSC_VER
#pragma warning(push)
#pragma warning(disable : 4706)
#pragma warning(disable : 4996)
#endif
#include <nlohmann/json.hpp>
#if defined _MSC_VER
#pragma warning(pop)
#endif
#if defined(__GNUC__) || defined(__GNUG__)
#pragma GCC diagnostic pop
#endif
#include "fdeep/common.hpp"
#include "fdeep/layers/add_layer.hpp"
#include "fdeep/layers/average_layer.hpp"
#include "fdeep/layers/average_pooling_2d_layer.hpp"
#include "fdeep/layers/batch_normalization_layer.hpp"
#include "fdeep/layers/bidirectional_layer.hpp"
#include "fdeep/layers/concatenate_layer.hpp"
#include "fdeep/layers/conv_2d_layer.hpp"
#include "fdeep/layers/cropping_2d_layer.hpp"
#include "fdeep/layers/dense_layer.hpp"
#include "fdeep/layers/depthwise_conv_2d_layer.hpp"
#include "fdeep/layers/elu_layer.hpp"
#include "fdeep/layers/flatten_layer.hpp"
#include "fdeep/layers/global_average_pooling_1d_layer.hpp"
#include "fdeep/layers/global_max_pooling_1d_layer.hpp"
#include "fdeep/layers/global_average_pooling_2d_layer.hpp"
#include "fdeep/layers/global_max_pooling_2d_layer.hpp"
#include "fdeep/layers/hard_sigmoid_layer.hpp"
#include "fdeep/layers/input_layer.hpp"
#include "fdeep/layers/layer.hpp"
#include "fdeep/layers/leaky_relu_layer.hpp"
#include "fdeep/layers/embedding_layer.hpp"
#include "fdeep/layers/lstm_layer.hpp"
#include "fdeep/layers/gru_layer.hpp"
#include "fdeep/layers/permute_layer.hpp"
#include "fdeep/layers/prelu_layer.hpp"
#include "fdeep/layers/linear_layer.hpp"
#include "fdeep/layers/max_pooling_2d_layer.hpp"
#include "fdeep/layers/maximum_layer.hpp"
#include "fdeep/layers/model_layer.hpp"
#include "fdeep/layers/multiply_layer.hpp"
#include "fdeep/layers/pooling_2d_layer.hpp"
#include "fdeep/layers/relu_layer.hpp"
#include "fdeep/layers/reshape_layer.hpp"
#include "fdeep/layers/separable_conv_2d_layer.hpp"
#include "fdeep/layers/selu_layer.hpp"
#include "fdeep/layers/sigmoid_layer.hpp"
#include "fdeep/layers/softmax_layer.hpp"
#include "fdeep/layers/softplus_layer.hpp"
#include "fdeep/layers/subtract_layer.hpp"
#include "fdeep/layers/tanh_layer.hpp"
#include "fdeep/layers/time_distributed_layer.hpp"
#include "fdeep/layers/upsampling_2d_layer.hpp"
#include "fdeep/layers/zero_padding_2d_layer.hpp"
#include "fdeep/shape5.hpp"
#include "fdeep/shape5_variable.hpp"
#include "fdeep/tensor5.hpp"
#include <fplus/fplus.hpp>
#include <algorithm>
#include <iostream>
#include <limits>
#include <memory>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
namespace fdeep { namespace internal
{
template<typename KeyT, typename ValueT>
ValueT json_object_get(const nlohmann::json& data, KeyT&& key, ValueT&& default_value)
{
auto&& it = data.find(key);
if (it != data.end())
return *it;
else
return default_value;
}
inline bool json_obj_has_member(const nlohmann::json& data,
const std::string& member_name)
{
return data.is_object() && data.find(member_name) != data.end();
}
inline fplus::maybe<std::size_t> create_maybe_size_t(const nlohmann::json& data)
{
if (data.is_null())
{
return fplus::nothing<std::size_t>();
}
const std::size_t result = data;
return fplus::just(result);
}
inline shape5_variable create_shape5_variable(const nlohmann::json& data)
{
assertion(data.is_array(), "shape5_variable needs to be an array");
assertion(data.size() > 0, "need at least one dimension");
if (data.size() == 1)
return shape5_variable(
fplus::nothing<std::size_t>(),
fplus::nothing<std::size_t>(),
fplus::nothing<std::size_t>(),
fplus::nothing<std::size_t>(),
create_maybe_size_t(data[0]));
if (data.size() == 2)
return shape5_variable(
fplus::nothing<std::size_t>(),
fplus::nothing<std::size_t>(),
fplus::nothing<std::size_t>(),
create_maybe_size_t(data[0]),
create_maybe_size_t(data[1]));
if (data.size() == 3)
return shape5_variable(
fplus::nothing<std::size_t>(),
fplus::nothing<std::size_t>(),
create_maybe_size_t(data[0]),
create_maybe_size_t(data[1]),
create_maybe_size_t(data[2]));
if (data.size() == 4)
return shape5_variable(
fplus::nothing<std::size_t>(),
create_maybe_size_t(data[0]),
create_maybe_size_t(data[1]),
create_maybe_size_t(data[2]),
create_maybe_size_t(data[3]));
if (data.size() == 5)
return shape5_variable(
create_maybe_size_t(data[0]),
create_maybe_size_t(data[1]),
create_maybe_size_t(data[2]),
create_maybe_size_t(data[3]),
create_maybe_size_t(data[4]));
if (data.size() == 6) // todo: is this needed?
return shape5_variable(
create_maybe_size_t(data[1]),
create_maybe_size_t(data[2]),
create_maybe_size_t(data[3]),
create_maybe_size_t(data[4]),
create_maybe_size_t(data[5]));
raise_error("shape5_variable needs 1, 2, 3, 4 or 5 dimensions");
return shape5_variable(
fplus::nothing<std::size_t>(),
fplus::nothing<std::size_t>(),
fplus::nothing<std::size_t>(),
fplus::nothing<std::size_t>(),
fplus::nothing<std::size_t>()); // Should never be called
}
inline shape5 create_shape5(const nlohmann::json& data)
{
assertion(data.is_array(), "shape5 needs to be an array");
assertion(data.size() > 0, "need at least one dimension");
if (data.size() == 1)
return shape5(1, 1, 1, 1, data[0]);
if (data.size() == 2)
return shape5(1, 1, 1, data[0], data[1]);
if (data.size() == 3)
return shape5(1, 1, data[0], data[1], data[2]);
if (data.size() == 4)
return shape5(1, data[0], data[1], data[2], data[3]);
if (data.size() == 5)
return shape5(data[0], data[1], data[2], data[3], data[4]);
raise_error("shape5 needs 1, 2, 3, 4 or 5 dimensions");
return shape5(0, 0, 0, 0, 0); // Should never be called
}
inline shape2 create_shape2(const nlohmann::json& data)
{
if (data.is_array())
{
assertion(data.size() == 1 || data.size() == 2,
"invalid number of dimensions in shape2");
if (data.size() == 1)
return shape2(1, data[0]);
else
return shape2(data[0], data[1]);
}
else
{
const std::size_t width = data;
return shape2(1, width);
}
}
inline std::size_t create_size_t(const nlohmann::json& int_data)
{
const int val = int_data;
assertion(val >= 0, "invalid size_t value");
return static_cast<std::size_t>(val);
}
inline int create_int(const nlohmann::json& int_data)
{
const int val = int_data;
return val;
}
inline float_vec decode_floats(const nlohmann::json& data)
{
assertion(data.is_array() || data.is_string(),
"invalid float array format");
if (data.is_array() && !data.empty() && data[0].is_number())
{
const float_vec result = data;
return result;
}
assertion(std::numeric_limits<float>::is_iec559,
"The floating-point format of your system is not supported.");
const auto res = Base64_decode(json_data_strs_char_prodiver(data, '='));
float_vec out;
assertion(res.size() % 4 == 0, "invalid float vector data");
out.reserve(res.size() / 4);
for (std::size_t i = 0; i < res.size(); i+=4)
{
float_type val = static_cast<float_type>(
*(reinterpret_cast<const float*>(&(res[i]))));
out.push_back(val);
}
return out;
}
inline tensor5 create_tensor5(const nlohmann::json& data)
{
const shape5 shape = create_shape5(data["shape"]);
return tensor5(shape, decode_floats(data["values"]));
}
template <typename T, typename F>
std::vector<T> create_vector(F f, const nlohmann::json& data)
{
if (data.is_array())
return fplus::transform_convert<std::vector<T>>(f, data);
else
return fplus::singleton_seq(f(data));
}
inline std::vector<shape5_variable> create_shape5s_variable(const nlohmann::json& data)
{
return create_vector<shape5_variable>(create_shape5_variable, data);
}
inline node_connection create_node_connection(const nlohmann::json& data)
{
assertion(data.is_array(), "invalid format for inbound node");
const std::string layer_id = data.front();
const auto node_idx = create_size_t(data[1]);
const auto tensor_idx = create_size_t(data[2]);
return node_connection(layer_id, node_idx, tensor_idx);
}
using get_param_f =
std::function<nlohmann::json(const std::string&, const std::string&)>;
using get_global_param_f = std::function<nlohmann::json(const std::string&)>;
layer_ptr create_layer(const get_param_f&, const get_global_param_f&,
const nlohmann::json&);
inline layer_ptr create_model_layer(const get_param_f& get_param,
const get_global_param_f& get_global_param, const nlohmann::json& data,
const std::string& name)
{
assertion(data["config"]["layers"].is_array(), "missing layers array");
const auto layers = create_vector<layer_ptr>(
fplus::bind_1st_and_2nd_of_3(create_layer, get_param, get_global_param),
data["config"]["layers"]);
assertion(data["config"]["input_layers"].is_array(), "no input layers");
const auto inputs = create_vector<node_connection>(
create_node_connection, data["config"]["input_layers"]);
const auto outputs = create_vector<node_connection>(
create_node_connection, data["config"]["output_layers"]);
return std::make_shared<model_layer>(name, layers, inputs, outputs);
}
inline void fill_with_zeros(float_vec& xs)
{
std::fill(std::begin(xs), std::end(xs), static_cast<float_type>(0));
}
inline padding create_padding(const std::string& padding_str)
{
return fplus::throw_on_nothing(error("no padding"),
fplus::choose<std::string, padding>({
{ std::string("valid"), padding::valid },
{ std::string("same"), padding::same },
}, padding_str));
}
inline layer_ptr create_conv_2d_layer(const get_param_f& get_param,
const get_global_param_f& get_global_param, const nlohmann::json& data,
const std::string& name)
{
const std::string padding_str = data["config"]["padding"];
const auto pad_type = create_padding(padding_str);
const shape2 strides = create_shape2(data["config"]["strides"]);
const shape2 dilation_rate = create_shape2(data["config"]["dilation_rate"]);
const auto filter_count = create_size_t(data["config"]["filters"]);
float_vec bias(filter_count, 0);
const bool use_bias = data["config"]["use_bias"];
if (use_bias)
bias = decode_floats(get_param(name, "bias"));
assertion(bias.size() == filter_count, "size of bias does not match");
const float_vec weights = decode_floats(get_param(name, "weights"));
const shape2 kernel_size = create_shape2(data["config"]["kernel_size"]);
assertion(weights.size() % kernel_size.area() == 0,
"invalid number of weights");
const std::size_t filter_depths =
weights.size() / (kernel_size.area() * filter_count);
const shape5 filter_shape(1, 1,
kernel_size.height_, kernel_size.width_, filter_depths);
const bool padding_valid_uses_offset_depth_1 =
get_global_param("conv2d_valid_offset_depth_1");
const bool padding_same_uses_offset_depth_1 =
get_global_param("conv2d_same_offset_depth_1");
const bool padding_valid_uses_offset_depth_2 =
get_global_param("conv2d_valid_offset_depth_2");
const bool padding_same_uses_offset_depth_2 =
get_global_param("conv2d_same_offset_depth_2");
return std::make_shared<conv_2d_layer>(name,
filter_shape, filter_count, strides, pad_type,
padding_valid_uses_offset_depth_1, padding_same_uses_offset_depth_1,
padding_valid_uses_offset_depth_2, padding_same_uses_offset_depth_2,
dilation_rate, weights, bias);
}
inline layer_ptr create_separable_conv_2D_layer(const get_param_f& get_param,
const get_global_param_f& get_global_param, const nlohmann::json& data,
const std::string& name)
{
const std::string padding_str = data["config"]["padding"];
const auto pad_type = create_padding(padding_str);
const shape2 strides = create_shape2(data["config"]["strides"]);
const shape2 dilation_rate = create_shape2(data["config"]["dilation_rate"]);
const auto filter_count = create_size_t(data["config"]["filters"]);
float_vec bias(filter_count, 0);
const bool use_bias = data["config"]["use_bias"];
if (use_bias)
bias = decode_floats(get_param(name, "bias"));
assertion(bias.size() == filter_count, "size of bias does not match");
const float_vec slice_weights = decode_floats(
get_param(name, "slice_weights"));
const float_vec stack_weights = decode_floats(
get_param(name, "stack_weights"));
const shape2 kernel_size = create_shape2(data["config"]["kernel_size"]);
assertion(slice_weights.size() % kernel_size.area() == 0,
"invalid number of weights");
assertion(stack_weights.size() % filter_count == 0,
"invalid number of weights");
const std::size_t input_depth = slice_weights.size() / kernel_size.area();
const std::size_t stack_output_depths_1 =
stack_weights.size() / input_depth;
assertion(stack_output_depths_1 == filter_count, "invalid weights sizes");
const shape5 filter_shape(1, 1, kernel_size.height_, kernel_size.width_, 1);
float_vec bias_0(input_depth, 0);
const bool padding_valid_uses_offset_depth_1 =
get_global_param("separable_conv2d_valid_offset_depth_1");
const bool padding_same_uses_offset_depth_1 =
get_global_param("separable_conv2d_same_offset_depth_1");
const bool padding_valid_uses_offset_depth_2 =
get_global_param("separable_conv2d_valid_offset_depth_2");
const bool padding_same_uses_offset_depth_2 =
get_global_param("separable_conv2d_same_offset_depth_2");
return std::make_shared<separable_conv_2d_layer>(name, input_depth,
filter_shape, filter_count, strides, pad_type,
padding_valid_uses_offset_depth_1, padding_same_uses_offset_depth_1,
padding_valid_uses_offset_depth_2, padding_same_uses_offset_depth_2,
dilation_rate, slice_weights, stack_weights, bias_0, bias);
}
inline layer_ptr create_depthwise_conv_2D_layer(const get_param_f& get_param,
const get_global_param_f& get_global_param, const nlohmann::json& data,
const std::string& name)
{
const std::string padding_str = data["config"]["padding"];
const auto pad_type = create_padding(padding_str);
const shape2 strides = create_shape2(data["config"]["strides"]);
const shape2 dilation_rate = create_shape2(data["config"]["dilation_rate"]);
const float_vec slice_weights = decode_floats(
get_param(name, "slice_weights"));
const shape2 kernel_size = create_shape2(data["config"]["kernel_size"]);
assertion(slice_weights.size() % kernel_size.area() == 0,
"invalid number of weights");
const std::size_t input_depth = slice_weights.size() / kernel_size.area();
const shape5 filter_shape(1, 1, kernel_size.height_, kernel_size.width_, 1);
const std::size_t filter_count = input_depth;
float_vec bias(filter_count, 0);
const bool use_bias = data["config"]["use_bias"];
if (use_bias)
bias = decode_floats(get_param(name, "bias"));
assertion(bias.size() == filter_count, "size of bias does not match");
const bool padding_valid_uses_offset_depth_1 =
get_global_param("separable_conv2d_valid_offset_depth_1");
const bool padding_same_uses_offset_depth_1 =
get_global_param("separable_conv2d_same_offset_depth_1");
const bool padding_valid_uses_offset_depth_2 =
get_global_param("separable_conv2d_valid_offset_depth_2");
const bool padding_same_uses_offset_depth_2 =
get_global_param("separable_conv2d_same_offset_depth_2");
return std::make_shared<depthwise_conv_2d_layer>(name, input_depth,
filter_shape, filter_count, strides, pad_type,
padding_valid_uses_offset_depth_1, padding_same_uses_offset_depth_1,
padding_valid_uses_offset_depth_2, padding_same_uses_offset_depth_2,
dilation_rate, slice_weights, bias);
}
inline layer_ptr create_input_layer(
const get_param_f&, const get_global_param_f&, const nlohmann::json& data,
const std::string& name)
{
assertion(data["inbound_nodes"].empty(),
"input layer is not allowed to have inbound nodes");
const auto input_shape = create_shape5_variable(data["config"]["batch_input_shape"]);
return std::make_shared<input_layer>(name, input_shape);
}
inline layer_ptr create_batch_normalization_layer(const get_param_f& get_param,
const get_global_param_f&, const nlohmann::json& data,
const std::string& name)
{
const float_vec moving_mean = decode_floats(get_param(name, "moving_mean"));
const float_vec moving_variance =
decode_floats(get_param(name, "moving_variance"));
const bool center = data["config"]["center"];
const bool scale = data["config"]["scale"];
const float_type epsilon = data["config"]["epsilon"];
float_vec gamma;
float_vec beta;
if (scale) gamma = decode_floats(get_param(name, "gamma"));
if (center) beta = decode_floats(get_param(name, "beta"));
return std::make_shared<batch_normalization_layer>(
name, moving_mean, moving_variance, beta, gamma, epsilon);
}
inline layer_ptr create_dropout_layer(
const get_param_f&, const get_global_param_f&, const nlohmann::json&,
const std::string& name)
{
// dropout rate equals zero in forward pass
return std::make_shared<linear_layer>(name);
}
inline layer_ptr create_max_pooling_2d_layer(
const get_param_f&, const get_global_param_f& get_global_param,
const nlohmann::json& data, const std::string& name)
{
const auto pool_size = create_shape2(data["config"]["pool_size"]);
const auto strides = create_shape2(data["config"]["strides"]);
const bool channels_first = json_object_get(data["config"], "data_format", std::string("channels_last")) == "channels_first";
const std::string padding_str = data["config"]["padding"];
const auto pad_type = create_padding(padding_str);
const bool padding_valid_uses_offset =
get_global_param("max_pooling_2d_valid_offset");
const bool padding_same_uses_offset =
get_global_param("max_pooling_2d_same_offset");
return std::make_shared<max_pooling_2d_layer>(name,
pool_size, strides, channels_first, pad_type,
padding_valid_uses_offset,
padding_same_uses_offset);
}
inline layer_ptr create_average_pooling_2d_layer(
const get_param_f&, const get_global_param_f& get_global_param,
const nlohmann::json& data, const std::string& name)
{
const auto pool_size = create_shape2(data["config"]["pool_size"]);
const auto strides = create_shape2(data["config"]["strides"]);
const bool channels_first = json_object_get(data["config"], "data_format", std::string("channels_last")) == "channels_first";
const std::string padding_str = data["config"]["padding"];
const auto pad_type = create_padding(padding_str);
const bool padding_valid_uses_offset =
get_global_param("average_pooling_2d_valid_offset");
const bool padding_same_uses_offset =
get_global_param("average_pooling_2d_same_offset");
return std::make_shared<average_pooling_2d_layer>(name,
pool_size, strides, channels_first, pad_type,
padding_valid_uses_offset,
padding_same_uses_offset);
}
inline layer_ptr create_global_max_pooling_1d_layer(
const get_param_f&, const get_global_param_f&, const nlohmann::json& data,
const std::string& name)
{
const bool channels_first = json_obj_has_member(data, "config")
&& json_object_get(data["config"], "data_format", std::string("channels_last")) == "channels_first";
return std::make_shared<global_max_pooling_1d_layer>(name, channels_first);
}
inline layer_ptr create_global_max_pooling_2d_layer(
const get_param_f&, const get_global_param_f&, const nlohmann::json& data,
const std::string& name)
{
const bool channels_first = json_obj_has_member(data, "config")
&& json_object_get(data["config"], "data_format", std::string("channels_last")) == "channels_first";
return std::make_shared<global_max_pooling_2d_layer>(name, channels_first);
}
inline layer_ptr create_global_average_pooling_1d_layer(
const get_param_f&, const get_global_param_f&, const nlohmann::json& data,
const std::string& name)
{
const bool channels_first = json_obj_has_member(data, "config")
&& json_object_get(data["config"], "data_format", std::string("channels_last")) == "channels_first";
return std::make_shared<global_average_pooling_1d_layer>(name, channels_first);
}
inline layer_ptr create_global_average_pooling_2d_layer(
const get_param_f&, const get_global_param_f&, const nlohmann::json& data,
const std::string& name)
{
const bool channels_first = json_obj_has_member(data, "config")
&& json_object_get(data["config"], "data_format", std::string("channels_last")) == "channels_first";
return std::make_shared<global_average_pooling_2d_layer>(name, channels_first);
}
inline layer_ptr create_upsampling_2d_layer(
const get_param_f&, const get_global_param_f&, const nlohmann::json& data,
const std::string& name)
{
const auto scale_factor = create_shape2(data["config"]["size"]);
return std::make_shared<upsampling_2d_layer>(name, scale_factor);
}
inline layer_ptr create_dense_layer(const get_param_f& get_param,
const get_global_param_f&, const nlohmann::json& data,
const std::string& name)
{
const float_vec weights = decode_floats(get_param(name, "weights"));
std::size_t units = data["config"]["units"];
float_vec bias(units, 0);
const bool use_bias = data["config"]["use_bias"];
if (use_bias)
bias = decode_floats(get_param(name, "bias"));
assertion(bias.size() == units, "size of bias does not match");
return std::make_shared<dense_layer>(
name, units, weights, bias);
}
inline layer_ptr create_concatenate_layer(
const get_param_f&, const get_global_param_f&, const nlohmann::json& data,
const std::string& name)
{
const std::int32_t keras_axis = data["config"]["axis"];
return std::make_shared<concatenate_layer>(name, keras_axis);
}
inline layer_ptr create_add_layer(
const get_param_f&, const get_global_param_f&, const nlohmann::json&,
const std::string& name)
{
return std::make_shared<add_layer>(name);
}
inline layer_ptr create_maximum_layer(
const get_param_f&, const get_global_param_f&, const nlohmann::json&,
const std::string& name)
{
return std::make_shared<maximum_layer>(name);
}
inline layer_ptr create_multiply_layer(
const get_param_f&, const get_global_param_f&, const nlohmann::json&,
const std::string& name)
{
return std::make_shared<multiply_layer>(name);
}
inline layer_ptr create_average_layer(
const get_param_f&, const get_global_param_f&, const nlohmann::json&,
const std::string& name)
{
return std::make_shared<average_layer>(name);
}
inline layer_ptr create_subtract_layer(
const get_param_f&, const get_global_param_f&, const nlohmann::json&,
const std::string& name)
{
return std::make_shared<subtract_layer>(name);
}
inline layer_ptr create_flatten_layer(
const get_param_f&, const get_global_param_f&, const nlohmann::json&,
const std::string& name)
{
return std::make_shared<flatten_layer>(name);
}
inline layer_ptr create_zero_padding_2d_layer(
const get_param_f&, const get_global_param_f&, const nlohmann::json& data,
const std::string& name)
{
const auto padding =
create_vector<std::vector<std::size_t>>(fplus::bind_1st_of_2(
create_vector<std::size_t, decltype(create_size_t)>, create_size_t),
data["config"]["padding"]);
assertion(padding.size() == 2 && padding[0].size() == padding[1].size(),
"invalid padding format");
if (padding[0].size() == 1)
{
const std::size_t top_pad = 0;
const std::size_t bottom_pad = 0;
const std::size_t left_pad = padding[0][0];
const std::size_t right_pad = padding[1][0];
return std::make_shared<zero_padding_2d_layer>(name,
top_pad, bottom_pad, left_pad, right_pad);
}
else
{
const std::size_t top_pad = padding[0][0];
const std::size_t bottom_pad = padding[0][1];
const std::size_t left_pad = padding[1][0];
const std::size_t right_pad = padding[1][1];
return std::make_shared<zero_padding_2d_layer>(name,
top_pad, bottom_pad, left_pad, right_pad);
}
}
inline layer_ptr create_cropping_2d_layer(
const get_param_f&, const get_global_param_f&, const nlohmann::json& data,
const std::string& name)
{
const auto cropping =
create_vector<std::vector<std::size_t>>(fplus::bind_1st_of_2(
create_vector<std::size_t, decltype(create_size_t)>, create_size_t),
data["config"]["cropping"]);
assertion(cropping.size() == 2 && cropping[0].size() == cropping[1].size(),
"invalid cropping format");
if (cropping[0].size() == 1)
{
const std::size_t top_crop = 0;
const std::size_t bottom_crop = 0;
const std::size_t left_crop = cropping[0][0];
const std::size_t right_crop = cropping[1][0];
return std::make_shared<cropping_2d_layer>(name,
top_crop, bottom_crop, left_crop, right_crop);
}
else
{
const std::size_t top_crop = cropping[0][0];
const std::size_t bottom_crop = cropping[0][1];
const std::size_t left_crop = cropping[1][0];
const std::size_t right_crop = cropping[1][1];
return std::make_shared<cropping_2d_layer>(name,
top_crop, bottom_crop, left_crop, right_crop);
}
}
inline layer_ptr create_reshape_layer(
const get_param_f&, const get_global_param_f&, const nlohmann::json& data,
const std::string& name)
{
const auto target_shape =
create_vector<int>(create_int, data["config"]["target_shape"]);
const auto filled_shape =
fplus::fill_left(1, 3, target_shape);
return std::make_shared<reshape_layer>(name, filled_shape);
}
inline activation_layer_ptr create_linear_layer(
const get_param_f&, const get_global_param_f&, const nlohmann::json&,
const std::string& name)
{
return std::make_shared<linear_layer>(name);
}
inline activation_layer_ptr create_softmax_layer(
const get_param_f&, const get_global_param_f&, const nlohmann::json&,
const std::string& name)
{
return std::make_shared<softmax_layer>(name);
}
inline activation_layer_ptr create_softplus_layer(
const get_param_f&, const get_global_param_f&, const nlohmann::json&,
const std::string& name)
{
return std::make_shared<softplus_layer>(name);
}
inline activation_layer_ptr create_tanh_layer(
const get_param_f&, const get_global_param_f&, const nlohmann::json&,
const std::string& name)
{
return std::make_shared<tanh_layer>(name);
}
inline activation_layer_ptr create_sigmoid_layer(
const get_param_f&, const get_global_param_f&, const nlohmann::json&,
const std::string& name)
{
return std::make_shared<sigmoid_layer>(name);
}
inline activation_layer_ptr create_hard_sigmoid_layer(
const get_param_f&, const get_global_param_f&, const nlohmann::json&,
const std::string& name)
{
return std::make_shared<hard_sigmoid_layer>(name);
}
inline activation_layer_ptr create_relu_layer(
const get_param_f&, const get_global_param_f&, const nlohmann::json& data,
const std::string& name)
{
float_type max_value = std::numeric_limits<float_type>::max();
if (json_obj_has_member(data, "config") &&
json_obj_has_member(data["config"], "max_value") &&
!data["config"]["max_value"].is_null())
{
max_value = data["config"]["max_value"];
}
return std::make_shared<relu_layer>(name, max_value);
}
inline activation_layer_ptr create_selu_layer(
const get_param_f&, const get_global_param_f&, const nlohmann::json&,
const std::string& name)
{
return std::make_shared<selu_layer>(name);
}
inline activation_layer_ptr create_leaky_relu_layer(
const get_param_f&, const get_global_param_f&, const nlohmann::json& data,
const std::string& name)
{
float_type alpha = 1.0f;
if (json_obj_has_member(data, "config") &&
json_obj_has_member(data["config"], "alpha"))
{
alpha = data["config"]["alpha"];
}
return std::make_shared<leaky_relu_layer>(name, alpha);
}
inline layer_ptr create_leaky_relu_layer_isolated(
const get_param_f& get_param, const get_global_param_f& get_global_param,
const nlohmann::json& data, const std::string& name)
{
return create_leaky_relu_layer(get_param, get_global_param, data, name);
}
inline layer_ptr create_prelu_layer(
const get_param_f& get_param, const get_global_param_f&,
const nlohmann::json& data, const std::string& name)
{
std::vector<std::size_t> shared_axes;
if (json_obj_has_member(data, "config") &&
json_obj_has_member(data["config"], "shared_axes") &&
!data["config"]["shared_axes"].empty())
{
shared_axes = create_vector<std::size_t>(create_size_t,
data["config"]["shared_axes"]);
}
const float_vec alpha = decode_floats(get_param(name, "alpha"));
return std::make_shared<prelu_layer>(name, alpha, shared_axes);
}
inline activation_layer_ptr create_elu_layer(
const get_param_f&, const get_global_param_f&, const nlohmann::json& data,
const std::string& name)
{
float_type alpha = 1.0f;
if (json_obj_has_member(data, "config") &&
json_obj_has_member(data["config"], "alpha"))
{
alpha = data["config"]["alpha"];
}
return std::make_shared<elu_layer>(name, alpha);
}
inline layer_ptr create_elu_layer_isolated(
const get_param_f& get_param, const get_global_param_f& get_global_param,
const nlohmann::json& data, const std::string& name)
{
return create_elu_layer(get_param, get_global_param, data, name);
}
inline layer_ptr create_relu_layer_isolated(
const get_param_f& get_param, const get_global_param_f& get_global_param,
const nlohmann::json& data, const std::string& name)
{
return create_relu_layer(get_param, get_global_param, data, name);
}
inline activation_layer_ptr create_activation_layer_type_name(
const get_param_f& get_param, const get_global_param_f& get_global_param,
const nlohmann::json& data,
const std::string& type, const std::string& name)
{
const std::unordered_map<std::string,
std::function<activation_layer_ptr(const get_param_f&,
const get_global_param_f&, const nlohmann::json&,
const std::string&)>>
creators = {
{"linear", create_linear_layer},
{"softmax", create_softmax_layer},
{"softplus", create_softplus_layer},
{"tanh", create_tanh_layer},
{"sigmoid", create_sigmoid_layer},
{"hard_sigmoid", create_hard_sigmoid_layer},
{"relu", create_relu_layer},
{"selu", create_selu_layer},
{"elu", create_elu_layer}
};
return fplus::throw_on_nothing(
error("unknown activation type: " + type),
fplus::get_from_map(creators, type))(
get_param, get_global_param, data, name);
}
inline layer_ptr create_activation_layer(
const get_param_f& get_param, const get_global_param_f& get_global_param,
const nlohmann::json& data, const std::string& name)
{
const std::string type = data["config"]["activation"];
return create_activation_layer_type_name(get_param, get_global_param,
data, type, name);
}
inline layer_ptr create_permute_layer(
const get_param_f&, const get_global_param_f&,
const nlohmann::json& data, const std::string& name)
{
const auto dims = create_vector<std::size_t>(create_size_t,
data["config"]["dims"]);
return std::make_shared<permute_layer>(name, dims);
}
inline node create_node(const nlohmann::json& inbound_nodes_data)
{
assertion(inbound_nodes_data.is_array(), "nodes need to be an array");
return node(create_vector<node_connection>(create_node_connection,
inbound_nodes_data));
}
inline nodes create_nodes(const nlohmann::json& data)
{
assertion(data["inbound_nodes"].is_array(), "no inbound nodes");
const std::vector<nlohmann::json> inbound_nodes_data =
data["inbound_nodes"];
return fplus::transform(create_node, inbound_nodes_data);
}
inline layer_ptr create_embedding_layer(const get_param_f &get_param,
const get_global_param_f &,
const nlohmann::json &data,
const std::string &name)
{
const std::size_t input_dim = data["config"]["input_dim"];
const std::size_t output_dim = data["config"]["output_dim"];
const float_vec weights = decode_floats(get_param(name, "weights"));
return std::make_shared<embedding_layer>(name, input_dim, output_dim, weights);
}
inline layer_ptr create_lstm_layer(const get_param_f &get_param,
const get_global_param_f &,
const nlohmann::json &data,
const std::string &name)
{
auto&& config = data["config"];
const std::size_t units = config["units"];
const std::string unit_activation = json_object_get(config, "activation", std::string("tanh"));
const std::string recurrent_activation = json_object_get(config,
"recurrent_activation",
data["class_name"] == "CuDNNLSTM"
? std::string("sigmoid")
: std::string("hard_sigmoid")
);
const bool use_bias = json_object_get(config, "use_bias", true);
float_vec bias;
if (use_bias)
bias = decode_floats(get_param(name, "bias"));
const float_vec weights = decode_floats(get_param(name, "weights"));
const float_vec recurrent_weights = decode_floats(get_param(name, "recurrent_weights"));
const bool return_sequences = config["return_sequences"];
return std::make_shared<lstm_layer>(name, units, unit_activation,
recurrent_activation, use_bias, return_sequences,
weights, recurrent_weights, bias);
}
inline layer_ptr create_gru_layer(const get_param_f &get_param,
const get_global_param_f &,
const nlohmann::json &data,
const std::string &name)
{
auto&& config = data["config"];
const std::size_t units = config["units"];
const std::string unit_activation = json_object_get(config, "activation", std::string("tanh"));
const std::string recurrent_activation = json_object_get(config,
"recurrent_activation",
data["class_name"] == "CuDNNGRU"
? std::string("sigmoid")
: std::string("hard_sigmoid")
);
const bool use_bias = json_object_get(config, "use_bias", true);
float_vec bias;
if (use_bias)
bias = decode_floats(get_param(name, "bias"));
const float_vec weights = decode_floats(get_param(name, "weights"));
const float_vec recurrent_weights = decode_floats(get_param(name, "recurrent_weights"));
bool reset_after = json_object_get(config,
"reset_after",
data["class_name"] == "CuDNNGRU"
);
const bool return_sequences = json_object_get(config, "return_sequences", false);
return std::make_shared<gru_layer>(name, units, unit_activation,
recurrent_activation, use_bias, reset_after, return_sequences,
weights, recurrent_weights, bias);
}
inline layer_ptr create_bidirectional_layer(const get_param_f& get_param,
const get_global_param_f&,
const nlohmann::json& data,
const std::string& name)
{
const std::string merge_mode = data["config"]["merge_mode"];
auto&& layer = data["config"]["layer"];
auto&& layer_config = layer["config"];
const std::string wrapped_layer_type = layer["class_name"];
const std::size_t units = layer_config["units"];
const std::string unit_activation = json_object_get(layer_config, "activation", std::string("tanh"));
const std::string recurrent_activation = json_object_get(layer_config,
"recurrent_activation",
wrapped_layer_type == "CuDNNGRU" || wrapped_layer_type == "CuDNNLSTM"
? std::string("sigmoid")
: std::string("hard_sigmoid")
);
const bool use_bias = json_object_get(layer_config, "use_bias", true);
float_vec forward_bias;
float_vec backward_bias;
if (use_bias)
{
forward_bias = decode_floats(get_param(name, "forward_bias"));
backward_bias = decode_floats(get_param(name, "backward_bias"));