Ejemplo n.º 1
0
def load_for_training(filename: str, cuda: bool = True) -> PlantModel:
    """
    Load a model checkpoint to make inference.
    Args:
        filename (str): filename/path of the checkpoint.pth
        cuda (bool = True): use cuda
    Returns:
        (FasterRCNNFood) model
    """
    device = torch.device("cuda") if (
        cuda and torch.cuda.is_available()) else torch.device("cpu")
    if Path(filename).exists():
        print("=> loading checkpoint '{}'".format(filename))
        checkpoint = torch.load(filename, map_location=device)
        # Load params
        model_name = checkpoint['model_name']
        # Build model key/architecture
        model = PlantModel(model_name, pretrained=True, num_classes=4)
        # Update model and optimizer
        model.load_state_dict(checkpoint['state_dict'])
        model = model.to(device)
        optimizer = checkpoint['optimizer']
        epoch = checkpoint['epoch']

        print("=> loaded checkpoint '{}'".format(filename))
        return model
    else:
        print("=> no checkpoint found at '{}'".format(filename))
Ejemplo n.º 2
0
    def test_simple_creation(self):
        p1 = PlantModel(scientific_name="Abronia alpina", lifeform="Perennial herb")
        self.assertEqual(p1.id, None)
        p1.save()
        pid = p1.id
        self.assert_(pid > 0)
        print "Saved plant id:", pid
        
        self.assertEqual(p1.scientific_name, "Abronia alpina")

        for n in PlantModel.objects.all().__nodeiter__():
            print "Plant Node: ", n# n['scientific_name']
Ejemplo n.º 3
0
 def initial_plant():
     plant_image_urls = [
         "https://cdn.bmstores.co.uk/images/hpcProductImage/imgFull/297350-Leafy-Plant-Pot.jpg",
         "https://target.scene7.com/is/image/Target/GUEST_fc982b27-ef4f-48a3-bf11-a69d32cc91cc?wid=488&hei=488&fmt=pjpeg",
         "https://images-na.ssl-images-amazon.com/images/I/41NbuQ-wKPL._SX425_.jpg",
         "https://cdn.shopclues.com/images1/thumbnails/92328/320/320/140786749-92328394-1539116494.jpg",
     ]
     plants = PlantModel.query(token.user_pub_id)
     if len(list(plants)) == 0:
         plant = PlantModel()
         plant.user_pub_id = token.user_pub_id
         plant.max_moisture = 100
         plant.min_moisture = 0
         plant.name = "A Dynamo of a Plant"
         plant.image_url = random.choice(plant_image_urls)
         plant.save()
Ejemplo n.º 4
0
def plant(token, plant_id):
    img = request.form.get("image_file")
    for plant in PlantModel.query(token.user_pub_id, PlantModel.pub_id.startswith(plant_id)):
        plant.name = request.form.get("name")
        # plant.image_url = request.form.get("image_url")
        plant.max_moisture = int(request.form.get("max_moisture"))
        plant.min_moisture = int(request.form.get("min_moisture"))
        plant.save()
        return jsonify({})
Ejemplo n.º 5
0
def moisture_get(token, plant_id, metric_type):
    hours = int(request.args.get("hours", 24))
    hours = min(hours, 24*7)
    if metric_type not in ["moisture", "temperature"]:
        return jsonify({"error": f"unknown metric type {metric_type}"})

    plant_qs = PlantModel.query(token.user_pub_id, PlantModel.pub_id.startswith(plant_id))
    plant = list(plant_qs)[0]
    start = datetime.datetime.utcnow() - datetime.timedelta(hours=hours)
    model_class = MoistureReadingModel if metric_type == "moisture" else MetricModel

    qs = model_class.query(plant.pub_id, MoistureReadingModel.created >= start)
    results = [r.json() for r in qs]
    return jsonify({"data": results})
Ejemplo n.º 6
0
def moisture(api_key, plant_id, metric_type):
    if metric_type not in ["moisture", "temperature"]:
        return jsonify({"error": f"unknown metric type {metric_type}"})

    plant_qs = PlantModel.query(api_key.user_pub_id, PlantModel.pub_id.startswith(plant_id))
    plant = list(plant_qs)[0]

    data = request.get_json()
    key = "moisture" if metric_type == "moisture" else "value"
    metric_value = data.get(key, -1)
    reading = plant.add_metric(metric_type, metric_value)

    if key == "moisture":
        water_response = jsonify({
            "water_for": 5,
            "wait_for": 25,
        })
        idle_response = jsonify({
            "water_for": 0,
            "wait_for": 60,
        })

        #  switched to watering
        if metric_value < plant.min_moisture and plant.state == "idling":
            plant.state = "watering"
            plant.save()

        # switched to idling
        if metric_value > plant.max_moisture and plant.state == "watering":
            plant.state = "idling"
            plant.save()

        # if plant.state == "watering":
        #     return water_response
        return idle_response

    return jsonify({
        "water_for": 0,
        "wait_for": 60,
    })
Ejemplo n.º 7
0
def plant_get(token, plant_id):
    plants = PlantModel.query(token.user_pub_id)
    for p in plants:
        if p.pub_id == plant_id:
            return jsonify(p.json())
    return jsonify({})
Ejemplo n.º 8
0
def train(model: PlantModel,
          optimizer,
          criterion,
          lr_scheduler,
          data_loader: DataLoader,
          data_loader_test: DataLoader,
          num_epochs: int = 10,
          use_cuda: bool = True,
          epoch_save_ckpt: Union[int, list] = None,
          dir: str = None):
    """
    Method to train FasterRCNN_SaladFruit model.
    Args:
        data_loader (torch.utils.data.DataLoader): data loader to train model on
        data_loader_test (torch.utils.data.DataLoader): data loader to evaluate model on
        num_epochs (int = 10): number of epoch to train model
        use_cuda (bool = True): use cuda or not
        epoch_save_ckpt (list or int): Epoch at which you want to save the model. If -1 save only last epoch.
        dir (str = "models/): Directory where model are saved under the name "{model_name}_{date}_ep{epoch}.pth"
    """
    if epoch_save_ckpt == -1:
        epoch_save_ckpt = [num_epochs - 1]
    if not dir:
        dir = "checkpoints"
    dir = Path(dir)
    dir.mkdir(parents=True, exist_ok=True)
    # choose device
    device = get_device(use_cuda)
    print(f"Using device {device.type}")
    # define dataset
    model.to(device)
    writer = SummaryWriter("logs")
    metric_logger_train = MetricLogger(delimiter="  ")
    # writer_test = SummaryWriter("runs/test")
    # metric_logger_test = MetricLogger(delimiter="  ", writer=writer_test)

    for epoch in metric_logger_train.log_every(range(num_epochs),
                                               print_freq=1,
                                               epoch=0,
                                               header="Training"):
        # train for one epoch, printing every 50 iterations
        train_metric = train_one_epoch(model,
                                       optimizer,
                                       data_loader,
                                       criterion,
                                       device,
                                       epoch,
                                       print_freq=40)
        # metric_logger_train.update(**train_metric)

        # update the learning rate
        lr_scheduler.step()
        # evaluate on the test dataset
        test_metric = evaluate(model,
                               criterion,
                               data_loader_test,
                               device=device)

        # print results
        print_result_table(train_metric, test_metric)

        # metric_logger_test.update(**test_metric)
        for key in train_metric.keys():
            writer.add_scalars("metrics/{}".format(key), {
                "{}_train".format(key, key): train_metric[key],
                "{}_test".format(key, key): test_metric[key],
            },
                               global_step=epoch)
        # save checkpoint
        if epoch in epoch_save_ckpt:
            save_checkpoint(model, optimizer, dir.as_posix(), epoch)
    writer.close()

    print("That's it!")
Ejemplo n.º 9
0
                                                  shuffle=True,
                                                  num_workers=4)

        data_loader_test = torch.utils.data.DataLoader(dataset_test,
                                                       batch_size=1,
                                                       shuffle=False,
                                                       num_workers=4)

        return data_loader, data_loader_test


if __name__ == '__main__':
    args = parse_args()

    model = PlantModel(backbone_name=args.backbone,
                       pretrained=args.pretrained,
                       num_classes=args.num_classes,
                       layer_freezed=args.layer_freezed)

    # Let's have different LRs for weight and biases for instance
    bias_params, weight_params = [], []
    for n, p in model.named_parameters():
        if n.endswith('.bias'):
            bias_params.append(p)
        else:
            weight_params.append(p)

    # Optimizer
    lr = cfg.LEARNING_RATE
    optimizer = Ranger([
        dict(params=weight_params, lr=lr),
        dict(params=bias_params, lr=lr / 2)