Beispiel #1
0
class ModelLoader():
    # Fill the information for your team
    team_name = 'LAG'
    team_member = ["Sree Gowri Addepalli"," Amartya prasad", "Sree Lakshmi Addepalli"]
    round_number = 1
    contact_email = '*****@*****.**'

    def __init__(self, model_file="baseline1.pth"):
        # You should 
        #       1. create the model object
        #       2. load your state_dict
        #       3. call cuda()

        device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
        self.model = Net()
        self.mdl = NetObj()
        self.modelObj = self.mdl.model
        if torch.cuda.device_count() > 1:
            print("Let's use", torch.cuda.device_count(), "GPUs!")
            self.model = nn.DataParallel(self.model)
            self.modelObj = nn.DataParallel(self.modelObj)
        checkpoint = torch.load("baseline1.pth")
        self.state_dict_1 = checkpoint['modelRoadMap_state_dict']
        self.state_dict_2 = checkpoint['modelObjectDetection_state_dict']
        self.model.load_state_dict(self.state_dict_1)
        self.modelObj.load_state_dict(self.state_dict_2)
        self.model.eval()
        self.modelObj.eval()
        self.model.to(device)
        self.modelObj.to(device)
        

    def get_bounding_boxes(self,samples):
        # samples is a cuda tensor with size [batch_size, 6, 3, 256, 306]
        # You need to return a tuple with size 'batch_size' and each element is a cuda tensor [N, 2, 4]
        # where N is the number of object

        batch_size = list(samples.shape)[0]
        # Convert it into [batch_size, 3, 512, 918]
        img_tensor = self.combine_images(samples,batch_size)
        tup_boxes = []
        with torch.no_grad():
            for img in img_tensor:
              prediction = self.modelObj([img.cuda()])
              cbox = self.convertBoundingBoxes(prediction[0]['boxes'])
              #print(cbox.shape)
              tup_boxes.append(cbox)
        return tuple(tup_boxes)

    def get_binary_road_map(self,samples):
        # samples is a cuda tensor with size [batch_size, 6, 3, 256, 306]
        # You need to return a cuda tensor with size [batch_size, 800, 800]
        with torch.no_grad(): 
            batch_size = list(samples.shape)[0]
            sample = samples.reshape(batch_size,18,256,306)
            output = self.model(sample)
            #print(output.shape)
            output = output.reshape(800,800)
            return output


    def combine_images(self, samples, batch_size):
        # samples is a cuda tensor with size [batch_size, 6, 3, 256, 306]
        # You need to return a tuple with size 'batch_size' and each element is a cuda tensor [N, 2, 4]
        # where N is the number of object
        ss = samples.reshape(batch_size, 2, 3, 3, 256, 306)
        t = ss.detach().cpu().clone().numpy().transpose(0, 3, 2, 1, 4, 5)
        # MergingImage
        tp = np.zeros((batch_size, 3, 3, 512, 306))
        for i in range(0, batch_size):
            for j in range(0, 3):
                for k in range(0, 3):
                    tp[i][j][k] = np.vstack([t[i][j][k][0], t[i][j][k][1]])
        tr = np.zeros((batch_size, 3, 512, 918))
        for i in range(0, batch_size):
            for j in range(0, 3):
                tr[i][j] = np.hstack([tp[i][j][0], tp[i][j][1], tp[i][j][2]])
        image_tensor = torch.from_numpy(tr).float()
        return image_tensor

    def convertBoundingBoxes(self, boxes):
        # convert [N,1,4] to [N,2,4]
        if len(boxes) == 0:
            boxes = [[0,0,0,0]]
        convBoxes = []
        for box in boxes:
            xmin = box[0]
            xmin = (xmin - 400)/10
            ymin = box[1]
            ymin = (-ymin +400)/10
            xmax = box[2]
            xmax = (xmax - 400)/10
            ymax = box[3]
            ymax = (-ymax + 400)/10
            cbox = [[xmin,xmin,xmax,xmax], [ymin,ymax,ymin,ymax]]
            convBoxes.append(cbox)
        convBoxes = torch.Tensor(convBoxes)
        return convBoxes
Beispiel #2
0
# import your model.

from Model import Net
model = Net()
print(model)
if torch.cuda.device_count() > 1:
    print("Let's use", torch.cuda.device_count(), "GPUs!")
    model = nn.DataParallel(model)

if args.reloadModel:
        model_fp = "model4/resnet18_model2_17.pth"
        model.load_state_dict(torch.load(model_fp)['modelRoadMap_state_dict'])
        print("model_loaded")


model.to(device)


# In[ ]:


# Optimizer
optimizer = optim.Adam(model.parameters(), lr=args.lr)


# In[ ]:


# loss func

def modify_output_for_loss_fn(loss_fn, output, dim):
if shuffle_dataset:
    np.random.seed(random_seed)
    np.random.shuffle(indices)
train_indices, val_indices = indices[split:], indices[:split]

train_sampler = SubsetRandomSampler(train_indices)
valid_sampler = SubsetRandomSampler(val_indices)

train_load = torch.utils.data.DataLoader(dataset,
                                         batch_size=batch_size,
                                         sampler=train_sampler)

device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
print(device)
net = Net()
net.to(device)
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(net.parameters(), lr=0.01, momentum=0.7)

# Training
for epoch in range(5):
    for i, data in enumerate(train_load, 0):
        inputs, labels, name = data
        inputs = inputs.float()
        inputs, labels = inputs.to(device), labels.to(device)

        # zero the parameter gradients
        optimizer.zero_grad()

        # forward + backward + optimize
        outputs = net(inputs)
Beispiel #4
0
    plt.imshow(np.transpose(npimg, (1, 2, 0)))
    plt.show()


dataiter = iter(train_loader)
images, labels = dataiter.next()

imshow(torchvision.utils.make_grid(images))
print(' '.join('%5s' % classes[labels[j]] for j in range(4)))

#=============Defining Training Parameters and type of Device
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")

model = Net(out_fea=len(classes))
model = model.train()
model = model.to(device)
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(model.parameters(), lr=0.001, momentum=0.9)

step = 0
loss_train = []
loss_val = []

min_loss = 100
patience = 5
training_loss_store = []
validation_loss_store = []

writer = SummaryWriter('writer')

file = open('logs_test4_epoch40_with_max_pool.txt', 'w')