def load_cv_folds(model_name): models = [] for i in range(5): net = np.load(paths.predictions + model_name + '-split_{}.npz'.format(i)) net = { 'train': np.average(net['train'], axis=0), 'val': np.average(net['val'], axis=0), 'test': np.average(net['test'], axis=0) } models.append(net) labels_df = labels.get_labels_df() kf = sklearn.model_selection.KFold(n_splits=5, shuffle=True, random_state=1) split = kf.split(labels_df) folds = [] for i, ((train_idx, val_idx), net) in enumerate(zip(split, models)): val = labels_df.ix[val_idx] train = labels_df.ix[train_idx] folds.append((net, val, train)) print(i) return folds
def predict_kfold(model_name, pre_transforms=[]): model = locate(model_name + '.generate_model')() random_state = locate(model_name + '.random_state') print('Random state: {}'.format(random_state)) labels_df = labels.get_labels_df() kf = sklearn.model_selection.KFold(n_splits=5, shuffle=True, random_state=random_state) split = kf.split(labels_df) for i, (train_idx, val_idx) in enumerate(split): split_name = model_name + '-split_' + str(i) best_epoch = util.find_epoch_val(split_name) print('Using epoch {} for predictions'.format(best_epoch)) epoch_name = split_name + '-epoch_' + str(best_epoch) train = labels_df.ix[train_idx] val = labels_df.ix[val_idx] state = torch.load(os.path.join(paths.models, split_name, epoch_name)) predict_model(model, state, train, val, output_file=split_name, pre_transforms=pre_transforms)
pretrained_model.avgpool = nn.AvgPool2d(8) classifier = [ nn.Linear(pretrained_model.fc.in_features, 17), ] self.classifier = nn.Sequential(*classifier) pretrained_model.fc = self.classifier def forward(self, x): return F.sigmoid(self.pretrained_model(x)) return MyModel(torchvision.models.resnet50(pretrained=True)) random_state = 1 labels_df = labels.get_labels_df() kf = sklearn.model_selection.KFold(n_splits=5, shuffle=True, random_state=random_state) split = kf.split(labels_df) def train_net(train, val, model, name): transformations_train = transforms.apply_chain([ transforms.random_fliplr(), transforms.random_flipud(), transforms.augment(), torchvision.transforms.ToTensor() ]) transformations_val = transforms.apply_chain([ torchvision.transforms.ToTensor(), ])