|
1 |
| -import io |
2 |
| -import base64 |
3 |
| -from IPython.display import HTML |
4 |
| -import gym |
5 |
| -import numpy as np |
6 | 1 | import cv2
|
7 |
| - |
8 |
| -def play_video(filename, width=None): |
9 |
| - encoded = base64.b64encode(io.open(filename, 'r+b').read()) |
10 |
| - video_width = 'width="' + str(width) + '"' if width is not None else '' |
11 |
| - embedded = HTML(data=''' |
12 |
| - <video controls {0}> |
13 |
| - <source src="data:video/mp4;base64,{1}" type="video/mp4" /> |
14 |
| - </video>'''.format(video_width, encoded.decode('ascii'))) |
15 |
| - |
16 |
| - return embedded |
17 |
| - |
18 |
| - |
19 |
| -def preprocess_pong(image): |
20 |
| - I = image[35:195] # Crop |
21 |
| - I = I[::2, ::2, 0] # Downsample width and height by a factor of 2 |
22 |
| - I[I == 144] = 0 # Remove background type 1 |
23 |
| - I[I == 109] = 0 # Remove background type 2 |
24 |
| - I[I != 0] = 1 # Set remaining elements (paddles, ball, etc.) to 1 |
25 |
| - I = cv2.dilate(I, np.ones((3, 3), np.uint8), iterations=1) |
26 |
| - I = I[::2, ::2, np.newaxis] |
27 |
| - return I.astype(np.float) |
28 |
| - |
29 |
| - |
30 |
| -def pong_change(prev, curr): |
31 |
| - prev = preprocess_pong(prev) |
32 |
| - curr = preprocess_pong(curr) |
33 |
| - I = prev - curr |
34 |
| - # I = (I - I.min()) / (I.max() - I.min() + 1e-10) |
35 |
| - return I |
36 |
| - |
37 |
| - |
38 |
| -class Memory: |
39 |
| - def __init__(self): |
40 |
| - self.clear() |
41 |
| - |
42 |
| - # Resets/restarts the memory buffer |
43 |
| - def clear(self): |
44 |
| - self.observations = [] |
45 |
| - self.actions = [] |
46 |
| - self.rewards = [] |
47 |
| - |
48 |
| - # Add observations, actions, rewards to memory |
49 |
| - def add_to_memory(self, new_observation, new_action, new_reward): |
50 |
| - self.observations.append(new_observation) |
51 |
| - self.actions.append(new_action) |
52 |
| - self.rewards.append(new_reward) |
53 |
| - |
54 |
| - |
55 |
| -def aggregate_memories(memories): |
56 |
| - batch_memory = Memory() |
57 |
| - |
58 |
| - for memory in memories: |
59 |
| - for step in zip(memory.observations, memory.actions, memory.rewards): |
60 |
| - batch_memory.add_to_memory(*step) |
61 |
| - |
62 |
| - return batch_memory |
63 |
| - |
64 |
| - |
65 |
| -def parallelized_collect_rollout(batch_size, envs, model, choose_action): |
66 |
| - |
67 |
| - assert len(envs) == batch_size, "Number of parallel environments must be equal to the batch size." |
68 |
| - |
69 |
| - memories = [Memory() for _ in range(batch_size)] |
70 |
| - next_observations = [single_env.reset() for single_env in envs] |
71 |
| - previous_frames = [obs for obs in next_observations] |
72 |
| - done = [False] * batch_size |
73 |
| - rewards = [0] * batch_size |
74 |
| - |
75 |
| - while True: |
76 |
| - |
77 |
| - current_frames = [obs for obs in next_observations] |
78 |
| - diff_frames = [pong_change(prev, curr) for (prev, curr) in zip(previous_frames, current_frames)] |
79 |
| - |
80 |
| - diff_frames_not_done = [diff_frames[b] for b in range(batch_size) if not done[b]] |
81 |
| - actions_not_done = choose_action(model, np.array(diff_frames_not_done), single=False) |
82 |
| - |
83 |
| - actions = [None] * batch_size |
84 |
| - ind_not_done = 0 |
85 |
| - for b in range(batch_size): |
86 |
| - if not done[b]: |
87 |
| - actions[b] = actions_not_done[ind_not_done] |
88 |
| - ind_not_done += 1 |
89 |
| - |
90 |
| - for b in range(batch_size): |
91 |
| - if done[b]: |
92 |
| - continue |
93 |
| - next_observations[b], rewards[b], done[b], info = envs[b].step(actions[b]) |
94 |
| - previous_frames[b] = current_frames[b] |
95 |
| - memories[b].add_to_memory(diff_frames[b], actions[b], rewards[b]) |
96 |
| - |
97 |
| - if all(done): |
98 |
| - break |
99 |
| - |
100 |
| - return memories |
101 |
| - |
102 |
| - |
103 |
| -def save_video_of_model(model, env_name, suffix=""): |
104 |
| - import skvideo.io |
105 |
| - from pyvirtualdisplay import Display |
106 |
| - display = Display(visible=0, size=(400, 300)) |
107 |
| - display.start() |
108 |
| - |
109 |
| - env = gym.make(env_name) |
110 |
| - obs = env.reset() |
111 |
| - prev_obs = obs |
112 |
| - |
113 |
| - filename = env_name + suffix + ".mp4" |
114 |
| - output_video = skvideo.io.FFmpegWriter(filename) |
115 |
| - |
116 |
| - counter = 0 |
117 |
| - done = False |
118 |
| - while not done: |
119 |
| - frame = env.render(mode='rgb_array') |
120 |
| - output_video.writeFrame(frame) |
121 |
| - |
122 |
| - if "CartPole" in env_name: |
123 |
| - input_obs = obs |
124 |
| - elif "Pong" in env_name: |
125 |
| - input_obs = pong_change(prev_obs, obs) |
| 2 | +import os |
| 3 | +import matplotlib.pyplot as plt |
| 4 | +import numpy as np |
| 5 | +import tensorflow as tf |
| 6 | +import time |
| 7 | +import h5py |
| 8 | +import sys |
| 9 | +import glob |
| 10 | + |
| 11 | +IM_SHAPE = (64, 64, 3) |
| 12 | + |
| 13 | + |
| 14 | +def plot_image_prediction(i, predictions_array, true_label, img): |
| 15 | + predictions_array, true_label, img = predictions_array[i], true_label[i], img[i] |
| 16 | + plt.grid(False) |
| 17 | + plt.xticks([]) |
| 18 | + plt.yticks([]) |
| 19 | + |
| 20 | + plt.imshow(np.squeeze(img), cmap=plt.cm.binary) |
| 21 | + |
| 22 | + predicted_label = np.argmax(predictions_array) |
| 23 | + if predicted_label == true_label: |
| 24 | + color = "blue" |
| 25 | + else: |
| 26 | + color = "red" |
| 27 | + |
| 28 | + plt.xlabel( |
| 29 | + "{} {:2.0f}% ({})".format( |
| 30 | + predicted_label, 100 * np.max(predictions_array), true_label |
| 31 | + ), |
| 32 | + color=color, |
| 33 | + ) |
| 34 | + |
| 35 | + |
| 36 | +def plot_value_prediction(i, predictions_array, true_label): |
| 37 | + predictions_array, true_label = predictions_array[i], true_label[i] |
| 38 | + plt.grid(False) |
| 39 | + plt.xticks([]) |
| 40 | + plt.yticks([]) |
| 41 | + thisplot = plt.bar(range(10), predictions_array, color="#777777") |
| 42 | + plt.ylim([0, 1]) |
| 43 | + predicted_label = np.argmax(predictions_array) |
| 44 | + |
| 45 | + thisplot[predicted_label].set_color("red") |
| 46 | + thisplot[true_label].set_color("blue") |
| 47 | + |
| 48 | + |
| 49 | +class DatasetLoader(tf.keras.utils.Sequence): |
| 50 | + def __init__(self, data_path, batch_size, training=True): |
| 51 | + |
| 52 | + print("Opening {}".format(data_path)) |
| 53 | + sys.stdout.flush() |
| 54 | + |
| 55 | + self.cache = h5py.File(data_path, "r") |
| 56 | + |
| 57 | + print("Loading data into memory...") |
| 58 | + sys.stdout.flush() |
| 59 | + self.images = self.cache["images"][:] |
| 60 | + self.labels = self.cache["labels"][:].astype(np.float32) |
| 61 | + self.image_dims = self.images.shape |
| 62 | + |
| 63 | + train_inds = np.arange(len(self.images)) |
| 64 | + pos_train_inds = train_inds[self.labels[train_inds, 0] == 1.0] |
| 65 | + neg_train_inds = train_inds[self.labels[train_inds, 0] != 1.0] |
| 66 | + if training: |
| 67 | + self.pos_train_inds = pos_train_inds[: int(0.7 * len(pos_train_inds))] |
| 68 | + self.neg_train_inds = neg_train_inds[: int(0.7 * len(neg_train_inds))] |
126 | 69 | else:
|
127 |
| - raise ValueError(f"Unknown env for saving: {env_name}") |
128 |
| - |
129 |
| - action = model(np.expand_dims(input_obs, 0)).numpy().argmax() |
130 |
| - |
131 |
| - prev_obs = obs |
132 |
| - obs, reward, done, info = env.step(action) |
133 |
| - counter += 1 |
134 |
| - |
135 |
| - output_video.close() |
136 |
| - print("Successfully saved {} frames into {}!".format(counter, filename)) |
137 |
| - return filename |
138 |
| - |
139 |
| - |
140 |
| -def save_video_of_memory(memory, filename, size=(512,512)): |
141 |
| - import skvideo.io |
142 |
| - |
143 |
| - output_video = skvideo.io.FFmpegWriter(filename) |
144 |
| - |
145 |
| - for observation in memory.observations: |
146 |
| - output_video.writeFrame(cv2.resize(255*observation, size)) |
147 |
| - |
148 |
| - output_video.close() |
149 |
| - return filename |
| 70 | + self.pos_train_inds = pos_train_inds[-1 * int(0.3 * len(pos_train_inds)) :] |
| 71 | + self.neg_train_inds = neg_train_inds[-1 * int(0.3 * len(neg_train_inds)) :] |
| 72 | + |
| 73 | + np.random.shuffle(self.pos_train_inds) |
| 74 | + np.random.shuffle(self.neg_train_inds) |
| 75 | + |
| 76 | + self.train_inds = np.concatenate((self.pos_train_inds, self.neg_train_inds)) |
| 77 | + self.batch_size = batch_size |
| 78 | + |
| 79 | + def get_train_size(self): |
| 80 | + return self.pos_train_inds.shape[0] + self.neg_train_inds.shape[0] |
| 81 | + |
| 82 | + def __len__(self): |
| 83 | + return int(np.floor(self.get_train_size() / self.batch_size)) |
| 84 | + |
| 85 | + def __getitem__(self, index): |
| 86 | + selected_pos_inds = np.random.choice( |
| 87 | + self.pos_train_inds, size=self.batch_size // 2, replace=False |
| 88 | + ) |
| 89 | + selected_neg_inds = np.random.choice( |
| 90 | + self.neg_train_inds, size=self.batch_size // 2, replace=False |
| 91 | + ) |
| 92 | + selected_inds = np.concatenate((selected_pos_inds, selected_neg_inds)) |
| 93 | + |
| 94 | + sorted_inds = np.sort(selected_inds) |
| 95 | + train_img = (self.images[sorted_inds] / 255.0).astype(np.float32) |
| 96 | + train_label = self.labels[sorted_inds, ...] |
| 97 | + inds = np.random.permutation(np.arange(len(train_img))) |
| 98 | + return np.array(train_img[inds]), np.array(train_label[inds]) |
| 99 | + |
| 100 | + def get_n_most_prob_faces(self, prob, n): |
| 101 | + idx = np.argsort(prob)[::-1] |
| 102 | + most_prob_inds = self.pos_train_inds[idx[: 10 * n : 10]] |
| 103 | + return (self.images[most_prob_inds, ...] / 255.0).astype(np.float32) |
| 104 | + |
| 105 | + def get_all_faces(self): |
| 106 | + return (self.images[self.pos_train_inds] / 255.0).astype(np.float32) |
| 107 | + |
| 108 | + def return_sample_batch(self): |
| 109 | + return self.__getitem__(0) |
| 110 | + |
| 111 | + |
| 112 | +def get_test_faces(): |
| 113 | + cwd = os.path.dirname(__file__) |
| 114 | + images = {"LF": [], "LM": [], "DF": [], "DM": []} |
| 115 | + for key in images.keys(): |
| 116 | + files = glob.glob(os.path.join(cwd, "data", "faces", key, "*.png")) |
| 117 | + for file in sorted(files): |
| 118 | + image = cv2.resize(cv2.imread(file), (64, 64))[:, :, ::-1] / 255.0 |
| 119 | + images[key].append(image) |
| 120 | + |
| 121 | + return images["LF"], images["LM"], images["DF"], images["DM"] |
| 122 | + |
| 123 | + |
| 124 | +def plot_k(imgs): |
| 125 | + fig = plt.figure() |
| 126 | + fig.subplots_adjust(hspace=0.6) |
| 127 | + num_images = len(imgs) |
| 128 | + for img in range(num_images): |
| 129 | + ax = fig.add_subplot(int(num_images / 5), 5, img + 1) |
| 130 | + ax.xaxis.set_visible(False) |
| 131 | + ax.yaxis.set_visible(False) |
| 132 | + img_to_show = imgs[img] |
| 133 | + ax.imshow(img_to_show, interpolation="nearest") |
| 134 | + plt.subplots_adjust(wspace=0.20, hspace=0.20) |
| 135 | + plt.show() |
| 136 | + plt.clf() |
| 137 | + |
| 138 | + |
| 139 | +def plot_percentile(imgs): |
| 140 | + fig = plt.figure() |
| 141 | + fig, axs = plt.subplots(1, len(imgs), figsize=(11, 8)) |
| 142 | + for img in range(len(imgs)): |
| 143 | + ax = axs[img] |
| 144 | + ax.xaxis.set_visible(False) |
| 145 | + ax.yaxis.set_visible(False) |
| 146 | + img_to_show = imgs[img] |
| 147 | + ax.imshow(img_to_show, interpolation="nearest") |
0 commit comments