-
Notifications
You must be signed in to change notification settings - Fork 77
/
utils.py
70 lines (60 loc) · 2.46 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import numpy as np
from pysc2.lib import actions
from pysc2.lib import features
# TODO: preprocessing functions for the following layers
_MINIMAP_PLAYER_ID = features.MINIMAP_FEATURES.player_id.index
_SCREEN_PLAYER_ID = features.SCREEN_FEATURES.player_id.index
_SCREEN_UNIT_TYPE = features.SCREEN_FEATURES.unit_type.index
def preprocess_minimap(minimap):
layers = []
assert minimap.shape[0] == len(features.MINIMAP_FEATURES)
for i in range(len(features.MINIMAP_FEATURES)):
if i == _MINIMAP_PLAYER_ID:
layers.append(minimap[i:i+1] / features.MINIMAP_FEATURES[i].scale)
elif features.MINIMAP_FEATURES[i].type == features.FeatureType.SCALAR:
layers.append(minimap[i:i+1] / features.MINIMAP_FEATURES[i].scale)
else:
layer = np.zeros([features.MINIMAP_FEATURES[i].scale, minimap.shape[1], minimap.shape[2]], dtype=np.float32)
for j in range(features.MINIMAP_FEATURES[i].scale):
indy, indx = (minimap[i] == j).nonzero()
layer[j, indy, indx] = 1
layers.append(layer)
return np.concatenate(layers, axis=0)
def preprocess_screen(screen):
layers = []
assert screen.shape[0] == len(features.SCREEN_FEATURES)
for i in range(len(features.SCREEN_FEATURES)):
if i == _SCREEN_PLAYER_ID or i == _SCREEN_UNIT_TYPE:
layers.append(screen[i:i+1] / features.SCREEN_FEATURES[i].scale)
elif features.SCREEN_FEATURES[i].type == features.FeatureType.SCALAR:
layers.append(screen[i:i+1] / features.SCREEN_FEATURES[i].scale)
else:
layer = np.zeros([features.SCREEN_FEATURES[i].scale, screen.shape[1], screen.shape[2]], dtype=np.float32)
for j in range(features.SCREEN_FEATURES[i].scale):
indy, indx = (screen[i] == j).nonzero()
layer[j, indy, indx] = 1
layers.append(layer)
return np.concatenate(layers, axis=0)
def minimap_channel():
c = 0
for i in range(len(features.MINIMAP_FEATURES)):
if i == _MINIMAP_PLAYER_ID:
c += 1
elif features.MINIMAP_FEATURES[i].type == features.FeatureType.SCALAR:
c += 1
else:
c += features.MINIMAP_FEATURES[i].scale
return c
def screen_channel():
c = 0
for i in range(len(features.SCREEN_FEATURES)):
if i == _SCREEN_PLAYER_ID or i == _SCREEN_UNIT_TYPE:
c += 1
elif features.SCREEN_FEATURES[i].type == features.FeatureType.SCALAR:
c += 1
else:
c += features.SCREEN_FEATURES[i].scale
return c