Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented SqueezeNet #37

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,10 @@ docs/_build/
# PyBuilder
target/

# Other
*.npy
checkpoint
log/*
*.model.*
*.tfevents.*

56 changes: 56 additions & 0 deletions SqueezeNet/DeepDream.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
from SqueezeNet import *
import time
import numpy as np
import cv2

WIDTH = 160
HEIGHT = 120
LR = 1e-3
EPOCHS = 1000
CLASSES = 3
MODEL_NAME = 'pygta5-car-fast-{}-{}-{}-epochs-300K-data.model'.format(LR, 'squeezenet',EPOCHS)

sess = tf.InteractiveSession()

x = tf.placeholder(tf.float32,(None,WIDTH,HEIGHT,1))
y = tf.placeholder(tf.float32,(None,CLASSES)) # Not used
keep_prob = tf.placeholder(tf.float32) # Set to 1

model,_,_ = getSqueezeNetModel(x,y,keep_prob,LR)
sess.run(tf.global_variables_initializer())
saver = tf.train.Saver()
saver.restore(sess,'./'+MODEL_NAME)

def dream(layer = len(activations)-1,ITERATIONS = 50):
#img_noise = np.random.uniform(size=(WIDTH,HEIGHT))
img_noise = np.ones((WIDTH,HEIGHT)) * .5
total_image = None

for channel in range(activations[layer].get_shape().as_list()[-1]):
try:
t_obj = activations[layer][:,:,:,channel]
except:
t_obj = activations[layer][:,channel]
t_score = tf.reduce_mean(t_obj)
t_grad = tf.gradients(t_score,x)[0]
img = img_noise.copy()
img = np.reshape(img,(1,WIDTH,HEIGHT,1))

for i in range(ITERATIONS):
g,score = sess.run([t_grad,t_score],{x:img,keep_prob:1})
g /= g.std()+1e-8
step = 1
img += g*step
print(channel,score)

img = (img-img.mean())/max(img.std(), 1e-4)*.1 + 0.5
if total_image is None:
total_image = img.reshape((WIDTH,HEIGHT))
else:
total_image = np.hstack((total_image,img.reshape((WIDTH,HEIGHT))))
cv2.imwrite('Total_%s.png'%layer,total_image * 255)

def dreamAll(ITERATIONS = 50):
for i in range(len(activations)):
print('Layer %d'%i)
dream(i,ITERATIONS)
125 changes: 125 additions & 0 deletions SqueezeNet/SqueezeNet.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
'''
SqueezeNet v1.1 (https://github.com/DeepScale/SqueezeNet/tree/master/SqueezeNet_v1.1)
Paper: https://arxiv.org/abs/1602.07360

TODO: Use Xavier initializer
'''

import tensorflow as tf
import numpy as np

#x = tf.placeholder(tf.float32,(None,28*28))
#y = tf.placeholder(tf.float32,(None,10))
#keep_prob = tf.placeholder(tf.float32)

activations = []

def getSqueezeNetModel(x,y,keep_prob,learning_rate=1e-3):
activations.append(x)

classCount = y.get_shape().as_list()[1]

NORM = 1e+2

def fire(inputs,squeezeTo,expandTo):
h = squeeze(inputs,squeezeTo)
h = expand(h,expandTo)
h = tf.clip_by_norm(h,NORM) # Remove if network fails to train
activations.append(h)

def squeeze(inputs,squeezeTo):
with tf.name_scope('squeeze'):
inputSize = inputs.get_shape().as_list()[3]
w = tf.Variable(tf.truncated_normal([1,1,inputSize,squeezeTo]))
h = tf.nn.relu(tf.nn.conv2d(inputs,w,[1,1,1,1],'SAME'))
return h

def expand(inputs,expandTo):
with tf.name_scope('expand'):
squeezeTo = inputs.get_shape().as_list()[3]
w = tf.Variable(tf.truncated_normal([1,1,squeezeTo,expandTo]))
h1x1 = tf.nn.relu(tf.nn.conv2d(inputs,w,[1,1,1,1],'SAME'))
w = tf.Variable(tf.truncated_normal([3,3,squeezeTo,expandTo]))
h3x3 = tf.nn.relu(tf.nn.conv2d(inputs,w,[1,1,1,1],'SAME'))
h = tf.concat(3,[h1x1,h3x3])
return h

# The original paper proposes the use of 6-bit variables which is not
# possible in tensorflow. For 32-bit tensorflow variables and 526,912 trainable
# parameters, the size becomes about 2 MB.

filters = np.array([64,64,128,128,192,192,256,256])
squeezes = np.array([16,16, 32, 32, 48, 48, 64, 64])

with tf.name_scope('conv1'):
w = tf.Variable(tf.truncated_normal([3,3,1,64]))
h = tf.nn.relu(tf.nn.conv2d(activations[-1],w,[1,2,2,1],'SAME'))
activations.append(h)

with tf.name_scope('maxpool1'):
h = tf.nn.max_pool(activations[-1],[1,3,3,1],[1,2,2,1],'SAME')
activations.append(h)

for i in range(0,2):
with tf.name_scope('fire'+str(i+2)):
fire(activations[-1],squeezes[i],filters[i])

with tf.name_scope('maxpool2'):
h = tf.nn.max_pool(activations[-1],[1,3,3,1],[1,2,2,1],'SAME')
activations.append(h)

for i in range(2,4):
with tf.name_scope('fire'+str(i+2)):
fire(activations[-1],squeezes[i],filters[i])

with tf.name_scope('maxpool3'):
h = tf.nn.max_pool(activations[-1],[1,3,3,1],[1,2,2,1],'SAME')
activations.append(h)

for i in range(4,7):
with tf.name_scope('fire'+str(i+2)):
fire(activations[-1],squeezes[i],filters[i])

with tf.name_scope('dropout'):
h = tf.nn.dropout(activations[-1],keep_prob)
activations.append(h)

with tf.name_scope('conv10'):
input_shape = activations[-1].get_shape().as_list()[3]
w = tf.Variable(tf.truncated_normal([1,1,input_shape,classCount]))
h = tf.nn.relu(tf.nn.conv2d(activations[-1],w,[1,1,1,1],'SAME'))
activations.append(h)

with tf.name_scope('avgpool'):
input_shape = activations[-1].get_shape().as_list()
h = tf.nn.avg_pool(activations[-1],[1,input_shape[1],input_shape[2],1],[1,1,1,1],'VALID')
h = tf.squeeze(h,[1,2])
activations.append(h)

y_conv = tf.nn.softmax(activations[-1])
cross_entropy = tf.nn.softmax_cross_entropy_with_logits(activations[-1], y)
train_step = tf.train.AdamOptimizer(learning_rate).minimize(cross_entropy)
correct_prediction = tf.equal(tf.argmax(y_conv,1), tf.argmax(y,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
tf.summary.scalar('accuracy',accuracy)

def getSize():
total_parameters = 0
for variable in tf.trainable_variables():
shape = variable.get_shape()
variable_parametes = 1
for dim in shape:
variable_parametes *= dim.value
print(shape,variable_parametes)
total_parameters += variable_parametes
print('Total trainable variables: ',total_parameters)

for a in activations:
print(a.get_shape())

#getSize()

return (y_conv,train_step,accuracy)



117 changes: 117 additions & 0 deletions test_model_squeeze_net.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
# test_model.py

import numpy as np
from grabscreen import grab_screen
import cv2
import time
from directkeys import PressKey,ReleaseKey, W, A, S, D
from SqueezeNet.SqueezeNet import *
from getkeys import key_check

import random

WIDTH = 160
HEIGHT = 120
LR = 1e-3
EPOCHS = 1000
CLASSES = 3
MODEL_NAME = 'pygta5-car-fast-{}-{}-{}-epochs-300K-data.model'.format(LR, 'squeezenet',EPOCHS)

t_time = 0.09

def straight():
## if random.randrange(4) == 2:
## ReleaseKey(W)
## else:
PressKey(W)
ReleaseKey(A)
ReleaseKey(D)

def left():
PressKey(W)
PressKey(A)
#ReleaseKey(W)
ReleaseKey(D)
#ReleaseKey(A)
time.sleep(t_time)
ReleaseKey(A)

def right():
PressKey(W)
PressKey(D)
ReleaseKey(A)
#ReleaseKey(W)
#ReleaseKey(D)
time.sleep(t_time)
ReleaseKey(D)

sess = tf.InteractiveSession()

x = tf.placeholder(tf.float32,(None,WIDTH,HEIGHT,1))
y = tf.placeholder(tf.float32,(None,CLASSES)) # Not used
keep_prob = tf.placeholder(tf.float32) # Set to 1

model,_,_ = getSqueezeNetModel(x,y,keep_prob,LR)
sess.run(tf.global_variables_initializer())
saver = tf.train.Saver()
saver.restore(sess,'./SqueezeNet/'+MODEL_NAME)

def main():
last_time = time.time()
for i in list(range(4))[::-1]:
print(i+1)
time.sleep(1)

paused = False
while(True):

if not paused:
# 800x600 windowed mode
#screen = np.array(ImageGrab.grab(bbox=(0,40,800,640)))
screen = grab_screen(region=(0,40,800,640))
print('loop took {} seconds'.format(time.time()-last_time))
last_time = time.time()
screen = cv2.cvtColor(screen, cv2.COLOR_BGR2GRAY)
screen = cv2.resize(screen, (WIDTH,HEIGHT))

#prediction = model.predict([screen.reshape(160,120,1)])[0]
prediction = model.eval({x:screen.reshape(1,WIDTH,HEIGHT,1),keep_prob:1})[0]
decision = np.argmax(prediction)
print(decision,prediction)

'''turn_thresh = .75
fwd_thresh = 0.70

if prediction[1] > fwd_thresh:
straight()
elif prediction[0] > turn_thresh:
left()
elif prediction[2] > turn_thresh:
right()
else:
straight()'''

if decision == 0:
left()
elif decision == 1:
straight()
else:
right()



keys = key_check()

# p pauses game and can get annoying.
if 'T' in keys:
if paused:
paused = False
time.sleep(1)
else:
paused = True
ReleaseKey(A)
ReleaseKey(W)
ReleaseKey(D)
time.sleep(1)

main()
64 changes: 64 additions & 0 deletions train_model_squeeze_net.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# train_model.py

from SqueezeNet.SqueezeNet import *
import time
import random

WIDTH = 160
HEIGHT = 120
LR = 1e-3
EPOCHS = 1000
CLASSES = 3
BATCH_SIZE = 64
MODEL_NAME = 'pygta5-car-fast-{}-{}-{}-epochs-300K-data.model'.format(LR, 'squeezenet',EPOCHS)

train_data = np.load('training_data.npy')

c = len(train_data)
train = train_data[:-c//10]
test = train_data[-c//10:]

X = np.array([i[0] for i in train]).reshape(-1,WIDTH,HEIGHT,1)
Y = [i[1] for i in train]

print('Dataset size:',len(X))

test_x = np.array([i[0] for i in test]).reshape(-1,WIDTH,HEIGHT,1)
test_y = [i[1] for i in test]

x = tf.placeholder(tf.float32,(None,WIDTH,HEIGHT,1))
y = tf.placeholder(tf.float32,(None,CLASSES))
keep_prob = tf.placeholder(tf.float32)

_,train_step,accuracy = getSqueezeNetModel(x,y,keep_prob,LR)

with tf.Session() as sess:
merged = tf.summary.merge_all()
summaryWriter = tf.summary.FileWriter('./SqueezeNet/Tensorboard',sess.graph)
sess.run(tf.global_variables_initializer())
run_options = tf.RunOptions(trace_level=tf.RunOptions.FULL_TRACE)
run_metadata = tf.RunMetadata()
saver = tf.train.Saver()

print('Started')
start_time = time.time()
for i in range(EPOCHS):
print(i)
if i%100 == 0:
summary, train_accuracy = sess.run([merged, accuracy],
feed_dict={x: test_x, y: test_y,keep_prob: 1},
options=run_options,
run_metadata=run_metadata)
summaryWriter.add_run_metadata(run_metadata, 'step%03d' % i)
summaryWriter.add_summary(summary, i)
print("step %d, training accuracy %g %f"%(i, train_accuracy,time.time()-start_time))
start_time = time.time()
saver.save(sess,'./SqueezeNet/'+MODEL_NAME)

batch = np.array(random.sample(list(zip(X,Y)),BATCH_SIZE))
batchX = list(batch[:,0])
batchY = list(batch[:,1])
train_step.run(feed_dict={x: batchX, y: batchY,keep_prob:.5})