def create_models(): models = [] models.append(create_dense201()) models.append(create_dense161()) models.append(create_res101()) models.append(create_res152()) return models
def train_dense161(): print('training densenet 161') model = create_dense161() try: load_best_weights(model) except: print('Failed to load weigths') train(model, max_num=3)
def ensemble(): preds_raw = [] os.chdir(MODEL_DIR) total_weight = 0 preds_w = None for match_str in w_file_matcher: w_files = glob.glob(match_str) for w_file in w_files: weight = 0 full_w_file = MODEL_DIR + '/' + w_file if w_file.startswith('dense161'): model, _ = create_dense161() weight = 1 elif w_file.startswith('dense169'): model, _ = create_dense169() weight = 0.8 elif w_file.startswith('dense201'): model, _ = create_dense201() weight = 1 elif w_file.startswith('res50'): model, _ = create_res50() weight = 0.9 elif w_file.startswith('res101'): model, _ = create_res101() weight = 0.9 elif w_file.startswith('res152'): model, _ = create_res152() weight = 0.9 elif w_file.startswith('vgg16'): model, _ = create_vgg16() weight = 0.2 elif w_file.startswith('vgg19'): model, _ = create_vgg19() weight = 0.7 elif w_file.startswith('inceptionv3'): model, _ = create_inceptionv3() weight = 0.8 else: pass model.load_state_dict(torch.load(full_w_file)) print(full_w_file) pred = make_preds(model, test_loader) pred = np.array(pred) preds_raw.append(pred) if preds_w is None: preds_w = np.zeros((pred.shape)) preds_w += pred * weight total_weight += weight del model save_array(PRED_FILE_RAW, preds_raw) preds = np.mean(preds_raw, axis=0) #preds = preds_w / total_weight save_array(PRED_FILE, preds)
def train_dense161(): print('training densenet 161') model, w_file = create_dense161() try: model.load_state_dict(torch.load(w_file)) except: print('{} not found, continue'.format(w_file)) pass train(model, w_file, max_num=4)
def ensemble(): res101, _ = create_res101(True) res152, _ = create_res152(True) dense201, _ = create_dense201(True) dense161, _ = create_dense161(True) pred1 = np.array(make_preds(res101, test_loader)) pred2 = np.array(make_preds(res152, test_loader)) pred3 = np.array(make_preds(dense201, test_loader)) pred4 = np.array(make_preds(dense161, test_loader)) preds = np.mean([pred1, pred2, pred3, pred4], axis=0) save_array(PRED_FILE, preds) print(preds[:10])
def find_weights(): res101,_ = create_res101(True) res152,_ = create_res152(True) dense201,_ = create_dense201(True) dense161,_ = create_dense161(True) pred1, y1 = make_preds(res101, val_loader) #print(y1) #pred1 = np.random.random((600, 3)) #print(pred1[:5]) pred2, y2 = make_preds(res152, val_loader) #print(y2) pred3, y3 = make_preds(dense201, val_loader) pred4, y4 = make_preds(dense161, val_loader) #_, preds_test = torch.max(torch.from_numpy(pred4), 1) #print(preds_test[:10]) print(pred1.shape) #y1 = np.array(do_clip(one_hot(y1), 0.9)) print(y1[:5]) print(pred1[:5]) print(y1.shape, pred1.shape) #print(y1.shape) #print(log_loss(y1, y2)) #print(log_loss(y3, y2)) #print(log_loss(y3, y4)) print(log_loss(y1, pred1)) print(log_loss(y2, pred2)) #print(log_loss(np.array(one_hot(y2), float32), pred2)) #print(log_loss(np.array(one_hot(y3), float32), pred3)) #print(log_loss(np.array(one_hot(y4), float32), pred4)) y = [] for data, labels in val_loader: for label in labels: y.append(label) print(np.array(y).shape) #print(y) return [pred1,pred2, pred3, pred4], y
def ensemble(): preds_raw = [] os.chdir(MODEL_DIR) for match_str in w_file_matcher: w_files = glob.glob(match_str) for w_file in w_files: full_w_file = MODEL_DIR + '/' + w_file if w_file.startswith('dense161'): model, _ = create_dense161() elif w_file.startswith('dense169'): model, _ = create_dense169() elif w_file.startswith('dense201'): model, _ = create_dense201() elif w_file.startswith('res50'): model, _ = create_res50() elif w_file.startswith('res101'): model, _ = create_res101() elif w_file.startswith('res152'): model, _ = create_res152() elif w_file.startswith('vgg16'): model, _ = create_vgg16() elif w_file.startswith('vgg19'): model, _ = create_vgg19() elif w_file.startswith('inceptionv3'): model, _ = create_inceptionv3() else: pass model.load_state_dict(torch.load(full_w_file)) print(full_w_file) pred = make_preds(model, test_loader) pred = np.array(pred) preds_raw.append(pred) del model save_array(PRED_FILE_RAW, preds_raw) preds = np.mean(preds_raw, axis=0) save_array(PRED_FILE, preds)
def ensemble(stage): preds_weighted = None total_weight = 0 preds_raw = [] preds_raw_weighted = [] for match_str in w_file_matcher: #print(match_str) os.chdir(MODEL_DIR) w_files = glob.glob(match_str) #print('cur:' + os.getcwd()) for w_file in w_files: full_w_file = MODEL_DIR + '/' + w_file print(full_w_file) if w_file.startswith('dense161'): model = create_dense161() elif w_file.startswith('dense169'): model = create_dense169() elif w_file.startswith('dense201'): model = create_dense201() elif w_file.startswith('dense121'): model = create_dense121() elif w_file.startswith('res50'): model = create_res50() elif w_file.startswith('res101'): model = create_res101() elif w_file.startswith('res152'): model = create_res152() elif w_file.startswith('vgg16bn'): model = create_vgg16bn() elif w_file.startswith('vgg19bn'): model = create_vgg19bn() elif w_file.startswith('vgg19'): model = create_vgg19() elif w_file.startswith('inceptionv3'): model = create_inceptionv3() else: print('No model for {}'.format(full_w_file)) continue model.load_state_dict(torch.load(full_w_file)) pred, filenames = make_preds(model, stage) pred = np.array(pred) preds_raw.append(pred) weight = get_weight(get_acc_from_w_filename(full_w_file)) preds_raw_weighted.append((pred, weight)) if preds_weighted is None: preds_weighted = np.zeros(pred.shape) preds_weighted = preds_weighted + pred * weight total_weight += weight del model preds_weighted = preds_weighted / total_weight save_array(PRED_FILE_RAW, preds_raw) save_array(PRED_FILE_RAW_WEIGHTED, preds_raw_weighted) preds = np.mean(preds_raw, axis=0) save_array(PRED_FILE, preds) save_array(PRED_FILE_WEIGHTED, preds_weighted) save_array(TEST_FILE_NAMES, filenames)