Spaces:
Sleeping
Sleeping
mzimm003
commited on
Commit
·
0154f58
1
Parent(s):
b798daa
Include chess models for pretrain loading.
Browse files- app.py +1 -1
- chessmodels.py +121 -0
- requirements.txt +3 -1
app.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
from my_chess.scripts.scripts import HumanVsBot
|
| 2 |
from my_chess.learner.environments import Chess
|
| 3 |
|
| 4 |
-
from
|
| 5 |
|
| 6 |
|
| 7 |
def main(kwargs=None):
|
|
|
|
| 1 |
from my_chess.scripts.scripts import HumanVsBot
|
| 2 |
from my_chess.learner.environments import Chess
|
| 3 |
|
| 4 |
+
from chessmodels import DCMinMax
|
| 5 |
|
| 6 |
|
| 7 |
def main(kwargs=None):
|
chessmodels.py
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pickle
|
| 2 |
+
from pathlib import Path
|
| 3 |
+
from typing import Dict, Type, Union, Literal, List
|
| 4 |
+
|
| 5 |
+
import torch
|
| 6 |
+
|
| 7 |
+
from my_chess.learner.models import (
|
| 8 |
+
Model,
|
| 9 |
+
ModelConfig,
|
| 10 |
+
DeepChessAlphaBeta,
|
| 11 |
+
DeepChessAlphaBetaConfig,
|
| 12 |
+
DeepChessEvaluator,
|
| 13 |
+
DeepChessEvaluatorConfig,
|
| 14 |
+
DeepChessFE,
|
| 15 |
+
DeepChessFEConfig
|
| 16 |
+
)
|
| 17 |
+
from huggingface_hub import PyTorchModelHubMixin
|
| 18 |
+
|
| 19 |
+
class DCFE(DeepChessFE, PyTorchModelHubMixin):
|
| 20 |
+
def __init__(
|
| 21 |
+
self,
|
| 22 |
+
hidden_dims:Union[int, List[int]]=[4096, 1024, 256, 128],
|
| 23 |
+
activations:Union[str, List[str]]='relu',
|
| 24 |
+
batch_norm:bool = True) -> None:
|
| 25 |
+
input_sample = torch.rand(1,8,8,111)
|
| 26 |
+
super().__init__(input_sample, config=DeepChessFEConfig(**dict(
|
| 27 |
+
hidden_dims=hidden_dims,
|
| 28 |
+
activations=activations,
|
| 29 |
+
batch_norm=batch_norm,
|
| 30 |
+
)))
|
| 31 |
+
|
| 32 |
+
class DCEvaluator(DeepChessEvaluator, PyTorchModelHubMixin):
|
| 33 |
+
def __init__(
|
| 34 |
+
self,
|
| 35 |
+
feature_extractor:Type[Model]=None,
|
| 36 |
+
feature_extractor_config:ModelConfig=None,
|
| 37 |
+
feature_extractor_param_dir:Union[str, Path]=None,
|
| 38 |
+
hidden_dims:Union[int, List[int]]=[512, 252, 128],
|
| 39 |
+
activations:Union[str, List[str]]='relu',
|
| 40 |
+
batch_norm:bool=True,) -> None:
|
| 41 |
+
input_sample = torch.rand(1,8,8,111)
|
| 42 |
+
if feature_extractor is None:
|
| 43 |
+
feature_extractor = DCFE.from_pretrained("mzimm003/DeepChessReplicationFeatureExtractor")
|
| 44 |
+
super().__init__(input_sample, config=DeepChessEvaluatorConfig(**dict(
|
| 45 |
+
feature_extractor=feature_extractor,
|
| 46 |
+
feature_extractor_config=feature_extractor_config,
|
| 47 |
+
feature_extractor_param_dir=feature_extractor_param_dir,
|
| 48 |
+
hidden_dims=hidden_dims,
|
| 49 |
+
activations=activations,
|
| 50 |
+
batch_norm=batch_norm,
|
| 51 |
+
)))
|
| 52 |
+
|
| 53 |
+
class DCMinMax(DeepChessAlphaBeta, PyTorchModelHubMixin):
|
| 54 |
+
def __init__(
|
| 55 |
+
self,
|
| 56 |
+
board_evaluator:Type[Model]=None,
|
| 57 |
+
board_evaluator_config:ModelConfig=None,
|
| 58 |
+
board_evaluator_param_dir:Union[str, Path]=None,
|
| 59 |
+
max_depth:int = 8,
|
| 60 |
+
iterate_depths:bool = True,
|
| 61 |
+
move_sort:Literal['none', 'random', 'evaluation'] = 'evaluation') -> None:
|
| 62 |
+
input_sample = torch.rand(1,8,8,111)
|
| 63 |
+
if board_evaluator is None:
|
| 64 |
+
board_evaluator = DCFE.from_pretrained("mzimm003/DeepChessReplicationBoardEvaluator")
|
| 65 |
+
super().__init__(input_sample, config=DeepChessAlphaBetaConfig(**dict(
|
| 66 |
+
board_evaluator=board_evaluator,
|
| 67 |
+
board_evaluator_config=board_evaluator_config,
|
| 68 |
+
board_evaluator_param_dir=board_evaluator_param_dir,
|
| 69 |
+
max_depth=max_depth,
|
| 70 |
+
iterate_depths=iterate_depths,
|
| 71 |
+
move_sort=move_sort,
|
| 72 |
+
)))
|
| 73 |
+
|
| 74 |
+
def get_model_attrs(dir:Union[str, Path]):
|
| 75 |
+
best_model_dir = Path(dir).resolve()
|
| 76 |
+
best_model_class = None
|
| 77 |
+
best_model_config = None
|
| 78 |
+
with open(best_model_dir/"params.pkl",'rb') as f:
|
| 79 |
+
x = pickle.load(f)
|
| 80 |
+
best_model_class = x['model']
|
| 81 |
+
best_model_config = x['model_config']
|
| 82 |
+
|
| 83 |
+
latest_checkpoint = sorted(best_model_dir.glob('checkpoint*'), reverse=True)[0]/'model.pt'
|
| 84 |
+
return best_model_class, best_model_config, latest_checkpoint
|
| 85 |
+
|
| 86 |
+
def main1(kwargs=None):
|
| 87 |
+
|
| 88 |
+
mod_cls, mod_config, mod_chkpt = get_model_attrs("/opt/ray/results/ChessFeatureExtractor/AutoEncoder_1557d_00000_0_batch_size=256,model_config=ref_ph_a52f5213,lr=0.0001_2024-07-12_22-30-58")
|
| 89 |
+
model = DCFE(**mod_config.asDict())
|
| 90 |
+
model.load_state_dict(torch.load(mod_chkpt,map_location=torch.device("cuda" if torch.cuda.is_available() else "cpu")))
|
| 91 |
+
model.save_pretrained('/opt/pretrained_models/DCFE')
|
| 92 |
+
model.push_to_hub(
|
| 93 |
+
"mzimm003/DeepChessReplicationFeatureExtractor")
|
| 94 |
+
|
| 95 |
+
mod_cls, mod_config, mod_chkpt = get_model_attrs("/opt/ray/results/DeepChessEvaluator/ChessEvaluation_9866d_00000_0_learning_rate_scheduler_config=step_size_200_gamma_0_9,model_config=ref_ph_a52f5213,lr=0.1000_2024-07-13_09-18-52")
|
| 96 |
+
model = DCEvaluator(**mod_config.asDict())
|
| 97 |
+
model.load_state_dict(torch.load(mod_chkpt,map_location=torch.device("cuda" if torch.cuda.is_available() else "cpu")))
|
| 98 |
+
model.save_pretrained('/opt/pretrained_models/DCEvaluator')
|
| 99 |
+
model.push_to_hub(
|
| 100 |
+
"mzimm003/DeepChessReplicationBoardEvaluator")
|
| 101 |
+
|
| 102 |
+
model = DCMinMax(
|
| 103 |
+
**dict(
|
| 104 |
+
board_evaluator=mod_cls,
|
| 105 |
+
board_evaluator_config=mod_config,
|
| 106 |
+
board_evaluator_param_dir=mod_chkpt,
|
| 107 |
+
max_depth=3,
|
| 108 |
+
move_sort='evaluation'))
|
| 109 |
+
model.save_pretrained('/opt/pretrained_models/DCMinMax')
|
| 110 |
+
model.push_to_hub(
|
| 111 |
+
"mzimm003/DeepChessReplicationMinMax")
|
| 112 |
+
|
| 113 |
+
|
| 114 |
+
def main(kwargs=None):
|
| 115 |
+
modelfe = DCFE.from_pretrained("mzimm003/DeepChessReplicationFeatureExtractor")
|
| 116 |
+
modeleval = DCEvaluator.from_pretrained("mzimm003/DeepChessReplicationBoardEvaluator")
|
| 117 |
+
modelminmax = DCMinMax.from_pretrained("mzimm003/DeepChessReplicationMinMax")
|
| 118 |
+
pass
|
| 119 |
+
|
| 120 |
+
if __name__ == "__main__":
|
| 121 |
+
main()
|
requirements.txt
CHANGED
|
@@ -6,4 +6,6 @@ pygame==2.1.3.dev8
|
|
| 6 |
pettingzoo==1.24.3
|
| 7 |
chess==1.10.0
|
| 8 |
dm_tree==0.1.8
|
| 9 |
-
scikit-image
|
|
|
|
|
|
|
|
|
| 6 |
pettingzoo==1.24.3
|
| 7 |
chess==1.10.0
|
| 8 |
dm_tree==0.1.8
|
| 9 |
+
scikit-image
|
| 10 |
+
posix_ipc==1.1.1
|
| 11 |
+
torchvision==0.14.1
|