예제 #1
0
def tf_to_torch(input_path, output_path):
    """
    Convert TensorFlow checkpoint to PyTorch model pickle
    :param input_path: path to TensorFlow checkpoint
    :param output_path: path to save PyTorch model pickle
    """
    g = tf.Graph()
    device = torch.device("cpu")
    bbmodel = models_torch.BaselineBreastModel(device,
                                               nodropout_probability=1.0)
    with tf.Session(graph=g,
                    config=tf.ConfigProto(allow_soft_placement=True)) as sess:
        saver = tf.train.import_meta_graph(input_path + ".meta")
        saver.restore(sess, input_path)
        var_dict = {
            var.name: var
            for var in g.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES)
        }
        for conv_name, conv_layer in bbmodel.conv_layer_dict.items():
            for view in ["CC", "MLO"]:
                conv_layer.ops[view].weight.data = torch.Tensor(
                    sess.run(var_dict["{}_{}/weights:0".format(
                        conv_name, view)])).permute(3, 2, 0, 1)
                conv_layer.ops[view].bias.data = torch.Tensor(
                    sess.run(var_dict["{}_{}/biases:0".format(conv_name,
                                                              view)]))
        bbmodel.fc1.weight.data = torch.Tensor(
            sess.run(var_dict["fully_connected/weights:0"]).T)
        bbmodel.fc1.bias.data = torch.Tensor(
            sess.run(var_dict["fully_connected/biases:0"]))
        bbmodel.fc2.weight.data = torch.Tensor(
            sess.run(var_dict["fully_connected_1/weights:0"]).T)
        bbmodel.fc2.bias.data = torch.Tensor(
            sess.run(var_dict["fully_connected_1/biases:0"]))
        torch.save(bbmodel.state_dict(), output_path)
예제 #2
0
def inference(parameters, verbose=True):

    # resolve device
    device = torch.device("cuda:{}".format(parameters["gpu_number"])
                          if parameters["device_type"] == "gpu" else "cpu")

    # load input images
    datum_l_cc = utils.load_images(parameters['image_path'], 'L-CC')
    datum_r_cc = utils.load_images(parameters['image_path'], 'R-CC')
    datum_l_mlo = utils.load_images(parameters['image_path'], 'L-MLO')
    datum_r_mlo = utils.load_images(parameters['image_path'], 'R-MLO')

    # construct models and prepare data
    if parameters["model_type"] == 'cnn':
        model = models.BaselineBreastModel(device,
                                           nodropout_probability=1.0,
                                           gaussian_noise_std=0.0).to(device)
        model.load_state_dict(torch.load(parameters["model_path"]))
        x = {
            "L-CC": torch.Tensor(datum_l_cc).permute(0, 3, 1, 2).to(device),
            "L-MLO": torch.Tensor(datum_l_mlo).permute(0, 3, 1, 2).to(device),
            "R-CC": torch.Tensor(datum_r_cc).permute(0, 3, 1, 2).to(device),
            "R-MLO": torch.Tensor(datum_r_mlo).permute(0, 3, 1, 2).to(device),
        }
    elif parameters["model_type"] == 'histogram':
        model = models.BaselineHistogramModel(
            num_bins=parameters["bins_histogram"]).to(device)
        model.load_state_dict(torch.load(parameters["model_path"]))
        x = torch.Tensor(
            utils.histogram_features_generator(
                [datum_l_cc, datum_r_cc, datum_l_mlo, datum_r_mlo],
                parameters)).to(device)
    else:
        raise RuntimeError(parameters["model_type"])

    # run prediction
    with torch.no_grad():
        prediction_density = model(x).cpu().numpy()

    if verbose:
        # nicely prints out the predictions
        print('Density prediction:\n'
              '\tAlmost entirely fatty (0):\t\t\t' +
              str(prediction_density[0, 0]) + '\n'
              '\tScattered areas of fibroglandular density (1):\t' +
              str(prediction_density[0, 1]) + '\n'
              '\tHeterogeneously dense (2):\t\t\t' +
              str(prediction_density[0, 2]) + '\n'
              '\tExtremely dense (3):\t\t\t\t' +
              str(prediction_density[0, 3]) + '\n')

    return prediction_density[0]
예제 #3
0
def inference(parameters, verbose=True):
    """
    Function that creates a model, loads the parameters, and makes a prediction
    :param parameters: dictionary of parameters
    :param verbose: Whether to print predicted probabilities
    :return: Predicted probabilities for each class
    """
    # resolve device
    device = torch.device(
        "cuda:{}".format(parameters["gpu_number"]) if parameters["device_type"] == "gpu"
        else "cpu"
    )

    # construct models
    model = models.BaselineBreastModel(device, nodropout_probability=1.0, gaussian_noise_std=0.0).to(device)
    model.load_state_dict(torch.load(parameters["model_path"]))

    # load input images and prepare data
    x = {
        "L-CC": torch.Tensor(get_image_path1()).permute(0, 3, 1, 2).to(device),
        "L-MLO": torch.Tensor(get_image_path3()).permute(0, 3, 1, 2).to(device),
        "R-CC": torch.Tensor(get_image_path2()).permute(0, 3, 1, 2).to(device),
        "R-MLO": torch.Tensor(get_image_path4()).permute(0, 3, 1, 2).to(device),
    }

    # run prediction
    with torch.no_grad():
        prediction_birads = model(x).cpu().numpy()

    if verbose:
        # nicely prints out the predictions
        birads0_prob = prediction_birads[0][0]
        birads1_prob = prediction_birads[0][1]
        birads2_prob = prediction_birads[0][2]
        #introducing  3 global variables so that they can be accesed from automail
        global b0
        global b1
        global b2
        b0 = birads0_prob
        b1 = birads1_prob
        b2 = birads2_prob
        #print(b0, b1, b2)
        label_image_1.configure(text="Predicted!")
        label1=Label(window, text="BI-RADS prediction: ",anchor="e", justify = CENTER)
        label2=Label(window, text='BI-RADS 0: ' + str(birads0_prob),anchor="e", justify = CENTER)
        label3=Label(window, text='BI-RADS 1: ' + str(birads1_prob),anchor="e", justify = CENTER)
        label4=Label(window, text= 'BI-RADS 2: ' + str(birads2_prob),anchor="e", justify = CENTER)
        label1.place(relx=0.47,y=500)
        label2.place(relx=0.47,y=520)
        label3.place(relx=0.47,y=540)
        label4.place(relx=0.47,y=560)
예제 #4
0
def inference(parameters, verbose=True):
    """
    Function that creates a model, loads the parameters, and makes a prediction
    :param parameters: dictionary of parameters
    :param verbose: Whether to print predicted probabilities
    :return: Predicted probabilities for each class
    """
    # resolve device
    device = torch.device("cuda:{}".format(parameters["gpu_number"])
                          if parameters["device_type"] == "gpu" else "cpu")

    # construct models
    model = models.BaselineBreastModel(device,
                                       nodropout_probability=1.0,
                                       gaussian_noise_std=0.0).to(device)
    model.load_state_dict(torch.load(parameters["model_path"]))

    # load input images and prepare data
    datum_l_cc = utils.load_images(parameters['image_path'], 'L-CC')
    datum_r_cc = utils.load_images(parameters['image_path'], 'R-CC')
    datum_l_mlo = utils.load_images(parameters['image_path'], 'L-MLO')
    datum_r_mlo = utils.load_images(parameters['image_path'], 'R-MLO')
    x = {
        "L-CC": torch.Tensor(datum_l_cc).permute(0, 3, 1, 2).to(device),
        "L-MLO": torch.Tensor(datum_l_mlo).permute(0, 3, 1, 2).to(device),
        "R-CC": torch.Tensor(datum_r_cc).permute(0, 3, 1, 2).to(device),
        "R-MLO": torch.Tensor(datum_r_mlo).permute(0, 3, 1, 2).to(device),
    }

    # run prediction
    with torch.no_grad():
        prediction_birads = model(x).cpu().numpy()

    if verbose:
        # nicely prints out the predictions
        birads0_prob = prediction_birads[0][0]
        birads1_prob = prediction_birads[0][1]
        birads2_prob = prediction_birads[0][2]
        print('BI-RADS prediction:\n' + '\tBI-RADS 0:\t' + str(birads0_prob) +
              '\n' + '\tBI-RADS 1:\t' + str(birads1_prob) + '\n' +
              '\tBI-RADS 2:\t' + str(birads2_prob))

    return prediction_birads[0]
예제 #5
0
            def inference(parameters, verbose=True):
                """
                Function that creates a model, loads the parameters, and makes a prediction
                :param parameters: dictionary of parameters
                :param verbose: Whether to print predicted probabilities
                :return: Predicted probabilities for each class
                """
                # resolve device
                device = torch.device(
                    "cuda:{}".format(parameters["gpu_number"])
                    if parameters["device_type"] == "gpu" else "cpu")

                # construct models
                model = models.BaselineBreastModel(
                    device, nodropout_probability=1.0,
                    gaussian_noise_std=0.0).to(device)
                model.load_state_dict(torch.load(parameters["model_path"]))

                # load input images and prepare data
                datum_l_cc = utils.load_images(parameters['image_path'],
                                               'L-CC')
                datum_r_cc = utils.load_images(parameters['image_path'],
                                               'R-CC')
                datum_l_mlo = utils.load_images(parameters['image_path'],
                                                'L-MLO')
                datum_r_mlo = utils.load_images(parameters['image_path'],
                                                'R-MLO')
                x = {
                    "L-CC":
                    torch.Tensor(datum_l_cc).permute(0, 3, 1, 2).to(device),
                    "L-MLO":
                    torch.Tensor(datum_l_mlo).permute(0, 3, 1, 2).to(device),
                    "R-CC":
                    torch.Tensor(datum_r_cc).permute(0, 3, 1, 2).to(device),
                    "R-MLO":
                    torch.Tensor(datum_r_mlo).permute(0, 3, 1, 2).to(device),
                }

                # run prediction
                with torch.no_grad():
                    prediction_birads = model(x).cpu().numpy()

                if verbose:
                    # nicely prints out the predictions
                    birads0_prob = prediction_birads[0][0]
                    birads1_prob = prediction_birads[0][1]
                    birads2_prob = prediction_birads[0][2]
                    print('BI-RADS prediction:\n' + '\tBI-RADS 0:\t' +
                          str(birads0_prob) + '\n' + '\tBI-RADS 1:\t' +
                          str(birads1_prob) + '\n' + '\tBI-RADS 2:\t' +
                          str(birads2_prob))

                results = {
                    'BI_RADS_prediction': {
                        'BI_RADS_zero': str("%.2f" % (birads0_prob * 100)),
                        'BI_RADS_one': str("%.2f" % (birads1_prob * 100)),
                        'BI_RADS_two': str("%.2f" % (birads2_prob * 100)),
                        'error': 'false'
                    }
                }

                credential_json_file = ['file_name']
                databaseURL = ['databaseURL']
                storageBucket = ['storageBucket']
                logs_bucket = ['logs_bucket']

                try:
                    my_logger.info('Detection : {}'.format(results))

                    if (not len(firebase_admin._apps)):
                        cred = credentials.Certificate(credential_json_file)
                        fa = firebase_admin.initialize_app(
                            cred, {
                                "databaseURL": databaseURL,
                                'storageBucket': storageBucket
                            })
                        fc = firebase_admin.firestore.client(fa)
                        db = firestore.client()
                        doc_ref = db.collection(
                            u'stripe_customers/{0}/results'.format(
                                userId)).document(currentTime)
                        doc_ref.update(results)
                        message.ack()
                        my_logger.info(
                            'finish successfully and message acknowledge')
                    else:
                        print('alredy initialize')
                        db = firestore.client()
                        doc_ref = db.collection(
                            u'stripe_customers/{0}/results'.format(
                                userId)).document(currentTime)
                        doc_ref.update(results)
                        message.ack()
                        my_logger.info(
                            'finish successfully and message acknowledge')

                except Exception as e:
                    print(e)
                    my_logger.error(e, 'results NOT save to firebase-database')

                storage_client = store.Client.from_service_account_json(
                    credential_json_file)
                log_bucket = storage_client.get_bucket(logs_bucket)
                log_blob = log_bucket.blob(
                    '{0}/logs/{1}/{2}/BIRADS-logs'.format(
                        userId, date, currentTime))
                log_blob.upload_from_filename('{}.log'.format(msg_id))
                shutil.rmtree(msg_id)
                os.remove('./{}.log'.format(msg_id))
                return prediction_birads[0]