Problem compiling code in Keras

Asked

Viewed 68 times

0

I was creating a simple classifier in Keras in Python3, but its getting the same error message:

Runtimeerror: You must Compile your model before using it.

Follows the code:

import keras
from keras.layers import Activation, MaxPooling2D, Convolution2D
from keras.layers import Dense, Flatten, Dropout
from keras.preprocessing.image import ImageDataGenerator


class OwnClassificator:
    def __init__(self):

# Preparando os dados de treino

        self.preparing_train = ImageDataGenerator(rescale=1. /225, 
           shear_range=0.2, zoom_range=0.2, horizontal_flip=True)

        self.data_train = self.preparing_train.flow_from_directory(
           directory=r'C:Meu diretório', 
           target_size=(200, 200), batch_size=15,
           class_mode='binary')

# Preparando os dados de validação

        self.validation_preparing = ImageDataGenerator(rescale=1. /225, 
           shear_range=0.2, zoom_range=0.2, horizontal_flip=True)

        self.data_validation =self.validation_preparing.flow_from_directory(
            directory=r'C:meu diretório', target_size=(200, 200), batch_size=10, class_mode='binary')

    # **************************************************************************************************************

    self.model = keras.Sequential()


    self.model.add(Convolution2D((32, 3, 3), kernel_size=15))
    self.model.add(Activation('relu'))
    self.model.add(MaxPooling2D(pool_size=(2, 2)))

    self.model.add(Convolution2D((32, 3, 3), kernel_size=15))
    self.model.add(Activation('relu'))
    self.model.add(MaxPooling2D(pool_size=(2, 2)))

    self.model.add(Convolution2D((32, 3, 3), kernel_size=15))
    self.model.add(Activation('relu'))
    self.model.add(MaxPooling2D(pool_size=(2, 2)))

    # **************************************************************************************************************

    self.model.add(Flatten())
    self.model.add(Dense(64))
    self.model.add(Activation('relu'))
    self.model.add(Dropout(0.5))

    self.model.add(Dropout(1))
    self.model.add(Activation('sigmoid'))

    # **************************************************************************************************************

    self.model.compile(loss='binary_crossentropy', optimizer='rmsprop', metrics=['accuracy'])

    self.model.fit_generator(generator=self.data_train, steps_per_epoch=554 // 15, epochs=80,
                validation_data=self.data_validation)

    self.model.save_weights('models/simple_CNN.h5')


    # **************************************************************************************************************

    self.img = keras.preprocessing.image.load_img(r'C:meu diretorio',
                                                  target_size=(200, 200))
    self.predict = self.model.predict(self.img)
    print(f'PREDICTION: {self.predict}')


FirstModel = OwnClassificator() 

1 answer

1

This code is badly indented and does not execute. In python, this is a deadly error.

The first thing you need to do is fix indentation. I get the error:

NameError: name 'self' is not defined

Second, for simple models, a class-based implementation is not the smartest.

Follow the Python zen:

  • Flat is Better than nested.

Try to make a shallow script only with the code you want to run. Using local variables to script.

Use this example Keras as a reference. See also the documentation of flow_from_directory.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.