def main(): pl_train, pl_labels = get_dataset('./Pan_Licence/') pl_labels = to_categorical(pl_labels, num_classes=36) x_train, x_val, y_train, y_val = train_test_split(pl_train, pl_labels, test_size=0.2, random_state=2064) tb = TensorBoard(log_dir='./logs/Squeezenet', write_graph=True) model = SqueezeNet() model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy']) print(model.summary()) history = model.fit(x_train, y_train, batch_size=32, epochs=5, validation_split=0.1, shuffle=True, callbacks=[tb]) ## Save Model json_model = model.to_json() with open('model_squeezenet.json', 'w') as f: f.write(json_model) model.save_weights('model_squeezenet.h5') print('Model Saved') print('Evaluating Model') predict = model.evaluate(x=x_val, y=y_val, batch_size=1) print('Score', predict[1] * 100.00) print('Loss', predict[0])
Y_train = to_categorical(y_train, nb_classes) Y_test = to_categorical(y_test, nb_classes) # classes = to_categorical(classes, nb_classes=nr_classes) print('Loading model..') model = SqueezeNet(nb_classes, input_shape=input_shape) model.compile(loss="categorical_crossentropy", optimizer='adam', metrics=['accuracy']) if os.path.isfile(weights_file): print('Loading weights: %s' % weights_file) model.load_weights(weights_file, by_name=True) print('Fitting model') model.fit(X_train, Y_train, batch_size=batch_size, nb_epoch=nb_epoch, verbose=1, validation_split=0.2, initial_epoch=0) print("Finished fitting model") print('Saving weights') model.save_weights(weights_file, overwrite=True) print('Evaluating model') score = model.evaluate(X_test, Y_test, verbose=1) print('result: %s' % score)
random.shuffle(imgpaths_classes) images, classes = zip(*imgpaths_classes) classes = to_categorical(classes, nb_classes=nb_classes) images = np.asarray(images) print('Loading model..') model = SqueezeNet(nb_classes, input_shape=(227, 227, 3)) model.compile(loss="categorical_crossentropy", optimizer='adam', metrics=['accuracy', 'categorical_crossentropy']) if os.path.isfile(weights_file): print('Loading weights: %s' % weights_file) model.load_weights(weights_file, by_name=True) print('Fitting model') model.fit(images, classes, batch_size=batch_size, nb_epoch=nb_epoch, verbose=1, validation_split=0.2, initial_epoch=0) print("Finished fitting model") print('Saving weights') model.save_weights(weights_file, overwrite=True) print('Evaluating model') score = model.evaluate(images, classes, verbose=1) print('result: %s' % score)