My guess is that at some point earlier in the session you used lr= rather than lr.set_value(). Once you do that, set_value won’t work again.
I did indeed. i’ll rewrite those sections and try again. thanks so much!
@jeremy I was going through your statefarm-sample notebook (after I tried my own…) and have several questions.
It gave me an error when I did
"model.compile(Adam(), loss=‘categorical_crossentropy’, metrics=[‘accuracy’])"
telling me that Adam is not defined. I went to the Keras website and corrected it to
"model.compile(optimizer=‘adam’, loss=‘categorical_crossentropy’, metrics=[‘accuracy’])"
and it was fine. I am simply wondering why your command didn’t work for me in case there is something interesting going on.
What is the difference between model.fit and model.fit_generator (the latter was used in your statefarm notebook)? I see keras does not have the function fit_generator, so was fit_generator defined in util which we imported at the very beginning?
I would also like to know where the function get_batches from? Is it from util class as well?
Why is that in the validation batch, batch_size is twice as much?
Thank you and @rachel for all the good work. Merry Christmas!
It’s imported by utils.py. So you’ll need to have imported that.
See the keras docs for details. In short, fit_generator takes a generator (e.g from get_batches()) as input, whereas fit() takes an array. fit_generator is used for data augmentation.
Yes - try searching for ‘get_batches’ in utils.py to see this.
Because validation doesn’t require back propagation, so can generally handle larger batch sizes in the same memory.
I’m trying to understand the Vgg16 model from lesson 1. In order to do so I edited the given code a little.
The original script plots the cats and dogs images with the class labels by using ‘plots’ from ‘utils’.
I tried to port that code snipped to matplotlib, but the image looks solarized. For example if I got two images loaded in a numpy array it has dimensions (2, 3, 300, 300).
Now, if I want to output an image with ‘matplotlib’ using ‘imshow’ I have to reshape the array first or else I get an error that the dimensions are wrong.
plt.imshow(np.rollaxis(imgs, 0, 3))
The problem with the code above is that the image looks solarized. How can I fix this?
The plot method defined in utils.py switches it back to RGB.
My favourite Python library for plotting is Plotly.
Allows you create great interactive plots in various formats, extremely quickly and easily. They do offer cloud options for easier sharing, I find the iPython notebook intergration brilliant.
if (ims.shape[-1] != 3):
ims = ims.transpose((0,2,3,1))
Did anyone run into this error with initializations
ImportError: cannot import name 'initializations' ---> 21 from keras import initializations 22 from keras.applications.resnet50 import ResNet50, decode_predictions, conv_block, identity_block 23 from keras.applications.vgg16 import VGG16
I can’t find initializations in keras documentation either. I see initializers but not initializations.
Is it possible to copy models or layers in Keras. For example in lesson 3 instead of defining a completely new model with the batchnorm layers included it would be nice if we could just do:
bn =  for layer in model.layers: if type(layer) is Dropout: bn.append(Dropout(.5)) bn.append(BatchNormalization()) else: bn.append(layer) model = Sequential(bn)
If I do this then the model.summary() has extra connected_to layers. For example:
convolution2d_209 (Convolution2D (None, 64, 224, 224) 1792 zeropadding2d_209 zeropadding2d_209
I can fit the model like this but what are practical implications? Is it fitting what I want or adding in extra nodes?
You can use copy_layer, copy_layers, copy_weights, and copy_model from utils.py. See the source to see how they work.
Does np_utils.to_categorical have the same result as OneHot Encoding?
I implemented DenseNet for a small data set that I have. In the reference implementation they used np_utils.to_categorical on CIFAR10 dataset to convert the labels to binary. I felt that with our OneHot encoding mechanism we achieved the same thing but would like to get expert opinion.
Its hard to tell since both return a 2 dimensional array with 1s and 0s.
I believe so - check the source to be sure though.
The code is different but the intent seems to be the same in to_categorical:
y = np.array(y, dtype=‘int’).ravel()
if not num_classes:
num_classes = np.max(y) + 1
n = y.shape
categorical = np.zeros((n, num_classes))
categorical[np.arange(n), y] = 1
One more question for you.
I wasn’t geting very good results on my DenseNet so I printed out my data. I am very suprised to see what get_data is doing.
My notebook code is as simple as :
val_data = get_data(path+'valid') plt.imshow(val_data)
And here is the get_data code
def get_batches_portrait(dirname, gen=image.ImageDataGenerator(), shuffle=False, batch_size=4, class_mode='categorical'): return gen.flow_from_directory(dirname, target_size=(540,270), class_mode=class_mode, shuffle=shuffle, batch_size=batch_size) def get_data(path): batches = get_batches_portrait(path, shuffle=False, batch_size=4, class_mode=None) return np.concatenate([batches.next() for i in range(batches.nb_sample)])
For some reason its taken my image and put some filter on it.
Any ideas what I might be missing?
Can you show the original image, to compare?
I would look at the maximum and minimum values of both images.
Images should generally be in [0,1] if float or [0,256) if int. And make sure no nans.
You also may have switched RGB order to BGR if you were using VGG, so if you did that make sure you also undo it when viewing the images. (img[…,::-1])
I created a separate folder and ran this code
def get_data(path): gen = image.ImageDataGenerator() batches = gen.flow_from_directory(path, target_size=(540,270), class_mode=None, shuffle=False, batch_size=2 ) result = np.concatenate([batches.next() for i in range(batches.nb_sample)]) print(result.shape) return result val_data = get_data(path+'imgflip')
plt.imshow(val_data) Shows the negative but
plt.imshow(val_data*255) shows the correct image. Even though if I printed val_data itself it shows values above 1
array([[[ 133., 131., 134.],
[ 127., 126., 131.],
[ 123., 124., 129.],
[ 113., 117., 126.],
[ 108., 115., 125.],
[ 99., 107., 118.],
[ 91., 101., 111.],
[ 88., 101., 110.],
[ 82., 95., 104.],
[ 76., 89., 98.],
[ 68., 78., 87.],
[ 60., 69., 74.],
I am going to train my model again on this muiltiplied number because I am not sure if the negative values will impact the model.
Let me know if you have any thoughts
You need to use
It’s just a plotting issue - unrelated to modeling.