如何将.ckpt文件转换为HD5文件?

时间:2019-05-11 21:01:31

标签: python tensorflow keras

我使用以下命令将我的模型另存为.ckpt文件:

saver = tf.train.Saver()

如何使用keras将该.ckpt文件转换为hd5文件?我正在运行的代码是:

try:
        saver = tf.train.Saver()
        with tf.Session(config = self.__get_processor()) as sess:
            sess.run(tf.global_variables_initializer())

            successful_runs = 0
            total_runs = 0
            accuracy = 0

            for epoch in range(self.hm_epochs):
                epoch_loss = 0
                for data in train_data:
                    total_runs += 1
                    try:
                        X = data[0]
                        Y = data[1]
                        _, c = sess.run([optimizer, cost], feed_dict={x: X, y: Y})
                        epoch_loss += c
                        successful_runs += 1
                    except Exception as e:
                        pass


                correct = tf.equal(tf.argmax(prediction, 1), tf.argmax(y, 1))
                accuracy = tf.reduce_mean(tf.cast(correct, 'float'))
                current_accuracy = accuracy.eval({x: [i[0] for i in validation_data], y: [i[1] for i in validation_data]})
                print('[INFO] Epoch', epoch + 1, 'completed out of', self.hm_epochs, 'loss:', epoch_loss)
                print('[INFO] Accuracy:', current_accuracy )

            finish_acc = accuracy.eval({x: [i[0] for i in validation_data], y: [i[1] for i in validation_data]})
            print('[INFO] Finished Accuracy:',finish_acc )
            print('[INFO] fitment percent:', successful_runs / total_runs)

            run_time = timeit.default_timer() - start
            print('[INFO] runtime: {}'.format(run_time))
            if return_output:
                return self.__build_output(run_time, finish_acc, successful_runs / total_runs)
            save_path = saver.save(sess, output_folder + self.model_name + '.ckpt')

1 个答案:

答案 0 :(得分:0)

您实际上不能那样做。 Keras是张量流的抽象(也有其他一些后端)。 Keras做了一些其他的事情,这些事情是tensorflow不知道的。这样,您可以从Keras转换为TF,但只能通过在Keras中重写模型来实现。

如果您已经具有等效的Keras模型,但只想将权重导入其中,则可以按照此处列出的建议进行操作:https://github.com/keras-team/keras/issues/8026