Python and keras questions and tips

If I want to split Resnet50 at what point is that feasible. The last layer being dense 1000 has to change for dogscats to dense 2. I am not sure where and how to cut it.

In Resnet50 we have 3 layers at the bottom AveragePooling2D -> Flatten -> Dense the final layer.
In previous splits with vgg16 we cut on the Flatten layer and then add BatchNormalization etc

Or simply just remove the last dense layer and replace with Dense(2,activation=‘softmax’)

When I do this I get an error when I want to see if the layer is there using model.summary() :: tried to call xxxx but layer isn’t built.

Not sure what that means with Resnet50 model

Ok I guess I just bumped up against the Functional API

def finetune(self, batches):
    model = self.model
    model.layers.pop()
    for layer in model.layers: layer.trainable=False
    m = Dense(batches.nb_class, activation='softmax')(model.layers[-1].output)
    self.model = Model(model.input, m)
    self.model.compile(optimizer=RMSprop(lr=0.1), loss='categorical_crossentropy', metrics=['accuracy'])

I finally have a model compiled using the lines from this function and modifying them for application directly against the model defined in my notebook. I could not call this function from the model Resnet50 I imported. Perhaps because I only imported that by name.

I can now see in my model summary that the output is a 2way softmax called output.

Hi, My name is Gertjan Brouwer and I am new to ML and AI and also to this forum.

I’m trying to learn AI by using fast.ai and the Keras documentation, I am currently following this tutorial: Keras tutorial .
The first training worked fine and I good about 80% accuracy with 2 hours of training. But now I wanted to use VGG16 and expand my training to 14 classes by using the second tutorial on that page.

I tried changing the binary_crossentropy to categorical_crossentropy but that did not work, I also changed the last dense layer from 1 to 14 but I keep getting this error: valueerror error when checking model expected shape (none, 10) but got array with shape(0, 44927)) . This is the code I am currently using: gist .
I also tried changed the training_samples and validation_samples from 2 to 44927 but that gave me the same error with different shapes, I still think the problem lies with this piece of code:
train_labels = np.array( [0] * (nb_train_samples / 2) + [1] * (nb_train_samples / 2)) validation_data = np.load(open('bottleneck_features_validation.npy')) validation_labels = np.array( [0] * (nb_validation_samples / 2) + [1] * (nb_validation_samples / 2))

Another problem might be that my 14 class training data is not evenly spread, I have classes with 6000 images and also classes with only a 1000 but I figured that would work fine because it worked on the first model too.

At this point I am unsure on what to try, I am willing to even change everything and follow an other tutorial if you guys can point me to one that uses image classification with the VGG16 model.

Kind regards,

Gertjan Brouwer

You need to one hot encode your labels (should be a 44k x 14 array of 1s and 0s).

You can code one yourself or use OneHotEncoder from sklearn.preprocessing (create instance and use the fit_transform method).

Hi - I’m pretty new to programming in python and don’t really understand everything. Maybe someone can help :slight_smile:
When I called vgg.get_batches(…), I get the batches, but how do I know what these batches exactly are? What are all the properties of the batches (e.g how would I know that you can access filenames through the batches)?

@oskar, my recommendation would be to follow the code down the trail.

So in your jupyter notebook, after you have imported utils, make a new code line and run the following: ??get_batches

This is going to show you that it is returning a gen.flow_from_directory object. What is gen? Let’s look in the arguments to the function. Looks like it’s an image.ImageDataGenerator(). So now in your python notebook, you can type in ??image.ImageDataGenerator.flow_from_directory and run the code. You’ll find it returns a DirectoryIterator.

Well damn, what the hell is that? Time to bust out the ol’ trusty google. Searching ‘DirectoryIterator Keras’ will point you to the top link: https://github.com/fchollet/keras/blob/master/keras/preprocessing/image.py, which is the actual method. You could have run ??image.DirectoryIterator in your jupyter notebook, but there was not an imediately easy way to know it would belong to image and not image.ImageDataGenerator or what-have-you.

If you go to line 893, you’ll find the Directory Iterator, with all the attributes you could ever hope to know about! And that is, eventually, what get_batches returns to you.

Hope this helps, let me know if anything is still unclear!

@corbin, thank you for the answer, this helps a lot.

This happens when you accidentally install Keras 2 instead of Keras 1.2.