Guest User

Untitled

a guest
Sep 19th, 2018
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.97 KB | None | 0 0
  1. # import os
  2. # from keras.preprocessing.image import ImageDataGenerator
  3. # from PIL import Image
  4. # from sklearn.metrics import accuracy_score
  5. import gym
  6. import time
  7. import random
  8. from collections import deque
  9.  
  10. from keras.models import Sequential
  11. from keras.layers import Conv2D
  12. from keras.layers import MaxPooling2D
  13. from keras.layers import AveragePooling2D
  14. from keras.layers import GlobalMaxPooling2D
  15. from keras.layers import Dense
  16. from keras.models import model_from_json
  17.  
  18. import win32gui
  19. from PIL import ImageGrab
  20. import cv2
  21. import numpy as np
  22.  
  23.  
  24. def conv_net():
  25.     cnn = Sequential()
  26.     cnn.add(Conv2D(32, (3, 3), input_shape=(128, 128, 3), padding='same', activation='relu'))
  27.     cnn.add(Conv2D(32, (3, 3), input_shape=(128, 128, 3), padding='same', activation='relu'))
  28.     cnn.add(MaxPooling2D(pool_size=(3, 3), padding='same', strides=(2, 2)))
  29.     cnn.add(Conv2D(64, (5, 5), padding='same', activation='relu', strides=(2, 2)))
  30.     cnn.add(Conv2D(64, (5, 5), padding='same', activation='relu', strides=(2, 2)))
  31.     cnn.add(MaxPooling2D(pool_size=(2, 2), padding='same', strides=(2, 2)))
  32.     cnn.add(Conv2D(128, (5, 5), padding='same', activation='relu'))
  33.     cnn.add(Conv2D(128, (5, 5), padding='same', activation='relu'))
  34.     cnn.add(Conv2D(256, (3, 3), padding='same', activation='relu'))
  35.     cnn.add(Conv2D(256, (3, 3), padding='same', activation='relu'))
  36.     cnn.add(AveragePooling2D(pool_size=(3, 3), padding='same'))
  37.     cnn.add(GlobalMaxPooling2D())
  38.     cnn.add(Dense(128, activation='relu'))
  39.     cnn.add(Dense(64, activation='relu'))
  40.     cnn.add(Dense(16, activation='relu'))
  41.     cnn.add(Dense(2, activation='linear'))
  42.     cnn.compile(optimizer='Adam', loss='mse')
  43.     return cnn
  44.  
  45.  
  46. def load_model():
  47.     try:
  48.         raise FileNotFoundError
  49.         json_file = open('CNNmodel.json', 'r')
  50.         loaded_model_json = json_file.read()
  51.         json_file.close()
  52.         cnn = model_from_json(loaded_model_json)
  53.         cnn.load_weights("CNNmodel.h5")
  54.         cnn.compile(optimizer='Adam', loss='mse')
  55.         print(cnn.summary())
  56.     except FileNotFoundError:
  57.         cnn = conv_net()
  58.         print(cnn.summary())
  59.     return cnn
  60.  
  61.  
  62. def save_model(cnn):
  63.     model_json = cnn.to_json()
  64.     with open("CNNmodel.json", "w") as json_file:
  65.         json_file.write(model_json)
  66.     cnn.save_weights("CNNmodel.h5")
  67.  
  68.  
  69. def grab_frame():
  70.     while True:
  71.         try:
  72.             hwnd = (win32gui.GetForegroundWindow())
  73.             bbox = win32gui.GetWindowRect(hwnd)
  74.             bbox = (bbox[0] + 10, bbox[1] + 50, bbox[2] - 5, bbox[3] - 5)
  75.             img = np.asarray(ImageGrab.grab(bbox))
  76.             return img
  77.         except:
  78.             pass
  79.  
  80.  
  81.  
  82. def reshape_frame(image):
  83.     return cv2.resize(image, (128, 128))
  84.  
  85.  
  86. def remember(state, action, reward, next_state, done):
  87.     global memory
  88.     memory.append((state, action, reward, next_state, done))
  89.  
  90.  
  91. def act(state):
  92.     global epsilon, cnn
  93.     if np.random.rand() <= epsilon:
  94.         return random.randrange(action_size)
  95.     act_values = cnn.predict(state.reshape(1, 128, 128, 3))
  96.     return np.argmax(act_values[0])
  97.  
  98.  
  99. def replay(batch_size):
  100.     global epsilon, epsilon_min, epsilon_decay, cnn, memory, gamma
  101.     minibatch = random.sample(memory, batch_size)
  102.     targets = []
  103.     states = []
  104.     for state, action, reward, next_state, done in minibatch:
  105.         target = reward
  106.         if not done:
  107.             target = reward + gamma * np.amax(cnn.predict(next_state.reshape(1, 128, 128, 3))[0])
  108.         target_f = cnn.predict(state.reshape(1, 128, 128, 3))
  109.         target_f[0][action] = target
  110.         targets.append(target_f)
  111.         states.append(state)
  112.     states = np.asarray(states)
  113.     targets = np.asarray(targets)
  114.     cnn.fit(states.reshape(batch_size, 128, 128, 3), targets.reshape(batch_size, 2), epochs=1, verbose=0)
  115.     if epsilon > epsilon_min:
  116.         epsilon *= epsilon_decay
  117.  
  118.  
  119. memory = deque(maxlen=1000)
  120. epsilon = 1.0
  121. epsilon_decay = 0.995
  122. epsilon_min = 0.01
  123. gamma = 0.90
  124. action_size = 2
  125.  
  126. cnn = load_model()
  127.  
  128. env = gym.make("CartPole-v0")
  129. with open("CNN_Log.txt", 'w') as f:
  130.     for games in range(1, 10000):
  131.         env.reset()
  132.         time.sleep(0.1)
  133.         frame = grab_frame()
  134.         s = reshape_frame(frame)
  135.         steps = 0
  136.         for time_t in range(500):
  137.             env.render()
  138.             a = act(s)
  139.             _, r, d, _ = env.step(a)
  140.             print(str(a), end=' ', file=f)
  141.             frame = grab_frame()
  142.             ns = reshape_frame(frame)
  143.             time.sleep(0.1)
  144.             remember(np.asarray(s).reshape(128, 128, 3), a, r, np.asarray(ns).reshape(128, 128, 3), d)
  145.             s = ns
  146.             if d:
  147.                 print("Episode: {}/{}, Score: {}".format(games, 10000, time_t), end='\n', file=f, flush=True)
  148.                 print("Episode: {}/{}, Score: {}".format(games, 10000, time_t))
  149.                 break
  150.             steps += 1
  151.         replay(10)
  152.         if games % 100 == 0:
  153.             save_model(cnn)
  154.  
  155. save_model(cnn)
Add Comment
Please, Sign In to add comment