Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 674
user1934212 Avatar asked Jan 25 '17 09:01

user1934212


2 Answers

It seems like your code is in directory like this: "C:\Users\python\u{...}.py". This kind of error is related to python 3 version, where we get special character \u and can't decode it on Windows machines. You can change filename or full path to file, so that it did not contain special characters or make a patch to function 'func_dump' from file generic_utils.py (it can be reached by following path 'keras/utils/generic_utils.py'). You should replace line code = marshal.dumps(func.__code__).decode('raw_unicode_escape') by line code = marshal.dumps(func.__code__).replace(b'\\',b'/').decode('raw_unicode_escape').

like image 50
Eduard Ilyasov Avatar answered Oct 21 '22 18:10

Eduard Ilyasov


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.

like image 4
davidawad Avatar answered Oct 21 '22 20:10

davidawad