keras-team / keras-applications

Reference implementations of popular deep learning models.
Other
2k stars 910 forks source link

NameError: name 'keras_applications' is not defined #109

Closed denka0711 closed 4 years ago

denka0711 commented 5 years ago

my keras version is 2.2.4

from keras.applications import *

def model_InceptionResNetV2(img_dim, num_label):
    print('begin to get model')
    input_tensor = Input(shape=img_dim)
    base_model = InceptionResNetV2(include_top=False, input_shape=img_dim, weights='imagenet')
    x = input_tensor
    x = Lambda(preprocess_input, name='preprocessing')(x)
    x = base_model(x)
    x = GlobalAveragePooling2D()(x)
    x = Dropout(0.5)(x)
    print('num_label:', num_label)
    x = Dense(num_label, activation='softmax', name='softmax')(x)
    model = Model(input_tensor, x)

    return model
model.compile(optimizer=Adam(lr=1e-4), loss='binary_crossentropy', 
                      metrics = ['accuracy'])

        model.fit_generator(train_generator(), train_steps, epochs=epochs, verbose=1, 
                            callbacks= callbacks, validation_data=valid_generator(), 
                            validation_steps=valid_steps)
        print('here2')
        # save mode to JSON file and weights to HDF5
        model_json = model.to_yaml()
        model.load_weights(filepath='../model/aug_inception.fold_' + str(i) + attr_class + '.hdf5')
        #model_json = model.to_yaml()
        with open('../model/aug_inceptionresnetv2_model_' + str(i) + attr_class  + '.yaml', 'w') as json_file:
            json_file.write(model_json)

after I have trained mode, I want to load the mode as the following code:

load YAML and create model

yaml_file = open('../model/aug_inceptionresnetv2_model_1s.yaml', 'r')
loaded_model_yaml = yaml_file.read()
yaml_file.close()
loaded_model = model_from_yaml(loaded_model_yaml)
# load weights into new model
loaded_model.load_weights("../modelaug_inceptionresnetv2_model_1s.h5")

and there is error as following:

~/.local/lib/python3.6/site-packages/keras/engine/base_layer.py in call(self, inputs, kwargs) 455 # Actually call the layer, 456 # collecting output(s), mask(s), and shape(s). --> 457 output = self.call(inputs, kwargs) 458 output_mask = self.compute_mask(inputs, previous_mask) 459

~/.local/lib/python3.6/site-packages/keras/layers/core.py in call(self, inputs, mask) 685 if has_arg(self.function, 'mask'): 686 arguments['mask'] = mask --> 687 return self.function(inputs, **arguments) 688 689 def compute_mask(self, inputs, mask=None):

~/.local/lib/python3.6/site-packages/keras/layers/core.py in wrapper(*args, *kwargs) 21 22 def wrapper(args, **kwargs): ---> 23 if hasattr(keras_applications, 'get_submodules_from_kwargs'): 24 kwargs['backend'] = backend 25 kwargs['layers'] = layers

NameError: name 'keras_applications' is not defined

I don't know how to solve it, could you guys help me

taehoonlee commented 5 years ago

@denka0711, Please let me know the versions of keras-applications. Maybe, in my guess, the problem will disappear if you install the latest versions. Please pip install -U keras-applications.

dayangkunurfaizah commented 4 years ago

NameError: name 'applications' is not defined

I have installed keras-applicaitions, but somehow my problem is similar.

Im using 1.12.0 version. Is it the latest version?