Esempio n. 1
0
    def __call__(self, draw = False):
        
        # Load model
        net = Net()
        # Trained model
        net.load_state_dict(torch.load('./saved_models/model_checkpoint_kpd.pt')['model'])
        ## print out your net and prepare it for testing (uncomment the line below)
        net.eval()
        
        ## Data preparation
        transformations = transforms.Compose([Rescale(250),
                                             RandomCrop(224),
                                             Normalize(),
                                             ToTensor()])

        # create the transformed dataset
        transformed_data = KeypointsIterableDataset(self.image, self.keypoints, transform=transformations)
        data_loader = DataLoader(transformed_data, num_workers=0)
        
        ## if train flag is set, Start training picking up old checkpoint
        if self.train:
            print("Training...")
            ## Run each record twice for training.
            n_epochs = 2
            train_net(n_epochs, data_loader, net)
        
        ## Get the prediction
        print("Predicting...")
        test_images, test_pts, gt_pts, sample = test_net(data_loader, net)
    
        if draw:
            visualize_output(test_images, test_pts)
        # Rescaled.
        return InverseTransform()(sample)
Esempio n. 2
0
def loading_model(model_path=BEST_MODEL_SAVE_PATH):
    model = Net(resnet_level=24).to(DEVICE)
    try:
        print('===> Loading the saved model...')
        model.load_state_dict(torch.load(model_path, map_location=DEVICE))
        return model
    except FileNotFoundError:
        print('===> Loading the saved model fail, create a new one...')
        return model
    finally:
        pass
Esempio n. 3
0
class BasicThymio:

    def __init__(self, thymio_name):
        """init"""
        self.net = Net()
        self.net.load_state_dict(torch.load('./cnn', 'cpu'))
        self.net.eval()
        self.thymio_name = thymio_name
        self.transform = transforms.Compose(
            [
                transforms.ToPILImage(),
                transforms.ToTensor()
            ])
        rospy.init_node('basic_thymio_controller', anonymous=True)
        time.sleep(5)

        self.velocity_publisher = rospy.Publisher(self.thymio_name + '/cmd_vel',
                                                  Twist, queue_size=10)
        self.pose_subscriber = rospy.Subscriber(self.thymio_name + '/odom',
                                                Odometry, self.update_state)

        self.camera_subscriber = rospy.Subscriber(self.thymio_name + '/camera/image_raw',
                                                  Image, self.update_image, queue_size=1)

        self.current_pose = Pose()
        self.current_twist = Twist()
        self.rate = rospy.Rate(10)

    def thymio_state_service_request(self, position, orientation):
        """Request the service (set thymio state values) exposed by
        the simulated thymio. A teleportation tool, by default in gazebo world frame.
        Be aware, this does not mean a reset (e.g. odometry values)."""
        rospy.wait_for_service('/gazebo/set_model_state')
        try:
            model_state = ModelState()
            model_state.model_name = self.thymio_name
            model_state.reference_frame = ''  # the frame for the pose information
            model_state.pose.position.x = position[0]
            model_state.pose.position.y = position[1]
            model_state.pose.position.z = position[2]
            qto = quaternion_from_euler(
                orientation[0], orientation[1], orientation[2], axes='sxyz')
            model_state.pose.orientation.x = qto[0]
            model_state.pose.orientation.y = qto[1]
            model_state.pose.orientation.z = qto[2]
            model_state.pose.orientation.w = qto[3]
            # a Twist can also be set but not recomended to do it in a service
            gms = rospy.ServiceProxy('/gazebo/set_model_state', SetModelState)
            response = gms(model_state)
            return response
        except rospy.ServiceException, e:
            print "Service call failed: %s" % e
def loading_model():
    if torch.cuda.is_available():
        torch.set_default_tensor_type('torch.cuda.FloatTensor')
    else:
        torch.set_default_tensor_type('torch.FloatTensor')
    model = Net(resnet_level=24).to(DEVICE)
    try:
        print('===> Loading the saved model...')
        model.load_state_dict(torch.load(MODEL_SAVE_PATH, map_location=DEVICE))
        return model
    except FileNotFoundError:
        print('===> Loading the saved model fail, create a new one...')
        return model
    finally:
        pass
def generate_experiment(method='FGSM'):

    # define your model and load pretrained weights
    # TODO
    # model = ...
    model = Net()
    model = model.load_state_dict(
        torch.load("/content/drive/My Drive/Colab Notebooks/model64"))

    # cinic class names
    import yaml
    with open('./cinic_classnames.yml', 'r') as fp:
        classnames = yaml.safe_load(fp)

    # load image
    # TODO:
    # img_path = Path(....)
    img_path = Path("/content/test/")
    input_img = Image.open(img_path / "airplane/cifar10-test-10.png")

    # define normalizer and un-normalizer for images
    # cinic
    mean = [0.47889522, 0.47227842, 0.43047404]
    std = [0.24205776, 0.23828046, 0.25874835]

    tf_img = transforms.Compose([
        # transforms.Resize((224, 224)),
        transforms.ToTensor(),
        transforms.Normalize(mean=mean, std=std)
    ])
    un_norm = transforms.Compose([
        transforms.Normalize(mean=[-m / s for m, s in zip(mean, std)],
                             std=[1 / s for s in std]),
        Clamp(),
        transforms.ToPILImage()
    ])

    # To be used for iterative method
    # to ensure staying within Linf limits
    clip_min = min([-m / s for m, s in zip(mean, std)])
    clip_max = max([(1 - m) / s for m, s in zip(mean, std)])

    input_tensor = tf_img(input_img)
    attacker = AdversialAttacker(method=method)

    return {
        'img': input_img,
        'inp': input_tensor.unsqueeze(0),
        'attacker': attacker,
        'mdl': model,
        'clip_min': clip_min,
        'clip_max': clip_max,
        'un_norm': un_norm,
        'classnames': classnames
    }
Esempio n. 6
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
import torch
import torch.nn as nn
import torch.nn.functional as F
import torchvision
from Model import Net
from modelobj import NetObj
import numpy as np


model = Net()
mdl = NetObj()
modelObj = mdl.model

checkpoint = torch.load("modelobj/fasterrcnn_model_19.pth")
checkpoint1 = torch.load("model1/resnet18_model2_20.pth")
state_dict_1 = checkpoint1['modelRoadMap_state_dict']
state_dict_2 = checkpoint['modelObjectDetection_state_dict']
model.load_state_dict(state_dict_1)
modelObj.load_state_dict(state_dict_2)
PATH = "baseline1.pth"
torch.save({'modelRoadMap_state_dict': state_dict_1, 'modelObjectDetection_state_dict': state_dict_2},PATH)


Esempio n. 8
0
class Trainer:
    def __init__(self, hidden_dim, buffer_size, gamma, batch_size, device, writer):
        self.env = make("connectx", debug=True)
        self.device = device
        self.policy = Net(self.env.configuration.columns * self.env.configuration.rows, hidden_dim,
                          self.env.configuration.columns).to(
            device)

        self.target = Net(self.env.configuration.columns * self.env.configuration.rows, hidden_dim,
                          self.env.configuration.columns).to(
            device)
        self.enemyNet = Net(self.env.configuration.columns * self.env.configuration.rows, hidden_dim,
                            self.env.configuration.columns).to(
            device)
        self.target.load_state_dict(self.policy.state_dict())
        self.target.eval()
        self.buffer = ExperienceReplay(buffer_size)
        self.enemy = "random"
        self.trainingPair = self.env.train([None, self.enemy])
        self.loss_function = nn.MSELoss()
        self.optimizer = optim.Adam(params=self.policy.parameters(), lr=0.001)
        self.gamma = gamma
        self.batch_size = batch_size

        self.first = True
        self.player = 1
        self.writer = writer

    def agent(self, observation, configuration):
        with torch.no_grad():
            state = torch.tensor(observation['board'], dtype=torch.float)
            reshaped = self.reshape(state)
            action = self.takeAction(self.enemyNet(reshaped).view(-1), reshaped, 0, False)
            return action

    def switch(self):
        self.trainingPair = self.env.train([None, "negamax"])
        self.enemy = "negamax"

    def switchPosition(self):
        self.env.reset()
        if self.first:
            self.trainingPair = self.env.train([self.enemy, None])
            self.player = 2
        else:
            self.trainingPair = self.env.train([None, self.enemy])
            self.player = 1
        self.first = not self.first

    def load(self, path):
        self.policy.load_state_dict(torch.load(path))

    def synchronize(self):
        self.target.load_state_dict(self.policy.state_dict())

    def save(self, name):
        torch.save(self.policy.state_dict(), name)

    def reset(self):
        self.env.reset()
        return self.trainingPair.reset()

    def step(self, action):
        return self.trainingPair.step(action)

    def addExperience(self, experience):
        self.buffer.append(experience)

    def epsilon(self, maxE, minE, episode, lastEpisode):
        return (maxE - minE) * max((lastEpisode - episode) / lastEpisode, 0) + minE

    def change_reward(self, reward, done):
        if done and reward == 1:
            return 10
        if done and reward == -1:
            return -10
        if reward is None and done:
            return -20
        if done:
            return 1
        if reward == 0:
            return 1 / 42
        else:
            return reward

    def change_reward_streak(self, reward, done, reshapedBoard, action, useStreak):
        if done and reward == 1:
            return 20
        if done and reward == -1:
            return -20
        if reward is None and done:
            return -40
        if done:
            return 1
        if reward == 0 & useStreak:
            return 1 / 42 + self.streakReward(self.player, reshapedBoard, action)
        if reward == 0:
            return 1 / 42
        else:
            return reward

    def streakReward(self, player, reshapedBoard, action):
        verticalReward = 0
        horizontalReward = 0
        if self.longestVerticalStreak(player, reshapedBoard, action) == 3:
            verticalReward = 3
        if self.longestHorizontalStreak(player, reshapedBoard, action) == 3:
            horizontalReward = 3
        return verticalReward + horizontalReward + self.longestDiagonalStreak(player, reshapedBoard, action)

    def longestVerticalStreak(self, player, reshapedBoard, action):
        count = 0
        wasZero = False
        for i in range(5, 0, -1):
            if reshapedBoard[0][player][i][action] == 0:
                wasZero = True
            if reshapedBoard[0][player][i][action] == 1 & wasZero:
                count = 0
                wasZero = False
            count += reshapedBoard[0][player][i][action]
        if reshapedBoard[0][0][0][action] == 0:
            return 0
        return count

    def longestHorizontalStreak(self, player, reshapedBoard, action):
        count = 0
        rowOfAction = self.rowOfAction(player, reshapedBoard, action)
        wasZero = False
        for i in range(7):
            if reshapedBoard[0][player][rowOfAction][i] == 0:
                wasZero = True
            if reshapedBoard[0][player][rowOfAction][i] == 1 & wasZero:
                count = 0
                wasZero = False
            count += reshapedBoard[0][player][rowOfAction][i]
        return count

    def longestDiagonalStreak(self, player, reshapedBoard, action):
        rowOfAction = self.rowOfAction(player, reshapedBoard, action)
        for row in range(4):
            for col in range(5):
                if reshapedBoard[0][player][row][col] == reshapedBoard[0][player][row + 1][col + 1] == \
                        reshapedBoard[0][player][row + 2][col + 2] == 1 and self.actionInDiagonal1(action, row, col,
                                                                                                   rowOfAction):
                    return 3
        for row in range(5, 1, -1):
            for col in range(4):
                if reshapedBoard[0][player][row][col] == reshapedBoard[0][player][row - 1][col + 1] == \
                        reshapedBoard[0][player][row - 2][col + 2] == 1 and self.actionInDiagonal2(action, row, col,
                                                                                                   rowOfAction):
                    return 3
        return 0

    def actionInDiagonal1(self, action, row, col, rowOfAction):
        return (rowOfAction == row and action == col or
                rowOfAction == row + 1 and action == col + 1 or
                rowOfAction == row + 2 and action == col + 2)

    def actionInDiagonal2(self, action, row, col, rowOfAction):
        return (rowOfAction == row and action == col or
                rowOfAction == row - 1 and action == col + 1 or
                rowOfAction == row - 2 and action == col + 2)

    def rowOfAction(self, player, reshapedBoard, action):
        rowOfAction = 10
        for i in range(6):
            if reshapedBoard[0][player][i][action] == 1:
                rowOfAction = min(i, rowOfAction)
        return rowOfAction

    def policyAction(self, board, episode, lastEpisode, minEp=0.1, maxEp=0.9):
        reshaped = self.reshape(torch.tensor(board))
        output = self.policy(reshaped).view(-1)
        return self.takeAction(output, reshaped, self.epsilon(maxEp, minEp, episode, lastEpisode))

    def takeAction(self, actionList: torch.tensor, board, epsilon, train=True):
        if (np.random.random() < epsilon) & train:
            # invalide actions rein=geht nicht
            #return torch.tensor(np.random.choice(len(actionList))).item()
            return np.random.choice([i for i in range(len(actionList)) if board[0][0][0][i] == 1])
        else:
            for i in range(7):
                if board[0][0][0][i] == 0:
                    actionList[i] = float('-inf')
            return torch.argmax(actionList).item()

    def reshape(self, board: torch.tensor, unsqz=True):
        tensor = board.view(-1, 7).long()
        # [0] = wo kann er reinwerfen(da wo es geht, steht eine 1), [1] = player1 (da wo es geht steht eine 0), [2] = player2 (da wo es geht steht eine 0)
        a = F.one_hot(tensor, 3).permute([2, 0, 1])
        b = a[:, :, :]
        if unsqz:
            return torch.unsqueeze(b, 0).float().to(self.device)
        return b.float().to(self.device)

    def preprocessState(self, state):
        state = self.reshape(torch.tensor(state), True)
        return state

    def trainActionFromPolicy(self, state, action):
        state = self.preprocessState(state)
        value = self.policy(state).view(-1).to(self.device)
        return value[action].to(self.device)

    def trainActionFromTarget(self, next_state, reward, done):
        next_state = self.preprocessState(next_state)
        target = self.target(next_state)
        target = torch.max(target, 1)[0].item()
        target = reward + ((self.gamma * target) * (1 - done))
        return torch.tensor(target).to(self.device)

    def train(self):
        if len(self.buffer) > self.batch_size:
            self.optimizer.zero_grad()
            states, actions, rewards, next_states, dones = self.buffer.sample(self.batch_size, self.device)
            meanLoss = 0
            for i in range(self.batch_size):
                value = self.trainActionFromPolicy(states[i], actions[i])
                target = self.trainActionFromTarget(next_states[i], rewards[i], dones[i])
                loss = self.loss_function(value, target)
                loss.backward()
                meanLoss += loss
            self.optimizer.step()
            return meanLoss / self.batch_size
Esempio n. 9
0
# In[ ]:


# 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[ ]:
Esempio n. 10
0
    writer.add_scalar('Memory Allocation', torch.cuda.memory_allocated(), step)

print('Finished Training................................................')
file.write(
    'Finished Training................................................\n')
end_training_time = time.time()
file.write('Training time:- ' + str(end_training_time - start_training_time))
file.close()
writer.close()

if (torch.cuda.is_available()):
    torch.cuda.empty_cache()

PATH = 'weight/epoch_3loss_0.2937310039997101.pt'
model = Net(out_fea=len(classes))
model.load_state_dict(torch.load(PATH))
model.eval()

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

# show images
imshow(torchvision.utils.make_grid(images))
# print labels
print('Ground Truth: ', ' '.join('%5s' % classes[labels[j]] for j in range(4)))

outputs = model(images)
_, predicted = torch.max(outputs, 1)

print('Predicted: ', ' '.join('%5s' % classes[predicted[j]] for j in range(4)))
i = outputs.detach().numpy()
Esempio n. 11
0
split = int(np.floor(validation_split * dataset_size))
if shuffle_dataset:
    np.random.seed(random_seed)
    np.random.shuffle(indices)
train_indices, val_indices = indices[split:], indices[:split]

valid_sampler = SubsetRandomSampler(val_indices)

test_load = torch.utils.data.DataLoader(dataset,
                                        batch_size=batch_size,
                                        sampler=valid_sampler)

device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
print(device)
net = Net()
net.load_state_dict(torch.load("./cnn", 'cpu'))
net.eval()
net.to(device)

class_correct = list(0. for i in range(3))
class_total = list(0. for i in range(3))
with torch.no_grad():
    for i, data in enumerate(test_load):
        print("%d/%d" % (i, len(test_load)), end="\r", flush=True)
        images, labels, name = data
        images, labels = images.float().to(device), labels.to(device)
        outputs = net(images)
        _, predicted = torch.max(outputs, 1)
        c = (predicted == labels).squeeze()
        label = labels.item()
        class_correct[label] += c.item()
Esempio n. 12
0
def main():
    global opt, model
    opt = parser.parse_args()
    print(opt)

    cuda = opt.cuda
    if cuda and not torch.cuda.is_available():
        raise Exception("No GPU found, please run without --cuda")

    opt.seed = random.randint(1, 10000)
    print("Random Seed: ", opt.seed)
    torch.manual_seed(opt.seed)
    if cuda:
        torch.cuda.manual_seed(opt.seed)

    cudnn.benchmark = True

    print("===> Loading datasets")
    train_set = DatasetFromHdf5("data/train.h5")
    training_data_loader = DataLoader(dataset=train_set,
                                      num_workers=opt.threads,
                                      batch_size=opt.batchSize,
                                      shuffle=True)

    print("===> Building model")
    model = Net()
    criterion = nn.MSELoss(size_average=False)

    print("===> Setting GPU")
    if cuda:
        model = torch.nn.DataParallel(model).cuda()
        criterion = criterion.cuda()

    # optionally resume from a checkpoint
    if opt.resume:
        if os.path.isfile(opt.resume):
            print("=> loading checkpoint '{}'".format(opt.resume))
            checkpoint = torch.load(opt.resume)
            opt.start_epoch = checkpoint["epoch"] + 1
            model.load_state_dict(checkpoint["model"].state_dict())
        else:
            print("=> no checkpoint found at '{}'".format(opt.resume))

    # optionally copy weights from a checkpoint
    if opt.pretrained:
        if os.path.isfile(opt.pretrained):
            print("=> loading model '{}'".format(opt.pretrained))
            weights = torch.load(opt.pretrained)
            model.load_state_dict(weights['model'].state_dict())
        else:
            print("=> no model found at '{}'".format(opt.pretrained))

    print("===> Setting Optimizer")
    optimizer = optim.SGD(model.parameters(),
                          lr=opt.lr,
                          momentum=opt.momentum,
                          weight_decay=opt.weight_decay)

    print("===> Training")
    for epoch in range(opt.start_epoch, opt.nEpochs + 1):
        train(training_data_loader, optimizer, model, criterion, epoch)
        save_checkpoint(model, epoch)