Loading a pre trained Keras model and predicting - python-3.x

I've cobbled together a simple neural network using some Keras examples, on the basic Kaggle Cat vs Dog data (https://www.kaggle.com/c/dogs-vs-cats-redux-kernels-edition/data). I was able to train and save a model using
model.fit_generator(
#train_generator,
#samples_per_epoch=2000,
#nb_epoch=50,
#validation_data=validation_generator,
#nb_val_samples=800)
model.save('first_model.h5')
But when I try loading the model to predict, I get
Traceback (most recent call last):
File "/Users/me/PycharmProjects/CatVsDog/SampleML.py", line 48, in <module>
print(saved_model.predict_generator(test_generator, 12500))
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/keras/models.py", line 1012, in predict_generator
pickle_safe=pickle_safe)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/keras/engine/training.py", line 1763, in predict_generator
outs = self.predict_on_batch(x)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/keras/engine/training.py", line 1371, in predict_on_batch
self.internal_input_shapes)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/keras/engine/training.py", line 85, in standardize_input_data
'Found: ' + str(data)[:200] + '...')
TypeError: Error when checking : data should be a Numpy array, or list/dict of Numpy arrays. Found: None...
Exception ignored in: <bound method Session.__del__ of <tensorflow.python.client.session.Session object at 0x10c7586d8>>
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/tensorflow/python/client/session.py", line 581, in __del__
UnboundLocalError: local variable 'status' referenced before assignment
Here's where the images are saved in my PyCharm solution. There are 1-12500 .jpg files in the Test directory, 11500 labeled .jpgs in each of the Training set cat and dog directories, and 1000 labeled .jpgs in each of the validate directories.
And here's my code
from __future__ import print_function
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten
from keras.layers import Convolution2D, MaxPooling2D
from keras.preprocessing.image import ImageDataGenerator
from keras.models import load_model
train_datagen = ImageDataGenerator(
rescale=1./255,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True)
test_datagen = ImageDataGenerator(rescale=1./255)
train_generator = train_datagen.flow_from_directory(
'train',
target_size=(64, 64),
batch_size=32,
class_mode='binary')
validation_generator = test_datagen.flow_from_directory(
'validate',
target_size=(64, 64),
batch_size=32,
class_mode='binary')
test_generator = train_datagen.flow_from_directory(
'test',
target_size=(64, 64),
batch_size=32,
class_mode='binary')
nb_filters = 32
kernel_size = (3,3)
pool_size = (2, 2)
nb_classes = 2
input_shape = (64, 64, 3)
saved_model = load_model('first_model.h5')
score = saved_model.evaluate_generator(validation_generator, 2000)
print('Test score:', score[0])
print('Test accuracy:', score[1])
print(saved_model.predict_generator(test_generator, 12500))

I believe that what you feed to the predict_generator isn't in the right format.
When you predict values, in opposition with training and evaluation, you don't want to feed the labels.
Therefore I would try to change your test_generator to this :
test_generator = train_datagen.flow_from_directory(
'test',
target_size=(64, 64),
batch_size=32,
#This will not output the targets.
class_mode=None)
You can find documentation about the ImageDataGenerator here.

Related

Why doesn't Python want to read pictures?

I don’t understand why my program doesn’t read pictures. I tried many different ways, but they only solved a small part of the problems. The main one remained - OSError: Initializing from file failed.
Here is what the program shows after execution (full text):
Traceback (most recent call last):
File "C:\Users\sanch\Downloads\test.py", line 123, in <module>
train_data = pd.read_csv("C:\\Users\\sanch\\AppData\\Local\\Programs\\Python\\Python36\\Lib\\site-packages\\tensorflow\\tensorflow-for-poets-2-master\\tf_files\\test_data", delimiter=',', nrows=nRowsRead)
File "C:\Users\sanch\AppData\Local\Programs\Python\Python36\lib\site-packages\pandas\io\parsers.py", line 685, in parser_f
return _read(filepath_or_buffer, kwds)
File "C:\Users\sanch\AppData\Local\Programs\Python\Python36\lib\site-packages\pandas\io\parsers.py", line 457, in _read
parser = TextFileReader(fp_or_buf, **kwds)
File "C:\Users\sanch\AppData\Local\Programs\Python\Python36\lib\site-packages\pandas\io\parsers.py", line 895, in __init__
self._make_engine(self.engine)
File "C:\Users\sanch\AppData\Local\Programs\Python\Python36\lib\site-packages\pandas\io\parsers.py", line 1135, in _make_engine
self._engine = CParserWrapper(self.f, **self.options)
File "C:\Users\sanch\AppData\Local\Programs\Python\Python36\lib\site-packages\pandas\io\parsers.py", line 1917, in __init__
self._reader = parsers.TextReader(src, **kwds)
File "pandas/_libs/parsers.pyx", line 382, in pandas._libs.parsers.TextReader.__cinit__
File "pandas/_libs/parsers.pyx", line 693, in pandas._libs.parsers.TextReader._setup_parser_source
OSError: Initializing from file failed
from mpl_toolkits.mplot3d import Axes3D # For Basic ploting
from sklearn.preprocessing import StandardScaler # Preprocessing
from sklearn import preprocessing # Preprocessing
from random import seed
import tensorflow as tf
from sklearn.naive_bayes import GaussianNB # import gaussian naive bayes model
from sklearn.tree import DecisionTreeClassifier # import Decision tree classifier
from sklearn import metrics # Import scikit - learn metrics module for accuracy calculation
import matplotlib.image as mpimg
import matplotlib.pyplot as plt# plotting
import numpy as np # linear algebra
import os # accessing directory structure
import pandas as pd # data processing, CSV file I / O(e.g.pd.read_csv)
for dirname, _, filenames in os.walk("C:\\Users\\sanch\\AppData\\Local\\Programs\\Python\\Python36\\Lib\\site-packages\\tensorflow\\tensorflow-for-poets-2-master\\tf_files\\test_data"):
for filename in filenames:
print(os.path.join(dirname))
def create_model(lyrs=[8], act='linear', opt='Adam', dr=0.0):
# set random seed for reproducibility
seed(42)
tf.random.set_seed(42)
model = tf.keras.Sequential()
# create first hidden layer
model.add(tf.keras.Dense(lyrs[0], input_dim=X_train.shape[1], activation=act))
# create additional hidden layers
for i in range(1, len(lyrs)):
model.add(tf.keras.Dense(lyrs[i], activation=act))
# add dropout, default is none
model.add(tf.keras.Dropout(dr))
# create output layer
model.add(tf.keras.Dense(1, activation='sigmoid')) # output layer
model.complete(loss='binary_crossentropy', optimizer=opt, metrics=['accuracy'])
return model
def create_model(lyrs=[5], act='linear', opt='Adam', dr=0.0):
# set random seed for reproducibility
seed(42)
tf.random.set_seed(42)
model = tf.keras.Sequential()
# create first hidden layer
model.add(tf.keras.Dense(lyrs[0], input_dim=X_train.shape[1], activation=act))
# create additional hidden layers
for i in range(1, len(lyrs)):
model.add(tf.keras.Dense(lyrs[i], activation=act))
# add dropout, default is none
model.add(tf.keras.Dropout(dr))
# create output layer
model.add(tf.keras.Dense(1, activation='sigmoid')) # output layer
model.complete(loss='binary_crossentropy', optimizer=opt, metrics=['accuracy'])
return model
model = create_model()
print(model.summary())
# train model on full train set, witch 80/20 CV split
trainig = model.fit(X_train, y_train, epochs=100, batch_size=32, validation_split=0.2, verbose=0)
val_acc = np.mean(trainig.history['val_acc'])
print("\n%s: %.2f%%" % ('val_acc', val_acc * 100))
model = create_model()
print(model.summary())
# train model on full train set, witch 80/20 CV split
trainig = model.fit(X_train, y_train, epochs=100, batch_size=32, validation_split=0.2, verbose=0)
val_acc = np.mean(trainig.history['val_acc'])
print("\n%s: %.2f%%" % ('val_acc', val_acc * 100))
def create_model(lyrs=[10], act='linear', opt='Adam', dr=0.0):
# set random seed for reproducibility
seed(42)
tf.random.set_seed(42)
model = tf.keras.Sequential()
# create first hidden layer
model.add(tf.keras.Dense(lyrs[0], input_dim=X_train.shape[1], activation=act))
# create additional hidden layers
for i in range(1, len(lyrs)):
model.add(tf.keras.Dense(lyrs[i], activation=act))
# add dropout, default is none
model.add(tf.keras.Dropout(dr))
# create output layer
model.add(tf.keras.Dense(1, activation='sigmoid')) # output layer
model.complete(loss='binary_crossentropy', optimizer=opt, metrics=['accuracy'])
return model
model = create_model()
print(model.summary())
# train model on full train set, witch 80/20 CV split
trainig = model.fit(X_train, y_train, epochs=100, batch_size=32, validation_split=0.2, verbose=0)
val_acc = np.mean(trainig.history['val_acc'])
print("\n%s: %.2f%%" % ('val_acc', val_acc * 100))
# create model
model = tf.keras.KerasClassifier(build_nf=create_model, verbose=0)
# define the grid search parameters
batch_size = [16, 32, 64]
epochs = [50, 100]
param_grid = dict(batch_size=batch_size, ephochs=epochs)
# search the grid
grid = tf.keras.GridSearchCV(estimator=model,
param_grid=param_grid,
cv=3,
verbose=2) # include n_jobs=-1 if you are using CPU
grid_result = grid.fit(X_train, y_train)
# summarize results
print("Best: %f using %s" % (grid_result.best_score_, grid_result.best_params_))
means = grid_result.cv_results_['mean_test_score']
stds = grid_result.cv_results_['std_test_score']
params = grid_result.cv_results_["params"]
for mean, stdev, param in zip(means, stds, params):
print("%f (%f) with: %r" % (mean, stdev, param))
if __name__ == '__main__':
nRowsRead = None # specify No.of row. 'None' for whole data
# test_mosaic.scv may have more rows in reality, but we are only loading / previewing the first 1000 rows
train_data = pd.read_csv("C:\\Users\\sanch\\AppData\\Local\\Programs\\Python\\Python36\\Lib\\site-packages\\tensorflow\\tensorflow-for-poets-2-master\\tf_files\\test_data", delimiter=',', nrows=nRowsRead)
nRow, nCol = train_data.shape
print(f'Yhere are {nRow} rows and {nCol} columns')
X_train = train_data.drop('Label', axis=1)
X_test = test_data.drop('Label', axis=1)
y_train = train_data['Label']
y_test = test_data['Label']
Perhaps I did not write everything correctly, I do not know. Please help, I have very little time to show this project. I don't know what to do anymore.

Need help correcting errors after keras model.fit_generator is ran

I am new to convolution neural network and python .My model is training correctly but when I add fit_genarator, I'm getting errors as mentioned in the output section.
import keras,os
from keras.models import Sequential
from keras.layers import Dense, Conv2D, MaxPool2D , Flatten
from keras.preprocessing.image import ImageDataGenerator
import numpy as np
Classifier = Sequential()
Classifier.add(Conv2D(input_shape=(64,3,3),filters=32,kernel_size=(3,3),padding="same", activation="relu"))
Classifier.add(MaxPool2D(pool_size= (2,2),strides=(2,2)))
Classifier.add(Flatten())
Classifier.add(Dense( units=128, activation="relu"))
Classifier.add(Dense( activation="sigmoid", units=1))
Classifier.compile(optimizer="adam",
loss="binary_crossentropy",
metric=['accuracy'] )
train_datagen = ImageDataGenerator(
rescale=1./255,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True)
test_datagen = ImageDataGenerator(rescale=1./255)
train_generator = train_datagen.flow_from_directory(
'E:/P14-Convolutional-Neural-Networks/Convolutional_Neural_Networks/dataset/training_set',
target_size=(224, 224),
batch_size=32,
class_mode='binary')
validation_generator = test_datagen.flow_from_directory(
'E:/P14-Convolutional-Neural-Networks/Convolutional_Neural_Networks/dataset/test_set',
target_size=(224, 224),
batch_size=32,
class_mode='binary')
Classifier.fit_generator(
train_generator,
steps_per_epoch=8000,
epochs=25,
validation_data=validation_generator,
validation_steps=2000)
The output is:
runfile('E:/vgg_16/code.py', wdir='E:/vgg_16')
Found 8000 images belonging to 2 classes.
Found 2000 images belonging to 2 classes.
Traceback (most recent call last):
File "<ipython-input-57-f246d1da43d0>", line 1, in <module>
runfile('E:/vgg_16/code.py', wdir='E:/vgg_16')
File "C:\Users\Qandeel\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 827, in runfile
execfile(filename, namespace)
File "C:\Users\Qandeel\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "E:/vgg_16/code.py", line 61, in <module>
validation_steps=2000)
File "C:\Users\Qandeel\Anaconda3\lib\site-packages\keras\legacy\interfaces.py", line 91, in wrapper
return func(*args, **kwargs)
File "C:\Users\Qandeel\Anaconda3\lib\site-packages\keras\engine\training.py", line 1732, in fit_generator
initial_epoch=initial_epoch)
File "C:\Users\Qandeel\Anaconda3\lib\site-packages\keras\engine\training_generator.py", line 42, in fit_generator
model._make_train_function()
File "C:\Users\Qandeel\Anaconda3\lib\site-packages\keras\engine\training.py", line 333, in _make_train_function
**self._function_kwargs)
File "C:\Users\Qandeel\Anaconda3\lib\site-packages\keras\backend\tensorflow_backend.py", line 3009, in function
**kwargs)
File "C:\Users\Qandeel\Anaconda3\lib\site-packages\tensorflow_core\python\keras\backend.py", line 3772, in function
'eager execution. You passed: %s' % (kwargs,))
ValueError: Session keyword arguments are not support during eager execution. You passed: {'metric': ['accuracy']}
Python documentation explains exception.ValueError best:
Raised when an operation or function receives an argument that has the
right type but an inappropriate value, and the situation is not
described by a more precise exception such as IndexError.
To answer your question, you have metric argument in your model's compile method and it is not supported for fit_generator method as per the official keras document.
Remove metric and try again to get past it.
fit_generator(generator, steps_per_epoch=None, epochs=1,
verbose=1, callbacks=None, validation_data=None,
validation_steps=None, validation_freq=1, class_weight=None,
max_queue_size=10, workers=1,
use_multiprocessing=False, shuffle=True, initial_epoch=0)

Keras fit_generator raise You must compile your model before using it Error

I try to build a CNN + LSTM model by Keras to train a model for video classification task. Firstly, A simple model was built and trained with mock data, 'fit()' api, also it works!
But actually, what is used to train this model is a video dataset, it's so big that can't be loaded into memory. so I need a generator, in this place, I also write a mock generator which generate data with shape same as mock data method. Also, fit_generator API replaces fit.
When I run train_gen function, I get following error:
File "lstm.py", line 48, in <module>
train_gen()
File "lstm.py", line 45, in train_gen
model.fit_generator(data_generator.mock_generator(batch_size=32, num_classes=16), steps_per_epoch=1000, epochs=20)
File "/usr/local/lib/python2.7/dist-packages/keras/legacy/interfaces.py", line 91, in wrapper
return func(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/keras/engine/training.py", line 1415, in fit_generator
initial_epoch=initial_epoch)
File "/usr/local/lib/python2.7/dist-packages/keras/engine/training_generator.py", line 39, in fit_generator
model._make_train_function()
File "/usr/local/lib/python2.7/dist-packages/keras/engine/training.py", line 485, in _make_train_function
raise RuntimeError('You must compile your model before using it.')
RuntimeError: You must compile your model before using it.
I tried to solve this problem by searching in stack overflow and google, and do not find exact solution. There are some similar questions can be solved by specifying LSTM input_shape and output size, or add model.compile().
The following snippet code is runnable, and completely same as that mentioned above.
import keras
from keras.models import Sequential
from keras.layers import Input, Embedding, LSTM, Dense, Reshape
from keras.layers import Dense, Dropout, Flatten, Activation
from keras.layers.normalization import BatchNormalization
from keras.layers.wrappers import Bidirectional
from keras.optimizers import Adam
import numpy as np
import os, random
os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID" # see issue #152
os.environ["CUDA_VISIBLE_DEVICES"] = ""
use_dropout = True
metrics = ['accuracy']
#mock data generator
def mock_generator(batch_size, input_length, input_dims, num_classes=16):
while True:
yield np.random.random((batch_size, input_length, input_dims)), keras.utils.to_categorical(np.random.randint(num_classes, size=(batch_size, 1)), num_classes=num_classes)
#mock data with shape as data generator
def mock_data(batch_size, input_length, input_dims, num_classes=16):
if True:
return np.random.random((batch_size, input_length, input_dims)), keras.utils.to_categorical(np.random.randint(num_classes, size=(batch_size, 1)), num_classes=num_classes)
#construct model, lstm units is fixed
def bi_lstm(input_shape, num_classes=16):
model = Sequential()
model.add(Bidirectional(LSTM(100, return_sequences=True, activation='relu', input_shape=input_shape), merge_mode='concat'))
model.add(Bidirectional(LSTM(100, activation='relu', input_shape=(input_shape[0],100), return_sequences=False), merge_mode='concat'))
if use_dropout:
model.add(Dropout(0.5))
model.add(Dense(num_classes))
model.add(BatchNormalization())
model.add(Activation('softmax'))
optimizer = Adam(lr=1e-5, decay=1e-6)
model.compile(loss='categorical_crossentropy', optimizer=optimizer,
metrics=metrics)
return model
#fit api run successfully
def train():
input_length=10
input_dims=128
num_classes=10
model = bi_lstm((input_length, input_dims), num_classes)
x_train, y_train = mock_data(32, input_length, input_dims, num_classes)
model.fit(x_train, y_train, epochs=20, batch_size=32)
#fit_generator api raise error!
def train_gen():
input_length=10
input_dims=128
num_classes=10
model = bi_lstm((input_length, input_dims), num_classes)
generator = mock_generator(32, input_length, input_dims, num_classes)
model.fit_generator(generator, steps_per_epoch=1000, epochs=20)
#test mock generator function
def test_mock_gen():
result = mock_generator(32,10,128,16)
for i in range(2):
x, y = result.next()
print x.shape
print y.shape
if __name__ == '__main__':
train()
train_gen()
#test_mock_gen()
The error you are getting couldn't be more explicit: model has been declared as a local object on the train() function and is not know by the function train_gen(). Define your model as a global variable (ex. in main) and it will work.

Error in converting batch_size from "str to "int" in Keras 2.2.2 fit_generator

I looked hard for similar solutions but none fits the problem that I am facing, I am trying to apply transfer learning through the following code :
import os
import sys
import glob
import argparse
import matplotlib.pyplot as plt
#from keras import backend as K
from keras.applications.inception_v3 import InceptionV3, preprocess_input
from keras.models import Model
from keras.layers import Dense, GlobalAveragePooling2D
from keras.preprocessing.image import ImageDataGenerator
from keras.optimizers import SGD
IM_WIDTH, IM_HEIGHT = 299, 299 #fixed size for InceptionV3
NB_EPOCHS = 3.0
BAT_SIZE = 32.0
FC_SIZE = 1024
NB_IV3_LAYERS_TO_FREEZE = 172
def get_nb_files(directory):
"""Get number of files by searching directory recursively"""
if not os.path.exists(directory):
return 0
cnt = 0
for r, dirs, files in os.walk(directory):
for dr in dirs:
cnt += len(glob.glob(os.path.join(r, dr + "/*")))
return cnt
def setup_to_transfer_learn(model, base_model):
"""Freeze all layers and compile the model"""
for layer in base_model.layers:
layer.trainable = False
model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy'])
def add_new_last_layer(base_model, nb_classes):
"""Add last layer to the convnet
Args:
base_model: keras model excluding top
nb_classes: # of classes
Returns:
new keras model with last layer
"""
x = base_model.output
x = GlobalAveragePooling2D()(x)
x = Dense(FC_SIZE, activation='relu')(x) #new FC layer, random init
predictions = Dense(nb_classes, activation='softmax')(x) #new softmax layer
model = Model(inputs=base_model.input, outputs=predictions)
return model
def setup_to_finetune(model):
"""Freeze the bottom NB_IV3_LAYERS and retrain the remaining top layers.
note: NB_IV3_LAYERS corresponds to the top 2 inception blocks in the inceptionv3 arch
Args:
model: keras model
"""
for layer in model.layers[:NB_IV3_LAYERS_TO_FREEZE]:
layer.trainable = False
for layer in model.layers[NB_IV3_LAYERS_TO_FREEZE:]:
layer.trainable = True
model.compile(optimizer=SGD(lr=0.0001, momentum=0.9), loss='categorical_crossentropy', metrics=['accuracy'])
def train(args):
"""Use transfer learning and fine-tuning to train a network on a new dataset"""
nb_train_samples = get_nb_files(args.train_dir)
nb_classes = len(glob.glob(args.train_dir + "/*"))
nb_val_samples = get_nb_files(args.val_dir)
epochs = args.nb_epoch
batch_size = args.batch_size
#batch_size = K.cast_to_floatx(batch_size)
print('nb_train_samples',type(nb_train_samples))
print('nb_train_samples',type(batch_size))
steps_per_epoch = nb_train_samples / batch_size
validation_steps = nb_val_samples / batch_size
# data prep
train_datagen = ImageDataGenerator(
preprocessing_function=preprocess_input,
rotation_range=30,
width_shift_range=0.2,
height_shift_range=0.2,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True
)
test_datagen = ImageDataGenerator(
preprocessing_function=preprocess_input,
rotation_range=30,
width_shift_range=0.2,
height_shift_range=0.2,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True
)
train_generator = train_datagen.flow_from_directory(
args.train_dir,
target_size=(IM_WIDTH, IM_HEIGHT),
batch_size=batch_size,
)
validation_generator = test_datagen.flow_from_directory(
args.val_dir,
target_size=(IM_WIDTH, IM_HEIGHT),
batch_size=batch_size,
)
# setup model
base_model = InceptionV3(weights='imagenet', include_top=False) #include_top=False excludes final FC layer
model = add_new_last_layer(base_model, nb_classes)
# transfer learning
setup_to_transfer_learn(model, base_model)
history_tl = model.fit_generator(
train_generator,
epochs=epochs,
steps_per_epoch=steps_per_epoch,
validation_data=validation_generator,
validation_steps=validation_steps,
class_weight=None)
# fine-tuning
setup_to_finetune(model)
history_ft = model.fit_generator(
train_generator,
epochs=epochs,
steps_per_epoch=steps_per_epoch,
validation_data=validation_generator,
validation_steps=validation_steps,
class_weight=None)
model.save(args.output_model_file)
if args.plot:
plot_training(history_ft)
def plot_training(history):
acc = history.history['acc']
val_acc = history.history['val_acc']
loss = history.history['loss']
val_loss = history.history['val_loss']
epochs = range(len(acc))
plt.plot(epochs, acc, 'r.')
plt.plot(epochs, val_acc, 'r')
plt.title('Training and validation accuracy')
plt.figure()
plt.plot(epochs, loss, 'r.')
plt.plot(epochs, val_loss, 'r-')
plt.title('Training and validation loss')
plt.show()
if __name__=="__main__":
a = argparse.ArgumentParser()
a.add_argument("--train_dir")
a.add_argument("--val_dir")
a.add_argument("--nb_epoch", default=NB_EPOCHS)
a.add_argument("--batch_size", default=BAT_SIZE)
a.add_argument("--output_model_file", default="inceptionv3-ft.model")
a.add_argument("--plot", action="store_true")
args = a.parse_args()
if args.train_dir is None or args.val_dir is None:
a.print_help()
sys.exit(1)
if (not os.path.exists(args.train_dir)) or (not os.path.exists(args.val_dir)):
print("directories do not exist")
sys.exit(1)
train(args)
after a series of transformation from previous Keras release (2.1.4) I changed parameters names accordingly in fit_generator , I usually run the code using Ubuntu terminal as follows :
python fine-tune.py --train_dir train_dir --val_dir val_dir --nb_epoch NB_EPOCH --batch_size BATCH_SIZE --output_model_file inceptionv3-ft.model --plot
and I am getting the following error :
/home/raed/anaconda3/lib/python3.6/site-packages/h5py/__init__.py:34: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.
from ._conv import register_converters as _register_converters
Using TensorFlow backend.
nb_train_samples <class 'int'>
nb_train_samples <class 'str'>
Traceback (most recent call last):
File "fine-tune.py", line 183, in <module>
train(args)
File "fine-tune.py", line 80, in train
steps_per_epoch = nb_train_samples / batch_size
TypeError: unsupported operand type(s) for /: 'int' and 'str'
I tried to convert batch_zise using int(), then using tensorflow backend of keras using cast and I also got the following error :
python fine-tune.py --train_dir train_dir --val_dir val_dir --nb_epoch NB_EPOCH --batch_size BATCH_SIZE --output_model_file inceptionv3-ft.model --plot
/home/raed/anaconda3/lib/python3.6/site-packages/h5py/__init__.py:34: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.
from ._conv import register_converters as _register_converters
Using TensorFlow backend.
Traceback (most recent call last):
File "fine-tune.py", line 183, in <module>
train(args)
File "fine-tune.py", line 76, in train
batch_size = K.cast_to_floatx(batch_size)
File "/home/raed/anaconda3/lib/python3.6/site-packages/keras/backend/common.py", line 110, in cast_to_floatx
return np.asarray(x, dtype=_FLOATX)
File "/home/raed/anaconda3/lib/python3.6/site-packages/numpy/core/numeric.py", line 492, in asarray
return array(a, dtype, copy=False, order=order)
ValueError: could not convert string to float: 'BATCH_SIZE'
Am I trying the right fix , could anyone help me please solving this problem, thank in advance.

Saving Keras model fails after renaming layers

I have a problem with renaming layers. Below is the simplest example illustrating the problem:
from keras.layers import Dense, Conv2D, Flatten, Input
from keras.models import Model
inputs = Input(shape=(784,))
x = Dense(64, activation='relu')(inputs)
x = Dense(64, activation='relu')(x)
predictions = Dense(10, activation='softmax')(x)
# This creates a model that includes
# the Input layer and three Dense layers
model = Model(inputs=inputs, outputs=predictions)
model.compile(optimizer='rmsprop',
loss='categorical_crossentropy',
metrics=['accuracy'])
print(model.summary())
for i, layer in enumerate(model.layers):
layer.name = 'layer' + str(i)
print(model.summary())
model.save('temp')
It fails with the message:
Traceback (most recent call last):
File "scripts/save_load.py", line 24, in <module>
model.save('temp')
File "/lib/python3.6/site-packages/keras/engine/topology.py", line 2416, in save
save_model(self, filepath, overwrite)
File "/lib/python3.6/site-packages/keras/models.py", line 101, in save_model
'config': model.get_config()
File "/lib/python3.6/site-packages/keras/engine/topology.py", line 2281, in get_config
new_node_index = node_conversion_map[node_key]
KeyError: 'layer0_ib-0'
What am I doing wrong?
I know I can pass names to the layer constructor, it seems not to fail in this case, but is there any chance to improve my solution?

Resources