Skip to content

Commit

Permalink
Merge pull request #167 from masa-su/feature/tutorial_readme
Browse files Browse the repository at this point in the history
added readme
  • Loading branch information
masa-su authored Feb 26, 2021
2 parents 23049ba + 9063894 commit dd6af82
Show file tree
Hide file tree
Showing 6 changed files with 4,639 additions and 420 deletions.
1,075 changes: 658 additions & 417 deletions tutorial/English/04-DeepMarkovModel.ipynb

Large diffs are not rendered by default.

50 changes: 47 additions & 3 deletions tutorial/English/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,65 @@ def __init__(self, pickle_path="cartpole_28.pickle"):
# episode_frames: np.array([episode_num, one_episode_length, height, width, Channels]) (10000, 30, 28, 28, 3)
# actions: np.array([episode_num, one_episode_length]) (10000, 30)
# HWC → CHW
episode_frames = episode_frames.transpose(0, 1, 4, 2, 3)
episode_frames = episode_frames.transpose(0, 1, 4, 2, 3) / 1.0
# print(episode_frames.dtype)
actions = actions[:, :, np.newaxis]

self.episode_frames = torch.from_numpy(episode_frames.astype(np.float32))
self.actions = torch.from_numpy(actions.astype(np.float32))

self.mean = torch.zeros_like(self.episode_frames[0])
self.std = torch.zeros_like(self.episode_frames[0])

self.mean[:, 0, :, :] = 182.6091
self.mean[:, 1, :, :] = 182.6091
self.mean[:, 2, :, :] = 182.6091

self.std[:, 0, :, :] = 45.5565
self.std[:, 1, :, :] = 47.6260
self.std[:, 2, :, :] = 50.7284

def __len__(self):
return len(self.episode_frames)

def __getitem__(self, idx):
return {
"episode_frames": self.episode_frames[idx] / 255,
"episode_frames": (self.episode_frames[idx] - self.mean) / self.std,
"actions": self.actions[idx]
}

def _calculate_mean_std(self):
print(self.episode_frames.shape)
std = torch.std(self.episode_frames, dim=(0, 1, 3, 4))
mean = torch.mean(self.episode_frames, dim=(0, 1, 3, 4))
print("mean: ", mean)
print(mean.shape)
print("std: ", std)
print(std.shape)
# mean: tensor([182.6091, 182.6091, 182.6091])
# torch.Size([3])
# std: tensor([45.5565, 47.6260, 50.7284])
# torch.Size([3])


def postprocess(image):
image_ = image.detach().clone()
# print(image_.shape)
mean = torch.ones_like(image_)
std = torch.ones_like(image_)
mean[:, 0, :, :] = 182.6091
mean[:, 1, :, :] = 182.6091
mean[:, 2, :, :] = 182.6091

std[:, 0, :, :] = 45.5565
std[:, 1, :, :] = 47.6260
std[:, 2, :, :] = 50.7284

image_ = image_ * std + mean
image_ = torch.clamp(image_, min=0.0, max=255.0) / 255.
return image_


if __name__ == "__main__":
pass
data_set = DMMDataset()
data_set._calculate_mean_std()
Loading

0 comments on commit dd6af82

Please sign in to comment.