Skip to content

Commit 17c79f9

Browse files
committed
added channels_last option for pytorch in mdl dataloader, added padding to classifier, added seed for reproducibility
1 parent d9edfd9 commit 17c79f9

6 files changed

Lines changed: 390 additions & 322 deletions

File tree

lab2/solutions/PT_Part2_Debiasing_Solution.ipynb

Lines changed: 171 additions & 167 deletions
Large diffs are not rendered by default.

mitdeeplearning/lab1.py

Lines changed: 52 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,51 +10,63 @@
1010

1111
cwd = os.path.dirname(__file__)
1212

13+
1314
def load_training_data():
1415
with open(os.path.join(cwd, "data", "irish.abc"), "r") as f:
1516
text = f.read()
1617
songs = extract_song_snippet(text)
1718
return songs
1819

20+
1921
def extract_song_snippet(text):
20-
pattern = '(^|\n\n)(.*?)\n\n'
22+
pattern = "(^|\n\n)(.*?)\n\n"
2123
search_results = re.findall(pattern, text, overlapped=True, flags=re.DOTALL)
2224
songs = [song[1] for song in search_results]
2325
print("Found {} songs in text".format(len(songs)))
2426
return songs
2527

28+
2629
def save_song_to_abc(song, filename="tmp"):
2730
save_name = "{}.abc".format(filename)
2831
with open(save_name, "w") as f:
2932
f.write(song)
3033
return filename
3134

35+
3236
def abc2wav(abc_file):
33-
path_to_tool = os.path.join(cwd, 'bin', 'abc2wav')
37+
path_to_tool = os.path.join(cwd, "bin", "abc2wav")
3438
cmd = "{} {}".format(path_to_tool, abc_file)
3539
return os.system(cmd)
3640

41+
3742
def play_wav(wav_file):
3843
return Audio(wav_file)
3944

45+
4046
def play_song(song):
4147
basename = save_song_to_abc(song)
42-
ret = abc2wav(basename+'.abc')
43-
if ret == 0: #did not suceed
44-
return play_wav(basename+'.wav')
48+
ret = abc2wav(basename + ".abc")
49+
if ret == 0: # did not suceed
50+
return play_wav(basename + ".wav")
4551
return None
4652

53+
4754
def play_generated_song(generated_text):
4855
songs = extract_song_snippet(generated_text)
4956
if len(songs) == 0:
50-
print("No valid songs found in generated text. Try training the \
57+
print(
58+
"No valid songs found in generated text. Try training the \
5159
model longer or increasing the amount of generated music to \
52-
ensure complete songs are generated!")
60+
ensure complete songs are generated!"
61+
)
5362

5463
for song in songs:
5564
play_song(song)
56-
print("None of the songs were valid, try training longer to improve \
57-
syntax.")
65+
print(
66+
"None of the songs were valid, try training longer to improve \
67+
syntax."
68+
)
69+
5870

5971
def test_batch_func_types(func, args):
6072
ret = func(*args)
@@ -64,25 +76,50 @@ def test_batch_func_types(func, args):
6476
print("[PASS] test_batch_func_types")
6577
return True
6678

79+
6780
def test_batch_func_shapes(func, args):
6881
dataset, seq_length, batch_size = args
6982
x, y = func(*args)
7083
correct = (batch_size, seq_length)
71-
assert x.shape == correct, "[FAIL] test_batch_func_shapes: x {} is not correct shape {}".format(x.shape, correct)
72-
assert y.shape == correct, "[FAIL] test_batch_func_shapes: y {} is not correct shape {}".format(y.shape, correct)
84+
assert (
85+
x.shape == correct
86+
), "[FAIL] test_batch_func_shapes: x {} is not correct shape {}".format(
87+
x.shape, correct
88+
)
89+
assert (
90+
y.shape == correct
91+
), "[FAIL] test_batch_func_shapes: y {} is not correct shape {}".format(
92+
y.shape, correct
93+
)
7394
print("[PASS] test_batch_func_shapes")
7495
return True
7596

97+
7698
def test_batch_func_next_step(func, args):
7799
x, y = func(*args)
78-
assert (x[:,1:] == y[:,:-1]).all(), "[FAIL] test_batch_func_next_step: x_{t} must equal y_{t-1} for all t"
100+
assert (
101+
x[:, 1:] == y[:, :-1]
102+
).all(), "[FAIL] test_batch_func_next_step: x_{t} must equal y_{t-1} for all t"
79103
print("[PASS] test_batch_func_next_step")
80104
return True
81105

106+
82107
def test_custom_dense_layer_output(y):
83108
# define the ground truth value for the array
84-
true_y = np.array([[0.27064407, 0.1826951, 0.50374055]],dtype='float32')
85-
assert tf.shape(y).numpy().tolist() == list(true_y.shape), "[FAIL] output is of incorrect shape. expected {} but got {}".format(true_y.shape, y.numpy().shape)
86-
np.testing.assert_almost_equal(y.numpy(), true_y, decimal=7, err_msg="[FAIL] output is of incorrect value. expected {} but got {}".format(true_y, y.numpy()), verbose=True)
109+
true_y = np.array([[0.27064407, 0.1826951, 0.50374055]], dtype="float32")
110+
assert tf.shape(y).numpy().tolist() == list(
111+
true_y.shape
112+
), "[FAIL] output is of incorrect shape. expected {} but got {}".format(
113+
true_y.shape, y.numpy().shape
114+
)
115+
np.testing.assert_almost_equal(
116+
y.numpy(),
117+
true_y,
118+
decimal=7,
119+
err_msg="[FAIL] output is of incorrect value. expected {} but got {}".format(
120+
true_y, y.numpy()
121+
),
122+
verbose=True,
123+
)
87124
print("[PASS] test_custom_dense_layer_output")
88125
return True

mitdeeplearning/lab2.py

Lines changed: 66 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -10,98 +10,114 @@
1010

1111
IM_SHAPE = (64, 64, 3)
1212

13+
1314
def plot_image_prediction(i, predictions_array, true_label, img):
14-
predictions_array, true_label, img = predictions_array[i], true_label[i], img[i]
15-
plt.grid(False)
16-
plt.xticks([])
17-
plt.yticks([])
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)
1821

19-
plt.imshow(np.squeeze(img), cmap=plt.cm.binary)
22+
predicted_label = np.argmax(predictions_array)
23+
if predicted_label == true_label:
24+
color = "blue"
25+
else:
26+
color = "red"
2027

21-
predicted_label = np.argmax(predictions_array)
22-
if predicted_label == true_label:
23-
color = 'blue'
24-
else:
25-
color = 'red'
28+
plt.xlabel(
29+
"{} {:2.0f}% ({})".format(
30+
predicted_label, 100 * np.max(predictions_array), true_label
31+
),
32+
color=color,
33+
)
2634

27-
plt.xlabel("{} {:2.0f}% ({})".format(predicted_label,
28-
100*np.max(predictions_array),
29-
true_label),
30-
color=color)
3135

3236
def plot_value_prediction(i, predictions_array, true_label):
33-
predictions_array, true_label = predictions_array[i], true_label[i]
34-
plt.grid(False)
35-
plt.xticks([])
36-
plt.yticks([])
37-
thisplot = plt.bar(range(10), predictions_array, color="#777777")
38-
plt.ylim([0, 1])
39-
predicted_label = np.argmax(predictions_array)
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)
4044

41-
thisplot[predicted_label].set_color('red')
42-
thisplot[true_label].set_color('blue')
45+
thisplot[predicted_label].set_color("red")
46+
thisplot[true_label].set_color("blue")
4347

4448

4549
class TrainingDatasetLoader(object):
46-
def __init__(self, data_path):
47-
48-
print ("Opening {}".format(data_path))
50+
def __init__(self, data_path, channels_last=True):
51+
print("Opening {}".format(data_path))
4952
sys.stdout.flush()
5053

51-
self.cache = h5py.File(data_path, 'r')
54+
self.cache = h5py.File(data_path, "r")
5255

53-
print ("Loading data into memory...")
56+
print("Loading data into memory...")
5457
sys.stdout.flush()
55-
self.images = self.cache['images'][:]
56-
self.labels = self.cache['labels'][:].astype(np.float32)
58+
self.images = self.cache["images"][:]
59+
self.channels_last = channels_last
60+
self.labels = self.cache["labels"][:].astype(np.float32)
5761
self.image_dims = self.images.shape
5862
n_train_samples = self.image_dims[0]
5963

6064
self.train_inds = np.random.permutation(np.arange(n_train_samples))
6165

62-
self.pos_train_inds = self.train_inds[ self.labels[self.train_inds, 0] == 1.0 ]
63-
self.neg_train_inds = self.train_inds[ self.labels[self.train_inds, 0] != 1.0 ]
66+
self.pos_train_inds = self.train_inds[self.labels[self.train_inds, 0] == 1.0]
67+
self.neg_train_inds = self.train_inds[self.labels[self.train_inds, 0] != 1.0]
6468

6569
def get_train_size(self):
6670
return self.train_inds.shape[0]
6771

6872
def get_train_steps_per_epoch(self, batch_size, factor=10):
69-
return self.get_train_size()//factor//batch_size
73+
return self.get_train_size() // factor // batch_size
7074

7175
def get_batch(self, n, only_faces=False, p_pos=None, p_neg=None, return_inds=False):
7276
if only_faces:
73-
selected_inds = np.random.choice(self.pos_train_inds, size=n, replace=False, p=p_pos)
77+
selected_inds = np.random.choice(
78+
self.pos_train_inds, size=n, replace=False, p=p_pos
79+
)
7480
else:
75-
selected_pos_inds = np.random.choice(self.pos_train_inds, size=n//2, replace=False, p=p_pos)
76-
selected_neg_inds = np.random.choice(self.neg_train_inds, size=n//2, replace=False, p=p_neg)
81+
selected_pos_inds = np.random.choice(
82+
self.pos_train_inds, size=n // 2, replace=False, p=p_pos
83+
)
84+
selected_neg_inds = np.random.choice(
85+
self.neg_train_inds, size=n // 2, replace=False, p=p_neg
86+
)
7787
selected_inds = np.concatenate((selected_pos_inds, selected_neg_inds))
7888

7989
sorted_inds = np.sort(selected_inds)
80-
train_img = (self.images[sorted_inds,:,:,::-1]/255.).astype(np.float32)
81-
train_label = self.labels[sorted_inds,...]
82-
return (train_img, train_label, sorted_inds) if return_inds else (train_img, train_label)
90+
train_img = (self.images[sorted_inds, :, :, ::-1] / 255.0).astype(np.float32)
91+
train_label = self.labels[sorted_inds, ...]
92+
93+
if not self.channels_last:
94+
train_img = np.ascontiguousarray(
95+
np.transpose(train_img, (0, 3, 1, 2))
96+
) # [B, H, W, C] -> [B, C, H, W]
97+
return (
98+
(train_img, train_label, sorted_inds)
99+
if return_inds
100+
else (train_img, train_label)
101+
)
83102

84103
def get_n_most_prob_faces(self, prob, n):
85104
idx = np.argsort(prob)[::-1]
86-
most_prob_inds = self.pos_train_inds[idx[:10*n:10]]
87-
return (self.images[most_prob_inds,...]/255.).astype(np.float32)
105+
most_prob_inds = self.pos_train_inds[idx[: 10 * n : 10]]
106+
return (self.images[most_prob_inds, ...] / 255.0).astype(np.float32)
88107

89108
def get_all_train_faces(self):
90-
return self.images[ self.pos_train_inds ]
109+
return self.images[self.pos_train_inds]
91110

92111

93-
def get_test_faces():
112+
def get_test_faces(channels_last=True):
94113
cwd = os.path.dirname(__file__)
95-
images = {
96-
"LF": [],
97-
"LM": [],
98-
"DF": [],
99-
"DM": []
100-
}
114+
images = {"LF": [], "LM": [], "DF": [], "DM": []}
101115
for key in images.keys():
102116
files = glob.glob(os.path.join(cwd, "data", "faces", key, "*.png"))
103117
for file in sorted(files):
104-
image = cv2.resize(cv2.imread(file), (64,64))[:,:,::-1]/255.
118+
image = cv2.resize(cv2.imread(file), (64, 64))[:, :, ::-1] / 255.0
119+
if not channels_last:
120+
image = np.transpose(image, (2, 0, 1))
105121
images[key].append(image)
106122

107123
return images["LF"], images["LM"], images["DF"], images["DM"]

mitdeeplearning/lab3.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ def plot_value_prediction(i, predictions_array, true_label):
4848

4949
class DatasetLoader(tf.keras.utils.Sequence):
5050
def __init__(self, data_path, batch_size, training=True):
51-
5251
print("Opening {}".format(data_path))
5352
sys.stdout.flush()
5453

0 commit comments

Comments
 (0)