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.
Related
Running into an issue trying to get a custom metric callback to work with Tensorflow. I've created a minimal working example below to help troubleshoot. I'm running:
Windows 10
Python 3.6
scikit-learn==0.23.2
pandas==0.25.3
numpy==1.18.5
tensorflow==2.3.0
Using the breast cancer binary dataset, I'm trying to invoke the custom metric that was shown as a solution here, but running into the above error, probably because I'm not using it right.
This code...
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.metrics import precision_score, recall_score, f1_score
import pandas as pd
import numpy as np
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.callbacks import Callback
# Get binary classification dataset
data = load_breast_cancer(as_frame=True)
print(data)
df = data['data']
df['target'] = data['target']
# Train Test split
train, test = train_test_split(data, test_size = 0.10, shuffle=False)
# Define features and labels
x_train = train.iloc[:, :-1]
y_train = train.iloc[:, -1]
x_test = test.iloc[:, :-1]
y_test = test.iloc[:, -1]
# https://github.com/keras-team/keras/issues/10472#issuecomment-472543538
class Metrics(Callback):
def __init__(self, val_data, batch_size=20):
super().__init__()
self.validation_data = val_data
self.batch_size = batch_size
def on_train_begin(self, logs={}):
# print(self.validation_data)
self.val_f1s = []
self.val_recalls = []
self.val_precisions = []
def on_epoch_end(self, epoch, logs={}):
batches = len(self.validation_data)
total = batches * self.batch_size
val_pred = np.zeros((total,1))
val_true = np.zeros((total))
for batch in range(batches):
xVal, yVal = next(self.validation_data)
val_pred[batch * self.batch_size : (batch+1) * self.batch_size] = np.asarray(self.model.predict(xVal)).round()
val_true[batch * self.batch_size : (batch+1) * self.batch_size] = yVal
val_pred = np.squeeze(val_pred)
_val_f1 = f1_score(val_true, val_pred)
_val_precision = precision_score(val_true, val_pred)
_val_recall = recall_score(val_true, val_pred)
self.val_f1s.append(_val_f1)
self.val_recalls.append(_val_recall)
self.val_precisions.append(_val_precision)
return
# Define a function that creates a basic model
def make_deep_learning_classifier():
model = Sequential()
model.add(Dense(64, activation='relu', input_dim=x_train.shape[1], kernel_initializer='normal'))
model.add(Dense(32, activation='relu', input_dim=x_train.shape[1], kernel_initializer='normal'))
model.add(Dense(1, kernel_initializer='normal', activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer=Adam(), metrics=['accuracy'])
return model
# Get our model
model = make_deep_learning_classifier()
print(model.summary())
# Define some params
batch_size = 32
# Call our custom callback
callback = [Metrics(val_data=[x_test, y_test], batch_size=batch_size)] # < Issue here?
# Start training
model.fit(x_train, y_train, epochs=1000, batch_size=batch_size, verbose=1, callbacks=callback, validation_data=(x_test, y_test))
print(Metrics.val_precisions) # < Issue here?
...produces this traceback...
File "test.py", line 91, in <module>
model.fit(x_train, y_train, epochs=1000, batch_size=batch_size, verbose=1, callbacks=callback, validation_data=(x_test, y_test))
File "C:\Users\...\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\keras\engine\training.py", line 108, in _method_wrapper
return method(self, *args, **kwargs)
File "C:\Users\...\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\keras\engine\training.py", line 1137, in fit
callbacks.on_epoch_end(epoch, epoch_logs)
File "C:\Users\...\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\keras\callbacks.py", line 416, in on_epoch_end
callback.on_epoch_end(epoch, numpy_logs)
File "test.py", line 54, in on_epoch_end
xVal, yVal = next(self.validation_data)
TypeError: 'list' object is not an iterator
When I change val_data=[x_test, y_test] to val_data=(x_test, y_test) in the callback variable, I get...
TypeError: 'tuple' object is not an iterator
The user who proposed this callback solution mentions something about generators, but I'm not sure how those work. Just trying to define my own custom metric for Tensorflow/Keras. I won't be using this exact callback, but once I get this one running, I can modify it to my own. Just providing it as an example that seemed to work in that GitHub post that I hope someone will be able to point out what I'm doing wrong.
Thanks!
UPDATE
Using the solution below, I try to properly call my iterator function on my val_data by using
iter_val_data = iter(self.validation_data)
for batch in range(batches):
xVal, yVal = next(iter_val_data)
But then I get a too many values to unpack error, so I change it to:
iter_val_data = iter(self.validation_data)
for batch in range(batches):
xVal = next(iter_val_data)
yVal = next(iter_val_data)
Then I get the error:
Traceback (most recent call last):
File "test.py", line 89, in <module>
model.fit(x_train, y_train, epochs=1000, batch_size=batch_size, verbose=1, callbacks=callback, validation_data=(x_test, y_test))
File "C:\Users\...\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\keras\engine\training.py", line 108, in _method_wrapper
return method(self, *args, **kwargs)
File "C:\Users\...\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\keras\engine\training.py", line 1137, in fit
callbacks.on_epoch_end(epoch, epoch_logs)
File "C:\Users\...\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\keras\callbacks.py", line 416, in on_epoch_end
callback.on_epoch_end(epoch, numpy_logs)
File "test.py", line 53, in on_epoch_end
val_pred[batch * self.batch_size : (batch+1) * self.batch_size] = np.asarray(self.model.predict(xVal)).round()
ValueError: could not broadcast input array from shape (57,1) into shape (32,1)
Ideas from here? Try and run the code in the same environment as described above if you can. Thanks!
As you see here and as your error messages states, you need to use next() with an iterator. You call next() on the list, how should next() know, which element is coming next? For that you need an iterator, that saves that state. So this should fix your issue:
iter_val_data = iter(self.validation_data)
for batch in range(batches):
xVal, yVal = next(iter_val_data)
I was working on tensorflow and tried to save and load a model. model resides in below file
model = keras.Sequential()
model.add(keras.layers.Dense(785, activation ='sigmoid' ))
model.add(keras.layers.Dense(25, activation = 'sigmoid'))
model.add(keras.layers.Dense(10, activation = 'sigmoid'))
model.compile(optimizer=tf.train.GradientDescentOptimizer(0.01),
loss='mse',
metrics=['mae'])
model.fit(X,Y,epochs = 20, callbacks=[history])
f = h5py.File(r'C:\Users\akash\Desktop\Big Data\Model\model1', "w")
tf.keras.models.save_model(
model,
f,
overwrite=True,
include_optimizer=True
)
and my load file is as below
model1 = tf.keras.models.load_model(
r'C:\Users\akash\Desktop\Big Data\Model\model1',
custom_objects=None,
compile=True
)
model1.compile(optimizer=tf.train.GradientDescentOptimizer(0.01),
loss='mse',
metrics=['mae'])
I had to compile my model again as tensorflow requires you to do so and does not allow optimisers to be saved
and due to this im getting the below error
Using TensorFlow backend.
WARNING:tensorflow:No training configuration found in save file: the model was *not* compiled. Compile it manually.
Traceback (most recent call last):
File "C:/Users/akash/Desktop/Big Data/scripts/load_model.py", line 21, in <module>
metrics=['mae'])
File "C:\Python\lib\site-packages\tensorflow\python\training\checkpointable\base.py", line 426, in _method_wrapper
method(self, *args, **kwargs)
File "C:\Python\lib\site-packages\tensorflow\python\keras\engine\training.py", line 525, in compile
metrics, self.output_names)
AttributeError: 'Sequential' object has no attribute 'output_names'
Maybe this helps you:
# MLP for Pima Indians Dataset Serialize to JSON and HDF5
from keras.models import Sequential
from keras.layers import Dense
from keras.models import model_from_json
import numpy
import os
# fix random seed for reproducibility
numpy.random.seed(7)
# load pima indians dataset
dataset = numpy.loadtxt("pima-indians-diabetes.csv", delimiter=",")
# split into input (X) and output (Y) variables
X = dataset[:,0:8]
Y = dataset[:,8]
# create model
model = Sequential()
model.add(Dense(12, input_dim=8, kernel_initializer='uniform', activation='relu'))
model.add(Dense(8, kernel_initializer='uniform', activation='relu'))
model.add(Dense(1, kernel_initializer='uniform', activation='sigmoid'))
# Compile model
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
# Fit the model
model.fit(X, Y, epochs=150, batch_size=10, verbose=0)
# evaluate the model
scores = model.evaluate(X, Y, verbose=0)
print("%s: %.2f%%" % (model.metrics_names[1], scores[1]*100))
# serialize model to JSON
model_json = model.to_json()
with open("model.json", "w") as json_file:
json_file.write(model_json)
# serialize weights to HDF5
model.save_weights("model.h5")
print("Saved model to disk")
# later...
# load json and create model
json_file = open('model.json', 'r')
loaded_model_json = json_file.read()
json_file.close()
loaded_model = model_from_json(loaded_model_json)
# load weights into new model
loaded_model.load_weights("model.h5")
print("Loaded model from disk")
# evaluate loaded model on test data
loaded_model.compile(loss='binary_crossentropy', optimizer='rmsprop', metrics=['accuracy'])
score = loaded_model.evaluate(X, Y, verbose=0)
print("%s: %.2f%%" % (loaded_model.metrics_names[1], score[1]*100))
I am training a LSTM model using keras. Training the training set gives accuracy:83% in the last epoch, but evaluating or predicting the model with the same training set gives accuracy:47%. I cant figure out the problem.
Also, I have commented out the training part in the following code, since i save the model & weights on disk, but I used it before to train i.e. model.fit(...) etc.
import pandas as pd
import Preprocessing as pre
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.utils import shuffle
import pickle
import numpy as np
import sys
from keras.models import Sequential, load_model
from keras.layers import Dense, Dropout, Activation
from keras.layers import Embedding
from keras.callbacks import ModelCheckpoint, ReduceLROnPlateau
from keras.layers import LSTM
from keras.preprocessing.sequence import pad_sequences
from keras.models import model_from_json
from keras.preprocessing.text import Tokenizer
df = pd.read_csv('tweets.csv',header=None,encoding = "ISO-8859-1")
df=shuffle(df)
length=df.size
train=[]
test=[]
Y=[]
Y2=[]
count=450000
for a in range(450000): #loading data
# b=pre.preprocess_tweet(df[1][a])
label=df[0][a]
# train.append(b)
Y.append(label)
count-=1
print("Loading training data...", count)
# with open('training_data_no_stopwords.obj', 'wb') as fp:
# pickle.dump(train, fp)
with open ('training_data.obj', 'rb') as fp:
train = pickle.load(fp)
count=156884
for a in range(450000,606884): #loading testin data
# b = pre.preprocess_tweet(df[1][a])
label=df[0][a]
# test.append(b)
Y2.append(label)
count-=1
print("Loading testing data...", count)
# with open('testing_data_no_stopwords.obj', 'wb') as fp:
# pickle.dump(test, fp)
with open ('testing_data.obj', 'rb') as fp:
test = pickle.load(fp)
# vectorizer = CountVectorizer(analyzer = "word",tokenizer = None, preprocessor = None, stop_words = None, max_features = 3500)
# # # fit_transform() does two functions: First, it fits the model
# # # and learns the vocabulary; second, it transforms our training data
# # # into feature vectors. The input to fit_transform should be a list of
# # # strings.
#
# train = vectorizer.fit_transform(train)
# test = vectorizer.transform(test)
tokenizer = Tokenizer(split=' ')
tokenizer.fit_on_texts(train)
train = tokenizer.texts_to_sequences(train)
max_words = 134
train = pad_sequences(train, maxlen=max_words)
tokenizer.fit_on_texts(test)
test = tokenizer.texts_to_sequences(test)
test = pad_sequences(test, maxlen=max_words)
print ('Extracting features & training batches')
print("Training...")
embedding_size=32
model = Sequential()
model.add(Embedding(606884, 32, input_length=134))
model.add(Dropout(0.4))
model.add(LSTM(128))
model.add(Dense(64))
model.add(Dropout(0.5))
model.add(Activation('relu'))
model.add(Dense(1))
model.add(Activation('sigmoid'))
print(model.summary())
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
# batch_size = 501
# num_epochs = 3
#
# model.fit(train, np.array(Y), batch_size=batch_size, epochs=num_epochs ,validation_split=0.2,shuffle=True)
# # Save the weights
# model.save_weights('LSTM_model_weights.h5')
#
# # Save the model architecture
# with open('LSTM_model.json', 'w') as f:
# f.write(model.to_json())
# Model reconstruction from JSON file
with open('LSTM_model.json', 'r') as f:
model = model_from_json(f.read())
# Load weights into the new model
model.load_weights('LSTM_model_weights.h5')
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
scores = model.evaluate(train, np.array(Y))
print('Test accuracy:', scores[1])
Maybe try training with Stratified K-Fold. This probably happens because your test data is very different from training data. Also, you can try shuffling the data before train-test splitting.
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.
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.