train_classifier_img.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. #!/usr/bin/env python3
  2. import time
  3. import random
  4. import numpy as np
  5. import gym
  6. from gym_minigrid.register import env_list
  7. from gym_minigrid.minigrid import Grid, OBJECT_TO_IDX
  8. import babyai
  9. import torch
  10. import torch.nn as nn
  11. import torch.optim as optim
  12. import torch.nn.functional as F
  13. from torch.autograd import Variable
  14. import torchvision
  15. import numpy as np
  16. import cv2
  17. import PIL
  18. ##############################################################################
  19. def make_var(arr):
  20. arr = np.ascontiguousarray(arr)
  21. #arr = torch.from_numpy(arr).float()
  22. arr = torch.from_numpy(arr)
  23. arr = Variable(arr)
  24. if torch.cuda.is_available():
  25. arr = arr.cuda()
  26. return arr
  27. def init_weights(m):
  28. classname = m.__class__.__name__
  29. if classname.startswith('Conv'):
  30. nn.init.orthogonal_(m.weight.data)
  31. m.bias.data.fill_(0)
  32. elif classname.find('Linear') != -1:
  33. nn.init.xavier_uniform_(m.weight)
  34. m.bias.data.fill_(0)
  35. elif classname.find('BatchNorm') != -1:
  36. m.weight.data.normal_(1.0, 0.02)
  37. m.bias.data.fill_(0)
  38. class ImageBOWEmbedding(nn.Module):
  39. def __init__(self, num_embeddings, embedding_dim, padding_idx=None, reduce_fn=torch.mean):
  40. super(ImageBOWEmbedding, self).__init__()
  41. self.num_embeddings = num_embeddings
  42. self.embedding_dim = embedding_dim
  43. self.padding_idx = padding_idx
  44. self.reduce_fn = reduce_fn
  45. self.embedding = nn.Embedding(num_embeddings, embedding_dim, padding_idx=padding_idx)
  46. def forward(self, inputs):
  47. embeddings = self.embedding(inputs.long())
  48. embeddings = self.reduce_fn(embeddings, dim=1)
  49. embeddings = torch.transpose(embeddings, 1, 3)
  50. embeddings = torch.transpose(embeddings, 2, 3)
  51. return embeddings
  52. class Flatten(nn.Module):
  53. """
  54. Flatten layer, to flatten convolutional layer output
  55. """
  56. def forward(self, input):
  57. return input.view(input.size(0), -1)
  58. class Print(nn.Module):
  59. def forward(self, input):
  60. print(input.size())
  61. return input
  62. class Model(nn.Module):
  63. def __init__(self):
  64. super().__init__()
  65. self.layers = nn.Sequential(
  66. #ImageBOWEmbedding(765, embedding_dim=16, padding_idx=0, reduce_fn=torch.mean),
  67. #nn.Conv2d(in_channels=16, out_channels=64, kernel_size=1),
  68. #nn.LeakyReLU(),
  69. nn.Conv2d(in_channels=3, out_channels=64, kernel_size=6, stride=2),
  70. nn.LeakyReLU(),
  71. nn.Conv2d(in_channels=64, out_channels=64, kernel_size=6, stride=2),
  72. nn.LeakyReLU(),
  73. nn.Conv2d(in_channels=64, out_channels=16, kernel_size=6, stride=2),
  74. nn.LeakyReLU(),
  75. #Print(),
  76. Flatten(),
  77. nn.Linear(144, 128),
  78. nn.LeakyReLU(),
  79. nn.Linear(128, 2),
  80. )
  81. self.apply(init_weights)
  82. def forward(self, obs):
  83. obs = obs / 16
  84. out = self.layers(obs)
  85. return out
  86. def present_prob(self, obs):
  87. obs = make_var(obs).unsqueeze(0)
  88. logits = self(obs)
  89. probs = F.softmax(logits, dim=-1)
  90. probs = probs.detach().cpu().squeeze().numpy()
  91. return probs[1]
  92. env = gym.make('BabyAI-GoToRedBall-v0')
  93. def sample_batch(batch_size=128):
  94. imgs = []
  95. labels = []
  96. for i in range(batch_size):
  97. obs = env.reset()['image']
  98. ball_visible = ('red', 'ball') in Grid.decode(obs)
  99. obs = env.get_obs_render(obs, tile_size=8, mode='rgb_array')
  100. obs = obs.transpose([2, 0, 1])
  101. imgs.append(np.copy(obs))
  102. labels.append(ball_visible)
  103. imgs = np.stack(imgs).astype(np.float32)
  104. labels = np.array(labels, dtype=np.long)
  105. return imgs, labels
  106. print('Generating test set')
  107. test_imgs, test_labels = sample_batch(256)
  108. def eval_model(model):
  109. num_true = 0
  110. for idx in range(test_imgs.shape[0]):
  111. img = test_imgs[idx]
  112. label = test_labels[idx]
  113. p = model.present_prob(img)
  114. out_label = p > 0.5
  115. #print(out_label)
  116. if np.equal(out_label, label):
  117. num_true += 1
  118. #else:
  119. # if label:
  120. # print("incorrectly predicted as absent")
  121. # else:
  122. # print("incorrectly predicted as present")
  123. acc = 100 * (num_true / test_imgs.shape[0])
  124. return acc
  125. ##############################################################################
  126. batch_size = 64
  127. model = Model()
  128. model.cuda()
  129. optimizer = optim.Adam(
  130. model.parameters(),
  131. lr=5e-4
  132. )
  133. criterion = nn.CrossEntropyLoss()
  134. running_loss = None
  135. for batch_no in range(1, 10000):
  136. batch_imgs, labels = sample_batch(batch_size)
  137. batch_imgs = make_var(batch_imgs)
  138. labels = make_var(labels)
  139. pred = model(batch_imgs)
  140. loss = criterion(pred, labels)
  141. optimizer.zero_grad()
  142. loss.backward()
  143. optimizer.step()
  144. loss = loss.data.detach().item()
  145. running_loss = loss if running_loss is None else 0.99 * running_loss + 0.01 * loss
  146. print('batch #{}, frames={}, loss={:.5f}'.format(
  147. batch_no,
  148. batch_no * batch_size,
  149. running_loss
  150. ))
  151. if batch_no % 25 == 0:
  152. acc = eval_model(model)
  153. print('accuracy: {:.2f}%'.format(acc))