from models.GoogLeNet import * model = GoogLeNet() elif modelName == "ResNet18": from models.ResNet18 import * model = ResNet18() elif modelName == "DenseNet121": from models.DenseNet import * model = DenseNet121() elif modelName == "MobileNet": from models.MobileNet import * model = MobileNet() else: model = None if model is not None: model = model.to(Device) else: raise Exception("model not found") # optimizer and loss function optimizer = optim.SGD(model.parameters(), lr=learning_rate, momentum=momentum, weight_decay=weightDecay) loss_func = torch.nn.CrossEntropyLoss() # use testset to evaluate def evaluateModel(): total_acc = 0.0 total_loss = 0.0 step = 0
cat_samples = train_dataset.images[: 10] # The first images on the list are cats dog_samples = train_dataset.images[ -10:] # The last images on the list are dogs for i in range(cat_samples.shape[0]): tensorboard.add_images("Cat/{}".format(i), None, cat_samples[i:i + 1]) tensorboard.add_images("Dog/{}".format(i), None, dog_samples[i:i + 1]) model = AlexNet(input_size, output_size) cur_epoch = 0 if RESUME_TRAINING: cur_epoch, optimizer_states_dict, loss = load_checkpoint(model) cur_epoch += 1 model = model.to(device) print(model) loss_func = torch.nn.BCELoss(reduction="mean") optimizer = opt(model.parameters(), lr=lr, momentum=momentum) if RESUME_TRAINING: optimizer.load_state_dict(optimizer_states_dict) # Train for epoch in range(cur_epoch, epochs): accumulated_train_loss = [] # Set model in trainng mode model.train() iteration = 0 for batch_x, batch_y in tqdm(train_generator):