def main(): model = create_model() model.load_weights(WEIGHTS_FILE) for filename in glob.glob(IMAGES): unscaled = cv2.imread(filename) image_height, image_width, _ = unscaled.shape image = cv2.resize(unscaled, (IMAGE_SIZE, IMAGE_SIZE)) feat_scaled = preprocess_input(np.array(image, dtype=np.float32)) region, class_id = model.predict(x=np.array([image])) region = region[0] x0 = int(region[0] * image_width / IMAGE_SIZE) y0 = int(region[1] * image_height / IMAGE_SIZE) x1 = int((region[0] + region[2]) * image_width / IMAGE_SIZE) y1 = int((region[1] + region[3]) * image_height / IMAGE_SIZE) class_id = np.argmax(class_id, axis=1)[0] cv2.rectangle(unscaled, (x0, y0), (x1, y1), (0, 0, 255), 1) cv2.putText(unscaled, "class: {}".format(class_names[class_id]), (x0, y0), cv2.FONT_HERSHEY_SIMPLEX, 1, (255,255,255), 2, cv2.LINE_AA) cv2.imshow("image", unscaled) cv2.waitKey(0) cv2.destroyAllWindows()
def read_model(IMG_SIZE, PATH_TO_MODEL): # Init model with right architecture model = create_model(IMG_SIZE, optimizer_param='sgd', depth='dense3') model.load_weights(PATH_TO_MODEL) model.compile(loss='categorical_crossentropy', optimizer='sgd', metrics=['accuracy']) return model
def build_mlp(input_size=PATCH_SIZE, phase='TRAIN'): optimizer_param = 'sgd' # Create Model model = create_model(input_size, optimizer_param='sgd', depth='patches') if phase == 'TEST': model.add( Dense(units=8, activation='linear') ) # In test phase we softmax the average output over the image patches else: model.add(Dense(units=8, activation='softmax')) model.compile(loss='categorical_crossentropy', optimizer=optimizer_param, metrics=['accuracy']) return model
from model_definition import create_model from config import EPOCHS, BATCH_SIZE, keras_model_dir, serving_model_dir from prepare_data import get_datasets # GPU settings gpus = tf.config.experimental.list_physical_devices('GPU') if gpus: for gpu in gpus: tf.config.experimental.set_memory_growth(gpu, True) train_generator, valid_generator, \ train_num, valid_num, \ class_weights = get_datasets() # start training model = create_model() model.fit_generator(train_generator, epochs=EPOCHS, steps_per_epoch=train_num // BATCH_SIZE, validation_data=valid_generator, validation_steps=valid_num // BATCH_SIZE, class_weight=class_weights) # save the whole keras model model.save(os.path.join('keras_model', keras_model_dir)) # save saved_model for tensorflow serving tf.keras.experimental.export_saved_model( model, os.path.join('saved_model', serving_model_dir))