简体   繁体   中英

How to convert .ckpt file to Hd5 file?

I have saved my model using as a .ckpt file using:

saver = tf.train.Saver()

How can I convert this .ckpt file to a hd5 file using keras? The code I am running is:

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')

You cannot actually so that. Keras is an abstraction on top of tensorflow (and has a few other backends as well). Keras does some extra things that tensorflow does not know of. As such, you can convert from Keras to TF, but not the other way around other than by rewriting the model in Keras.

If you already have an equivalent Keras model but want to import only the weights into it, then you can follow the suggestions listed here: https://github.com/keras-team/keras/issues/8026

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM