Exemple #1
0
def predict_to_file(filename, path_to_model):
    model = load_model(path_to_model)

    # Data
    h5_file_location = os.path.join('C:\\Users\Jeftha\stack\Rommel\ISMI\data',
                                    'prostatex-test.hdf5')
    h5_file = h5py.File(h5_file_location, 'r')
    x, _, attr = get_train_data(h5_file, ['ADC'])
    x = np.expand_dims(x, axis=-1)
    windowed = []
    for lesion in x:
        windowed.append(apply_window(lesion, (500, 1100)))
    x = np.asarray(windowed)
    predictions = model.predict(x, verbose=1)

    with open(filename, 'w') as f:
        f.write('proxid,clinsig\n')

        for i in range(len(predictions)):
            patient_id = attr[i]['patient_id']
            fid = attr[i]['fid']

            line = "ProstateX-%s-%s,%f\n" % (patient_id, fid, predictions[i])
            f.write(line)
            print(line)
Exemple #2
0
def get_model(configuration='baseline'):
    AUGMENTATION_CONFIGURATION = configuration

    ## Model
    model = Sequential()
    model.add(
        Conv2D(32, (3, 3),
               kernel_initializer='he_normal',
               bias_initializer=RandomNormal(mean=0.1),
               input_shape=(16, 16, 1)))
    model.add(LeakyReLU())
    model.add(BatchNormalization())
    model.add(MaxPooling2D(pool_size=(2, 2)))

    model.add(
        Conv2D(64, (2, 2),
               kernel_initializer='he_normal',
               bias_initializer=RandomNormal(mean=0.1)))
    model.add(LeakyReLU())
    model.add(BatchNormalization())
    model.add(MaxPooling2D(pool_size=(2, 2)))

    model.add(
        Conv2D(64, (2, 2),
               kernel_initializer='he_normal',
               bias_initializer=RandomNormal(mean=0.1)))
    model.add(LeakyReLU())
    model.add(BatchNormalization())
    model.add(
        Conv2D(64, (2, 2),
               kernel_initializer='he_normal',
               bias_initializer=RandomNormal(mean=0.1)))
    model.add(LeakyReLU())
    model.add(BatchNormalization())

    model.add(
        Conv2D(64, (1, 1),
               kernel_initializer='he_normal',
               bias_initializer=RandomNormal(mean=0.1)))
    model.add(LeakyReLU())
    model.add(BatchNormalization())

    model.add(Conv2D(1, (1, 1), activation='sigmoid'))
    model.add(Flatten())

    # For a binary classification problem
    sgd = SGD(lr=0.0005, momentum=0.9)
    model.compile(optimizer=sgd,
                  loss='binary_crossentropy',
                  metrics=['accuracy'])

    ## Data
    h5_file_location = os.path.join('C:\\Users\Jeftha\stack\Rommel\ISMI\data',
                                    'prostatex-train.hdf5')
    h5_file = h5py.File(h5_file_location, 'r')
    train_data_list, train_labels_list, attr = get_train_data(h5_file, ['ADC'])
    windowed = []
    for lesion in train_data_list:
        windowed.append(apply_window(lesion, (500, 1100)))
    train_data_list = np.asarray(windowed)

    train_data, val_data, train_labels, val_labels = train_test_split(
        train_data_list, train_labels_list, attr, test_size=0.33)

    train_data = np.expand_dims(train_data, axis=-1)
    val_data = np.expand_dims(val_data, axis=-1)
    train_labels = np.expand_dims(train_labels, axis=-1)
    val_labels = np.expand_dims(val_labels, axis=-1)

    ## Stuff for training
    generator = get_generator(configuration=AUGMENTATION_CONFIGURATION)

    train_generator = generator.flow(
        train_data,
        train_labels)  #, save_to_dir="/nfs/home4/schellev/augmented_images")
    batch_size = 128
    steps_per_epoch = len(train_labels_list) // batch_size

    auc_history = AucHistory(train_data,
                             train_labels,
                             val_data,
                             val_labels,
                             output_graph_name=AUGMENTATION_CONFIGURATION)

    model.fit_generator(train_generator,
                        steps_per_epoch,
                        epochs=15000,
                        verbose=2,
                        callbacks=[auc_history],
                        max_q_size=50,
                        workers=8)

    return model
def predict_to_file(filename, path_to_model):
    model = load_model(path_to_model)
    model.summary()

    # Data
    if IS_TRAIN:
        file = 'prostatex-train.hdf5'
    else:
        file = 'prostatex-test.hdf5'
    h5_file_location = os.path.join('/scratch-shared/ISMI/prostatex', file)
    h5_file = h5py.File(h5_file_location, 'r')
    x, y, attr = get_train_data(h5_file, ['ADC'])
    x = np.expand_dims(x, axis=-1)
    windowed = []
    for lesion in x:
        windowed.append(apply_window(lesion, (500, 1100)))
    x = np.asarray(windowed)

    print(attr[0])

    activations = get_activations(model, -3, x)
    print('length of predictions:', len(activations))
    print('length of prediction[0]:', len(activations[0]))
    print(activations[0].shape)

    activations = activations[0]

    with open(filename, 'w') as f:
        header_items = ['proxid']
        if IS_TRAIN:
            header_items.append('clinsig')

        for index in range(activations.shape[-1]):
            header_items.append('feature' + str(index).zfill(2))

        attr_features = ['Age', 'Zone']
        header_items.extend(attr_features)

        header = ','.join(header_items) + '\n'

        f.write(header)

        for i in range(activations.shape[0]):
            patient_id = attr[i]['patient_id']
            fid = attr[i]['fid']

            line = ["ProstateX-%s-%s" % (patient_id, fid)]

            if IS_TRAIN:
                line.append('%f' % float(y[i]))

            features = ['%f' % feature for feature in activations[i, 0, 0, :]]
            line.extend(features)

            attribute_values = []
            for attribute in attr_features:
                attribute_values.append(str(attr[i][attribute], 'utf-8'))

            line.extend(attribute_values)

            line = ','.join(line) + '\n'
            f.write(line)
            print(line)