I'm trying to do multiclass classification for 26 classes. It seems my model is not validating data, though I wrote a validation data generator apart from training data generator. Here is the code:
datagen = ImageDataGenerator(
rotation_range=0.2,
width_shift_range=0.05,
height_shift_range=0.05,
shear_range=0.05,
horizontal_flip=True,
fill_mode='nearest',
)
batch_size = 8
train_generator = datagen.flow(
train_images,
train_labels,
batch_size=batch_size,
shuffle=True,
subset='training',
seed=42)
valid_generator = datagen.flow(
val_images,
val_labels,
batch_size=batch_size,
shuffle=True,
subset='validation',
seed=42)
Here's the model:
img_rows = 256
img_cols = 256
def get_net():
inputs = Input((img_rows, img_cols, 1))
print("inputs shape:",inputs.shape)
#Convolution layers
conv1 = Conv2D(24, 3, strides=(2, 2), activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(inputs)
print("conv1 shape:",conv1.shape)
conv2 = Conv2D(24, 3, strides=(2, 2), activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(conv1)
print("conv2 shape:",conv2.shape)
pool1 = MaxPooling2D(pool_size=(2, 2))(conv2)
print("pool1 shape:",pool1.shape)
drop1 = Dropout(0.25)(pool1)
conv3 = Conv2D(36, 3, strides=(2, 2), activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(drop1)
print("conv3 shape:",conv3.shape)
conv4 = Conv2D(36, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(conv3)
print("conv4 shape:",conv4.shape)
pool2 = MaxPooling2D(pool_size=(2, 2))(conv4)
print("pool2 shape:",pool2.shape)
drop2 = Dropout(0.25)(pool2)
conv5 = Conv2D(48, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(drop2)
print("conv5 shape:",conv5.shape)
conv6 = Conv2D(48, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(conv5)
print("conv6 shape:",conv6.shape)
pool3 = MaxPooling2D(pool_size=(2, 2))(conv6)
print("pool3 shape:",pool3.shape)
drop3 = Dropout(0.25)(pool3)
#Flattening
flat = Flatten()(drop3)
#Fully connected layers
dense1 = Dense(128, activation = 'relu', use_bias=True, kernel_initializer = 'he_normal')(flat)
print("dense1 shape:",dense1.shape)
drop4 = Dropout(0.5)(dense1)
dense2 = Dense(128, activation = 'relu', use_bias=True, kernel_initializer = 'he_normal')(drop4)
print("dense2 shape:",dense2.shape)
drop5 = Dropout(0.5)(dense2)
dense4 = Dense(26, activation = 'softmax', use_bias=True, kernel_initializer = 'he_normal')(drop5)
print("dense4 shape:",dense4.shape)
#drop7 = Dropout(0.25)(dense4)
model = Model(input = inputs, output = dense4)
optimizer = Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=0.00000001, decay=0.0)
model.compile(optimizer = optimizer, loss = 'categorical_crossentropy', metrics = ['accuracy'])
return model
Here's the code for training:
def train():
model = get_net()
print("got model")
model.summary()
model_checkpoint = ModelCheckpoint('seqnet.hdf5', monitor='loss',verbose=1, save_best_only=True)
print('Fitting model...')
#model.fit_generator(train_generator, validation_data=validation_generator, steps_per_epoch=len(train_generator), epochs=2)
history = model.fit_generator(
train_generator,
steps_per_epoch = len(train_generator) // batch_size,
validation_data = valid_generator,
validation_steps = len(valid_generator) // batch_size,
epochs = 50)
# list all data in history
print(history.history.keys())
# summarize history for accuracy
plt.plot(history.history['acc'])
plt.plot(history.history['val_acc'])
plt.title('model accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'validation'], loc='upper left')
plt.show()
# summarize history for loss
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'validation'], loc='upper left')
plt.show()
return model
model = train()
Training log with last few epochs - note it prints info only about training accuracy and training loss, no info regarding validation.
Epoch 48/50
18/18 [==============================] - 12s 639ms/step - loss: 1.8327 - acc: 0.3125
Epoch 49/50
18/18 [==============================] - 11s 604ms/step - loss: 1.7274 - acc: 0.3840
Epoch 50/50
18/18 [==============================] - 11s 609ms/step - loss: 1.5989 - acc: 0.3542
dict_keys(['acc', 'loss'])
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-72-387ae82a9daf> in <module>()
39 return model
40
---> 41 model = train()
<ipython-input-72-387ae82a9daf> in train()
21 # summarize history for accuracy
22 plt.plot(history.history['acc'])
---> 23 plt.plot(history.history['val_acc'])
24 plt.title('model accuracy')
25 plt.ylabel('accuracy')
KeyError: 'val_acc'
Here's the incomplete curve (according to my code two plots should have been produced - one is training accuracy vs validation accuracy, another is training loss vs validation loss) :
Related
I want to predict a time series using cnn-lstm model.This is my model:
def generate_model():
model = keras.models.Sequential([
Conv1D(64, 3, padding='causal', activation='relu', input_shape=(24, 20)),
BatchNormalization(),
Conv1D(64, 3, padding='causal', activation='relu'),
BatchNormalization(),
Conv1D(32, 3, padding='causal', activation='relu'),
MaxPool1D(3),
LSTM(100, dropout=0.2, return_sequences=True),
LSTM(50, dropout=0.3),
Dense(1, activation='relu')
])
model.compile(optimizer=tf.keras.optimizers.Adam(),
loss='mean_squared_error',
metrics=[tf.keras.metrics.MeanAbsoluteError(), tf.keras.metrics.RootMeanSquaredError(), RSquare()])
return model
Then I use this line of code to train my model:
history1 = model1.fit(X1_train, y1_train, epochs=200, batch_size=32, validation_data=(X1_test, y1_test), verbose=2, callbacks=callbacks)
But values of loss and metrics stays the same and does not change. This is how they look.
These are my callbacks, just in case:
from keras.callbacks import LearningRateScheduler
def decay_schedule(epoch, lr):
lr = lr - 0.0001
return lr
lr_scheduler = LearningRateScheduler(decay_schedule)
callback = tf.keras.callbacks.EarlyStopping(monitor='val_loss', mode='max', min_delta=1e-3, patience=50)
callbacks=[lr_scheduler, callback]
Thank you in advance.
The code :
from google.colab import drive
import tensorflow as tf
from tensorflow.python.keras.models import Sequential
from tensorflow.python.keras.layers import Dense, Conv2D, MaxPool2D, Flatten
from tensorflow.python.keras.optimizer_v1 import Adam
import numpy as np
import cv2
import matplotlib.pyplot as plt
from tensorflow.python.keras.callbacks import ModelCheckpoint, EarlyStopping, ReduceLROnPlateau
device_list = tf.test.gpu_device_name()
if device_list != '/device:GPU:0':
raise SystemError('GPU device not found')
print('Found GPU at: {}'.format(device_list))
datagen_train = tf.keras.preprocessing.image.ImageDataGenerator()
datagen_val = tf.keras.preprocessing.image.ImageDataGenerator()
datagen_test = tf.keras.preprocessing.image.ImageDataGenerator()
size = 128
batch_size=20
tf.compat.v1.disable_eager_execution()
train_set = datagen_train.flow_from_directory("drive/MyDrive/train",
target_size = (size,size),
color_mode = "grayscale",
batch_size = batch_size,
class_mode='categorical',
shuffle=True)
val_set = datagen_val.flow_from_directory("drive/MyDrive/valid",
target_size = (size,size),
color_mode = "grayscale",
batch_size = batch_size,
class_mode='categorical',
shuffle=True)
test_set = datagen_train.flow_from_directory("drive/MyDrive/test",
target_size = (size,size),
color_mode = "grayscale",
batch_size = batch_size,
class_mode='categorical',
shuffle=True)
imgs,labels = next(test_set)
model = Sequential([
Conv2D(filters=64, kernel_size=(3,3), padding='same', activation='relu', input_shape=(128,128,1)),
MaxPool2D(pool_size =(2,2), strides=2),
Conv2D(filters=128, kernel_size=(3,3), padding='same', activation='relu'),
MaxPool2D(pool_size =(2,2), strides=2),
Conv2D(filters=256, kernel_size=(3,3), padding='same', activation='relu'),
MaxPool2D(pool_size =(2,2), strides=2),
Conv2D(filters=512, kernel_size=(3,3), padding='same', activation='relu'),
MaxPool2D(pool_size =(2,2), strides=2),
Flatten(),
Dense(units=256, activation='relu'),
Dense(units=512, activation='relu'),
Dense(units=2, activation='softmax')
])
checkpoint = ModelCheckpoint("./model.h5", monitor = 'val_acc', verbose=1, save_best_only = True, mode='max')
earlystopping = EarlyStopping(monitor='vall_loss', min_delta=0, patience=3, verbose=1,restore_best_weights= True)
reducelearningrate = ReduceLROnPlateau(monitor='val_loss', factor=0.2,patience=3,verbose=1,min_delta=0.0001)
callbacks_list = [earlystopping,checkpoint,reducelearningrate]
ep = 30
opt = Adam(lr=0.0001)
model.summary()
model.compile(optimizer=opt, loss='categorical_crossentropy', metrics=['accuracy'])
model.fit(x=train_set, epochs=ep,steps_per_epoch=601, validation_data = val_set, validation_steps = 209, verbose=1)
model.save('Drowsines_Detector2.h5')
model.evaluate(x = imgs, y = labels, verbose = 2)
The program on first run in google colab takes 1hour and 30 mins in first epoch. Then if gets stuck on the first epoch step 601/601. Then by cancelling it ang rerunning it, it completes the first epoch very fast, like in 15 or 16 secs. Then sometimes it gets stuck on step 600/601. And sometimes gets stuck on step 601/601. But it does not continue to second epoch. How can I fix this.
I don't know how to solve it.
def create_model(time_steps, num_layer, num_filters, kernel_size, strides, dropout_rate, activation):
model = tf.keras.Sequential()
model.add(tf.keras.layers.InputLayer(input_shape=(time_steps, 1)))
for i in range(num_layer):
filters = int(num_filters / (i+1))
model.add(
tf.keras.layers.Conv1D(
filters=filters, kernel_size=kernel_size, padding="same", strides=strides, activation=activation
)
)
if i < (num_layer - 1):
model.add(tf.keras.layers.Dropout(rate=dropout_rate))
for i in reversed(range(num_layer)):
filters = int(num_filters / (i+1))
model.add(
tf.keras.layers.Conv1DTranspose(
filters=filters, kernel_size=kernel_size, padding="same", strides=strides, activation=activation
)
)
if i != 0:
model.add(tf.keras.layers.Dropout(rate=dropout_rate))
model.add(
tf.keras.layers.Conv1DTranspose(
filters=1, kernel_size=kernel_size, padding="same"
)
)
return model
def objective(trial):
num_layer = trial.suggest_int("num_layer", 1, 3)
num_filters = int(trial.suggest_categorical("num_filters", [16, 32, 64]))
kernel_size = trial.suggest_int("kernel_size", 1, 5, 2)
strides = trial.suggest_int("strides", 2, 4, 2)
dropout_rate = trial.suggest_uniform('dropout_rate', 0.0, 0.5)
activation = trial.suggest_categorical("activation", ["relu", "sigmoid", "tanh"])
optimizer = trial.suggest_categorical("optimizer", ["sgd", "adam"])
model = create_model(TIME_STEPS, num_layer, num_filters, kernel_size, strides, dropout_rate, activation)
model.compile(
optimizer=optimizer,
loss="mse"
)
model.summary()
history = model.fit(
x_train,
x_train,
epochs=50,
batch_size=128,
validation_split=0.1,
callbacks=[
tf.keras.callbacks.EarlyStopping(monitor="val_loss", patience=5, mode="min")
],
)
return history.history["val_loss"][-1]
study = optuna.create_study()
study.optimize(objective, n_trials=50)
Error Code
study.optimize(objective, n_trials=50)
Error Statement
ValueError: Dimensions must be equal, but are 32 and 20 for '{{node mean_squared_error/SquaredDifference}} = SquaredDifference[T=DT_FLOAT](mean_squared_error/remove_squeezable_dimensions/Squeeze, IteratorGetNext:1)' with input shapes: [?,32], [?,20].
I am trying to take my CNN model and add a LSTM layer. It would be beneficial to do so given my images are ordered in time series. I've loaded each of my images using ImageDataGenerator and flow_from_directory. I am unable to add a TimeDistributed layer to make my model work. Any help would be greatly appreciated!
model = Sequential()
model.add(TimeDistributed(Conv2D(16, (3,3), padding='same', strides=(2,2),
activation='relu', input_shape = (224,224,3))))
model.add(TimeDistributed(MaxPooling2D(pool_size=(2, 2))))
model.add(Dropout(0.5))
model.add(TimeDistributed(Conv2D(32, (3,3), padding='same', strides=(2,2),
activation='relu')))
model.add(TimeDistributed(MaxPooling2D(pool_size=(2, 2))))
model.add(Dropout(0.5))
model.add(TimeDistributed(Conv2D(64, (3,3), padding='same', strides=(2,2),
activation='relu')))
model.add(TimeDistributed(MaxPooling2D(pool_size=(2, 2))))
model.add(Dropout(0.5))
model.add(TimeDistributed(Flatten()))
model.add(LSTM(units=128, return_sequences=False))
model.add(LSTM(units=64, return_sequences=False))
model.add(Dense(32))
model.add(Dense(2, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
filenames = os.listdir("train/")
categories = []
for f_name in filenames:
decision = f_name.split('.')[0]
if decision == 'cat':
categories.append(0)
if decision == 'dog':
categories.append(1)
dataset = pd.DataFrame({
'filename':filenames,
'category':categories
})
dataset["category"] = dataset["category"].replace({0: 'cat', 1: 'dog'})
train_df,validate_df = train_test_split(dataset, test_size=0.20,random_state=42)
train_df = train_df.reset_index(drop=True)
validate_df = validate_df.reset_index(drop=True)
total_train = train_df.shape[0]
total_validate = validate_df.shape[0]
train_datagen = ImageDataGenerator(
rescale=1./255,
horizontal_flip=False)
train_generator = train_datagen.flow_from_directory(train_df,
"train/",
x_col='filename',
y_col='category',
target_size=img_size,
color_mode='rgb',
class_mode='categorical',
shuffle=True,
batch_size=batch_size)
validation_datagen = ImageDataGenerator(
rescale=1./255,
horizontal_flip=False)
validation_generator = validation_datagen.flow_from_directory(
validate_df,
"train/",
x_col='filename',
y_col='category',
target_size=img_size,
color_mode='rgb',
class_mode='categorical',
shuffle=False,
batch_size=batch_size
)
model.fit_generator(
train_generator,
epochs=epochs,
validation_data=validation_generator,
validation_steps=total_validate//batch_size,
steps_per_epoch=total_train//batch_size,
callbacks=callbacks,
class_weight=class_weight
)
Can you help me on my code to : save the model (architecture and weights) every epoch, and how I can continue to training my model from the 5th checkpoint like training epoch from 1 to 25 without making checkpoints(5th model I have saved).
classifier = Sequential()
classifier.add(Conv2D(6, (3, 3), input_shape = (30, 30, 3), data_format="channels_last", activation = 'relu'))
classifier.add(MaxPooling2D(pool_size = (2, 2)))
classifier.add(Conv2D(6, (3, 3), activation = 'relu'))
classifier.add(MaxPooling2D(pool_size = (2, 2)))
classifier.add(Flatten())
classifier.add(Dense(units = 128, activation = 'relu'))
classifier.add(Dense(units = 64, activation = 'relu'))
classifier.add(Dense(units = 1, activation = 'sigmoid'))
opt = Adam(learning_rate = 0.001, beta_1 = 0.9, beta_2 = 0.999, epsilon = 1e-08, decay = 0.0)
classifier.compile(optimizer = opt, loss = 'binary_crossentropy', metrics = ['accuracy', precision, recall, fmeasure])
from keras.preprocessing.image import ImageDataGenerator
train_datagen = ImageDataGenerator(rescale = 1./255,
horizontal_flip = True,
vertical_flip = True,
rotation_range = 180)
validation_datagen = ImageDataGenerator(rescale = 1./255)
training_set = train_datagen.flow_from_directory('/home/dataset/training_set',
target_size = (30, 30),
batch_size = 32,
class_mode = 'binary')
validation_set = validation_datagen.flow_from_directory('/home/dataset/validation_set',
target_size = (30, 30),
batch_size = 32,
class_mode = 'binary')
history = classifier.fit_generator(training_set,
steps_per_epoch = 208170,
epochs = 15,
validation_data = validation_set,
validation_steps = 89140)
I'm assuming you mean you want to save your model and weights after every epoch and then at a later stage, load the model and weights saved after the fifth epoch.
You can use the SaveModel format in TensorFlow in general like this:
classifier.save()
This will save the architecture, weights, info regarding the optimizer and the configurations you set up in compile()
Since you're using fit_generator, you can just use ModelCheckpoint() to save your models like this:
from keras.callbacks import ModelCheckpoint
checkpoint = ModelCheckpoint(path_to_save_to, save_freq = 'epoch',
save_weights_only = False)
history = classifier.fit_generator(training_set,
steps_per_epoch = 208170,
epochs = 15,
validation_data = validation_set,
validation_steps = 89140,
callbacks = [checkpoint])
You can format the path so that it will save the model with epoch/loss details like this path_name + '-{epoch:02d}-{val_loss:.2f}.h5'
To load the fifth checkpoint, do this:
from keras.models import load_model
classifier = load_model(path_to_fifth_checkpoint)