Beispiel #1
0
    def get_disc_batch(
            self,
            X,
            y,
            generator,
            batch_counter,
            patch_size,
            label_smoothing=False,
            label_flipping=0):
        '''
        Get the discriminator batch data
            Generator predict the sample
        '''
        generator.train_on_batch(X, y)
        y_disc = np.zeros((X.shape[0], 2), dtype=np.uint8)
        if batch_counter % 2 == 0:
            X_disc = generator.predict(X)
            y_disc[:, 0] = 1

        else:
            X_disc = y
            if label_smoothing:
                y_disc[:, 1] = np.random.uniform(
                    low=0.9, high=1, size=y_disc.shape[0])
            else:
                y_disc[:, 1] = 1

        if label_flipping > 0:
            p = np.random.binomial(1, label_flipping)
            if p > 0:
                y_disc[:, [0, 1]] = y_disc[:, [1, 0]]

        X_disc = extract_patches(X_disc, patch_size)
        return X_disc, y_disc
Beispiel #2
0
def predict(img, patch_size, patch_step, nb_filters, nb_conv, batch_size, dim_img, wpath):
    """
    the cnn model for image transformation


    Parameters
    ----------
    img : array
        The image need to be calculated

    patch_size : (int, int)
        The patches dimension

    dim_img : int
        The input image dimension

    Returns
    -------
    img_rec
        Description.

      """
    # img = nor_data(img)
    img_y, img_x = img.shape
    x_img = extract_patches(img, patch_size, patch_step)
    x_img = np.reshape(x_img, (len(x_img), 1, dim_img, dim_img))
    mdl = mirror_model(dim_img, nb_filters, nb_conv)
    mdl.load_weights(wpath)
    y_img = mdl.predict(x_img, batch_size=batch_size)
    del x_img
    y_img = np.reshape(y_img, (len(y_img), dim_img, dim_img))
    img_rec = reconstruct_patches(y_img, (img_y, img_x), patch_step)
    return img_rec
Beispiel #3
0
def train(img_x, img_y, patch_size, patch_step, dim_img, nb_filters, nb_conv, batch_size, nb_epoch, x_test, y_test):
    """
    Function description.

    Parameters
    ----------
    parameter_01 : type
        Description.

    parameter_02 : type
        Description.

    parameter_03 : type
        Description.

    Returns
    -------
    return_01
        Description.
    """

    # img_x = nor_data(img_x)
    img_y = nor_data(img_y)
    img_input = extract_patches(img_x, patch_size, 1)
    img_output = extract_patches(img_y, patch_size, 1)
    img_input = np.reshape(img_input, (len(img_input), 1, dim_img, dim_img))
    img_output = np.reshape(img_output, (len(img_input), 1, dim_img, dim_img))

    # test_x = nor_data(x_test)
    test_y = nor_data(y_test)
    test_x = extract_patches(x_test, patch_size, 1)
    test_y = extract_patches(test_y, patch_size, 1)
    test_x = np.reshape(test_x, (len(test_x), 1, dim_img, dim_img))
    test_y = np.reshape(test_y, (len(test_y), 1, dim_img, dim_img))

    mdl = mirror_model(dim_img, nb_filters, nb_conv)
    print(mdl.summary())
    mdl.fit(img_input, img_output, batch_size=batch_size, nb_epoch=nb_epoch, validation_data=(test_x,test_y))
    return mdl
def dataset(dataset_train_path,batch_size,scale_factor):
    assert(os.path.exists(dataset_train_path))
    data = []
    for file in os.listdir(dataset_train_path):
        if file.endswith('.bmp'):
            filepath = os.path.join(dataset_train_path,file)
            img = imageio.imread(filepath).dot([0.299, 0.587, 0.114])
            patches = extract_patches(img,(36,36),0.166)

            data += [patches[idx] for idx in range(patches.shape[0])]

    mod_data = [from_numpy(np.expand_dims(blur(upscale(patch,scale_factor),scale_factor),0)).float() for patch in data]
    data = [from_numpy(np.expand_dims(upscale(patch,scale_factor),0)).float() for patch in data]
    l = len(data)
    for idx in range(0,l,batch_size):
        yield stack(mod_data[idx:min(idx+batch_size,l)]),stack(data[idx:min(idx+batch_size,l)])
Beispiel #5
0
im_train_x = np.array(
    Image.open(os.path.join(topdir, '2idd_0034_P_crop62x62_top.tif')))
im_train_y = np.array(
    Image.open(os.path.join(topdir, '2idd_0043_P_fine310x310_top.tif')))
im_test_x = np.array(
    Image.open(os.path.join(bottomdir, '2idd_0034_P_crop62x62_bottom.tif')))
im_test_y = np.array(
    Image.open(os.path.join(bottomdir, '2idd_0043_P_fine310x310_bottom.tif')))
patch_size = [15, 30]
patch_step = 1
pxH, pyH = patch_size
pxh, pyh = np.multiply(5, patch_size)
nxH, nyH = im_train_x.shape
nxh, nyh = im_train_y.shape
train_x = extract_patches(im_train_x, patch_size, patch_step)
train_y = extract_patches(im_train_y, [pxh, pyh], patch_step * 5)
X_train = np.reshape(train_x, (len(train_x), pxH, pyH))
Y_train = np.reshape(train_y, (len(train_y), pxh, pyh))

X_test = extract_patches(im_test_x, patch_size, patch_step)
X_test = np.reshape(X_test, (len(X_test), pxH, pyH))
Y_test = extract_patches(im_test_y, [pxh, pyh], patch_step * 5)
Y_test = np.reshape(Y_test, (len(Y_test), pxh, pyh))

model = []
model = Sequential()
model.add(Dense(pxh * pyh, input_dim=pxH * pyH))
model.add(Dense(pxh * pyh, input_dim=pxH * pyH))
model.add(Dense(pxh * pyh, input_dim=pxH * pyH))
model.compile(loss='categorical_crossentropy',
        bsp_out_dir = sys.argv[arg_index + 1]
    # Check whether the --asp_out_dir command line parameter was
    # provided.
    asp_out_dir = None
    if '--asp_out_dir' in sys.argv:
        # Get the command line parameter value.
        arg_index = sys.argv.index('--asp_out_dir')
        asp_out_dir = sys.argv[arg_index + 1]

    if bsp_columns_file is None and asp_columns_file is None:
        sys.exit('One or both of the --bsp_columns_file'
                 ' --asp_columns_file must be provided.')

    images, _ = load_images(imgs_dir, extensions=('.jpg',),
                            img_shape=(256, 256))
    patches = extract_patches(images, (16, 16), images.shape[0]*256,
                              randomize=False)

    if asp_columns_file is not None:
        with open(asp_columns_file, 'rb') as fp:
            asp_columns = pickle.load(fp)
        asp_activations =\
            reconstruct_images(alg='asp', images=patches, columns=asp_columns,
                               connect_threshold=0.2,
                               desired_activity_mult=0.05, min_overlap=3,
                               img_shape=(256, 256), out_dir=asp_out_dir)
        calculate_print_stats(asp_activations, alg='ASP')
        save_activations(asp_activations, asp_columns_file)

    if bsp_columns_file is not None:
        with open(bsp_columns_file, 'rb') as fp:
            bsp_columns = pickle.load(fp)
Beispiel #7
0
def predict_one_img(img,
                
                weights_path,
                inception_model,
                model,
                labels = {'Benign': 0, 'InSitu': 1, 'Invasive': 2, 'Normal': 3}

            ):

    #predicts the class label of one 2048*1536 img
    #labels is a the class labels dict
    #return the label 0,1,2 or 3

    label_vals = list(labels.values())

    print('label_vals',label_vals)



    #list to hold predicted labels and confidence scores of the patches
    patch_pred_labels = []
    patch_pred_confs = []



    for patch,index in extract_patches(img,512):

        #print('[INFO]....Processing patch no. '+str(index))

        #model = InceptionModel()

        label,confidence = inception_model.predict(patch,model,weights_path)

        patch_pred_labels.append(label)
        patch_pred_confs.append(confidence)

        #print('patch_pred_labels,patch_pred_confs',patch_pred_labels,patch_pred_confs)



    
    #Majority Voting Decision over all the patches 
    #to predict class label of entire image

    #predict label with max occurences in patch_pred_labels
    #if tie, choose label with max total confidence
    #if tie again , choose any of those labels at random

    patches_pred_label_freqs = {}

    for label in label_vals:

        label_freq = patch_pred_labels.count(label)

        #print('label '+str(label)+' has a freq of ',label_freq)

        patches_pred_label_freqs[str(label)] = label_freq

        #print('patches_pred_label_freqs',patches_pred_label_freqs)

    #print(patches_pred_label_freqs)

    max_freq = max(patches_pred_label_freqs.values())

    #print('max_freq',max_freq)

    #list of labels with max_freq
    argmax_freqs = [int(k) for k, v in patches_pred_label_freqs.items() if v == max_freq]
    
    #print('list of labels with max_freq',argmax_freqs)


    if (len(argmax_freqs) == 1):

        print('Only one class majority')

        print('Predicted Label : ',argmax_freqs[0])

        return argmax_freqs[0]


    elif (len(argmax_freqs) > 1):

        #if tie, choose label with max total confidence

        print('Multiple classes with same majority')

        argmax_confs_dict = {}

        for argmax_freq in argmax_freqs:

            total_confidence = 0
            total_instances = 0

            #print('Current label',argmax_freq)
            
        
            for label,confidence in zip(patch_pred_labels,patch_pred_confs):

                #print('label',label)

                if (label == argmax_freq):

                    total_confidence += confidence
                    total_instances += 1

                    #print('total_confidence',total_confidence)
                    #print('total_instances',total_instances)
                    

            mean_confidence = total_confidence/max_freq
            #print('mean_confidence',mean_confidence)

            argmax_confs_dict[str(argmax_freq)] = mean_confidence

            #print('argmax_confs_dict',argmax_confs_dict)

        max_conf = max(argmax_confs_dict.values())
        #print('max_conf',max_conf)

        #list of labels with max_conf
        argmax_confs = [int(k) for k, v in argmax_confs_dict.items() if v == max_conf]

        #print('argmax_confs',argmax_confs)

        if (len(argmax_confs) == 1):

            print('Predicted Label : ',argmax_confs[0])

            return argmax_confs[0]

        elif (len(argmax_confs) > 1):

            #if tie again,choose any of those labels at random

            op_label = random.choice(argmax_confs)
            print('Predicted Label : ',op_label)

            return op_label
Beispiel #8
0
    if patches_file is not None:
        # If the patches_file command line parameter was provided,
        # read it from disk.
        pprint("Reading patches file ...")
        with open(patches_file, 'rb') as fp:
            patches = pickle.load(fp)
    else:
        # If the patches_file command line parameter was not provided,
        # load the images from disk, ...
        pprint("Loading images ...")
        images, image_files = load_images(images_path, extensions=['.png'],
                                          img_shape=(256, 256))
        if create_patches:
            # and if the --create_patches was provided generate the patches.
            pprint("Extracting patches ...")
            patches = extract_patches(images, patch_shape=(16, 16))
            if patches_file is not None:
                pprint("Saving patches to disk ...")
                with open(patches_file, 'wb') as fp:
                    pickle.dump(patches, fp)
        else:
            # or, if the --create_patches was not provided, use the images
            # themselves as the patches.
            patches = images

    # Finally, start the learning procedure.
    pprint("Starting training ...")
    columns = spatial_pooler(patches, shape=(16, 16, 16, 16), p_connect=0.1,
                             connect_threshold=0.2, p_inc=0.0005, p_dec=0.0025,
                             b_inc=0.005, p_mult=0.01,
                             min_activity_threshold=0.01,
import numpy as np
import matplotlib.pyplot as plt

import utils

DATASET_PATH = "./dataset/van_hateren/"
mode = "l4"
p = int(5e5)  # number of samples
n = 256  # data dimension
num_steps = 1000

# process data
print("processing data")
img_arr = utils.read_raw_imgs(DATASET_PATH)
img_arr = utils.normalize_imgs(img_arr)
img_arr = utils.extract_patches(img_arr, p)
img_arr, wtn_mat = utils.whiten_imgs(img_arr)

# initialize model
Y = img_arr.reshape(p, n).T
A = np.linalg.qr(np.random.rand(n, n))[0]

# train model
if mode == "l4":
    for t in range(num_steps):
        dA = 4 * np.power(A @ Y, 3) @ Y.T
        U, _, V = np.linalg.svd(dA, compute_uv=True)
        A = U @ V
        #
        loss = np.sum(np.power(A @ Y, 4))
        print(f'step: {t}\tloss: {loss}')
Beispiel #10
0
                   interpolation=cv2.INTER_LINEAR) for label in amsr_labels
    ]

    # Replacing invalid data with NaNs
    no_data = np.logical_or(np.isnan(HH), np.isnan(HH_nersc))
    CT[no_data] = np.nan
    DST[no_data] = np.nan
    INC[no_data] = np.nan
    HH_nersc[no_data] = np.nan
    HV_nersc[no_data] = np.nan
    for AMSR_channel in AMSR:
        AMSR_channel[no_data] = np.nan

    # Extract all patches (shape=patch_shape) from HH and return indices of all patches without NaN values
    HH_patches, non_nan_idxs = utils.extract_patches(HH,
                                                     patch_shape=PATCH_SHAPE,
                                                     return_non_nan_idxs=True,
                                                     overlap=OVERLAP)
    if not len(non_nan_idxs) == 0:  # if valid non-NaN patches in scene
        HH_patches = HH_patches[non_nan_idxs]
        del HH
        HV_patches = utils.extract_patches(HV,
                                           patch_shape=PATCH_SHAPE,
                                           overlap=OVERLAP)[non_nan_idxs]
        del HV
        HH_nersc_patches = utils.extract_patches(HH_nersc,
                                                 patch_shape=PATCH_SHAPE,
                                                 overlap=OVERLAP)[non_nan_idxs]
        del HH_nersc
        HV_nersc_patches = utils.extract_patches(HV_nersc,
                                                 patch_shape=PATCH_SHAPE,
                                                 overlap=OVERLAP)[non_nan_idxs]