Read a number of similar questions, most of them mentioned that you shouldn't try to serialize an unserializable object. I am not able to understand the issue. I am able to save the model as .h5 file but that doesn't serve the purpose of what I am trying to do. Please Help!
def image_generator(train_data_dir, test_data_dir):
train_datagen = ImageDataGenerator(rescale=1/255,
rotation_range = 30,
zoom_range = 0.2,
width_shift_range=0.1,
height_shift_range=0.1,
validation_split = 0.15)
test_datagen = ImageDataGenerator(rescale=1/255)
train_generator = train_datagen.flow_from_directory(train_data_dir,
target_size = (160,160),
batch_size = 32,
class_mode = 'categorical',
subset='training')
val_generator = train_datagen.flow_from_directory(train_data_dir,
target_size = (160,160),
batch_size = 32,
class_mode = 'categorical',
subset = 'validation')
test_generator = test_datagen.flow_from_directory(test_data_dir,
target_size=(160,160),
batch_size = 32,
class_mode = 'categorical')
return train_generator, val_generator, test_generator
def model_output_for_TL (pre_trained_model, last_output):
x = Flatten()(last_output)
# Dense hidden layer
x = Dense(512, activation='relu')(x)
x = Dropout(0.2)(x)
# Output neuron.
x = Dense(2, activation='softmax')(x)
model = Model(pre_trained_model.input, x)
return model
train_generator, validation_generator, test_generator = image_generator(train_dir,test_dir)
pre_trained_model = InceptionV3(input_shape = (160, 160, 3),
include_top = False,
weights = 'imagenet')
for layer in pre_trained_model.layers:
layer.trainable = False
last_layer = pre_trained_model.get_layer('mixed5')
last_output = last_layer.output
model_TL = model_output_for_TL(pre_trained_model, last_output)
model_TL.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
history_TL = model_TL.fit(
train_generator,
steps_per_epoch=10,
epochs=10,
verbose=1,
validation_data = validation_generator)
pickle.dump(model_TL,open('img_model.pkl','wb'))
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…