def run(output, subjects, ae_weights, clusters): # subjects = Subjects.from_data(path) fname = subjects with open(subjects, 'rb') as file: subjects = pickle.load(file) logger.info('Done loading subjects') config = Config(**{ 'n_clusters': int(clusters or 10), 'batch_size': 256, 'lr': 0.01, 'momentum': 0.9, 'tol': 0.001, 'maxiter': 2e4, 'update_interval': 140, 'save_dir': output, 'ae_weights': ae_weights or os.path.join(output, 'ae_weights.h5'), 'subjects': fname, }) config.dump() cluster = Cluster.create(subjects, config) logger.info('Training model') cluster.train() pred = cluster.predictions logger.info('Done training network') # if save: # pickle.dump(cluster, open(save, 'wb')) interact(locals())
def load(config): config = Config.load(config) subjects = pickle.load(open(config.subjects, 'rb')) cluster = Cluster.create(subjects, config) logger.info('Training model') cluster.train() pred = cluster.predictions logger.info('Done training network') interact(locals())
def main(path, name, lr): config = Config.load(_config) print(config.__dict__) subjects = pickle.load(open(config.subjects, 'rb')) labels = pe.Aggregate.load('mh2') truth = pe.Aggregate.load('mh2_gold') _labels = labels.subject_labels(), truth.subject_labels() subjects = subjects.subset(truth.labeled_subjects()) truth.apply_labels(subjects) cluster = Cluster.create(subjects, config) cluster.initialize() mapping = DEC_Updater(cluster, *_labels, train, validate, 0.95) print(mapping.scores) optimizer = optimizers.SGD(lr=float(lr)) mapping.init_model('categorical_crossentropy', optimizer) print(mapping.scores) weights_fname = os.path.join(path, name) kwargs = { 'epochs': 500, 'batch_size': 256, 'callbacks': [ ModelCheckpoint(weights_fname, monitor='val_loss', save_best_only=True, save_weights_only=True, mode='min'), EarlyStopping(monitor='val_loss', min_delta=0, patience=20, verbose=0, mode='min') ] } def fit(model, train, val): model.fit(*train, validation_data=val, **kwargs) # def xy(train, subjects): # return subjects.get_xy_volunteer(labels.data['subjects'], False) mapping.apply_mapping(fit) print(mapping.cluster_mapping()) print('scores:', utils.pd_scores(mapping.scores))
def test(config, new): config = Config.load(config) subjects = pickle.load(open(config.subjects, 'rb')) cluster = Cluster.create(subjects, config) logger.info('Training model') cluster.train() pred = cluster.predictions logger.info('Done training network') from muon.project.images import Random_Images if new: images = Random_Images.new(cluster) else: images = Random_Images.load_group(0) interact(locals())
def new(config, width, size, permutations, save): config = Config.load(config) subjects = pickle.load(open(config.subjects, 'rb')) cluster = Cluster.create(subjects, config) logger.info('Training model') cluster.train() logger.info('Done training network') kwargs = {} if width: kwargs['width'] = width if size: kwargs['image_size'] = size if permutations: kwargs['permutations'] = permutations images = Random_Images.new(cluster, **kwargs) if save: images.save_group() interact(locals())
import csv logger = logging.getLogger(__name__) config = 'mnt/dec/dec_no_labels/config_jupyter.json' config = Config.load(config) print(config.__dict__) subjects = pickle.load(open(config.subjects, 'rb')) # cluster = Cluster.create(subjects, config) import muon.project.parse_export as pe agg = pe.Aggregate.load('mh2') _s = list(agg.data['subjects'].keys()) subjects2 = subjects.subset(_s) agg.apply_labels(subjects2) cluster = Cluster.create(subjects2, config) print('Initializing model') cluster.initialize() def load_set(fname): with open(fname, 'r') as file: reader = csv.DictReader(file) return [int(item['subject']) for item in reader] train = load_set('mnt/training_set/train.csv') validate = load_set('mnt/training_set/test.csv') kwargs = {'mode': 'weightedagg', 'batched': False, 'shuffle': False} mapping = Mapping(cluster, agg, train, validate, **kwargs)