Ejemplo n.º 1
0
X_test = X_test.astype('float32') / 255

# Convert class vectors to binary class matrices.
Y_train = keras.utils.to_categorical(Y_train, NB_CLASSES)
Y_test = keras.utils.to_categorical(Y_test, NB_CLASSES)
'''人为削减'''
X_train = X_train[0:1000]
Y_train = Y_train[0:1000]
X_test = X_test[0:200]
Y_test = Y_test[0:200]

# initiate RMSprop optimizer
opt = keras.optimizers.rmsprop(lr=0.001, decay=1e-6)

# train the model using RMSprop
model = CNN.createCNN(INPUT_SHAPE, NB_CLASSES)
model.compile(loss='categorical_crossentropy',
              optimizer=opt,
              metrics=['accuracy'])

modelfile = 'modelweight_10percent.model'  #神经网络权重保存
file_path_history = 'historyfile.bin'  #保存history,留着作图

if os.path.exists(modelfile):  #如果存在之前训练的权重矩阵,载入模型
    print('载入模型参数')
    model.load_weights(modelfile)
else:
    print('训练')
    history = model.fit(X_train,
                        Y_train,
                        batch_size=BATCH_SIZE,