修剪成json格式后如何导出tf.keras模型(修剪的版本)

时间:2019-07-12 15:19:06

标签: tensorflow keras tf.keras

我正在尝试使用MNIST数据集训练神经网络,并使用HLS4ML工具将其导出到FPGA(Xilinx)。如您所知,FPGA设备的硬件资源有限,因此在培训期间我设置了修剪优化。但是,不会修剪json格式的模型(model.to_json)。

pruning_params = {
      'pruning_schedule': sparsity.PolynomialDecay(initial_sparsity=0.50,
                                                   final_sparsity=0.90,
                                                   begin_step=0,
                                                   end_step=end_step,
                                                   frequency=100)
}



model = keras.Sequential([
    sparsity.prune_low_magnitude(
        l.Conv2D(32, 5, padding='same', activation='relu'),
        input_shape=input_shape,
        **pruning_params),
    l.MaxPooling2D((2, 2), (2, 2), padding='same'),
    l.BatchNormalization(),
    sparsity.prune_low_magnitude(
        l.Conv2D(64, 5, padding='same', activation='relu'), **pruning_params),
    l.MaxPooling2D((2, 2), (2, 2), padding='same'),
    l.Flatten(),
    sparsity.prune_low_magnitude(l.Dense(512, activation='relu'),
                                 **pruning_params),
    l.Dropout(0.4),
    sparsity.prune_low_magnitude(l.Dense(num_classes, activation='softmax'),
                                 **pruning_params)
])




model.compile(optimizer="Adam", loss='categorical_crossentropy', metrics=['accuracy'])

callbacks = [
    sparsity.UpdatePruningStep()
]

hist = model.fit(x_train, y_train,
          batch_size=batch_size,
          epochs=epochs,
          verbose=1,
          callbacks=callbacks,
          validation_data=(x_test, y_test))


model = sparsity.strip_pruning(model)

model_json = model.to_json()
with open("model.json", "w") as json_file:
    json_file.write(model_json)

model.save_weights("model.h5")

model_json在修剪之前包含整个模型,我需要修剪模型的版本。

0 个答案:

没有答案