or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

builder-pattern.mdconfiguration-import.mdindex.mdlayer-support.mdmodel-import.mdpretrained-models.mdseparate-files-import.md

separate-files-import.mddocs/

0

# Separate Files Import

1

2

Import models where configuration (JSON) and weights (HDF5) are stored in separate files. This is a common workflow in Keras where models are saved using `model.to_json()` and `model.save_weights()`.

3

4

## Functional API Separate Files Import

5

6

Import Keras Functional API models from separate JSON configuration and HDF5 weights files.

7

8

```java { .api }

9

public static ComputationGraph importKerasModelAndWeights(String modelJsonFilename, String weightsHdf5Filename)

10

throws IOException, InvalidKerasConfigurationException, UnsupportedKerasConfigurationException;

11

12

public static ComputationGraph importKerasModelAndWeights(String modelJsonFilename, String weightsHdf5Filename, boolean enforceTrainingConfig)

13

throws IOException, InvalidKerasConfigurationException, UnsupportedKerasConfigurationException;

14

```

15

16

### Parameters

17

18

- `modelJsonFilename` (String): Path to JSON file containing model configuration

19

- `weightsHdf5Filename` (String): Path to HDF5 file containing model weights

20

- `enforceTrainingConfig` (boolean): Whether to enforce training-related configurations

21

22

### Returns

23

24

- `ComputationGraph`: DeepLearning4J computation graph with imported configuration and weights

25

26

### Exceptions

27

28

- `IOException`: File I/O errors when reading JSON or HDF5 files

29

- `InvalidKerasConfigurationException`: Malformed or invalid Keras model configuration

30

- `UnsupportedKerasConfigurationException`: Keras features not supported by DeepLearning4J

31

32

### Usage Examples

33

34

```java

35

import org.deeplearning4j.nn.modelimport.keras.KerasModelImport;

36

import org.deeplearning4j.nn.graph.ComputationGraph;

37

38

// Import from separate files with default enforcement

39

ComputationGraph model = KerasModelImport.importKerasModelAndWeights(

40

"model_architecture.json",

41

"model_weights.h5"

42

);

43

44

// Import with relaxed training config enforcement

45

ComputationGraph model = KerasModelImport.importKerasModelAndWeights(

46

"model_architecture.json",

47

"model_weights.h5",

48

false

49

);

50

51

// Use the imported model

52

INDArray input = Nd4j.randn(1, 224, 224, 3);

53

INDArray output = model.outputSingle(input);

54

```

55

56

## Sequential Separate Files Import

57

58

Import Keras Sequential models from separate JSON configuration and HDF5 weights files.

59

60

```java { .api }

61

public static MultiLayerNetwork importKerasSequentialModelAndWeights(String modelJsonFilename, String weightsHdf5Filename)

62

throws IOException, InvalidKerasConfigurationException, UnsupportedKerasConfigurationException;

63

64

public static MultiLayerNetwork importKerasSequentialModelAndWeights(String modelJsonFilename, String weightsHdf5Filename, boolean enforceTrainingConfig)

65

throws IOException, InvalidKerasConfigurationException, UnsupportedKerasConfigurationException;

66

```

67

68

### Parameters

69

70

- `modelJsonFilename` (String): Path to JSON file containing Sequential model configuration

71

- `weightsHdf5Filename` (String): Path to HDF5 file containing model weights

72

- `enforceTrainingConfig` (boolean): Whether to enforce training-related configurations

73

74

### Returns

75

76

- `MultiLayerNetwork`: DeepLearning4J multi-layer network with imported configuration and weights

77

78

### Exceptions

79

80

- `IOException`: File I/O errors when reading JSON or HDF5 files

81

- `InvalidKerasConfigurationException`: Malformed or invalid Keras model configuration

82

- `UnsupportedKerasConfigurationException`: Keras features not supported by DeepLearning4J

83

84

### Usage Examples

85

86

```java

87

import org.deeplearning4j.nn.modelimport.keras.KerasModelImport;

88

import org.deeplearning4j.nn.multilayer.MultiLayerNetwork;

89

90

// Import Sequential model from separate files

91

MultiLayerNetwork model = KerasModelImport.importKerasSequentialModelAndWeights(

92

"sequential_architecture.json",

93

"sequential_weights.h5"

94

);

95

96

// Use the imported model

97

INDArray input = Nd4j.randn(1, 784); // Example for MNIST

98

INDArray output = model.output(input);

99

```

100

101

## Creating Separate Files in Keras

102

103

This workflow corresponds to the following Python/Keras code:

104

105

### Functional API Model

106

107

```python

108

import keras

109

import json

110

111

# Create or load your model

112

model = keras.models.Model(inputs=inputs, outputs=outputs)

113

114

# Save architecture to JSON

115

model_json = model.to_json()

116

with open('model_architecture.json', 'w') as json_file:

117

json_file.write(model_json)

118

119

# Save weights to HDF5

120

model.save_weights('model_weights.h5')

121

```

122

123

### Sequential Model

124

125

```python

126

import keras

127

import json

128

129

# Create or load your Sequential model

130

model = keras.models.Sequential([...])

131

132

# Save architecture to JSON

133

model_json = model.to_json()

134

with open('sequential_architecture.json', 'w') as json_file:

135

json_file.write(model_json)

136

137

# Save weights to HDF5

138

model.save_weights('sequential_weights.h5')

139

```

140

141

## Advantages of Separate Files

142

143

### Version Control Friendly

144

- JSON configuration files are text-based and can be version controlled effectively

145

- Binary weight files can be stored separately using Git LFS or other solutions

146

- Architecture changes are easily tracked in version control

147

148

### Flexibility

149

- Same architecture can be used with different weight files

150

- Easy to share model architectures without weights

151

- Supports different weight formats or sources

152

153

### Memory Efficiency

154

- Load configuration first to validate compatibility

155

- Load weights only when needed

156

- Useful for model analysis without loading large weight files

157

158

## File Format Requirements

159

160

### JSON Configuration File

161

Must contain valid Keras model configuration in JSON format:

162

```json

163

{

164

"class_name": "Model" | "Sequential",

165

"config": {

166

// Model-specific configuration

167

}

168

}

169

```

170

171

### HDF5 Weights File

172

Must contain model weights in HDF5 format with:

173

- Layer-specific weight groups

174

- Proper weight naming conventions matching layer names

175

- Compatible data types and shapes

176

177

## Common Use Cases

178

179

### Model Deployment Pipeline

180

181

```java

182

// Step 1: Validate architecture

183

ComputationGraphConfiguration config = KerasModelImport.importKerasModelConfiguration("model.json");

184

185

// Step 2: Load full model with weights

186

ComputationGraph model = KerasModelImport.importKerasModelAndWeights("model.json", "weights.h5");

187

188

// Step 3: Deploy for inference

189

INDArray prediction = model.outputSingle(inputData);

190

```

191

192

### Model Versioning

193

194

```java

195

// Load same architecture with different weight versions

196

String architecture = "resnet50_architecture.json";

197

198

// Load v1 weights

199

ComputationGraph modelV1 = KerasModelImport.importKerasModelAndWeights(architecture, "weights_v1.h5");

200

201

// Load v2 weights

202

ComputationGraph modelV2 = KerasModelImport.importKerasModelAndWeights(architecture, "weights_v2.h5");

203

204

// Compare performance or ensemble predictions

205

```

206

207

### Transfer Learning Setup

208

209

```java

210

// Import pre-trained model architecture

211

ComputationGraphConfiguration baseConfig = KerasModelImport.importKerasModelConfiguration("base_model.json");

212

213

// Create model without pre-trained weights for custom training

214

ComputationGraph customModel = new ComputationGraph(baseConfig);

215

customModel.init();

216

217

// Or load pre-trained weights as starting point

218

ComputationGraph pretrainedModel = KerasModelImport.importKerasModelAndWeights("base_model.json", "pretrained_weights.h5");

219

```

220

221

## Error Handling

222

223

### File Path Validation

224

225

```java

226

import java.nio.file.Paths;

227

import java.nio.file.Files;

228

229

public static boolean validateFiles(String jsonPath, String weightsPath) {

230

return Files.exists(Paths.get(jsonPath)) && Files.exists(Paths.get(weightsPath));

231

}

232

233

// Usage

234

if (validateFiles("model.json", "weights.h5")) {

235

ComputationGraph model = KerasModelImport.importKerasModelAndWeights("model.json", "weights.h5");

236

} else {

237

System.err.println("One or both files do not exist");

238

}

239

```

240

241

### Weight Compatibility Check

242

243

```java

244

try {

245

// Try with strict enforcement first

246

ComputationGraph model = KerasModelImport.importKerasModelAndWeights("model.json", "weights.h5", true);

247

} catch (UnsupportedKerasConfigurationException e) {

248

System.out.println("Warning: " + e.getMessage());

249

// Fall back to relaxed enforcement

250

ComputationGraph model = KerasModelImport.importKerasModelAndWeights("model.json", "weights.h5", false);

251

}

252

```