tensorflow: Checkpoint is not work properly

Please make sure that this is a bug. As per our GitHub Policy, we only address code/doc bugs, performance issues, feature requests and build/installation issues on GitHub. tag:bug_template

System information

  • Have I written custom code (as opposed to using a stock example script provided in TensorFlow):
  • OS Platform and Distribution (e.g., Linux Ubuntu 16.04): Windows 10 x64 1909 Home
  • Mobile device (e.g. iPhone 8, Pixel 2, Samsung Galaxy) if the issue happens on mobile device:
  • TensorFlow installed from (source or binary): pip
  • TensorFlow version (use command below): 2.1.0
  • Python version: 3.7.6
  • Bazel version (if compiling from source):
  • GCC/Compiler version (if compiling from source):
  • CUDA/cuDNN version:
  • GPU model and memory:

You can collect some of this information using our environment capture script You can also obtain the TensorFlow version with:

  1. TF 1.0: python -c "import tensorflow as tf; print(tf.GIT_VERSION, tf.VERSION)"
  2. TF 2.0: python -c "import tensorflow as tf; print(tf.version.GIT_VERSION, tf.version.VERSION)"

Describe the current behavior tf.save_weights is not work properly

https://www.tensorflow.org/tutorials/keras/save_and_load#saved_model을_사용하기

When I follow the tutorial of tensorflow checkpoint section, It does not make *.ckpt file but makes .ckpt folder!!!

Describe the expected behavior It need make .ckpt files.

Standalone code to reproduce the issue Provide a reproducible test case that is the bare minimum necessary to generate the problem. If possible, please share a link to Colab/Jupyter/any notebook.

Other info / logs Include any logs or source code that would be helpful to diagnose the problem. If including tracebacks, please include the full traceback. Large logs and files should be attached.

import tensorflow as tf
import matplotlib.pyplot as plt
import numpy as np
import cv2
import random
import os




np.random.seed(1)
random.seed(1)
tf.random.set_seed(1)

checkpoint_path = 'checkpoint\\cp-{epoch:04d}.ckpt'
checkpoint_dir = os.path.dirname(checkpoint_path)


(train_images, train_labels), (test_images, test_labels) = tf.keras.datasets.mnist.load_data()

train_images = train_images / 255.0
test_images = test_images / 255.0


train_dataset = tf.data.Dataset.from_tensor_slices((train_images, train_labels))


model = tf.keras.Sequential([
    tf.keras.layers.Flatten(input_shape=(28, 28)),
    tf.keras.layers.Dense(512, activation='relu'),
    tf.keras.layers.BatchNormalization(),
    tf.keras.layers.Dropout(0.02, seed=1),
    tf.keras.layers.Dense(254, activation='relu'),
    tf.keras.layers.Dropout(0.02, seed=1),
    tf.keras.layers.Dense(128, activation='relu'),
    tf.keras.layers.Dropout(0.01, seed=1),
    tf.keras.layers.Dense(64, activation='relu'),
    tf.keras.layers.Dense(32, activation='sigmoid'),
    tf.keras.layers.Dense(10)
])



checkpoint_callback = tf.keras.callbacks.ModelCheckpoint(
    filepath=checkpoint_path,
    verbose=1,
    save_weight_only=True,
    period=2
)


model.save_weights(checkpoint_path.format(epoch=0))

epochs = 10

model.compile(
    loss = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
    metrics=['accuracy']
)


model.fit(
    train_dataset.shuffle(int(len(train_images)+500), seed=1).batch(256),
    epochs=epochs,
    callbacks=[checkpoint_callback],
    verbose=1
)


loss, acc = model.evaluate(test_images,  test_labels, verbose=2)
print("Untrained model, accuracy: {:5.2f}%".format(100*acc))

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 22 (7 by maintainers)

Most upvoted comments

@abStack-pk HI, I hadn’t know that has missing letter ‘s’. Thanks, My question is solved.

Best regards.

Change save_weight_only=True, to save_weights_only=True (weights with s) in tf.keras.callbacks.ModelCheckpoint and it should create files instead of folders

Ref : https://abstack.pk/post/tensorflow-create-ckpt-files-instead-of-folders