コード例 #1
0
import sys, os
sys.path.append(os.path.dirname(os.path.abspath(__file__))+"/game")
from main import Main
from PIL import Image
import time
from skimage import color, transform, exposure

game = Main()

from config import num_of_cols, num_of_rows

for i in range(9999999):
    _, test, _ = game.MainLoop(3)
    if i>200:
        gray = color.rgb2gray(test)
        resized_gray = transform.resize(gray,(num_of_cols,num_of_rows))
        result = Image.fromarray(test, 'RGB')
        result.show()
        result.save('screenshots/screenshot.png')

        #We have the details of all pixels on a screen on this frame. next step is convert it to grayscale and resize it to 80x80

        result = Image.fromarray(gray*255)
        result.show()
        result.convert('RGB').save('screenshots/grayscale.png')
        result = Image.fromarray(resized_gray*255)
        result.show()
        result.convert('RGB').save('screenshots/downsized.png')

        import time
        time.sleep(99999)
コード例 #2
0
#Convolves 32 filters size 8x8 with stride = 4
model = Sequential()
model.add(Conv2D(32, kernel_size=(8,8), strides=(4, 4), activation='relu',input_shape=(num_of_cols,num_of_rows,img_channels)))
model.add(MaxPooling2D(pool_size=(4,4), strides=(2, 2), padding='same'))
model.add(Conv2D(64, kernel_size=(4,4), strides=(2, 2), activation='relu'))
model.add(Conv2D(128, kernel_size=(2,2), strides=(2, 2), activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2), strides=(1, 1), padding='same'))
model.add(Flatten())
model.add(Dense(num_of_hidden_layer_neurons, activation='relu'))
model.add(Dense(num_of_actions))
model.compile(loss='mse',optimizer='adam')
#plot_model(model, to_file='model.png')
model.load_weights("weights.hdf5")
#Start game and press 'x' so we enter the game.
start_game = game.MainLoop(3)


#Obtain the starting state
r_0, s_t, s_f = game.MainLoop(3)
#Failsafe press of x - sometimes startup lags affects ability to enter the game successfully
#pyautogui.press('x')
#Turn our screenshot to gray scale, resize to num_of_cols*num_of_rows, and make pixels in 0-255 range
s_t = color.rgb2gray(s_t)
s_t = transform.resize(s_t,(num_of_cols,num_of_rows))
s_t = exposure.rescale_intensity(s_t,out_range=(0,255))
s_t = np.stack((s_t, s_t, s_t, s_t), axis=2)
#In Keras, need to reshape
s_t = s_t.reshape(1, s_t.shape[0], s_t.shape[1], s_t.shape[2])	#1*num_of_cols*num_of_rows*4

    single_state = exposure.rescale_intensity(single_state, out_range=(0, 255))
    single_state = torch.from_numpy(single_state)
    single_state = single_state.unsqueeze(0).unsqueeze(0)
    single_state = single_state.to(device, dtype=torch.float)
    return single_state


model = DQN().to(device)
model = nn.DataParallel(model)
model.load_state_dict(torch.load('./weights_pytorch'))
optimizer = optim.Adam(model.parameters())
Transition = namedtuple(
    'Transition', ('state', 'action', 'reward', 'next_state', 'is_terminal'))

# Obtain the starting state
_, curr_state, _ = game.MainLoop(3)
# Fail safe press of x - sometimes startup lags affects ability to enter the game successfully
curr_state = transform_state(curr_state)
curr_state = torch.cat((curr_state, curr_state, curr_state, curr_state),
                       1)  # 4 image stack

steps = 0
while True:
    loss = 0  # initialize the loss of the network
    action = 0  # initialize action index
    Q_sa = [0]  # initialize state
    '''    
    The probability of choosing a random action will start at EPS_START
    and will decay exponentially towards EPS_END. EPS_DECAY controls the rate of the decay.
    '''
    eps_threshold = eps_end + (eps_start - eps_end) * math.exp(