I am new to stackoverflow and deep learning.I have been following a example of multi label image classification from below.
https://www.analyticsvidhya.com/blog/2019/04/build-first-multi-label-image-classification-model-python/
I reduced the images to 500...with 2 epochs...It gives accuracy of around 72% in test set and training test.This I did because I was getting memory error.
But the predictions are very very poor...I took a image from training set..Even for that the prediction is completely wrong...
Please can you help me out.Apologies in case I missed something from my end.
Thanks.
#Jankey, 2 epochs is not enough to properly train a neural network, you can start with 50 for MNIST. If you have memory issues, try to decrease batch size to 8 or 16.
model.fit(X_train, y_train, epochs=10, validation_data=(X_test, y_test), batch_size=16)
You can also decrease the number of convolution filters to 32 and number of nodes in Dense layer to 32. Try to make it more efficient according to your infrastructure limitations.
Thanks for suggestion and guidance .I did the following changes.
1) model.fit(X_train, y_train, epochs=10, validation_data=(X_test, y_test), batch_size=32)
2) kept two convolutional layers with 32 filters
3) Change input shape as (64,64,3) tested then changed to (224,224,3).
With these I am able to get better predictions.Not 100%.But far better.
Will try to tune and test the model further.
Again Thanks for quick help.
Related
currently i am working with LSTM and GRU Modells.
I applied them on a Multivariate Time Series Problem.
reset_random_seeds()
# design network weekly
model = Sequential()
#inputshape is using time stamps , feautures
model.add(LSTM(64,activation="relu" ,dropout=0.2, input_shape=(1, 19)))
model.add(Dense(20))
model.add(Dense(1))
model.compile(loss=root_mean_squared_error, optimizer='adam')
# fit network
history = model.fit(train_X, train_y, epochs=300, batch_size=258, validation_data=(test_X, test_y), verbose=2, shuffle=False)
# plot history
pyplot.plot(history.history['loss'], label='train')
pyplot.plot(history.history['val_loss'], label='test')
pyplot.legend()
pyplot.show()
enter image description here
The Loss Validation looks like this and the results are pretty good to but i wonder what this graphs means to me.
Results:
enter image description here
enter image description here
So at first this loss graph tells you how high the cumulated error for each example in your validation or training dataset is. So the loss value implies how good or bad your model is after each training iteration.
You loss graph shows us, that you have a much higher training loss than validation loss. This should be a good indicator, that your model at least might not be overfitting. On the other hand it also can be an indication, that your validation dataset might be very small or not that representativ. For example your validation dataset could be very skewed.
So we would have to look into your datasets a little deeper to fully interpret your loss graphes. If you can provide them I would be happy to have a look.
I can't obtain history from model.fit_generator()
net_history=densenet_model.fit_generator(
generator=train_loader,
steps_per_epoch=len(train_dataset),
max_queue_size=500,
workers=1,
validation_data=val_loader,
epochs=10,
validation_steps=len(val_dataset),
verbose=0,
callbacks=[checkpointer, tensorboard]
)
print(net_history.history)
print(net_history.history.keys())
result:
{}
dict_keys([])
But I can obtain history information from model.fit()
net_history2=densenet_model.fit(
x_train,
y_train,
validation_split=0.3,
epochs=4,
verbose=0,
batch_size=4,
callbacks=[checkpointer, tensorboard]
)
print(net_history2.history)
result:
{'val_loss': [1.0166720993050904, 0.8762002832421633, 0.9079110455290179, 0.8696109705439238],
'val_categorical_crossentropy': [0.8133353590965271, 0.677170991897583, 0.7131518721580505,
0.6792631149291992], 'val_categorical_accuracy': [0.5887850522994995, 0.6355140209197998,
0.5794392228126526, 0.6074766516685486], 'loss': [0.8654926233446067, 0.8416075144219495,
0.8553338176325748, 0.8491003025881192], 'categorical_crossentropy': [0.6599225, 0.6403823,
0.6583931, 0.6564969], 'categorical_accuracy': [0.6396761, 0.659919, 0.5991903, 0.562753]}
Why does this happen?
How can I get the loss and accuracy information from model.fit_generator?
ps: I failed to visualize iterative process through log event because each time I try to open it in tensorboard, the browser gets stuck in 'namespace hierarchy finding similar subgraphs' and then crashes... QAQ
You can stick with model.fit becuase it supports generators.
For TensorBoard you may want to wait a little bit longer (I thought that tensorboard stucked in 'namespace hierarchy finding similar subgraphs' but it was running and it took some time).
Note: for training and validation steps you should get the length of the generators which is equal to trainORval_dataset_size / batch size, not the length of the dataset. You don't want the same images to be repeated in the same epoch.
steps_per_epoch=len(train_loader)
validation_steps=len(val_loader)
As for why exactly this is happening I can only speculate that it is because model.fit_generator is deprecated, in TensorFlow 2.2 and higher you can just use model.fit because this now supports generators.
Source:
https://www.tensorflow.org/api_docs/python/tf/keras/Model#fit_generator
I am a bit confused on how Keras fits the models. In general, Keras models are fitted by simply using model.fit(...) something like the following:
model.fit(X_train, y_train, epochs=300, batch_size=64, validation_data=(X_test, y_test))
My question is: Because I stated the testing data by the argument validation_data=(X_test, y_test), does it mean that each epoch is independent? In other words, I understand that at each epoch, Keras train the model using the training data (after getting shuffled) followed by testing the trained model using the provided validation_data. If that's the case, then no matter how many epochs I choose, I only take the results of the last epoch!!
If this scenario is correct, so we do we need multiple epoches? Unless these epoches are dependent somwhow where each epoch uses the same NN weights from the previous epoch, correct?
Thank you
When Keras fit your model it pass throught all the dataset at each epoch by a step corresponding to your batch_size.
For exemple if you have a dataset of 1000 items and a batch_size of 8, the weight of your model will be updated by using 8 items and this until it have seen all your data set.
At the end of that epoch, the model will try to do a prediction on your validation set.
If we have made only one epoch, it would mean that the weight of the model is updated only once per element (because it only "saw" one time the complete dataset).
But in order to minimize the loss function and by backpropagation, we need to update those weights multiple times in order to reach the optimum loss, so pass throught all the dataset multiple times, in other word, multiple epochs.
I hope i'm clear, ask if you need more informations.
I'm using Keras with the Tensorflow back end to train a CNN, and I'm using tensorboard to visualize the loss functions and accuracy. I would like to see the loss function of both the training data and validation data on the same graph, but I've only found ways to do so when using Tensorflow and not through keras.
Is there a way to do so?
Edit 1:
I tried writing loss/acc in the Regex but instead of putting both of the graphs together it shows them side by side like so:
http://imgur.com/a/oLIcL
Ive added what I use to log to tensor board:
tbCallBack=keras.callbacks.TensorBoard(log_dir='C:\\logs', histogram_freq=0, write_graph=False, write_images=True, embeddings_freq=0, embeddings_layer_names=None, embeddings_metadata=None)
model.fit_generator(train_generator,
steps_per_epoch=x_train.shape[0] // batch_size,
epochs=epochs,
validation_data=(x_test, y_test))
You can add a regex in the text box in the upper left corner of the Tensorboard window.
Add acc for accuracy of both train/validation data. Add lossfor the loss values. This works for me for Keras as well as Tensorflow.
Got this from this nice tutorial on TB: https://www.youtube.com/watch?v=eBbEDRsCmv4
As a code snippet I use this:
logdir = "_tf_logs/" + now.strftime("%Y%m%d-%H%M%S") + "/"
tb = TensorBoard(log_dir=logdir)
callbacks=[tb]
...
model.fit(X_train, Y_train, validation_data=val_data, epochs=10, verbose=2, callbacks=callbacks)
I found this from Github for this exact purpose but without using tensorboard. Hope this helps!
live loss plot for keras
I am training a model using Keras.
model = Sequential()
model.add(LSTM(units=300, input_shape=(timestep,103), use_bias=True, dropout=0.2, recurrent_dropout=0.2))
model.add(Dense(units=536))
model.add(Activation("sigmoid"))
model.compile(loss="binary_crossentropy", optimizer="adam", metrics=["accuracy"])
while True:
history = model.fit_generator(
generator = data_generator(x_[train_indices],
y_[train_indices], batch = batch, timestep=timestep),
steps_per_epoch=(int)(train_indices.shape[0] / batch),
epochs=1,
verbose=1,
validation_steps=(int)(validation_indices.shape[0] / batch),
validation_data=data_generator(
x_[validation_indices],y_[validation_indices], batch=batch,timestep=timestep))
It is a multiouput classification accoriding to scikit-learn.org definition:
Multioutput regression assigns each sample a set of target values.This can be thought of as predicting several properties for each data-point, such as wind direction and magnitude at a certain location.
Thus, it is a recurrent neural network I tried out different timestep sizes. But the result/problem is mostly the same.
After one epoch, my train loss is around 0.0X and my validation loss is around 0.6X. And this values keep stable for the next 10 epochs.
Dataset is around 680000 rows. Training data is 9/10 and validation data is 1/10.
I ask for intuition behind that..
Is my model already over fittet after just one epoch?
Is 0.6xx even a good value for a validation loss?
High level question:
Therefore it is a multioutput classification task (not multi class), I see the only way by using sigmoid an binary_crossentropy. Do you suggest an other approach?
I've experienced this issue and found that the learning rate and batch size have a huge impact on the learning process. In my case, I've done two things.
Reduce the learning rate (try 0.00005)
Reduce the batch size (8, 16, 32)
Moreover, you can try the basic steps for preventing overfitting.
Reduce the complexity of your model
Increase the training data and also balance each sample per class.
Add more regularization (Dropout, BatchNorm)