예제 #1
0
def test_softmax_layer(stop_after_n_examples):

    results_path = '../results'

    model_manager = ModelsManager(results_path)

    train_params = test_softmax.make_model()

    model, model_name, input_shape = train_params
    num_classes = 2

    model_manager.new_model(
        model,
        model_name,
        input_shape,
        num_classes,
        lr=0.001,  #0.001
    )

    model_class = model_manager.get_model(model_name)

    model_class.summary()

    input_shape = model_class.input_shape

    model_class.set_session('default')

    x = np.linspace(-10, 10, 30)
    batch_size = x.shape[0]
    x = x.reshape(1, batch_size)

    y = model_class.model.predict(x)

    for i in range(batch_size):
        print("softmax({:04.2f}) = {:04.2f}".format(x[0, i], y[0, i]))
예제 #2
0
def predict_single_image(img_number):

    #dataset_path = '../data/lausanne'
    #dataset_path = '/scratch1/xkv467/lausanne'
    dataset_path = '/scratch1/xkv467/lausanne_unregistered'
    #dataset_path = '../data/test_dataset'
    results_path = '../results'
    batch_size = 128
    #img_class_map = [[0, 3, 4, 5, 6, 7, 8], [1,2]]
    img_class_map = [[0, 7], [1], [2], [3, 4, 8], [6], [5]]
    #img_class_map = [[0, 1, 2, 5, 6, 7], [3, 4, 8]] # other, vesicles
    #img_class_map = [[0, 7], [1, 2, 3, 4, 8, 6, 5]] # cytosol, rest


    output_size = len(img_class_map)

    norm_params = (126.04022903600975, 29.063149797089494)

    model_manager = ModelsManager(results_path)

    saved_model = model_manager.get_model('conv_2_layer_07_1_2_348_6_5_more_reg')

    model = saved_model.load_model('best')

    epoch, train, test = saved_model.session_stats()

    input_shape = saved_model.input_shape

    dataset = PatchDataset(dataset_path,
                           batch_size,
                           input_shape,
                           img_class_map,
                           predict_only=True
                           )

    # consistency test:
    # Does model produce the same test acc as expected two times in a row?

    #test_acc = test_model(dataset, model)
    #print('test_acc:', test_acc)

    image = get_image_stack(img_number, input_shape[0])

    output = predict_image(dataset, model, image)
    #print(output.shape)

    #plt.imshow(image[image.shape[0]//2,:,:], cmap='gray')

    #segments = np.argmax(output, axis=2)

    #colors = ['red', 'blue', 'g', 'purple', 'cyan', 'gold']

    #for class_idx in range(6):
    #    if class_idx == 1:
    #        pass
            #plt.contour((segments==class_idx).astype(int), levels=[0.5], colors = [colors[class_idx]], linewidths=1)

    #plt.show()

    return output
def plot_specific(model_names):

    global model_manager

    if model_manager == None:
        results_path = '../results'
        model_manager = ModelsManager(results_path)

    for model_name in model_names:
        if model_manager.has_model(model_name):
            saved_model = model_manager.get_model(model_name)
            plot_single(saved_model)
예제 #4
0
def train_single():
    #dataset_path = '../data/lausanne'
    dataset_path = '/scratch1/xkv467/lausanne'
    results_path = '../results'
    batch_size = 32
    #img_class_map = [[0, 3, 4, 5, 6, 7, 8], [1,2]]
    img_class_map = [[0, 7], [1], [2], [3, 4, 8], [6], [5]]
    #img_class_map = [[0, 7], [1, 2, 3, 4, 8, 6, 5]]
    output_size = len(img_class_map)
    norm_params = (126.04022903600975, 29.063149797089494)

    model_manager = ModelsManager(results_path)

    dropout_p = 0.35

    model_type = 'conv_2_layer'
    model_id = 'conv_2_layer_07_1_2_348_6_5'

    model_params = {'norm_params': norm_params,
                    'output_size': output_size,
                    'lr': 0.01,
                    'rotation':True,
                    'foveation':True,
                    'linear_deformation':False,
                    'conv_dropout_p':dropout_p,
                    'dense_dropout_p':dropout_p,
                    }

    model_manager.new_model(model_type,
                            model_id,
                            **model_params,
                            )

    model_class = model_manager.get_model(model_id)
    input_shape = model_class.input_shape

    dataset = PatchDataset(dataset_path,
                           batch_size,
                           input_shape,
                           img_class_map,
                           )

    iterations_per_epoch = 565000//4
    max_epochs = 32*4

    train_model(dataset,
                model_class,
                batch_size,
                iterations_per_epoch,
                max_epochs,
                avg_grad_stop=False,
                avg_grad_n=16
                )
def compare_all():
    results_path = '../results'

    model_manager = ModelsManager(results_path)
    model_names = model_manager.models

    preprocessing = ['rot', 'fov', 'aff', 'warp', 'noi']

    print(model_names)

    for model_name in model_names:
        saved_model = model_manager.get_model(model_name)

        for session_name in  saved_model.sessions:
            #if session_name == 'default':
            #    continue
            epoch, train, test = saved_model.session_stats(session_name)

            '''if len(epoch) > 32:
                epoch = epoch[:32]
                train = train[:32]
                test = test[:32]'''

            #plt.subplot(1,2,1)
            #plt.title('Train acc')
            #plt.plot(epoch, train, label = model_name+'.'+session_name+'_train_acc')
            #plt.legend()

            #plt.subplot(1,2,2)
            #plt.title('Test acc')
            printed_name = model_name.replace('conv_2_layer_conf_', '')

            printed_name = printed_name.replace('rotation', 'rot')
            printed_name = printed_name.replace('foveation', 'fov')
            printed_name = printed_name.replace('linear_deformation', 'aff')
            printed_name = printed_name.replace('non_linear_resampling', 'warp')
            printed_name = printed_name.replace('noise', 'noi')

            for i in range(5):
                plt.subplot(1,5,i+1)
                plt.title(preprocessing[i])
                color = 'black'
                print(printed_name)
                if preprocessing[i] == printed_name:
                    #color = 'red'
                    plt.plot(epoch, test, color=color)
                plt.ylim(0,1)

            #plt.legend()

    plt.show()
def plot_single_all_sessions(model_name):
    results_path = '../results'

    model_manager = ModelsManager(results_path)

    saved_model = model_manager.get_model(model_name)

    for session_name in saved_model.sessions:
        epoch, train, test = saved_model.session_stats(session_name)

        plt.title(model_name)

        plt.plot(epoch, test, label=session_name)
        plt.ylim(0,1)

    plt.legend()
예제 #7
0
def mini_test():
    dataset_path = '../data/lausanne'
    results_path = '../results'
    batch_size = 32
    #img_class_map = [[0, 3, 4, 5, 6, 7, 8], [1,2]] # other, membrane + synapse
    #img_class_map = [[0, 7], [1], [2], [3, 4, 8], [6], [5]] # 6 groups
    img_class_map = [[0, 1, 2, 5, 6, 7], [3, 4, 8]] # other, vesicles
    output_size = len(img_class_map)
    norm_params = (126.04022903600975, 29.063149797089494)

    model_manager = ModelsManager(results_path)

    model_type = 'mini'
    model_id = 'mini'

    model_params = {'norm_params': norm_params,
                    'rotation':True,
                    'output_size': output_size,
                    'lr': 0.01,
                    }

    model_manager.new_model(model_type,
                            model_id,
                            **model_params,
                            )

    model_class = model_manager.get_model(model_id)
    input_shape = model_class.input_shape

    dataset = PatchDataset(dataset_path,
                           batch_size,
                           input_shape,
                           img_class_map,
                           )

    iterations_per_epoch = 131072//2
    max_epochs = 9

    train_model(dataset,
                model_class,
                batch_size,
                iterations_per_epoch,
                max_epochs,
                avg_grad_stop=False,
                )
def test_acc_matches_stored_test_acc():
    results_path = '../results'
    batch_size = 32
    img_class_map = [[0, 3, 4, 5, 6, 7, 8], [1,2]]
    output_size = len(img_class_map)
    norm_params = (126.04022903600975, 29.063149797089494)

    model_manager = ModelsManager(results_path)

    dataset = dataset_real()

    model_type = 'mini'
    model_id = 'test_mini'
    model_params = {'norm_params': norm_params,
                    'output_size': output_size,
                    'lr': 0.01}

    model_manager.new_model(model_type,
                            model_id,
                            **model_params,
                            )

    model_class = model_manager.get_model(model_id)

    input_shape = model_class.input_shape

    iterations_per_epoch=256
    max_epochs=2

    train_model(dataset,
                model_class,
                batch_size,
                iterations_per_epoch,
                max_epochs,
                avg_grad_stop=False,
                )

    epochs, train_accs, test_accs = model_class.session_stats()

    for i, epoch in enumerate(epochs):
        epoch_model = model_class.load_model(epoch)
        test_acc = test_model(dataset, epoch_model)
        print('test_acc:', test_accs[i], test_acc)
def test_train_model_again():
    results_path = '../results'
    batch_size = 32
    img_class_map = [[0, 3, 4, 5, 6, 7, 8], [1,2]]
    output_size = len(img_class_map)
    norm_params = (126.04022903600975, 29.063149797089494)

    model_manager = ModelsManager(results_path)

    dataset = dataset_real()

    model_name = 'test_mini'

    model_manager.new_model('mini',
                            model_name,
                            output_size,
                            norm_params,
                            lr = 0.01,
                            )

    model_class = model_manager.get_model(model_name)
    input_shape = model_class.input_shape

    iterations_per_epoch=128
    max_epochs=16

    train_model(dataset,
                model_class,
                batch_size,
                iterations_per_epoch,
                max_epochs,
                avg_grad_stop=False,
                )

    max_epochs=32

    train_model(dataset,
                model_class,
                batch_size,
                iterations_per_epoch,
                max_epochs,
                avg_grad_stop=False,
                )
def plot_bar_chart_average_success(model_names):

    global model_manager

    if model_manager == None:
        results_path = '../results'
        model_manager = ModelsManager(results_path)

    y_avg = []
    y_max = []
    formated_names = []

    for model_name in model_names:
        if model_manager.has_model(model_name):

            saved_model = model_manager.get_model(model_name)

            epoch, train, test = saved_model.session_stats()

            avg_acc = np.mean(test)
            max_acc = np.max(test)

            y_avg.append(avg_acc)
            y_max.append(max_acc)

            formated_name = model_name.replace('conv_2_layer_', '').replace('_','\n')
            formated_names.append(formated_name)

    x = range(1,len(formated_names) + 1)

    plt.plot(x, y_avg, 'go', label='average')
    plt.plot(x, y_max, 'ro', label='max')
    plt.xticks(x, formated_names) #, rotation='vertical'
    plt.ylabel('average test accuracy')
    plt.subplots_adjust(bottom=0.25)
    plt.legend()
예제 #11
0
'''
Main file that includes all functions in appropriate order
'''
from config import *
from time import sleep
import connection
from ModelsManager import ModelsManager

if __name__ == '__main__':
	## Main section
	# Set logging
	init_logging()
	# Create directories
	Remove_folder(SCRIPTS_FOLDER)
	Preconditions(SCRIPTS_FOLDER)
	Preconditions(VIDEO_FOLDER)
	Preconditions(TEMP_FOLDER)
	# Instantiate ModelsManager
	mm = ModelsManager()
	
	while True:
	
		mm.update()

		sleep(DELAY)
예제 #12
0
def test_foveation_layer(stop_after_n_examples):
    #dataset_path = '../data/lausanne'
    #dataset_path = '/scratch/xkv467/lausanne'
    dataset_path = '../data/test_dataset'
    results_path = '../results'
    batch_size = 32
    #img_class_map = [[0, 3, 4, 5, 6, 7, 8], [1,2]]
    img_class_map = [[0], [1], [2]]
    num_classes = len(img_class_map)
    norm_params = (142.1053396892233, 30.96410819657719)

    model_manager = ModelsManager(results_path)

    conv_dropout_p = 0.5
    dense_dropout_p = 0.5

    train_params = test_foveation_model.make_model(
        num_classes,
        conv_dropout_p=conv_dropout_p,
        dense_dropout_p=dense_dropout_p,
        norm_params=norm_params,
    )

    model, model_name, input_shape = train_params

    model_manager.new_model(
        model,
        model_name,
        input_shape,
        num_classes,
        lr=0.001,  #0.001
    )

    model_class = model_manager.get_model(model_name)

    model_class.summary()

    input_shape = model_class.input_shape

    model_class.set_session('default')

    dataset = PatchDataset(
        dataset_path,
        batch_size,
        input_shape,
        img_class_map,
        norm_params=norm_params,
    )
    d, h, w = model_class.input_shape

    inputs = []
    outputs = []

    for i in range(1):
        x, _ = dataset.next_batch()
        x = x.reshape(batch_size, d, h, w, 1)
        output = model_class.model.predict(x)
        inputs.append(x)
        outputs.append(output)

    mu_before = np.mean(np.concatenate(inputs))
    mu_after = np.mean(np.concatenate(outputs))
    stddev_before = np.std(np.concatenate(inputs))
    stddev_after = np.std(np.concatenate(outputs))

    print("Before mean: {:04.2f}, stddev: {:04.2f}".format(
        mu_before, stddev_before))
    print("After mean: {:04.2f}, stddev: {:04.2f}".format(
        mu_after, stddev_after))

    x = inputs[0]
    z = outputs[0]

    for i in range(batch_size):

        if i >= stop_after_n_examples:
            break

        for j in range(14):

            plt.subplot(4, 7, j + 1)
            plt.title('original ' + str(j + 5))
            plt.imshow(x[i, j + 5, :, :, 0], cmap='gray')

            plt.subplot(4, 7, 2 * 7 + j + 1)
            plt.title('foveated ' + str(j + 5))
            plt.imshow(z[i, j + 5, :, :, 0], cmap='gray')

        plt.show()
예제 #13
0
def test_all_layers(stop_after_n_examples):
    dataset_path = '../data/lausanne'
    #dataset_path = '/scratch/xkv467/lausanne'
    #dataset_path = '../data/test_dataset'
    results_path = '../results'
    batch_size = 32
    bubbles = False
    img_class_map = [[0, 3, 4, 5, 6, 7, 8], [1, 2]]
    #img_class_map = [[0], [1], [2]]
    num_classes = len(img_class_map)
    norm_params = (142.1053396892233, 30.96410819657719)

    model_manager = ModelsManager(results_path)

    conv_dropout_p = 0.5
    dense_dropout_p = 0.5

    train_params = test_all.make_model(
        num_classes,
        norm_params=norm_params,
    )

    model, model_name, input_shape = train_params

    dataset = PatchDataset(
        dataset_path,
        batch_size,
        input_shape,
        img_class_map,
        norm_params=norm_params,
    )

    model_manager.new_model(
        model,
        model_name,
        input_shape,
        num_classes,
        lr=0.001,  #0.001
    )
    model_class = model_manager.get_model(model_name)

    model_class.summary()

    input_shape = model_class.input_shape

    model_class.set_session('default')

    d, h, w = input_shape

    inputs = []
    outputs = []

    if bubbles:
        x = np.zeros((batch_size, d, h, w))
        for l in range(batch_size):
            bubblePeriod = l % 4 + 3
            for k in range(d):
                for j in range(h):
                    for i in range(w):
                        x[l,k,j,i] = np.cos(np.pi*float(i - w//2)/bubblePeriod) \
                                   * np.cos(np.pi*float(j - h//2)/bubblePeriod) \
                                   * np.cos(np.pi*float(k - d//2)/bubblePeriod)
    else:
        x, _ = dataset.next_batch()

    x = x.reshape(batch_size, d, h, w, 1)
    output = model_class.model.predict(x)
    inputs.append(x)
    outputs.append(output)

    mu_before = np.mean(np.concatenate(inputs))
    mu_after = np.mean(np.concatenate(outputs))
    stddev_before = np.std(np.concatenate(inputs), ddof=1)
    stddev_after = np.std(np.concatenate(outputs), ddof=1)

    print("Before mean: {:04.2f}, stddev: {:04.2f}".format(
        mu_before, stddev_before))
    print("After mean: {:04.2f}, stddev: {:04.2f}".format(
        mu_after, stddev_after))

    x = inputs[0]
    z = outputs[0]

    x = x[:, 5:-5, :, :, :]

    vmin_input, vmax_input = np.min(x), np.max(x)
    vmin_output, vmax_output = np.min(z), np.max(z)

    for i in range(batch_size):

        if i >= stop_after_n_examples:
            break

        for j in range(15):

            plt.subplot(6, 5, j + 1)
            plt.title('original ' + str(j))
            plt.imshow(x[i, j, :, :, 0],
                       vmin=vmin_input,
                       vmax=vmax_input,
                       cmap='gray')

            plt.subplot(6, 5, 3 * 5 + j + 1)
            plt.title('resampled ' + str(j))
            plt.imshow(z[i, j, :, :, 0],
                       vmin=vmin_output,
                       vmax=vmax_output,
                       cmap='gray')

        plt.show()
예제 #14
0
'''
Main file that includes all functions in appropriate order
'''
from config import *
from time import sleep
import connection
from ModelsManager import ModelsManager

if __name__ == '__main__':
    ## Main section
    # Set logging
    init_logging()
    # Create directories
    Remove_folder(SCRIPTS_FOLDER)
    Preconditions(SCRIPTS_FOLDER)
    Preconditions(VIDEO_FOLDER)
    Preconditions(TEMP_FOLDER)
    # Instantiate ModelsManager
    mm = ModelsManager()

    while True:

        mm.update()

        sleep(DELAY)
예제 #15
0
import config
from config import *
from time import sleep
import connection, time, datetime
from ModelsManager import ModelsManager

if __name__ == '__main__':
    init_logging()
    # Create directories
    Remove_folder(SCRIPTS_FOLDER)
    Preconditions(SCRIPTS_FOLDER)
    Preconditions(VIDEO_FOLDER)
    Preconditions(TEMP_FOLDER)
    mm = ModelsManager()
    while True:
        reload(config)
        from config import *
        try:
            if mm._tUpdate:
                if len(mm._wanted) > 0:
                    mm.stopProcess()
                    mm = ModelsManager()
                    logging.info('')

                logging.info(
                    '************************ Starting application: version %s ************************'
                    % VERSION)
                logging.info(
                    '***********************************************************************************'
                )
                mm._tSpan = RELOAD_TIME
예제 #16
0
def dropbox_and_preprocessing_effect():
    #dataset_path = '../data/lausanne'
    dataset_path = '/scratch1/xkv467/lausanne'
    results_path = '../results'
    batch_size = 32
    #img_class_map = [[0, 3, 4, 5, 6, 7, 8], [1,2]]
    img_class_map = [[0, 7], [1], [2], [3, 4, 8], [6], [5]]
    img_class_map = [[0, 7], [1, 2, 3, 4, 8, 6, 5]]
    output_size = len(img_class_map)
    norm_params = (126.04022903600975, 29.063149797089494)

    model_manager = ModelsManager(results_path)

    dropout_p_list = [0.25] #[0.35, 0.5, 0.65]

    for dropout_p in dropout_p_list:
        for preprocessing_idx in [1]:

            pp = [False, False, False, False]
            pp[preprocessing_idx] = True

            noise = False
            none, rot, fovea, linear = pp
            pp_affix = ['none', 'rot2', 'fovea', 'linear'][preprocessing_idx]

            model_type = 'conv_2_layer'
            model_id = 'conv_2_layer_' + pp_affix + '_' + str(dropout_p)

            model_params = {'norm_params': norm_params,
                            'output_size': output_size,
                            'lr': 0.01,
                            'rotation':rot,
                            'foveation':fovea,
                            'linear_deformation':linear,
                            }

            model_manager.new_model(model_type,
                                    model_id,
                                    **model_params,
                                    )

            model_class = model_manager.get_model(model_id)
            input_shape = model_class.input_shape

            dataset = PatchDataset(dataset_path,
                                   batch_size,
                                   input_shape,
                                   img_class_map,
                                   )

            iterations_per_epoch = 565000//2
            max_epochs = 32

            train_model(dataset,
                        model_class,
                        batch_size,
                        iterations_per_epoch,
                        max_epochs,
                        avg_grad_stop=False,
                        avg_grad_n=16
                        )