-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Finish collision detection, Q-Table creation script
- Loading branch information
1 parent
73a2f5f
commit 425d5e9
Showing
11 changed files
with
481 additions
and
24,796 deletions.
There are no files selected for viewing
23,779 changes: 119 additions & 23,660 deletions
23,779
RL/.ipynb_checkpoints/Vessel_Collisions-checkpoint.ipynb
Large diffs are not rendered by default.
Oops, something went wrong.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import time | ||
import pickle | ||
import numpy as np | ||
|
||
import neural_thread | ||
|
||
start_q_table = None | ||
SIZE = 150 | ||
|
||
if start_q_table is None: | ||
# initialize the q-table# | ||
q_table = {} | ||
for i in range(-SIZE+1, SIZE): | ||
for ii in range(-SIZE+1, SIZE): | ||
for iii in range(-SIZE+1, SIZE): | ||
for iiii in range(-SIZE+1, SIZE): | ||
print(f"i:{i}, ii:{ii}, iii{iii}, iiii{iii}") | ||
q_table[((i, ii), (iii, iiii))] = [np.random.uniform(-5, 0) for i in range(4)] | ||
else: | ||
with open(start_q_table, "rb") as f: | ||
q_table = pickle.load(f) | ||
|
||
with open(f"qtable-{int(time.time())}.pickle", "wb") as f: | ||
pickle.dump(q_table, f) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import os | ||
import sys | ||
import glob | ||
import math | ||
|
||
import cv2 | ||
import numpy as np | ||
|
||
|
||
################### | ||
# HELPER FUNCTIONS | ||
################### | ||
def order_files_by_date(path_to_folder, file_type): | ||
files = glob.glob("%s*%s" % (path_to_folder, file_type)) | ||
files.sort(key=os.path.getmtime) | ||
return files | ||
|
||
|
||
def get_example_image(image_files, index): | ||
"""Extract an image from a list of file names by index | ||
Arguments: | ||
image_files {[list]} -- [list of image files] | ||
index {[int]} -- [image in the list of files we would like to access] | ||
""" | ||
img = cv2.imread(image_files[index]) | ||
# Threshold all pixels to be entirelly white or black (for some reason some of the pixels were 244 / 1) | ||
img[img > 150] = 255 | ||
img[img < 150] = 0 | ||
print(f"Shape: {img.shape}, Size: {img.size}, Unique: {np.unique(img)}") | ||
return img | ||
|
||
|
||
def euc_dist(p1, p2): | ||
"""Return the euclidean distance between two poitns | ||
Arguments: | ||
p1 {[x, y]} -- [point containing [x, y] coordinates in a list] | ||
p2 {[x, y]} -- [point containing [x, y] coordinates in a list] | ||
Returns: | ||
[int] -- [The euclidean distance between two points] | ||
""" | ||
return math.sqrt(((p1[0] - p2[0]) ** 2) + ((p1[1] - p2[1]) ** 2)) | ||
|
||
|
||
def black_pixel_bool(vessel_img, i, j): | ||
"""Return boolean indicating if a pixel in vessel_img at the ith row and jth column is black | ||
Arguments: | ||
vessel_img {[nparray]} -- [input image of segmented vasculature] | ||
i {[type]} -- [row] | ||
j {[type]} -- [column] | ||
Returns: | ||
[bool] -- [black = true, white = false] | ||
""" | ||
if ( | ||
vessel_img[i][j][0] == 0 | ||
and vessel_img[i][j][1] == 0 | ||
and vessel_img[i][j][2] == 0 | ||
): | ||
return True | ||
else: | ||
return False |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import sys | ||
sys.path.remove('/opt/ros/kinetic/lib/python2.7/dist-packages') | ||
|
||
import cv2 | ||
import math | ||
import numpy as np | ||
|
||
class Thread: # Note, often times in my writing I use thread and lace iterchangably to describe neural lace | ||
def __init__(self, SIZE): | ||
self.SIZE = SIZE | ||
self.x = np.random.randint(0, SIZE) | ||
self.y = np.random.randint(0, SIZE) | ||
|
||
def __str__(self): | ||
return f"x:{self.x}, y:{self.y}" | ||
|
||
def action(self, choice): | ||
"""Choose between 5 actions | ||
Up, Down, Left, Right, or Place Electrode Thread | ||
""" | ||
if choice == 0: | ||
self.move(x=1, y=1) | ||
elif choice == 1: | ||
self.move(x=-1, y=-1) | ||
elif choice == 2: | ||
self.move(x=-1, y=1) | ||
elif choice == 3: | ||
self.move(x=1, y=-1) | ||
|
||
def move(self, x=False, y=False): | ||
# If no value for x, move randomly | ||
if not x: | ||
self.x += np.random.randint(-1, 2) | ||
else: | ||
self.x += x | ||
|
||
# If no value for y, move randomly | ||
if not y: | ||
self.y += np.random.randint(-1, 2) | ||
else: | ||
self.y += y | ||
|
||
# If we are out of bounds, fix! | ||
if self.x < 0: | ||
self.x = 0 | ||
elif self.x > self.SIZE-1: | ||
self.x = self.SIZE-1 | ||
if self.y < 0: | ||
self.y = 0 | ||
elif self.y > self.SIZE-1: | ||
self.y = self.SIZE-1 | ||
|
||
def distance(self, vessel_image, distance_dict): | ||
"""Compute distance between a electrode thread and the nearest blood vessel | ||
Arguments: | ||
vessel_image {[nparray]} -- [input image of segmented vasculature] | ||
""" | ||
for i in range(len(vessel_image)): | ||
for j in range(len(vessel_image)): | ||
if black_pixel_bool(vessel_image, i, j) == True: | ||
result = euc_dist([i, j], [self.y, self.x]) | ||
|
||
distance_dict.add(result, [i, j]) | ||
|
||
# Get the coordinates of the closest vessel | ||
keys_list = [] | ||
for key in distance_dict.keys(): | ||
keys_list.append(key) | ||
|
||
keys_list.sort() | ||
keys_list[0] | ||
|
||
# Return the coordinates of the nearest vessel | ||
return distance_dict[keys_list[0]] | ||
|
||
class Distances(dict): | ||
"""Distance Dictionary Object""" | ||
def __init__(self): | ||
self = dict() | ||
|
||
def add(self, key, value): | ||
self[key] = value |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import sys | ||
#sys.path.remove('/opt/ros/kinetic/lib.python2.7/dist-packages') | ||
|
||
import neur | ||
from helper_functions import * | ||
|
||
import time | ||
import pickle | ||
import cv2 | ||
import numpy as np | ||
from PIL import Image | ||
import matplotlib.pyplot as plt | ||
from matplotlib import style | ||
|
||
style.use('ggplot') | ||
|
||
###################### | ||
# SIMULATION CONSTANTS | ||
###################### | ||
|
||
SIZE = 150 # Size of the thread | ||
|
||
NUM_EPISODES = 25000 | ||
MOVE_PENALTY = 1 | ||
RUPTURE_PENALY = 300 | ||
|
||
epsilon = 0 | ||
EPS_DECAY = 0.9998 # Every episode will be epsilon * EPS_DECAY | ||
SHOW_EVERY = 500 # Render the simulation every 500 iterations | ||
|
||
start_q_table = None | ||
|
||
LEARNING_RATE = 0.1 | ||
DISCOUNT = 0.95 | ||
|
||
left_squares = order_files_by_date( | ||
"/home/andrew/Github/neuralink-bot/Image_Segmentation/segmented_images/square_segmentation_crops/left_crop/", | ||
".jpg") | ||
VESSEL_IMG = get_example_image(left_squares, 0) | ||
|
||
THREAD_K = 1 # Thread key in color dict | ||
VES_K = 2 # Vessel key in color dict | ||
TISSUE_K = 3 # Tieeu key in color dict | ||
|
||
d = {1: (255, 175, 0), | ||
2: (255, 255, 255), # Black Vessels | ||
3: (0, 0, 0)} # White neural tissue | ||
|
||
|
||
################### | ||
# Previous Q-Table? | ||
################### | ||
if start_q_table is None: | ||
# initialize the q-table# | ||
q_table = {} | ||
for i in range(-SIZE+1, SIZE): | ||
for ii in range(-SIZE+1, SIZE): | ||
for iii in range(-SIZE+1, SIZE): | ||
for iiii in range(-SIZE+1, SIZE): | ||
q_table[((i, ii), (iii, iiii))] = [np.random.uniform(-5, 0) for i in range(4)] | ||
else: | ||
with open(start_q_table, "rb") as f: | ||
q_table = pickle.load(f) | ||
|
||
################ | ||
# MAIN SIM BLOCK | ||
################ | ||
episode_rewards = [] | ||
|
||
for episode in range(NUM_EPISODES): | ||
main_thread = thread.Thread(SIZE) | ||
print(main_thread.__str__()) | ||
|
Empty file.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.