Coder Social home page Coder Social logo

Comments (12)

jasmcaus avatar jasmcaus commented on August 11, 2024

Hey @zaraken! Thanks for this. This is usually a problem encountered one when performing the train-test split.
In my video, I used the caer.train_val_split(). However, this is now deprecated.

Alternatively, you may use sklearn's method sklearn.model_selection.train_test_split() to do the same.
Ensure, however, that you use something similar to

X_train, X_test, y_train, y_test = sklearn.model_selection.train_test_split(X, y, test_size=0.2)

Hope this helps :)

from opencv-course.

VibhuAg avatar VibhuAg commented on August 11, 2024

@zaraken I resolved this issue by converting x_train and y_train to numpy arrays.
x_train = np.array(x_train)
y_train = np.array(y_train)
train_gen = datagen.flow(x_train, y_train, batch_size=BATCH_SIZE)

I also encountered an issue later in the tutorial which I solved using the same method, by converting x_val and y_val to numpy arrays. The error message I got that time had a message like this: "ValueError: Data cardinality is ambiguous:"
I'm not sure if these fixes affect the accuracy of the model.

from opencv-course.

syahmi001 avatar syahmi001 commented on August 11, 2024

Hello and Hi!

I followed exactly your code and somehow my model's accuracy is not improving.

Epoch 1/10
345/345 [==============================] - 11s 33ms/step - loss: 13.7243 - accuracy: 0.1000 - val_loss: 13.7243 - val_accuracy: 0.1000
Epoch 2/10
345/345 [==============================] - 10s 29ms/step - loss: 13.7243 - accuracy: 0.1000
Epoch 3/10
345/345 [==============================] - 10s 29ms/step - loss: 13.7243 - accuracy: 0.1000
Epoch 4/10
345/345 [==============================] - 10s 28ms/step - loss: 13.7243 - accuracy: 0.1000
Epoch 5/10
345/345 [==============================] - 10s 30ms/step - loss: 13.7243 - accuracy: 0.1000
Epoch 6/10
345/345 [==============================] - 11s 31ms/step - loss: 13.7243 - accuracy: 0.1000
Epoch 7/10
345/345 [==============================] - 10s 29ms/step - loss: 13.7243 - accuracy: 0.1000
Epoch 8/10
345/345 [==============================] - 11s 31ms/step - loss: 13.7243 - accuracy: 0.1000
Epoch 9/10
345/345 [==============================] - 10s 28ms/step - loss: 13.7243 - accuracy: 0.1000
Epoch 10/10
345/345 [==============================] - 9s 27ms/step - loss: 13.7243 - accuracy: 0.1000

I even tried the sklearn method but still to no avail.
Any clue to solve this?

from opencv-course.

jasmcaus avatar jasmcaus commented on August 11, 2024

Hi @syahmi001, it appears that your model is overfitting. By a lot. Physically, it can't improve further (its already at 100%). Possible reasons for this:

  1. Your dataset is too small
  2. The images in your dataset are too very similar to each other

If you can't change the above, try modifying the learning rate, decay and momentum values (as discussed in the course). Sometimes, this may help. But overall, it's safe to say that your model is too specific. Generalize it by adding more data (or better yet, augment that data by using a library like caer).

Let me know if this helps :)

from opencv-course.

harryhancock avatar harryhancock commented on August 11, 2024

Hi @jasmcaus, thanks for your help!

The syntax change did fix the problem, however the accuracy is now only at 27%:

Epoch 8/10
345/345 [==============================] - 9s 27ms/step - loss: 0.3015 - accuracy: 0.2612
Epoch 9/10
345/345 [==============================] - 9s 27ms/step - loss: 0.2989 - accuracy: 0.2718
Epoch 10/10
345/345 [==============================] - 9s 27ms/step - loss: 0.2976 - accuracy: 0.2762

Did anyone find a way to fix this and get back to near 70% or 100?

from opencv-course.

jasmcaus avatar jasmcaus commented on August 11, 2024

There's no "real way" per se to get a model to generalize well, but you can try playing around with the learning_rate, decay and momentum.

From the output, it is clear that your model is stuck (also called overfitting). Generalize it by adding more data (or better yet, augment that data by using a library like caer).

Let me know if that helps :)

from opencv-course.

harryhancock avatar harryhancock commented on August 11, 2024

Hey @jasmcaus thanks for the help. I followed the tutorial on youtube where they got 70% so I got confused as to why now we get only 27 % even with using the caer library as in the tutorial.

Failing that, do you know what the best values for parameters are, as i assumed they were already the best from the video?

from opencv-course.

harryhancock avatar harryhancock commented on August 11, 2024

Also @jasmcaus new changes to the part 4 file now produce error:


AttributeError Traceback (most recent call last)
in
1 # Do note that val_ratio is now test_size.
----> 2 split_data = sklearn.model_selection.train_test_split(featureSet, labels, test_size=.2)
3 x_train, x_val, y_train, y_val = (np.array(item) for item in split_data)
4
5

AttributeError: module 'sklearn' has no attribute 'model_selection'

Make sure to run the code before uploading to github to check it works! :)

from opencv-course.

jasmcaus avatar jasmcaus commented on August 11, 2024

Also @jasmcaus new changes to the part 4 file now produce error:

AttributeError Traceback (most recent call last)
in
1 # Do note that val_ratio is now test_size.
----> 2 split_data = sklearn.model_selection.train_test_split(featureSet, labels, test_size=.2)
3 x_train, x_val, y_train, y_val = (np.array(item) for item in split_data)
4
5

AttributeError: module 'sklearn' has no attribute 'model_selection'

Make sure to run the code before uploading to github to check it works! :)

Works fine on my system tbh, but I've gone ahead and added a small fix to the code. Sklearn doesn't seem to like a direct call to model_selection (see https://stackoverflow.com/questions/53742441/attributeerror-module-sklearn-has-no-attribute-model-selection)

from opencv-course.

jasmcaus avatar jasmcaus commented on August 11, 2024

Hey @jasmcaus thanks for the help. I followed the tutorial on youtube where they got 70% so I got confused as to why now we get only 27 % even with using the caer library as in the tutorial.

Failing that, do you know what the best values for parameters are, as i assumed they were already the best from the video?

Well, like I mentioned in my previous reply, there is no best set of parameters. Training Deep Learning models with a reasonable degree of accuracy comes with a bit of practice (it takes time!).

A quick pointer: when you train the model, observe the accuracy/validation accuracy. If it stays roughly at the same value (whether 27% or 99%), your model is too specific and needs to be generalized.

Apart from the two tips l I'd given you earlier (get more data and apply augmentation with caer), you could also try reducing the number of categories you train on. I've used the top 10 in the course, but try it with the top 5. While you're sacrificing on the overall size of the dataset, you are ensuring your model focuses on a fewer number of categories (which each have ~1000-2000 images).

from opencv-course.

jasmcaus avatar jasmcaus commented on August 11, 2024

Also, I suggest opening up a new issue as your question doesn't quite fall under the topic of the current issue

from opencv-course.

jasmcaus avatar jasmcaus commented on August 11, 2024

Given there hasn't been any activity on this issue, I will be closing it.
Feel free to re-open it if need be.

from opencv-course.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.