Search code examples
pythonunicodekeras

Keras model.to_json() error: 'rawunicodeescape' codec can't decode bytes in position 94-98: truncated \uXXXX


model.to_json()

for the model

____________________________________________________________________________________________________ Layer (type) Output Shape Param #
Connected to
==================================================================================================== lambda_1 (Lambda) (None, 3, 160, 320) 0
lambda_input_1[0][0]
____________________________________________________________________________________________________ convolution2d_1 (Convolution2D) (None, 1, 40, 16) 327696
lambda_1[0][0]
____________________________________________________________________________________________________ elu_1 (ELU) (None, 1, 40, 16) 0
convolution2d_1[0][0]
____________________________________________________________________________________________________ convolution2d_2 (Convolution2D) (None, 1, 20, 32) 12832
elu_1[0][0]
____________________________________________________________________________________________________ elu_2 (ELU) (None, 1, 20, 32) 0
convolution2d_2[0][0]
____________________________________________________________________________________________________ convolution2d_3 (Convolution2D) (None, 1, 10, 64) 51264
elu_2[0][0]
____________________________________________________________________________________________________ flatten_1 (Flatten) (None, 640) 0
convolution2d_3[0][0]
____________________________________________________________________________________________________ dropout_1 (Dropout) (None, 640) 0
flatten_1[0][0]
____________________________________________________________________________________________________ elu_3 (ELU) (None, 640) 0
dropout_1[0][0]
____________________________________________________________________________________________________ dense_1 (Dense) (None, 512) 328192
elu_3[0][0]
____________________________________________________________________________________________________ dropout_2 (Dropout) (None, 512) 0
dense_1[0][0]
____________________________________________________________________________________________________ elu_4 (ELU) (None, 512) 0
dropout_2[0][0]
____________________________________________________________________________________________________ dense_2 (Dense) (None, 1) 513
elu_4[0][0]
==================================================================================================== Total params: 720,497 Trainable params: 720,497 Non-trainable params: 0 ____________________________________________________________________________________________________ None

throws the exception

'rawunicodeescape' codec can't decode bytes in position 94-98: truncated \uXXXX

What could be the problem and how can I solve it?


Solution

  • I came across a similar problem when using keras 1.2.1 with a tensorflow-gpu backend.

    I found out it was caused because windows 10 anniversary edition was having problems encoding the forward slash character.

    Using the Lambda layer makes the to_json() call fail but switching to batch normalization works just fine.

    model = Sequential()
    
    # model.add(Lambda(lambda x: x / 255. - .5, input_shape=INPUT_DIMENSIONS))
    model.add(BatchNormalization(input_shape=INPUT_DIMENSIONS, axis=1))
    . . . 
    # POST PROCESSING, SAVE MODEL TO DISK
    with open('model.json', 'w') as json_file:
        json_file.write(model.to_json())
    

    Not an ideal solution but hopefully it works for someone looking at this in the future.