Created
September 19, 2019 13:34
-
-
Save Jimut123/f54b792f5c4e3ad242c545984d82f888 to your computer and use it in GitHub Desktop.
CNN AI's main file: blog(https://jimut123.github.io/blogs/cnn_games_ai.html)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import keras | |
from keras.models import load_model | |
from keras.models import Sequential | |
from keras.layers import Dense, Dropout, Activation, Flatten | |
from keras.layers import Conv2D, MaxPooling2D | |
from sklearn.model_selection import train_test_split | |
import numpy as np | |
from sklearn import preprocessing | |
import cv2 | |
X = [] | |
Y = [] | |
with open ('actions.csv', 'r') as f: | |
for line in f: | |
Y.append(line.rstrip()) | |
all_images = [] | |
img_num = 0 | |
while img_num < 3000: #3000 denotes the total no of input images | |
img = cv2.imread('images/frame_{0}.jpg'.format(img_num), cv2.IMREAD_GRAYSCALE) | |
img = img[:, :, np.newaxis] | |
all_images.append(img) | |
img_num += 1 | |
X = np.array(all_images) | |
# split into test and train set | |
x_train, x_test, y_train, y_test = train_test_split(X, Y, test_size=.2, random_state=5) | |
# convert class vectors to binary class matricies for use in catagorical_crossentropy loss below | |
# number of action classifications | |
classifications = 3 | |
y_train = keras.utils.to_categorical(y_train, classifications) | |
y_test = keras.utils.to_categorical(y_test, classifications) | |
#reference:-https://github.com/keras-team/keras/blob/master/examples/cifar10_cnn.py | |
# CNN model | |
model = Sequential() | |
model.add(Conv2D(32, (3, 3), padding='same', | |
input_shape=x_train.shape[1:])) | |
model.add(Activation('relu')) | |
model.add(Conv2D(32, (3, 3))) | |
model.add(Activation('relu')) | |
model.add(MaxPooling2D(pool_size=(2, 2))) | |
model.add(Dropout(0.25)) | |
model.add(Conv2D(64, (3, 3), padding='same')) | |
model.add(Activation('relu')) | |
model.add(Conv2D(64, (3, 3))) | |
model.add(Activation('relu')) | |
model.add(MaxPooling2D(pool_size=(2, 2))) | |
model.add(Dropout(0.25)) | |
model.add(Flatten()) | |
model.add(Dense(512)) | |
model.add(Activation('relu')) | |
model.add(Dropout(0.5)) | |
model.add(Dense(3)) | |
model.add(Activation('softmax')) | |
# initiate RMSprop optimizer | |
opt = keras.optimizers.rmsprop(lr=0.0001, decay=1e-6) | |
# Let's train the model using RMSprop | |
model.compile(loss='categorical_crossentropy', | |
optimizer=opt, | |
metrics=['accuracy']) | |
x_train = x_train.astype('float32') | |
x_test = x_test.astype('float32') | |
x_train /= 255 | |
x_test /= 255 | |
model.fit(x_train, y_train, | |
batch_size=32, | |
epochs=30, | |
validation_data=(x_test, y_test), | |
shuffle=True) | |
#save weights post training | |
model.save('pong.h5') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment