def main(): # ==================================================== # Download and build a custom Xception # ==================================================== # instantiate pre-trained Xception model base_model = Xception(include_top=False, weights='imagenet', input_shape=(299, 299, 3)) # save base model base_model_json = base_model.to_json() name = "xcept_base_model" with open(name + ".json", "w") as json_file: json_file.write(base_model_json) base_model.save_weights(name + "_weight.h5") print("Saved base model to disk") # create a custom top classifier num_classes = 2 x = base_model.output x = GlobalAveragePooling2D()(x) x = Dense(1024, activation='relu')(x) predictions = Dense(num_classes, activation='softmax')(x) model = Model(inputs=base_model.inputs, outputs=predictions) # save custom model model_json = model.to_json() name = "xcept_custom_model" with open(name + ".json", "w") as json_file: json_file.write(model_json) model.save_weights(name + "_weight.h5") print("Saved custom model to disk")
lb = LabelBinarizer() lb.fit(np.asarray(data['primary_microconstituent'])) y = lb.transform(labels) print('\nLabels Binarized, converting array') input = np.asarray(processed_imgs) X_train, X_test, y_train, y_test = train_test_split( input, y, test_size=0.1, random_state=42) model = Xception(weights=None, classes = 7) model.summary() model.compile(loss = 'categorical_crossentropy', optimizer = 'sgd', metrics = ['accuracy']) model.fit(X_train, y_train, epochs = 5, batch_size = 32, validation_data=(X_test, y_test)) name = 'results/UHCS_Xception_Weights' score = model.evaluate(X_test, y_test) print('Test score:', score[0]) print('Test accuracy:', score[1]) model.save_weights(name+'.h5') file = open('Xception.txt', 'w') file.write('Test score:', score[0]) file.write('Test accuracy:', score[1]) file.close()
classes=1000) else: exit() if args.compress == True: model.save("model_original.hdf5") import sys sys.path.append("./keras_compressor/keras_compressor") import subprocess subprocess.call( "python ./keras_compressor/bin/keras-compressor.py model_original.hdf5 model_compressed.hdf5 --log-level DEBUG", shell=True) from keras.models import load_model model = load_model("model_compressed.hdf5") model.summary() model.save_weights("model.hdf5") model.summary() model.save_weights("model.hdf5") with open('model.json', 'w') as f: f.write(model.to_json()) from keras.utils import plot_model plot_model(model, to_file='model.png', show_shapes=True, show_layer_names=True) import subprocess subprocess.call("python ./keras-js/encoder.py model.hdf5", shell=True)