Eu estava criando um classificador simples em Keras no Python3, ma seu fico recebendo a mesma mensagem de erro:
RuntimeError: You must compile your model before using it.
Segue o código:
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()