Esempio n. 1
0
def test_image():
    resized_height = 224
    resized_width = 224

    #init local model
    global model
    model = azure_chestxray_keras_utils.build_model(
        keras_contrib.applications.densenet.DenseNetImageNet121)
    model.load_weights(
        'azure_chest_xray_14_weights_712split_epoch_250_val_loss_179-4776.hdf5'
    )

    #script for later use
    import cv2
    image_dir = "./../../../data/chestxray/ChestX-ray8/ChestXray-NIHCC/images"
    image_name = "00000003_000.png"
    full_path = os.path.join(image_dir, image_name)
    cv2_image = cv2.resize(cv2.imread(full_path),
                           (resized_height, resized_width))

    #     ans = model.predict(np.expand_dims(cv2_image,0))

    test_images = json.dumps({"data": cv2_image.tolist()})
    test_images = bytes(test_images, encoding="utf8")
    ans = run(test_images, True)

    return ans
    data_base_output_dir,
    os.path.join(*(prj_consts.FULLY_PRETRAINED_MODEL_DIR_list)))

test_images_dir = os.path.join(data_base_input_dir,
                               os.path.join(*(['test_images'])))

test_images = azure_chestxray_utils.get_files_in_dir(test_images_dir)

nih_chest_xray_data_dir = os.path.join(
    data_base_input_dir, os.path.join(*(prj_consts.ChestXray_IMAGES_DIR_list)))

chestXray_images = azure_chestxray_utils.get_files_in_dir(
    nih_chest_xray_data_dir)

import keras_contrib
model = azure_chestxray_keras_utils.build_model(
    keras_contrib.applications.densenet.DenseNetImageNet121)

model_file_name = prj_consts.PRETRAINED_DENSENET201_IMAGENET_CHESTXRAY_MODEL_FILE_NAME
model_file_name = 'weights_only_azure_chest_xray_14_weights_712split_epoch_054_val_loss_191.2588.hdf5'
model.load_weights(os.path.join(fully_trained_weights_dir, model_file_name))

import cv2
cv2_image = cv2.imread(os.path.join(test_images_dir, test_images[3]))

from keras.models import Model

predictions = model.predict(cv2_image[None, :, :, :])
print(predictions)
conv_map_model = Model(inputs=model.input,
                       outputs=model.get_layer(index=-3).output)
conv_features = conv_map_model.predict(cv2_image[None, :, :, :])
Esempio n. 3
0
def init():
    global model
    model_path = Model.get_model_path("chest_xray_model_179.hdf5")
    model = azure_chestxray_keras_utils.build_model(
        keras_contrib.applications.densenet.DenseNetImageNet121)
    model.load_weights(model_path)