我无法在Keras中保存顺序模型

时间:2020-07-01 00:19:23

标签: tensorflow keras

nuxt.js

我有错误


## Step 1) Import Libraries

import os
os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
os.environ["CUDA_VISIBLE_DEVICE"] = ""

from tensorflow.python.client import device_lib;
print(device_lib.list_local_devices());

import keras
import numpy as np
from keras.datasets import cifar10
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten
from keras.layers import Conv2D, MaxPooling2D

#### Set some parameters

batch_size = 256
num_classes = 10
epochs = 3

## Step 2) Load and Prepare data|

(x_train, y_train), (x_test, y_test) = cifar10.load_data()
print('x_train shape:', x_train.shape)
print('y_train shape:', y_train.shape)
print(x_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples')

### Prepare data

x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255

import matplotlib.pyplot as plt
%matplotlib inline

plt.figure()
fig_size = [20, 20]

plt.rcParams["figure.figsize"] = fig_size

idx = 0
for idx in range(1,101):
    ax = plt.subplot(10, 10, idx)
    img = x_train[idx,:,:,:]
    ax.get_xaxis().set_visible(False)
    ax.get_yaxis().set_visible(False)
    plt.imshow(img, cmap="Greys_r")
plt.show()

#### Preprocess class lables

y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)

#### Data Augmentation

datagen = ImageDataGenerator(
featurewise_center = False, # set input mean to over the dataset
samplewise_center = False, # set each sample mean to 0
featurewise_std_normalization = False, # divide inputs of the dataset
samplewise_std_normalization = False, # divide each input by its std
zca_whitening = False, # apply ZCA whitening
rotation_range = 0, # randomply rotate images in the range (degrees, 0 to 180)
width_shift_range = 0.1, # randomly shift images horiznontally (fraction of totla width)
height_shift_range = 0.1, # randomly shift images vertically (fraction of total height)
horizontal_flip = True, # randomly flip images
vertical_flip = False) # randomly flip images


## Setp 3) Define model architecture

model = Sequential()

model.add(Conv2D(32, (3,3), padding = 'same', activation = 'relu', input_shape = x_train.shape[1:]))

model.add(Conv2D(32, (3,3), activation = 'relu',))
model.add(MaxPooling2D(pool_size = (2,2)))
model.add(Dropout(0.25))

model.add(Conv2D(64, (3,3), padding = 'same', activation = 'relu',))

model.add(Conv2D(64, (3,3), activation = 'relu',))
model.add(MaxPooling2D(pool_size = (2,2)))
model.add(Dropout(0.25))

model.add(Flatten())
model.add(Dense(512, activation = 'relu',))
model.add(Dropout(0.5))
model.add(Dense(num_classes, activation = 'softmax'))

## Step 4) Compile model

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

#### Summary of model

model.summary()

## Step 5) Fit model on training data

# Compute quantities required for feature-wise normalization
# (std, mean, and principal components if ZCA whitening is applied).
datagen.fit(x_train)

# Fit the model on the batches generated by datagen.flow().
model.fit_generator(datagen.flow(x_train, y_train, batch_size = batch_size),
                    steps_per_epoch = x_train.shape[0] // batch_size, # // is the quotient without remainder
                    epochs = epochs, 
                    validation_data = (x_test, y_test))

model.save('my_model')

我训练了一个顺序模型并尝试通过代码模型保存它。save(filename) 但是它说AttributeError:模块'keras.optimizers'没有属性'TFOptimizer'

错误出现在model.save(path)上。 我不知道为什么我不能保存顺序模型。 即使当我直接从keras.optimizers导入TFOptimizer时,也会出现该错误 我还安装了H

1 个答案:

答案 0 :(得分:1)

我遇到了同样的问题。当我将keras和tensorflow的版本改为keras2.2.5和tensorflow1.14.0后,问题就解决了。