keras-team / keras

Deep Learning for humans
http://keras.io/
Apache License 2.0
62.14k stars 19.49k forks source link

Nasnet weight errors (no_top version) #12013

Closed moondra2017 closed 3 years ago

moondra2017 commented 5 years ago

I"m running into this error:

ValueError: You are trying to load a weight file containing 532 layers into a model with 526 layers.

My keras version is:

>>> keras.__version__ '2.2.4'

`Tensorflow version:

'1.12.0'`

Bascially, when the model tries to load I get this value error ValueError: You are trying to load a weight file containing 532 layers into a model with 526 layers.

I"ve looked at this thread:

https://github.com/keras-team/keras/issues/10109

However, I"m trying to run the no_top version, so It shouldn't matter what my input vector is.

base_model(weights='imagenet', include_top=False)

Thank you.

Here is the script Im using:

   ################IMPORTS########################

#--Keras imports--#

from keras.applications import resnet50, xception,inception_v3,inception_resnet_v2, densenet, nasnet, imagenet_utils
from keras.preprocessing import image
from keras.preprocessing.image import ImageDataGenerator
from keras.layers import Input, Dense
from keras import backend as k
from keras.models import Model, clone_model
from keras.layers import Dense, GlobalAveragePooling2D,Dropout, BatchNormalization
from keras import optimizers
from keras.optimizers import Adam
from keras.callbacks import ModelCheckpoint,ProgbarLogger
from keras.utils import print_summary
from keras import __version__
from keras.datasets import cifar10

#--python imports--#
import os
import numpy as np
import datetime
import h5py
import json
import time

################# -- PARAMETERS -- ##################################

img_width, img_height = 480, 480
(x_train, y_train), _ = cifar10.load_data()
classes = len(y_train[0])

##------initial training parameters -----##

i_epochs = 10
i_batch_size = 20
i_steps_per_epoch = 100
i_optimizer = optimizers.SGD(lr=0.0001, momentum=0.9)

#################### MODELS ######################################

def basemodel():

    base_model = nasnet.NASNetLarge(weights='imagenet', include_top=False)
    preprocess = nasnet.preprocess_input
    return base_model, preprocess

def full_model():
    base_model, preprocess = basemodel()
    x = base_model.output
    x = GlobalAveragePooling2D()(x)
    x = Dense(2048, activation='relu')(x)
    x = Dropout(.60)(x)
    x=  Dense(512, activation='relu')(x)
    predictions = Dense(classes, activation='softmax')(x)
    model = Model(inputs= base_model.input, outputs=predictions)
    return model,preprocess

def initial_training_full():
    model, preprocess = full_model()
    for layer in model.layers[:-5]:
        layer.trainable = False

    model.compile(optimizer= i_optimizer,
              loss='sparse_categorical_crossentropy', metrics = ['accuracy'])

    print('Starting model training')

    history = model.fit(x_train, y_train,
    steps_per_epoch = i_steps_per_epoch,
    epochs = i_epochs,
    shuffle= True,
    verbose = 1)
    return history

if __name__ == "__main__":
    initial_training_full()
gabrieldemarmiesse commented 5 years ago

Can you write the python script that you ran which gives the error?

moondra2017 commented 5 years ago

Sure, It"s a long script so I will try to create a reproducible version and paste it here. Thank you.

moondra2017 commented 5 years ago

OK, Ive added the script Im using in the OP. Thank you.

gabrieldemarmiesse commented 5 years ago

Can you try to reduce the size of the script while preserving the bug? It's much easier to debug small scripts than big ones. Thanks!

moondra2017 commented 5 years ago

Sorry for the late reply, Ive updated the OP with a self-contained example. The data being used is the cifar data set. With teh above code Im still running into the same error. Thank you.

gabrieldemarmiesse commented 5 years ago

Can you try with the latest version from master of keras and keras_applications? You can do it with pip install git+{url of the github repo here}. Maybe it has already been fixed since 2.2.4.

moondra2017 commented 5 years ago

Hi. I"m trying pip install https://github.com/keras-team/keras but I"m running into this error:

Cannot unpack file C:\Users\yomog\AppData\Local\Temp\pip-unpack-lao25qho\keras (downloaded from C:\Users\yomog\AppData\Local\Temp\pip-req-build-rwmbrd1f, content-type: text/html; charset=utf-8); cannot detect archive format

I"m using Windows 10 btw. Thank you.

gabrieldemarmiesse commented 5 years ago

You need to use git+.

moondra2017 commented 5 years ago

Hi, so I"ve installed the lastest keras version following your instructions, and I"m still running into the same error.

gabrieldemarmiesse commented 5 years ago

Thanks! @taehoonlee can you please have a look? It seems there are problems with the imagenet weights in NasNet with include_top set to False.

erfaneshrati commented 5 years ago

I have the same problem!

taehoonlee commented 5 years ago

@moondra2017, @erfaneshrati, The architectures of NASNetMobile and NASNetLarge differ depending on whether input_shape is even or odd, while the architectures of the other networks are independent to input_shape.

Currently, you must designate input_shape as (224, 224, 3) for NASNetMobile or (331, 331, 3) for NASNetLarge. And now, the issue has been resolved in keras-team/keras-applications#62. The patch will be included in a new release.