Search code examples
python-3.xtensorflowdeep-learningmacos-sierratflearn

TensorFlow : Cant load trained model


I am trying to train, save and load a tensorflow model using tflearn

        # Building convolutional network

        network = input_data(shape=[None, imageSize, imageSize, 1], name='input')
        network = conv_2d(network, imageSize, self.windowSize, activation='relu', regularizer="L2")
        network = max_pool_2d(network, 2)
        network = local_response_normalization(network)
        network = conv_2d(network, imageSize * 2, self.windowSize, activation='relu', regularizer="L2")
        network = max_pool_2d(network, 2)
        network = local_response_normalization(network)
        network = fully_connected(network, (dim4 * dim4) * (imageSize * 2), activation='tanh')
        network = dropout(network, keep)
        network = fully_connected(network, (dim4 * dim4) * (imageSize * 2), activation='tanh')
        network = dropout(network, keep)
        network = fully_connected(network, n_classes, activation='softmax')
        network = regression(network, optimizer='adam', learning_rate=self.learningRate,
                                loss='categorical_crossentropy', name='target')

        model = tflearn.DNN(network, tensorboard_verbose=0, tensorboard_dir='some/dir')
        model.fit(

            {'input': np.array(myData.train_x).reshape(-1, self.imageSize, self.imageSize, 1)}, {'target': myData.train_y}, n_epoch=self.epochs,

            validation_set=(
                {'input': np.array(myData.test_x).reshape(-1, self.imageSize, self.imageSize, 1)},
            {'target': myData.test_y}),
        snapshot_step=100, show_metric=True, run_id='convnet')
        model.save("some/path/model")

this part works. next, i do

        model_path = "some/path/model.meta"

        if os.path.exists(model_path):
            model.load(model_path)
        else :
            return "need to train the model"

        prediction = self.model.predict([<some_input>])
        print(str(prediction))
        return prediction

this fails at model.load(model_path). I get the following error trace

DataLossError (see above for traceback): Unable to open table file some/path/model.meta: Data loss: not an sstable (bad magic number): perhaps your file is in a different file format and you need to use a different restore operator?
     [[Node: save_5/RestoreV2_4 = RestoreV2[dtypes=[DT_FLOAT], _device="/job:localhost/replica:0/task:0/device:CPU:0"](_arg_save_5/Const_0_0, save_5/RestoreV2_4/tensor_names, save_5/RestoreV2_4/shape_and_slices)]]
Caused by op 'save_5/RestoreV2_4', defined at:

what is meant by

Data loss: not an sstable (bad magic number): perhaps your file is in a different file format and you need to use a different restore operator?

I can see that the model is indeed saved properly and is not an empty file. Why cant i load it?

Version Information

tensorflow==1.4.0
tensorflow-tensorboard==0.4.0rc2
tflearn==0.3.2
Python 3.6.3 :: Anaconda, Inc.

Solution

  • ANSWER :

    As discussed in the comments, The path you are saving the variables to must contain the ".ckpt" file name.

    Similarly restoring should take place through the same ".ckpt" file