def mlnet_method(X, Y, splits=5): accuracy, cnt = 0, 0 mskf = my_SKF(X, Y, splits) device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu') for X_train, Y_train, X_test, Y_test in mskf: new_X = transform(X_train, Y_train) dataset = ImageDataset(new_X) dataloader = DataLoader(dataset, batch_size=128, shuffle=True, num_workers=4) net = SiameseNetwork().to(device) net.fit(dataloader) net.eval() hit = 0 for i, x in enumerate(X_test): y_pred = knn_pred(net, X_train, Y_train, x, batch_num=1) y_real = Y_test[i] if y_pred == y_real: hit += 1 acc = hit / X_test.shape[0] accuracy += acc cnt += 1 print('Split:{}, Acc:{:.4f}'.format(cnt, acc)) return accuracy / splits
def get_siamese_model(nclasses): device = torch.device("cuda") model = SiameseNetwork() # model.classifier.classifier = nn.Sequential() checkpoint = torch.load('./model/siamese/net_59.pth') model.load_state_dict(checkpoint['model_state_dict']) model.to(device) model.eval() return model
print("metric gogo : {} ".format(last_loss)) #best model check - DBY best_model = (last_loss > best_loss) if best_model: save_model("best_" + str(epoch + 1), model, optimizer, scheduler) best_loss = last_loss print("best epoch {} best loss {} ".format( str(epoch + 1), last_loss)) last_loss = validate(best_prediction_file, model, validate_dataloader, validate_label_file, cuda) print("best prediction file save") time_ = datetime.datetime.now() elapsed = datetime.datetime.now() - time_ print('[epoch {}] elapsed: {}'.format(epoch + 1, elapsed)) # save model #if (epoch+1) % 10 == 0: # save_model(str(epoch + 1), model, optimizer) elif mode == 'test': model.eval() # accuracy 확인 test_dataloader, _ = data_loader(root=DATASET_PATH, phase='test', batch_size=1) prediction_file = "prediction_test.txt" test(prediction_file, model, test_dataloader, cuda)