Trying to run classification on the CIFAR-10 dataset with a simple CNN. However, the model stops after completing the first epoch and doesn't go on to complete all five. Please help.
INPUT:
cifar10 = tf.keras.datasets.cifar10
(train_images, train_labels), (test_images, test_labels) = cifar10.load_data()
import os
import matplotlib.pyplot as plt
import numpy as np
import time
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
from tensorflow.keras import models
from tensorflow.keras import optimizers
from tensorflow.keras.applications import VGG16
from tensorflow.keras.preprocessing.image import ImageDataGenerator
model = models.Sequential()
# Convolutional base (feature extractor)
model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3)))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
# Deep feed-forward classifier
model.add(layers.Flatten())
model.add(layers.Dense(64, activation='relu'))
model.add(layers.Dense(10, activation='softmax'))
model.compile(loss='sparse_categorical_crossentropy', optimizer=optimizers.RMSprop(lr=1e-4), metrics=['acc'])
history = model.fit(
x=train_images,
y=train_labels,
steps_per_epoch=100,
epochs=5,
verbose=1,
validation_data=(test_images, test_labels),
validation_steps=50)
OUTPUT:
Train on 50000 samples, validate on 10000 samples
Epoch 1/5
50000/50000 [==============================] - 28s 564us/sample - loss: 2.1455 - acc: 0.2945 - val_loss: 2.0011 - val_acc: 0.3038
You should remove steps_per_epoh and validation_steps and use batch_size params.
Related
I'm having some problems which I cannot solve on my own. I'm pretty new to ML and Sequential Models of Keras.
Problem:
I only get NaN for loss and accuracy during fit()
Further when I try to predict, I'm just getting NaNs for the prediction.
My data is defined as datas(85802, 223) inclusive target
*UPDATE:
The problem was, that I did new NaN values after clearing the NaNs.
datas.dropna(inplace=True) <===== This have switched
labels = datas.columns
datas['target'] = datas['close'].shift(-4) <==== With this
Pretty dumb issue... Thank you guys!
import pandas as pd
import numpy as numpy
from keras.layers import Dense, Dropout, Activation, Flatten,Reshape
from keras.layers import Conv1D, MaxPooling1D, LeakyReLU
from keras.utils import np_utils
from keras.layers import GRU,CuDNNGRU
from keras.callbacks import CSVLogger, ModelCheckpoint
from sklearn.model_selection import train_test_split
from tensorflow.keras.layers.experimental import preprocessing
import matplotlib.pyplot as plt
from tensorflow import keras
from tensorflow.keras.models import Sequential
import tensorflow as tf
import h5py
import os
import pandas as pd
datas = pd.read_csv("D:/freqtrade/user_data/ML/New_Approach_16_08_2021/csv/ADA_USDT.csv")
datas.drop(columns=["date"], inplace=True)
print(datas.isna().sum())
datas.dropna(inplace=True)
labels = datas.columns
datas['target'] = datas['close'].shift(-4)
target = datas['target']
datas = datas[:].values
X_train, X_test, y_train, y_test = train_test_split(datas, target, test_size=0.33)
X_train = tf.convert_to_tensor(X_train, dtype=tf.float32)
X_test = tf.convert_to_tensor(X_test, dtype=tf.float32)
def build_and_compile_model():
model = Sequential()
model.add(Dense(200, input_shape=(222,), activation="relu"))
# model.add(Dropout(0.2))
model.add(Dense(180, activation="tanh"))
model.add(Dense(100, activation="relu"))
model.add(Dense(64, activation='relu'))
model.add(Dense(12, activation='relu'))
model.add(Dense(1))
model.compile(loss='mse', metrics=['accuracy'],
optimizer='adam')
return model
dnn_model = build_and_compile_model()
history = dnn_model.fit(
X_train, y_train,
validation_split=0.2,
verbose=1, epochs=2, batch_size=32)
dnn_model.summary()
history.model.save('dnn_model2')
X_test.columns = labels
test_predictions = history.model.predict(X_test)
print(test_predictions)
1438/1438 [==============================] - 6s 4ms/step - loss: nan - accuracy: 0.0000e+00 - val_loss: nan - val_accuracy: 0.0000e+00
Epoch 2/2
1438/1438 [==============================] - 6s 4ms/step - loss: nan - accuracy: 0.0000e+00 -
I've been researching this issue and while there seems to be references to it, I wasn't able to to get it to work.
I'm using keras flow_from_dataframe. My file names and labels are stored in two dataframes, one for train (df) and one for validation (dfv).
As I train my model the validation accuracy is at .75. I can verify that looking at model.history. Even model.evaluate_generator will confirm the exact same values.
But when I do a model.predict_generator using the exact same validation dataset provided to .fit and model.evaluate_generator, I get essentially randomness (.5), which tells me that either something with the order of observation is going on, or that there might be parts of the pipeline that are not carried on to the evaluation part (like rescaling).
Here's my code:
datagen=ImageDataGenerator(rescale=1./255.)
train_generator=datagen.flow_from_dataframe(
dataframe=df,
directory=path+'\\img.tar\\img',
x_col="loc",
y_col="target",
weight_col=None,
target_size=(32, 32),
color_mode="rgb",
classes=None,
class_mode="binary",
batch_size=100,
shuffle=True,
seed=SEED,
save_to_dir=None,
save_prefix="",
save_format="png",
subset=None,
interpolation="nearest",
validate_filenames=True
)
valid_generator=datagen.flow_from_dataframe(
dataframe=dfv,
directory=path+'\\img.tar\\img',
x_col="loc",
y_col="target",
weight_col=None,
target_size=(32, 32),
color_mode="rgb",
classes=None,
class_mode="binary",
batch_size=100,
shuffle=False,
seed=SEED,
save_to_dir=None,
save_prefix="",
save_format="png",
subset=None,
interpolation="nearest",
validate_filenames=True
)
#Found 8500 validated image filenames belonging to 2 classes.
#Found 500 validated image filenames belonging to 2 classes.
from keras.models import Sequential
#Import from keras_preprocessing not from keras.preprocessing
from keras_preprocessing.image import ImageDataGenerator
from keras.layers import Dense, Activation, Flatten, Dropout, BatchNormalization
from keras.layers import Conv2D, MaxPooling2D
from keras import regularizers, optimizers
import pandas as pd
import numpy as np
model = Sequential()
model.add(Conv2D(32, (3, 3), padding='same',
input_shape=(32,32,3)))
model.add(Activation('relu'))
model.add(Conv2D(32, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Conv2D(64, (3, 3), padding='same'))
model.add(Activation('relu'))
model.add(Conv2D(64, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(512))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='adam',metrics=["accuracy"])
STEP_SIZE_TRAIN=train_generator.n//train_generator.batch_size
STEP_SIZE_VALID=valid_generator.n//valid_generator.batch_size
#STEP_SIZE_TEST=test_generator.n//test_generator.batch_size
hx = model.fit_generator(generator=train_generator,
steps_per_epoch=STEP_SIZE_TRAIN,
validation_data=valid_generator,
validation_steps=STEP_SIZE_VALID,
epochs=1
)
#Epoch 1/1
#85/85 [==============================] - 74s 871ms/step - loss: 0.6584 - accuracy: 0.6398 - val_loss: 0.6059 - val_accuracy: 0.7500
hx.history
#{'val_loss': [0.6059464812278748],
#'val_accuracy': [0.75],
#'loss': [0.6583675097016727],
#'accuracy': [0.6397647]}
model.evaluate_generator(generator=valid_generator,
steps=STEP_SIZE_VALID)
#[0.6059464812278748, 0.75]
pred=model.predict_generator(valid_generator,verbose=1,steps=STEP_SIZE_VALID)
roc_auc_score(dfv.label.values,pred.ravel())
#0.51
print(roc_auc_score(dfv.label.values,pred.ravel()>df['label'].mean()))
print(roc_auc_score(dfv.label.values,pred.ravel()>0.5))
#0.5
#0.5
I was following the 2 class image classification tutorial here and wanted to convert it into a multi-class classifier.
I am trying to train a model to predict the brand of a watch from 17 classes. My accuracy after 50 epochs is only 21.88% so I'm not really sure where I am going wrong or if I am even doing this right.
Here is my code:
All the images are in their own separate folders under the /data or /valid folders.
Ex: ../watch finder/data/armani
Ex2: ../watch finder/data/gucci
import numpy as np
import matplotlib.pyplot as plt
import os
import cv2
from keras.utils import np_utils
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D
from keras.layers import Activation, Dropout, Flatten, Dense
from keras import backend as K
import keras.optimizers
img_width, img_height = 210, 210
train_data_dir = 'C:/Users/Adrian/Desktop/watch finder/data'
validation_data_dir = 'C:/Users/Adrian/Desktop/watch finder/valid'
nb_train_samples = 4761
nb_validation_samples = 612
epochs = 50
batch_size = 16
model = Sequential()
model.add(Conv2D(32, (3, 3), input_shape=input_shape))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(32, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(64, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dense(64))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(17))
model.add(Activation('softmax'))
model.compile(loss='categorical_crossentropy',
optimizer='rmsprop',
metrics=['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(
train_data_dir,
target_size=(img_width, img_height),
batch_size=batch_size,
class_mode='categorical')
validation_generator = test_datagen.flow_from_directory(
validation_data_dir,
target_size=(img_width, img_height),
batch_size=batch_size,
class_mode='categorical')
model.fit_generator(
train_generator,
samples_per_epoch=nb_train_samples // batch_size,
epochs=epochs,
validation_data=validation_generator,
validation_steps=nb_validation_samples // batch_size)
This is my first epoch:
Epoch 1/50
18/18 [==============================] - 8s 422ms/step - loss: 4.1104 - accuracy: 0.0833 - val_loss: 2.8369 - val_accuracy: 0.0592
And this is my 50th/last epoch:
Epoch 50/50
18/18 [==============================] - 7s 404ms/step - loss: 2.4840 - accuracy: 0.2188 - val_loss: 3.0823 - val_accuracy: 0.1795
I am fairly certain I am doing something wrong here but I am really new to deep learning so I'm not sure what that something is.
All the help is appreciated.
Also, each brand of watch has between 300-400 images and each image size is the same at 210x210.
There seems to be nothing wrong with your approach on a high level.
Has training stopped on the 50th epoch, or is it still learning? If so, then you might need to increase the learning rate so that it trains faster.
You need to also try different architectures and start tuning the hyperparameters.
Another point I'd like to make is that you have a really small number of images. Try using an established architecture for which you can find pretrained models for. These can help you significantly boost your performance.
One final note is that since you have 17 classes, if your model were predicting at random, you'd get an accuracy of just under 6%. This means that your model is at least learning something.
In order to analyze data, I need loss for each of output dimensions, instead I get only one loss which i suspect is a mean of the losses for all output dimensions.
Any help to understand what is the loss I get and how to get separate loss for each output:
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
from scipy import stats
from keras import models
from keras.models import Sequential
from keras.layers import Dense, Dropout
from keras import optimizers
from sklearn.model_selection import KFold
siz=100000
inp0=np.random.randint(100, 1000000 , size=(siz,3))
rand0=np.random.randint(-100, 100 , size=(siz,2))
a1=0.2;a2=0.8;a3=2.5;a4=2.6;a5=1.2;a6=0.3
oup1=np.dot(inp0[:,0],a1)+np.dot(inp0[:,1],a2)+np.dot(inp0[:,2],a3)\
+rand0[:,0]
oup2=np.dot(inp0[:,0],a4)+np.dot(inp0[:,1],a5)+np.dot(inp0[:,2],a6)\
+rand0[:,1]
oup_tot=np.concatenate((oup1.reshape(siz,1), oup2.reshape(siz,1)),\
axis=1)
normzer_inp = MinMaxScaler()
inp_norm = normzer_inp.fit_transform(inp0)
normzer_oup = MinMaxScaler()
oup_norm = normzer_oup.fit_transform(oup_tot)
X=inp_norm
Y=oup_norm
kfold = KFold(n_splits=2, random_state=None, shuffle=False)
opti_SGD = SGD(lr=0.01, momentum=0.9)
model1 = Sequential()
for train, test in kfold.split(X, Y):
model = Sequential()
model.add(Dense(64, input_dim=X.shape[1], activation='relu'))
model.add(Dense(64, activation='relu'))
model.add(Dense(64, activation='relu'))
model.add(Dense(Y.shape[1], activation='linear'))
model.compile(loss='mean_squared_error', optimizer=opti_SGD)
history = model.fit(X[train], Y[train], \
validation_data=(X[test], Y[test]), \
epochs=100,batch_size=2048, verbose=2)
I get:
Epoch 1/100
- 0s - loss: 0.0864 - val_loss: 0.0248
Epoch 2/100
- 0s - loss: 0.0218 - val_loss: 0.0160
Epoch 3/100
- 0s - loss: 0.0125 - val_loss: 0.0091
I would like to know what is the loss i got now and how to get losses for each output dimension.
Pass a list of functions to the metrics argument in the compile function. See here: https://keras.io/metrics/#custom-metrics
import keras.backend as K
...
def loss_first_dim(y_true, y_pred):
return K.mean(K.square(y_pred[:, 0] - y_true[:, 0]))
model.compile(optimizer='rmsprop',
loss='binary_crossentropy',
metrics=[loss_first_dim])
...
Im a new in deeplearning. I have a X with 34 dimension, which are some stock technical indicator data. And Y is label, which is binary(1,-1) represent the stock is uptrend or downtrend.Here is my code.
import pandas as pd
import numpy as np
data = pd.read_csv('data/data_week.csv')
data.dropna(inplace=True)
x = data.loc[:, 'bbands_upperband':'turn_std_5']
y = data['label']
from keras.models import Sequential
from keras.layers import Dense, Activation
model = Sequential()
model.add(Dense(512, activation='relu', input_dim=34))
model.add(Dense(200, activation='relu'))
model.add(Dense(200, activation='relu'))
model.add(Dense(200, activation='relu'))
model.add(Dense(128, activation='relu'))
model.add(Dense(1, activation='relu'))
model.compile(optimizer='rmsprop',
loss='binary_crossentropy',
metrics=['accuracy'])
model.fit(x, y, epochs=2, batch_size=200)
231200/1041021 [=====>........................] - ETA: 59s - loss: 0.7098 - acc: 0.0086
232000/1041021 [=====>........................] - ETA: 59s - loss: 0.7087 - acc: 0.0086
However, the accuracy is less than 1%. I think this must has something wrong.
If you know please tell me and thank you very much!
For a binary classification model you should use sigmoid activation function in your last dense layer
model.add(Dense(1, activation='sigmoid'))
also, your classes must be (0,1) not (-1,1)