22import pathlib
33
44import cv2
5+ import matplotlib .pyplot as plt
56import numpy as np
7+ from keras import backend as K
8+ from keras import layers
9+ from keras import models
10+ from keras .callbacks import ReduceLROnPlateau
11+ from keras .utils import to_categorical
612
713
814def load_data (fn = 'texts.npz' , to = False ):
9- from keras .utils import to_categorical
1015 data = np .load (fn )
1116 texts , labels = data ['texts' ], data ['labels' ]
1217 texts = texts / 255.0
@@ -19,7 +24,6 @@ def load_data(fn='texts.npz', to=False):
1924
2025
2126def savefig (history , fn = 'loss.jpg' , start = 2 ):
22- import matplotlib .pyplot as plt
2327 # 忽略起点
2428 loss = history .history ['loss' ][start - 1 :]
2529 val_loss = history .history ['val_loss' ][start - 1 :]
@@ -34,13 +38,9 @@ def savefig(history, fn='loss.jpg', start=2):
3438
3539
3640def main ():
37- from keras import models
38- from keras import layers
39- from keras .callbacks import ReduceLROnPlateau
4041 (train_x , train_y ), (test_x , test_y ) = load_data ()
41- _ , h , w , _ = train_x .shape
4242 model = models .Sequential ([
43- layers .Conv2D (64 , (3 , 3 ), activation = 'relu ' , padding = 'same ' , input_shape = (h , w , 1 )),
43+ layers .Conv2D (64 , (3 , 3 ), padding = 'same ' , activation = 'relu ' , input_shape = (None , None , 1 )),
4444 layers .MaxPooling2D (), # 19 -> 9
4545 layers .Conv2D (64 , (3 , 3 ), padding = 'same' , activation = 'relu' ),
4646 layers .MaxPooling2D (), # 9 -> 4
@@ -77,15 +77,12 @@ def load_data_v2():
7777
7878
7979def acc (y_true , y_pred ):
80- import keras .backend as K
8180 return K .cast (K .equal (K .argmax (y_true + y_pred , axis = - 1 ),
8281 K .argmax (y_pred , axis = - 1 )),
8382 K .floatx ())
8483
8584
8685def main_v19 (): # 1.9
87- from keras import models
88- from keras .callbacks import ReduceLROnPlateau
8986 (train_x , train_y ), (test_x , test_y ) = load_data_v2 ()
9087 model = models .load_model ('model.v1.0.h5' )
9188 model .compile (optimizer = 'RMSprop' ,
@@ -100,13 +97,9 @@ def main_v19(): # 1.9
10097
10198
10299def main_v20 ():
103- from keras import models
104- from keras import layers
105- from keras .callbacks import ReduceLROnPlateau
106100 (train_x , train_y ), (test_x , test_y ) = load_data ()
107- _ , h , w , _ = train_x .shape
108101 model = models .Sequential ([
109- layers .Conv2D (64 , (3 , 3 ), activation = 'relu' , padding = 'same' , input_shape = (h , w , 1 )),
102+ layers .Conv2D (64 , (3 , 3 ), activation = 'relu' , padding = 'same' , input_shape = (None , None , 1 )),
110103 layers .MaxPooling2D (), # 19 -> 9
111104 layers .Conv2D (64 , (3 , 3 ), activation = 'relu' , padding = 'same' ),
112105 layers .MaxPooling2D (), # 9 -> 4
@@ -140,7 +133,6 @@ def main_v20():
140133
141134
142135def predict (texts ):
143- from keras import models
144136 model = models .load_model ('model.h5' )
145137 texts = texts / 255.0
146138 _ , h , w = texts .shape
0 commit comments