In [1]:
import pyscreenshot as ImageGrab
import keras


Using Theano backend.
Using gpu device 0: GeForce GTX 1080 (CNMeM is enabled with initial size: 30.0% of memory, cuDNN 5103)
/usr/local/lib/python2.7/dist-packages/theano/sandbox/cuda/__init__.py:600: UserWarning: Your cuDNN version is more recent than the one Theano officially supports. If you see any problems, try updating Theano or downgrading cuDNN to version 5.
  warnings.warn(warn)

model


In [1]:
from keras.utils import np_utils
from keras.callbacks import ModelCheckpoint, EarlyStopping
from itertools import chain
from keras import backend as K
from keras.layers import Dense, Activation, Convolution2D, Convolution3D, MaxPooling2D, MaxPooling3D, BatchNormalization, Flatten, ZeroPadding2D, Dropout
from keras.models import load_model, Sequential
from keras.optimizers import Adam

NB_FRAMES = 5
INPUT_SHAPE = (NB_FRAMES, 91, 118)
# OUTPUT_SHAPE = (1, OUTPUT_ROW, OUTPUT_COL)
OBJECTIVE_FUNCTION = 'binary_crossentropy'
METRICS = ['binary_crossentropy', 'accuracy']

def center_normalize(x):
    """Custom activation for online sample-wise center and std. normalization."""
    return (x - K.mean(x)) / K.std(x)

def VGG_16(weights_path=None):
    model = Sequential()
    model.add(Activation(activation=center_normalize, input_shape=INPUT_SHAPE))
    model.add(ZeroPadding2D((1,1), input_shape=INPUT_SHAPE))
    # model.add(ZeroPadding2D((1,1),input_shape=(2, INPUT_ROW, INPUT_COL)))
    model.add(Convolution2D(64, 3, 3, activation='relu'))
    model.add(ZeroPadding2D((1,1)))
    model.add(Convolution2D(64, 3, 3, activation='relu'))
    model.add(MaxPooling2D((2,2), strides=(2,2)))

    model.add(ZeroPadding2D((1,1)))
    model.add(Convolution2D(128, 3, 3, activation='relu'))
    model.add(ZeroPadding2D((1,1)))
    model.add(Convolution2D(128, 3, 3, activation='relu'))
    model.add(MaxPooling2D((2,2), strides=(2,2)))

    model.add(ZeroPadding2D((1,1)))
    model.add(Convolution2D(256, 3, 3, activation='relu'))
    model.add(ZeroPadding2D((1,1)))
    model.add(Convolution2D(256, 3, 3, activation='relu'))
    model.add(ZeroPadding2D((1,1)))
    model.add(Convolution2D(256, 3, 3, activation='relu'))
    model.add(MaxPooling2D((2,2), strides=(2,2)))

    model.add(ZeroPadding2D((1,1)))
    model.add(Convolution2D(512, 3, 3, activation='relu'))
    model.add(ZeroPadding2D((1,1)))
    model.add(Convolution2D(512, 3, 3, activation='relu'))
    model.add(ZeroPadding2D((1,1)))
    model.add(Convolution2D(512, 3, 3, activation='relu'))
    model.add(MaxPooling2D((2,2), strides=(2,2)))

    model.add(ZeroPadding2D((1,1)))
    model.add(Convolution2D(512, 3, 3, activation='relu'))
    model.add(ZeroPadding2D((1,1)))
    model.add(Convolution2D(512, 3, 3, activation='relu'))
    model.add(ZeroPadding2D((1,1)))
    model.add(Convolution2D(512, 3, 3, activation='relu'))
    model.add(MaxPooling2D((2,2), strides=(2,2)))

    model.add(Flatten())
    model.add(Dense(256, activation='relu'))
    model.add(Dropout(0.5))
    model.add(Dense(256, activation='relu'))
    model.add(Dropout(0.5))
    model.add(Dense(9, activation='softmax'))
    model.compile(optimizer=Adam(lr=1e-5), loss=OBJECTIVE_FUNCTION, metrics=METRICS)

    return model

model = VGG_16()
model.summary()


Using Theano backend.
Using gpu device 0: GeForce GTX 1080 (CNMeM is enabled with initial size: 30.0% of memory, cuDNN 5103)
/usr/local/lib/python2.7/dist-packages/theano/sandbox/cuda/__init__.py:600: UserWarning: Your cuDNN version is more recent than the one Theano officially supports. If you see any problems, try updating Theano or downgrading cuDNN to version 5.
  warnings.warn(warn)
____________________________________________________________________________________________________
Layer (type)                     Output Shape          Param #     Connected to                     
====================================================================================================
activation_1 (Activation)        (None, 5, 91, 118)    0           activation_input_1[0][0]         
____________________________________________________________________________________________________
zeropadding2d_1 (ZeroPadding2D)  (None, 5, 93, 120)    0           activation_1[0][0]               
____________________________________________________________________________________________________
convolution2d_1 (Convolution2D)  (None, 64, 91, 118)   2944        zeropadding2d_1[0][0]            
____________________________________________________________________________________________________
zeropadding2d_2 (ZeroPadding2D)  (None, 64, 93, 120)   0           convolution2d_1[0][0]            
____________________________________________________________________________________________________
convolution2d_2 (Convolution2D)  (None, 64, 91, 118)   36928       zeropadding2d_2[0][0]            
____________________________________________________________________________________________________
maxpooling2d_1 (MaxPooling2D)    (None, 64, 45, 59)    0           convolution2d_2[0][0]            
____________________________________________________________________________________________________
zeropadding2d_3 (ZeroPadding2D)  (None, 64, 47, 61)    0           maxpooling2d_1[0][0]             
____________________________________________________________________________________________________
convolution2d_3 (Convolution2D)  (None, 128, 45, 59)   73856       zeropadding2d_3[0][0]            
____________________________________________________________________________________________________
zeropadding2d_4 (ZeroPadding2D)  (None, 128, 47, 61)   0           convolution2d_3[0][0]            
____________________________________________________________________________________________________
convolution2d_4 (Convolution2D)  (None, 128, 45, 59)   147584      zeropadding2d_4[0][0]            
____________________________________________________________________________________________________
maxpooling2d_2 (MaxPooling2D)    (None, 128, 22, 29)   0           convolution2d_4[0][0]            
____________________________________________________________________________________________________
zeropadding2d_5 (ZeroPadding2D)  (None, 128, 24, 31)   0           maxpooling2d_2[0][0]             
____________________________________________________________________________________________________
convolution2d_5 (Convolution2D)  (None, 256, 22, 29)   295168      zeropadding2d_5[0][0]            
____________________________________________________________________________________________________
zeropadding2d_6 (ZeroPadding2D)  (None, 256, 24, 31)   0           convolution2d_5[0][0]            
____________________________________________________________________________________________________
convolution2d_6 (Convolution2D)  (None, 256, 22, 29)   590080      zeropadding2d_6[0][0]            
____________________________________________________________________________________________________
zeropadding2d_7 (ZeroPadding2D)  (None, 256, 24, 31)   0           convolution2d_6[0][0]            
____________________________________________________________________________________________________
convolution2d_7 (Convolution2D)  (None, 256, 22, 29)   590080      zeropadding2d_7[0][0]            
____________________________________________________________________________________________________
maxpooling2d_3 (MaxPooling2D)    (None, 256, 11, 14)   0           convolution2d_7[0][0]            
____________________________________________________________________________________________________
zeropadding2d_8 (ZeroPadding2D)  (None, 256, 13, 16)   0           maxpooling2d_3[0][0]             
____________________________________________________________________________________________________
convolution2d_8 (Convolution2D)  (None, 512, 11, 14)   1180160     zeropadding2d_8[0][0]            
____________________________________________________________________________________________________
zeropadding2d_9 (ZeroPadding2D)  (None, 512, 13, 16)   0           convolution2d_8[0][0]            
____________________________________________________________________________________________________
convolution2d_9 (Convolution2D)  (None, 512, 11, 14)   2359808     zeropadding2d_9[0][0]            
____________________________________________________________________________________________________
zeropadding2d_10 (ZeroPadding2D) (None, 512, 13, 16)   0           convolution2d_9[0][0]            
____________________________________________________________________________________________________
convolution2d_10 (Convolution2D) (None, 512, 11, 14)   2359808     zeropadding2d_10[0][0]           
____________________________________________________________________________________________________
maxpooling2d_4 (MaxPooling2D)    (None, 512, 5, 7)     0           convolution2d_10[0][0]           
____________________________________________________________________________________________________
zeropadding2d_11 (ZeroPadding2D) (None, 512, 7, 9)     0           maxpooling2d_4[0][0]             
____________________________________________________________________________________________________
convolution2d_11 (Convolution2D) (None, 512, 5, 7)     2359808     zeropadding2d_11[0][0]           
____________________________________________________________________________________________________
zeropadding2d_12 (ZeroPadding2D) (None, 512, 7, 9)     0           convolution2d_11[0][0]           
____________________________________________________________________________________________________
convolution2d_12 (Convolution2D) (None, 512, 5, 7)     2359808     zeropadding2d_12[0][0]           
____________________________________________________________________________________________________
zeropadding2d_13 (ZeroPadding2D) (None, 512, 7, 9)     0           convolution2d_12[0][0]           
____________________________________________________________________________________________________
convolution2d_13 (Convolution2D) (None, 512, 5, 7)     2359808     zeropadding2d_13[0][0]           
____________________________________________________________________________________________________
maxpooling2d_5 (MaxPooling2D)    (None, 512, 2, 3)     0           convolution2d_13[0][0]           
____________________________________________________________________________________________________
flatten_1 (Flatten)              (None, 3072)          0           maxpooling2d_5[0][0]             
____________________________________________________________________________________________________
dense_1 (Dense)                  (None, 256)           786688      flatten_1[0][0]                  
____________________________________________________________________________________________________
dropout_1 (Dropout)              (None, 256)           0           dense_1[0][0]                    
____________________________________________________________________________________________________
dense_2 (Dense)                  (None, 256)           65792       dropout_1[0][0]                  
____________________________________________________________________________________________________
dropout_2 (Dropout)              (None, 256)           0           dense_2[0][0]                    
____________________________________________________________________________________________________
dense_3 (Dense)                  (None, 9)             2313        dropout_2[0][0]                  
====================================================================================================
Total params: 15570633
____________________________________________________________________________________________________

In [2]:
from keras.optimizers import sgd
from keras.optimizers import RMSprop

def dummy_model():  
    model = Sequential()
    model.add(Convolution2D(16, nb_row=3, nb_col=3, activation='relu', input_shape=INPUT_SHAPE))
    model.add(Convolution2D(32, nb_row=3, nb_col=3, activation='relu'))
    model.add(Flatten())
    model.add(Dense(256, activation='relu'))
    model.add(Dense(9))
    model.compile(RMSprop(), 'MSE')
    return model

model = dummy_model()

get screenshot


In [24]:
import numpy as np
import pyscreenshot as ImageGrab
from scipy import stats
im=ImageGrab.grab(bbox=(500,75,1085,530)) # X1,Y1,X2,Y2
# im.show()
# im.save("fim.png")
pix = np.array(im)
pix = rgb2gray(pix)
plt.figure()
plt.imshow(pix, cmap=plt.cm.gray)
plt.show()
print(type(pix))
print(pix.shape)
print(pix.mean(), pix.std(), stats.mstats.mode(pix.ravel()))


<type 'numpy.ndarray'>
(455, 585)
(84.583930789893884, 43.756218700654372, ModeResult(mode=array([ 56.407]), count=array([ 96507.])))

In [22]:
import numpy as np
import pyscreenshot as ImageGrab
from matplotlib import pyplot as plt
im=ImageGrab.grab(bbox=(500,75,1085,530)) # X1,Y1,X2,Y2
# im.show()
# im.save("errou.png")
pix2 = np.array(im)
pix2 = rgb2gray(pix2)
plt.figure()
plt.imshow(pix2, cmap=plt.cm.gray)
plt.show()
print(type(pix2))
print(pix2.shape)
print(pix2.mean(), pix2.std())


<type 'numpy.ndarray'>
(455, 585)
(84.474436494787255, 43.76023958462708)

In [26]:
abs((pix2 - pix).mean()), (pix2 - pix).std(),  stats.mstats.mode((pix2 - pix).ravel())


Out[26]:
(0.10949429510659962,
 50.026935536518934,
 ModeResult(mode=array([ 0.]), count=array([ 77676.])))

joystick


In [ ]:
with open('capture.out', 'rb') as f:
    my_list = pickle.load(f)
    print(my_list)

this is q-learning or a trial


In [43]:
from time import sleep
from qlearning4k.games.game import Game
from pymouse import PyMouse
from pykeyboard import PyKeyboard
import pyscreenshot as ImageGrab
import numpy as np
from matplotlib import pyplot as plt

k = PyKeyboard()

def rgb2gray(rgb):
    return np.dot(rgb[...,:3], [0.299, 0.587, 0.114])

class Mario(Game):
    SLEEP = 0.1
    ACTIONS = {
        0: 'w',
        1: 's',
        2: 'a',
        3: 'd',
        4: 'f',
        5: 'g',
        6: 'r',
        7: 't',
        8: '\\',
    }
    
    def __init__(self, shape):
        self.reset()
        self.state = np.zeros(shape)
        self.update_state()
        k.press_key(self.ACTIONS[0])
        # which you then follow with a release of the key
        sleep(self.SLEEP)
        k.release_key(self.ACTIONS[0])
        self.__score = 10

    @property
    def name(self):
        return "Mario"

    @property
    def nb_actions(self):
        return len(self.ACTIONS.values())

    def reset(self):
        k.press_key(k.function_keys[7])
        sleep(self.SLEEP)
        k.release_key(k.function_keys[7])
        self.__score = 10
        
    def update_state(self):
        im = ImageGrab.grab(bbox=(500,75,1085,530))
        im = im.resize((INPUT_SHAPE[2], INPUT_SHAPE[1]))
        self.last_state = self.state
        self.state = np.array(im)
        self.state = rgb2gray(self.state) / 255.

    def play(self, action):
#         print('this is action: ', action, self.ACTIONS[action])
        k.press_key(self.ACTIONS[action])
        sleep(self.SLEEP)
        k.release_key(self.ACTIONS[action])
        
        self.update_state()
        if abs((self.state - self.last_state).mean()) > 0.01:
            self.__score += 1
        self.__score -= 1.0

    def get_state(self):
        return self.state

    def get_score(self):
        if self.is_over():
            return 0
        else:
            return self.__score
        
    def is_over(self):
        print('\rSCORE: {}'.format(self.__score)),
        if self.__score <= 0:
            print ''
            return True
        else:
            return False
#         return self.state.mean() <= 1.0

    def is_won(self):
        return self.state.mean() > 1.0 and self.state.mean() < 30.0 and self.state.std() < 50.0

In [44]:
import sys
import theano
import os
sys.path.append(os.path.abspath('.'))
from agent import Agent
from time import sleep
theano.config.exception_verbosity = 'high'
theano.config.optimizer = 'fast_compile'
sleep(3)
print('Start!')

mario = Mario((INPUT_SHAPE[1], INPUT_SHAPE[2]))
agent = Agent(model=model, nb_frames=NB_FRAMES)
agent.train(mario, batch_size=2, nb_epoch=10000, epsilon=.1)
# agent.play(mario)


Start!
SCORE: 0.0 SCORE: 9.0 SCORE: 8.0 SCORE: 8.0 SCORE: 8.0 SCORE: 7.0 SCORE: 6.0 SCORE: 5.0 SCORE: 4.0 SCORE: 3.0 SCORE: 2.0 SCORE: 2.0 SCORE: 2.0 SCORE: 1.0 
SCORE: 0.0 
Epoch 001/1000 | Loss 12.9197 | Epsilon 0.10 | Win count 0
SCORE: 0.0 SCORE: 9.0 SCORE: 8.0 SCORE: 7.0 SCORE: 6.0 SCORE: 5.0 SCORE: 4.0 SCORE: 3.0 SCORE: 2.0 SCORE: 1.0 
SCORE: 0.0 
Epoch 002/1000 | Loss 54.9049 | Epsilon 0.10 | Win count 0
SCORE: 0.0 SCORE: 9.0 SCORE: 8.0 SCORE: 7.0 SCORE: 6.0 SCORE: 5.0 SCORE: 4.0 SCORE: 3.0 SCORE: 2.0 SCORE: 1.0 
SCORE: 0.0 
Epoch 003/1000 | Loss 69.7982 | Epsilon 0.10 | Win count 0
SCORE: 0.0 SCORE: 9.0 SCORE: 8.0 SCORE: 7.0 SCORE: 6.0 SCORE: 5.0 SCORE: 4.0 SCORE: 3.0 SCORE: 2.0 SCORE: 1.0 
SCORE: 0.0 
Epoch 004/1000 | Loss 203.2071 | Epsilon 0.10 | Win count 0
SCORE: 6.0 SCORE: 9.0 SCORE: 8.0 SCORE: 8.0 SCORE: 8.0 SCORE: 7.0 SCORE: 6.0
/usr/lib/python2.7/dist-packages/gtk-2.0/gtk/__init__.py:40: RuntimeWarning: tp_compare didn't return -1 or -2 for exception
  from gtk import _gtk
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
ImportError: could not import gio
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
ImportError: could not import gio
---------------------------------------------------------------------------
KeyboardInterrupt                         Traceback (most recent call last)
<ipython-input-44-bc9d53165eb1> in <module>()
     12 mario = Mario((INPUT_SHAPE[1], INPUT_SHAPE[2]))
     13 agent = Agent(model=model, nb_frames=NB_FRAMES)
---> 14 agent.train(mario, batch_size=2, nb_epoch=1000, epsilon=.1)
     15 # agent.play(mario)

/home/violenta/Projects/deep_mario/agent.py in train(self, game, nb_epoch, batch_size, gamma, epsilon, epsilon_rate, reset_memory, observe, checkpoint)
     84                     q = model.predict(S)
     85                     a = int(np.argmax(q[0]))
---> 86                 game.play(a)
     87                 r = game.get_score()
     88                 S_prime = self.get_game_data(game)

<ipython-input-43-5f7062f5935e> in play(self, action)
     63         k.release_key(self.ACTIONS[action])
     64 
---> 65         self.update_state()
     66         if abs((self.state - self.last_state).mean()) > 0.01:
     67             self.__score += 1

<ipython-input-43-5f7062f5935e> in update_state(self)
     51 
     52     def update_state(self):
---> 53         im = ImageGrab.grab(bbox=(500,75,1085,530))
     54         im = im.resize((INPUT_SHAPE[2], INPUT_SHAPE[1]))
     55         self.last_state = self.state

/usr/local/lib/python2.7/dist-packages/pyscreenshot/__init__.pyc in grab(bbox, childprocess, backend)
     44 
     45     """
---> 46     return _grab(to_file=False, childprocess=childprocess, backend=backend, bbox=bbox)
     47 
     48 

/usr/local/lib/python2.7/dist-packages/pyscreenshot/__init__.pyc in _grab(to_file, childprocess, backend, bbox, filename)
     27     if childprocess:
     28         log.debug('running "%s" in child process', backend)
---> 29         return run_in_childprocess(_grab_simple, imcodec.codec, to_file, backend, bbox, filename)
     30     else:
     31         return _grab_simple(to_file, backend, bbox, filename)

/usr/local/lib/python2.7/dist-packages/pyscreenshot/procutil.pyc in run_in_childprocess(target, codec, *args, **kwargs)
     27     p = Process(target=_wrapper, args=(target, codec, queue,  args, kwargs))
     28     p.start()
---> 29     e, r = queue.get()
     30     p.join()
     31 

/usr/lib/python2.7/multiprocessing/queues.pyc in get(self, block, timeout)
    115             self._rlock.acquire()
    116             try:
--> 117                 res = self._recv()
    118                 self._sem.release()
    119                 return res

KeyboardInterrupt: 

In [ ]:
sleep(3)

k.press_key('w')
# which you then follow with a release of the key
sleep(0.1)
k.release_key('w')
print ('vaca')

In [ ]: