fastai / courses

fast.ai Courses
Apache License 2.0
5.6k stars 2.74k forks source link

cannot import name activity_l2 from keras.regularizers #146

Closed Jayoprell closed 7 years ago

Jayoprell commented 7 years ago

screenshot from 2017-08-18 15-57-41

Jayoprell commented 7 years ago

I' ve see this problem in the course forums and it has been solved now. The version of keras are caused the problem. So all you need to do is as follows: Make some changes to utils.py if you are using keras 2.0+: from keras.regularizers import l2, activity_l2, l1, activity_l1 ----> from keras.regularizers import l2, l1 from keras.utils.layer_utils import layer_from_config -----> from keras.layers import deserialize as layer_from_config

almandsky commented 7 years ago

I think you are using keras 2.0+. You can try install the keras 1.2.2

pip install keras=1.2.2

otherwise you may need to make some changes to the utils.py as @Jayoprell mentioned above. I also need to modify the vgg16.py to make it work in my laptop.

--- a/deeplearning1/nbs/vgg16.py
+++ b/deeplearning1/nbs/vgg16.py
@@ -174,7 +174,7 @@ class Vgg16():
                 batches : A keras.preprocessing.image.ImageDataGenerator object.
                           See definition for get_batches().
         """
-        self.ft(batches.nb_class)
+        self.ft(batches.num_class)
         classes = list(iter(batches.class_indices)) # get a list of all the class labels

         # batches.class_indices is a dict with the class name as key and an index as value
@@ -209,8 +209,8 @@ class Vgg16():
             Fits the model on data yielded batch-by-batch by a Python generator.
             See Keras documentation: https://keras.io/models/model/
         """
-        self.model.fit_generator(batches, samples_per_epoch=batches.nb_sample, nb_epoch=nb_epoch,
-                validation_data=val_batches, nb_val_samples=val_batches.nb_sample)
+        self.model.fit_generator(batches, samples_per_epoch=batches.samples, nb_epoch=nb_epoch,
+                validation_data=val_batches, nb_val_samples=val_batches.samples)

     def test(self, path, batch_size=8):
@@ -227,5 +227,5 @@ class Vgg16():

         """
         test_batches = self.get_batches(path, shuffle=False, batch_size=batch_size, class_mode=None)
-        return test_batches, self.model.predict_generator(test_batches, test_batches.nb_sample)
+        return test_batches, self.model.predict_generator(test_batches, test_batches.samples)
royshouvik commented 6 years ago

I faced the same issue on trying to run the lesson 1 notebook on Crestle. Here's what I did

  1. On a terminal pip install keras==1.2.2

  2. Restart Jupyter notebook.

  3. Verify Keras version using python -c 'import keras; print(keras.__version__)'

Hope it helps.

ethirajsrinivasan commented 6 years ago

image

In keras 2 even after changing nb_class to num_class i am getting the error as above

ak9250 commented 5 years ago

@royshouvik after doing pip install I am getting AttributeError: 'module' object has no attribute 'HiddenLayer'