<jupyter_start><jupyter_text>Train Model<jupyter_code>base_model = Xception(input_shape=(299, 299, 3), include_top=False, weights='imagenet')
train_datagen = ImageDataGenerator(
        rescale=1./255,
        shear_range=0.2,
        zoom_range=0.2,
        horizontal_flip=True
)

train_generator = train_datagen.flow_from_directory(
    path_to_data,  # this is the target directory
    target_size=(299, 299),  # all images will be resized to 299x299
    batch_size=batch_size,
    shuffle=True,
    class_mode='categorical'
)

# Get the indices, we'll use these when testing (associate output with pokemon name)
class_indices = train_generator.class_indices

import json
with open(model_path + '/class_indices.json', 'w') as outfile:
    json.dump(class_indices, outfile)
validation_datagen = ImageDataGenerator(
    rescale=1. / 255,
    shear_range=0.2,
    zoom_range=0.2,
    horizontal_flip=True
)

validation_generator = validation_datagen.flow_from_directory(
    path_to_validation_data,
    target_size=(299, 299),
    batch_size=batch_size,
    shuffle=True,
    class_mode='categorical'
)
# Top Model Block
x = base_model.output
x = GlobalAveragePooling2D()(x)
predictions = Dense(151, activation='softmax')(x)

# add your top layer block to your base model
model = Model(base_model.input, predictions)

print(model.summary())

for layer in base_model.layers:
    layer.trainable = False

model.compile(optimizer='nadam', loss='categorical_crossentropy', metrics=['accuracy'])

top_weights_path = os.path.join(os.path.abspath(model_path), 'top_model_weights.h5')
callbacks_list = [
    ModelCheckpoint(top_weights_path, monitor='acc', verbose=1, save_best_only=True),
    EarlyStopping(monitor='val_acc', patience=5, verbose=0)
]

# fine-tune the model first only top layer
model.fit_generator(
        train_generator,
        steps_per_epoch = train_generator.samples // batch_size,
        use_multiprocessing=True,
        epochs=16,
        # epochs=100,
        validation_data=validation_generator,
        validation_steps = validation_generator.samples // batch_size,
        callbacks=callbacks_list
)
model.load_weights(top_weights_path)
based_model_last_block_layer_number = 126

for layer in model.layers[:based_model_last_block_layer_number]:
    layer.trainable = False
for layer in model.layers[based_model_last_block_layer_number:]:
    layer.trainable = True

model.compile(optimizer='nadam',
    loss='categorical_crossentropy',
    metrics=['accuracy'])

final_weights_path = os.path.join(os.path.abspath(model_path), 'model_weights.h5')
callbacks_list = [
    ModelCheckpoint(final_weights_path, monitor='acc', verbose=1, save_best_only=True),
    EarlyStopping(monitor='loss', patience=5, verbose=0)
]

model.fit_generator(
    train_generator,
    steps_per_epoch = train_generator.samples // batch_size,
    use_multiprocessing=True,
    shuffle=True,
    epochs=16,
    validation_data=validation_generator,
    validation_steps = validation_generator.samples // batch_size,
    callbacks=callbacks_list
)<jupyter_output><empty_output><jupyter_text>Save Model<jupyter_code># save model as architecture and weights
model_json = model.to_json()
with open(os.path.join(os.path.abspath(model_path), 'model.json'), 'w') as json_file:
    json_file.write(model_json)
# save whole thing
model.save('models/my_model.h5')<jupyter_output><empty_output><jupyter_text>Load Model<jupyter_code># load model as architecture and weights

from keras.models import model_from_json

json_file = open(model_path + "/model.json", 'r')
loaded_model_json = json_file.read()
json_file.close()

test_model = model_from_json(loaded_model_json)

test_model.load_weights(model_path + '/model_weights.h5')
# load whole thing
test_model = load_model('models/my_model.h5')<jupyter_output><empty_output><jupyter_text>Test Model<jupyter_code>img = load_img(path_to_data + '/Starmie/b05d5dc1b3364dc8ac42f593ad3a479f.jpg', target_size=(299,299))
x = img_to_array(img)
x = np.expand_dims(x, axis=0)

pred = model.predict(x)
np.argmax(pred, axis=1)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10828025477707007, 'fraction_numerical': 0.02253797158255757, 'mean_word_length': 5.553772070626003, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '531523', 'license': 'MIT', 'n_tokens_mistral': 1459, 'n_tokens_neox': 1402, 'n_words': 239, 'path': 'pokemon_classifier.ipynb', 'repo_name': 'PandaXcentric/pokemon_classifier', 'size': '4082'}